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

Annotation of mandoc/mdoc_validate.c, Revision 1.137

1.137   ! kristaps    1: /*     $Id: mdoc_validate.c,v 1.136 2010/12/05 15:49: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
1.135     kristaps   45: #define        DATESIZE  32
1.131     kristaps   46:
1.120     kristaps   47: enum   check_ineq {
                     48:        CHECK_LT,
                     49:        CHECK_GT,
                     50:        CHECK_EQ
                     51: };
                     52:
                     53: enum   check_lvl {
                     54:        CHECK_WARN,
                     55:        CHECK_ERROR,
                     56:        CHECK_FATAL
                     57: };
                     58:
1.1       kristaps   59: typedef        int     (*v_pre)(PRE_ARGS);
                     60: typedef        int     (*v_post)(POST_ARGS);
                     61:
                     62: struct valids {
                     63:        v_pre   *pre;
                     64:        v_post  *post;
                     65: };
                     66:
1.120     kristaps   67: static int      check_count(struct mdoc *, enum mdoc_type,
                     68:                        enum check_lvl, enum check_ineq, int);
1.59      kristaps   69: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.133     kristaps   70: static void     check_text(struct mdoc *, int, int, char *);
                     71: static void     check_argv(struct mdoc *,
1.92      kristaps   72:                        struct mdoc_node *, struct mdoc_argv *);
1.133     kristaps   73: static void     check_args(struct mdoc *, struct mdoc_node *);
1.32      kristaps   74:
1.132     kristaps   75: static int      concat(struct mdoc *, char *,
                     76:                        const struct mdoc_node *, size_t);
                     77:
1.120     kristaps   78: static int      ebool(POST_ARGS);
1.32      kristaps   79: static int      berr_ge1(POST_ARGS);
                     80: static int      bwarn_ge1(POST_ARGS);
1.120     kristaps   81: static int      eerr_eq0(POST_ARGS);
1.32      kristaps   82: static int      eerr_eq1(POST_ARGS);
                     83: static int      eerr_ge1(POST_ARGS);
1.46      kristaps   84: static int      eerr_le1(POST_ARGS);
1.116     kristaps   85: static int      ewarn_eq0(POST_ARGS);
1.32      kristaps   86: static int      ewarn_ge1(POST_ARGS);
                     87: static int      herr_eq0(POST_ARGS);
                     88: static int      herr_ge1(POST_ARGS);
1.120     kristaps   89: static int      hwarn_eq0(POST_ARGS);
1.32      kristaps   90: static int      hwarn_eq1(POST_ARGS);
                     91: static int      hwarn_le1(POST_ARGS);
                     92:
                     93: static int      post_an(POST_ARGS);
                     94: static int      post_at(POST_ARGS);
                     95: static int      post_bf(POST_ARGS);
                     96: static int      post_bl(POST_ARGS);
1.131     kristaps   97: static int      post_bl_block(POST_ARGS);
                     98: static int      post_bl_block_width(POST_ARGS);
                     99: static int      post_bl_block_tag(POST_ARGS);
1.32      kristaps  100: static int      post_bl_head(POST_ARGS);
1.132     kristaps  101: static int      post_dd(POST_ARGS);
                    102: static int      post_dt(POST_ARGS);
1.123     kristaps  103: static int      post_defaults(POST_ARGS);
1.128     kristaps  104: static int      post_literal(POST_ARGS);
1.123     kristaps  105: static int      post_eoln(POST_ARGS);
1.32      kristaps  106: static int      post_it(POST_ARGS);
                    107: static int      post_lb(POST_ARGS);
                    108: static int      post_nm(POST_ARGS);
1.132     kristaps  109: static int      post_os(POST_ARGS);
                    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:        if (MDOC_BLOCK != n->type)
                    579:                return(1);
                    580:
                    581:        for (node = mdoc->last->parent; node; node = node->parent)
                    582:                if (MDOC_BLOCK == node->type)
                    583:                        if (MDOC_Bd == node->tok)
                    584:                                break;
1.128     kristaps  585:
1.135     kristaps  586:        if (node)
                    587:                mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
1.1       kristaps  588:
1.135     kristaps  589:        return(1);
1.1       kristaps  590: }
                    591:
                    592:
                    593: static int
                    594: pre_bl(PRE_ARGS)
                    595: {
1.104     kristaps  596:        int               i, comp, dup;
                    597:        const char       *offs, *width;
                    598:        enum mdoc_list    lt;
                    599:        struct mdoc_node *np;
1.1       kristaps  600:
1.91      kristaps  601:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  602:                if (ENDBODY_NOT != n->end) {
                    603:                        assert(n->pending);
                    604:                        np = n->pending->parent;
                    605:                } else
                    606:                        np = n->parent;
                    607:
                    608:                assert(np);
                    609:                assert(MDOC_BLOCK == np->type);
                    610:                assert(MDOC_Bl == np->tok);
                    611:                assert(np->data.Bl);
                    612:                n->data.Bl = np->data.Bl;
1.1       kristaps  613:                return(1);
1.79      kristaps  614:        }
1.1       kristaps  615:
1.91      kristaps  616:        /*
                    617:         * First figure out which kind of list to use: bind ourselves to
                    618:         * the first mentioned list type and warn about any remaining
                    619:         * ones.  If we find no list type, we default to LIST_item.
                    620:         */
1.1       kristaps  621:
1.104     kristaps  622:        assert(NULL == n->data.Bl);
                    623:        n->data.Bl = mandoc_calloc(1, sizeof(struct mdoc_bl));
1.1       kristaps  624:
                    625:        /* LINTED */
1.91      kristaps  626:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    627:                lt = LIST__NONE;
1.97      kristaps  628:                dup = comp = 0;
1.99      kristaps  629:                width = offs = NULL;
1.91      kristaps  630:                switch (n->args->argv[i].arg) {
                    631:                /* Set list types. */
1.1       kristaps  632:                case (MDOC_Bullet):
1.91      kristaps  633:                        lt = LIST_bullet;
                    634:                        break;
1.1       kristaps  635:                case (MDOC_Dash):
1.91      kristaps  636:                        lt = LIST_dash;
                    637:                        break;
1.1       kristaps  638:                case (MDOC_Enum):
1.91      kristaps  639:                        lt = LIST_enum;
                    640:                        break;
1.1       kristaps  641:                case (MDOC_Hyphen):
1.91      kristaps  642:                        lt = LIST_hyphen;
                    643:                        break;
1.1       kristaps  644:                case (MDOC_Item):
1.91      kristaps  645:                        lt = LIST_item;
                    646:                        break;
1.1       kristaps  647:                case (MDOC_Tag):
1.91      kristaps  648:                        lt = LIST_tag;
                    649:                        break;
1.1       kristaps  650:                case (MDOC_Diag):
1.91      kristaps  651:                        lt = LIST_diag;
                    652:                        break;
1.1       kristaps  653:                case (MDOC_Hang):
1.91      kristaps  654:                        lt = LIST_hang;
                    655:                        break;
1.1       kristaps  656:                case (MDOC_Ohang):
1.91      kristaps  657:                        lt = LIST_ohang;
                    658:                        break;
1.1       kristaps  659:                case (MDOC_Inset):
1.91      kristaps  660:                        lt = LIST_inset;
                    661:                        break;
1.1       kristaps  662:                case (MDOC_Column):
1.91      kristaps  663:                        lt = LIST_column;
                    664:                        break;
                    665:                /* Set list arguments. */
1.45      kristaps  666:                case (MDOC_Compact):
1.104     kristaps  667:                        dup = n->data.Bl->comp;
1.97      kristaps  668:                        comp = 1;
1.91      kristaps  669:                        break;
1.1       kristaps  670:                case (MDOC_Width):
1.104     kristaps  671:                        dup = (NULL != n->data.Bl->width);
1.99      kristaps  672:                        width = n->args->argv[i].value[0];
1.8       kristaps  673:                        break;
1.1       kristaps  674:                case (MDOC_Offset):
1.98      kristaps  675:                        /* NB: this can be empty! */
                    676:                        if (n->args->argv[i].sz) {
                    677:                                offs = n->args->argv[i].value[0];
1.104     kristaps  678:                                dup = (NULL != n->data.Bl->offs);
1.98      kristaps  679:                                break;
                    680:                        }
1.133     kristaps  681:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.1       kristaps  682:                        break;
1.113     kristaps  683:                default:
                    684:                        continue;
1.1       kristaps  685:                }
                    686:
1.91      kristaps  687:                /* Check: duplicate auxiliary arguments. */
                    688:
