Annotation of mandoc/mdoc_validate.c, Revision 1.126
1.126 ! kristaps 1: /* $Id: mdoc_validate.c,v 1.125 2010/11/29 13:51:03 kristaps Exp $ */
1.1 kristaps 2: /*
1.110 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.5 kristaps 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.5 kristaps 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: */
1.56 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <sys/types.h>
22:
23: #include <assert.h>
24: #include <ctype.h>
1.35 kristaps 25: #include <limits.h>
1.81 kristaps 26: #include <stdio.h>
1.1 kristaps 27: #include <stdlib.h>
1.2 kristaps 28: #include <string.h>
1.1 kristaps 29:
1.79 kristaps 30: #include "mandoc.h"
1.1 kristaps 31: #include "libmdoc.h"
1.18 kristaps 32: #include "libmandoc.h"
1.1 kristaps 33:
34: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
35:
1.91 kristaps 36: #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
1.32 kristaps 37: #define POST_ARGS struct mdoc *mdoc
1.1 kristaps 38:
1.120 kristaps 39: enum check_ineq {
40: CHECK_LT,
41: CHECK_GT,
42: CHECK_EQ
43: };
44:
45: enum check_lvl {
46: CHECK_WARN,
47: CHECK_ERROR,
48: CHECK_FATAL
49: };
50:
1.1 kristaps 51: typedef int (*v_pre)(PRE_ARGS);
52: typedef int (*v_post)(POST_ARGS);
53:
54: struct valids {
55: v_pre *pre;
56: v_post *post;
57: };
58:
1.120 kristaps 59: static int check_count(struct mdoc *, enum mdoc_type,
60: enum check_lvl, enum check_ineq, int);
1.59 kristaps 61: static int check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.32 kristaps 62: static int check_stdarg(PRE_ARGS);
1.92 kristaps 63: static int check_text(struct mdoc *, int, int, char *);
1.32 kristaps 64: static int check_argv(struct mdoc *,
1.92 kristaps 65: struct mdoc_node *, struct mdoc_argv *);
66: static int check_args(struct mdoc *, struct mdoc_node *);
1.32 kristaps 67:
1.120 kristaps 68: static int ebool(POST_ARGS);
1.32 kristaps 69: static int berr_ge1(POST_ARGS);
70: static int bwarn_ge1(POST_ARGS);
1.120 kristaps 71: static int eerr_eq0(POST_ARGS);
1.32 kristaps 72: static int eerr_eq1(POST_ARGS);
73: static int eerr_ge1(POST_ARGS);
1.46 kristaps 74: static int eerr_le1(POST_ARGS);
1.116 kristaps 75: static int ewarn_eq0(POST_ARGS);
1.32 kristaps 76: static int ewarn_ge1(POST_ARGS);
77: static int herr_eq0(POST_ARGS);
78: static int herr_ge1(POST_ARGS);
1.120 kristaps 79: static int hwarn_eq0(POST_ARGS);
1.32 kristaps 80: static int hwarn_eq1(POST_ARGS);
81: static int hwarn_le1(POST_ARGS);
82:
83: static int post_an(POST_ARGS);
84: static int post_at(POST_ARGS);
85: static int post_bf(POST_ARGS);
86: static int post_bl(POST_ARGS);
87: static int post_bl_head(POST_ARGS);
1.123 kristaps 88: static int post_defaults(POST_ARGS);
89: static int post_eoln(POST_ARGS);
1.85 kristaps 90: static int post_dt(POST_ARGS);
1.32 kristaps 91: static int post_it(POST_ARGS);
92: static int post_lb(POST_ARGS);
93: static int post_nm(POST_ARGS);
94: static int post_root(POST_ARGS);
1.43 kristaps 95: static int post_rs(POST_ARGS);
1.32 kristaps 96: static int post_sh(POST_ARGS);
97: static int post_sh_body(POST_ARGS);
98: static int post_sh_head(POST_ARGS);
99: static int post_st(POST_ARGS);
1.57 kristaps 100: static int post_vt(POST_ARGS);
1.32 kristaps 101: static int pre_an(PRE_ARGS);
102: static int pre_bd(PRE_ARGS);
103: static int pre_bl(PRE_ARGS);
104: static int pre_dd(PRE_ARGS);
105: static int pre_display(PRE_ARGS);
106: static int pre_dt(PRE_ARGS);
107: static int pre_it(PRE_ARGS);
108: static int pre_os(PRE_ARGS);
1.124 kristaps 109: static int pre_par(PRE_ARGS);
1.32 kristaps 110: static int pre_rv(PRE_ARGS);
111: static int pre_sh(PRE_ARGS);
112: static int pre_ss(PRE_ARGS);
113:
114: static v_post posts_an[] = { post_an, NULL };
1.126 ! kristaps 115: static v_post posts_at[] = { post_at, post_defaults, NULL };
1.101 schwarze 116: static v_post posts_bd_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.32 kristaps 117: static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
118: static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
119: static v_post posts_bool[] = { eerr_eq1, ebool, NULL };
1.84 kristaps 120: static v_post posts_eoln[] = { post_eoln, NULL };
1.123 kristaps 121: static v_post posts_defaults[] = { post_defaults, NULL };
1.85 kristaps 122: static v_post posts_dt[] = { post_dt, NULL };
1.32 kristaps 123: static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
124: static v_post posts_it[] = { post_it, NULL };
125: static v_post posts_lb[] = { eerr_eq1, post_lb, NULL };
126: static v_post posts_nd[] = { berr_ge1, NULL };
127: static v_post posts_nm[] = { post_nm, NULL };
1.116 kristaps 128: static v_post posts_notext[] = { ewarn_eq0, NULL };
1.44 kristaps 129: static v_post posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
1.32 kristaps 130: static v_post posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
1.46 kristaps 131: static v_post posts_sp[] = { eerr_le1, NULL };
1.32 kristaps 132: static v_post posts_ss[] = { herr_ge1, NULL };
133: static v_post posts_st[] = { eerr_eq1, post_st, NULL };
134: static v_post posts_text[] = { eerr_ge1, NULL };
1.51 kristaps 135: static v_post posts_text1[] = { eerr_eq1, NULL };
1.57 kristaps 136: static v_post posts_vt[] = { post_vt, NULL };
1.32 kristaps 137: static v_post posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
138: static v_post posts_wtext[] = { ewarn_ge1, NULL };
139: static v_pre pres_an[] = { pre_an, NULL };
1.124 kristaps 140: static v_pre pres_bd[] = { pre_display, pre_bd, pre_par, NULL };
141: static v_pre pres_bl[] = { pre_bl, pre_par, NULL };
1.32 kristaps 142: static v_pre pres_d1[] = { pre_display, NULL };
143: static v_pre pres_dd[] = { pre_dd, NULL };
144: static v_pre pres_dt[] = { pre_dt, NULL };
1.67 kristaps 145: static v_pre pres_er[] = { NULL, NULL };
1.76 kristaps 146: static v_pre pres_ex[] = { NULL, NULL };
1.77 kristaps 147: static v_pre pres_fd[] = { NULL, NULL };
1.32 kristaps 148: static v_pre pres_it[] = { pre_it, NULL };
149: static v_pre pres_os[] = { pre_os, NULL };
1.124 kristaps 150: static v_pre pres_pp[] = { pre_par, NULL };
1.32 kristaps 151: static v_pre pres_rv[] = { pre_rv, NULL };
152: static v_pre pres_sh[] = { pre_sh, NULL };
153: static v_pre pres_ss[] = { pre_ss, NULL };
1.1 kristaps 154:
155: const struct valids mdoc_valids[MDOC_MAX] = {
1.10 kristaps 156: { NULL, NULL }, /* Ap */
1.114 kristaps 157: { pres_dd, posts_wtext }, /* Dd */
1.85 kristaps 158: { pres_dt, posts_dt }, /* Dt */
1.1 kristaps 159: { pres_os, NULL }, /* Os */
160: { pres_sh, posts_sh }, /* Sh */
161: { pres_ss, posts_ss }, /* Ss */
1.117 kristaps 162: { pres_pp, posts_notext }, /* Pp */
1.1 kristaps 163: { pres_d1, posts_wline }, /* D1 */
164: { pres_d1, posts_wline }, /* Dl */
1.104 kristaps 165: { pres_bd, posts_bd_bk }, /* Bd */
1.1 kristaps 166: { NULL, NULL }, /* Ed */
167: { pres_bl, posts_bl }, /* Bl */
168: { NULL, NULL }, /* El */
169: { pres_it, posts_it }, /* It */
170: { NULL, posts_text }, /* Ad */
171: { pres_an, posts_an }, /* An */
1.123 kristaps 172: { NULL, posts_defaults }, /* Ar */
1.69 kristaps 173: { NULL, posts_text }, /* Cd */
1.1 kristaps 174: { NULL, NULL }, /* Cm */
175: { NULL, NULL }, /* Dv */
176: { pres_er, posts_text }, /* Er */
177: { NULL, NULL }, /* Ev */
1.42 kristaps 178: { pres_ex, NULL }, /* Ex */
1.1 kristaps 179: { NULL, NULL }, /* Fa */
180: { pres_fd, posts_wtext }, /* Fd */
181: { NULL, NULL }, /* Fl */
182: { NULL, posts_text }, /* Fn */
183: { NULL, posts_wtext }, /* Ft */
184: { NULL, posts_text }, /* Ic */
1.51 kristaps 185: { NULL, posts_text1 }, /* In */
1.123 kristaps 186: { NULL, posts_defaults }, /* Li */
1.27 kristaps 187: { NULL, posts_nd }, /* Nd */
1.1 kristaps 188: { NULL, posts_nm }, /* Nm */
189: { NULL, posts_wline }, /* Op */
190: { NULL, NULL }, /* Ot */
191: { NULL, NULL }, /* Pa */
1.42 kristaps 192: { pres_rv, NULL }, /* Rv */
1.1 kristaps 193: { NULL, posts_st }, /* St */
194: { NULL, NULL }, /* Va */
1.57 kristaps 195: { NULL, posts_vt }, /* Vt */
1.84 kristaps 196: { NULL, posts_wtext }, /* Xr */
1.1 kristaps 197: { NULL, posts_text }, /* %A */
1.47 kristaps 198: { NULL, posts_text }, /* %B */ /* FIXME: can be used outside Rs/Re. */
1.54 kristaps 199: { NULL, posts_text }, /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1 kristaps 200: { NULL, posts_text }, /* %I */
201: { NULL, posts_text }, /* %J */
202: { NULL, posts_text }, /* %N */
203: { NULL, posts_text }, /* %O */
204: { NULL, posts_text }, /* %P */
205: { NULL, posts_text }, /* %R */
1.47 kristaps 206: { NULL, posts_text }, /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1 kristaps 207: { NULL, posts_text }, /* %V */
208: { NULL, NULL }, /* Ac */
209: { NULL, NULL }, /* Ao */
210: { NULL, posts_wline }, /* Aq */
211: { NULL, posts_at }, /* At */
212: { NULL, NULL }, /* Bc */
213: { NULL, posts_bf }, /* Bf */
214: { NULL, NULL }, /* Bo */
215: { NULL, posts_wline }, /* Bq */
216: { NULL, NULL }, /* Bsx */
217: { NULL, NULL }, /* Bx */
218: { NULL, posts_bool }, /* Db */
219: { NULL, NULL }, /* Dc */
220: { NULL, NULL }, /* Do */
221: { NULL, posts_wline }, /* Dq */
222: { NULL, NULL }, /* Ec */
223: { NULL, NULL }, /* Ef */
224: { NULL, NULL }, /* Em */
225: { NULL, NULL }, /* Eo */
226: { NULL, NULL }, /* Fx */
227: { NULL, posts_text }, /* Ms */
228: { NULL, posts_notext }, /* No */
229: { NULL, posts_notext }, /* Ns */
230: { NULL, NULL }, /* Nx */
231: { NULL, NULL }, /* Ox */
232: { NULL, NULL }, /* Pc */
1.51 kristaps 233: { NULL, posts_text1 }, /* Pf */
1.1 kristaps 234: { NULL, NULL }, /* Po */
235: { NULL, posts_wline }, /* Pq */
236: { NULL, NULL }, /* Qc */
237: { NULL, posts_wline }, /* Ql */
238: { NULL, NULL }, /* Qo */
239: { NULL, posts_wline }, /* Qq */
240: { NULL, NULL }, /* Re */
1.43 kristaps 241: { NULL, posts_rs }, /* Rs */
1.1 kristaps 242: { NULL, NULL }, /* Sc */
243: { NULL, NULL }, /* So */
244: { NULL, posts_wline }, /* Sq */
245: { NULL, posts_bool }, /* Sm */
246: { NULL, posts_text }, /* Sx */
247: { NULL, posts_text }, /* Sy */
248: { NULL, posts_text }, /* Tn */
249: { NULL, NULL }, /* Ux */
250: { NULL, NULL }, /* Xc */
251: { NULL, NULL }, /* Xo */
252: { NULL, posts_fo }, /* Fo */
253: { NULL, NULL }, /* Fc */
254: { NULL, NULL }, /* Oo */
255: { NULL, NULL }, /* Oc */
1.101 schwarze 256: { NULL, posts_bd_bk }, /* Bk */
1.1 kristaps 257: { NULL, NULL }, /* Ek */
1.84 kristaps 258: { NULL, posts_eoln }, /* Bt */
1.1 kristaps 259: { NULL, NULL }, /* Hf */
260: { NULL, NULL }, /* Fr */
1.84 kristaps 261: { NULL, posts_eoln }, /* Ud */
1.83 kristaps 262: { NULL, posts_lb }, /* Lb */
1.34 kristaps 263: { NULL, posts_notext }, /* Lp */
1.51 kristaps 264: { NULL, posts_text }, /* Lk */
1.123 kristaps 265: { NULL, posts_defaults }, /* Mt */
1.1 kristaps 266: { NULL, posts_wline }, /* Brq */
267: { NULL, NULL }, /* Bro */
268: { NULL, NULL }, /* Brc */
269: { NULL, posts_text }, /* %C */
270: { NULL, NULL }, /* Es */
271: { NULL, NULL }, /* En */
272: { NULL, NULL }, /* Dx */
273: { NULL, posts_text }, /* %Q */
1.33 kristaps 274: { NULL, posts_notext }, /* br */
1.119 schwarze 275: { pres_pp, posts_sp }, /* sp */
1.51 kristaps 276: { NULL, posts_text1 }, /* %U */
1.88 kristaps 277: { NULL, NULL }, /* Ta */
1.1 kristaps 278: };
279:
1.122 kristaps 280: #define RSORD_MAX 14 /* Number of `Rs' blocks. */
281:
282: static const enum mdoct rsord[RSORD_MAX] = {
283: MDOC__A,
284: MDOC__T,
285: MDOC__B,
286: MDOC__I,
287: MDOC__J,
288: MDOC__R,
289: MDOC__N,
290: MDOC__V,
291: MDOC__P,
292: MDOC__Q,
293: MDOC__D,
294: MDOC__O,
295: MDOC__C,
296: MDOC__U
297: };
298:
1.1 kristaps 299:
300: int
1.91 kristaps 301: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1 kristaps 302: {
303: v_pre *p;
304: int line, pos;
1.92 kristaps 305: char *tp;
1.1 kristaps 306:
307: if (MDOC_TEXT == n->type) {
308: tp = n->string;
309: line = n->line;
310: pos = n->pos;
311: return(check_text(mdoc, line, pos, tp));
312: }
313:
314: if ( ! check_args(mdoc, n))
315: return(0);
316: if (NULL == mdoc_valids[n->tok].pre)
317: return(1);
318: for (p = mdoc_valids[n->tok].pre; *p; p++)
319: if ( ! (*p)(mdoc, n))
320: return(0);
321: return(1);
322: }
323:
324:
325: int
326: mdoc_valid_post(struct mdoc *mdoc)
327: {
328: v_post *p;
329:
330: if (MDOC_VALID & mdoc->last->flags)
331: return(1);
332: mdoc->last->flags |= MDOC_VALID;
333:
334: if (MDOC_TEXT == mdoc->last->type)
335: return(1);
336: if (MDOC_ROOT == mdoc->last->type)
337: return(post_root(mdoc));
338:
339: if (NULL == mdoc_valids[mdoc->last->tok].post)
340: return(1);
341: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
342: if ( ! (*p)(mdoc))
343: return(0);
344:
345: return(1);
346: }
347:
1.120 kristaps 348: static int
349: check_count(struct mdoc *m, enum mdoc_type type,
350: enum check_lvl lvl, enum check_ineq ineq, int val)
351: {
352: const char *p;
353:
354: if (m->last->type != type)
355: return(1);
356:
357: switch (ineq) {
358: case (CHECK_LT):
359: p = "less than ";
360: if (m->last->nchild < val)
361: return(1);
362: break;
363: case (CHECK_GT):
364: p = "greater than ";
365: if (m->last->nchild > val)
366: return(1);
367: break;
368: case (CHECK_EQ):
369: p = "";
370: if (val == m->last->nchild)
371: return(1);
372: break;
373: }
374:
375: if (CHECK_WARN == lvl) {
376: return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
377: m->last->line, m->last->pos,
378: "want %s%d children (have %d)",
379: p, val, m->last->nchild));
380: }
381:
382: return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
383: m->last->line, m->last->pos,
384: "require %s%d children (have %d)",
385: p, val, m->last->nchild));
386: }
387:
388: static int
389: berr_ge1(POST_ARGS)
390: {
391:
392: return(check_count(mdoc, MDOC_BODY, CHECK_FATAL, CHECK_GT, 0));
393: }
394:
395: static int
396: bwarn_ge1(POST_ARGS)
397: {
398: return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
399: }
400:
401: static int
402: eerr_eq0(POST_ARGS)
403: {
404: return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 0));
405: }
1.1 kristaps 406:
1.120 kristaps 407: static int
408: eerr_eq1(POST_ARGS)
1.1 kristaps 409: {
1.120 kristaps 410: return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 1));
411: }
1.1 kristaps 412:
1.120 kristaps 413: static int
414: eerr_ge1(POST_ARGS)
415: {
416: return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_GT, 0));
1.1 kristaps 417: }
418:
1.120 kristaps 419: static int
420: eerr_le1(POST_ARGS)
421: {
422: return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_LT, 2));
423: }
1.1 kristaps 424:
1.120 kristaps 425: static int
426: ewarn_eq0(POST_ARGS)
1.1 kristaps 427: {
1.120 kristaps 428: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
429: }
1.1 kristaps 430:
1.120 kristaps 431: static int
432: ewarn_ge1(POST_ARGS)
433: {
434: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1 kristaps 435: }
436:
1.120 kristaps 437: static int
438: herr_eq0(POST_ARGS)
439: {
440: return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_EQ, 0));
441: }
1.1 kristaps 442:
1.120 kristaps 443: static int
444: herr_ge1(POST_ARGS)
445: {
446: return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_GT, 0));
447: }
1.1 kristaps 448:
1.120 kristaps 449: static int
450: hwarn_eq0(POST_ARGS)
451: {
452: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.1 kristaps 453: }
454:
1.120 kristaps 455: static int
456: hwarn_eq1(POST_ARGS)
457: {
458: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
459: }
1.1 kristaps 460:
1.120 kristaps 461: static int
462: hwarn_le1(POST_ARGS)
463: {
464: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
465: }
1.1 kristaps 466:
467:
468: static int
469: check_stdarg(PRE_ARGS)
470: {
471:
472: if (n->args && 1 == n->args->argc)
473: if (MDOC_Std == n->args->argv[0].arg)
474: return(1);
1.79 kristaps 475: return(mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV));
1.1 kristaps 476: }
477:
478:
479: static int
1.92 kristaps 480: check_args(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 481: {
482: int i;
483:
484: if (NULL == n->args)
485: return(1);
486:
487: assert(n->args->argc);
488: for (i = 0; i < (int)n->args->argc; i++)
489: if ( ! check_argv(m, n, &n->args->argv[i]))
490: return(0);
491:
492: return(1);
493: }
494:
495:
496: static int
1.92 kristaps 497: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1 kristaps 498: {
499: int i;
500:
501: for (i = 0; i < (int)v->sz; i++)
502: if ( ! check_text(m, v->line, v->pos, v->value[i]))
503: return(0);
504:
505: if (MDOC_Std == v->arg) {
506: if (v->sz || m->meta.name)
507: return(1);
1.79 kristaps 508: if ( ! mdoc_nmsg(m, n, MANDOCERR_NONAME))
509: return(0);
1.1 kristaps 510: }
511:
512: return(1);
513: }
514:
515:
516: static int
1.112 kristaps 517: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1 kristaps 518: {
1.18 kristaps 519: int c;
1.112 kristaps 520: size_t sz;
1.102 kristaps 521:
1.112 kristaps 522: for ( ; *p; p++, pos++) {
523: sz = strcspn(p, "\t\\");
524: p += (int)sz;
525:
526: if ('\0' == *p)
527: break;
528:
529: pos += (int)sz;
1.1 kristaps 530:
531: if ('\t' == *p) {
1.112 kristaps 532: if (MDOC_LITERAL & m->flags)
533: continue;
534: if (mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB))
535: continue;
536: return(0);
537: }
1.1 kristaps 538:
1.112 kristaps 539: /* Check the special character. */
1.1 kristaps 540:
1.18 kristaps 541: c = mandoc_special(p);
1.1 kristaps 542: if (c) {
1.18 kristaps 543: p += c - 1;
544: pos += c - 1;
1.115 schwarze 545: } else
546: mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
1.1 kristaps 547: }
548:
549: return(1);
550: }
551:
552:
553: static int
1.59 kristaps 554: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1 kristaps 555: {
556:
557: assert(n->parent);
558: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
559: (t == n->parent->type))
560: return(1);
561:
1.79 kristaps 562: mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
563: n->line, n->pos, "want parent %s",
564: MDOC_ROOT == t ? "<root>" :
565: mdoc_macronames[tok]);
566: return(0);
1.1 kristaps 567: }
568:
569:
570: static int
571: pre_display(PRE_ARGS)
572: {
573: struct mdoc_node *node;
574:
575: /* Display elements (`Bd', `D1'...) cannot be nested. */
576:
577: if (MDOC_BLOCK != n->type)
578: return(1);
579:
580: /* LINTED */
581: for (node = mdoc->last->parent; node; node = node->parent)
582: if (MDOC_BLOCK == node->type)
583: if (MDOC_Bd == node->tok)
584: break;
585: if (NULL == node)
586: return(1);
587:
1.79 kristaps 588: mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
589: return(0);
1.1 kristaps 590: }
591:
592:
593: static int
594: pre_bl(PRE_ARGS)
595: {
1.104 kristaps 596: int i, comp, dup;
597: const char *offs, *width;
598: enum mdoc_list lt;
599: struct mdoc_node *np;
1.1 kristaps 600:
1.91 kristaps 601: if (MDOC_BLOCK != n->type) {
1.104 kristaps 602: if (ENDBODY_NOT != n->end) {
603: assert(n->pending);
604: np = n->pending->parent;
605: } else
606: np = n->parent;
607:
608: assert(np);
609: assert(MDOC_BLOCK == np->type);
610: assert(MDOC_Bl == np->tok);
611: assert(np->data.Bl);
612: n->data.Bl = np->data.Bl;
1.1 kristaps 613: return(1);
1.79 kristaps 614: }
1.1 kristaps 615:
1.91 kristaps 616: /*
617: * First figure out which kind of list to use: bind ourselves to
618: * the first mentioned list type and warn about any remaining
619: * ones. If we find no list type, we default to LIST_item.
620: */
1.1 kristaps 621:
1.104 kristaps 622: assert(NULL == n->data.Bl);
623: n->data.Bl = mandoc_calloc(1, sizeof(struct mdoc_bl));
1.1 kristaps 624:
625: /* LINTED */
1.91 kristaps 626: for (i = 0; n->args && i < (int)n->args->argc; i++) {
627: lt = LIST__NONE;
1.97 kristaps 628: dup = comp = 0;
1.99 kristaps 629: width = offs = NULL;
1.91 kristaps 630: switch (n->args->argv[i].arg) {
631: /* Set list types. */
1.1 kristaps 632: case (MDOC_Bullet):
1.91 kristaps 633: lt = LIST_bullet;
634: break;
1.1 kristaps 635: case (MDOC_Dash):
1.91 kristaps 636: lt = LIST_dash;
637: break;
1.1 kristaps 638: case (MDOC_Enum):
1.91 kristaps 639: lt = LIST_enum;
640: break;
1.1 kristaps 641: case (MDOC_Hyphen):
1.91 kristaps 642: lt = LIST_hyphen;
643: break;
1.1 kristaps 644: case (MDOC_Item):
1.91 kristaps 645: lt = LIST_item;
646: break;
1.1 kristaps 647: case (MDOC_Tag):
1.91 kristaps 648: lt = LIST_tag;
649: break;
1.1 kristaps 650: case (MDOC_Diag):
1.91 kristaps 651: lt = LIST_diag;
652: break;
1.1 kristaps 653: case (MDOC_Hang):
1.91 kristaps 654: lt = LIST_hang;
655: break;
1.1 kristaps 656: case (MDOC_Ohang):
1.91 kristaps 657: lt = LIST_ohang;
658: break;
1.1 kristaps 659: case (MDOC_Inset):
1.91 kristaps 660: lt = LIST_inset;
661: break;
1.1 kristaps 662: case (MDOC_Column):
1.91 kristaps 663: lt = LIST_column;
664: break;
665: /* Set list arguments. */
1.45 kristaps 666: case (MDOC_Compact):
1.104 kristaps 667: dup = n->data.Bl->comp;
1.97 kristaps 668: comp = 1;
1.91 kristaps 669: break;
1.1 kristaps 670: case (MDOC_Width):
1.104 kristaps 671: dup = (NULL != n->data.Bl->width);
1.99 kristaps 672: width = n->args->argv[i].value[0];
1.8 kristaps 673: break;
1.1 kristaps 674: case (MDOC_Offset):
1.98 kristaps 675: /* NB: this can be empty! */
676: if (n->args->argv[i].sz) {
677: offs = n->args->argv[i].value[0];
1.104 kristaps 678: dup = (NULL != n->data.Bl->offs);
1.98 kristaps 679: break;
680: }
681: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
682: return(0);
1.1 kristaps 683: break;
1.113 kristaps 684: default:
685: continue;
1.1 kristaps 686: }
687:
1.91 kristaps 688: /* Check: duplicate auxiliary arguments. */
689:
1.97 kristaps 690: if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
691: return(0);
692:
693: if (comp && ! dup)
1.104 kristaps 694: n->data.Bl->comp = comp;
1.98 kristaps 695: if (offs && ! dup)
1.104 kristaps 696: n->data.Bl->offs = offs;
1.99 kristaps 697: if (width && ! dup)
1.104 kristaps 698: n->data.Bl->width = width;
1.91 kristaps 699:
700: /* Check: multiple list types. */
701:
1.104 kristaps 702: if (LIST__NONE != lt && n->data.Bl->type != LIST__NONE)
1.91 kristaps 703: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP))
704: return(0);
705:
706: /* Assign list type. */
707:
1.109 kristaps 708: if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
1.104 kristaps 709: n->data.Bl->type = lt;
1.109 kristaps 710: /* Set column information, too. */
711: if (LIST_column == lt) {
712: n->data.Bl->ncols =
713: n->args->argv[i].sz;
714: n->data.Bl->cols = (const char **)
715: n->args->argv[i].value;
716: }
717: }
1.91 kristaps 718:
719: /* The list type should come first. */
720:
1.104 kristaps 721: if (n->data.Bl->type == LIST__NONE)
722: if (n->data.Bl->width ||
723: n->data.Bl->offs ||
724: n->data.Bl->comp)
1.91 kristaps 725: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
726: return(0);
727:
728: continue;
729: }
730:
731: /* Allow lists to default to LIST_item. */
732:
1.104 kristaps 733: if (LIST__NONE == n->data.Bl->type) {
1.91 kristaps 734: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE))
735: return(0);
1.104 kristaps 736: n->data.Bl->type = LIST_item;
1.79 kristaps 737: }
1.1 kristaps 738:
1.8 kristaps 739: /*
740: * Validate the width field. Some list types don't need width
741: * types and should be warned about them. Others should have it
742: * and must also be warned.
743: */
744:
1.104 kristaps 745: switch (n->data.Bl->type) {
1.91 kristaps 746: case (LIST_tag):
1.104 kristaps 747: if (n->data.Bl->width)
1.91 kristaps 748: break;
749: if (mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG))
750: break;
751: return(0);
752: case (LIST_column):
1.1 kristaps 753: /* FALLTHROUGH */
1.91 kristaps 754: case (LIST_diag):
1.1 kristaps 755: /* FALLTHROUGH */
1.91 kristaps 756: case (LIST_ohang):
1.53 kristaps 757: /* FALLTHROUGH */
1.91 kristaps 758: case (LIST_inset):
1.1 kristaps 759: /* FALLTHROUGH */
1.91 kristaps 760: case (LIST_item):
1.104 kristaps 761: if (NULL == n->data.Bl->width)
1.91 kristaps 762: break;
763: if (mdoc_nmsg(mdoc, n, MANDOCERR_WIDTHARG))
764: break;
765: return(0);
1.8 kristaps 766: default:
767: break;
768: }
769:
1.1 kristaps 770: return(1);
771: }
772:
773:
774: static int
775: pre_bd(PRE_ARGS)
776: {
1.104 kristaps 777: int i, dup, comp;
778: enum mdoc_disp dt;
779: const char *offs;
780: struct mdoc_node *np;
1.1 kristaps 781:
1.93 kristaps 782: if (MDOC_BLOCK != n->type) {
1.104 kristaps 783: if (ENDBODY_NOT != n->end) {
784: assert(n->pending);
785: np = n->pending->parent;
786: } else
787: np = n->parent;
788:
789: assert(np);
790: assert(MDOC_BLOCK == np->type);
791: assert(MDOC_Bd == np->tok);
792: assert(np->data.Bd);
793: n->data.Bd = np->data.Bd;
1.1 kristaps 794: return(1);
1.79 kristaps 795: }
1.1 kristaps 796:
1.104 kristaps 797: assert(NULL == n->data.Bd);
798: n->data.Bd = mandoc_calloc(1, sizeof(struct mdoc_bd));
1.1 kristaps 799:
800: /* LINTED */
1.93 kristaps 801: for (i = 0; n->args && i < (int)n->args->argc; i++) {
802: dt = DISP__NONE;
1.94 kristaps 803: dup = comp = 0;
804: offs = NULL;
805:
1.1 kristaps 806: switch (n->args->argv[i].arg) {
1.49 kristaps 807: case (MDOC_Centred):
1.93 kristaps 808: dt = DISP_centred;
809: break;
1.1 kristaps 810: case (MDOC_Ragged):
1.93 kristaps 811: dt = DISP_ragged;
812: break;
1.1 kristaps 813: case (MDOC_Unfilled):
1.93 kristaps 814: dt = DISP_unfilled;
815: break;
1.1 kristaps 816: case (MDOC_Filled):
1.93 kristaps 817: dt = DISP_filled;
818: break;
1.1 kristaps 819: case (MDOC_Literal):
1.93 kristaps 820: dt = DISP_literal;
1.79 kristaps 821: break;
1.93 kristaps 822: case (MDOC_File):
823: mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
824: return(0);
825: case (MDOC_Offset):
1.94 kristaps 826: /* NB: this can be empty! */
827: if (n->args->argv[i].sz) {
828: offs = n->args->argv[i].value[0];
1.104 kristaps 829: dup = (NULL != n->data.Bd->offs);
1.94 kristaps 830: break;
831: }
1.95 kristaps 832: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
833: return(0);
1.94 kristaps 834: break;
1.93 kristaps 835: case (MDOC_Compact):
1.94 kristaps 836: comp = 1;
1.104 kristaps 837: dup = n->data.Bd->comp;
1.94 kristaps 838: break;
1.1 kristaps 839: default:
1.94 kristaps 840: abort();
841: /* NOTREACHED */
1.1 kristaps 842: }
843:
1.94 kristaps 844: /* Check whether we have duplicates. */
845:
846: if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
847: return(0);
848:
849: /* Make our auxiliary assignments. */
850:
851: if (offs && ! dup)
1.104 kristaps 852: n->data.Bd->offs = offs;
1.94 kristaps 853: if (comp && ! dup)
1.104 kristaps 854: n->data.Bd->comp = comp;
1.94 kristaps 855:
856: /* Check whether a type has already been assigned. */
857:
1.104 kristaps 858: if (DISP__NONE != dt && n->data.Bd->type != DISP__NONE)
1.93 kristaps 859: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP))
860: return(0);
861:
1.94 kristaps 862: /* Make our type assignment. */
863:
1.104 kristaps 864: if (DISP__NONE != dt && n->data.Bd->type == DISP__NONE)
865: n->data.Bd->type = dt;
1.93 kristaps 866: }
867:
1.104 kristaps 868: if (DISP__NONE == n->data.Bd->type) {
1.93 kristaps 869: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE))
870: return(0);
1.104 kristaps 871: n->data.Bd->type = DISP_ragged;
1.93 kristaps 872: }
873:
874: return(1);
1.1 kristaps 875: }
876:
877:
878: static int
879: pre_ss(PRE_ARGS)
880: {
881:
882: if (MDOC_BLOCK != n->type)
883: return(1);
884: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
885: }
886:
887:
888: static int
889: pre_sh(PRE_ARGS)
890: {
891:
892: if (MDOC_BLOCK != n->type)
893: return(1);
1.100 kristaps 894:
895: mdoc->regs->regs[(int)REG_nS].set = 0;
1.70 kristaps 896: return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1 kristaps 897: }
898:
899:
900: static int
901: pre_it(PRE_ARGS)
902: {
903:
904: if (MDOC_BLOCK != n->type)
905: return(1);
1.79 kristaps 906: /*
907: * FIXME: this can probably be lifted if we make the It into
908: * something else on-the-fly?
909: */
1.1 kristaps 910: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
911: }
912:
913:
914: static int
915: pre_an(PRE_ARGS)
916: {
1.121 kristaps 917: int i;
1.1 kristaps 918:
1.107 kristaps 919: if (NULL == n->args)
1.1 kristaps 920: return(1);
1.121 kristaps 921:
922: for (i = 1; i < (int)n->args->argc; i++)
923: if ( ! mdoc_pmsg(mdoc, n->args->argv[i].line,
924: n->args->argv[i].pos, MANDOCERR_IGNARGV))
1.107 kristaps 925: return(0);
1.120 kristaps 926:
1.107 kristaps 927: if (MDOC_Split == n->args->argv[0].arg)
928: n->data.An.auth = AUTH_split;
929: else if (MDOC_Nosplit == n->args->argv[0].arg)
930: n->data.An.auth = AUTH_nosplit;
931: else
932: abort();
933:
934: return(1);
1.1 kristaps 935: }
936:
937:
938: static int
939: pre_rv(PRE_ARGS)
940: {
941:
942: return(check_stdarg(mdoc, n));
943: }
944:
945:
946: static int
1.85 kristaps 947: post_dt(POST_ARGS)
948: {
949: const struct mdoc_node *nn;
950: const char *p;
951:
952: if (NULL != (nn = mdoc->last->child))
953: for (p = nn->string; *p; p++) {
1.86 kristaps 954: if (toupper((u_char)*p) == *p)
1.85 kristaps 955: continue;
956: if ( ! mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE))
957: return(0);
958: break;
959: }
960:
961: return(1);
962: }
963:
964:
965: static int
1.1 kristaps 966: pre_dt(PRE_ARGS)
967: {
1.54 kristaps 968:
1.1 kristaps 969: if (0 == mdoc->meta.date || mdoc->meta.os)
1.79 kristaps 970: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1 kristaps 971: return(0);
972: if (mdoc->meta.title)
1.79 kristaps 973: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1 kristaps 974: return(0);
975: return(1);
976: }
977:
978:
979: static int
980: pre_os(PRE_ARGS)
981: {
982:
983: if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.79 kristaps 984: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1 kristaps 985: return(0);
986: if (mdoc->meta.os)
1.79 kristaps 987: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1 kristaps 988: return(0);
989: return(1);
990: }
991:
992:
993: static int
994: pre_dd(PRE_ARGS)
995: {
996:
997: if (mdoc->meta.title || mdoc->meta.os)
1.79 kristaps 998: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1 kristaps 999: return(0);
1000: if (mdoc->meta.date)
1.79 kristaps 1001: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1 kristaps 1002: return(0);
1003: return(1);
1004: }
1005:
1006:
1007: static int
1008: post_bf(POST_ARGS)
1009: {
1.105 kristaps 1010: struct mdoc_node *np;
1.113 kristaps 1011: enum mdocargt arg;
1.105 kristaps 1012:
1013: /*
1014: * Unlike other data pointers, these are "housed" by the HEAD
1015: * element, which contains the goods.
1016: */
1.1 kristaps 1017:
1.105 kristaps 1018: if (MDOC_HEAD != mdoc->last->type) {
1019: if (ENDBODY_NOT != mdoc->last->end) {
1020: assert(mdoc->last->pending);
1021: np = mdoc->last->pending->parent->head;
1022: } else if (MDOC_BLOCK != mdoc->last->type) {
1023: np = mdoc->last->parent->head;
1024: } else
1025: np = mdoc->last->head;
1026:
1027: assert(np);
1028: assert(MDOC_HEAD == np->type);
1029: assert(MDOC_Bf == np->tok);
1030: assert(np->data.Bf);
1031: mdoc->last->data.Bf = np->data.Bf;
1.1 kristaps 1032: return(1);
1.105 kristaps 1033: }
1.1 kristaps 1034:
1.105 kristaps 1035: np = mdoc->last;
1.106 kristaps 1036: assert(MDOC_BLOCK == np->parent->type);
1037: assert(MDOC_Bf == np->parent->tok);
1.105 kristaps 1038: np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
1.1 kristaps 1039:
1.105 kristaps 1040: /*
1041: * Cannot have both argument and parameter.
1042: * If neither is specified, let it through with a warning.
1043: */
1044:
1.106 kristaps 1045: if (np->parent->args && np->child) {
1.105 kristaps 1046: mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.79 kristaps 1047: return(0);
1.106 kristaps 1048: } else if (NULL == np->parent->args && NULL == np->child)
1.105 kristaps 1049: return(mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE));
1050:
1051: /* Extract argument into data. */
1052:
1.106 kristaps 1053: if (np->parent->args) {
1054: arg = np->parent->args->argv[0].arg;
1.105 kristaps 1055: if (MDOC_Emphasis == arg)
1056: np->data.Bf->font = FONT_Em;
1057: else if (MDOC_Literal == arg)
1058: np->data.Bf->font = FONT_Li;
1059: else if (MDOC_Symbolic == arg)
1060: np->data.Bf->font = FONT_Sy;
1061: else
1062: abort();
1.13 kristaps 1063: return(1);
1.79 kristaps 1064: }
1.1 kristaps 1065:
1.105 kristaps 1066: /* Extract parameter into data. */
1.1 kristaps 1067:
1.105 kristaps 1068: if (0 == strcmp(np->child->string, "Em"))
1069: np->data.Bf->font = FONT_Em;
1070: else if (0 == strcmp(np->child->string, "Li"))
1071: np->data.Bf->font = FONT_Li;
1072: else if (0 == strcmp(np->child->string, "Sy"))
1073: np->data.Bf->font = FONT_Sy;
1074: else if ( ! mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE))
1075: return(0);
1.1 kristaps 1076:
1.105 kristaps 1077: return(1);
1.1 kristaps 1078: }
1079:
1080:
1081: static int
1.31 kristaps 1082: post_lb(POST_ARGS)
1083: {
1084:
1085: if (mdoc_a2lib(mdoc->last->child->string))
1086: return(1);
1.79 kristaps 1087: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADLIB));
1.84 kristaps 1088: }
1089:
1090:
1091: static int
1092: post_eoln(POST_ARGS)
1093: {
1094:
1095: if (NULL == mdoc->last->child)
1096: return(1);
1097: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST));
1.31 kristaps 1098: }
1099:
1100:
1101: static int
1.57 kristaps 1102: post_vt(POST_ARGS)
1103: {
1104: const struct mdoc_node *n;
1105:
1106: /*
1107: * The Vt macro comes in both ELEM and BLOCK form, both of which
1108: * have different syntaxes (yet more context-sensitive
1109: * behaviour). ELEM types must have a child; BLOCK types,
1110: * specifically the BODY, should only have TEXT children.
1111: */
1112:
1113: if (MDOC_ELEM == mdoc->last->type)
1114: return(eerr_ge1(mdoc));
1115: if (MDOC_BODY != mdoc->last->type)
1116: return(1);
1117:
1118: for (n = mdoc->last->child; n; n = n->next)
1119: if (MDOC_TEXT != n->type)
1.79 kristaps 1120: if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_CHILD))
1.57 kristaps 1121: return(0);
1122:
1123: return(1);
1124: }
1125:
1126:
1127: static int
1.1 kristaps 1128: post_nm(POST_ARGS)
1129: {
1130:
1131: if (mdoc->last->child)
1132: return(1);
1133: if (mdoc->meta.name)
1134: return(1);
1.79 kristaps 1135: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME));
1.1 kristaps 1136: }
1137:
1.123 kristaps 1138: static int
1139: post_defaults(POST_ARGS)
1140: {
1141: struct mdoc_node *nn;
1142:
1143: /*
1144: * The `Ar' defaults to "file ..." if no value is provided as an
1145: * argument; the `Mt' macro uses "~"; the `Li' just gets an
1146: * empty string.
1147: */
1148:
1149: if (mdoc->last->child)
1150: return(1);
1151:
1152: nn = mdoc->last;
1153: mdoc->next = MDOC_NEXT_CHILD;
1154:
1155: switch (nn->tok) {
1156: case (MDOC_Ar):
1157: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
1158: return(0);
1159: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
1160: return(0);
1161: break;
1.126 ! kristaps 1162: case (MDOC_At):
! 1163: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
! 1164: return(0);
! 1165: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
! 1166: return(0);
! 1167: break;
1.123 kristaps 1168: case (MDOC_Li):
1169: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
1170: return(0);
1171: break;
1172: case (MDOC_Mt):
1173: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
1174: return(0);
1175: break;
1176: default:
1177: abort();
1178: /* NOTREACHED */
1179: }
1180:
1181: mdoc->last = nn;
1182: return(1);
1183: }
1.1 kristaps 1184:
1185: static int
1186: post_at(POST_ARGS)
1187: {
1.126 ! kristaps 1188: const char *p, *q;
! 1189: char *buf;
! 1190: size_t sz;
1.1 kristaps 1191:
1.126 ! kristaps 1192: /*
! 1193: * If we have a child, look it up in the standard keys. If a
! 1194: * key exist, use that instead of the child; if it doesn't,
! 1195: * prefix "AT&T UNIX " to the existing data.
! 1196: */
! 1197:
1.1 kristaps 1198: if (NULL == mdoc->last->child)
1199: return(1);
1.126 ! kristaps 1200:
1.79 kristaps 1201: assert(MDOC_TEXT == mdoc->last->child->type);
1.126 ! kristaps 1202: p = mdoc_a2att(mdoc->last->child->string);
! 1203:
! 1204: if (p) {
! 1205: free(mdoc->last->child->string);
! 1206: mdoc->last->child->string = mandoc_strdup(p);
! 1207: } else {
! 1208: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
! 1209: p = "AT&T UNIX ";
! 1210: q = mdoc->last->child->string;
! 1211: sz = strlen(p) + strlen(q) + 1;
! 1212: buf = mandoc_malloc(sz);
! 1213: strlcpy(buf, p, sz);
! 1214: strlcat(buf, q, sz);
! 1215: free(mdoc->last->child->string);
! 1216: mdoc->last->child->string = buf;
! 1217: }
! 1218:
! 1219: return(1);
1.1 kristaps 1220: }
1221:
1222: static int
1223: post_an(POST_ARGS)
1224: {
1.107 kristaps 1225: struct mdoc_node *np;
1.1 kristaps 1226:
1.107 kristaps 1227: np = mdoc->last;
1228: if (AUTH__NONE != np->data.An.auth && np->child)
1.120 kristaps 1229: return(eerr_eq0(mdoc));
1230: /*
1231: * FIXME: make this ewarn and make sure that the front-ends
1232: * don't print the arguments.
1233: */
1.107 kristaps 1234: if (AUTH__NONE != np->data.An.auth || np->child)
1.1 kristaps 1235: return(1);
1.107 kristaps 1236: return(mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS));
1.1 kristaps 1237: }
1238:
1239:
1240: static int
1241: post_it(POST_ARGS)
1242: {
1.89 kristaps 1243: int i, cols, rc;
1244: enum mdoc_list lt;
1.1 kristaps 1245: struct mdoc_node *n, *c;
1.89 kristaps 1246: enum mandocerr er;
1.1 kristaps 1247:
1248: if (MDOC_BLOCK != mdoc->last->type)
1249: return(1);
1250:
1251: n = mdoc->last->parent->parent;
1.104 kristaps 1252: assert(n->data.Bl);
1253: lt = n->data.Bl->type;
1.89 kristaps 1254:
1255: if (LIST__NONE == lt) {
1.79 kristaps 1256: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1257: return(0);
1258: }
1.1 kristaps 1259:
1.89 kristaps 1260: switch (lt) {
1261: case (LIST_tag):
1262: if (mdoc->last->head->child)
1.1 kristaps 1263: break;
1.89 kristaps 1264: /* FIXME: give this a dummy value. */
1265: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
1266: return(0);
1.1 kristaps 1267: break;
1.89 kristaps 1268: case (LIST_hang):
1.1 kristaps 1269: /* FALLTHROUGH */
1.89 kristaps 1270: case (LIST_ohang):
1.1 kristaps 1271: /* FALLTHROUGH */
1.89 kristaps 1272: case (LIST_inset):
1.1 kristaps 1273: /* FALLTHROUGH */
1.89 kristaps 1274: case (LIST_diag):
1.1 kristaps 1275: if (NULL == mdoc->last->head->child)
1.79 kristaps 1276: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
1.1 kristaps 1277: return(0);
1278: break;
1.89 kristaps 1279: case (LIST_bullet):
1.1 kristaps 1280: /* FALLTHROUGH */
1.89 kristaps 1281: case (LIST_dash):
1.1 kristaps 1282: /* FALLTHROUGH */
1.89 kristaps 1283: case (LIST_enum):
1.1 kristaps 1284: /* FALLTHROUGH */
1.89 kristaps 1285: case (LIST_hyphen):
1.108 schwarze 1286: if (NULL == mdoc->last->body->child)
1287: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
1288: return(0);
1.1 kristaps 1289: /* FALLTHROUGH */
1.89 kristaps 1290: case (LIST_item):
1.1 kristaps 1291: if (mdoc->last->head->child)
1.79 kristaps 1292: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST))
1.1 kristaps 1293: return(0);
1294: break;
1.89 kristaps 1295: case (LIST_column):
1.109 kristaps 1296: cols = (int)n->data.Bl->ncols;
1.89 kristaps 1297:
1.87 kristaps 1298: assert(NULL == mdoc->last->head->child);
1.89 kristaps 1299:
1.87 kristaps 1300: if (NULL == mdoc->last->body->child)
1301: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
1.1 kristaps 1302: return(0);
1.87 kristaps 1303:
1.89 kristaps 1304: for (i = 0, c = mdoc->last->child; c; c = c->next)
1.87 kristaps 1305: if (MDOC_BODY == c->type)
1306: i++;
1.38 kristaps 1307:
1.89 kristaps 1308: if (i < cols)
1309: er = MANDOCERR_ARGCOUNT;
1310: else if (i == cols || i == cols + 1)
1.38 kristaps 1311: break;
1.89 kristaps 1312: else
1313: er = MANDOCERR_SYNTARGCOUNT;
1.38 kristaps 1314:
1.89 kristaps 1315: rc = mdoc_vmsg(mdoc, er,
1.79 kristaps 1316: mdoc->last->line, mdoc->last->pos,
1317: "columns == %d (have %d)", cols, i);
1.89 kristaps 1318: return(rc);
1.1 kristaps 1319: default:
1320: break;
1321: }
1322:
1323: return(1);
1324: }
1325:
1326:
1327: static int
1.14 kristaps 1328: post_bl_head(POST_ARGS)
1329: {
1.90 kristaps 1330: struct mdoc_node *n;
1.14 kristaps 1331:
1.90 kristaps 1332: assert(mdoc->last->parent);
1.14 kristaps 1333: n = mdoc->last->parent;
1334:
1.104 kristaps 1335: if (LIST_column == n->data.Bl->type) {
1.109 kristaps 1336: if (n->data.Bl->ncols && mdoc->last->nchild) {
1.90 kristaps 1337: mdoc_nmsg(mdoc, n, MANDOCERR_COLUMNS);
1338: return(0);
1.65 kristaps 1339: }
1.90 kristaps 1340: return(1);
1.65 kristaps 1341: }
1.14 kristaps 1342:
1.120 kristaps 1343: /* FIXME: should be ERROR class. */
1344: return(hwarn_eq0(mdoc));
1.14 kristaps 1345: }
1346:
1347:
1348: static int
1.1 kristaps 1349: post_bl(POST_ARGS)
1350: {
1351: struct mdoc_node *n;
1352:
1.14 kristaps 1353: if (MDOC_HEAD == mdoc->last->type)
1354: return(post_bl_head(mdoc));
1.1 kristaps 1355: if (MDOC_BODY != mdoc->last->type)
1356: return(1);
1357: if (NULL == mdoc->last->child)
1358: return(1);
1359:
1.55 kristaps 1360: /*
1361: * We only allow certain children of `Bl'. This is usually on
1362: * `It', but apparently `Sm' occurs here and there, so we let
1363: * that one through, too.
1364: */
1365:
1.1 kristaps 1366: /* LINTED */
1367: for (n = mdoc->last->child; n; n = n->next) {
1.55 kristaps 1368: if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
1369: continue;
1370: if (MDOC_Sm == n->tok)
1371: continue;
1.79 kristaps 1372: mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
1373: return(0);
1.1 kristaps 1374: }
1375:
1376: return(1);
1377: }
1378:
1379:
1380: static int
1381: ebool(struct mdoc *mdoc)
1382: {
1383: struct mdoc_node *n;
1384:
1385: /* LINTED */
1386: for (n = mdoc->last->child; n; n = n->next) {
1387: if (MDOC_TEXT != n->type)
1388: break;
1.2 kristaps 1389: if (0 == strcmp(n->string, "on"))
1.1 kristaps 1390: continue;
1.2 kristaps 1391: if (0 == strcmp(n->string, "off"))
1.1 kristaps 1392: continue;
1393: break;
1394: }
1395:
1396: if (NULL == n)
1397: return(1);
1.79 kristaps 1398: return(mdoc_nmsg(mdoc, n, MANDOCERR_BADBOOL));
1.1 kristaps 1399: }
1400:
1401:
1402: static int
1403: post_root(POST_ARGS)
1404: {
1405:
1406: if (NULL == mdoc->first->child)
1.79 kristaps 1407: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1408: else if ( ! (MDOC_PBODY & mdoc->flags))
1409: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1410: else if (MDOC_BLOCK != mdoc->first->child->type)
1411: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1412: else if (MDOC_Sh != mdoc->first->child->tok)
1413: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1414: else
1415: return(1);
1.1 kristaps 1416:
1.79 kristaps 1417: return(0);
1.1 kristaps 1418: }
1419:
1420:
1421: static int
1422: post_st(POST_ARGS)
1423: {
1424:
1425: if (mdoc_a2st(mdoc->last->child->string))
1426: return(1);
1.79 kristaps 1427: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD));
1.1 kristaps 1428: }
1429:
1430:
1431: static int
1.43 kristaps 1432: post_rs(POST_ARGS)
1433: {
1.122 kristaps 1434: struct mdoc_node *nn, *next, *prev;
1435: int i, j;
1.43 kristaps 1436:
1437: if (MDOC_BODY != mdoc->last->type)
1438: return(1);
1439:
1.122 kristaps 1440: /*
1441: * Make sure only certain types of nodes are allowed within the
1442: * the `Rs' body. Delete offending nodes and raise a warning.
1443: * Do this before re-ordering for the sake of clarity.
1444: */
1445:
1446: next = NULL;
1447: for (nn = mdoc->last->child; nn; nn = next) {
1448: for (i = 0; i < RSORD_MAX; i++)
1449: if (nn->tok == rsord[i])
1450: break;
1451:
1452: if (i < RSORD_MAX) {
1453: next = nn->next;
1454: continue;
1455: }
1456:
1457: next = nn->next;
1458: mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
1459: mdoc_node_delete(mdoc, nn);
1460: }
1461:
1462: /*
1463: * The full `Rs' block needs special handling to order the
1464: * sub-elements according to `rsord'. Pick through each element
1465: * and correctly order it. This is a insertion sort.
1466: */
1467:
1468: next = NULL;
1469: for (nn = mdoc->last->child->next; nn; nn = next) {
1470: /* Determine order of `nn'. */
1471: for (i = 0; i < RSORD_MAX; i++)
1472: if (rsord[i] == nn->tok)
1473: break;
1474:
1475: /*
1476: * Remove `nn' from the chain. This somewhat
1477: * repeats mdoc_node_unlink(), but since we're
1478: * just re-ordering, there's no need for the
1479: * full unlink process.
1480: */
1481:
1482: if (NULL != (next = nn->next))
1483: next->prev = nn->prev;
1484:
1485: if (NULL != (prev = nn->prev))
1486: prev->next = nn->next;
1487:
1488: nn->prev = nn->next = NULL;
1489:
1490: /*
1491: * Scan back until we reach a node that's
1492: * ordered before `nn'.
1493: */
1494:
1495: for ( ; prev ; prev = prev->prev) {
1496: /* Determine order of `prev'. */
1497: for (j = 0; j < RSORD_MAX; j++)
1498: if (rsord[j] == prev->tok)
1499: break;
1500:
1501: if (j <= i)
1502: break;
1503: }
1504:
1505: /*
1506: * Set `nn' back into its correct place in front
1507: * of the `prev' node.
1508: */
1509:
1510: nn->prev = prev;
1511:
1512: if (prev) {
1513: if (prev->next)
1514: prev->next->prev = nn;
1515: nn->next = prev->next;
1516: prev->next = nn;
1517: } else {
1518: mdoc->last->child->prev = nn;
1519: nn->next = mdoc->last->child;
1520: mdoc->last->child = nn;
1.43 kristaps 1521: }
1.122 kristaps 1522: }
1.43 kristaps 1523:
1524: return(1);
1525: }
1526:
1527:
1528: static int
1.1 kristaps 1529: post_sh(POST_ARGS)
1530: {
1531:
1532: if (MDOC_HEAD == mdoc->last->type)
1533: return(post_sh_head(mdoc));
1534: if (MDOC_BODY == mdoc->last->type)
1535: return(post_sh_body(mdoc));
1536:
1537: return(1);
1538: }
1539:
1540:
1541: static int
1542: post_sh_body(POST_ARGS)
1543: {
1544: struct mdoc_node *n;
1545:
1.29 kristaps 1546: if (SEC_NAME != mdoc->lastsec)
1.1 kristaps 1547: return(1);
1548:
1549: /*
1550: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1551: * macros (can have multiple `Nm' and one `Nd'). Note that the
1552: * children of the BODY declaration can also be "text".
1553: */
1554:
1555: if (NULL == (n = mdoc->last->child))
1.79 kristaps 1556: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1 kristaps 1557:
1558: for ( ; n && n->next; n = n->next) {
1559: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1560: continue;
1.28 kristaps 1561: if (MDOC_TEXT == n->type)
1562: continue;
1.79 kristaps 1563: if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC))
1.1 kristaps 1564: return(0);
1565: }
1566:
1.41 kristaps 1567: assert(n);
1.27 kristaps 1568: if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1 kristaps 1569: return(1);
1.79 kristaps 1570: return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1 kristaps 1571: }
1572:
1573:
1574: static int
1575: post_sh_head(POST_ARGS)
1576: {
1.125 kristaps 1577: char buf[BUFSIZ];
1578: enum mdoc_sec sec;
1579: struct mdoc_node *n;
1.1 kristaps 1580:
1581: /*
1582: * Process a new section. Sections are either "named" or
1.125 kristaps 1583: * "custom". Custom sections are user-defined, while named ones
1584: * follow a conventional order and may only appear in certain
1585: * manual sections.
1.1 kristaps 1586: */
1587:
1.79 kristaps 1588: buf[0] = '\0';
1589:
1.125 kristaps 1590: /* FIXME: use dynamic buffer... */
1.1 kristaps 1591:
1.2 kristaps 1592: for (n = mdoc->last->child; n; n = n->next) {
1.125 kristaps 1593: /* XXX - copied from concat(). */
1.2 kristaps 1594: assert(MDOC_TEXT == n->type);
1.12 kristaps 1595:
1.81 kristaps 1596: if (strlcat(buf, n->string, BUFSIZ) >= BUFSIZ) {
1.79 kristaps 1597: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
1598: return(0);
1599: }
1.125 kristaps 1600:
1.2 kristaps 1601: if (NULL == n->next)
1602: continue;
1.125 kristaps 1603:
1.81 kristaps 1604: if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.79 kristaps 1605: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
1606: return(0);
1607: }
1.2 kristaps 1608: }
1.1 kristaps 1609:
1.72 kristaps 1610: sec = mdoc_str2sec(buf);
1.1 kristaps 1611:
1.125 kristaps 1612: /* The NAME should be first. */
1.1 kristaps 1613:
1.72 kristaps 1614: if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.125 kristaps 1615: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
1616:
1617: /* The SYNOPSIS gets special attention in other areas. */
1618:
1619: if (SEC_SYNOPSIS == sec)
1620: mdoc->flags |= MDOC_SYNOPSIS;
1621: else
1622: mdoc->flags &= ~MDOC_SYNOPSIS;
1623:
1624: /* Mark our last section. */
1625:
1626: mdoc->lastsec = sec;
1627:
1628: /* We don't care about custom sections after this. */
1.72 kristaps 1629:
1.1 kristaps 1630: if (SEC_CUSTOM == sec)
1631: return(1);
1.72 kristaps 1632:
1.125 kristaps 1633: /*
1634: * Check whether our non-custom section is being repeated or is
1635: * out of order.
1636: */
1637:
1.1 kristaps 1638: if (sec == mdoc->lastnamed)
1.125 kristaps 1639: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.72 kristaps 1640:
1.1 kristaps 1641: if (sec < mdoc->lastnamed)
1.125 kristaps 1642: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
1643:
1644: /* Mark the last named section. */
1645:
1646: mdoc->lastnamed = sec;
1647:
1648: /* Check particular section/manual conventions. */
1.1 kristaps 1649:
1.125 kristaps 1650: assert(mdoc->meta.msec);
1.1 kristaps 1651:
1652: switch (sec) {
1.125 kristaps 1653: case (SEC_RETURN_VALUES):
1654: /* FALLTHROUGH */
1655: case (SEC_ERRORS):
1656: /* FALLTHROUGH */
1.1 kristaps 1657: case (SEC_LIBRARY):
1.78 kristaps 1658: if (*mdoc->meta.msec == '2')
1659: break;
1660: if (*mdoc->meta.msec == '3')
1661: break;
1662: if (*mdoc->meta.msec == '9')
1.1 kristaps 1663: break;
1.125 kristaps 1664: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
1665: break;
1.1 kristaps 1666: default:
1667: break;
1668: }
1669:
1.117 kristaps 1670: return(1);
1671: }
1672:
1673: static int
1.124 kristaps 1674: pre_par(PRE_ARGS)
1.117 kristaps 1675: {
1676:
1.118 kristaps 1677: if (NULL == mdoc->last)
1678: return(1);
1679:
1.124 kristaps 1680: /*
1681: * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1682: * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1683: */
1.118 kristaps 1684:
1685: if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1.117 kristaps 1686: return(1);
1687:
1688: if (MDOC_Bl == n->tok && n->data.Bl->comp)
1689: return(1);
1690: if (MDOC_Bd == n->tok && n->data.Bd->comp)
1691: return(1);
1692:
1693: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
1694: mdoc_node_delete(mdoc, mdoc->last);
1.1 kristaps 1695: return(1);
1696: }
CVSweb