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