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