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