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