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