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