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