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