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

Annotation of mandoc/mdoc_validate.c, Revision 1.172

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

CVSweb