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