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

Annotation of mandoc/mdoc_validate.c, Revision 1.146

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

CVSweb