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

Annotation of mandoc/mdoc_validate.c, Revision 1.140

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

CVSweb