Annotation of mandoc/validate.c, Revision 1.70
1.70 ! kristaps 1: /* $Id: validate.c,v 1.69 2009/03/06 14:13:47 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
1.56 kristaps 20: #include <ctype.h>
1.69 kristaps 21: #include <stdarg.h>
1.8 kristaps 22: #include <stdlib.h>
1.1 kristaps 23:
24: #include "private.h"
25:
1.67 kristaps 26: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
27:
1.44 kristaps 28: /*
29: * Pre- and post-validate macros as they're parsed. Pre-validation
30: * occurs when the macro has been detected and its arguments parsed.
31: * Post-validation occurs when all child macros have also been parsed.
32: * In the ELEMENT case, this is simply the parameters of the macro; in
33: * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
34: */
35:
1.55 kristaps 36: #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
37: #define POST_ARGS struct mdoc *mdoc
38:
39: typedef int (*v_pre)(PRE_ARGS);
40: typedef int (*v_post)(POST_ARGS);
1.14 kristaps 41:
1.64 kristaps 42: /* TODO: ignoring Pp (it's superfluous in some invocations). */
1.36 kristaps 43:
1.14 kristaps 44: struct valids {
1.24 kristaps 45: v_pre *pre;
1.17 kristaps 46: v_post *post;
1.14 kristaps 47: };
1.1 kristaps 48:
1.37 kristaps 49: /* Utility checks. */
50:
1.55 kristaps 51: static int check_parent(PRE_ARGS, int, enum mdoc_type);
1.69 kristaps 52: static int check_msec(PRE_ARGS, ...);
53: static int check_sec(PRE_ARGS, ...);
1.55 kristaps 54: static int check_stdarg(PRE_ARGS);
55:
56: static int check_text(struct mdoc *,
1.63 kristaps 57: int, int, const char *);
1.68 kristaps 58: static int check_argv(struct mdoc *,
59: const struct mdoc_node *,
60: const struct mdoc_arg *);
1.55 kristaps 61:
1.51 kristaps 62: static int err_child_lt(struct mdoc *, const char *, int);
1.59 kristaps 63: static int warn_child_lt(struct mdoc *, const char *, int);
1.51 kristaps 64: static int err_child_gt(struct mdoc *, const char *, int);
65: static int warn_child_gt(struct mdoc *, const char *, int);
66: static int err_child_eq(struct mdoc *, const char *, int);
67: static int warn_child_eq(struct mdoc *, const char *, int);
68:
69: /* Utility auxiliaries. */
70:
71: static inline int count_child(struct mdoc *);
72: static inline int warn_count(struct mdoc *, const char *,
73: int, const char *, int);
74: static inline int err_count(struct mdoc *, const char *,
75: int, const char *, int);
1.11 kristaps 76:
1.37 kristaps 77: /* Specific pre-child-parse routines. */
78:
1.69 kristaps 79: static int pre_an(PRE_ARGS);
1.55 kristaps 80: static int pre_bd(PRE_ARGS);
81: static int pre_bl(PRE_ARGS);
82: static int pre_cd(PRE_ARGS);
1.69 kristaps 83: static int pre_dd(PRE_ARGS);
84: static int pre_display(PRE_ARGS);
85: static int pre_dt(PRE_ARGS);
1.55 kristaps 86: static int pre_er(PRE_ARGS);
87: static int pre_ex(PRE_ARGS);
1.69 kristaps 88: static int pre_fd(PRE_ARGS);
89: static int pre_it(PRE_ARGS);
90: static int pre_lb(PRE_ARGS);
91: static int pre_os(PRE_ARGS);
92: static int pre_prologue(PRE_ARGS);
1.55 kristaps 93: static int pre_rv(PRE_ARGS);
1.69 kristaps 94: static int pre_sh(PRE_ARGS);
95: static int pre_ss(PRE_ARGS);
1.24 kristaps 96:
1.37 kristaps 97: /* Specific post-child-parse routines. */
98:
1.55 kristaps 99: static int herr_ge1(POST_ARGS);
1.59 kristaps 100: static int hwarn_le1(POST_ARGS);
1.55 kristaps 101: static int herr_eq0(POST_ARGS);
102: static int eerr_eq0(POST_ARGS);
103: static int eerr_le1(POST_ARGS);
104: static int eerr_le2(POST_ARGS);
105: static int eerr_eq1(POST_ARGS);
106: static int eerr_ge1(POST_ARGS);
107: static int ewarn_eq0(POST_ARGS);
108: static int ewarn_eq1(POST_ARGS);
109: static int bwarn_ge1(POST_ARGS);
1.58 kristaps 110: static int hwarn_eq1(POST_ARGS);
1.55 kristaps 111: static int ewarn_ge1(POST_ARGS);
112: static int ebool(POST_ARGS);
113:
1.69 kristaps 114: static int post_an(POST_ARGS);
115: static int post_at(POST_ARGS);
116: static int post_bf(POST_ARGS);
1.55 kristaps 117: static int post_bl(POST_ARGS);
1.69 kristaps 118: static int post_ex(POST_ARGS);
1.55 kristaps 119: static int post_it(POST_ARGS);
120: static int post_nm(POST_ARGS);
121: static int post_root(POST_ARGS);
1.69 kristaps 122: static int post_sh(POST_ARGS);
123: static int post_sh_body(POST_ARGS);
124: static int post_sh_head(POST_ARGS);
125: static int post_st(POST_ARGS);
1.37 kristaps 126:
127: /* Collections of pre-child-parse routines. */
1.17 kristaps 128:
1.69 kristaps 129: static v_pre pres_an[] = { pre_an, NULL };
1.24 kristaps 130: static v_pre pres_bd[] = { pre_display, pre_bd, NULL };
131: static v_pre pres_bl[] = { pre_bl, NULL };
1.33 kristaps 132: static v_pre pres_cd[] = { pre_cd, NULL };
1.69 kristaps 133: static v_pre pres_dd[] = { pre_prologue, pre_dd, NULL };
134: static v_pre pres_d1[] = { pre_display, NULL };
135: static v_pre pres_dt[] = { pre_prologue, pre_dt, NULL };
1.33 kristaps 136: static v_pre pres_er[] = { pre_er, NULL };
137: static v_pre pres_ex[] = { pre_ex, NULL };
1.69 kristaps 138: static v_pre pres_fd[] = { pre_fd, NULL };
139: static v_pre pres_it[] = { pre_it, NULL };
140: static v_pre pres_lb[] = { pre_lb, NULL };
141: static v_pre pres_os[] = { pre_prologue, pre_os, NULL };
1.36 kristaps 142: static v_pre pres_rv[] = { pre_rv, NULL };
1.69 kristaps 143: static v_pre pres_sh[] = { pre_sh, NULL };
144: static v_pre pres_ss[] = { pre_ss, NULL };
1.24 kristaps 145:
1.37 kristaps 146: /* Collections of post-child-parse routines. */
147:
148: static v_post posts_bool[] = { eerr_eq1, ebool, NULL };
149: static v_post posts_bd[] = { herr_eq0, bwarn_ge1, NULL };
150: static v_post posts_text[] = { eerr_ge1, NULL };
151: static v_post posts_wtext[] = { ewarn_ge1, NULL };
152: static v_post posts_notext[] = { eerr_eq0, NULL };
1.43 kristaps 153: static v_post posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
1.37 kristaps 154: static v_post posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
155: static v_post posts_bl[] = { herr_eq0, bwarn_ge1, post_bl, NULL };
1.25 kristaps 156: static v_post posts_it[] = { post_it, NULL };
1.39 kristaps 157: static v_post posts_in[] = { ewarn_eq1, NULL };
1.37 kristaps 158: static v_post posts_ss[] = { herr_ge1, NULL };
1.52 kristaps 159: static v_post posts_pf[] = { eerr_eq1, NULL };
1.69 kristaps 160: static v_post posts_lb[] = { eerr_eq1, NULL };
161: static v_post posts_st[] = { eerr_eq1, post_st, NULL };
1.37 kristaps 162: static v_post posts_pp[] = { ewarn_eq0, NULL };
163: static v_post posts_ex[] = { eerr_le1, post_ex, NULL };
1.35 kristaps 164: static v_post posts_an[] = { post_an, NULL };
1.37 kristaps 165: static v_post posts_at[] = { post_at, NULL };
1.69 kristaps 166: static v_post posts_xr[] = { eerr_ge1, eerr_le2, NULL };
1.37 kristaps 167: static v_post posts_nm[] = { post_nm, NULL };
1.59 kristaps 168: static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
1.45 kristaps 169: static v_post posts_rs[] = { herr_eq0, bwarn_ge1, NULL };
1.58 kristaps 170: static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
1.45 kristaps 171: static v_post posts_bk[] = { herr_eq0, bwarn_ge1, NULL };
1.69 kristaps 172: static v_post posts_fd[] = { ewarn_ge1, NULL };
1.9 kristaps 173:
1.37 kristaps 174: /* Per-macro pre- and post-child-check routine collections. */
1.12 kristaps 175:
1.9 kristaps 176: const struct valids mdoc_valids[MDOC_MAX] = {
1.51 kristaps 177: { NULL, NULL }, /* \" */
1.69 kristaps 178: { pres_dd, posts_text }, /* Dd */
179: { pres_dt, NULL }, /* Dt */
180: { pres_os, NULL }, /* Os */
1.51 kristaps 181: { pres_sh, posts_sh }, /* Sh */
182: { pres_ss, posts_ss }, /* Ss */
183: { NULL, posts_pp }, /* Pp */
184: { pres_d1, posts_wline }, /* D1 */
185: { pres_d1, posts_wline }, /* Dl */
186: { pres_bd, posts_bd }, /* Bd */
187: { NULL, NULL }, /* Ed */
188: { pres_bl, posts_bl }, /* Bl */
189: { NULL, NULL }, /* El */
190: { pres_it, posts_it }, /* It */
191: { NULL, posts_text }, /* Ad */
192: { pres_an, posts_an }, /* An */
193: { NULL, NULL }, /* Ar */
194: { pres_cd, posts_text }, /* Cd */
195: { NULL, NULL }, /* Cm */
196: { NULL, posts_text }, /* Dv */
197: { pres_er, posts_text }, /* Er */
198: { NULL, posts_text }, /* Ev */
199: { pres_ex, posts_ex }, /* Ex */
200: { NULL, posts_text }, /* Fa */
1.69 kristaps 201: { pres_fd, posts_fd }, /* Fd */
1.51 kristaps 202: { NULL, NULL }, /* Fl */
203: { NULL, posts_text }, /* Fn */
204: { NULL, posts_wtext }, /* Ft */
205: { NULL, posts_text }, /* Ic */
206: { NULL, posts_in }, /* In */
207: { NULL, posts_text }, /* Li */
208: { NULL, posts_wtext }, /* Nd */
209: { NULL, posts_nm }, /* Nm */
210: { NULL, posts_wline }, /* Op */
211: { NULL, NULL }, /* Ot */
212: { NULL, NULL }, /* Pa */
213: { pres_rv, posts_notext }, /* Rv */
1.69 kristaps 214: { NULL, posts_st }, /* St */
1.51 kristaps 215: { NULL, posts_text }, /* Va */
216: { NULL, posts_text }, /* Vt */
217: { NULL, posts_xr }, /* Xr */
218: { NULL, posts_text }, /* %A */
219: { NULL, posts_text }, /* %B */
220: { NULL, posts_text }, /* %D */
221: { NULL, posts_text }, /* %I */
222: { NULL, posts_text }, /* %J */
223: { NULL, posts_text }, /* %N */
224: { NULL, posts_text }, /* %O */
225: { NULL, posts_text }, /* %P */
226: { NULL, posts_text }, /* %R */
227: { NULL, posts_text }, /* %T */
228: { NULL, posts_text }, /* %V */
229: { NULL, NULL }, /* Ac */
230: { NULL, NULL }, /* Ao */
231: { NULL, posts_wline }, /* Aq */
232: { NULL, posts_at }, /* At */
233: { NULL, NULL }, /* Bc */
234: { NULL, posts_bf }, /* Bf */
235: { NULL, NULL }, /* Bo */
236: { NULL, posts_wline }, /* Bq */
237: { NULL, NULL }, /* Bsx */
238: { NULL, NULL }, /* Bx */
239: { NULL, posts_bool }, /* Db */
240: { NULL, NULL }, /* Dc */
241: { NULL, NULL }, /* Do */
242: { NULL, posts_wline }, /* Dq */
243: { NULL, NULL }, /* Ec */
244: { NULL, NULL }, /* Ef */
245: { NULL, posts_text }, /* Em */
246: { NULL, NULL }, /* Eo */
247: { NULL, NULL }, /* Fx */
248: { NULL, posts_text }, /* Ms */
249: { NULL, posts_notext }, /* No */
250: { NULL, posts_notext }, /* Ns */
251: { NULL, NULL }, /* Nx */
252: { NULL, NULL }, /* Ox */
253: { NULL, NULL }, /* Pc */
1.52 kristaps 254: { NULL, posts_pf }, /* Pf */
1.51 kristaps 255: { NULL, NULL }, /* Po */
256: { NULL, posts_wline }, /* Pq */
257: { NULL, NULL }, /* Qc */
258: { NULL, posts_wline }, /* Ql */
259: { NULL, NULL }, /* Qo */
260: { NULL, posts_wline }, /* Qq */
261: { NULL, NULL }, /* Re */
262: { NULL, posts_rs }, /* Rs */
263: { NULL, NULL }, /* Sc */
264: { NULL, NULL }, /* So */
265: { NULL, posts_wline }, /* Sq */
266: { NULL, posts_bool }, /* Sm */
267: { NULL, posts_text }, /* Sx */
268: { NULL, posts_text }, /* Sy */
269: { NULL, posts_text }, /* Tn */
270: { NULL, NULL }, /* Ux */
271: { NULL, NULL }, /* Xc */
272: { NULL, NULL }, /* Xo */
273: { NULL, posts_fo }, /* Fo */
274: { NULL, NULL }, /* Fc */
275: { NULL, NULL }, /* Oo */
276: { NULL, NULL }, /* Oc */
277: { NULL, posts_bk }, /* Bk */
278: { NULL, NULL }, /* Ek */
279: { NULL, posts_notext }, /* Bt */
280: { NULL, NULL }, /* Hf */
281: { NULL, NULL }, /* Fr */
282: { NULL, posts_notext }, /* Ud */
1.69 kristaps 283: { pres_lb, posts_lb }, /* Lb */
1.9 kristaps 284: };
1.6 kristaps 285:
286:
1.57 kristaps 287: int
288: mdoc_valid_pre(struct mdoc *mdoc,
289: const struct mdoc_node *node)
290: {
291: v_pre *p;
292: struct mdoc_arg *argv;
1.63 kristaps 293: size_t argc;
294: int line, pos, i, j;
1.57 kristaps 295: const char *tp;
296:
297: if (MDOC_TEXT == node->type) {
298: tp = node->data.text.string;
299: line = node->line;
300: pos = node->pos;
301: return(check_text(mdoc, line, pos, tp));
302: }
303:
304: if (MDOC_BLOCK == node->type || MDOC_ELEM == node->type) {
305: argv = MDOC_BLOCK == node->type ?
306: node->data.block.argv :
307: node->data.elem.argv;
308: argc = MDOC_BLOCK == node->type ?
309: node->data.block.argc :
310: node->data.elem.argc;
311:
1.63 kristaps 312: for (i = 0; i < (int)argc; i++) {
313: for (j = 0; j < (int)argv[i].sz; j++) {
1.57 kristaps 314: tp = argv[i].value[j];
315: line = argv[i].line;
316: pos = argv[i].pos;
317: if ( ! check_text(mdoc, line, pos, tp))
318: return(0);
319: }
1.68 kristaps 320: if ( ! check_argv(mdoc, node, &argv[i]))
321: return(0);
1.57 kristaps 322: }
323: }
324:
325: if (NULL == mdoc_valids[node->tok].pre)
326: return(1);
327: for (p = mdoc_valids[node->tok].pre; *p; p++)
328: if ( ! (*p)(mdoc, node))
329: return(0);
330: return(1);
331: }
332:
333:
334: int
335: mdoc_valid_post(struct mdoc *mdoc)
336: {
337: v_post *p;
338:
339: /*
340: * This check occurs after the macro's children have been filled
341: * in: postfix validation. Since this happens when we're
342: * rewinding the scope tree, it's possible to have multiple
343: * invocations (as by design, for now), we set bit MDOC_VALID to
344: * indicate that we've validated.
345: */
346:
347: if (MDOC_VALID & mdoc->last->flags)
348: return(1);
349: mdoc->last->flags |= MDOC_VALID;
350:
351: if (MDOC_TEXT == mdoc->last->type)
352: return(1);
353: if (MDOC_ROOT == mdoc->last->type)
354: return(post_root(mdoc));
355:
356: if (NULL == mdoc_valids[mdoc->last->tok].post)
357: return(1);
358: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
359: if ( ! (*p)(mdoc))
360: return(0);
361:
362: return(1);
363: }
364:
365:
366:
1.51 kristaps 367: static inline int
368: warn_count(struct mdoc *m, const char *k,
369: int want, const char *v, int has)
370: {
371:
1.55 kristaps 372: return(mdoc_warn(m, WARN_SYNTAX,
1.58 kristaps 373: "suggests %s %s %d (has %d)",
374: v, k, want, has));
1.51 kristaps 375: }
376:
377:
378: static inline int
379: err_count(struct mdoc *m, const char *k,
380: int want, const char *v, int has)
381: {
382:
1.58 kristaps 383: return(mdoc_err(m, "requires %s %s %d (has %d)",
384: v, k, want, has));
1.51 kristaps 385: }
386:
387:
388: static inline int
389: count_child(struct mdoc *mdoc)
1.36 kristaps 390: {
1.51 kristaps 391: int i;
1.36 kristaps 392: struct mdoc_node *n;
393:
394: for (i = 0, n = mdoc->last->child; n; n = n->next, i++)
395: /* Do nothing */ ;
1.53 kristaps 396:
1.36 kristaps 397: return(i);
398: }
399:
400:
1.53 kristaps 401: /*
402: * Build these up with macros because they're basically the same check
403: * for different inequalities. Yes, this could be done with functions,
404: * but this is reasonable for now.
405: */
1.36 kristaps 406:
1.53 kristaps 407: #define CHECK_CHILD_DEFN(lvl, name, ineq) \
408: static int \
409: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
410: { \
411: int i; \
412: if ((i = count_child(mdoc)) ineq sz) \
413: return(1); \
414: return(lvl##_count(mdoc, #ineq, sz, p, i)); \
415: }
416:
417: #define CHECK_BODY_DEFN(name, lvl, func, num) \
418: static int \
1.55 kristaps 419: b##lvl##_##name(POST_ARGS) \
1.53 kristaps 420: { \
421: if (MDOC_BODY != mdoc->last->type) \
422: return(1); \
423: return(func(mdoc, "multiline parameters", (num))); \
424: }
425:
426: #define CHECK_ELEM_DEFN(name, lvl, func, num) \
427: static int \
1.55 kristaps 428: e##lvl##_##name(POST_ARGS) \
1.53 kristaps 429: { \
430: assert(MDOC_ELEM == mdoc->last->type); \
431: return(func(mdoc, "line parameters", (num))); \
432: }
433:
434: #define CHECK_HEAD_DEFN(name, lvl, func, num) \
435: static int \
1.55 kristaps 436: h##lvl##_##name(POST_ARGS) \
1.53 kristaps 437: { \
438: if (MDOC_HEAD != mdoc->last->type) \
439: return(1); \
1.58 kristaps 440: return(func(mdoc, "line parameters", (num))); \
1.36 kristaps 441: }
442:
443:
1.53 kristaps 444: CHECK_CHILD_DEFN(warn, gt, >) /* warn_child_gt() */
445: CHECK_CHILD_DEFN(err, gt, >) /* err_child_gt() */
446: CHECK_CHILD_DEFN(warn, eq, ==) /* warn_child_eq() */
447: CHECK_CHILD_DEFN(err, eq, ==) /* err_child_eq() */
448: CHECK_CHILD_DEFN(err, lt, <) /* err_child_lt() */
1.59 kristaps 449: CHECK_CHILD_DEFN(warn, lt, <) /* warn_child_lt() */
1.53 kristaps 450: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0) /* bwarn_ge1() */
451: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1) /* ewarn_eq1() */
452: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0) /* ewarn_eq0() */
453: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0) /* ewarn_gt1() */
454: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1) /* eerr_eq1() */
455: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3) /* eerr_le2() */
456: CHECK_ELEM_DEFN(le1, err, err_child_lt, 2) /* eerr_le1() */
457: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0) /* eerr_eq0() */
458: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0) /* eerr_ge1() */
459: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0) /* herr_eq0() */
1.59 kristaps 460: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2) /* hwarn_le1() */
1.53 kristaps 461: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0) /* herr_ge1() */
1.58 kristaps 462: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1) /* hwarn_eq1() */
1.36 kristaps 463:
464:
465: static int
1.55 kristaps 466: check_stdarg(PRE_ARGS)
1.36 kristaps 467: {
468:
1.55 kristaps 469: if (MDOC_Std == n->data.elem.argv[0].arg &&
470: 1 == n->data.elem.argc)
1.36 kristaps 471: return(1);
1.51 kristaps 472:
1.55 kristaps 473: return(mdoc_nwarn(mdoc, n, WARN_COMPAT,
1.53 kristaps 474: "one argument suggested"));
1.36 kristaps 475: }
476:
477:
478: static int
1.69 kristaps 479: check_sec(PRE_ARGS, ...)
480: {
481: enum mdoc_sec sec;
482: va_list ap;
483:
484: va_start(ap, n);
485:
486: for (;;) {
1.70 ! kristaps 487: sec = (enum mdoc_sec)va_arg(ap, int);
! 488: if (SEC_CUSTOM == sec)
1.69 kristaps 489: break;
490: if (sec != mdoc->lastsec)
491: continue;
492: va_end(ap);
493: return(1);
494: }
495:
496: va_end(ap);
497: return(mdoc_nwarn(mdoc, n, WARN_COMPAT,
498: "inappropriate document section for macro"));
499: }
500:
501:
502: static int
503: check_msec(PRE_ARGS, ...)
1.33 kristaps 504: {
1.69 kristaps 505: va_list ap;
506: int msec;
1.33 kristaps 507:
1.69 kristaps 508: va_start(ap, n);
509: for (;;) {
510: if (0 == (msec = va_arg(ap, int)))
511: break;
512: if (msec != mdoc->meta.msec)
513: continue;
514: va_end(ap);
515: return(1);
516: }
517:
518: va_end(ap);
1.55 kristaps 519: return(mdoc_nwarn(mdoc, n, WARN_COMPAT,
1.69 kristaps 520: "inappropriate manual section for macro"));
1.55 kristaps 521: }
522:
523:
1.68 kristaps 524: /*
525: * Check over an argument. When this has more stuff in it, make this
526: * into a table-driven function; until then, a switch is fine.
527: */
528: static int
529: check_argv(struct mdoc *mdoc,
530: const struct mdoc_node *node,
531: const struct mdoc_arg *argv)
532: {
533:
534:
535: switch (argv->arg) {
536: case (MDOC_Std):
537: switch (node->tok) {
538: case (MDOC_Ex):
539: /*
540: * If the -std does not have an argument, then
541: * set it with the default name (if set). This
542: * only happens with MDOC_Ex.
543: */
544: if (1 == argv->sz)
545: return(1);
546: assert(0 == argv->sz);
547: if (mdoc->meta.name)
548: return(1);
549: return(mdoc_nerr(mdoc, node,
550: "default name not yet set"));
551: default:
552: break;
553: }
554: break;
555: default:
556: break;
557: }
558:
559: return(1);
560: }
561:
562:
1.55 kristaps 563: static int
1.63 kristaps 564: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
1.55 kristaps 565: {
566: size_t c;
567:
1.62 kristaps 568: /* XXX - indicate deprecated escapes \*(xx and \*x. */
569:
1.55 kristaps 570: for ( ; *p; p++) {
1.68 kristaps 571: if ( ! isprint((u_char)*p) && '\t' != *p)
1.56 kristaps 572: return(mdoc_perr(mdoc, line, pos,
1.66 kristaps 573: "invalid non-printing characters"));
1.55 kristaps 574: if ('\\' != *p)
575: continue;
576: if ((c = mdoc_isescape(p))) {
1.63 kristaps 577: p += (int)c - 1;
1.55 kristaps 578: continue;
579: }
1.65 kristaps 580: return(mdoc_perr(mdoc, line, pos,
581: "invalid escape sequence"));
1.55 kristaps 582: }
583:
584: return(1);
1.14 kristaps 585: }
586:
587:
1.55 kristaps 588:
589:
1.14 kristaps 590: static int
1.55 kristaps 591: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
1.54 kristaps 592: {
593:
594: assert(n->parent);
595: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
596: (t == n->parent->type))
597: return(1);
598:
599: return(mdoc_nerr(mdoc, n, "require parent %s",
600: MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
601: }
602:
603:
604:
605: static int
1.55 kristaps 606: pre_display(PRE_ARGS)
1.23 kristaps 607: {
1.55 kristaps 608: struct mdoc_node *node;
1.23 kristaps 609:
1.53 kristaps 610: /* Display elements (`Bd', `D1'...) cannot be nested. */
611:
1.55 kristaps 612: if (MDOC_BLOCK != n->type)
1.24 kristaps 613: return(1);
614:
1.38 kristaps 615: /* LINTED */
1.55 kristaps 616: for (node = mdoc->last->parent; node; node = node->parent)
617: if (MDOC_BLOCK == node->type)
618: if (MDOC_Bd == node->tok)
1.23 kristaps 619: break;
1.55 kristaps 620: if (NULL == node)
1.23 kristaps 621: return(1);
1.53 kristaps 622:
1.55 kristaps 623: return(mdoc_nerr(mdoc, n, "displays may not be nested"));
1.23 kristaps 624: }
625:
626:
627: static int
1.55 kristaps 628: pre_bl(PRE_ARGS)
1.24 kristaps 629: {
1.61 kristaps 630: int type, i, width, offset;
1.24 kristaps 631: struct mdoc_arg *argv;
1.53 kristaps 632: size_t argc;
1.24 kristaps 633:
1.55 kristaps 634: if (MDOC_BLOCK != n->type)
1.24 kristaps 635: return(1);
636:
1.55 kristaps 637: argc = n->data.block.argc;
1.24 kristaps 638:
1.53 kristaps 639: /* Make sure that only one type of list is specified. */
640:
1.61 kristaps 641: type = offset = width = -1;
642:
1.38 kristaps 643: /* LINTED */
1.61 kristaps 644: for (i = 0; i < (int)argc; i++) {
1.55 kristaps 645: argv = &n->data.block.argv[i];
1.53 kristaps 646:
1.24 kristaps 647: switch (argv->arg) {
648: case (MDOC_Bullet):
649: /* FALLTHROUGH */
650: case (MDOC_Dash):
651: /* FALLTHROUGH */
652: case (MDOC_Enum):
653: /* FALLTHROUGH */
654: case (MDOC_Hyphen):
655: /* FALLTHROUGH */
656: case (MDOC_Item):
657: /* FALLTHROUGH */
658: case (MDOC_Tag):
659: /* FALLTHROUGH */
660: case (MDOC_Diag):
661: /* FALLTHROUGH */
662: case (MDOC_Hang):
663: /* FALLTHROUGH */
664: case (MDOC_Ohang):
665: /* FALLTHROUGH */
666: case (MDOC_Inset):
1.26 kristaps 667: /* FALLTHROUGH */
668: case (MDOC_Column):
1.61 kristaps 669: if (-1 == type) {
670: type = argv->arg;
1.53 kristaps 671: break;
1.61 kristaps 672: }
1.53 kristaps 673: return(mdoc_perr(mdoc, argv->line, argv->pos,
674: "multiple types specified"));
1.61 kristaps 675: case (MDOC_Width):
676: if (-1 == width) {
677: width = argv->arg;
678: break;
679: }
680: return(mdoc_perr(mdoc, argv->line, argv->pos,
681: "multiple -%s arguments",
682: mdoc_argnames[MDOC_Width]));
683: case (MDOC_Offset):
684: if (-1 == offset) {
685: offset = argv->arg;
686: break;
687: }
688: return(mdoc_perr(mdoc, argv->line, argv->pos,
689: "multiple -%s arguments",
690: mdoc_argnames[MDOC_Offset]));
1.24 kristaps 691: default:
692: break;
693: }
694: }
1.53 kristaps 695:
1.61 kristaps 696: if (-1 == type)
697: return(mdoc_err(mdoc, "no type specified"));
698:
699: switch (type) {
700: case (MDOC_Column):
701: /* FALLTHROUGH */
702: case (MDOC_Diag):
703: /* FALLTHROUGH */
704: case (MDOC_Inset):
705: /* FALLTHROUGH */
706: case (MDOC_Item):
707: if (-1 == width)
708: break;
709: return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
710: "superfluous -%s argument",
711: mdoc_argnames[MDOC_Width]));
712: case (MDOC_Tag):
1.64 kristaps 713: if (-1 == width && ! mdoc_nwarn(mdoc, n, WARN_SYNTAX,
714: "suggest -%s argument",
715: mdoc_argnames[MDOC_Width]))
716: return(0);
717: break;
1.61 kristaps 718: default:
719: break;
720: }
721:
722: return(1);
1.24 kristaps 723: }
724:
725:
726: static int
1.55 kristaps 727: pre_bd(PRE_ARGS)
1.24 kristaps 728: {
1.53 kristaps 729: int type, err, i;
1.24 kristaps 730: struct mdoc_arg *argv;
1.53 kristaps 731: size_t argc;
1.24 kristaps 732:
1.55 kristaps 733: if (MDOC_BLOCK != n->type)
1.24 kristaps 734: return(1);
735:
1.55 kristaps 736: argc = n->data.block.argc;
1.24 kristaps 737:
1.53 kristaps 738: /* Make sure that only one type of display is specified. */
739:
1.38 kristaps 740: /* LINTED */
1.53 kristaps 741: for (i = 0, err = type = 0; ! err && i < (int)argc; i++) {
1.55 kristaps 742: argv = &n->data.block.argv[i];
1.53 kristaps 743:
1.24 kristaps 744: switch (argv->arg) {
745: case (MDOC_Ragged):
746: /* FALLTHROUGH */
747: case (MDOC_Unfilled):
748: /* FALLTHROUGH */
1.29 kristaps 749: case (MDOC_Filled):
750: /* FALLTHROUGH */
1.24 kristaps 751: case (MDOC_Literal):
752: /* FALLTHROUGH */
753: case (MDOC_File):
1.53 kristaps 754: if (0 == type++)
755: break;
756: return(mdoc_perr(mdoc, argv->line, argv->pos,
757: "multiple types specified"));
1.24 kristaps 758: default:
759: break;
760: }
761: }
1.53 kristaps 762:
763: if (type)
764: return(1);
765: return(mdoc_err(mdoc, "no type specified"));
1.24 kristaps 766: }
767:
768:
769: static int
1.55 kristaps 770: pre_ss(PRE_ARGS)
1.33 kristaps 771: {
772:
1.55 kristaps 773: if (MDOC_BLOCK != n->type)
1.33 kristaps 774: return(1);
1.55 kristaps 775: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
1.33 kristaps 776: }
777:
778:
779: static int
1.55 kristaps 780: pre_sh(PRE_ARGS)
1.33 kristaps 781: {
782:
1.55 kristaps 783: if (MDOC_BLOCK != n->type)
1.33 kristaps 784: return(1);
1.55 kristaps 785: return(check_parent(mdoc, n, -1, MDOC_ROOT));
1.33 kristaps 786: }
787:
788:
789: static int
1.55 kristaps 790: pre_it(PRE_ARGS)
1.53 kristaps 791: {
792:
1.55 kristaps 793: if (MDOC_BLOCK != n->type)
1.53 kristaps 794: return(1);
1.55 kristaps 795: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
1.53 kristaps 796: }
797:
798:
799: static int
1.69 kristaps 800: pre_an(PRE_ARGS)
1.36 kristaps 801: {
802:
1.69 kristaps 803: if (1 >= n->data.elem.argc)
1.36 kristaps 804: return(1);
1.69 kristaps 805: return(mdoc_nerr(mdoc, n, "one argument allowed"));
1.36 kristaps 806: }
807:
808:
809: static int
1.69 kristaps 810: pre_lb(PRE_ARGS)
1.35 kristaps 811: {
1.36 kristaps 812:
1.69 kristaps 813: return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
1.35 kristaps 814: }
815:
816:
817: static int
1.55 kristaps 818: pre_rv(PRE_ARGS)
1.36 kristaps 819: {
820:
1.69 kristaps 821: if ( ! check_msec(mdoc, n, 2, 3, 0))
1.36 kristaps 822: return(0);
1.55 kristaps 823: return(check_stdarg(mdoc, n));
1.36 kristaps 824: }
825:
826:
827: static int
1.55 kristaps 828: pre_ex(PRE_ARGS)
1.33 kristaps 829: {
1.35 kristaps 830:
1.69 kristaps 831: if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
1.35 kristaps 832: return(0);
1.55 kristaps 833: return(check_stdarg(mdoc, n));
1.33 kristaps 834: }
835:
836:
837: static int
1.55 kristaps 838: pre_er(PRE_ARGS)
1.33 kristaps 839: {
840:
1.69 kristaps 841: return(check_msec(mdoc, n, 2, 0));
1.33 kristaps 842: }
843:
844:
845: static int
1.55 kristaps 846: pre_cd(PRE_ARGS)
1.33 kristaps 847: {
848:
1.69 kristaps 849: return(check_msec(mdoc, n, 4, 0));
1.33 kristaps 850: }
851:
852:
853: static int
1.55 kristaps 854: pre_prologue(PRE_ARGS)
1.20 kristaps 855: {
856:
1.69 kristaps 857: return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
858: }
1.20 kristaps 859:
860:
1.69 kristaps 861: static int
862: pre_dt(PRE_ARGS)
863: {
864:
865: if (0 == mdoc->meta.date || mdoc->meta.os)
866: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
867: "out-of-order prologue"))
868: return(0);
869: if (mdoc->meta.title)
870: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
871: "prologue re-invoked"))
872: return(0);
873: return(1);
874: }
875:
876:
877: static int
878: pre_os(PRE_ARGS)
879: {
880:
881: if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
882: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
883: "out-of-order prologue"))
884: return(0);
885: if (mdoc->meta.os)
886: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
887: "prologue re-invoked"))
888: return(0);
889: return(1);
890: }
1.20 kristaps 891:
892:
1.69 kristaps 893: static int
894: pre_dd(PRE_ARGS)
895: {
1.20 kristaps 896:
1.69 kristaps 897: if (mdoc->meta.title || mdoc->meta.os)
898: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
899: "out-of-order prologue"))
900: return(0);
901: if (mdoc->meta.date)
902: if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
903: "prologue re-invoked"))
904: return(0);
905: return(1);
1.20 kristaps 906: }
907:
908:
1.35 kristaps 909: static int
1.55 kristaps 910: post_bf(POST_ARGS)
1.41 kristaps 911: {
912: char *p;
913: struct mdoc_node *head;
914:
915: if (MDOC_BLOCK != mdoc->last->type)
916: return(1);
1.53 kristaps 917:
1.41 kristaps 918: head = mdoc->last->data.block.head;
919:
920: if (0 == mdoc->last->data.block.argc) {
1.53 kristaps 921: if (NULL == head->child)
922: return(mdoc_err(mdoc, "argument expected"));
923:
924: p = head->child->data.text.string;
925: if (xstrcmp(p, "Em"))
926: return(1);
927: else if (xstrcmp(p, "Li"))
928: return(1);
929: else if (xstrcmp(p, "Sm"))
930: return(1);
931: return(mdoc_nerr(mdoc, head->child, "invalid font"));
1.41 kristaps 932: }
1.53 kristaps 933:
1.41 kristaps 934: if (head->child)
1.53 kristaps 935: return(mdoc_err(mdoc, "argument expected"));
936:
1.41 kristaps 937: if (1 == mdoc->last->data.block.argc)
938: return(1);
1.53 kristaps 939: return(mdoc_err(mdoc, "argument expected"));
1.41 kristaps 940: }
941:
942:
943: static int
1.55 kristaps 944: post_nm(POST_ARGS)
1.37 kristaps 945: {
946:
947: if (mdoc->last->child)
948: return(1);
949: if (mdoc->meta.name)
950: return(1);
1.53 kristaps 951: return(mdoc_err(mdoc, "not yet invoked with name"));
1.37 kristaps 952: }
953:
954:
955: static int
1.55 kristaps 956: post_at(POST_ARGS)
1.36 kristaps 957: {
958:
1.37 kristaps 959: if (NULL == mdoc->last->child)
960: return(1);
1.69 kristaps 961: if (mdoc_a2att(mdoc->last->child->data.text.string))
1.36 kristaps 962: return(1);
1.69 kristaps 963: return(mdoc_err(mdoc, "require valid AT&T symbol"));
1.36 kristaps 964: }
965:
966:
967: static int
1.55 kristaps 968: post_an(POST_ARGS)
1.35 kristaps 969: {
970:
971: if (0 != mdoc->last->data.elem.argc) {
972: if (NULL == mdoc->last->child)
973: return(1);
1.53 kristaps 974: return(mdoc_err(mdoc, "argument(s) expected"));
1.35 kristaps 975: }
976:
977: if (mdoc->last->child)
978: return(1);
1.53 kristaps 979: return(mdoc_err(mdoc, "argument(s) expected"));
1.35 kristaps 980: }
981:
982:
983: static int
1.55 kristaps 984: post_ex(POST_ARGS)
1.35 kristaps 985: {
986:
987: if (0 == mdoc->last->data.elem.argc) {
988: if (mdoc->last->child)
989: return(1);
1.53 kristaps 990: return(mdoc_err(mdoc, "argument(s) expected"));
1.35 kristaps 991: }
992: if (mdoc->last->child)
1.53 kristaps 993: return(mdoc_err(mdoc, "argument(s) expected"));
1.35 kristaps 994: if (1 != mdoc->last->data.elem.argc)
1.53 kristaps 995: return(mdoc_err(mdoc, "argument(s) expected"));
1.35 kristaps 996: if (MDOC_Std != mdoc->last->data.elem.argv[0].arg)
1.53 kristaps 997: return(mdoc_err(mdoc, "argument(s) expected"));
998:
1.35 kristaps 999: return(1);
1000: }
1001:
1002:
1.25 kristaps 1003: static int
1.55 kristaps 1004: post_it(POST_ARGS)
1.25 kristaps 1005: {
1.53 kristaps 1006: int type, sv, i;
1.25 kristaps 1007: #define TYPE_NONE (0)
1008: #define TYPE_BODY (1)
1009: #define TYPE_HEAD (2)
1.47 kristaps 1010: #define TYPE_OHEAD (3)
1.53 kristaps 1011: size_t argc;
1.25 kristaps 1012: struct mdoc_node *n;
1013:
1014: if (MDOC_BLOCK != mdoc->last->type)
1015: return(1);
1016:
1.53 kristaps 1017: n = mdoc->last->parent->parent;
1.25 kristaps 1018:
1019: argc = n->data.block.argc;
1020: type = TYPE_NONE;
1.38 kristaps 1021: sv = -1;
1.26 kristaps 1022:
1023: /* Some types require block-head, some not. */
1.25 kristaps 1024:
1.38 kristaps 1025: /* LINTED */
1.53 kristaps 1026: for (i = 0; TYPE_NONE == type && i < (int)argc; i++)
1027: switch (n->data.block.argv[i].arg) {
1.25 kristaps 1028: case (MDOC_Tag):
1029: /* FALLTHROUGH */
1030: case (MDOC_Diag):
1031: /* FALLTHROUGH */
1032: case (MDOC_Hang):
1033: /* FALLTHROUGH */
1034: case (MDOC_Ohang):
1035: /* FALLTHROUGH */
1036: case (MDOC_Inset):
1037: type = TYPE_HEAD;
1.53 kristaps 1038: sv = n->data.block.argv[i].arg;
1.25 kristaps 1039: break;
1040: case (MDOC_Bullet):
1041: /* FALLTHROUGH */
1042: case (MDOC_Dash):
1043: /* FALLTHROUGH */
1044: case (MDOC_Enum):
1045: /* FALLTHROUGH */
1046: case (MDOC_Hyphen):
1047: /* FALLTHROUGH */
1048: case (MDOC_Item):
1.47 kristaps 1049: type = TYPE_BODY;
1.53 kristaps 1050: sv = n->data.block.argv[i].arg;
1.47 kristaps 1051: break;
1.25 kristaps 1052: case (MDOC_Column):
1.47 kristaps 1053: type = TYPE_OHEAD;
1.53 kristaps 1054: sv = n->data.block.argv[i].arg;
1.25 kristaps 1055: break;
1056: default:
1057: break;
1058: }
1059:
1060: assert(TYPE_NONE != type);
1061:
1.47 kristaps 1062: n = mdoc->last->data.block.head;
1063:
1.25 kristaps 1064: if (TYPE_HEAD == type) {
1.33 kristaps 1065: if (NULL == n->child)
1.53 kristaps 1066: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1067: "argument(s) suggested"))
1.25 kristaps 1068: return(0);
1069:
1.33 kristaps 1070: n = mdoc->last->data.block.body;
1071: if (NULL == n->child)
1.53 kristaps 1072: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1073: "multiline body suggested"))
1.25 kristaps 1074: return(0);
1075:
1.47 kristaps 1076: } else if (TYPE_BODY == type) {
1077: if (n->child)
1.53 kristaps 1078: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1079: "no argument suggested"))
1.47 kristaps 1080: return(0);
1081:
1082: n = mdoc->last->data.block.body;
1083: if (NULL == n->child)
1.53 kristaps 1084: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1085: "multiline body suggested"))
1.47 kristaps 1086: return(0);
1087: } else {
1088: if (NULL == n->child)
1.53 kristaps 1089: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1090: "argument(s) suggested"))
1.47 kristaps 1091: return(0);
1092:
1093: n = mdoc->last->data.block.body;
1094: if (n->child)
1.53 kristaps 1095: if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1096: "no multiline body suggested"))
1.47 kristaps 1097: return(0);
1.25 kristaps 1098: }
1099:
1.47 kristaps 1100: if (MDOC_Column != sv)
1.26 kristaps 1101: return(1);
1102:
1.38 kristaps 1103: argc = mdoc->last->parent->parent->data.block.argv->sz;
1.26 kristaps 1104: n = mdoc->last->data.block.head->child;
1.25 kristaps 1105:
1.26 kristaps 1106: for (i = 0; n; n = n->next)
1107: i++;
1108:
1.53 kristaps 1109: if (i == (int)argc)
1.26 kristaps 1110: return(1);
1.53 kristaps 1111:
1112: return(mdoc_err(mdoc, "need %zu columns (have %d)", argc, i));
1.25 kristaps 1113: #undef TYPE_NONE
1114: #undef TYPE_BODY
1115: #undef TYPE_HEAD
1.47 kristaps 1116: #undef TYPE_OHEAD
1.25 kristaps 1117: }
1118:
1119:
1.24 kristaps 1120: static int
1.55 kristaps 1121: post_bl(POST_ARGS)
1.24 kristaps 1122: {
1.53 kristaps 1123: struct mdoc_node *n;
1.24 kristaps 1124:
1125: if (MDOC_BODY != mdoc->last->type)
1126: return(1);
1.64 kristaps 1127: if (NULL == (mdoc->last->child))
1128: return(1);
1129:
1130: /*
1131: * Only allow `It' macros to be the immediate descendants of the
1132: * `Bl' list.
1133: */
1.24 kristaps 1134:
1.38 kristaps 1135: /* LINTED */
1.24 kristaps 1136: for (n = mdoc->last->child; n; n = n->next) {
1137: if (MDOC_BLOCK == n->type)
1.25 kristaps 1138: if (MDOC_It == n->tok)
1.24 kristaps 1139: continue;
1.64 kristaps 1140:
1141: return(mdoc_nerr(mdoc, n, "bad child of parent %s",
1142: mdoc_macronames[mdoc->last->tok]));
1.24 kristaps 1143: }
1.53 kristaps 1144:
1.64 kristaps 1145: return(1);
1.24 kristaps 1146: }
1147:
1148:
1.34 kristaps 1149: static int
1.37 kristaps 1150: ebool(struct mdoc *mdoc)
1.34 kristaps 1151: {
1152: struct mdoc_node *n;
1153:
1.38 kristaps 1154: /* LINTED */
1.34 kristaps 1155: for (n = mdoc->last->child; n; n = n->next) {
1156: if (MDOC_TEXT != n->type)
1157: break;
1158: if (xstrcmp(n->data.text.string, "on"))
1159: continue;
1160: if (xstrcmp(n->data.text.string, "off"))
1161: continue;
1162: break;
1163: }
1.53 kristaps 1164:
1.34 kristaps 1165: if (NULL == n)
1166: return(1);
1.53 kristaps 1167: return(mdoc_nerr(mdoc, n, "expected boolean"));
1.37 kristaps 1168: }
1169:
1170:
1171: static int
1.55 kristaps 1172: post_root(POST_ARGS)
1.37 kristaps 1173: {
1174:
1.46 kristaps 1175: if (NULL == mdoc->first->child)
1.53 kristaps 1176: return(mdoc_err(mdoc, "document lacks data"));
1.46 kristaps 1177: if (SEC_PROLOGUE == mdoc->lastnamed)
1.53 kristaps 1178: return(mdoc_err(mdoc, "document lacks prologue"));
1179:
1.46 kristaps 1180: if (MDOC_BLOCK != mdoc->first->child->type)
1.54 kristaps 1181: return(mdoc_err(mdoc, "lacking post-prologue %s",
1.53 kristaps 1182: mdoc_macronames[MDOC_Sh]));
1.46 kristaps 1183: if (MDOC_Sh != mdoc->first->child->tok)
1.54 kristaps 1184: return(mdoc_err(mdoc, "lacking post-prologue %s",
1.53 kristaps 1185: mdoc_macronames[MDOC_Sh]));
1186:
1.37 kristaps 1187: return(1);
1.34 kristaps 1188: }
1189:
1190:
1.20 kristaps 1191: static int
1.69 kristaps 1192: post_st(POST_ARGS)
1193: {
1194:
1195: if (mdoc_a2st(mdoc->last->child->data.text.string))
1196: return(1);
1197:
1198: return(mdoc_warn(mdoc, WARN_SYNTAX, "invalid standard"));
1199: }
1200:
1201:
1202: static int
1.55 kristaps 1203: post_sh(POST_ARGS)
1.14 kristaps 1204: {
1.46 kristaps 1205:
1206: if (MDOC_HEAD == mdoc->last->type)
1207: return(post_sh_head(mdoc));
1208: if (MDOC_BODY == mdoc->last->type)
1209: return(post_sh_body(mdoc));
1.53 kristaps 1210:
1.46 kristaps 1211: return(1);
1212: }
1213:
1214:
1215: static int
1.55 kristaps 1216: post_sh_body(POST_ARGS)
1.46 kristaps 1217: {
1218: struct mdoc_node *n;
1219:
1220: if (SEC_NAME != mdoc->lastnamed)
1221: return(1);
1222:
1.51 kristaps 1223: /*
1224: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1225: * macros (can have multiple `Nm' and one `Nd'). Note that the
1226: * children of the BODY declaration can also be "text".
1227: */
1228:
1.46 kristaps 1229: if (NULL == (n = mdoc->last->child))
1.54 kristaps 1230: return(mdoc_warn(mdoc, WARN_SYNTAX,
1231: "section should have %s and %s",
1.51 kristaps 1232: mdoc_macronames[MDOC_Nm],
1233: mdoc_macronames[MDOC_Nd]));
1234:
1235: for ( ; n && n->next; n = n->next) {
1236: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1237: continue;
1238: if (MDOC_TEXT == n->type)
1239: continue;
1.54 kristaps 1240: if ( ! (mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1241: "section should have %s first",
1.51 kristaps 1242: mdoc_macronames[MDOC_Nm])))
1243: return(0);
1244: }
1245:
1246: if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1.46 kristaps 1247: return(1);
1248:
1.54 kristaps 1249: return(mdoc_warn(mdoc, WARN_SYNTAX,
1250: "section should have %s last",
1.51 kristaps 1251: mdoc_macronames[MDOC_Nd]));
1.46 kristaps 1252: }
1253:
1254:
1255: static int
1.55 kristaps 1256: post_sh_head(POST_ARGS)
1.46 kristaps 1257: {
1.36 kristaps 1258: char buf[64];
1.21 kristaps 1259: enum mdoc_sec sec;
1260:
1.69 kristaps 1261: /*
1262: * Process a new section. Sections are either "named" or
1263: * "custom"; custom sections are user-defined, while named ones
1264: * usually follow a conventional order and may only appear in
1265: * certain manual sections.
1266: */
1267:
1.25 kristaps 1268: assert(MDOC_Sh == mdoc->last->tok);
1.21 kristaps 1269:
1.69 kristaps 1270: (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.14 kristaps 1271:
1.46 kristaps 1272: sec = mdoc_atosec(buf);
1273:
1.69 kristaps 1274: /* The NAME section should always be first. */
1275:
1.46 kristaps 1276: if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1.54 kristaps 1277: return(mdoc_warn(mdoc, WARN_SYNTAX,
1278: "section NAME should be first"));
1.46 kristaps 1279: if (SEC_CUSTOM == sec)
1.21 kristaps 1280: return(1);
1.69 kristaps 1281:
1282: /* Check for repeated or out-of-order sections. */
1283:
1.46 kristaps 1284: if (sec == mdoc->lastnamed)
1.54 kristaps 1285: return(mdoc_warn(mdoc, WARN_SYNTAX,
1286: "section repeated"));
1.46 kristaps 1287: if (sec < mdoc->lastnamed)
1.54 kristaps 1288: return(mdoc_warn(mdoc, WARN_SYNTAX,
1.69 kristaps 1289: "section out of conventional order"));
1290:
1291: /* Check particular section/manual section conventions. */
1292:
1293: switch (sec) {
1294: case (SEC_LIBRARY):
1295: switch (mdoc->meta.msec) {
1296: case (2):
1297: /* FALLTHROUGH */
1298: case (3):
1299: break;
1300: default:
1301: return(mdoc_warn(mdoc, WARN_COMPAT,
1302: "section in wrong manual section"));
1303: }
1304: break;
1305: default:
1306: break;
1307: }
1.46 kristaps 1308:
1309: return(1);
1.11 kristaps 1310: }
1311:
1312:
1.57 kristaps 1313: static int
1.69 kristaps 1314: pre_fd(PRE_ARGS)
1.11 kristaps 1315: {
1.39 kristaps 1316:
1.69 kristaps 1317: return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1.11 kristaps 1318: }
CVSweb