1.133     kristaps  689:                if (dup)
                    690:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.97      kristaps  691:
                    692:                if (comp && ! dup)
1.104     kristaps  693:                        n->data.Bl->comp = comp;
1.98      kristaps  694:                if (offs && ! dup)
1.104     kristaps  695:                        n->data.Bl->offs = offs;
1.99      kristaps  696:                if (width && ! dup)
1.104     kristaps  697:                        n->data.Bl->width = width;
1.91      kristaps  698:
                    699:                /* Check: multiple list types. */
                    700:
1.104     kristaps  701:                if (LIST__NONE != lt && n->data.Bl->type != LIST__NONE)
1.133     kristaps  702:                        mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
1.91      kristaps  703:
                    704:                /* Assign list type. */
                    705:
1.109     kristaps  706:                if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
1.104     kristaps  707:                        n->data.Bl->type = lt;
1.109     kristaps  708:                        /* Set column information, too. */
                    709:                        if (LIST_column == lt) {
                    710:                                n->data.Bl->ncols =
                    711:                                        n->args->argv[i].sz;
                    712:                                n->data.Bl->cols = (const char **)
                    713:                                        n->args->argv[i].value;
                    714:                        }
                    715:                }
1.91      kristaps  716:
                    717:                /* The list type should come first. */
                    718:
1.104     kristaps  719:                if (n->data.Bl->type == LIST__NONE)
                    720:                        if (n->data.Bl->width ||
                    721:                                        n->data.Bl->offs ||
                    722:                                        n->data.Bl->comp)
1.133     kristaps  723:                                mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
1.91      kristaps  724:
                    725:                continue;
                    726:        }
                    727:
                    728:        /* Allow lists to default to LIST_item. */
                    729:
1.104     kristaps  730:        if (LIST__NONE == n->data.Bl->type) {
1.133     kristaps  731:                mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
1.104     kristaps  732:                n->data.Bl->type = LIST_item;
1.79      kristaps  733:        }
1.1       kristaps  734:
1.8       kristaps  735:        /*
                    736:         * Validate the width field.  Some list types don't need width
                    737:         * types and should be warned about them.  Others should have it
                    738:         * and must also be warned.
                    739:         */
                    740:
1.104     kristaps  741:        switch (n->data.Bl->type) {
1.91      kristaps  742:        case (LIST_tag):
1.104     kristaps  743:                if (n->data.Bl->width)
1.91      kristaps  744:                        break;
1.133     kristaps  745:                mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
                    746:                break;
1.91      kristaps  747:        case (LIST_column):
1.1       kristaps  748:                /* FALLTHROUGH */
1.91      kristaps  749:        case (LIST_diag):
1.1       kristaps  750:                /* FALLTHROUGH */
1.91      kristaps  751:        case (LIST_ohang):
1.53      kristaps  752:                /* FALLTHROUGH */
1.91      kristaps  753:        case (LIST_inset):
1.1       kristaps  754:                /* FALLTHROUGH */
1.91      kristaps  755:        case (LIST_item):
1.137   ! kristaps  756:                if (n->data.Bl->width)
        !           757:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.133     kristaps  758:                break;
1.8       kristaps  759:        default:
                    760:                break;
                    761:        }
                    762:
1.1       kristaps  763:        return(1);
                    764: }
                    765:
                    766:
                    767: static int
                    768: pre_bd(PRE_ARGS)
                    769: {
1.104     kristaps  770:        int               i, dup, comp;
                    771:        enum mdoc_disp    dt;
                    772:        const char       *offs;
                    773:        struct mdoc_node *np;
1.1       kristaps  774:
1.93      kristaps  775:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  776:                if (ENDBODY_NOT != n->end) {
                    777:                        assert(n->pending);
                    778:                        np = n->pending->parent;
                    779:                } else
                    780:                        np = n->parent;
                    781:
                    782:                assert(np);
                    783:                assert(MDOC_BLOCK == np->type);
                    784:                assert(MDOC_Bd == np->tok);
                    785:                assert(np->data.Bd);
                    786:                n->data.Bd = np->data.Bd;
1.1       kristaps  787:                return(1);
1.79      kristaps  788:        }
1.1       kristaps  789:
1.104     kristaps  790:        assert(NULL == n->data.Bd);
                    791:        n->data.Bd = mandoc_calloc(1, sizeof(struct mdoc_bd));
1.1       kristaps  792:
                    793:        /* LINTED */
1.93      kristaps  794:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    795:                dt = DISP__NONE;
1.94      kristaps  796:                dup = comp = 0;
                    797:                offs = NULL;
                    798:
1.1       kristaps  799:                switch (n->args->argv[i].arg) {
1.49      kristaps  800:                case (MDOC_Centred):
1.93      kristaps  801:                        dt = DISP_centred;
                    802:                        break;
1.1       kristaps  803:                case (MDOC_Ragged):
1.93      kristaps  804:                        dt = DISP_ragged;
                    805:                        break;
1.1       kristaps  806:                case (MDOC_Unfilled):
1.93      kristaps  807:                        dt = DISP_unfilled;
                    808:                        break;
1.1       kristaps  809:                case (MDOC_Filled):
1.93      kristaps  810:                        dt = DISP_filled;
                    811:                        break;
1.1       kristaps  812:                case (MDOC_Literal):
1.93      kristaps  813:                        dt = DISP_literal;
1.79      kristaps  814:                        break;
1.93      kristaps  815:                case (MDOC_File):
                    816:                        mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
                    817:                        return(0);
                    818:                case (MDOC_Offset):
1.94      kristaps  819:                        /* NB: this can be empty! */
                    820:                        if (n->args->argv[i].sz) {
                    821:                                offs = n->args->argv[i].value[0];
1.104     kristaps  822:                                dup = (NULL != n->data.Bd->offs);
1.94      kristaps  823:                                break;
                    824:                        }
1.133     kristaps  825:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.94      kristaps  826:                        break;
1.93      kristaps  827:                case (MDOC_Compact):
1.94      kristaps  828:                        comp = 1;
1.104     kristaps  829:                        dup = n->data.Bd->comp;
1.94      kristaps  830:                        break;
1.1       kristaps  831:                default:
1.94      kristaps  832:                        abort();
                    833:                        /* NOTREACHED */
1.1       kristaps  834:                }
                    835:
1.94      kristaps  836:                /* Check whether we have duplicates. */
                    837:
1.133     kristaps  838:                if (dup)
                    839:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.94      kristaps  840:
                    841:                /* Make our auxiliary assignments. */
                    842:
                    843:                if (offs && ! dup)
1.104     kristaps  844:                        n->data.Bd->offs = offs;
1.94      kristaps  845:                if (comp && ! dup)
1.104     kristaps  846:                        n->data.Bd->comp = comp;
1.94      kristaps  847:
                    848:                /* Check whether a type has already been assigned. */
                    849:
1.104     kristaps  850:                if (DISP__NONE != dt && n->data.Bd->type != DISP__NONE)
1.133     kristaps  851:                        mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
1.93      kristaps  852:
1.94      kristaps  853:                /* Make our type assignment. */
                    854:
1.104     kristaps  855:                if (DISP__NONE != dt && n->data.Bd->type == DISP__NONE)
                    856:                        n->data.Bd->type = dt;
1.93      kristaps  857:        }
                    858:
1.104     kristaps  859:        if (DISP__NONE == n->data.Bd->type) {
1.133     kristaps  860:                mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
1.104     kristaps  861:                n->data.Bd->type = DISP_ragged;
1.93      kristaps  862:        }
                    863:
                    864:        return(1);
