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