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

Annotation of mandoc/mdoc_validate.c, Revision 1.134

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

CVSweb