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