1.1       kristaps  865: }
                    866:
                    867:
                    868: static int
                    869: pre_ss(PRE_ARGS)
                    870: {
                    871:
                    872:        if (MDOC_BLOCK != n->type)
                    873:                return(1);
                    874:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    875: }
                    876:
                    877:
                    878: static int
                    879: pre_sh(PRE_ARGS)
                    880: {
                    881:
                    882:        if (MDOC_BLOCK != n->type)
                    883:                return(1);
1.100     kristaps  884:
                    885:        mdoc->regs->regs[(int)REG_nS].set = 0;
1.70      kristaps  886:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  887: }
                    888:
                    889:
                    890: static int
                    891: pre_it(PRE_ARGS)
                    892: {
                    893:
                    894:        if (MDOC_BLOCK != n->type)
                    895:                return(1);
1.133     kristaps  896:
1.79      kristaps  897:        /*
                    898:         * FIXME: this can probably be lifted if we make the It into
                    899:         * something else on-the-fly?
                    900:         */
1.1       kristaps  901:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    902: }
                    903:
                    904:
                    905: static int
                    906: pre_an(PRE_ARGS)
                    907: {
1.121     kristaps  908:        int              i;
1.1       kristaps  909:
1.107     kristaps  910:        if (NULL == n->args)
1.1       kristaps  911:                return(1);
1.121     kristaps  912:
                    913:        for (i = 1; i < (int)n->args->argc; i++)
1.133     kristaps  914:                mdoc_pmsg(mdoc, n->args->argv[i].line,
                    915:                        n->args->argv[i].pos, MANDOCERR_IGNARGV);
1.120     kristaps  916:
1.107     kristaps  917:        if (MDOC_Split == n->args->argv[0].arg)
                    918:                n->data.An.auth = AUTH_split;
                    919:        else if (MDOC_Nosplit == n->args->argv[0].arg)
                    920:                n->data.An.auth = AUTH_nosplit;
                    921:        else
                    922:                abort();
                    923:
                    924:        return(1);
1.1       kristaps  925: }
                    926:
                    927: static int
1.133     kristaps  928: pre_std(PRE_ARGS)
1.1       kristaps  929: {
                    930:
1.133     kristaps  931:        if (n->args && 1 == n->args->argc)
                    932:                if (MDOC_Std == n->args->argv[0].arg)
                    933:                        return(1);
                    934:
                    935:        mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
                    936:        return(1);
1.1       kristaps  937: }
                    938:
1.85      kristaps  939: static int
1.1       kristaps  940: pre_dt(PRE_ARGS)
                    941: {
1.54      kristaps  942:
1.1       kristaps  943:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.133     kristaps  944:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    945:
1.1       kristaps  946:        if (mdoc->meta.title)
1.133     kristaps  947:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    948:
1.1       kristaps  949:        return(1);
                    950: }
                    951:
                    952: static int
                    953: pre_os(PRE_ARGS)
                    954: {
                    955:
                    956:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.133     kristaps  957:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    958:
1.1       kristaps  959:        if (mdoc->meta.os)
1.133     kristaps  960:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    961:
1.1       kristaps  962:        return(1);
                    963: }
                    964:
                    965: static int
                    966: pre_dd(PRE_ARGS)
                    967: {
                    968:
                    969:        if (mdoc->meta.title || mdoc->meta.os)
1.133     kristaps  970:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    971:
1.1       kristaps  972:        if (mdoc->meta.date)
1.133     kristaps  973:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    974:
1.1       kristaps  975:        return(1);
                    976: }
                    977:
                    978:
                    979: static int
                    980: post_bf(POST_ARGS)
                    981: {
1.105     kristaps  982:        struct mdoc_node *np;
1.113     kristaps  983:        enum mdocargt     arg;
1.105     kristaps  984:
                    985:        /*
                    986:         * Unlike other data pointers, these are "housed" by the HEAD
                    987:         * element, which contains the goods.
                    988:         */
1.1       kristaps  989:
1.105     kristaps  990:        if (MDOC_HEAD != mdoc->last->type) {
                    991:                if (ENDBODY_NOT != mdoc->last->end) {
                    992:                        assert(mdoc->last->pending);
                    993:                        np = mdoc->last->pending->parent->head;
                    994:                } else if (MDOC_BLOCK != mdoc->last->type) {
                    995:                        np = mdoc->last->parent->head;
                    996:                } else
                    997:                        np = mdoc->last->head;
                    998:
                    999:                assert(np);
                   1000:                assert(MDOC_HEAD == np->type);
                   1001:                assert(MDOC_Bf == np->tok);
                   1002:                assert(np->data.Bf);
                   1003:                mdoc->last->data.Bf = np->data.Bf;
1.1       kristaps 1004:                return(1);
1.105     kristaps 1005:        }
1.1       kristaps 1006:
1.105     kristaps 1007:        np = mdoc->last;
1.106     kristaps 1008:        assert(MDOC_BLOCK == np->parent->type);
                   1009:        assert(MDOC_Bf == np->parent->tok);
1.105     kristaps 1010:        np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
1.1       kristaps 1011:
1.105     kristaps 1012:        /*
                   1013:         * Cannot have both argument and parameter.
                   1014:         * If neither is specified, let it through with a warning.
                   1015:         */
                   1016:
1.106     kristaps 1017:        if (np->parent->args && np->child) {
1.105     kristaps 1018:                mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.79      kristaps 1019:                return(0);
1.133     kristaps 1020:        } else if (NULL == np->parent->args && NULL == np->child) {
                   1021:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
                   1022:                return(1);
                   1023:        }
1.105     kristaps 1024:
                   1025:        /* Extract argument into data. */
                   1026:
1.106     kristaps 1027:        if (np->parent->args) {
                   1028:                arg = np->parent->args->argv[0].arg;
1.105     kristaps 1029:                if (MDOC_Emphasis == arg)
                   1030:                        np->data.Bf->font = FONT_Em;
                   1031:                else if (MDOC_Literal == arg)
                   1032:                        np->data.Bf->font = FONT_Li;
                   1033:                else if (MDOC_Symbolic == arg)
                   1034:                        np->data.Bf->font = FONT_Sy;
                   1035:                else
                   1036:                        abort();
1.13      kristaps 1037:                return(1);
1.79      kristaps 1038:        }
1.1       kristaps 1039:
1.105     kristaps 1040:        /* Extract parameter into data. */
1.1       kristaps 1041:
1.105     kristaps 1042:        if (0 == strcmp(np->child->string, "Em"))
                   1043:                np->data.Bf->font = FONT_Em;
                   1044:        else if (0 == strcmp(np->child->string, "Li"))
                   1045:                np->data.Bf->font = FONT_Li;
                   1046:        else if (0 == strcmp(np->child->string, "Sy"))
                   1047:                np->data.Bf->font = FONT_Sy;
1.133     kristaps 1048:        else
                   1049:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1.1       kristaps 1050:
1.105     kristaps 1051:        return(1);
1.1       kristaps 1052: }
                   1053:
                   1054: static int
1.31      kristaps 1055: post_lb(POST_ARGS)
                   1056: {
1.127     kristaps 1057:        const char      *p;
                   1058:        char            *buf;
                   1059:        size_t           sz;
                   1060:
                   1061:        assert(mdoc->last->child);
                   1062:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1063:
                   1064:        p = mdoc_a2lib(mdoc->last->child->string);
                   1065:
                   1066:        /* If lookup ok, replace with table value. */
1.31      kristaps 1067:
1.127     kristaps 1068:        if (p) {
                   1069:                free(mdoc->last->child->string);
                   1070:                mdoc->last->child->string = mandoc_strdup(p);
1.31      kristaps 1071:                return(1);
1.127     kristaps 1072:        }
                   1073:
                   1074:        /* If not, use "library ``xxxx''. */
                   1075:
                   1076:        sz = strlen(mdoc->last->child->string) +
                   1077:                2 + strlen("\\(lqlibrary\\(rq");
                   1078:        buf = mandoc_malloc(sz);
                   1079:        snprintf(buf, sz, "library \\(lq%s\\(rq",
                   1080:                        mdoc->last->child->string);
                   1081:        free(mdoc->last->child->string);
                   1082:        mdoc->last->child->string = buf;
                   1083:        return(1);
1.84      kristaps 1084: }
                   1085:
                   1086: static int
                   1087: post_eoln(POST_ARGS)
                   1088: {
                   1089:
1.133     kristaps 1090:        if (mdoc->last->child)
                   1091:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
                   1092:        return(1);
1.31      kristaps 1093: }
                   1094:
                   1095:
                   1096: static int
1.57      kristaps 1097: post_vt(POST_ARGS)
                   1098: {
                   1099:        const struct mdoc_node *n;
                   1100:
                   1101:        /*
                   1102:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                   1103:         * have different syntaxes (yet more context-sensitive
                   1104:         * behaviour).  ELEM types must have a child; BLOCK types,
                   1105:         * specifically the BODY, should only have TEXT children.
                   1106:         */
                   1107:
                   1108:        if (MDOC_ELEM == mdoc->last->type)
                   1109:                return(eerr_ge1(mdoc));
                   1110:        if (MDOC_BODY != mdoc->last->type)
                   1111:                return(1);
                   1112:
                   1113:        for (n = mdoc->last->child; n; n = n->next)
                   1114:                if (MDOC_TEXT != n->type)
1.133     kristaps 1115:                        mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.57      kristaps 1116:
                   1117:        return(1);
                   1118: }
                   1119:
                   1120:
                   1121: static int
