[BACK]Return to mdoc_validate.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc_validate.c, Revision 1.129

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

CVSweb