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

Annotation of mandoc/mdoc_validate.c, Revision 1.132

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

CVSweb