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