1.1       kristaps 1122: post_nm(POST_ARGS)
                   1123: {
1.132     kristaps 1124:        char             buf[BUFSIZ];
1.129     kristaps 1125:
                   1126:        /* If no child specified, make sure we have the meta name. */
1.1       kristaps 1127:
1.129     kristaps 1128:        if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
                   1129:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1.1       kristaps 1130:                return(1);
1.129     kristaps 1131:        } else if (mdoc->meta.name)
1.1       kristaps 1132:                return(1);
1.129     kristaps 1133:
                   1134:        /* If no meta name, set it from the child. */
                   1135:
1.132     kristaps 1136:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1137:                return(0);
1.129     kristaps 1138:
1.132     kristaps 1139:        mdoc->meta.name = mandoc_strdup(buf);
1.129     kristaps 1140:
                   1141:        return(1);
1.1       kristaps 1142: }
                   1143:
1.123     kristaps 1144: static int
1.128     kristaps 1145: post_literal(POST_ARGS)
                   1146: {
                   1147:
                   1148:        /*
                   1149:         * The `Dl' (note "el" not "one") and `Bd' macros unset the
                   1150:         * MDOC_LITERAL flag as they leave.  Note that `Bd' only sets
                   1151:         * this in literal mode, but it doesn't hurt to just switch it
                   1152:         * off in general since displays can't be nested.
                   1153:         */
                   1154:
                   1155:        if (MDOC_BODY == mdoc->last->type)
1.134     kristaps 1156:                mdoc->flags &= ~MDOC_LITERAL;
1.128     kristaps 1157:
                   1158:        return(1);
                   1159: }
                   1160:
                   1161: static int
1.123     kristaps 1162: post_defaults(POST_ARGS)
                   1163: {
                   1164:        struct mdoc_node *nn;
                   1165:
                   1166:        /*
                   1167:         * The `Ar' defaults to "file ..." if no value is provided as an
1.129     kristaps 1168:         * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
                   1169:         * gets an empty string.
1.123     kristaps 1170:         */
                   1171:
                   1172:        if (mdoc->last->child)
                   1173:                return(1);
                   1174:
                   1175:        nn = mdoc->last;
                   1176:        mdoc->next = MDOC_NEXT_CHILD;
                   1177:
                   1178:        switch (nn->tok) {
                   1179:        case (MDOC_Ar):
                   1180:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
                   1181:                        return(0);
                   1182:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
                   1183:                        return(0);
                   1184:                break;
1.126     kristaps 1185:        case (MDOC_At):
                   1186:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
                   1187:                        return(0);
                   1188:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
                   1189:                        return(0);
                   1190:                break;
1.123     kristaps 1191:        case (MDOC_Li):
                   1192:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
                   1193:                        return(0);
                   1194:                break;
1.129     kristaps 1195:        case (MDOC_Pa):
                   1196:                /* FALLTHROUGH */
1.123     kristaps 1197:        case (MDOC_Mt):
                   1198:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
                   1199:                        return(0);
                   1200:                break;
                   1201:        default:
                   1202:                abort();
                   1203:                /* NOTREACHED */
                   1204:        }
                   1205:
                   1206:        mdoc->last = nn;
                   1207:        return(1);
                   1208: }
1.1       kristaps 1209:
                   1210: static int
                   1211: post_at(POST_ARGS)
                   1212: {
1.126     kristaps 1213:        const char       *p, *q;
                   1214:        char             *buf;
                   1215:        size_t            sz;
1.1       kristaps 1216:
1.126     kristaps 1217:        /*
                   1218:         * If we have a child, look it up in the standard keys.  If a
                   1219:         * key exist, use that instead of the child; if it doesn't,
                   1220:         * prefix "AT&T UNIX " to the existing data.
                   1221:         */
                   1222:
1.1       kristaps 1223:        if (NULL == mdoc->last->child)
                   1224:                return(1);
1.126     kristaps 1225:
1.79      kristaps 1226:        assert(MDOC_TEXT == mdoc->last->child->type);
1.126     kristaps 1227:        p = mdoc_a2att(mdoc->last->child->string);
                   1228:
                   1229:        if (p) {
                   1230:                free(mdoc->last->child->string);
                   1231:                mdoc->last->child->string = mandoc_strdup(p);
                   1232:        } else {
                   1233:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
                   1234:                p = "AT&T UNIX ";
                   1235:                q = mdoc->last->child->string;
                   1236:                sz = strlen(p) + strlen(q) + 1;
                   1237:                buf = mandoc_malloc(sz);
                   1238:                strlcpy(buf, p, sz);
                   1239:                strlcat(buf, q, sz);
                   1240:                free(mdoc->last->child->string);
                   1241:                mdoc->last->child->string = buf;
                   1242:        }
                   1243:
                   1244:        return(1);
1.1       kristaps 1245: }
                   1246:
                   1247: static int
                   1248: post_an(POST_ARGS)
                   1249: {
1.107     kristaps 1250:        struct mdoc_node *np;
1.1       kristaps 1251:
1.107     kristaps 1252:        np = mdoc->last;
                   1253:        if (AUTH__NONE != np->data.An.auth && np->child)
1.120     kristaps 1254:                return(eerr_eq0(mdoc));
1.133     kristaps 1255:
1.120     kristaps 1256:        /*
                   1257:         * FIXME: make this ewarn and make sure that the front-ends
                   1258:         * don't print the arguments.
                   1259:         */
1.107     kristaps 1260:        if (AUTH__NONE != np->data.An.auth || np->child)
1.1       kristaps 1261:                return(1);
1.133     kristaps 1262:
                   1263:        mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS);
                   1264:        return(1);
1.1       kristaps 1265: }
                   1266:
                   1267:
                   1268: static int
                   1269: post_it(POST_ARGS)
                   1270: {
1.89      kristaps 1271:        int               i, cols, rc;
                   1272:        enum mdoc_list    lt;
1.1       kristaps 1273:        struct mdoc_node *n, *c;
1.89      kristaps 1274:        enum mandocerr    er;
1.1       kristaps 1275:
                   1276:        if (MDOC_BLOCK != mdoc->last->type)
                   1277:                return(1);
                   1278:
                   1279:        n = mdoc->last->parent->parent;
1.104     kristaps 1280:        assert(n->data.Bl);
                   1281:        lt = n->data.Bl->type;
1.89      kristaps 1282:
                   1283:        if (LIST__NONE == lt) {
1.79      kristaps 1284:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1.133     kristaps 1285:                return(1);
1.79      kristaps 1286:        }
1.1       kristaps 1287:
1.89      kristaps 1288:        switch (lt) {
                   1289:        case (LIST_tag):
                   1290:                if (mdoc->last->head->child)
1.1       kristaps 1291:                        break;
1.89      kristaps 1292:                /* FIXME: give this a dummy value. */
1.133     kristaps 1293:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1294:                break;
1.89      kristaps 1295:        case (LIST_hang):
1.1       kristaps 1296:                /* FALLTHROUGH */
1.89      kristaps 1297:        case (LIST_ohang):
1.1       kristaps 1298:                /* FALLTHROUGH */
1.89      kristaps 1299:        case (LIST_inset):
1.1       kristaps 1300:                /* FALLTHROUGH */
1.89      kristaps 1301:        case (LIST_diag):
1.1       kristaps 1302:                if (NULL == mdoc->last->head->child)
1.133     kristaps 1303:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1304:                break;
1.89      kristaps 1305:        case (LIST_bullet):
1.1       kristaps 1306:                /* FALLTHROUGH */
1.89      kristaps 1307:        case (LIST_dash):
1.1       kristaps 1308:                /* FALLTHROUGH */
1.89      kristaps 1309:        case (LIST_enum):
1.1       kristaps 1310:                /* FALLTHROUGH */
1.89      kristaps 1311:        case (LIST_hyphen):
1.108     schwarze 1312:                if (NULL == mdoc->last->body->child)
1.133     kristaps 1313:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.1       kristaps 1314:                /* FALLTHROUGH */
1.89      kristaps 1315:        case (LIST_item):
1.1       kristaps 1316:                if (mdoc->last->head->child)
1.133     kristaps 1317:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1.1       kristaps 1318:                break;
1.89      kristaps 1319:        case (LIST_column):
1.109     kristaps 1320:                cols = (int)n->data.Bl->ncols;
1.89      kristaps 1321:
1.87      kristaps 1322:                assert(NULL == mdoc->last->head->child);
1.89      kristaps 1323:
1.87      kristaps 1324:                if (NULL == mdoc->last->body->child)
1.133     kristaps 1325:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.87      kristaps 1326:
1.89      kristaps 1327:                for (i = 0, c = mdoc->last->child; c; c = c->next)
1.87      kristaps 1328:                        if (MDOC_BODY == c->type)
                   1329:                                i++;
1.38      kristaps 1330:
1.89      kristaps 1331:                if (i < cols)
                   1332:                        er = MANDOCERR_ARGCOUNT;
                   1333:                else if (i == cols || i == cols + 1)
1.38      kristaps 1334:                        break;
1.89      kristaps 1335:                else
                   1336:                        er = MANDOCERR_SYNTARGCOUNT;
1.38      kristaps 1337:
1.89      kristaps 1338:                rc = mdoc_vmsg(mdoc, er,
1.79      kristaps 1339:                                mdoc->last->line, mdoc->last->pos,
                   1340:                                "columns == %d (have %d)", cols, i);
1.89      kristaps 1341:                return(rc);
1.1       kristaps 1342:        default:
                   1343:                break;
                   1344:        }
                   1345:
                   1346:        return(1);
                   1347: }
                   1348:
