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