[BACK]Return to mdoc_validate.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_validate.c, Revision 1.119

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