Annotation of mandoc/validate.c, Revision 1.85
1.85 ! kristaps 1: /* $Id: validate.c,v 1.84 2009/03/16 22:19:19 kristaps Exp $ */
1.1 kristaps 2: /*
1.85 ! kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1 kristaps 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.85 ! kristaps 318: { NULL, NULL }, /* Dx */
1.9 kristaps 319: };
1.6 kristaps 320:
321:
1.57 kristaps 322: int
323: mdoc_valid_pre(struct mdoc *mdoc,
1.84 kristaps 324: const struct mdoc_node *n)
1.57 kristaps 325: {
326: v_pre *p;
1.71 kristaps 327: int line, pos;
1.57 kristaps 328: const char *tp;
329:
1.84 kristaps 330: if (MDOC_TEXT == n->type) {
331: tp = n->string;
332: line = n->line;
333: pos = n->pos;
1.57 kristaps 334: return(check_text(mdoc, line, pos, tp));
335: }
336:
1.84 kristaps 337: if ( ! check_args(mdoc, n))
1.71 kristaps 338: return(0);
1.84 kristaps 339: if (NULL == mdoc_valids[n->tok].pre)
1.57 kristaps 340: return(1);
1.84 kristaps 341: for (p = mdoc_valids[n->tok].pre; *p; p++)
342: if ( ! (*p)(mdoc, n))
1.57 kristaps 343: return(0);
344: return(1);
345: }
346:
347:
348: int
349: mdoc_valid_post(struct mdoc *mdoc)
350: {
351: v_post *p;
352:
353: /*
354: * This check occurs after the macro's children have been filled
355: * in: postfix validation. Since this happens when we're
356: * rewinding the scope tree, it's possible to have multiple
357: * invocations (as by design, for now), we set bit MDOC_VALID to
358: * indicate that we've validated.
359: */
360:
361: if (MDOC_VALID & mdoc->last->flags)
362: return(1);
363: mdoc->last->flags |= MDOC_VALID;
364:
365: if (MDOC_TEXT == mdoc->last->type)
366: return(1);
367: if (MDOC_ROOT == mdoc->last->type)
368: return(post_root(mdoc));
369:
370: if (NULL == mdoc_valids[mdoc->last->tok].post)
371: return(1);
372: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
373: if ( ! (*p)(mdoc))
374: return(0);
375:
376: return(1);
377: }
378:
379:
1.71 kristaps 380: static int
1.84 kristaps 381: perr(struct mdoc *m, int line, int pos, enum merr type)
1.71 kristaps 382: {
383: char *p;
384:
385: p = NULL;
386: switch (type) {
1.84 kristaps 387: case (EPRINT):
388: p = "invalid character";
389: break;
1.71 kristaps 390: case (ENESTDISP):
391: p = "displays may not be nested";
392: break;
393: case (EBOOL):
394: p = "expected boolean value";
395: break;
396: case (EARGREP):
397: p = "argument repeated";
398: break;
399: case (EMULTIDISP):
400: p = "multiple display types specified";
401: break;
402: case (EMULTILIST):
403: p = "multiple list types specified";
404: break;
405: case (ELISTTYPE):
406: p = "missing list type";
407: break;
408: case (EDISPTYPE):
409: p = "missing display type";
410: break;
411: case (ELINE):
412: p = "expected line arguments";
413: break;
414: case (ENOPROLOGUE):
415: p = "document has no prologue";
416: break;
417: case (ENODATA):
418: p = "document has no data";
419: break;
420: case (EATT):
421: p = "expected valid AT&T symbol";
422: break;
423: case (ENAME):
424: p = "default name not yet set";
425: break;
426: }
427: assert(p);
1.84 kristaps 428: return(mdoc_perr(m, line, pos, p));
1.71 kristaps 429: }
430:
431:
432: static int
1.84 kristaps 433: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
1.71 kristaps 434: {
435: char *p;
436: enum mdoc_warn c;
437:
438: c = WARN_SYNTAX;
439: p = NULL;
440: switch (type) {
441: case (WBADMSEC):
442: p = "inappropriate manual section";
443: c = WARN_COMPAT;
444: break;
445: case (WBADSEC):
446: p = "inappropriate document section";
447: c = WARN_COMPAT;
448: break;
449: case (WARGVAL):
450: p = "argument value suggested";
451: c = WARN_COMPAT;
452: break;
453: case (WPROLREP):
454: p = "prologue macros repeated";
455: c = WARN_COMPAT;
456: break;
457: case (WPROLOOO):
458: p = "prologue macros out-of-order";
459: c = WARN_COMPAT;
460: break;
461: case (WNOLINE):
462: p = "suggested no line arguments";
463: break;
464: case (WLINE):
465: p = "suggested line arguments";
466: break;
467: case (WMULTILINE):
468: p = "suggested multi-line arguments";
469: break;
470: case (WNOMULTILINE):
471: p = "suggested no multi-line arguments";
472: break;
473: case (WWRONGMSEC):
474: p = "document section in wrong manual section";
475: c = WARN_COMPAT;
476: break;
477: case (WSECOOO):
478: p = "document section out of conventional order";
479: break;
480: case (WSECREP):
481: p = "document section repeated";
482: break;
483: case (WBADSTAND):
484: p = "unknown standard";
485: break;
486: case (WNAMESECINC):
487: p = "NAME section contents incomplete/badly-ordered";
488: break;
489: }
490: assert(p);
1.84 kristaps 491: return(mdoc_pwarn(m, line, pos, c, p));
1.71 kristaps 492: }
493:
494:
1.57 kristaps 495:
1.51 kristaps 496: static inline int
497: warn_count(struct mdoc *m, const char *k,
498: int want, const char *v, int has)
499: {
500:
1.55 kristaps 501: return(mdoc_warn(m, WARN_SYNTAX,
1.71 kristaps 502: "suggests %s %s %d (has %d)", v, k, want, has));
1.51 kristaps 503: }
504:
505:
506: static inline int
507: err_count(struct mdoc *m, const char *k,
508: int want, const char *v, int has)
509: {
510:
1.71 kristaps 511: return(mdoc_err(m,
512: "requires %s %s %d (has %d)", v, k, want, has));
1.51 kristaps 513: }
514:
515:
516: static inline int
517: count_child(struct mdoc *mdoc)
1.36 kristaps 518: {
1.51 kristaps 519: int i;
1.36 kristaps 520: struct mdoc_node *n;
521:
522: for (i = 0, n = mdoc->last->child; n; n = n->next, i++)
523: /* Do nothing */ ;
1.53 kristaps 524:
1.36 kristaps 525: return(i);
526: }
527:
528:
1.53 kristaps 529: /*
530: * Build these up with macros because they're basically the same check
531: * for different inequalities. Yes, this could be done with functions,
532: * but this is reasonable for now.
533: */
1.36 kristaps 534:
1.53 kristaps 535: #define CHECK_CHILD_DEFN(lvl, name, ineq) \
536: static int \
537: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
538: { \
539: int i; \
540: if ((i = count_child(mdoc)) ineq sz) \
541: return(1); \
542: return(lvl##_count(mdoc, #ineq, sz, p, i)); \
543: }
544:
545: #define CHECK_BODY_DEFN(name, lvl, func, num) \
546: static int \
1.55 kristaps 547: b##lvl##_##name(POST_ARGS) \
1.53 kristaps 548: { \
549: if (MDOC_BODY != mdoc->last->type) \
550: return(1); \
1.71 kristaps 551: return(func(mdoc, "multi-line arguments", (num))); \
1.53 kristaps 552: }
553:
554: #define CHECK_ELEM_DEFN(name, lvl, func, num) \
555: static int \
1.55 kristaps 556: e##lvl##_##name(POST_ARGS) \
1.53 kristaps 557: { \
558: assert(MDOC_ELEM == mdoc->last->type); \
1.71 kristaps 559: return(func(mdoc, "line arguments", (num))); \
1.53 kristaps 560: }
561:
562: #define CHECK_HEAD_DEFN(name, lvl, func, num) \
563: static int \
1.55 kristaps 564: h##lvl##_##name(POST_ARGS) \
1.53 kristaps 565: { \
566: if (MDOC_HEAD != mdoc->last->type) \
567: return(1); \
1.71 kristaps 568: return(func(mdoc, "line arguments", (num))); \
1.36 kristaps 569: }
570:
571:
1.53 kristaps 572: CHECK_CHILD_DEFN(warn, gt, >) /* warn_child_gt() */
573: CHECK_CHILD_DEFN(err, gt, >) /* err_child_gt() */
574: CHECK_CHILD_DEFN(warn, eq, ==) /* warn_child_eq() */
575: CHECK_CHILD_DEFN(err, eq, ==) /* err_child_eq() */
576: CHECK_CHILD_DEFN(err, lt, <) /* err_child_lt() */
1.59 kristaps 577: CHECK_CHILD_DEFN(warn, lt, <) /* warn_child_lt() */
1.53 kristaps 578: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0) /* bwarn_ge1() */
579: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1) /* ewarn_eq1() */
580: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0) /* ewarn_eq0() */
581: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0) /* ewarn_gt1() */
582: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1) /* eerr_eq1() */
583: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3) /* eerr_le2() */
584: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0) /* eerr_eq0() */
585: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0) /* eerr_ge1() */
586: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0) /* herr_eq0() */
1.59 kristaps 587: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2) /* hwarn_le1() */
1.53 kristaps 588: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0) /* herr_ge1() */
1.58 kristaps 589: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1) /* hwarn_eq1() */
1.36 kristaps 590:
591:
592: static int
1.55 kristaps 593: check_stdarg(PRE_ARGS)
1.36 kristaps 594: {
595:
1.71 kristaps 596: if (n->args && 1 == n->args->argc)
597: if (MDOC_Std == n->args->argv[0].arg)
598: return(1);
599: return(nwarn(mdoc, n, WARGVAL));
1.36 kristaps 600: }
601:
602:
603: static int
1.69 kristaps 604: check_sec(PRE_ARGS, ...)
605: {
606: enum mdoc_sec sec;
607: va_list ap;
608:
609: va_start(ap, n);
610:
611: for (;;) {
1.71 kristaps 612: /* LINTED */
1.70 kristaps 613: sec = (enum mdoc_sec)va_arg(ap, int);
614: if (SEC_CUSTOM == sec)
1.69 kristaps 615: break;
616: if (sec != mdoc->lastsec)
617: continue;
618: va_end(ap);
619: return(1);
620: }
621:
622: va_end(ap);
1.71 kristaps 623: return(nwarn(mdoc, n, WBADSEC));
1.69 kristaps 624: }
625:
626:
627: static int
628: check_msec(PRE_ARGS, ...)
1.33 kristaps 629: {
1.69 kristaps 630: va_list ap;
631: int msec;
1.33 kristaps 632:
1.69 kristaps 633: va_start(ap, n);
634: for (;;) {
1.71 kristaps 635: /* LINTED */
1.69 kristaps 636: if (0 == (msec = va_arg(ap, int)))
637: break;
638: if (msec != mdoc->meta.msec)
639: continue;
640: va_end(ap);
641: return(1);
642: }
643:
644: va_end(ap);
1.71 kristaps 645: return(nwarn(mdoc, n, WBADMSEC));
1.55 kristaps 646: }
647:
648:
1.68 kristaps 649: static int
1.71 kristaps 650: check_args(struct mdoc *m, const struct mdoc_node *n)
651: {
652: int i;
653:
654: if (NULL == n->args)
655: return(1);
656:
1.73 kristaps 657: assert(n->args->argc);
1.71 kristaps 658: for (i = 0; i < (int)n->args->argc; i++)
659: if ( ! check_argv(m, n, &n->args->argv[i]))
660: return(0);
661:
662: return(1);
663: }
664:
665:
666: static int
667: check_argv(struct mdoc *m, const struct mdoc_node *n,
668: const struct mdoc_argv *v)
1.68 kristaps 669: {
1.71 kristaps 670: int i;
1.68 kristaps 671:
1.71 kristaps 672: for (i = 0; i < (int)v->sz; i++)
673: if ( ! check_text(m, v->line, v->pos, v->value[i]))
674: return(0);
1.68 kristaps 675:
1.82 kristaps 676: if (MDOC_Std == v->arg) {
1.71 kristaps 677: /* `Nm' name must be set. */
678: if (v->sz || m->meta.name)
679: return(1);
680: return(nerr(m, n, ENAME));
1.68 kristaps 681: }
682:
683: return(1);
684: }
685:
686:
1.55 kristaps 687: static int
1.63 kristaps 688: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
1.55 kristaps 689: {
690: size_t c;
691:
1.84 kristaps 692: /* FIXME: indicate deprecated escapes \*(xx and \*x. */
693: /* FIXME: don't allow tabs unless in literal mode. */
1.62 kristaps 694:
1.55 kristaps 695: for ( ; *p; p++) {
1.84 kristaps 696: if ('\t' != *p && ! isprint((u_char)*p))
697: return(perr(mdoc, line, pos, EPRINT));
1.55 kristaps 698: if ('\\' != *p)
699: continue;
700: if ((c = mdoc_isescape(p))) {
1.63 kristaps 701: p += (int)c - 1;
1.55 kristaps 702: continue;
703: }
1.74 kristaps 704: if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
705: return(mdoc_perr(mdoc, line, pos,
706: "invalid escape sequence"));
707: if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
708: "invalid escape sequence"))
709: return(0);
1.55 kristaps 710: }
711:
712: return(1);
1.14 kristaps 713: }
714:
715:
1.55 kristaps 716:
717:
1.14 kristaps 718: static int
1.55 kristaps 719: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
1.54 kristaps 720: {
721:
722: assert(n->parent);
723: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
724: (t == n->parent->type))
725: return(1);
726:
727: return(mdoc_nerr(mdoc, n, "require parent %s",
728: MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
729: }
730:
731:
732:
733: static int
1.55 kristaps 734: pre_display(PRE_ARGS)
1.23 kristaps 735: {
1.55 kristaps 736: struct mdoc_node *node;
1.23 kristaps 737:
1.53 kristaps 738: /* Display elements (`Bd', `D1'...) cannot be nested. */
739:
1.55 kristaps 740: if (MDOC_BLOCK != n->type)
1.24 kristaps 741: return(1);
742:
1.38 kristaps 743: /* LINTED */
1.55 kristaps 744: for (node = mdoc->last->parent; node; node = node->parent)
745: if (MDOC_BLOCK == node->type)
746: if (MDOC_Bd == node->tok)
1.23 kristaps 747: break;
1.55 kristaps 748: if (NULL == node)
1.23 kristaps 749: return(1);
1.53 kristaps 750:
1.71 kristaps 751: return(nerr(mdoc, n, ENESTDISP));
1.23 kristaps 752: }
753:
754:
755: static int
1.55 kristaps 756: pre_bl(PRE_ARGS)
1.24 kristaps 757: {
1.71 kristaps 758: int i, type, width, offset;
1.24 kristaps 759:
1.55 kristaps 760: if (MDOC_BLOCK != n->type)
1.24 kristaps 761: return(1);
1.71 kristaps 762: if (NULL == n->args)
763: return(nerr(mdoc, n, ELISTTYPE));
1.24 kristaps 764:
1.53 kristaps 765: /* Make sure that only one type of list is specified. */
766:
1.61 kristaps 767: type = offset = width = -1;
768:
1.38 kristaps 769: /* LINTED */
1.71 kristaps 770: for (i = 0; i < (int)n->args->argc; i++)
771: switch (n->args->argv[i].arg) {
1.24 kristaps 772: case (MDOC_Bullet):
773: /* FALLTHROUGH */
774: case (MDOC_Dash):
775: /* FALLTHROUGH */
776: case (MDOC_Enum):
777: /* FALLTHROUGH */
778: case (MDOC_Hyphen):
779: /* FALLTHROUGH */
780: case (MDOC_Item):
781: /* FALLTHROUGH */
782: case (MDOC_Tag):
783: /* FALLTHROUGH */
784: case (MDOC_Diag):
785: /* FALLTHROUGH */
786: case (MDOC_Hang):
787: /* FALLTHROUGH */
788: case (MDOC_Ohang):
789: /* FALLTHROUGH */
790: case (MDOC_Inset):
1.26 kristaps 791: /* FALLTHROUGH */
792: case (MDOC_Column):
1.61 kristaps 793: if (-1 == type) {
1.71 kristaps 794: type = n->args->argv[i].arg;
1.53 kristaps 795: break;
1.61 kristaps 796: }
1.71 kristaps 797: return(nerr(mdoc, n, EMULTILIST));
1.61 kristaps 798: case (MDOC_Width):
799: if (-1 == width) {
1.71 kristaps 800: width = n->args->argv[i].arg;
1.61 kristaps 801: break;
802: }
1.71 kristaps 803: return(nerr(mdoc, n, EARGREP));
1.61 kristaps 804: case (MDOC_Offset):
805: if (-1 == offset) {
1.71 kristaps 806: offset = n->args->argv[i].arg;
1.61 kristaps 807: break;
808: }
1.71 kristaps 809: return(nerr(mdoc, n, EARGREP));
1.24 kristaps 810: default:
811: break;
812: }
1.53 kristaps 813:
1.61 kristaps 814: if (-1 == type)
1.71 kristaps 815: return(nerr(mdoc, n, ELISTTYPE));
1.61 kristaps 816:
817: switch (type) {
818: case (MDOC_Column):
819: /* FALLTHROUGH */
820: case (MDOC_Diag):
821: /* FALLTHROUGH */
822: case (MDOC_Inset):
823: /* FALLTHROUGH */
824: case (MDOC_Item):
825: if (-1 == width)
826: break;
827: return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1.71 kristaps 828: "superfluous %s argument",
1.61 kristaps 829: mdoc_argnames[MDOC_Width]));
830: case (MDOC_Tag):
1.71 kristaps 831: if (-1 != width)
832: break;
833: return(mdoc_nerr(mdoc, n, "missing %s argument",
834: mdoc_argnames[MDOC_Width]));
1.61 kristaps 835: default:
836: break;
837: }
838:
839: return(1);
1.24 kristaps 840: }
841:
842:
843: static int
1.55 kristaps 844: pre_bd(PRE_ARGS)
1.24 kristaps 845: {
1.71 kristaps 846: int i, type, err;
1.24 kristaps 847:
1.55 kristaps 848: if (MDOC_BLOCK != n->type)
1.24 kristaps 849: return(1);
1.71 kristaps 850: if (NULL == n->args)
851: return(nerr(mdoc, n, EDISPTYPE));
1.24 kristaps 852:
1.53 kristaps 853: /* Make sure that only one type of display is specified. */
854:
1.38 kristaps 855: /* LINTED */
1.71 kristaps 856: for (i = 0, err = type = 0; ! err &&
857: i < (int)n->args->argc; i++)
858: switch (n->args->argv[i].arg) {
1.24 kristaps 859: case (MDOC_Ragged):
860: /* FALLTHROUGH */
861: case (MDOC_Unfilled):
862: /* FALLTHROUGH */
1.29 kristaps 863: case (MDOC_Filled):
864: /* FALLTHROUGH */
1.24 kristaps 865: case (MDOC_Literal):
866: /* FALLTHROUGH */
867: case (MDOC_File):
1.53 kristaps 868: if (0 == type++)
869: break;
1.71 kristaps 870: return(nerr(mdoc, n, EMULTIDISP));
1.24 kristaps 871: default:
872: break;
873: }
1.53 kristaps 874:
875: if (type)
876: return(1);
1.71 kristaps 877: return(nerr(mdoc, n, EDISPTYPE));
1.24 kristaps 878: }
879:
880:
881: static int
1.55 kristaps 882: pre_ss(PRE_ARGS)
1.33 kristaps 883: {
884:
1.55 kristaps 885: if (MDOC_BLOCK != n->type)
1.33 kristaps 886: return(1);
1.55 kristaps 887: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
1.33 kristaps 888: }
889:
890:
891: static int
1.55 kristaps 892: pre_sh(PRE_ARGS)
1.33 kristaps 893: {
894:
1.55 kristaps 895: if (MDOC_BLOCK != n->type)
1.33 kristaps 896: return(1);
1.55 kristaps 897: return(check_parent(mdoc, n, -1, MDOC_ROOT));
1.33 kristaps 898: }
899:
900:
901: static int
1.55 kristaps 902: pre_it(PRE_ARGS)
1.53 kristaps 903: {
904:
1.55 kristaps 905: if (MDOC_BLOCK != n->type)
1.53 kristaps 906: return(1);
1.55 kristaps 907: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
1.53 kristaps 908: }
909:
910:
911: static int
1.69 kristaps 912: pre_an(PRE_ARGS)
1.36 kristaps 913: {
914:
1.73 kristaps 915: if (NULL == n->args || 1 == n->args->argc)
1.36 kristaps 916: return(1);
1.71 kristaps 917: return(mdoc_nerr(mdoc, n, "only one argument allowed"));
1.36 kristaps 918: }
919:
920:
921: static int
1.69 kristaps 922: pre_lb(PRE_ARGS)
1.35 kristaps 923: {
1.36 kristaps 924:
1.69 kristaps 925: return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
1.35 kristaps 926: }
927:
928:
929: static int
1.55 kristaps 930: pre_rv(PRE_ARGS)
1.36 kristaps 931: {
932:
1.69 kristaps 933: if ( ! check_msec(mdoc, n, 2, 3, 0))
1.36 kristaps 934: return(0);
1.55 kristaps 935: return(check_stdarg(mdoc, n));
1.36 kristaps 936: }
937:
938:
939: static int
1.55 kristaps 940: pre_ex(PRE_ARGS)
1.33 kristaps 941: {
1.35 kristaps 942:
1.69 kristaps 943: if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
1.35 kristaps 944: return(0);
1.55 kristaps 945: return(check_stdarg(mdoc, n));
1.33 kristaps 946: }
947:
948:
949: static int
1.55 kristaps 950: pre_er(PRE_ARGS)
1.33 kristaps 951: {
952:
1.69 kristaps 953: return(check_msec(mdoc, n, 2, 0));
1.33 kristaps 954: }
955:
956:
957: static int
1.55 kristaps 958: pre_cd(PRE_ARGS)
1.33 kristaps 959: {
960:
1.69 kristaps 961: return(check_msec(mdoc, n, 4, 0));
1.33 kristaps 962: }
963:
964:
965: static int
1.55 kristaps 966: pre_prologue(PRE_ARGS)
1.20 kristaps 967: {
968:
1.69 kristaps 969: return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
970: }
1.20 kristaps 971:
972:
1.69 kristaps 973: static int
974: pre_dt(PRE_ARGS)
975: {
976:
977: if (0 == mdoc->meta.date || mdoc->meta.os)
1.71 kristaps 978: if ( ! nwarn(mdoc, n, WPROLOOO))
1.69 kristaps 979: return(0);
980: if (mdoc->meta.title)
1.71 kristaps 981: if ( ! nwarn(mdoc, n, WPROLREP))
1.69 kristaps 982: return(0);
983: return(1);
984: }
985:
986:
987: static int
988: pre_os(PRE_ARGS)
989: {
990:
991: if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.71 kristaps 992: if ( ! nwarn(mdoc, n, WPROLOOO))
1.69 kristaps 993: return(0);
994: if (mdoc->meta.os)
1.71 kristaps 995: if ( ! nwarn(mdoc, n, WPROLREP))
1.69 kristaps 996: return(0);
997: return(1);
998: }
1.20 kristaps 999:
1000:
1.69 kristaps 1001: static int
1002: pre_dd(PRE_ARGS)
1003: {
1.20 kristaps 1004:
1.69 kristaps 1005: if (mdoc->meta.title || mdoc->meta.os)
1.71 kristaps 1006: if ( ! nwarn(mdoc, n, WPROLOOO))
1.69 kristaps 1007: return(0);
1008: if (mdoc->meta.date)
1.71 kristaps 1009: if ( ! nwarn(mdoc, n, WPROLREP))
1.69 kristaps 1010: return(0);
1011: return(1);
1.20 kristaps 1012: }
1013:
1014:
1.35 kristaps 1015: static int
1.55 kristaps 1016: post_bf(POST_ARGS)
1.41 kristaps 1017: {
1018: char *p;
1019: struct mdoc_node *head;
1020:
1021: if (MDOC_BLOCK != mdoc->last->type)
1022: return(1);
1.53 kristaps 1023:
1.71 kristaps 1024: head = mdoc->last->head;
1.41 kristaps 1025:
1.71 kristaps 1026: if (NULL == mdoc->last->args) {
1027: if (NULL == head->child ||
1028: MDOC_TEXT != head->child->type)
1029: return(mdoc_err(mdoc, "text argument expected"));
1.53 kristaps 1030:
1.71 kristaps 1031: p = head->child->string;
1.53 kristaps 1032: if (xstrcmp(p, "Em"))
1033: return(1);
1034: else if (xstrcmp(p, "Li"))
1035: return(1);
1036: else if (xstrcmp(p, "Sm"))
1037: return(1);
1038: return(mdoc_nerr(mdoc, head->child, "invalid font"));
1.41 kristaps 1039: }
1.53 kristaps 1040:
1.41 kristaps 1041: if (head->child)
1.71 kristaps 1042: return(mdoc_err(mdoc, "one argument expected"));
1.53 kristaps 1043:
1.71 kristaps 1044: return(1);
1.41 kristaps 1045: }
1046:
1047:
1048: static int
1.55 kristaps 1049: post_nm(POST_ARGS)
1.37 kristaps 1050: {
1051:
1052: if (mdoc->last->child)
1053: return(1);
1054: if (mdoc->meta.name)
1055: return(1);
1.71 kristaps 1056: return(merr(mdoc, ENAME));
1.37 kristaps 1057: }
1058:
1059:
1060: static int
1.55 kristaps 1061: post_at(POST_ARGS)
1.36 kristaps 1062: {
1063:
1.37 kristaps 1064: if (NULL == mdoc->last->child)
1065: return(1);
1.71 kristaps 1066: if (MDOC_TEXT != mdoc->last->child->type)
1067: return(merr(mdoc, EATT));
1068: if (mdoc_a2att(mdoc->last->child->string))
1.36 kristaps 1069: return(1);
1.71 kristaps 1070: return(merr(mdoc, EATT));
1.36 kristaps 1071: }
1072:
1073:
1074: static int
1.55 kristaps 1075: post_an(POST_ARGS)
1.35 kristaps 1076: {
1077:
1.71 kristaps 1078: if (mdoc->last->args) {
1.35 kristaps 1079: if (NULL == mdoc->last->child)
1080: return(1);
1.71 kristaps 1081: return(merr(mdoc, ELINE));
1.35 kristaps 1082: }
1083:
1084: if (mdoc->last->child)
1085: return(1);
1.71 kristaps 1086: return(merr(mdoc, ELINE));
1.35 kristaps 1087: }
1088:
1089:
1090: static int
1.82 kristaps 1091: post_args(POST_ARGS)
1.35 kristaps 1092: {
1093:
1.71 kristaps 1094: if (mdoc->last->args)
1095: return(1);
1096: return(merr(mdoc, ELINE));
1.35 kristaps 1097: }
1098:
1099:
1.25 kristaps 1100: static int
1.55 kristaps 1101: post_it(POST_ARGS)
1.25 kristaps 1102: {
1.71 kristaps 1103: int type, i, cols;
1104: struct mdoc_node *n, *c;
1.25 kristaps 1105:
1106: if (MDOC_BLOCK != mdoc->last->type)
1107: return(1);
1108:
1.53 kristaps 1109: n = mdoc->last->parent->parent;
1.71 kristaps 1110: if (NULL == n->args)
1111: return(merr(mdoc, ELISTTYPE));
1.25 kristaps 1112:
1.26 kristaps 1113: /* Some types require block-head, some not. */
1.25 kristaps 1114:
1.38 kristaps 1115: /* LINTED */
1.71 kristaps 1116: for (cols = type = -1, i = 0; -1 == type &&
1117: i < (int)n->args->argc; i++)
1118: switch (n->args->argv[i].arg) {
1.25 kristaps 1119: case (MDOC_Tag):
1120: /* FALLTHROUGH */
1121: case (MDOC_Diag):
1122: /* FALLTHROUGH */
1123: case (MDOC_Hang):
1124: /* FALLTHROUGH */
1125: case (MDOC_Ohang):
1126: /* FALLTHROUGH */
1127: case (MDOC_Inset):
1.71 kristaps 1128: /* FALLTHROUGH */
1.25 kristaps 1129: case (MDOC_Bullet):
1130: /* FALLTHROUGH */
1131: case (MDOC_Dash):
1132: /* FALLTHROUGH */
1133: case (MDOC_Enum):
1134: /* FALLTHROUGH */
1135: case (MDOC_Hyphen):
1136: /* FALLTHROUGH */
1137: case (MDOC_Item):
1.71 kristaps 1138: type = n->args->argv[i].arg;
1.47 kristaps 1139: break;
1.25 kristaps 1140: case (MDOC_Column):
1.71 kristaps 1141: type = n->args->argv[i].arg;
1142: cols = (int)n->args->argv[i].sz;
1.25 kristaps 1143: break;
1144: default:
1145: break;
1146: }
1147:
1.71 kristaps 1148: if (-1 == type)
1149: return(merr(mdoc, ELISTTYPE));
1.25 kristaps 1150:
1.71 kristaps 1151: switch (type) {
1152: case (MDOC_Tag):
1153: if (NULL == mdoc->last->head->child)
1154: if ( ! mwarn(mdoc, WLINE))
1155: return(0);
1156: break;
1157: case (MDOC_Hang):
1158: /* FALLTHROUGH */
1159: case (MDOC_Ohang):
1160: /* FALLTHROUGH */
1161: case (MDOC_Inset):
1162: /* FALLTHROUGH */
1163: case (MDOC_Diag):
1164: if (NULL == mdoc->last->head->child)
1165: if ( ! mwarn(mdoc, WLINE))
1.25 kristaps 1166: return(0);
1.71 kristaps 1167: if (NULL == mdoc->last->body->child)
1168: if ( ! mwarn(mdoc, WMULTILINE))
1.25 kristaps 1169: return(0);
1.71 kristaps 1170: break;
1171: case (MDOC_Bullet):
1172: /* FALLTHROUGH */
1173: case (MDOC_Dash):
1174: /* FALLTHROUGH */
1175: case (MDOC_Enum):
1176: /* FALLTHROUGH */
1177: case (MDOC_Hyphen):
1178: /* FALLTHROUGH */
1179: case (MDOC_Item):
1180: if (mdoc->last->head->child)
1181: if ( ! mwarn(mdoc, WNOLINE))
1.47 kristaps 1182: return(0);
1.71 kristaps 1183: if (NULL == mdoc->last->body->child)
1184: if ( ! mwarn(mdoc, WMULTILINE))
1.47 kristaps 1185: return(0);
1.71 kristaps 1186: break;
1187: case (MDOC_Column):
1188: if (NULL == mdoc->last->head->child)
1189: if ( ! mwarn(mdoc, WLINE))
1.47 kristaps 1190: return(0);
1.71 kristaps 1191: if (mdoc->last->body->child)
1192: if ( ! mwarn(mdoc, WNOMULTILINE))
1.47 kristaps 1193: return(0);
1.81 kristaps 1194: c = mdoc->last->child;
1.80 kristaps 1195: for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
1.71 kristaps 1196: i++;
1197: if (i == cols)
1198: break;
1.81 kristaps 1199: return(mdoc_err(mdoc, "column mismatch (have "
1200: "%d, want %d)", i, cols));
1.71 kristaps 1201: default:
1202: break;
1.25 kristaps 1203: }
1204:
1.71 kristaps 1205: return(1);
1.25 kristaps 1206: }
1207:
1208:
1.24 kristaps 1209: static int
1.55 kristaps 1210: post_bl(POST_ARGS)
1.24 kristaps 1211: {
1.53 kristaps 1212: struct mdoc_node *n;
1.24 kristaps 1213:
1214: if (MDOC_BODY != mdoc->last->type)
1215: return(1);
1.71 kristaps 1216: if (NULL == mdoc->last->child)
1.64 kristaps 1217: return(1);
1218:
1.38 kristaps 1219: /* LINTED */
1.24 kristaps 1220: for (n = mdoc->last->child; n; n = n->next) {
1221: if (MDOC_BLOCK == n->type)
1.25 kristaps 1222: if (MDOC_It == n->tok)
1.24 kristaps 1223: continue;
1.64 kristaps 1224: return(mdoc_nerr(mdoc, n, "bad child of parent %s",
1225: mdoc_macronames[mdoc->last->tok]));
1.24 kristaps 1226: }
1.53 kristaps 1227:
1.64 kristaps 1228: return(1);
1.24 kristaps 1229: }
1230:
1231:
1.34 kristaps 1232: static int
1.37 kristaps 1233: ebool(struct mdoc *mdoc)
1.34 kristaps 1234: {
1235: struct mdoc_node *n;
1236:
1.38 kristaps 1237: /* LINTED */
1.34 kristaps 1238: for (n = mdoc->last->child; n; n = n->next) {
1239: if (MDOC_TEXT != n->type)
1240: break;
1.71 kristaps 1241: if (xstrcmp(n->string, "on"))
1.34 kristaps 1242: continue;
1.71 kristaps 1243: if (xstrcmp(n->string, "off"))
1.34 kristaps 1244: continue;
1245: break;
1246: }
1.53 kristaps 1247:
1.34 kristaps 1248: if (NULL == n)
1249: return(1);
1.71 kristaps 1250: return(nerr(mdoc, n, EBOOL));
1.37 kristaps 1251: }
1252:
1253:
1254: static int
1.55 kristaps 1255: post_root(POST_ARGS)
1.37 kristaps 1256: {
1257:
1.46 kristaps 1258: if (NULL == mdoc->first->child)
1.71 kristaps 1259: return(merr(mdoc, ENODATA));
1.46 kristaps 1260: if (SEC_PROLOGUE == mdoc->lastnamed)
1.71 kristaps 1261: return(merr(mdoc, ENOPROLOGUE));
1.53 kristaps 1262:
1.46 kristaps 1263: if (MDOC_BLOCK != mdoc->first->child->type)
1.71 kristaps 1264: return(merr(mdoc, ENODATA));
1.46 kristaps 1265: if (MDOC_Sh != mdoc->first->child->tok)
1.71 kristaps 1266: return(merr(mdoc, ENODATA));
1.53 kristaps 1267:
1.37 kristaps 1268: return(1);
1.34 kristaps 1269: }
1270:
1271:
1.20 kristaps 1272: static int
1.69 kristaps 1273: post_st(POST_ARGS)
1274: {
1275:
1.71 kristaps 1276: if (mdoc_a2st(mdoc->last->child->string))
1.69 kristaps 1277: return(1);
1.71 kristaps 1278: return(mwarn(mdoc, WBADSTAND));
1.69 kristaps 1279: }
1280:
1281:
1282: static int
1.55 kristaps 1283: post_sh(POST_ARGS)
1.14 kristaps 1284: {
1.46 kristaps 1285:
1286: if (MDOC_HEAD == mdoc->last->type)
1287: return(post_sh_head(mdoc));
1288: if (MDOC_BODY == mdoc->last->type)
1289: return(post_sh_body(mdoc));
1.53 kristaps 1290:
1.46 kristaps 1291: return(1);
1292: }
1293:
1294:
1295: static int
1.55 kristaps 1296: post_sh_body(POST_ARGS)
1.46 kristaps 1297: {
1298: struct mdoc_node *n;
1299:
1300: if (SEC_NAME != mdoc->lastnamed)
1301: return(1);
1302:
1.51 kristaps 1303: /*
1304: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1305: * macros (can have multiple `Nm' and one `Nd'). Note that the
1306: * children of the BODY declaration can also be "text".
1307: */
1308:
1.46 kristaps 1309: if (NULL == (n = mdoc->last->child))
1.71 kristaps 1310: return(mwarn(mdoc, WNAMESECINC));
1.51 kristaps 1311:
1312: for ( ; n && n->next; n = n->next) {
1313: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1314: continue;
1315: if (MDOC_TEXT == n->type)
1316: continue;
1.71 kristaps 1317: if ( ! mwarn(mdoc, WNAMESECINC))
1.51 kristaps 1318: return(0);
1319: }
1320:
1321: if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1.46 kristaps 1322: return(1);
1.71 kristaps 1323: return(mwarn(mdoc, WNAMESECINC));
1.46 kristaps 1324: }
1325:
1326:
1327: static int
1.55 kristaps 1328: post_sh_head(POST_ARGS)
1.46 kristaps 1329: {
1.36 kristaps 1330: char buf[64];
1.21 kristaps 1331: enum mdoc_sec sec;
1332:
1.69 kristaps 1333: /*
1334: * Process a new section. Sections are either "named" or
1335: * "custom"; custom sections are user-defined, while named ones
1336: * usually follow a conventional order and may only appear in
1337: * certain manual sections.
1338: */
1339:
1.25 kristaps 1340: assert(MDOC_Sh == mdoc->last->tok);
1.21 kristaps 1341:
1.69 kristaps 1342: (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.14 kristaps 1343:
1.46 kristaps 1344: sec = mdoc_atosec(buf);
1345:
1.69 kristaps 1346: /* The NAME section should always be first. */
1347:
1.46 kristaps 1348: if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1.71 kristaps 1349: return(mwarn(mdoc, WSECOOO));
1.46 kristaps 1350: if (SEC_CUSTOM == sec)
1.21 kristaps 1351: return(1);
1.69 kristaps 1352:
1353: /* Check for repeated or out-of-order sections. */
1354:
1.46 kristaps 1355: if (sec == mdoc->lastnamed)
1.71 kristaps 1356: return(mwarn(mdoc, WSECREP));
1.46 kristaps 1357: if (sec < mdoc->lastnamed)
1.71 kristaps 1358: return(mwarn(mdoc, WSECOOO));
1.69 kristaps 1359:
1360: /* Check particular section/manual section conventions. */
1361:
1362: switch (sec) {
1363: case (SEC_LIBRARY):
1364: switch (mdoc->meta.msec) {
1365: case (2):
1366: /* FALLTHROUGH */
1367: case (3):
1368: break;
1369: default:
1.71 kristaps 1370: return(mwarn(mdoc, WWRONGMSEC));
1.69 kristaps 1371: }
1372: break;
1373: default:
1374: break;
1375: }
1.46 kristaps 1376:
1377: return(1);
1.11 kristaps 1378: }
1379:
1380:
1.57 kristaps 1381: static int
1.69 kristaps 1382: pre_fd(PRE_ARGS)
1.11 kristaps 1383: {
1.39 kristaps 1384:
1.69 kristaps 1385: return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1.11 kristaps 1386: }
CVSweb