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