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