Annotation of src/usr.bin/mandoc/mdoc_validate.c, Revision 1.89
1.89 ! schwarze 1: /* $Id: mdoc_validate.c,v 1.88 2011/02/06 17:33:21 schwarze Exp $ */
1.1 kristaps 2: /*
1.84 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.89 ! schwarze 4: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.2 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.76 schwarze 18: #ifndef OSNAME
19: #include <sys/utsname.h>
20: #endif
21:
1.1 kristaps 22: #include <sys/types.h>
23:
24: #include <assert.h>
25: #include <ctype.h>
1.31 schwarze 26: #include <limits.h>
1.57 schwarze 27: #include <stdio.h>
1.1 kristaps 28: #include <stdlib.h>
29: #include <string.h>
1.76 schwarze 30: #include <time.h>
1.1 kristaps 31:
1.55 schwarze 32: #include "mandoc.h"
1.1 kristaps 33: #include "libmdoc.h"
1.15 schwarze 34: #include "libmandoc.h"
1.1 kristaps 35:
36: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
37:
1.60 schwarze 38: #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
1.28 schwarze 39: #define POST_ARGS struct mdoc *mdoc
1.1 kristaps 40:
1.76 schwarze 41: #define NUMSIZ 32
42: #define DATESIZE 32
43:
1.72 schwarze 44: enum check_ineq {
45: CHECK_LT,
46: CHECK_GT,
47: CHECK_EQ
48: };
49:
50: enum check_lvl {
51: CHECK_WARN,
52: CHECK_ERROR,
53: };
54:
1.1 kristaps 55: typedef int (*v_pre)(PRE_ARGS);
56: typedef int (*v_post)(POST_ARGS);
57:
58: struct valids {
59: v_pre *pre;
60: v_post *post;
61: };
62:
1.72 schwarze 63: static int check_count(struct mdoc *, enum mdoc_type,
64: enum check_lvl, enum check_ineq, int);
1.44 schwarze 65: static int check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.76 schwarze 66: static void check_text(struct mdoc *, int, int, char *);
67: static void check_argv(struct mdoc *,
1.61 schwarze 68: struct mdoc_node *, struct mdoc_argv *);
1.76 schwarze 69: static void check_args(struct mdoc *, struct mdoc_node *);
70:
71: static int concat(struct mdoc *, char *,
72: const struct mdoc_node *, size_t);
1.28 schwarze 73:
1.72 schwarze 74: static int ebool(POST_ARGS);
1.28 schwarze 75: static int berr_ge1(POST_ARGS);
76: static int bwarn_ge1(POST_ARGS);
1.69 schwarze 77: static int ewarn_eq0(POST_ARGS);
1.83 schwarze 78: static int ewarn_eq1(POST_ARGS);
1.28 schwarze 79: static int ewarn_ge1(POST_ARGS);
1.83 schwarze 80: static int ewarn_le1(POST_ARGS);
1.72 schwarze 81: static int hwarn_eq0(POST_ARGS);
1.28 schwarze 82: static int hwarn_eq1(POST_ARGS);
1.83 schwarze 83: static int hwarn_ge1(POST_ARGS);
1.28 schwarze 84: static int hwarn_le1(POST_ARGS);
85:
86: static int post_an(POST_ARGS);
87: static int post_at(POST_ARGS);
88: static int post_bf(POST_ARGS);
89: static int post_bl(POST_ARGS);
1.76 schwarze 90: static int post_bl_block(POST_ARGS);
91: static int post_bl_block_width(POST_ARGS);
92: static int post_bl_block_tag(POST_ARGS);
1.28 schwarze 93: static int post_bl_head(POST_ARGS);
1.87 schwarze 94: static int post_bx(POST_ARGS);
1.76 schwarze 95: static int post_dd(POST_ARGS);
1.60 schwarze 96: static int post_dt(POST_ARGS);
1.76 schwarze 97: static int post_defaults(POST_ARGS);
98: static int post_literal(POST_ARGS);
99: static int post_eoln(POST_ARGS);
1.28 schwarze 100: static int post_it(POST_ARGS);
101: static int post_lb(POST_ARGS);
102: static int post_nm(POST_ARGS);
1.88 schwarze 103: static int post_ns(POST_ARGS);
1.76 schwarze 104: static int post_os(POST_ARGS);
1.79 schwarze 105: static int post_ignpar(POST_ARGS);
1.76 schwarze 106: static int post_prol(POST_ARGS);
1.28 schwarze 107: static int post_root(POST_ARGS);
1.36 schwarze 108: static int post_rs(POST_ARGS);
1.28 schwarze 109: static int post_sh(POST_ARGS);
110: static int post_sh_body(POST_ARGS);
111: static int post_sh_head(POST_ARGS);
112: static int post_st(POST_ARGS);
1.76 schwarze 113: static int post_std(POST_ARGS);
1.42 schwarze 114: static int post_vt(POST_ARGS);
1.28 schwarze 115: static int pre_an(PRE_ARGS);
116: static int pre_bd(PRE_ARGS);
117: static int pre_bl(PRE_ARGS);
118: static int pre_dd(PRE_ARGS);
119: static int pre_display(PRE_ARGS);
120: static int pre_dt(PRE_ARGS);
121: static int pre_it(PRE_ARGS);
1.76 schwarze 122: static int pre_literal(PRE_ARGS);
1.28 schwarze 123: static int pre_os(PRE_ARGS);
1.76 schwarze 124: static int pre_par(PRE_ARGS);
1.28 schwarze 125: static int pre_sh(PRE_ARGS);
126: static int pre_ss(PRE_ARGS);
1.76 schwarze 127: static int pre_std(PRE_ARGS);
1.28 schwarze 128:
129: static v_post posts_an[] = { post_an, NULL };
1.76 schwarze 130: static v_post posts_at[] = { post_at, post_defaults, NULL };
131: static v_post posts_bd[] = { post_literal, hwarn_eq0, bwarn_ge1, NULL };
1.28 schwarze 132: static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
1.76 schwarze 133: static v_post posts_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.28 schwarze 134: static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
1.87 schwarze 135: static v_post posts_bx[] = { post_bx, NULL };
1.83 schwarze 136: static v_post posts_bool[] = { ebool, NULL };
1.59 schwarze 137: static v_post posts_eoln[] = { post_eoln, NULL };
1.76 schwarze 138: static v_post posts_defaults[] = { post_defaults, NULL };
1.89 ! schwarze 139: static v_post posts_dd[] = { post_dd, post_prol, NULL };
1.83 schwarze 140: static v_post posts_dl[] = { post_literal, bwarn_ge1, NULL };
1.76 schwarze 141: static v_post posts_dt[] = { post_dt, post_prol, NULL };
1.28 schwarze 142: static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
143: static v_post posts_it[] = { post_it, NULL };
1.83 schwarze 144: static v_post posts_lb[] = { post_lb, NULL };
1.28 schwarze 145: static v_post posts_nd[] = { berr_ge1, NULL };
146: static v_post posts_nm[] = { post_nm, NULL };
1.69 schwarze 147: static v_post posts_notext[] = { ewarn_eq0, NULL };
1.88 schwarze 148: static v_post posts_ns[] = { post_ns, NULL };
1.76 schwarze 149: static v_post posts_os[] = { post_os, post_prol, NULL };
1.83 schwarze 150: static v_post posts_rs[] = { post_rs, NULL };
151: static v_post posts_sh[] = { post_ignpar, hwarn_ge1, bwarn_ge1, post_sh, NULL };
152: static v_post posts_sp[] = { ewarn_le1, NULL };
153: static v_post posts_ss[] = { post_ignpar, hwarn_ge1, bwarn_ge1, NULL };
154: static v_post posts_st[] = { post_st, NULL };
1.76 schwarze 155: static v_post posts_std[] = { post_std, NULL };
1.85 schwarze 156: static v_post posts_text[] = { ewarn_ge1, NULL };
1.83 schwarze 157: static v_post posts_text1[] = { ewarn_eq1, NULL };
1.42 schwarze 158: static v_post posts_vt[] = { post_vt, NULL };
1.83 schwarze 159: static v_post posts_wline[] = { bwarn_ge1, NULL };
1.28 schwarze 160: static v_pre pres_an[] = { pre_an, NULL };
1.76 schwarze 161: static v_pre pres_bd[] = { pre_display, pre_bd, pre_literal, pre_par, NULL };
162: static v_pre pres_bl[] = { pre_bl, pre_par, NULL };
1.28 schwarze 163: static v_pre pres_d1[] = { pre_display, NULL };
1.76 schwarze 164: static v_pre pres_dl[] = { pre_literal, pre_display, NULL };
1.28 schwarze 165: static v_pre pres_dd[] = { pre_dd, NULL };
166: static v_pre pres_dt[] = { pre_dt, NULL };
1.48 schwarze 167: static v_pre pres_er[] = { NULL, NULL };
1.53 schwarze 168: static v_pre pres_fd[] = { NULL, NULL };
1.79 schwarze 169: static v_pre pres_it[] = { pre_it, pre_par, NULL };
1.28 schwarze 170: static v_pre pres_os[] = { pre_os, NULL };
1.76 schwarze 171: static v_pre pres_pp[] = { pre_par, NULL };
1.28 schwarze 172: static v_pre pres_sh[] = { pre_sh, NULL };
173: static v_pre pres_ss[] = { pre_ss, NULL };
1.76 schwarze 174: static v_pre pres_std[] = { pre_std, NULL };
1.1 kristaps 175:
176: const struct valids mdoc_valids[MDOC_MAX] = {
1.7 schwarze 177: { NULL, NULL }, /* Ap */
1.76 schwarze 178: { pres_dd, posts_dd }, /* Dd */
1.60 schwarze 179: { pres_dt, posts_dt }, /* Dt */
1.76 schwarze 180: { pres_os, posts_os }, /* Os */
1.1 kristaps 181: { pres_sh, posts_sh }, /* Sh */
182: { pres_ss, posts_ss }, /* Ss */
1.70 schwarze 183: { pres_pp, posts_notext }, /* Pp */
1.1 kristaps 184: { pres_d1, posts_wline }, /* D1 */
1.76 schwarze 185: { pres_dl, posts_dl }, /* Dl */
186: { pres_bd, posts_bd }, /* Bd */
1.1 kristaps 187: { NULL, NULL }, /* Ed */
188: { pres_bl, posts_bl }, /* Bl */
189: { NULL, NULL }, /* El */
190: { pres_it, posts_it }, /* It */
1.85 schwarze 191: { NULL, NULL }, /* Ad */
1.1 kristaps 192: { pres_an, posts_an }, /* An */
1.76 schwarze 193: { NULL, posts_defaults }, /* Ar */
1.85 schwarze 194: { NULL, NULL }, /* Cd */
1.1 kristaps 195: { NULL, NULL }, /* Cm */
196: { NULL, NULL }, /* Dv */
1.85 schwarze 197: { pres_er, NULL }, /* Er */
1.1 kristaps 198: { NULL, NULL }, /* Ev */
1.76 schwarze 199: { pres_std, posts_std }, /* Ex */
1.1 kristaps 200: { NULL, NULL }, /* Fa */
1.85 schwarze 201: { pres_fd, posts_text }, /* Fd */
1.1 kristaps 202: { NULL, NULL }, /* Fl */
1.85 schwarze 203: { NULL, NULL }, /* Fn */
204: { NULL, NULL }, /* Ft */
205: { NULL, NULL }, /* Ic */
1.38 schwarze 206: { NULL, posts_text1 }, /* In */
1.76 schwarze 207: { NULL, posts_defaults }, /* Li */
1.25 schwarze 208: { NULL, posts_nd }, /* Nd */
1.1 kristaps 209: { NULL, posts_nm }, /* Nm */
1.78 schwarze 210: { NULL, NULL }, /* Op */
1.1 kristaps 211: { NULL, NULL }, /* Ot */
1.76 schwarze 212: { NULL, posts_defaults }, /* Pa */
213: { pres_std, posts_std }, /* Rv */
1.1 kristaps 214: { NULL, posts_st }, /* St */
215: { NULL, NULL }, /* Va */
1.42 schwarze 216: { NULL, posts_vt }, /* Vt */
1.85 schwarze 217: { NULL, posts_text }, /* Xr */
1.1 kristaps 218: { NULL, posts_text }, /* %A */
1.37 schwarze 219: { NULL, posts_text }, /* %B */ /* FIXME: can be used outside Rs/Re. */
1.89 ! schwarze 220: { NULL, posts_text }, /* %D */
1.1 kristaps 221: { NULL, posts_text }, /* %I */
222: { NULL, posts_text }, /* %J */
223: { NULL, posts_text }, /* %N */
224: { NULL, posts_text }, /* %O */
225: { NULL, posts_text }, /* %P */
226: { NULL, posts_text }, /* %R */
1.37 schwarze 227: { NULL, posts_text }, /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1 kristaps 228: { NULL, posts_text }, /* %V */
229: { NULL, NULL }, /* Ac */
230: { NULL, NULL }, /* Ao */
1.78 schwarze 231: { NULL, NULL }, /* Aq */
1.1 kristaps 232: { NULL, posts_at }, /* At */
233: { NULL, NULL }, /* Bc */
234: { NULL, posts_bf }, /* Bf */
235: { NULL, NULL }, /* Bo */
1.78 schwarze 236: { NULL, NULL }, /* Bq */
1.1 kristaps 237: { NULL, NULL }, /* Bsx */
1.87 schwarze 238: { NULL, posts_bx }, /* Bx */
1.1 kristaps 239: { NULL, posts_bool }, /* Db */
240: { NULL, NULL }, /* Dc */
241: { NULL, NULL }, /* Do */
1.78 schwarze 242: { NULL, NULL }, /* Dq */
1.1 kristaps 243: { NULL, NULL }, /* Ec */
244: { NULL, NULL }, /* Ef */
245: { NULL, NULL }, /* Em */
246: { NULL, NULL }, /* Eo */
247: { NULL, NULL }, /* Fx */
1.85 schwarze 248: { NULL, NULL }, /* Ms */
1.1 kristaps 249: { NULL, posts_notext }, /* No */
1.88 schwarze 250: { NULL, posts_ns }, /* Ns */
1.1 kristaps 251: { NULL, NULL }, /* Nx */
252: { NULL, NULL }, /* Ox */
253: { NULL, NULL }, /* Pc */
1.38 schwarze 254: { NULL, posts_text1 }, /* Pf */
1.1 kristaps 255: { NULL, NULL }, /* Po */
1.78 schwarze 256: { NULL, NULL }, /* Pq */
1.1 kristaps 257: { NULL, NULL }, /* Qc */
1.78 schwarze 258: { NULL, NULL }, /* Ql */
1.1 kristaps 259: { NULL, NULL }, /* Qo */
1.78 schwarze 260: { NULL, NULL }, /* Qq */
1.1 kristaps 261: { NULL, NULL }, /* Re */
1.82 schwarze 262: { NULL, posts_rs }, /* Rs */
1.1 kristaps 263: { NULL, NULL }, /* Sc */
264: { NULL, NULL }, /* So */
1.78 schwarze 265: { NULL, NULL }, /* Sq */
1.1 kristaps 266: { NULL, posts_bool }, /* Sm */
1.85 schwarze 267: { NULL, NULL }, /* Sx */
268: { NULL, NULL }, /* Sy */
269: { NULL, NULL }, /* Tn */
1.1 kristaps 270: { NULL, NULL }, /* Ux */
271: { NULL, NULL }, /* Xc */
272: { NULL, NULL }, /* Xo */
273: { NULL, posts_fo }, /* Fo */
274: { NULL, NULL }, /* Fc */
275: { NULL, NULL }, /* Oo */
276: { NULL, NULL }, /* Oc */
1.76 schwarze 277: { NULL, posts_bk }, /* Bk */
1.1 kristaps 278: { NULL, NULL }, /* Ek */
1.59 schwarze 279: { NULL, posts_eoln }, /* Bt */
1.1 kristaps 280: { NULL, NULL }, /* Hf */
281: { NULL, NULL }, /* Fr */
1.59 schwarze 282: { NULL, posts_eoln }, /* Ud */
283: { NULL, posts_lb }, /* Lb */
1.31 schwarze 284: { NULL, posts_notext }, /* Lp */
1.85 schwarze 285: { NULL, NULL }, /* Lk */
1.76 schwarze 286: { NULL, posts_defaults }, /* Mt */
1.78 schwarze 287: { NULL, NULL }, /* Brq */
1.1 kristaps 288: { NULL, NULL }, /* Bro */
289: { NULL, NULL }, /* Brc */
290: { NULL, posts_text }, /* %C */
291: { NULL, NULL }, /* Es */
292: { NULL, NULL }, /* En */
293: { NULL, NULL }, /* Dx */
294: { NULL, posts_text }, /* %Q */
1.31 schwarze 295: { NULL, posts_notext }, /* br */
1.70 schwarze 296: { pres_pp, posts_sp }, /* sp */
1.38 schwarze 297: { NULL, posts_text1 }, /* %U */
1.60 schwarze 298: { NULL, NULL }, /* Ta */
1.1 kristaps 299: };
300:
1.76 schwarze 301: #define RSORD_MAX 14 /* Number of `Rs' blocks. */
302:
303: static const enum mdoct rsord[RSORD_MAX] = {
304: MDOC__A,
305: MDOC__T,
306: MDOC__B,
307: MDOC__I,
308: MDOC__J,
309: MDOC__R,
310: MDOC__N,
311: MDOC__V,
312: MDOC__P,
313: MDOC__Q,
314: MDOC__D,
315: MDOC__O,
316: MDOC__C,
317: MDOC__U
318: };
319:
1.1 kristaps 320:
321: int
1.60 schwarze 322: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1 kristaps 323: {
324: v_pre *p;
325: int line, pos;
1.61 schwarze 326: char *tp;
1.1 kristaps 327:
1.84 schwarze 328: switch (n->type) {
329: case (MDOC_TEXT):
1.1 kristaps 330: tp = n->string;
331: line = n->line;
332: pos = n->pos;
1.76 schwarze 333: check_text(mdoc, line, pos, tp);
1.84 schwarze 334: /* FALLTHROUGH */
335: case (MDOC_TBL):
336: /* FALLTHROUGH */
337: case (MDOC_ROOT):
1.76 schwarze 338: return(1);
1.84 schwarze 339: default:
340: break;
1.1 kristaps 341: }
342:
1.76 schwarze 343: check_args(mdoc, n);
344:
1.1 kristaps 345: if (NULL == mdoc_valids[n->tok].pre)
346: return(1);
347: for (p = mdoc_valids[n->tok].pre; *p; p++)
348: if ( ! (*p)(mdoc, n))
349: return(0);
350: return(1);
351: }
352:
353:
354: int
355: mdoc_valid_post(struct mdoc *mdoc)
356: {
357: v_post *p;
358:
359: if (MDOC_VALID & mdoc->last->flags)
360: return(1);
361: mdoc->last->flags |= MDOC_VALID;
362:
1.84 schwarze 363: switch (mdoc->last->type) {
364: case (MDOC_TEXT):
365: /* FALLTHROUGH */
366: case (MDOC_TBL):
1.1 kristaps 367: return(1);
1.84 schwarze 368: case (MDOC_ROOT):
1.1 kristaps 369: return(post_root(mdoc));
1.84 schwarze 370: default:
371: break;
372: }
1.1 kristaps 373:
374: if (NULL == mdoc_valids[mdoc->last->tok].post)
375: return(1);
376: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
377: if ( ! (*p)(mdoc))
378: return(0);
379:
380: return(1);
381: }
382:
1.72 schwarze 383: static int
384: check_count(struct mdoc *m, enum mdoc_type type,
385: enum check_lvl lvl, enum check_ineq ineq, int val)
386: {
387: const char *p;
1.83 schwarze 388: enum mandocerr t;
1.72 schwarze 389:
390: if (m->last->type != type)
391: return(1);
392:
393: switch (ineq) {
394: case (CHECK_LT):
395: p = "less than ";
396: if (m->last->nchild < val)
397: return(1);
398: break;
399: case (CHECK_GT):
1.83 schwarze 400: p = "more than ";
1.72 schwarze 401: if (m->last->nchild > val)
402: return(1);
403: break;
404: case (CHECK_EQ):
405: p = "";
406: if (val == m->last->nchild)
407: return(1);
408: break;
1.76 schwarze 409: default:
410: abort();
411: /* NOTREACHED */
1.72 schwarze 412: }
413:
1.83 schwarze 414: t = lvl == CHECK_WARN ? MANDOCERR_ARGCWARN : MANDOCERR_ARGCOUNT;
1.72 schwarze 415:
1.83 schwarze 416: return(mdoc_vmsg(m, t, m->last->line, m->last->pos,
417: "want %s%d children (have %d)",
1.72 schwarze 418: p, val, m->last->nchild));
419: }
420:
421: static int
422: berr_ge1(POST_ARGS)
423: {
424:
1.83 schwarze 425: return(check_count(mdoc, MDOC_BODY, CHECK_ERROR, CHECK_GT, 0));
1.72 schwarze 426: }
427:
428: static int
429: bwarn_ge1(POST_ARGS)
430: {
431: return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
432: }
433:
434: static int
1.83 schwarze 435: ewarn_eq0(POST_ARGS)
1.72 schwarze 436: {
1.83 schwarze 437: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
1.72 schwarze 438: }
1.1 kristaps 439:
1.72 schwarze 440: static int
1.83 schwarze 441: ewarn_eq1(POST_ARGS)
1.1 kristaps 442: {
1.83 schwarze 443: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1));
1.72 schwarze 444: }
1.1 kristaps 445:
1.72 schwarze 446: static int
447: ewarn_ge1(POST_ARGS)
448: {
449: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1 kristaps 450: }
451:
1.72 schwarze 452: static int
1.83 schwarze 453: ewarn_le1(POST_ARGS)
1.72 schwarze 454: {
1.83 schwarze 455: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_LT, 2));
1.72 schwarze 456: }
1.1 kristaps 457:
1.72 schwarze 458: static int
1.83 schwarze 459: hwarn_eq0(POST_ARGS)
1.72 schwarze 460: {
1.83 schwarze 461: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.72 schwarze 462: }
1.1 kristaps 463:
1.72 schwarze 464: static int
1.83 schwarze 465: hwarn_eq1(POST_ARGS)
1.72 schwarze 466: {
1.83 schwarze 467: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
1.1 kristaps 468: }
469:
1.72 schwarze 470: static int
1.83 schwarze 471: hwarn_ge1(POST_ARGS)
1.72 schwarze 472: {
1.83 schwarze 473: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_GT, 0));
1.72 schwarze 474: }
1.1 kristaps 475:
1.72 schwarze 476: static int
477: hwarn_le1(POST_ARGS)
478: {
479: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
480: }
1.1 kristaps 481:
1.76 schwarze 482: static void
1.61 schwarze 483: check_args(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 484: {
485: int i;
486:
487: if (NULL == n->args)
1.76 schwarze 488: return;
1.1 kristaps 489:
490: assert(n->args->argc);
491: for (i = 0; i < (int)n->args->argc; i++)
1.76 schwarze 492: check_argv(m, n, &n->args->argv[i]);
1.1 kristaps 493: }
494:
1.76 schwarze 495: static void
1.61 schwarze 496: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1 kristaps 497: {
498: int i;
499:
500: for (i = 0; i < (int)v->sz; i++)
1.76 schwarze 501: check_text(m, v->line, v->pos, v->value[i]);
1.1 kristaps 502:
1.76 schwarze 503: /* FIXME: move to post_std(). */
1.1 kristaps 504:
1.76 schwarze 505: if (MDOC_Std == v->arg)
506: if ( ! (v->sz || m->meta.name))
507: mdoc_nmsg(m, n, MANDOCERR_NONAME);
1.1 kristaps 508: }
509:
1.76 schwarze 510: static void
1.66 schwarze 511: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1 kristaps 512: {
1.15 schwarze 513: int c;
1.66 schwarze 514: size_t sz;
1.1 kristaps 515:
1.66 schwarze 516: for ( ; *p; p++, pos++) {
517: sz = strcspn(p, "\t\\");
518: p += (int)sz;
519:
520: if ('\0' == *p)
521: break;
522:
523: pos += (int)sz;
1.65 schwarze 524:
1.1 kristaps 525: if ('\t' == *p) {
1.76 schwarze 526: if ( ! (MDOC_LITERAL & m->flags))
527: mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB);
528: continue;
1.66 schwarze 529: }
1.1 kristaps 530:
1.76 schwarze 531: if (0 == (c = mandoc_special(p))) {
532: mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
533: continue;
534: }
1.1 kristaps 535:
1.76 schwarze 536: p += c - 1;
537: pos += c - 1;
1.1 kristaps 538: }
539: }
540:
541: static int
1.44 schwarze 542: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1 kristaps 543: {
544:
545: assert(n->parent);
546: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
547: (t == n->parent->type))
548: return(1);
549:
1.55 schwarze 550: mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
551: n->line, n->pos, "want parent %s",
552: MDOC_ROOT == t ? "<root>" :
553: mdoc_macronames[tok]);
554: return(0);
1.1 kristaps 555: }
556:
557:
558: static int
559: pre_display(PRE_ARGS)
560: {
561: struct mdoc_node *node;
562:
563: if (MDOC_BLOCK != n->type)
564: return(1);
565:
566: for (node = mdoc->last->parent; node; node = node->parent)
567: if (MDOC_BLOCK == node->type)
568: if (MDOC_Bd == node->tok)
569: break;
1.76 schwarze 570:
1.74 schwarze 571: if (node)
572: mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
1.1 kristaps 573:
1.74 schwarze 574: return(1);
1.1 kristaps 575: }
576:
577:
578: static int
579: pre_bl(PRE_ARGS)
580: {
1.65 schwarze 581: int i, comp, dup;
582: const char *offs, *width;
583: enum mdoc_list lt;
584: struct mdoc_node *np;
1.1 kristaps 585:
1.60 schwarze 586: if (MDOC_BLOCK != n->type) {
1.65 schwarze 587: if (ENDBODY_NOT != n->end) {
588: assert(n->pending);
589: np = n->pending->parent;
590: } else
591: np = n->parent;
592:
593: assert(np);
594: assert(MDOC_BLOCK == np->type);
595: assert(MDOC_Bl == np->tok);
1.1 kristaps 596: return(1);
1.55 schwarze 597: }
1.1 kristaps 598:
1.60 schwarze 599: /*
600: * First figure out which kind of list to use: bind ourselves to
601: * the first mentioned list type and warn about any remaining
602: * ones. If we find no list type, we default to LIST_item.
603: */
1.1 kristaps 604:
605: /* LINTED */
1.60 schwarze 606: for (i = 0; n->args && i < (int)n->args->argc; i++) {
607: lt = LIST__NONE;
1.61 schwarze 608: dup = comp = 0;
609: width = offs = NULL;
1.60 schwarze 610: switch (n->args->argv[i].arg) {
611: /* Set list types. */
1.1 kristaps 612: case (MDOC_Bullet):
1.60 schwarze 613: lt = LIST_bullet;
614: break;
1.1 kristaps 615: case (MDOC_Dash):
1.60 schwarze 616: lt = LIST_dash;
617: break;
1.1 kristaps 618: case (MDOC_Enum):
1.60 schwarze 619: lt = LIST_enum;
620: break;
1.1 kristaps 621: case (MDOC_Hyphen):
1.60 schwarze 622: lt = LIST_hyphen;
623: break;
1.1 kristaps 624: case (MDOC_Item):
1.60 schwarze 625: lt = LIST_item;
626: break;
1.1 kristaps 627: case (MDOC_Tag):
1.60 schwarze 628: lt = LIST_tag;
629: break;
1.1 kristaps 630: case (MDOC_Diag):
1.60 schwarze 631: lt = LIST_diag;
632: break;
1.1 kristaps 633: case (MDOC_Hang):
1.60 schwarze 634: lt = LIST_hang;
635: break;
1.1 kristaps 636: case (MDOC_Ohang):
1.60 schwarze 637: lt = LIST_ohang;
638: break;
1.1 kristaps 639: case (MDOC_Inset):
1.60 schwarze 640: lt = LIST_inset;
641: break;
1.1 kristaps 642: case (MDOC_Column):
1.60 schwarze 643: lt = LIST_column;
644: break;
645: /* Set list arguments. */
1.37 schwarze 646: case (MDOC_Compact):
1.82 schwarze 647: dup = n->norm->Bl.comp;
1.61 schwarze 648: comp = 1;
1.60 schwarze 649: break;
1.1 kristaps 650: case (MDOC_Width):
1.82 schwarze 651: dup = (NULL != n->norm->Bl.width);
1.61 schwarze 652: width = n->args->argv[i].value[0];
1.5 schwarze 653: break;
1.1 kristaps 654: case (MDOC_Offset):
1.61 schwarze 655: /* NB: this can be empty! */
656: if (n->args->argv[i].sz) {
657: offs = n->args->argv[i].value[0];
1.82 schwarze 658: dup = (NULL != n->norm->Bl.offs);
1.61 schwarze 659: break;
660: }
1.76 schwarze 661: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.1 kristaps 662: break;
1.66 schwarze 663: default:
664: continue;
1.1 kristaps 665: }
666:
1.60 schwarze 667: /* Check: duplicate auxiliary arguments. */
668:
1.76 schwarze 669: if (dup)
670: mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.61 schwarze 671:
672: if (comp && ! dup)
1.82 schwarze 673: n->norm->Bl.comp = comp;
1.61 schwarze 674: if (offs && ! dup)
1.82 schwarze 675: n->norm->Bl.offs = offs;
1.61 schwarze 676: if (width && ! dup)
1.82 schwarze 677: n->norm->Bl.width = width;
1.60 schwarze 678:
679: /* Check: multiple list types. */
680:
1.82 schwarze 681: if (LIST__NONE != lt && n->norm->Bl.type != LIST__NONE)
1.76 schwarze 682: mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
1.60 schwarze 683:
684: /* Assign list type. */
685:
1.82 schwarze 686: if (LIST__NONE != lt && n->norm->Bl.type == LIST__NONE) {
687: n->norm->Bl.type = lt;
1.65 schwarze 688: /* Set column information, too. */
689: if (LIST_column == lt) {
1.82 schwarze 690: n->norm->Bl.ncols =
1.65 schwarze 691: n->args->argv[i].sz;
1.82 schwarze 692: n->norm->Bl.cols = (const char **)
1.65 schwarze 693: n->args->argv[i].value;
694: }
695: }
1.60 schwarze 696:
697: /* The list type should come first. */
698:
1.82 schwarze 699: if (n->norm->Bl.type == LIST__NONE)
700: if (n->norm->Bl.width ||
701: n->norm->Bl.offs ||
702: n->norm->Bl.comp)
1.76 schwarze 703: mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
1.60 schwarze 704:
705: continue;
706: }
707:
708: /* Allow lists to default to LIST_item. */
709:
1.82 schwarze 710: if (LIST__NONE == n->norm->Bl.type) {
1.76 schwarze 711: mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
1.82 schwarze 712: n->norm->Bl.type = LIST_item;
1.55 schwarze 713: }
1.1 kristaps 714:
1.5 schwarze 715: /*
716: * Validate the width field. Some list types don't need width
717: * types and should be warned about them. Others should have it
718: * and must also be warned.
719: */
720:
1.82 schwarze 721: switch (n->norm->Bl.type) {
1.60 schwarze 722: case (LIST_tag):
1.82 schwarze 723: if (n->norm->Bl.width)
1.60 schwarze 724: break;
1.76 schwarze 725: mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
726: break;
1.60 schwarze 727: case (LIST_column):
1.1 kristaps 728: /* FALLTHROUGH */
1.60 schwarze 729: case (LIST_diag):
1.1 kristaps 730: /* FALLTHROUGH */
1.60 schwarze 731: case (LIST_ohang):
1.40 schwarze 732: /* FALLTHROUGH */
1.60 schwarze 733: case (LIST_inset):
1.1 kristaps 734: /* FALLTHROUGH */
1.60 schwarze 735: case (LIST_item):
1.82 schwarze 736: if (n->norm->Bl.width)
1.75 schwarze 737: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
738: break;
1.5 schwarze 739: default:
740: break;
741: }
742:
1.1 kristaps 743: return(1);
744: }
745:
746:
747: static int
748: pre_bd(PRE_ARGS)
749: {
1.65 schwarze 750: int i, dup, comp;
751: enum mdoc_disp dt;
752: const char *offs;
753: struct mdoc_node *np;
1.1 kristaps 754:
1.61 schwarze 755: if (MDOC_BLOCK != n->type) {
1.65 schwarze 756: if (ENDBODY_NOT != n->end) {
757: assert(n->pending);
758: np = n->pending->parent;
759: } else
760: np = n->parent;
761:
762: assert(np);
763: assert(MDOC_BLOCK == np->type);
764: assert(MDOC_Bd == np->tok);
1.1 kristaps 765: return(1);
1.55 schwarze 766: }
1.1 kristaps 767:
768: /* LINTED */
1.61 schwarze 769: for (i = 0; n->args && i < (int)n->args->argc; i++) {
770: dt = DISP__NONE;
771: dup = comp = 0;
772: offs = NULL;
773:
1.1 kristaps 774: switch (n->args->argv[i].arg) {
1.38 schwarze 775: case (MDOC_Centred):
1.61 schwarze 776: dt = DISP_centred;
777: break;
1.1 kristaps 778: case (MDOC_Ragged):
1.61 schwarze 779: dt = DISP_ragged;
780: break;
1.1 kristaps 781: case (MDOC_Unfilled):
1.61 schwarze 782: dt = DISP_unfilled;
783: break;
1.1 kristaps 784: case (MDOC_Filled):
1.61 schwarze 785: dt = DISP_filled;
786: break;
1.1 kristaps 787: case (MDOC_Literal):
1.61 schwarze 788: dt = DISP_literal;
789: break;
790: case (MDOC_File):
791: mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
792: return(0);
793: case (MDOC_Offset):
794: /* NB: this can be empty! */
795: if (n->args->argv[i].sz) {
796: offs = n->args->argv[i].value[0];
1.82 schwarze 797: dup = (NULL != n->norm->Bd.offs);
1.1 kristaps 798: break;
1.61 schwarze 799: }
1.76 schwarze 800: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.55 schwarze 801: break;
1.61 schwarze 802: case (MDOC_Compact):
803: comp = 1;
1.82 schwarze 804: dup = n->norm->Bd.comp;
1.61 schwarze 805: break;
1.1 kristaps 806: default:
1.61 schwarze 807: abort();
808: /* NOTREACHED */
1.1 kristaps 809: }
810:
1.61 schwarze 811: /* Check whether we have duplicates. */
812:
1.76 schwarze 813: if (dup)
814: mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.61 schwarze 815:
816: /* Make our auxiliary assignments. */
817:
818: if (offs && ! dup)
1.82 schwarze 819: n->norm->Bd.offs = offs;
1.61 schwarze 820: if (comp && ! dup)
1.82 schwarze 821: n->norm->Bd.comp = comp;
1.61 schwarze 822:
823: /* Check whether a type has already been assigned. */
824:
1.82 schwarze 825: if (DISP__NONE != dt && n->norm->Bd.type != DISP__NONE)
1.76 schwarze 826: mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
1.61 schwarze 827:
828: /* Make our type assignment. */
829:
1.82 schwarze 830: if (DISP__NONE != dt && n->norm->Bd.type == DISP__NONE)
831: n->norm->Bd.type = dt;
1.61 schwarze 832: }
833:
1.82 schwarze 834: if (DISP__NONE == n->norm->Bd.type) {
1.76 schwarze 835: mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
1.82 schwarze 836: n->norm->Bd.type = DISP_ragged;
1.61 schwarze 837: }
838:
839: return(1);
1.1 kristaps 840: }
841:
842:
843: static int
844: pre_ss(PRE_ARGS)
845: {
846:
847: if (MDOC_BLOCK != n->type)
848: return(1);
849: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
850: }
851:
852:
853: static int
854: pre_sh(PRE_ARGS)
855: {
856:
857: if (MDOC_BLOCK != n->type)
858: return(1);
1.63 schwarze 859:
860: mdoc->regs->regs[(int)REG_nS].set = 0;
1.50 schwarze 861: return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1 kristaps 862: }
863:
864:
865: static int
866: pre_it(PRE_ARGS)
867: {
868:
869: if (MDOC_BLOCK != n->type)
870: return(1);
1.76 schwarze 871:
1.1 kristaps 872: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
873: }
874:
875:
876: static int
877: pre_an(PRE_ARGS)
878: {
1.73 schwarze 879: int i;
1.1 kristaps 880:
1.65 schwarze 881: if (NULL == n->args)
1.1 kristaps 882: return(1);
1.73 schwarze 883:
884: for (i = 1; i < (int)n->args->argc; i++)
1.76 schwarze 885: mdoc_pmsg(mdoc, n->args->argv[i].line,
886: n->args->argv[i].pos, MANDOCERR_IGNARGV);
1.72 schwarze 887:
1.65 schwarze 888: if (MDOC_Split == n->args->argv[0].arg)
1.82 schwarze 889: n->norm->An.auth = AUTH_split;
1.65 schwarze 890: else if (MDOC_Nosplit == n->args->argv[0].arg)
1.82 schwarze 891: n->norm->An.auth = AUTH_nosplit;
1.65 schwarze 892: else
893: abort();
894:
895: return(1);
1.1 kristaps 896: }
897:
898: static int
1.76 schwarze 899: pre_std(PRE_ARGS)
1.60 schwarze 900: {
901:
1.76 schwarze 902: if (n->args && 1 == n->args->argc)
903: if (MDOC_Std == n->args->argv[0].arg)
904: return(1);
1.60 schwarze 905:
1.76 schwarze 906: mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
1.60 schwarze 907: return(1);
908: }
909:
910: static int
1.1 kristaps 911: pre_dt(PRE_ARGS)
912: {
1.40 schwarze 913:
1.89 ! schwarze 914: if (NULL == mdoc->meta.date || mdoc->meta.os)
1.76 schwarze 915: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
916:
1.1 kristaps 917: if (mdoc->meta.title)
1.76 schwarze 918: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
919:
1.1 kristaps 920: return(1);
921: }
922:
923: static int
924: pre_os(PRE_ARGS)
925: {
926:
1.89 ! schwarze 927: if (NULL == mdoc->meta.title || NULL == mdoc->meta.date)
1.76 schwarze 928: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
929:
1.1 kristaps 930: if (mdoc->meta.os)
1.76 schwarze 931: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
932:
1.1 kristaps 933: return(1);
934: }
935:
936: static int
937: pre_dd(PRE_ARGS)
938: {
939:
940: if (mdoc->meta.title || mdoc->meta.os)
1.76 schwarze 941: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
942:
1.1 kristaps 943: if (mdoc->meta.date)
1.76 schwarze 944: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
945:
1.1 kristaps 946: return(1);
947: }
948:
949:
950: static int
951: post_bf(POST_ARGS)
952: {
1.65 schwarze 953: struct mdoc_node *np;
1.66 schwarze 954: enum mdocargt arg;
1.65 schwarze 955:
956: /*
957: * Unlike other data pointers, these are "housed" by the HEAD
958: * element, which contains the goods.
959: */
1.1 kristaps 960:
1.65 schwarze 961: if (MDOC_HEAD != mdoc->last->type) {
962: if (ENDBODY_NOT != mdoc->last->end) {
963: assert(mdoc->last->pending);
964: np = mdoc->last->pending->parent->head;
965: } else if (MDOC_BLOCK != mdoc->last->type) {
966: np = mdoc->last->parent->head;
967: } else
968: np = mdoc->last->head;
969:
970: assert(np);
971: assert(MDOC_HEAD == np->type);
972: assert(MDOC_Bf == np->tok);
1.1 kristaps 973: return(1);
1.65 schwarze 974: }
975:
976: np = mdoc->last;
977: assert(MDOC_BLOCK == np->parent->type);
978: assert(MDOC_Bf == np->parent->tok);
1.1 kristaps 979:
1.65 schwarze 980: /*
981: * Cannot have both argument and parameter.
982: * If neither is specified, let it through with a warning.
983: */
1.1 kristaps 984:
1.65 schwarze 985: if (np->parent->args && np->child) {
986: mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.55 schwarze 987: return(0);
1.76 schwarze 988: } else if (NULL == np->parent->args && NULL == np->child) {
989: mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
990: return(1);
991: }
1.65 schwarze 992:
993: /* Extract argument into data. */
994:
995: if (np->parent->args) {
996: arg = np->parent->args->argv[0].arg;
997: if (MDOC_Emphasis == arg)
1.82 schwarze 998: np->norm->Bf.font = FONT_Em;
1.65 schwarze 999: else if (MDOC_Literal == arg)
1.82 schwarze 1000: np->norm->Bf.font = FONT_Li;
1.65 schwarze 1001: else if (MDOC_Symbolic == arg)
1.82 schwarze 1002: np->norm->Bf.font = FONT_Sy;
1.65 schwarze 1003: else
1004: abort();
1.10 schwarze 1005: return(1);
1.55 schwarze 1006: }
1.1 kristaps 1007:
1.65 schwarze 1008: /* Extract parameter into data. */
1.1 kristaps 1009:
1.65 schwarze 1010: if (0 == strcmp(np->child->string, "Em"))
1.82 schwarze 1011: np->norm->Bf.font = FONT_Em;
1.65 schwarze 1012: else if (0 == strcmp(np->child->string, "Li"))
1.82 schwarze 1013: np->norm->Bf.font = FONT_Li;
1.65 schwarze 1014: else if (0 == strcmp(np->child->string, "Sy"))
1.82 schwarze 1015: np->norm->Bf.font = FONT_Sy;
1.76 schwarze 1016: else
1017: mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1.1 kristaps 1018:
1.65 schwarze 1019: return(1);
1.27 schwarze 1020: }
1021:
1022: static int
1023: post_lb(POST_ARGS)
1024: {
1.76 schwarze 1025: const char *p;
1026: char *buf;
1027: size_t sz;
1028:
1.83 schwarze 1029: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1030:
1.76 schwarze 1031: assert(mdoc->last->child);
1032: assert(MDOC_TEXT == mdoc->last->child->type);
1033:
1034: p = mdoc_a2lib(mdoc->last->child->string);
1.27 schwarze 1035:
1.76 schwarze 1036: /* If lookup ok, replace with table value. */
1037:
1038: if (p) {
1039: free(mdoc->last->child->string);
1040: mdoc->last->child->string = mandoc_strdup(p);
1.27 schwarze 1041: return(1);
1.76 schwarze 1042: }
1043:
1044: /* If not, use "library ``xxxx''. */
1045:
1046: sz = strlen(mdoc->last->child->string) +
1047: 2 + strlen("\\(lqlibrary\\(rq");
1048: buf = mandoc_malloc(sz);
1049: snprintf(buf, sz, "library \\(lq%s\\(rq",
1050: mdoc->last->child->string);
1051: free(mdoc->last->child->string);
1052: mdoc->last->child->string = buf;
1053: return(1);
1.59 schwarze 1054: }
1055:
1056: static int
1057: post_eoln(POST_ARGS)
1058: {
1059:
1.76 schwarze 1060: if (mdoc->last->child)
1061: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1062: return(1);
1.42 schwarze 1063: }
1064:
1065:
1066: static int
1067: post_vt(POST_ARGS)
1068: {
1069: const struct mdoc_node *n;
1070:
1071: /*
1072: * The Vt macro comes in both ELEM and BLOCK form, both of which
1073: * have different syntaxes (yet more context-sensitive
1.85 schwarze 1074: * behaviour). ELEM types must have a child, which is already
1075: * guaranteed by the in_line parsing routine; BLOCK types,
1.42 schwarze 1076: * specifically the BODY, should only have TEXT children.
1077: */
1078:
1079: if (MDOC_BODY != mdoc->last->type)
1080: return(1);
1081:
1082: for (n = mdoc->last->child; n; n = n->next)
1.52 schwarze 1083: if (MDOC_TEXT != n->type)
1.76 schwarze 1084: mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.42 schwarze 1085:
1086: return(1);
1.1 kristaps 1087: }
1088:
1089:
1090: static int
1091: post_nm(POST_ARGS)
1092: {
1.76 schwarze 1093: char buf[BUFSIZ];
1.1 kristaps 1094:
1.76 schwarze 1095: /* If no child specified, make sure we have the meta name. */
1096:
1097: if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
1098: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1.1 kristaps 1099: return(1);
1.76 schwarze 1100: } else if (mdoc->meta.name)
1.1 kristaps 1101: return(1);
1.76 schwarze 1102:
1103: /* If no meta name, set it from the child. */
1104:
1105: if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1106: return(0);
1107:
1108: mdoc->meta.name = mandoc_strdup(buf);
1109:
1110: return(1);
1111: }
1112:
1113: static int
1114: post_literal(POST_ARGS)
1115: {
1116:
1117: /*
1118: * The `Dl' (note "el" not "one") and `Bd' macros unset the
1119: * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1120: * this in literal mode, but it doesn't hurt to just switch it
1121: * off in general since displays can't be nested.
1122: */
1123:
1124: if (MDOC_BODY == mdoc->last->type)
1125: mdoc->flags &= ~MDOC_LITERAL;
1126:
1127: return(1);
1.1 kristaps 1128: }
1129:
1.76 schwarze 1130: static int
1131: post_defaults(POST_ARGS)
1132: {
1133: struct mdoc_node *nn;
1134:
1135: /*
1136: * The `Ar' defaults to "file ..." if no value is provided as an
1137: * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1138: * gets an empty string.
1139: */
1140:
1141: if (mdoc->last->child)
1142: return(1);
1143:
1144: nn = mdoc->last;
1145: mdoc->next = MDOC_NEXT_CHILD;
1146:
1147: switch (nn->tok) {
1148: case (MDOC_Ar):
1149: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
1150: return(0);
1151: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
1152: return(0);
1153: break;
1154: case (MDOC_At):
1155: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
1156: return(0);
1157: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
1158: return(0);
1159: break;
1160: case (MDOC_Li):
1161: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
1162: return(0);
1163: break;
1164: case (MDOC_Pa):
1165: /* FALLTHROUGH */
1166: case (MDOC_Mt):
1167: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
1168: return(0);
1169: break;
1170: default:
1171: abort();
1172: /* NOTREACHED */
1173: }
1174:
1175: mdoc->last = nn;
1176: return(1);
1177: }
1.1 kristaps 1178:
1179: static int
1180: post_at(POST_ARGS)
1181: {
1.76 schwarze 1182: const char *p, *q;
1183: char *buf;
1184: size_t sz;
1.1 kristaps 1185:
1.76 schwarze 1186: /*
1187: * If we have a child, look it up in the standard keys. If a
1188: * key exist, use that instead of the child; if it doesn't,
1189: * prefix "AT&T UNIX " to the existing data.
1190: */
1191:
1.1 kristaps 1192: if (NULL == mdoc->last->child)
1193: return(1);
1.76 schwarze 1194:
1.55 schwarze 1195: assert(MDOC_TEXT == mdoc->last->child->type);
1.76 schwarze 1196: p = mdoc_a2att(mdoc->last->child->string);
1197:
1198: if (p) {
1199: free(mdoc->last->child->string);
1200: mdoc->last->child->string = mandoc_strdup(p);
1201: } else {
1202: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
1203: p = "AT&T UNIX ";
1204: q = mdoc->last->child->string;
1205: sz = strlen(p) + strlen(q) + 1;
1206: buf = mandoc_malloc(sz);
1207: strlcpy(buf, p, sz);
1208: strlcat(buf, q, sz);
1209: free(mdoc->last->child->string);
1210: mdoc->last->child->string = buf;
1211: }
1212:
1213: return(1);
1.1 kristaps 1214: }
1215:
1216: static int
1217: post_an(POST_ARGS)
1218: {
1.65 schwarze 1219: struct mdoc_node *np;
1.1 kristaps 1220:
1.65 schwarze 1221: np = mdoc->last;
1.85 schwarze 1222: if (AUTH__NONE == np->norm->An.auth) {
1223: if (0 == np->child)
1224: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0);
1225: } else if (np->child)
1.83 schwarze 1226: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0);
1.76 schwarze 1227:
1228: return(1);
1.1 kristaps 1229: }
1230:
1231:
1232: static int
1233: post_it(POST_ARGS)
1234: {
1.60 schwarze 1235: int i, cols, rc;
1236: enum mdoc_list lt;
1.1 kristaps 1237: struct mdoc_node *n, *c;
1.60 schwarze 1238: enum mandocerr er;
1.1 kristaps 1239:
1240: if (MDOC_BLOCK != mdoc->last->type)
1241: return(1);
1242:
1243: n = mdoc->last->parent->parent;
1.82 schwarze 1244: lt = n->norm->Bl.type;
1.60 schwarze 1245:
1246: if (LIST__NONE == lt) {
1.55 schwarze 1247: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1.76 schwarze 1248: return(1);
1.55 schwarze 1249: }
1.1 kristaps 1250:
1.60 schwarze 1251: switch (lt) {
1252: case (LIST_tag):
1253: if (mdoc->last->head->child)
1.1 kristaps 1254: break;
1.60 schwarze 1255: /* FIXME: give this a dummy value. */
1.76 schwarze 1256: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1 kristaps 1257: break;
1.60 schwarze 1258: case (LIST_hang):
1.1 kristaps 1259: /* FALLTHROUGH */
1.60 schwarze 1260: case (LIST_ohang):
1.1 kristaps 1261: /* FALLTHROUGH */
1.60 schwarze 1262: case (LIST_inset):
1.1 kristaps 1263: /* FALLTHROUGH */
1.60 schwarze 1264: case (LIST_diag):
1.1 kristaps 1265: if (NULL == mdoc->last->head->child)
1.76 schwarze 1266: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1 kristaps 1267: break;
1.60 schwarze 1268: case (LIST_bullet):
1.1 kristaps 1269: /* FALLTHROUGH */
1.60 schwarze 1270: case (LIST_dash):
1.1 kristaps 1271: /* FALLTHROUGH */
1.60 schwarze 1272: case (LIST_enum):
1.1 kristaps 1273: /* FALLTHROUGH */
1.60 schwarze 1274: case (LIST_hyphen):
1.64 schwarze 1275: if (NULL == mdoc->last->body->child)
1.76 schwarze 1276: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.1 kristaps 1277: /* FALLTHROUGH */
1.60 schwarze 1278: case (LIST_item):
1.1 kristaps 1279: if (mdoc->last->head->child)
1.76 schwarze 1280: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1.1 kristaps 1281: break;
1.60 schwarze 1282: case (LIST_column):
1.82 schwarze 1283: cols = (int)n->norm->Bl.ncols;
1.60 schwarze 1284:
1285: assert(NULL == mdoc->last->head->child);
1286:
1287: if (NULL == mdoc->last->body->child)
1.76 schwarze 1288: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.60 schwarze 1289:
1290: for (i = 0, c = mdoc->last->child; c; c = c->next)
1291: if (MDOC_BODY == c->type)
1292: i++;
1293:
1294: if (i < cols)
1295: er = MANDOCERR_ARGCOUNT;
1296: else if (i == cols || i == cols + 1)
1.1 kristaps 1297: break;
1.60 schwarze 1298: else
1299: er = MANDOCERR_SYNTARGCOUNT;
1.32 schwarze 1300:
1.60 schwarze 1301: rc = mdoc_vmsg(mdoc, er,
1.55 schwarze 1302: mdoc->last->line, mdoc->last->pos,
1303: "columns == %d (have %d)", cols, i);
1.60 schwarze 1304: return(rc);
1.1 kristaps 1305: default:
1306: break;
1307: }
1308:
1309: return(1);
1310: }
1311:
1312: static int
1.76 schwarze 1313: post_bl_block(POST_ARGS)
1.11 schwarze 1314: {
1.60 schwarze 1315: struct mdoc_node *n;
1.11 schwarze 1316:
1.76 schwarze 1317: /*
1318: * These are fairly complicated, so we've broken them into two
1319: * functions. post_bl_block_tag() is called when a -tag is
1320: * specified, but no -width (it must be guessed). The second
1321: * when a -width is specified (macro indicators must be
1322: * rewritten into real lengths).
1323: */
1324:
1325: n = mdoc->last;
1.11 schwarze 1326:
1.82 schwarze 1327: if (LIST_tag == n->norm->Bl.type &&
1328: NULL == n->norm->Bl.width) {
1.76 schwarze 1329: if ( ! post_bl_block_tag(mdoc))
1.60 schwarze 1330: return(0);
1.82 schwarze 1331: } else if (NULL != n->norm->Bl.width) {
1.76 schwarze 1332: if ( ! post_bl_block_width(mdoc))
1333: return(0);
1334: } else
1.60 schwarze 1335: return(1);
1.11 schwarze 1336:
1.82 schwarze 1337: assert(n->norm->Bl.width);
1.76 schwarze 1338: return(1);
1.11 schwarze 1339: }
1340:
1341: static int
1.76 schwarze 1342: post_bl_block_width(POST_ARGS)
1.1 kristaps 1343: {
1.76 schwarze 1344: size_t width;
1345: int i;
1346: enum mdoct tok;
1347: struct mdoc_node *n;
1348: char buf[NUMSIZ];
1.1 kristaps 1349:
1.76 schwarze 1350: n = mdoc->last;
1.1 kristaps 1351:
1.41 schwarze 1352: /*
1.76 schwarze 1353: * Calculate the real width of a list from the -width string,
1354: * which may contain a macro (with a known default width), a
1355: * literal string, or a scaling width.
1356: *
1357: * If the value to -width is a macro, then we re-write it to be
1358: * the macro's width as set in share/tmac/mdoc/doc-common.
1.41 schwarze 1359: */
1360:
1.82 schwarze 1361: if (0 == strcmp(n->norm->Bl.width, "Ds"))
1.76 schwarze 1362: width = 6;
1.82 schwarze 1363: else if (MDOC_MAX == (tok = mdoc_hash_find(n->norm->Bl.width)))
1.76 schwarze 1364: return(1);
1365: else if (0 == (width = mdoc_macro2len(tok))) {
1366: mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
1367: return(1);
1.1 kristaps 1368: }
1369:
1.76 schwarze 1370: /* The value already exists: free and reallocate it. */
1371:
1372: assert(n->args);
1373:
1374: for (i = 0; i < (int)n->args->argc; i++)
1375: if (MDOC_Width == n->args->argv[i].arg)
1376: break;
1377:
1378: assert(i < (int)n->args->argc);
1379:
1380: snprintf(buf, NUMSIZ, "%zun", width);
1381: free(n->args->argv[i].value[0]);
1382: n->args->argv[i].value[0] = mandoc_strdup(buf);
1383:
1384: /* Set our width! */
1.82 schwarze 1385: n->norm->Bl.width = n->args->argv[i].value[0];
1.1 kristaps 1386: return(1);
1387: }
1388:
1389: static int
1.76 schwarze 1390: post_bl_block_tag(POST_ARGS)
1.1 kristaps 1391: {
1.76 schwarze 1392: struct mdoc_node *n, *nn;
1393: size_t sz, ssz;
1394: int i;
1395: char buf[NUMSIZ];
1396:
1397: /*
1398: * Calculate the -width for a `Bl -tag' list if it hasn't been
1399: * provided. Uses the first head macro. NOTE AGAIN: this is
1400: * ONLY if the -width argument has NOT been provided. See
1401: * post_bl_block_width() for converting the -width string.
1402: */
1403:
1404: sz = 10;
1405: n = mdoc->last;
1406:
1407: for (nn = n->body->child; nn; nn = nn->next) {
1408: if (MDOC_It != nn->tok)
1409: continue;
1410:
1411: assert(MDOC_BLOCK == nn->type);
1412: nn = nn->head->child;
1413:
1414: if (nn == NULL)
1415: break;
1.1 kristaps 1416:
1.76 schwarze 1417: if (MDOC_TEXT == nn->type) {
1418: sz = strlen(nn->string) + 1;
1.1 kristaps 1419: break;
1.76 schwarze 1420: }
1421:
1422: if (0 != (ssz = mdoc_macro2len(nn->tok)))
1423: sz = ssz;
1424:
1.1 kristaps 1425: break;
1.76 schwarze 1426: }
1427:
1428: /* Defaults to ten ens. */
1429:
1430: snprintf(buf, NUMSIZ, "%zun", sz);
1.1 kristaps 1431:
1.76 schwarze 1432: /*
1433: * We have to dynamically add this to the macro's argument list.
1434: * We're guaranteed that a MDOC_Width doesn't already exist.
1435: */
1.1 kristaps 1436:
1.76 schwarze 1437: assert(n->args);
1438: i = (int)(n->args->argc)++;
1.1 kristaps 1439:
1.76 schwarze 1440: n->args->argv = mandoc_realloc(n->args->argv,
1441: n->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 1442:
1.76 schwarze 1443: n->args->argv[i].arg = MDOC_Width;
1444: n->args->argv[i].line = n->line;
1445: n->args->argv[i].pos = n->pos;
1446: n->args->argv[i].sz = 1;
1447: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
1448: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 1449:
1.76 schwarze 1450: /* Set our width! */
1.82 schwarze 1451: n->norm->Bl.width = n->args->argv[i].value[0];
1.76 schwarze 1452: return(1);
1.1 kristaps 1453: }
1454:
1455:
1456: static int
1.76 schwarze 1457: post_bl_head(POST_ARGS)
1.1 kristaps 1458: {
1.76 schwarze 1459: struct mdoc_node *np, *nn, *nnp;
1460: int i, j;
1.1 kristaps 1461:
1.82 schwarze 1462: if (LIST_column != mdoc->last->norm->Bl.type)
1.76 schwarze 1463: /* FIXME: this should be ERROR class... */
1464: return(hwarn_eq0(mdoc));
1.36 schwarze 1465:
1.76 schwarze 1466: /*
1467: * Convert old-style lists, where the column width specifiers
1468: * trail as macro parameters, to the new-style ("normal-form")
1469: * lists where they're argument values following -column.
1470: */
1.36 schwarze 1471:
1.76 schwarze 1472: /* First, disallow both types and allow normal-form. */
1.36 schwarze 1473:
1.76 schwarze 1474: /*
1475: * TODO: technically, we can accept both and just merge the two
1476: * lists, but I'll leave that for another day.
1477: */
1478:
1.82 schwarze 1479: if (mdoc->last->norm->Bl.ncols && mdoc->last->nchild) {
1.76 schwarze 1480: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
1481: return(0);
1482: } else if (NULL == mdoc->last->child)
1483: return(1);
1484:
1485: np = mdoc->last->parent;
1486: assert(np->args);
1487:
1488: for (j = 0; j < (int)np->args->argc; j++)
1489: if (MDOC_Column == np->args->argv[j].arg)
1490: break;
1491:
1492: assert(j < (int)np->args->argc);
1493: assert(0 == np->args->argv[j].sz);
1494:
1495: /*
1496: * Accomodate for new-style groff column syntax. Shuffle the
1497: * child nodes, all of which must be TEXT, as arguments for the
1498: * column field. Then, delete the head children.
1499: */
1500:
1501: np->args->argv[j].sz = (size_t)mdoc->last->nchild;
1502: np->args->argv[j].value = mandoc_malloc
1503: ((size_t)mdoc->last->nchild * sizeof(char *));
1504:
1.82 schwarze 1505: mdoc->last->norm->Bl.ncols = np->args->argv[j].sz;
1506: mdoc->last->norm->Bl.cols = (const char **)np->args->argv[j].value;
1.76 schwarze 1507:
1508: for (i = 0, nn = mdoc->last->child; nn; i++) {
1509: np->args->argv[j].value[i] = nn->string;
1510: nn->string = NULL;
1511: nnp = nn;
1512: nn = nn->next;
1513: mdoc_node_delete(NULL, nnp);
1514: }
1515:
1516: mdoc->last->nchild = 0;
1517: mdoc->last->child = NULL;
1518:
1519: return(1);
1520: }
1521:
1522: static int
1523: post_bl(POST_ARGS)
1524: {
1525: struct mdoc_node *n;
1526:
1527: if (MDOC_HEAD == mdoc->last->type)
1528: return(post_bl_head(mdoc));
1529: if (MDOC_BLOCK == mdoc->last->type)
1530: return(post_bl_block(mdoc));
1.36 schwarze 1531: if (MDOC_BODY != mdoc->last->type)
1532: return(1);
1533:
1.76 schwarze 1534: for (n = mdoc->last->child; n; n = n->next) {
1.79 schwarze 1535: switch (n->tok) {
1536: case (MDOC_Lp):
1537: /* FALLTHROUGH */
1538: case (MDOC_Pp):
1539: mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1540: /* FALLTHROUGH */
1541: case (MDOC_It):
1542: /* FALLTHROUGH */
1543: case (MDOC_Sm):
1.76 schwarze 1544: continue;
1.79 schwarze 1545: default:
1546: break;
1547: }
1548:
1.76 schwarze 1549: mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
1550: return(0);
1551: }
1552:
1553: return(1);
1554: }
1555:
1556: static int
1557: ebool(struct mdoc *mdoc)
1558: {
1559:
1.83 schwarze 1560: if (NULL == mdoc->last->child) {
1561: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1562: mdoc_node_delete(mdoc, mdoc->last);
1.76 schwarze 1563: return(1);
1.83 schwarze 1564: }
1565: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1.76 schwarze 1566:
1567: assert(MDOC_TEXT == mdoc->last->child->type);
1568:
1569: if (0 == strcmp(mdoc->last->child->string, "on"))
1570: return(1);
1571: if (0 == strcmp(mdoc->last->child->string, "off"))
1572: return(1);
1573:
1574: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
1575: return(1);
1576: }
1577:
1578: static int
1579: post_root(POST_ARGS)
1580: {
1581: int erc;
1582: struct mdoc_node *n;
1583:
1584: erc = 0;
1585:
1586: /* Check that we have a finished prologue. */
1587:
1588: if ( ! (MDOC_PBODY & mdoc->flags)) {
1589: erc++;
1590: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1591: }
1592:
1593: n = mdoc->first;
1594: assert(n);
1595:
1596: /* Check that we begin with a proper `Sh'. */
1597:
1598: if (NULL == n->child) {
1599: erc++;
1600: mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1601: } else if (MDOC_BLOCK != n->child->type ||
1602: MDOC_Sh != n->child->tok) {
1603: erc++;
1604: /* Can this be lifted? See rxdebug.1 for example. */
1605: mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1606: }
1607:
1608: return(erc ? 0 : 1);
1609: }
1610:
1611: static int
1612: post_st(POST_ARGS)
1613: {
1.83 schwarze 1614: struct mdoc_node *ch;
1615: const char *p;
1.76 schwarze 1616:
1.83 schwarze 1617: if (NULL == (ch = mdoc->last->child)) {
1618: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1619: mdoc_node_delete(mdoc, mdoc->last);
1620: return(1);
1621: }
1.76 schwarze 1622:
1.83 schwarze 1623: assert(MDOC_TEXT == ch->type);
1.76 schwarze 1624:
1.83 schwarze 1625: if (NULL == (p = mdoc_a2st(ch->string))) {
1.76 schwarze 1626: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
1627: mdoc_node_delete(mdoc, mdoc->last);
1628: } else {
1.83 schwarze 1629: free(ch->string);
1630: ch->string = mandoc_strdup(p);
1.76 schwarze 1631: }
1632:
1633: return(1);
1634: }
1635:
1636: static int
1637: post_rs(POST_ARGS)
1638: {
1639: struct mdoc_node *nn, *next, *prev;
1640: int i, j;
1641:
1.83 schwarze 1642: switch (mdoc->last->type) {
1643: case (MDOC_HEAD):
1644: check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0);
1645: return(1);
1646: case (MDOC_BODY):
1647: if (mdoc->last->child)
1648: break;
1649: check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0);
1650: return(1);
1651: default:
1.76 schwarze 1652: return(1);
1.83 schwarze 1653: }
1.76 schwarze 1654:
1655: /*
1656: * Make sure only certain types of nodes are allowed within the
1657: * the `Rs' body. Delete offending nodes and raise a warning.
1658: * Do this before re-ordering for the sake of clarity.
1659: */
1660:
1661: next = NULL;
1662: for (nn = mdoc->last->child; nn; nn = next) {
1663: for (i = 0; i < RSORD_MAX; i++)
1664: if (nn->tok == rsord[i])
1665: break;
1666:
1667: if (i < RSORD_MAX) {
1.86 schwarze 1668: if (MDOC__J == rsord[i] || MDOC__B == rsord[i])
1669: mdoc->last->norm->Rs.quote_T++;
1.76 schwarze 1670: next = nn->next;
1671: continue;
1672: }
1673:
1674: next = nn->next;
1675: mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
1676: mdoc_node_delete(mdoc, nn);
1677: }
1678:
1679: /*
1680: * The full `Rs' block needs special handling to order the
1681: * sub-elements according to `rsord'. Pick through each element
1682: * and correctly order it. This is a insertion sort.
1683: */
1684:
1685: next = NULL;
1686: for (nn = mdoc->last->child->next; nn; nn = next) {
1687: /* Determine order of `nn'. */
1688: for (i = 0; i < RSORD_MAX; i++)
1689: if (rsord[i] == nn->tok)
1690: break;
1691:
1692: /*
1693: * Remove `nn' from the chain. This somewhat
1694: * repeats mdoc_node_unlink(), but since we're
1695: * just re-ordering, there's no need for the
1696: * full unlink process.
1697: */
1698:
1699: if (NULL != (next = nn->next))
1700: next->prev = nn->prev;
1701:
1702: if (NULL != (prev = nn->prev))
1703: prev->next = nn->next;
1704:
1705: nn->prev = nn->next = NULL;
1706:
1707: /*
1708: * Scan back until we reach a node that's
1709: * ordered before `nn'.
1710: */
1711:
1712: for ( ; prev ; prev = prev->prev) {
1713: /* Determine order of `prev'. */
1714: for (j = 0; j < RSORD_MAX; j++)
1715: if (rsord[j] == prev->tok)
1716: break;
1717:
1718: if (j <= i)
1719: break;
1720: }
1721:
1722: /*
1723: * Set `nn' back into its correct place in front
1724: * of the `prev' node.
1725: */
1726:
1727: nn->prev = prev;
1728:
1729: if (prev) {
1730: if (prev->next)
1731: prev->next->prev = nn;
1732: nn->next = prev->next;
1733: prev->next = nn;
1734: } else {
1735: mdoc->last->child->prev = nn;
1736: nn->next = mdoc->last->child;
1737: mdoc->last->child = nn;
1.36 schwarze 1738: }
1.76 schwarze 1739: }
1.36 schwarze 1740:
1.88 schwarze 1741: return(1);
1742: }
1743:
1744: static int
1745: post_ns(POST_ARGS)
1746: {
1747:
1748: if (MDOC_LINE & mdoc->last->flags)
1749: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNNS);
1.36 schwarze 1750: return(1);
1.1 kristaps 1751: }
1752:
1753: static int
1754: post_sh(POST_ARGS)
1755: {
1756:
1757: if (MDOC_HEAD == mdoc->last->type)
1758: return(post_sh_head(mdoc));
1759: if (MDOC_BODY == mdoc->last->type)
1760: return(post_sh_body(mdoc));
1761:
1762: return(1);
1763: }
1764:
1765: static int
1766: post_sh_body(POST_ARGS)
1767: {
1768: struct mdoc_node *n;
1769:
1.26 schwarze 1770: if (SEC_NAME != mdoc->lastsec)
1.1 kristaps 1771: return(1);
1772:
1773: /*
1774: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1775: * macros (can have multiple `Nm' and one `Nd'). Note that the
1776: * children of the BODY declaration can also be "text".
1777: */
1778:
1.76 schwarze 1779: if (NULL == (n = mdoc->last->child)) {
1780: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1781: return(1);
1782: }
1.1 kristaps 1783:
1784: for ( ; n && n->next; n = n->next) {
1785: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1786: continue;
1787: if (MDOC_TEXT == n->type)
1788: continue;
1.76 schwarze 1789: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1.1 kristaps 1790: }
1791:
1.34 schwarze 1792: assert(n);
1.25 schwarze 1793: if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1 kristaps 1794: return(1);
1.76 schwarze 1795:
1796: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1797: return(1);
1.1 kristaps 1798: }
1799:
1800: static int
1801: post_sh_head(POST_ARGS)
1802: {
1.76 schwarze 1803: char buf[BUFSIZ];
1804: enum mdoc_sec sec;
1.1 kristaps 1805:
1806: /*
1807: * Process a new section. Sections are either "named" or
1.76 schwarze 1808: * "custom". Custom sections are user-defined, while named ones
1809: * follow a conventional order and may only appear in certain
1810: * manual sections.
1.1 kristaps 1811: */
1812:
1.76 schwarze 1813: if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1814: return(0);
1815:
1816: sec = mdoc_str2sec(buf);
1817:
1818: /* The NAME should be first. */
1.55 schwarze 1819:
1.76 schwarze 1820: if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1821: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
1.1 kristaps 1822:
1.76 schwarze 1823: /* The SYNOPSIS gets special attention in other areas. */
1.9 schwarze 1824:
1.76 schwarze 1825: if (SEC_SYNOPSIS == sec)
1826: mdoc->flags |= MDOC_SYNOPSIS;
1827: else
1828: mdoc->flags &= ~MDOC_SYNOPSIS;
1.1 kristaps 1829:
1.76 schwarze 1830: /* Mark our last section. */
1.1 kristaps 1831:
1.76 schwarze 1832: mdoc->lastsec = sec;
1.1 kristaps 1833:
1.76 schwarze 1834: /* We don't care about custom sections after this. */
1.51 schwarze 1835:
1.1 kristaps 1836: if (SEC_CUSTOM == sec)
1837: return(1);
1.51 schwarze 1838:
1.76 schwarze 1839: /*
1840: * Check whether our non-custom section is being repeated or is
1841: * out of order.
1842: */
1843:
1.1 kristaps 1844: if (sec == mdoc->lastnamed)
1.76 schwarze 1845: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.51 schwarze 1846:
1.1 kristaps 1847: if (sec < mdoc->lastnamed)
1.76 schwarze 1848: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
1849:
1850: /* Mark the last named section. */
1851:
1852: mdoc->lastnamed = sec;
1853:
1854: /* Check particular section/manual conventions. */
1.1 kristaps 1855:
1.76 schwarze 1856: assert(mdoc->meta.msec);
1.1 kristaps 1857:
1858: switch (sec) {
1.76 schwarze 1859: case (SEC_RETURN_VALUES):
1860: /* FALLTHROUGH */
1861: case (SEC_ERRORS):
1862: /* FALLTHROUGH */
1.1 kristaps 1863: case (SEC_LIBRARY):
1.54 schwarze 1864: if (*mdoc->meta.msec == '2')
1865: break;
1866: if (*mdoc->meta.msec == '3')
1867: break;
1868: if (*mdoc->meta.msec == '9')
1.1 kristaps 1869: break;
1.76 schwarze 1870: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
1871: break;
1.1 kristaps 1872: default:
1873: break;
1874: }
1875:
1.70 schwarze 1876: return(1);
1877: }
1878:
1.76 schwarze 1879: static int
1.79 schwarze 1880: post_ignpar(POST_ARGS)
1881: {
1882: struct mdoc_node *np;
1883:
1884: if (MDOC_BODY != mdoc->last->type)
1885: return(1);
1886:
1887: if (NULL != (np = mdoc->last->child))
1888: if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1889: mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1890: mdoc_node_delete(mdoc, np);
1891: }
1892:
1893: if (NULL != (np = mdoc->last->last))
1894: if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1895: mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1896: mdoc_node_delete(mdoc, np);
1897: }
1.76 schwarze 1898:
1899: return(1);
1900: }
1.70 schwarze 1901:
1902: static int
1.76 schwarze 1903: pre_par(PRE_ARGS)
1.70 schwarze 1904: {
1905:
1906: if (NULL == mdoc->last)
1907: return(1);
1.79 schwarze 1908: if (MDOC_ELEM != n->type && MDOC_BLOCK != n->type)
1909: return(1);
1.70 schwarze 1910:
1.76 schwarze 1911: /*
1912: * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1913: * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1914: */
1.70 schwarze 1915:
1916: if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1917: return(1);
1.82 schwarze 1918: if (MDOC_Bl == n->tok && n->norm->Bl.comp)
1.70 schwarze 1919: return(1);
1.82 schwarze 1920: if (MDOC_Bd == n->tok && n->norm->Bd.comp)
1.79 schwarze 1921: return(1);
1.82 schwarze 1922: if (MDOC_It == n->tok && n->parent->norm->Bl.comp)
1.70 schwarze 1923: return(1);
1924:
1925: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
1926: mdoc_node_delete(mdoc, mdoc->last);
1.1 kristaps 1927: return(1);
1928: }
1.76 schwarze 1929:
1930: static int
1931: pre_literal(PRE_ARGS)
1932: {
1933:
1934: if (MDOC_BODY != n->type)
1935: return(1);
1936:
1937: /*
1938: * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1939: * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1940: */
1941:
1942: switch (n->tok) {
1943: case (MDOC_Dl):
1944: mdoc->flags |= MDOC_LITERAL;
1945: break;
1946: case (MDOC_Bd):
1.82 schwarze 1947: if (DISP_literal == n->norm->Bd.type)
1.76 schwarze 1948: mdoc->flags |= MDOC_LITERAL;
1.82 schwarze 1949: if (DISP_unfilled == n->norm->Bd.type)
1.76 schwarze 1950: mdoc->flags |= MDOC_LITERAL;
1951: break;
1952: default:
1953: abort();
1954: /* NOTREACHED */
1955: }
1956:
1957: return(1);
1958: }
1959:
1960: static int
1961: post_dd(POST_ARGS)
1962: {
1963: char buf[DATESIZE];
1964: struct mdoc_node *n;
1965:
1.89 ! schwarze 1966: if (mdoc->meta.date)
! 1967: free(mdoc->meta.date);
! 1968:
1.76 schwarze 1969: n = mdoc->last;
1.89 ! schwarze 1970: if (NULL == n->child || '\0' == n->child->string[0]) {
! 1971: mdoc->meta.date = mandoc_normdate(NULL,
! 1972: mdoc->msg, mdoc->data, n->line, n->pos);
1.76 schwarze 1973: return(1);
1974: }
1975:
1976: if ( ! concat(mdoc, buf, n->child, DATESIZE))
1977: return(0);
1978:
1.89 ! schwarze 1979: mdoc->meta.date = mandoc_normdate(buf,
! 1980: mdoc->msg, mdoc->data, n->line, n->pos);
1.76 schwarze 1981:
1982: return(1);
1983: }
1984:
1985: static int
1986: post_dt(POST_ARGS)
1987: {
1988: struct mdoc_node *nn, *n;
1989: const char *cp;
1990: char *p;
1991:
1992: n = mdoc->last;
1993:
1994: if (mdoc->meta.title)
1995: free(mdoc->meta.title);
1996: if (mdoc->meta.vol)
1997: free(mdoc->meta.vol);
1998: if (mdoc->meta.arch)
1999: free(mdoc->meta.arch);
2000:
2001: mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
2002:
2003: /* First make all characters uppercase. */
2004:
2005: if (NULL != (nn = n->child))
2006: for (p = nn->string; *p; p++) {
2007: if (toupper((u_char)*p) == *p)
2008: continue;
2009:
2010: /*
2011: * FIXME: don't be lazy: have this make all
2012: * characters be uppercase and just warn once.
2013: */
2014: mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
2015: break;
2016: }
2017:
2018: /* Handles: `.Dt'
2019: * --> title = unknown, volume = local, msec = 0, arch = NULL
2020: */
2021:
2022: if (NULL == (nn = n->child)) {
2023: /* XXX: make these macro values. */
2024: /* FIXME: warn about missing values. */
2025: mdoc->meta.title = mandoc_strdup("UNKNOWN");
2026: mdoc->meta.vol = mandoc_strdup("LOCAL");
2027: mdoc->meta.msec = mandoc_strdup("1");
2028: return(1);
2029: }
2030:
2031: /* Handles: `.Dt TITLE'
2032: * --> title = TITLE, volume = local, msec = 0, arch = NULL
2033: */
2034:
2035: mdoc->meta.title = mandoc_strdup
2036: ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
2037:
2038: if (NULL == (nn = nn->next)) {
2039: /* FIXME: warn about missing msec. */
2040: /* XXX: make this a macro value. */
2041: mdoc->meta.vol = mandoc_strdup("LOCAL");
2042: mdoc->meta.msec = mandoc_strdup("1");
2043: return(1);
2044: }
2045:
2046: /* Handles: `.Dt TITLE SEC'
2047: * --> title = TITLE, volume = SEC is msec ?
2048: * format(msec) : SEC,
2049: * msec = SEC is msec ? atoi(msec) : 0,
2050: * arch = NULL
2051: */
2052:
2053: cp = mdoc_a2msec(nn->string);
2054: if (cp) {
2055: mdoc->meta.vol = mandoc_strdup(cp);
2056: mdoc->meta.msec = mandoc_strdup(nn->string);
2057: } else {
2058: mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
2059: mdoc->meta.vol = mandoc_strdup(nn->string);
2060: mdoc->meta.msec = mandoc_strdup(nn->string);
2061: }
2062:
2063: if (NULL == (nn = nn->next))
2064: return(1);
2065:
2066: /* Handles: `.Dt TITLE SEC VOL'
2067: * --> title = TITLE, volume = VOL is vol ?
2068: * format(VOL) :
2069: * VOL is arch ? format(arch) :
2070: * VOL
2071: */
2072:
2073: cp = mdoc_a2vol(nn->string);
2074: if (cp) {
2075: free(mdoc->meta.vol);
2076: mdoc->meta.vol = mandoc_strdup(cp);
2077: } else {
2078: /* FIXME: warn about bad arch. */
2079: cp = mdoc_a2arch(nn->string);
2080: if (NULL == cp) {
2081: free(mdoc->meta.vol);
2082: mdoc->meta.vol = mandoc_strdup(nn->string);
2083: } else
2084: mdoc->meta.arch = mandoc_strdup(cp);
2085: }
2086:
2087: /* Ignore any subsequent parameters... */
2088: /* FIXME: warn about subsequent parameters. */
2089:
2090: return(1);
2091: }
2092:
2093: static int
2094: post_prol(POST_ARGS)
2095: {
2096: /*
2097: * Remove prologue macros from the document after they're
2098: * processed. The final document uses mdoc_meta for these
2099: * values and discards the originals.
2100: */
2101:
2102: mdoc_node_delete(mdoc, mdoc->last);
2103: if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
2104: mdoc->flags |= MDOC_PBODY;
1.87 schwarze 2105:
2106: return(1);
2107: }
2108:
2109: static int
2110: post_bx(POST_ARGS)
2111: {
2112: struct mdoc_node *n;
2113:
2114: /*
2115: * Make `Bx's second argument always start with an uppercase
2116: * letter. Groff checks if it's an "accepted" term, but we just
2117: * uppercase blindly.
2118: */
2119:
2120: n = mdoc->last->child;
2121: if (n && NULL != (n = n->next))
2122: *n->string = toupper((unsigned char)*n->string);
1.76 schwarze 2123:
2124: return(1);
2125: }
2126:
2127: static int
2128: post_os(POST_ARGS)
2129: {
2130: struct mdoc_node *n;
2131: char buf[BUFSIZ];
2132: #ifndef OSNAME
2133: struct utsname utsname;
2134: #endif
2135:
2136: n = mdoc->last;
2137:
2138: /*
2139: * Set the operating system by way of the `Os' macro. Note that
2140: * if an argument isn't provided and -DOSNAME="\"foo\"" is
2141: * provided during compilation, this value will be used instead
2142: * of filling in "sysname release" from uname().
2143: */
2144:
1.77 schwarze 2145: if (mdoc->meta.os)
2146: free(mdoc->meta.os);
1.76 schwarze 2147:
2148: if ( ! concat(mdoc, buf, n->child, BUFSIZ))
2149: return(0);
2150:
2151: /* XXX: yes, these can all be dynamically-adjusted buffers, but
2152: * it's really not worth the extra hackery.
2153: */
2154:
2155: if ('\0' == buf[0]) {
2156: #ifdef OSNAME
2157: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
2158: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2159: return(0);
2160: }
2161: #else /*!OSNAME */
2162: if (uname(&utsname)) {
2163: mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
2164: mdoc->meta.os = mandoc_strdup("UNKNOWN");
2165: return(post_prol(mdoc));
2166: }
2167:
2168: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
2169: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2170: return(0);
2171: }
2172: if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
2173: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2174: return(0);
2175: }
2176: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
2177: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2178: return(0);
2179: }
2180: #endif /*!OSNAME*/
2181: }
2182:
2183: mdoc->meta.os = mandoc_strdup(buf);
2184: return(1);
2185: }
2186:
2187: static int
2188: post_std(POST_ARGS)
2189: {
2190: struct mdoc_node *nn, *n;
2191:
2192: n = mdoc->last;
2193:
2194: /*
2195: * Macros accepting `-std' as an argument have the name of the
2196: * current document (`Nm') filled in as the argument if it's not
2197: * provided.
2198: */
2199:
2200: if (n->child)
2201: return(1);
2202:
2203: if (NULL == mdoc->meta.name)
2204: return(1);
2205:
2206: nn = n;
2207: mdoc->next = MDOC_NEXT_CHILD;
2208:
2209: if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
2210: return(0);
2211:
2212: mdoc->last = nn;
2213: return(1);
2214: }
2215:
2216: static int
2217: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
2218: {
2219:
2220: p[0] = '\0';
2221:
2222: /*
2223: * Concatenate sibling nodes together. All siblings must be of
2224: * type MDOC_TEXT or an assertion is raised. Concatenation is
2225: * separated by a single whitespace. Returns 0 on fatal (string
2226: * overrun) error.
2227: */
2228:
2229: for ( ; n; n = n->next) {
2230: assert(MDOC_TEXT == n->type);
2231:
2232: if (strlcat(p, n->string, sz) >= sz) {
2233: mdoc_nmsg(m, n, MANDOCERR_MEM);
2234: return(0);
2235: }
2236:
2237: if (NULL == n->next)
2238: continue;
2239:
2240: if (strlcat(p, " ", sz) >= sz) {
2241: mdoc_nmsg(m, n, MANDOCERR_MEM);
2242: return(0);
2243: }
2244: }
2245:
2246: return(1);
2247: }
2248: