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

Annotation of mandoc/mdoc_validate.c, Revision 1.158

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

CVSweb