1.131     kristaps 1349: static int
                   1350: post_bl_block(POST_ARGS)
                   1351: {
                   1352:        struct mdoc_node *n;
                   1353:
                   1354:        /*
                   1355:         * These are fairly complicated, so we've broken them into two
                   1356:         * functions.  post_bl_block_tag() is called when a -tag is
                   1357:         * specified, but no -width (it must be guessed).  The second
                   1358:         * when a -width is specified (macro indicators must be
                   1359:         * rewritten into real lengths).
                   1360:         */
                   1361:
                   1362:        n = mdoc->last;
                   1363:
                   1364:        if (LIST_tag == n->data.Bl->type &&
                   1365:                        NULL == n->data.Bl->width) {
                   1366:                if ( ! post_bl_block_tag(mdoc))
                   1367:                        return(0);
                   1368:        } else if (NULL != n->data.Bl->width) {
                   1369:                if ( ! post_bl_block_width(mdoc))
                   1370:                        return(0);
                   1371:        } else
                   1372:                return(1);
                   1373:
                   1374:        assert(n->data.Bl->width);
                   1375:        return(1);
                   1376: }
                   1377:
                   1378: static int
                   1379: post_bl_block_width(POST_ARGS)
                   1380: {
                   1381:        size_t            width;
                   1382:        int               i;
                   1383:        enum mdoct        tok;
                   1384:        struct mdoc_node *n;
                   1385:        char              buf[NUMSIZ];
                   1386:
                   1387:        n = mdoc->last;
                   1388:
                   1389:        /*
                   1390:         * Calculate the real width of a list from the -width string,
                   1391:         * which may contain a macro (with a known default width), a
                   1392:         * literal string, or a scaling width.
                   1393:         *
                   1394:         * If the value to -width is a macro, then we re-write it to be
                   1395:         * the macro's width as set in share/tmac/mdoc/doc-common.
                   1396:         */
                   1397:
                   1398:        if (0 == strcmp(n->data.Bl->width, "Ds"))
                   1399:                width = 6;
                   1400:        else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl->width)))
                   1401:                return(1);
1.133     kristaps 1402:        else if (0 == (width = mdoc_macro2len(tok)))  {
                   1403:                mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
                   1404:                return(1);
                   1405:        }
1.131     kristaps 1406:
                   1407:        /* The value already exists: free and reallocate it. */
                   1408:
                   1409:        assert(n->args);
                   1410:
                   1411:        for (i = 0; i < (int)n->args->argc; i++)
                   1412:                if (MDOC_Width == n->args->argv[i].arg)
                   1413:                        break;
                   1414:
                   1415:        assert(i < (int)n->args->argc);
                   1416:
                   1417:        snprintf(buf, NUMSIZ, "%zun", width);
                   1418:        free(n->args->argv[i].value[0]);
                   1419:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                   1420:
                   1421:        /* Set our width! */
                   1422:        n->data.Bl->width = n->args->argv[i].value[0];
                   1423:        return(1);
                   1424: }
                   1425:
                   1426: static int
                   1427: post_bl_block_tag(POST_ARGS)
                   1428: {
                   1429:        struct mdoc_node *n, *nn;
                   1430:        size_t            sz, ssz;
                   1431:        int               i;
                   1432:        char              buf[NUMSIZ];
                   1433:
                   1434:        /*
                   1435:         * Calculate the -width for a `Bl -tag' list if it hasn't been
                   1436:         * provided.  Uses the first head macro.  NOTE AGAIN: this is
                   1437:         * ONLY if the -width argument has NOT been provided.  See
                   1438:         * post_bl_block_width() for converting the -width string.
                   1439:         */
                   1440:
                   1441:        sz = 10;
                   1442:        n = mdoc->last;
                   1443:
                   1444:        for (nn = n->body->child; nn; nn = nn->next) {
                   1445:                if (MDOC_It != nn->tok)
                   1446:                        continue;
                   1447:
                   1448:                assert(MDOC_BLOCK == nn->type);
                   1449:                nn = nn->head->child;
                   1450:
                   1451:                if (nn == NULL) {
                   1452:                        /* No -width for .Bl and first .It is emtpy */
1.133     kristaps 1453:                        mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
1.131     kristaps 1454:                        break;
                   1455:                }
                   1456:
                   1457:                if (MDOC_TEXT == nn->type) {
                   1458:                        sz = strlen(nn->string) + 1;
                   1459:                        break;
                   1460:                }
                   1461:
                   1462:                if (0 != (ssz = mdoc_macro2len(nn->tok)))
                   1463:                        sz = ssz;
1.133     kristaps 1464:                else
                   1465:                        mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
1.131     kristaps 1466:
                   1467:                break;
                   1468:        }
                   1469:
                   1470:        /* Defaults to ten ens. */
                   1471:
                   1472:        snprintf(buf, NUMSIZ, "%zun", sz);
                   1473:
                   1474:        /*
                   1475:         * We have to dynamically add this to the macro's argument list.
                   1476:         * We're guaranteed that a MDOC_Width doesn't already exist.
                   1477:         */
                   1478:
                   1479:        assert(n->args);
                   1480:        i = (int)(n->args->argc)++;
                   1481:
                   1482:        n->args->argv = mandoc_realloc(n->args->argv,
                   1483:                        n->args->argc * sizeof(struct mdoc_argv));
                   1484:
                   1485:        n->args->argv[i].arg = MDOC_Width;
                   1486:        n->args->argv[i].line = n->line;
                   1487:        n->args->argv[i].pos = n->pos;
                   1488:        n->args->argv[i].sz = 1;
                   1489:        n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                   1490:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                   1491:
                   1492:        /* Set our width! */
                   1493:        n->data.Bl->width = n->args->argv[i].value[0];
                   1494:        return(1);
                   1495: }
                   1496:
1.1       kristaps 1497:
                   1498: static int
1.14      kristaps 1499: post_bl_head(POST_ARGS)
                   1500: {
1.130     kristaps 1501:        struct mdoc_node *np, *nn, *nnp;
                   1502:        int               i, j;
1.14      kristaps 1503:
1.130     kristaps 1504:        if (LIST_column != mdoc->last->data.Bl->type)
                   1505:                /* FIXME: this should be ERROR class... */
                   1506:                return(hwarn_eq0(mdoc));
1.14      kristaps 1507:
1.130     kristaps 1508:        /*
                   1509:         * Convert old-style lists, where the column width specifiers
                   1510:         * trail as macro parameters, to the new-style ("normal-form")
                   1511:         * lists where they're argument values following -column.
                   1512:         */
                   1513:
                   1514:        /* First, disallow both types and allow normal-form. */
                   1515:
                   1516:        /*
                   1517:         * TODO: technically, we can accept both and just merge the two
                   1518:         * lists, but I'll leave that for another day.
                   1519:         */
                   1520:
                   1521:        if (mdoc->last->data.Bl->ncols && mdoc->last->nchild) {
                   1522:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
                   1523:                return(0);
                   1524:        } else if (NULL == mdoc->last->child)
1.90      kristaps 1525:                return(1);
1.130     kristaps 1526:
                   1527:        np = mdoc->last->parent;
                   1528:        assert(np->args);
                   1529:
                   1530:        for (j = 0; j < (int)np->args->argc; j++)
                   1531:                if (MDOC_Column == np->args->argv[j].arg)
                   1532:                        break;
                   1533:
                   1534:        assert(j < (int)np->args->argc);
                   1535:        assert(0 == np->args->argv[j].sz);
                   1536:
                   1537:        /*
                   1538:         * Accomodate for new-style groff column syntax.  Shuffle the
                   1539:         * child nodes, all of which must be TEXT, as arguments for the
                   1540:         * column field.  Then, delete the head children.
                   1541:         */
                   1542:
                   1543:        np->args->argv[j].sz = (size_t)mdoc->last->nchild;
                   1544:        np->args->argv[j].value = mandoc_malloc
                   1545:                ((size_t)mdoc->last->nchild * sizeof(char *));
                   1546:
                   1547:        mdoc->last->data.Bl->ncols = np->args->argv[j].sz;
                   1548:        mdoc->last->data.Bl->cols = (const char **)np->args->argv[j].value;
                   1549:
                   1550:        for (i = 0, nn = mdoc->last->child; nn; i++) {
                   1551:                np->args->argv[j].value[i] = nn->string;
                   1552:                nn->string = NULL;
                   1553:                nnp = nn;
                   1554:                nn = nn->next;
                   1555:                mdoc_node_delete(NULL, nnp);
1.65      kristaps 1556:        }
1.14      kristaps 1557:
1.130     kristaps 1558:        mdoc->last->nchild = 0;
                   1559:        mdoc->last->child = NULL;
                   1560:
                   1561:        return(1);
1.14      kristaps 1562: }
                   1563:
                   1564: static int
