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

Annotation of mandoc/mdoc_validate.c, Revision 1.162

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

CVSweb