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