1.1       kristaps 1565: post_bl(POST_ARGS)
                   1566: {
                   1567:        struct mdoc_node        *n;
                   1568:
1.14      kristaps 1569:        if (MDOC_HEAD == mdoc->last->type)
                   1570:                return(post_bl_head(mdoc));
1.131     kristaps 1571:        if (MDOC_BLOCK == mdoc->last->type)
                   1572:                return(post_bl_block(mdoc));
1.1       kristaps 1573:        if (MDOC_BODY != mdoc->last->type)
                   1574:                return(1);
                   1575:        if (NULL == mdoc->last->child)
                   1576:                return(1);
                   1577:
1.55      kristaps 1578:        /*
                   1579:         * We only allow certain children of `Bl'.  This is usually on
                   1580:         * `It', but apparently `Sm' occurs here and there, so we let
                   1581:         * that one through, too.
                   1582:         */
                   1583:
1.1       kristaps 1584:        /* LINTED */
                   1585:        for (n = mdoc->last->child; n; n = n->next) {
1.55      kristaps 1586:                if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
                   1587:                        continue;
                   1588:                if (MDOC_Sm == n->tok)
                   1589:                        continue;
1.79      kristaps 1590:                mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
                   1591:                return(0);
1.1       kristaps 1592:        }
                   1593:
                   1594:        return(1);
                   1595: }
                   1596:
                   1597: static int
                   1598: ebool(struct mdoc *mdoc)
                   1599: {
                   1600:
1.133     kristaps 1601:        if (NULL == mdoc->last->child)
                   1602:                return(1);
                   1603:
                   1604:        assert(MDOC_TEXT == mdoc->last->child->type);
1.1       kristaps 1605:
1.133     kristaps 1606:        if (0 == strcmp(mdoc->last->child->string, "on"))
1.1       kristaps 1607:                return(1);
1.133     kristaps 1608:        if (0 == strcmp(mdoc->last->child->string, "off"))
                   1609:                return(1);
                   1610:
                   1611:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
                   1612:        return(1);
1.1       kristaps 1613: }
                   1614:
                   1615: static int
                   1616: post_root(POST_ARGS)
                   1617: {
1.133     kristaps 1618:        int               erc;
                   1619:        struct mdoc_node *n;
1.1       kristaps 1620:
1.133     kristaps 1621:        erc = 0;
                   1622:
                   1623:        /* Check that we have a finished prologue. */
                   1624:
                   1625:        if ( ! (MDOC_PBODY & mdoc->flags)) {
                   1626:                erc++;
1.79      kristaps 1627:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1.133     kristaps 1628:        }
                   1629:
                   1630:        n = mdoc->first;
                   1631:        assert(n);
                   1632:
                   1633:        /* Check that we begin with a proper `Sh'. */
                   1634:
                   1635:        if (NULL == n->child) {
                   1636:                erc++;
                   1637:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1638:        } else if (MDOC_BLOCK != n->child->type ||
                   1639:                        MDOC_Sh != n->child->tok) {
                   1640:                erc++;
                   1641:                /* Can this be lifted?  See rxdebug.1 for example. */
                   1642:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1643:        }
1.1       kristaps 1644:
1.133     kristaps 1645:        return(erc ? 0 : 1);
1.1       kristaps 1646: }
                   1647:
                   1648: static int
                   1649: post_st(POST_ARGS)
                   1650: {
1.128     kristaps 1651:        const char      *p;
                   1652:
                   1653:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1654:
                   1655:        p = mdoc_a2st(mdoc->last->child->string);
                   1656:
                   1657:        if (p == NULL) {
                   1658:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
                   1659:                mdoc_node_delete(mdoc, mdoc->last);
                   1660:        } else {
                   1661:                free(mdoc->last->child->string);
                   1662:                mdoc->last->child->string = mandoc_strdup(p);
                   1663:        }
1.1       kristaps 1664:
1.128     kristaps 1665:        return(1);
1.1       kristaps 1666: }
                   1667:
                   1668: static int
1.43      kristaps 1669: post_rs(POST_ARGS)
                   1670: {
1.122     kristaps 1671:        struct mdoc_node *nn, *next, *prev;
                   1672:        int               i, j;
1.43      kristaps 1673:
                   1674:        if (MDOC_BODY != mdoc->last->type)
                   1675:                return(1);
                   1676:
1.122     kristaps 1677:        /*
                   1678:         * Make sure only certain types of nodes are allowed within the
                   1679:         * the `Rs' body.  Delete offending nodes and raise a warning.
                   1680:         * Do this before re-ordering for the sake of clarity.
                   1681:         */
                   1682:
                   1683:        next = NULL;
                   1684:        for (nn = mdoc->last->child; nn; nn = next) {
                   1685:                for (i = 0; i < RSORD_MAX; i++)
                   1686:                        if (nn->tok == rsord[i])
                   1687:                                break;
                   1688:
                   1689:                if (i < RSORD_MAX) {
                   1690:                        next = nn->next;
                   1691:                        continue;
                   1692:                }
                   1693:
                   1694:                next = nn->next;
                   1695:                mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
                   1696:                mdoc_node_delete(mdoc, nn);
                   1697:        }
                   1698:
                   1699:        /*
                   1700:         * The full `Rs' block needs special handling to order the
                   1701:         * sub-elements according to `rsord'.  Pick through each element
                   1702:         * and correctly order it.  This is a insertion sort.
                   1703:         */
                   1704:
                   1705:        next = NULL;
                   1706:        for (nn = mdoc->last->child->next; nn; nn = next) {
                   1707:                /* Determine order of `nn'. */
                   1708:                for (i = 0; i < RSORD_MAX; i++)
                   1709:                        if (rsord[i] == nn->tok)
                   1710:                                break;
                   1711:
                   1712:                /*
                   1713:                 * Remove `nn' from the chain.  This somewhat
                   1714:                 * repeats mdoc_node_unlink(), but since we're
                   1715:                 * just re-ordering, there's no need for the
                   1716:                 * full unlink process.
                   1717:                 */
                   1718:
                   1719:                if (NULL != (next = nn->next))
                   1720:                        next->prev = nn->prev;
                   1721:
                   1722:                if (NULL != (prev = nn->prev))
                   1723:                        prev->next = nn->next;
                   1724:
                   1725:                nn->prev = nn->next = NULL;
                   1726:
                   1727:                /*
                   1728:                 * Scan back until we reach a node that's
                   1729:                 * ordered before `nn'.
                   1730:                 */
                   1731:
                   1732:                for ( ; prev ; prev = prev->prev) {
                   1733:                        /* Determine order of `prev'. */
                   1734:                        for (j = 0; j < RSORD_MAX; j++)
                   1735:                                if (rsord[j] == prev->tok)
                   1736:                                        break;
                   1737:
                   1738:                        if (j <= i)
                   1739:                                break;
                   1740:                }
                   1741:
                   1742:                /*
                   1743:                 * Set `nn' back into its correct place in front
                   1744:                 * of the `prev' node.
                   1745:                 */
                   1746:
                   1747:                nn->prev = prev;
                   1748:
                   1749:                if (prev) {
                   1750:                        if (prev->next)
                   1751:                                prev->next->prev = nn;
                   1752:                        nn->next = prev->next;
                   1753:                        prev->next = nn;
                   1754:                } else {
                   1755:                        mdoc->last->child->prev = nn;
                   1756:                        nn->next = mdoc->last->child;
                   1757:                        mdoc->last->child = nn;
1.43      kristaps 1758:                }
1.122     kristaps 1759:        }
1.43      kristaps 1760:
                   1761:        return(1);
                   1762: }
                   1763:
                   1764: static int
