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