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