1.1       kristaps 1765: post_sh(POST_ARGS)
                   1766: {
                   1767:
                   1768:        if (MDOC_HEAD == mdoc->last->type)
                   1769:                return(post_sh_head(mdoc));
                   1770:        if (MDOC_BODY == mdoc->last->type)
                   1771:                return(post_sh_body(mdoc));
                   1772:
                   1773:        return(1);
                   1774: }
                   1775:
                   1776: static int
                   1777: post_sh_body(POST_ARGS)
                   1778: {
                   1779:        struct mdoc_node *n;
                   1780:
1.29      kristaps 1781:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1782:                return(1);
                   1783:
                   1784:        /*
                   1785:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1786:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1787:         * children of the BODY declaration can also be "text".
                   1788:         */
                   1789:
1.133     kristaps 1790:        if (NULL == (n = mdoc->last->child)) {
                   1791:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1792:                return(1);
                   1793:        }
1.1       kristaps 1794:
                   1795:        for ( ; n && n->next; n = n->next) {
                   1796:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1797:                        continue;
1.28      kristaps 1798:                if (MDOC_TEXT == n->type)
                   1799:                        continue;
1.133     kristaps 1800:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1.1       kristaps 1801:        }
                   1802:
1.41      kristaps 1803:        assert(n);
1.27      kristaps 1804:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1805:                return(1);
1.133     kristaps 1806:
                   1807:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1808:        return(1);
1.1       kristaps 1809: }
                   1810:
                   1811: static int
                   1812: post_sh_head(POST_ARGS)
                   1813: {
1.132     kristaps 1814:        char             buf[BUFSIZ];
                   1815:        enum mdoc_sec    sec;
1.1       kristaps 1816:
                   1817:        /*
                   1818:         * Process a new section.  Sections are either "named" or
1.125     kristaps 1819:         * "custom".  Custom sections are user-defined, while named ones
                   1820:         * follow a conventional order and may only appear in certain
                   1821:         * manual sections.
1.1       kristaps 1822:         */
                   1823:
1.132     kristaps 1824:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1825:                return(0);
1.1       kristaps 1826:
1.72      kristaps 1827:        sec = mdoc_str2sec(buf);
1.1       kristaps 1828:
1.125     kristaps 1829:        /* The NAME should be first. */
1.1       kristaps 1830:
1.72      kristaps 1831:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.125     kristaps 1832:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
                   1833:
                   1834:        /* The SYNOPSIS gets special attention in other areas. */
                   1835:
                   1836:        if (SEC_SYNOPSIS == sec)
                   1837:                mdoc->flags |= MDOC_SYNOPSIS;
                   1838:        else
                   1839:                mdoc->flags &= ~MDOC_SYNOPSIS;
                   1840:
                   1841:        /* Mark our last section. */
                   1842:
                   1843:        mdoc->lastsec = sec;
                   1844:
                   1845:        /* We don't care about custom sections after this. */
1.72      kristaps 1846:
1.1       kristaps 1847:        if (SEC_CUSTOM == sec)
                   1848:                return(1);
1.72      kristaps 1849:
1.125     kristaps 1850:        /*
                   1851:         * Check whether our non-custom section is being repeated or is
                   1852:         * out of order.
                   1853:         */
                   1854:
1.1       kristaps 1855:        if (sec == mdoc->lastnamed)
1.125     kristaps 1856:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.72      kristaps 1857:
1.1       kristaps 1858:        if (sec < mdoc->lastnamed)
1.125     kristaps 1859:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
                   1860:
                   1861:        /* Mark the last named section. */
                   1862:
                   1863:        mdoc->lastnamed = sec;
                   1864:
                   1865:        /* Check particular section/manual conventions. */
1.1       kristaps 1866:
1.125     kristaps 1867:        assert(mdoc->meta.msec);
1.1       kristaps 1868:
                   1869:        switch (sec) {
1.125     kristaps 1870:        case (SEC_RETURN_VALUES):
                   1871:                /* FALLTHROUGH */
                   1872:        case (SEC_ERRORS):
                   1873:                /* FALLTHROUGH */
1.1       kristaps 1874:        case (SEC_LIBRARY):
1.78      kristaps 1875:                if (*mdoc->meta.msec == '2')
                   1876:                        break;
                   1877:                if (*mdoc->meta.msec == '3')
                   1878:                        break;
                   1879:                if (*mdoc->meta.msec == '9')
1.1       kristaps 1880:                        break;
1.125     kristaps 1881:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
                   1882:                break;
1.1       kristaps 1883:        default:
                   1884:                break;
                   1885:        }
                   1886:
1.117     kristaps 1887:        return(1);
                   1888: }
                   1889:
                   1890: static int
1.124     kristaps 1891: pre_par(PRE_ARGS)
1.117     kristaps 1892: {
                   1893:
1.118     kristaps 1894:        if (NULL == mdoc->last)
                   1895:                return(1);
                   1896:
1.124     kristaps 1897:        /*
                   1898:         * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
                   1899:         * block:  `Lp', `Pp', or non-compact `Bd' or `Bl'.
                   1900:         */
1.118     kristaps 1901:
                   1902:        if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1.117     kristaps 1903:                return(1);
                   1904:
                   1905:        if (MDOC_Bl == n->tok && n->data.Bl->comp)
                   1906:                return(1);
                   1907:        if (MDOC_Bd == n->tok && n->data.Bd->comp)
                   1908:                return(1);
                   1909:
                   1910:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
                   1911:        mdoc_node_delete(mdoc, mdoc->last);
1.128     kristaps 1912:        return(1);
                   1913: }
                   1914:
                   1915: static int
                   1916: pre_literal(PRE_ARGS)
                   1917: {
                   1918:
                   1919:        if (MDOC_BODY != n->type)
                   1920:                return(1);
                   1921:
                   1922:        /*
                   1923:         * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
                   1924:         * -unfilled' macros set MDOC_LITERAL on entrance to the body.
                   1925:         */
                   1926:
                   1927:        switch (n->tok) {
                   1928:        case (MDOC_Dl):
                   1929:                mdoc->flags |= MDOC_LITERAL;
                   1930:                break;
                   1931:        case (MDOC_Bd):
                   1932:                assert(n->data.Bd);
                   1933:                if (DISP_literal == n->data.Bd->type)
                   1934:                        mdoc->flags |= MDOC_LITERAL;
                   1935:                if (DISP_unfilled == n->data.Bd->type)
                   1936:                        mdoc->flags |= MDOC_LITERAL;
                   1937:                break;
                   1938:        default:
                   1939:                abort();
                   1940:                /* NOTREACHED */
                   1941:        }
                   1942:
1.1       kristaps 1943:        return(1);
                   1944: }
