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

Annotation of mandoc/mdoc_validate.c, Revision 1.152

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

CVSweb