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