1.132     kristaps 1945:
                   1946: static int
                   1947: post_dd(POST_ARGS)
                   1948: {
1.135     kristaps 1949:        char              buf[DATESIZE];
1.132     kristaps 1950:        struct mdoc_node *n;
                   1951:
                   1952:        n = mdoc->last;
                   1953:
                   1954:        if (NULL == n->child) {
                   1955:                mdoc->meta.date = time(NULL);
1.133     kristaps 1956:                return(1);
1.132     kristaps 1957:        }
                   1958:
1.135     kristaps 1959:        if ( ! concat(mdoc, buf, n->child, DATESIZE))
1.132     kristaps 1960:                return(0);
                   1961:
                   1962:        mdoc->meta.date = mandoc_a2time
                   1963:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                   1964:
                   1965:        if (0 == mdoc->meta.date) {
1.133     kristaps 1966:                mdoc_nmsg(mdoc, n, MANDOCERR_BADDATE);
1.132     kristaps 1967:                mdoc->meta.date = time(NULL);
                   1968:        }
                   1969:
                   1970:        return(1);
                   1971: }
                   1972:
                   1973: static int
                   1974: post_dt(POST_ARGS)
                   1975: {
                   1976:        struct mdoc_node *nn, *n;
                   1977:        const char       *cp;
                   1978:        char             *p;
                   1979:
                   1980:        n = mdoc->last;
                   1981:
                   1982:        if (mdoc->meta.title)
                   1983:                free(mdoc->meta.title);
                   1984:        if (mdoc->meta.vol)
                   1985:                free(mdoc->meta.vol);
                   1986:        if (mdoc->meta.arch)
                   1987:                free(mdoc->meta.arch);
                   1988:
                   1989:        mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
                   1990:
                   1991:        /* First make all characters uppercase. */
                   1992:
                   1993:        if (NULL != (nn = n->child))
                   1994:                for (p = nn->string; *p; p++) {
                   1995:                        if (toupper((u_char)*p) == *p)
                   1996:                                continue;
1.133     kristaps 1997:
                   1998:                        /*
                   1999:                         * FIXME: don't be lazy: have this make all
                   2000:                         * characters be uppercase and just warn once.
                   2001:                         */
                   2002:                        mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
1.132     kristaps 2003:                        break;
                   2004:                }
                   2005:
                   2006:        /* Handles: `.Dt'
                   2007:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                   2008:         */
                   2009:
                   2010:        if (NULL == (nn = n->child)) {
                   2011:                /* XXX: make these macro values. */
                   2012:                /* FIXME: warn about missing values. */
                   2013:                mdoc->meta.title = mandoc_strdup("UNKNOWN");
                   2014:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2015:                mdoc->meta.msec = mandoc_strdup("1");
                   2016:                return(1);
                   2017:        }
                   2018:
                   2019:        /* Handles: `.Dt TITLE'
                   2020:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                   2021:         */
                   2022:
                   2023:        mdoc->meta.title = mandoc_strdup
                   2024:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
                   2025:
                   2026:        if (NULL == (nn = nn->next)) {
                   2027:                /* FIXME: warn about missing msec. */
                   2028:                /* XXX: make this a macro value. */
                   2029:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2030:                mdoc->meta.msec = mandoc_strdup("1");
                   2031:                return(1);
                   2032:        }
                   2033:
                   2034:        /* Handles: `.Dt TITLE SEC'
                   2035:         *   --> title = TITLE, volume = SEC is msec ?
                   2036:         *           format(msec) : SEC,
                   2037:         *       msec = SEC is msec ? atoi(msec) : 0,
                   2038:         *       arch = NULL
                   2039:         */
                   2040:
                   2041:        cp = mdoc_a2msec(nn->string);
                   2042:        if (cp) {
                   2043:                mdoc->meta.vol = mandoc_strdup(cp);
                   2044:                mdoc->meta.msec = mandoc_strdup(nn->string);
1.133     kristaps 2045:        } else {
                   2046:                mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
1.132     kristaps 2047:                mdoc->meta.vol = mandoc_strdup(nn->string);
                   2048:                mdoc->meta.msec = mandoc_strdup(nn->string);
1.133     kristaps 2049:        }
1.132     kristaps 2050:
                   2051:        if (NULL == (nn = nn->next))
                   2052:                return(1);
                   2053:
                   2054:        /* Handles: `.Dt TITLE SEC VOL'
                   2055:         *   --> title = TITLE, volume = VOL is vol ?
                   2056:         *       format(VOL) :
                   2057:         *           VOL is arch ? format(arch) :
                   2058:         *               VOL
                   2059:         */
                   2060:
                   2061:        cp = mdoc_a2vol(nn->string);
                   2062:        if (cp) {
                   2063:                free(mdoc->meta.vol);
                   2064:                mdoc->meta.vol = mandoc_strdup(cp);
                   2065:        } else {
                   2066:                /* FIXME: warn about bad arch. */
                   2067:                cp = mdoc_a2arch(nn->string);
                   2068:                if (NULL == cp) {
                   2069:                        free(mdoc->meta.vol);
                   2070:                        mdoc->meta.vol = mandoc_strdup(nn->string);
                   2071:                } else
                   2072:                        mdoc->meta.arch = mandoc_strdup(cp);
                   2073:        }
                   2074:
                   2075:        /* Ignore any subsequent parameters... */
                   2076:        /* FIXME: warn about subsequent parameters. */
                   2077:
                   2078:        return(1);
                   2079: }
                   2080:
                   2081: static int
                   2082: post_prol(POST_ARGS)
                   2083: {
                   2084:        /*
                   2085:         * Remove prologue macros from the document after they're
                   2086:         * processed.  The final document uses mdoc_meta for these
                   2087:         * values and discards the originals.
                   2088:         */
                   2089:
                   2090:        mdoc_node_delete(mdoc, mdoc->last);
                   2091:        if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
                   2092:                mdoc->flags |= MDOC_PBODY;
                   2093:
                   2094:        return(1);
                   2095: }
                   2096:
                   2097: static int
                   2098: post_os(POST_ARGS)
                   2099: {
                   2100:        struct mdoc_node *n;
                   2101:        char              buf[BUFSIZ];
                   2102: #ifndef OSNAME
                   2103:        struct utsname    utsname;
                   2104: #endif
                   2105:
                   2106:        n = mdoc->last;
                   2107:
                   2108:        /*
                   2109:         * Set the operating system by way of the `Os' macro.  Note that
                   2110:         * if an argument isn't provided and -DOSNAME="\"foo\"" is
                   2111:         * provided during compilation, this value will be used instead
                   2112:         * of filling in "sysname release" from uname().
                   2113:         */
                   2114:
                   2115:        if (mdoc->meta.os)
                   2116:                free(mdoc->meta.os);
                   2117:
                   2118:        if ( ! concat(mdoc, buf, n->child, BUFSIZ))
                   2119:                return(0);
                   2120:
                   2121:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                   2122:         * it's really not worth the extra hackery.
                   2123:         */
                   2124:
                   2125:        if ('\0' == buf[0]) {
                   2126: #ifdef OSNAME
                   2127:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                   2128:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2129:                        return(0);
                   2130:                }
                   2131: #else /*!OSNAME */
1.136     kristaps 2132:                if (uname(&utsname)) {
                   2133:                        mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
                   2134:                         mdoc->meta.os = mandoc_strdup("UNKNOWN");
                   2135:                         return(post_prol(mdoc));
                   2136:                 }
1.132     kristaps 2137:
                   2138:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                   2139:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2140:                        return(0);
                   2141:                }
1.136     kristaps 2142:                if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.132     kristaps 2143:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2144:                        return(0);
                   2145:                }
                   2146:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                   2147:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2148:                        return(0);
                   2149:                }
                   2150: #endif /*!OSNAME*/
                   2151:        }
                   2152:
                   2153:        mdoc->meta.os = mandoc_strdup(buf);
                   2154:        return(1);
                   2155: }
                   2156:
                   2157: static int
                   2158: post_std(POST_ARGS)
                   2159: {
                   2160:        struct mdoc_node *nn, *n;
                   2161:
                   2162:        n = mdoc->last;
                   2163:
                   2164:        /*
                   2165:         * Macros accepting `-std' as an argument have the name of the
                   2166:         * current document (`Nm') filled in as the argument if it's not
                   2167:         * provided.
                   2168:         */
                   2169:
                   2170:        if (n->child)
                   2171:                return(1);
1.133     kristaps 2172:
1.132     kristaps 2173:        if (NULL == mdoc->meta.name)
                   2174:                return(1);
                   2175:
                   2176:        nn = n;
                   2177:        mdoc->next = MDOC_NEXT_CHILD;
                   2178:
                   2179:        if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
                   2180:                return(0);
1.133     kristaps 2181:
1.132     kristaps 2182:        mdoc->last = nn;
                   2183:        return(1);
                   2184: }
                   2185:
                   2186: static int
                   2187: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
                   2188: {
                   2189:
                   2190:        p[0] = '\0';
                   2191:
                   2192:        /*
                   2193:         * Concatenate sibling nodes together.  All siblings must be of
                   2194:         * type MDOC_TEXT or an assertion is raised.  Concatenation is
1.133     kristaps 2195:         * separated by a single whitespace.  Returns 0 on fatal (string
                   2196:         * overrun) error.
1.132     kristaps 2197:         */
                   2198:
                   2199:        for ( ; n; n = n->next) {
                   2200:                assert(MDOC_TEXT == n->type);
                   2201:
                   2202:                if (strlcat(p, n->string, sz) >= sz) {
                   2203:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2204:                        return(0);
                   2205:                }
                   2206:
                   2207:                if (NULL == n->next)
                   2208:                        continue;
                   2209:
                   2210:                if (strlcat(p, " ", sz) >= sz) {
                   2211:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2212:                        return(0);
                   2213:                }
                   2214:        }
                   2215:
                   2216:        return(1);
                   2217: }
                   2218:

CVSweb