[BACK]Return to mdoc_validate.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_validate.c, Revision 1.71

1.71    ! schwarze    1: /*     $Id: mdoc_validate.c,v 1.70 2010/09/27 21:25:28 schwarze Exp $ */
1.1       kristaps    2: /*
1.65      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.2       schwarze    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
                     17: #include <sys/types.h>
                     18:
                     19: #include <assert.h>
                     20: #include <ctype.h>
1.31      schwarze   21: #include <limits.h>
1.57      schwarze   22: #include <stdio.h>
1.1       kristaps   23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
1.55      schwarze   26: #include "mandoc.h"
1.1       kristaps   27: #include "libmdoc.h"
1.15      schwarze   28: #include "libmandoc.h"
1.1       kristaps   29:
                     30: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
                     31:
1.60      schwarze   32: #define        PRE_ARGS  struct mdoc *mdoc, struct mdoc_node *n
1.28      schwarze   33: #define        POST_ARGS struct mdoc *mdoc
1.1       kristaps   34:
                     35: typedef        int     (*v_pre)(PRE_ARGS);
                     36: typedef        int     (*v_post)(POST_ARGS);
                     37:
                     38: struct valids {
                     39:        v_pre   *pre;
                     40:        v_post  *post;
                     41: };
                     42:
1.44      schwarze   43: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.28      schwarze   44: static int      check_stdarg(PRE_ARGS);
1.61      schwarze   45: static int      check_text(struct mdoc *, int, int, char *);
1.28      schwarze   46: static int      check_argv(struct mdoc *,
1.61      schwarze   47:                        struct mdoc_node *, struct mdoc_argv *);
                     48: static int      check_args(struct mdoc *, struct mdoc_node *);
1.28      schwarze   49: static int      err_child_lt(struct mdoc *, const char *, int);
                     50: static int      warn_child_lt(struct mdoc *, const char *, int);
                     51: static int      err_child_gt(struct mdoc *, const char *, int);
                     52: static int      warn_child_gt(struct mdoc *, const char *, int);
                     53: static int      err_child_eq(struct mdoc *, const char *, int);
                     54: static int      warn_child_eq(struct mdoc *, const char *, int);
                     55: static int      warn_count(struct mdoc *, const char *,
1.1       kristaps   56:                        int, const char *, int);
1.28      schwarze   57: static int      err_count(struct mdoc *, const char *,
1.1       kristaps   58:                        int, const char *, int);
1.28      schwarze   59:
                     60: static int      berr_ge1(POST_ARGS);
                     61: static int      bwarn_ge1(POST_ARGS);
                     62: static int      ebool(POST_ARGS);
                     63: static int      eerr_eq1(POST_ARGS);
                     64: static int      eerr_ge1(POST_ARGS);
1.37      schwarze   65: static int      eerr_le1(POST_ARGS);
1.69      schwarze   66: static int      ewarn_eq0(POST_ARGS);
1.28      schwarze   67: static int      ewarn_ge1(POST_ARGS);
                     68: static int      herr_eq0(POST_ARGS);
                     69: static int      herr_ge1(POST_ARGS);
                     70: static int      hwarn_eq1(POST_ARGS);
1.48      schwarze   71: static int      hwarn_eq0(POST_ARGS);
1.28      schwarze   72: static int      hwarn_le1(POST_ARGS);
                     73:
                     74: static int      post_an(POST_ARGS);
                     75: static int      post_at(POST_ARGS);
                     76: static int      post_bf(POST_ARGS);
                     77: static int      post_bl(POST_ARGS);
                     78: static int      post_bl_head(POST_ARGS);
1.60      schwarze   79: static int      post_dt(POST_ARGS);
1.28      schwarze   80: static int      post_it(POST_ARGS);
                     81: static int      post_lb(POST_ARGS);
                     82: static int      post_nm(POST_ARGS);
                     83: static int      post_root(POST_ARGS);
1.36      schwarze   84: static int      post_rs(POST_ARGS);
1.28      schwarze   85: static int      post_sh(POST_ARGS);
                     86: static int      post_sh_body(POST_ARGS);
                     87: static int      post_sh_head(POST_ARGS);
                     88: static int      post_st(POST_ARGS);
1.59      schwarze   89: static int      post_eoln(POST_ARGS);
1.42      schwarze   90: static int      post_vt(POST_ARGS);
1.28      schwarze   91: static int      pre_an(PRE_ARGS);
                     92: static int      pre_bd(PRE_ARGS);
                     93: static int      pre_bl(PRE_ARGS);
                     94: static int      pre_dd(PRE_ARGS);
                     95: static int      pre_display(PRE_ARGS);
                     96: static int      pre_dt(PRE_ARGS);
                     97: static int      pre_it(PRE_ARGS);
                     98: static int      pre_os(PRE_ARGS);
1.70      schwarze   99: static int      pre_pp(PRE_ARGS);
1.28      schwarze  100: static int      pre_rv(PRE_ARGS);
                    101: static int      pre_sh(PRE_ARGS);
                    102: static int      pre_ss(PRE_ARGS);
                    103:
                    104: static v_post   posts_an[] = { post_an, NULL };
                    105: static v_post   posts_at[] = { post_at, NULL };
1.62      schwarze  106: static v_post   posts_bd_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.28      schwarze  107: static v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
                    108: static v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
                    109: static v_post   posts_bool[] = { eerr_eq1, ebool, NULL };
1.59      schwarze  110: static v_post   posts_eoln[] = { post_eoln, NULL };
1.60      schwarze  111: static v_post   posts_dt[] = { post_dt, NULL };
1.28      schwarze  112: static v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
                    113: static v_post   posts_it[] = { post_it, NULL };
                    114: static v_post   posts_lb[] = { eerr_eq1, post_lb, NULL };
                    115: static v_post   posts_nd[] = { berr_ge1, NULL };
                    116: static v_post   posts_nm[] = { post_nm, NULL };
1.69      schwarze  117: static v_post   posts_notext[] = { ewarn_eq0, NULL };
1.36      schwarze  118: static v_post   posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
1.28      schwarze  119: static v_post   posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
1.37      schwarze  120: static v_post   posts_sp[] = { eerr_le1, NULL };
1.28      schwarze  121: static v_post   posts_ss[] = { herr_ge1, NULL };
                    122: static v_post   posts_st[] = { eerr_eq1, post_st, NULL };
                    123: static v_post   posts_text[] = { eerr_ge1, NULL };
1.38      schwarze  124: static v_post   posts_text1[] = { eerr_eq1, NULL };
1.42      schwarze  125: static v_post   posts_vt[] = { post_vt, NULL };
1.28      schwarze  126: static v_post   posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
                    127: static v_post   posts_wtext[] = { ewarn_ge1, NULL };
                    128: static v_pre    pres_an[] = { pre_an, NULL };
1.70      schwarze  129: static v_pre    pres_bd[] = { pre_display, pre_bd, pre_pp, NULL };
                    130: static v_pre    pres_bl[] = { pre_bl, pre_pp, NULL };
1.28      schwarze  131: static v_pre    pres_d1[] = { pre_display, NULL };
                    132: static v_pre    pres_dd[] = { pre_dd, NULL };
                    133: static v_pre    pres_dt[] = { pre_dt, NULL };
1.48      schwarze  134: static v_pre    pres_er[] = { NULL, NULL };
1.53      schwarze  135: static v_pre    pres_ex[] = { NULL, NULL };
                    136: static v_pre    pres_fd[] = { NULL, NULL };
1.28      schwarze  137: static v_pre    pres_it[] = { pre_it, NULL };
                    138: static v_pre    pres_os[] = { pre_os, NULL };
1.70      schwarze  139: static v_pre    pres_pp[] = { pre_pp, NULL };
1.28      schwarze  140: static v_pre    pres_rv[] = { pre_rv, NULL };
                    141: static v_pre    pres_sh[] = { pre_sh, NULL };
                    142: static v_pre    pres_ss[] = { pre_ss, NULL };
1.1       kristaps  143:
                    144: const  struct valids mdoc_valids[MDOC_MAX] = {
1.7       schwarze  145:        { NULL, NULL },                         /* Ap */
1.67      schwarze  146:        { pres_dd, posts_wtext },               /* Dd */
1.60      schwarze  147:        { pres_dt, posts_dt },                  /* Dt */
1.1       kristaps  148:        { pres_os, NULL },                      /* Os */
                    149:        { pres_sh, posts_sh },                  /* Sh */
                    150:        { pres_ss, posts_ss },                  /* Ss */
1.70      schwarze  151:        { pres_pp, posts_notext },              /* Pp */
1.1       kristaps  152:        { pres_d1, posts_wline },               /* D1 */
                    153:        { pres_d1, posts_wline },               /* Dl */
1.65      schwarze  154:        { pres_bd, posts_bd_bk },               /* Bd */
1.1       kristaps  155:        { NULL, NULL },                         /* Ed */
                    156:        { pres_bl, posts_bl },                  /* Bl */
                    157:        { NULL, NULL },                         /* El */
                    158:        { pres_it, posts_it },                  /* It */
                    159:        { NULL, posts_text },                   /* Ad */
                    160:        { pres_an, posts_an },                  /* An */
                    161:        { NULL, NULL },                         /* Ar */
1.50      schwarze  162:        { NULL, posts_text },                   /* Cd */
1.1       kristaps  163:        { NULL, NULL },                         /* Cm */
                    164:        { NULL, NULL },                         /* Dv */
                    165:        { pres_er, posts_text },                /* Er */
                    166:        { NULL, NULL },                         /* Ev */
1.35      schwarze  167:        { pres_ex, NULL },                      /* Ex */
1.1       kristaps  168:        { NULL, NULL },                         /* Fa */
                    169:        { pres_fd, posts_wtext },               /* Fd */
                    170:        { NULL, NULL },                         /* Fl */
                    171:        { NULL, posts_text },                   /* Fn */
                    172:        { NULL, posts_wtext },                  /* Ft */
                    173:        { NULL, posts_text },                   /* Ic */
1.38      schwarze  174:        { NULL, posts_text1 },                  /* In */
1.1       kristaps  175:        { NULL, NULL },                         /* Li */
1.25      schwarze  176:        { NULL, posts_nd },                     /* Nd */
1.1       kristaps  177:        { NULL, posts_nm },                     /* Nm */
                    178:        { NULL, posts_wline },                  /* Op */
                    179:        { NULL, NULL },                         /* Ot */
                    180:        { NULL, NULL },                         /* Pa */
1.35      schwarze  181:        { pres_rv, NULL },                      /* Rv */
1.1       kristaps  182:        { NULL, posts_st },                     /* St */
                    183:        { NULL, NULL },                         /* Va */
1.42      schwarze  184:        { NULL, posts_vt },                     /* Vt */
1.59      schwarze  185:        { NULL, posts_wtext },                  /* Xr */
1.1       kristaps  186:        { NULL, posts_text },                   /* %A */
1.37      schwarze  187:        { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
1.40      schwarze  188:        { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1       kristaps  189:        { NULL, posts_text },                   /* %I */
                    190:        { NULL, posts_text },                   /* %J */
                    191:        { NULL, posts_text },                   /* %N */
                    192:        { NULL, posts_text },                   /* %O */
                    193:        { NULL, posts_text },                   /* %P */
                    194:        { NULL, posts_text },                   /* %R */
1.37      schwarze  195:        { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1       kristaps  196:        { NULL, posts_text },                   /* %V */
                    197:        { NULL, NULL },                         /* Ac */
                    198:        { NULL, NULL },                         /* Ao */
                    199:        { NULL, posts_wline },                  /* Aq */
                    200:        { NULL, posts_at },                     /* At */
                    201:        { NULL, NULL },                         /* Bc */
                    202:        { NULL, posts_bf },                     /* Bf */
                    203:        { NULL, NULL },                         /* Bo */
                    204:        { NULL, posts_wline },                  /* Bq */
                    205:        { NULL, NULL },                         /* Bsx */
                    206:        { NULL, NULL },                         /* Bx */
                    207:        { NULL, posts_bool },                   /* Db */
                    208:        { NULL, NULL },                         /* Dc */
                    209:        { NULL, NULL },                         /* Do */
                    210:        { NULL, posts_wline },                  /* Dq */
                    211:        { NULL, NULL },                         /* Ec */
                    212:        { NULL, NULL },                         /* Ef */
                    213:        { NULL, NULL },                         /* Em */
                    214:        { NULL, NULL },                         /* Eo */
                    215:        { NULL, NULL },                         /* Fx */
                    216:        { NULL, posts_text },                   /* Ms */
                    217:        { NULL, posts_notext },                 /* No */
                    218:        { NULL, posts_notext },                 /* Ns */
                    219:        { NULL, NULL },                         /* Nx */
                    220:        { NULL, NULL },                         /* Ox */
                    221:        { NULL, NULL },                         /* Pc */
1.38      schwarze  222:        { NULL, posts_text1 },                  /* Pf */
1.1       kristaps  223:        { NULL, NULL },                         /* Po */
                    224:        { NULL, posts_wline },                  /* Pq */
                    225:        { NULL, NULL },                         /* Qc */
                    226:        { NULL, posts_wline },                  /* Ql */
                    227:        { NULL, NULL },                         /* Qo */
                    228:        { NULL, posts_wline },                  /* Qq */
                    229:        { NULL, NULL },                         /* Re */
1.36      schwarze  230:        { NULL, posts_rs },                     /* Rs */
1.1       kristaps  231:        { NULL, NULL },                         /* Sc */
                    232:        { NULL, NULL },                         /* So */
                    233:        { NULL, posts_wline },                  /* Sq */
                    234:        { NULL, posts_bool },                   /* Sm */
                    235:        { NULL, posts_text },                   /* Sx */
                    236:        { NULL, posts_text },                   /* Sy */
                    237:        { NULL, posts_text },                   /* Tn */
                    238:        { NULL, NULL },                         /* Ux */
                    239:        { NULL, NULL },                         /* Xc */
                    240:        { NULL, NULL },                         /* Xo */
                    241:        { NULL, posts_fo },                     /* Fo */
                    242:        { NULL, NULL },                         /* Fc */
                    243:        { NULL, NULL },                         /* Oo */
                    244:        { NULL, NULL },                         /* Oc */
1.62      schwarze  245:        { NULL, posts_bd_bk },                  /* Bk */
1.1       kristaps  246:        { NULL, NULL },                         /* Ek */
1.59      schwarze  247:        { NULL, posts_eoln },                   /* Bt */
1.1       kristaps  248:        { NULL, NULL },                         /* Hf */
                    249:        { NULL, NULL },                         /* Fr */
1.59      schwarze  250:        { NULL, posts_eoln },                   /* Ud */
                    251:        { NULL, posts_lb },                     /* Lb */
1.31      schwarze  252:        { NULL, posts_notext },                 /* Lp */
1.38      schwarze  253:        { NULL, posts_text },                   /* Lk */
1.1       kristaps  254:        { NULL, posts_text },                   /* Mt */
                    255:        { NULL, posts_wline },                  /* Brq */
                    256:        { NULL, NULL },                         /* Bro */
                    257:        { NULL, NULL },                         /* Brc */
                    258:        { NULL, posts_text },                   /* %C */
                    259:        { NULL, NULL },                         /* Es */
                    260:        { NULL, NULL },                         /* En */
                    261:        { NULL, NULL },                         /* Dx */
                    262:        { NULL, posts_text },                   /* %Q */
1.31      schwarze  263:        { NULL, posts_notext },                 /* br */
1.70      schwarze  264:        { pres_pp, posts_sp },                  /* sp */
1.38      schwarze  265:        { NULL, posts_text1 },                  /* %U */
1.60      schwarze  266:        { NULL, NULL },                         /* Ta */
1.71    ! schwarze  267:        { NULL, NULL },                         /* TS */
        !           268:        { NULL, NULL },                         /* TE */
1.1       kristaps  269: };
                    270:
                    271:
                    272: int
1.60      schwarze  273: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1       kristaps  274: {
                    275:        v_pre           *p;
                    276:        int              line, pos;
1.61      schwarze  277:        char            *tp;
1.1       kristaps  278:
                    279:        if (MDOC_TEXT == n->type) {
                    280:                tp = n->string;
                    281:                line = n->line;
                    282:                pos = n->pos;
                    283:                return(check_text(mdoc, line, pos, tp));
                    284:        }
                    285:
                    286:        if ( ! check_args(mdoc, n))
                    287:                return(0);
                    288:        if (NULL == mdoc_valids[n->tok].pre)
                    289:                return(1);
                    290:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    291:                if ( ! (*p)(mdoc, n))
                    292:                        return(0);
                    293:        return(1);
                    294: }
                    295:
                    296:
                    297: int
                    298: mdoc_valid_post(struct mdoc *mdoc)
                    299: {
                    300:        v_post          *p;
                    301:
                    302:        if (MDOC_VALID & mdoc->last->flags)
                    303:                return(1);
                    304:        mdoc->last->flags |= MDOC_VALID;
                    305:
                    306:        if (MDOC_TEXT == mdoc->last->type)
                    307:                return(1);
                    308:        if (MDOC_ROOT == mdoc->last->type)
                    309:                return(post_root(mdoc));
                    310:
                    311:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    312:                return(1);
                    313:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    314:                if ( ! (*p)(mdoc))
                    315:                        return(0);
                    316:
                    317:        return(1);
                    318: }
                    319:
                    320:
                    321: static inline int
                    322: warn_count(struct mdoc *m, const char *k,
                    323:                int want, const char *v, int has)
                    324: {
                    325:
1.55      schwarze  326:        return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
                    327:                                m->last->line, m->last->pos,
                    328:                                "%s %s %d (have %d)", v, k, want, has));
1.1       kristaps  329: }
                    330:
                    331:
                    332: static inline int
                    333: err_count(struct mdoc *m, const char *k,
                    334:                int want, const char *v, int has)
                    335: {
                    336:
1.55      schwarze  337:        mdoc_vmsg(m, MANDOCERR_SYNTARGCOUNT,
                    338:                        m->last->line, m->last->pos,
                    339:                        "%s %s %d (have %d)",
                    340:                        v, k, want, has);
                    341:        return(0);
1.1       kristaps  342: }
                    343:
                    344:
                    345: /*
                    346:  * Build these up with macros because they're basically the same check
                    347:  * for different inequalities.  Yes, this could be done with functions,
                    348:  * but this is reasonable for now.
                    349:  */
                    350:
                    351: #define CHECK_CHILD_DEFN(lvl, name, ineq)                      \
                    352: static int                                                     \
                    353: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)   \
                    354: {                                                              \
1.12      schwarze  355:        if (mdoc->last->nchild ineq sz)                         \
1.1       kristaps  356:                return(1);                                      \
1.12      schwarze  357:        return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
1.1       kristaps  358: }
                    359:
                    360: #define CHECK_BODY_DEFN(name, lvl, func, num)                  \
                    361: static int                                                     \
                    362: b##lvl##_##name(POST_ARGS)                                     \
                    363: {                                                              \
                    364:        if (MDOC_BODY != mdoc->last->type)                      \
                    365:                return(1);                                      \
                    366:        return(func(mdoc, "multi-line arguments", (num)));      \
                    367: }
                    368:
                    369: #define CHECK_ELEM_DEFN(name, lvl, func, num)                  \
                    370: static int                                                     \
                    371: e##lvl##_##name(POST_ARGS)                                     \
                    372: {                                                              \
                    373:        assert(MDOC_ELEM == mdoc->last->type);                  \
                    374:        return(func(mdoc, "line arguments", (num)));            \
                    375: }
                    376:
                    377: #define CHECK_HEAD_DEFN(name, lvl, func, num)                  \
                    378: static int                                                     \
                    379: h##lvl##_##name(POST_ARGS)                                     \
                    380: {                                                              \
                    381:        if (MDOC_HEAD != mdoc->last->type)                      \
                    382:                return(1);                                      \
                    383:        return(func(mdoc, "line arguments", (num)));            \
                    384: }
                    385:
                    386:
                    387: CHECK_CHILD_DEFN(warn, gt, >)                  /* warn_child_gt() */
                    388: CHECK_CHILD_DEFN(err, gt, >)                   /* err_child_gt() */
                    389: CHECK_CHILD_DEFN(warn, eq, ==)                 /* warn_child_eq() */
                    390: CHECK_CHILD_DEFN(err, eq, ==)                  /* err_child_eq() */
                    391: CHECK_CHILD_DEFN(err, lt, <)                   /* err_child_lt() */
                    392: CHECK_CHILD_DEFN(warn, lt, <)                  /* warn_child_lt() */
                    393: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)   /* bwarn_ge1() */
1.25      schwarze  394: CHECK_BODY_DEFN(ge1, err, err_child_gt, 0)     /* berr_ge1() */
1.69      schwarze  395: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0)   /* ewarn_eq0() */
1.59      schwarze  396: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)   /* ewarn_ge1() */
1.1       kristaps  397: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)     /* eerr_eq1() */
1.37      schwarze  398: CHECK_ELEM_DEFN(le1, err, err_child_lt, 2)     /* eerr_le1() */
1.1       kristaps  399: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)     /* eerr_ge1() */
                    400: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)     /* herr_eq0() */
                    401: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)   /* hwarn_le1() */
                    402: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)     /* herr_ge1() */
                    403: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)   /* hwarn_eq1() */
1.48      schwarze  404: CHECK_HEAD_DEFN(eq0, warn, warn_child_eq, 0)   /* hwarn_eq0() */
1.1       kristaps  405:
                    406:
                    407: static int
                    408: check_stdarg(PRE_ARGS)
                    409: {
                    410:
                    411:        if (n->args && 1 == n->args->argc)
                    412:                if (MDOC_Std == n->args->argv[0].arg)
                    413:                        return(1);
1.55      schwarze  414:        return(mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV));
1.1       kristaps  415: }
                    416:
                    417:
                    418: static int
1.61      schwarze  419: check_args(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  420: {
                    421:        int              i;
                    422:
                    423:        if (NULL == n->args)
                    424:                return(1);
                    425:
                    426:        assert(n->args->argc);
                    427:        for (i = 0; i < (int)n->args->argc; i++)
                    428:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    429:                        return(0);
                    430:
                    431:        return(1);
                    432: }
                    433:
                    434:
                    435: static int
1.61      schwarze  436: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1       kristaps  437: {
                    438:        int              i;
                    439:
                    440:        for (i = 0; i < (int)v->sz; i++)
                    441:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    442:                        return(0);
                    443:
                    444:        if (MDOC_Std == v->arg) {
                    445:                if (v->sz || m->meta.name)
                    446:                        return(1);
1.55      schwarze  447:                if ( ! mdoc_nmsg(m, n, MANDOCERR_NONAME))
                    448:                        return(0);
1.1       kristaps  449:        }
                    450:
                    451:        return(1);
                    452: }
                    453:
                    454:
                    455: static int
1.66      schwarze  456: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1       kristaps  457: {
1.15      schwarze  458:        int              c;
1.66      schwarze  459:        size_t           sz;
1.1       kristaps  460:
1.66      schwarze  461:        for ( ; *p; p++, pos++) {
                    462:                sz = strcspn(p, "\t\\");
                    463:                p += (int)sz;
                    464:
                    465:                if ('\0' == *p)
                    466:                        break;
                    467:
                    468:                pos += (int)sz;
1.65      schwarze  469:
1.1       kristaps  470:                if ('\t' == *p) {
1.66      schwarze  471:                        if (MDOC_LITERAL & m->flags)
                    472:                                continue;
                    473:                        if (mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB))
                    474:                                continue;
                    475:                        return(0);
                    476:                }
1.1       kristaps  477:
1.66      schwarze  478:                /* Check the special character. */
1.1       kristaps  479:
1.15      schwarze  480:                c = mandoc_special(p);
1.1       kristaps  481:                if (c) {
1.15      schwarze  482:                        p += c - 1;
                    483:                        pos += c - 1;
1.68      schwarze  484:                } else
                    485:                        mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
1.1       kristaps  486:        }
                    487:
                    488:        return(1);
                    489: }
                    490:
                    491:
                    492: static int
1.44      schwarze  493: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1       kristaps  494: {
                    495:
                    496:        assert(n->parent);
                    497:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    498:                        (t == n->parent->type))
                    499:                return(1);
                    500:
1.55      schwarze  501:        mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
                    502:                                n->line, n->pos, "want parent %s",
                    503:                                MDOC_ROOT == t ? "<root>" :
                    504:                                        mdoc_macronames[tok]);
                    505:        return(0);
1.1       kristaps  506: }
                    507:
                    508:
                    509: static int
                    510: pre_display(PRE_ARGS)
                    511: {
                    512:        struct mdoc_node *node;
                    513:
                    514:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    515:
                    516:        if (MDOC_BLOCK != n->type)
                    517:                return(1);
                    518:
                    519:        /* LINTED */
                    520:        for (node = mdoc->last->parent; node; node = node->parent)
                    521:                if (MDOC_BLOCK == node->type)
                    522:                        if (MDOC_Bd == node->tok)
                    523:                                break;
                    524:        if (NULL == node)
                    525:                return(1);
                    526:
1.55      schwarze  527:        mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
                    528:        return(0);
1.1       kristaps  529: }
                    530:
                    531:
                    532: static int
                    533: pre_bl(PRE_ARGS)
                    534: {
1.65      schwarze  535:        int               i, comp, dup;
                    536:        const char       *offs, *width;
                    537:        enum mdoc_list    lt;
                    538:        struct mdoc_node *np;
1.1       kristaps  539:
1.60      schwarze  540:        if (MDOC_BLOCK != n->type) {
1.65      schwarze  541:                if (ENDBODY_NOT != n->end) {
                    542:                        assert(n->pending);
                    543:                        np = n->pending->parent;
                    544:                } else
                    545:                        np = n->parent;
                    546:
                    547:                assert(np);
                    548:                assert(MDOC_BLOCK == np->type);
                    549:                assert(MDOC_Bl == np->tok);
                    550:                assert(np->data.Bl);
                    551:                n->data.Bl = np->data.Bl;
1.1       kristaps  552:                return(1);
1.55      schwarze  553:        }
1.1       kristaps  554:
1.60      schwarze  555:        /*
                    556:         * First figure out which kind of list to use: bind ourselves to
                    557:         * the first mentioned list type and warn about any remaining
                    558:         * ones.  If we find no list type, we default to LIST_item.
                    559:         */
1.1       kristaps  560:
1.65      schwarze  561:        assert(NULL == n->data.Bl);
                    562:        n->data.Bl = mandoc_calloc(1, sizeof(struct mdoc_bl));
1.1       kristaps  563:
                    564:        /* LINTED */
1.60      schwarze  565:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    566:                lt = LIST__NONE;
1.61      schwarze  567:                dup = comp = 0;
                    568:                width = offs = NULL;
1.60      schwarze  569:                switch (n->args->argv[i].arg) {
                    570:                /* Set list types. */
1.1       kristaps  571:                case (MDOC_Bullet):
1.60      schwarze  572:                        lt = LIST_bullet;
                    573:                        break;
1.1       kristaps  574:                case (MDOC_Dash):
1.60      schwarze  575:                        lt = LIST_dash;
                    576:                        break;
1.1       kristaps  577:                case (MDOC_Enum):
1.60      schwarze  578:                        lt = LIST_enum;
                    579:                        break;
1.1       kristaps  580:                case (MDOC_Hyphen):
1.60      schwarze  581:                        lt = LIST_hyphen;
                    582:                        break;
1.1       kristaps  583:                case (MDOC_Item):
1.60      schwarze  584:                        lt = LIST_item;
                    585:                        break;
1.1       kristaps  586:                case (MDOC_Tag):
1.60      schwarze  587:                        lt = LIST_tag;
                    588:                        break;
1.1       kristaps  589:                case (MDOC_Diag):
1.60      schwarze  590:                        lt = LIST_diag;
                    591:                        break;
1.1       kristaps  592:                case (MDOC_Hang):
1.60      schwarze  593:                        lt = LIST_hang;
                    594:                        break;
1.1       kristaps  595:                case (MDOC_Ohang):
1.60      schwarze  596:                        lt = LIST_ohang;
                    597:                        break;
1.1       kristaps  598:                case (MDOC_Inset):
1.60      schwarze  599:                        lt = LIST_inset;
                    600:                        break;
1.1       kristaps  601:                case (MDOC_Column):
1.60      schwarze  602:                        lt = LIST_column;
                    603:                        break;
                    604:                /* Set list arguments. */
1.37      schwarze  605:                case (MDOC_Compact):
1.65      schwarze  606:                        dup = n->data.Bl->comp;
1.61      schwarze  607:                        comp = 1;
1.60      schwarze  608:                        break;
1.1       kristaps  609:                case (MDOC_Width):
1.65      schwarze  610:                        dup = (NULL != n->data.Bl->width);
1.61      schwarze  611:                        width = n->args->argv[i].value[0];
1.5       schwarze  612:                        break;
1.1       kristaps  613:                case (MDOC_Offset):
1.61      schwarze  614:                        /* NB: this can be empty! */
                    615:                        if (n->args->argv[i].sz) {
                    616:                                offs = n->args->argv[i].value[0];
1.65      schwarze  617:                                dup = (NULL != n->data.Bl->offs);
1.61      schwarze  618:                                break;
                    619:                        }
                    620:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
                    621:                                return(0);
1.1       kristaps  622:                        break;
1.66      schwarze  623:                default:
                    624:                        continue;
1.1       kristaps  625:                }
                    626:
1.60      schwarze  627:                /* Check: duplicate auxiliary arguments. */
                    628:
1.61      schwarze  629:                if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
                    630:                        return(0);
                    631:
                    632:                if (comp && ! dup)
1.65      schwarze  633:                        n->data.Bl->comp = comp;
1.61      schwarze  634:                if (offs && ! dup)
1.65      schwarze  635:                        n->data.Bl->offs = offs;
1.61      schwarze  636:                if (width && ! dup)
1.65      schwarze  637:                        n->data.Bl->width = width;
1.60      schwarze  638:
                    639:                /* Check: multiple list types. */
                    640:
1.65      schwarze  641:                if (LIST__NONE != lt && n->data.Bl->type != LIST__NONE)
1.60      schwarze  642:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP))
                    643:                                return(0);
                    644:
                    645:                /* Assign list type. */
                    646:
1.65      schwarze  647:                if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
                    648:                        n->data.Bl->type = lt;
                    649:                        /* Set column information, too. */
                    650:                        if (LIST_column == lt) {
                    651:                                n->data.Bl->ncols =
                    652:                                        n->args->argv[i].sz;
                    653:                                n->data.Bl->cols = (const char **)
                    654:                                        n->args->argv[i].value;
                    655:                        }
                    656:                }
1.60      schwarze  657:
                    658:                /* The list type should come first. */
                    659:
1.65      schwarze  660:                if (n->data.Bl->type == LIST__NONE)
                    661:                        if (n->data.Bl->width ||
                    662:                                        n->data.Bl->offs ||
                    663:                                        n->data.Bl->comp)
1.60      schwarze  664:                                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
                    665:                                        return(0);
                    666:
                    667:                continue;
                    668:        }
                    669:
                    670:        /* Allow lists to default to LIST_item. */
                    671:
1.65      schwarze  672:        if (LIST__NONE == n->data.Bl->type) {
1.60      schwarze  673:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE))
                    674:                        return(0);
1.65      schwarze  675:                n->data.Bl->type = LIST_item;
1.55      schwarze  676:        }
1.1       kristaps  677:
1.5       schwarze  678:        /*
                    679:         * Validate the width field.  Some list types don't need width
                    680:         * types and should be warned about them.  Others should have it
                    681:         * and must also be warned.
                    682:         */
                    683:
1.65      schwarze  684:        switch (n->data.Bl->type) {
1.60      schwarze  685:        case (LIST_tag):
1.65      schwarze  686:                if (n->data.Bl->width)
1.60      schwarze  687:                        break;
                    688:                if (mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG))
                    689:                        break;
                    690:                return(0);
                    691:        case (LIST_column):
1.1       kristaps  692:                /* FALLTHROUGH */
1.60      schwarze  693:        case (LIST_diag):
1.1       kristaps  694:                /* FALLTHROUGH */
1.60      schwarze  695:        case (LIST_ohang):
1.40      schwarze  696:                /* FALLTHROUGH */
1.60      schwarze  697:        case (LIST_inset):
1.1       kristaps  698:                /* FALLTHROUGH */
1.60      schwarze  699:        case (LIST_item):
1.65      schwarze  700:                if (NULL == n->data.Bl->width)
1.60      schwarze  701:                        break;
                    702:                if (mdoc_nmsg(mdoc, n, MANDOCERR_WIDTHARG))
                    703:                        break;
                    704:                return(0);
1.5       schwarze  705:        default:
                    706:                break;
                    707:        }
                    708:
1.1       kristaps  709:        return(1);
                    710: }
                    711:
                    712:
                    713: static int
                    714: pre_bd(PRE_ARGS)
                    715: {
1.65      schwarze  716:        int               i, dup, comp;
                    717:        enum mdoc_disp    dt;
                    718:        const char       *offs;
                    719:        struct mdoc_node *np;
1.1       kristaps  720:
1.61      schwarze  721:        if (MDOC_BLOCK != n->type) {
1.65      schwarze  722:                if (ENDBODY_NOT != n->end) {
                    723:                        assert(n->pending);
                    724:                        np = n->pending->parent;
                    725:                } else
                    726:                        np = n->parent;
                    727:
                    728:                assert(np);
                    729:                assert(MDOC_BLOCK == np->type);
                    730:                assert(MDOC_Bd == np->tok);
                    731:                assert(np->data.Bd);
                    732:                n->data.Bd = np->data.Bd;
1.1       kristaps  733:                return(1);
1.55      schwarze  734:        }
1.1       kristaps  735:
1.65      schwarze  736:        assert(NULL == n->data.Bd);
                    737:        n->data.Bd = mandoc_calloc(1, sizeof(struct mdoc_bd));
1.1       kristaps  738:
                    739:        /* LINTED */
1.61      schwarze  740:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    741:                dt = DISP__NONE;
                    742:                dup = comp = 0;
                    743:                offs = NULL;
                    744:
1.1       kristaps  745:                switch (n->args->argv[i].arg) {
1.38      schwarze  746:                case (MDOC_Centred):
1.61      schwarze  747:                        dt = DISP_centred;
                    748:                        break;
1.1       kristaps  749:                case (MDOC_Ragged):
1.61      schwarze  750:                        dt = DISP_ragged;
                    751:                        break;
1.1       kristaps  752:                case (MDOC_Unfilled):
1.61      schwarze  753:                        dt = DISP_unfilled;
                    754:                        break;
1.1       kristaps  755:                case (MDOC_Filled):
1.61      schwarze  756:                        dt = DISP_filled;
                    757:                        break;
1.1       kristaps  758:                case (MDOC_Literal):
1.61      schwarze  759:                        dt = DISP_literal;
                    760:                        break;
                    761:                case (MDOC_File):
                    762:                        mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
                    763:                        return(0);
                    764:                case (MDOC_Offset):
                    765:                        /* NB: this can be empty! */
                    766:                        if (n->args->argv[i].sz) {
                    767:                                offs = n->args->argv[i].value[0];
1.65      schwarze  768:                                dup = (NULL != n->data.Bd->offs);
1.1       kristaps  769:                                break;
1.61      schwarze  770:                        }
                    771:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
1.55      schwarze  772:                                return(0);
                    773:                        break;
1.61      schwarze  774:                case (MDOC_Compact):
                    775:                        comp = 1;
1.65      schwarze  776:                        dup = n->data.Bd->comp;
1.61      schwarze  777:                        break;
1.1       kristaps  778:                default:
1.61      schwarze  779:                        abort();
                    780:                        /* NOTREACHED */
1.1       kristaps  781:                }
                    782:
1.61      schwarze  783:                /* Check whether we have duplicates. */
                    784:
                    785:                if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
                    786:                        return(0);
                    787:
                    788:                /* Make our auxiliary assignments. */
                    789:
                    790:                if (offs && ! dup)
1.65      schwarze  791:                        n->data.Bd->offs = offs;
1.61      schwarze  792:                if (comp && ! dup)
1.65      schwarze  793:                        n->data.Bd->comp = comp;
1.61      schwarze  794:
                    795:                /* Check whether a type has already been assigned. */
                    796:
1.65      schwarze  797:                if (DISP__NONE != dt && n->data.Bd->type != DISP__NONE)
1.61      schwarze  798:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP))
                    799:                                return(0);
                    800:
                    801:                /* Make our type assignment. */
                    802:
1.65      schwarze  803:                if (DISP__NONE != dt && n->data.Bd->type == DISP__NONE)
                    804:                        n->data.Bd->type = dt;
1.61      schwarze  805:        }
                    806:
1.65      schwarze  807:        if (DISP__NONE == n->data.Bd->type) {
1.61      schwarze  808:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE))
                    809:                        return(0);
1.65      schwarze  810:                n->data.Bd->type = DISP_ragged;
1.61      schwarze  811:        }
                    812:
                    813:        return(1);
1.1       kristaps  814: }
                    815:
                    816:
                    817: static int
                    818: pre_ss(PRE_ARGS)
                    819: {
                    820:
                    821:        if (MDOC_BLOCK != n->type)
                    822:                return(1);
                    823:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    824: }
                    825:
                    826:
                    827: static int
                    828: pre_sh(PRE_ARGS)
                    829: {
                    830:
                    831:        if (MDOC_BLOCK != n->type)
                    832:                return(1);
1.63      schwarze  833:
                    834:        mdoc->regs->regs[(int)REG_nS].set = 0;
1.50      schwarze  835:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  836: }
                    837:
                    838:
                    839: static int
                    840: pre_it(PRE_ARGS)
                    841: {
                    842:
                    843:        if (MDOC_BLOCK != n->type)
                    844:                return(1);
1.55      schwarze  845:        /*
                    846:         * FIXME: this can probably be lifted if we make the It into
                    847:         * something else on-the-fly?
                    848:         */
1.1       kristaps  849:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    850: }
                    851:
                    852:
                    853: static int
                    854: pre_an(PRE_ARGS)
                    855: {
                    856:
1.65      schwarze  857:        if (NULL == n->args)
1.1       kristaps  858:                return(1);
1.65      schwarze  859:        if (n->args->argc > 1)
                    860:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGCOUNT))
                    861:                        return(0);
                    862:
                    863:        if (MDOC_Split == n->args->argv[0].arg)
                    864:                n->data.An.auth = AUTH_split;
                    865:        else if (MDOC_Nosplit == n->args->argv[0].arg)
                    866:                n->data.An.auth = AUTH_nosplit;
                    867:        else
                    868:                abort();
                    869:
                    870:        return(1);
1.1       kristaps  871: }
                    872:
                    873:
                    874: static int
                    875: pre_rv(PRE_ARGS)
                    876: {
                    877:
                    878:        return(check_stdarg(mdoc, n));
                    879: }
                    880:
                    881:
                    882: static int
1.60      schwarze  883: post_dt(POST_ARGS)
                    884: {
                    885:        const struct mdoc_node *nn;
                    886:        const char      *p;
                    887:
                    888:        if (NULL != (nn = mdoc->last->child))
                    889:                for (p = nn->string; *p; p++) {
                    890:                        if (toupper((u_char)*p) == *p)
                    891:                                continue;
                    892:                        if ( ! mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE))
                    893:                                return(0);
                    894:                        break;
                    895:                }
                    896:
                    897:        return(1);
                    898: }
                    899:
                    900:
                    901: static int
1.1       kristaps  902: pre_dt(PRE_ARGS)
                    903: {
1.40      schwarze  904:
1.1       kristaps  905:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.55      schwarze  906:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  907:                        return(0);
                    908:        if (mdoc->meta.title)
1.55      schwarze  909:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  910:                        return(0);
                    911:        return(1);
                    912: }
                    913:
                    914:
                    915: static int
                    916: pre_os(PRE_ARGS)
                    917: {
                    918:
                    919:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.55      schwarze  920:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  921:                        return(0);
                    922:        if (mdoc->meta.os)
1.55      schwarze  923:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  924:                        return(0);
                    925:        return(1);
                    926: }
                    927:
                    928:
                    929: static int
                    930: pre_dd(PRE_ARGS)
                    931: {
                    932:
                    933:        if (mdoc->meta.title || mdoc->meta.os)
1.55      schwarze  934:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  935:                        return(0);
                    936:        if (mdoc->meta.date)
1.55      schwarze  937:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  938:                        return(0);
                    939:        return(1);
                    940: }
                    941:
                    942:
                    943: static int
                    944: post_bf(POST_ARGS)
                    945: {
1.65      schwarze  946:        struct mdoc_node *np;
1.66      schwarze  947:        enum mdocargt     arg;
1.65      schwarze  948:
                    949:        /*
                    950:         * Unlike other data pointers, these are "housed" by the HEAD
                    951:         * element, which contains the goods.
                    952:         */
1.1       kristaps  953:
1.65      schwarze  954:        if (MDOC_HEAD != mdoc->last->type) {
                    955:                if (ENDBODY_NOT != mdoc->last->end) {
                    956:                        assert(mdoc->last->pending);
                    957:                        np = mdoc->last->pending->parent->head;
                    958:                } else if (MDOC_BLOCK != mdoc->last->type) {
                    959:                        np = mdoc->last->parent->head;
                    960:                } else
                    961:                        np = mdoc->last->head;
                    962:
                    963:                assert(np);
                    964:                assert(MDOC_HEAD == np->type);
                    965:                assert(MDOC_Bf == np->tok);
                    966:                assert(np->data.Bf);
                    967:                mdoc->last->data.Bf = np->data.Bf;
1.1       kristaps  968:                return(1);
1.65      schwarze  969:        }
                    970:
                    971:        np = mdoc->last;
                    972:        assert(MDOC_BLOCK == np->parent->type);
                    973:        assert(MDOC_Bf == np->parent->tok);
                    974:        np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
1.1       kristaps  975:
1.65      schwarze  976:        /*
                    977:         * Cannot have both argument and parameter.
                    978:         * If neither is specified, let it through with a warning.
                    979:         */
1.1       kristaps  980:
1.65      schwarze  981:        if (np->parent->args && np->child) {
                    982:                mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.55      schwarze  983:                return(0);
1.65      schwarze  984:        } else if (NULL == np->parent->args && NULL == np->child)
                    985:                return(mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE));
                    986:
                    987:        /* Extract argument into data. */
                    988:
                    989:        if (np->parent->args) {
                    990:                arg = np->parent->args->argv[0].arg;
                    991:                if (MDOC_Emphasis == arg)
                    992:                        np->data.Bf->font = FONT_Em;
                    993:                else if (MDOC_Literal == arg)
                    994:                        np->data.Bf->font = FONT_Li;
                    995:                else if (MDOC_Symbolic == arg)
                    996:                        np->data.Bf->font = FONT_Sy;
                    997:                else
                    998:                        abort();
1.10      schwarze  999:                return(1);
1.55      schwarze 1000:        }
1.1       kristaps 1001:
1.65      schwarze 1002:        /* Extract parameter into data. */
1.1       kristaps 1003:
1.65      schwarze 1004:        if (0 == strcmp(np->child->string, "Em"))
                   1005:                np->data.Bf->font = FONT_Em;
                   1006:        else if (0 == strcmp(np->child->string, "Li"))
                   1007:                np->data.Bf->font = FONT_Li;
                   1008:        else if (0 == strcmp(np->child->string, "Sy"))
                   1009:                np->data.Bf->font = FONT_Sy;
                   1010:        else if ( ! mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE))
                   1011:                return(0);
1.1       kristaps 1012:
1.65      schwarze 1013:        return(1);
1.27      schwarze 1014: }
                   1015:
                   1016:
                   1017: static int
                   1018: post_lb(POST_ARGS)
                   1019: {
                   1020:
                   1021:        if (mdoc_a2lib(mdoc->last->child->string))
                   1022:                return(1);
1.55      schwarze 1023:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADLIB));
1.59      schwarze 1024: }
                   1025:
                   1026:
                   1027: static int
                   1028: post_eoln(POST_ARGS)
                   1029: {
                   1030:
                   1031:        if (NULL == mdoc->last->child)
                   1032:                return(1);
                   1033:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST));
1.42      schwarze 1034: }
                   1035:
                   1036:
                   1037: static int
                   1038: post_vt(POST_ARGS)
                   1039: {
                   1040:        const struct mdoc_node *n;
                   1041:
                   1042:        /*
                   1043:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                   1044:         * have different syntaxes (yet more context-sensitive
                   1045:         * behaviour).  ELEM types must have a child; BLOCK types,
                   1046:         * specifically the BODY, should only have TEXT children.
                   1047:         */
                   1048:
                   1049:        if (MDOC_ELEM == mdoc->last->type)
                   1050:                return(eerr_ge1(mdoc));
                   1051:        if (MDOC_BODY != mdoc->last->type)
                   1052:                return(1);
                   1053:
                   1054:        for (n = mdoc->last->child; n; n = n->next)
1.52      schwarze 1055:                if (MDOC_TEXT != n->type)
1.55      schwarze 1056:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_CHILD))
1.42      schwarze 1057:                                return(0);
                   1058:
                   1059:        return(1);
1.1       kristaps 1060: }
                   1061:
                   1062:
                   1063: static int
                   1064: post_nm(POST_ARGS)
                   1065: {
                   1066:
                   1067:        if (mdoc->last->child)
                   1068:                return(1);
                   1069:        if (mdoc->meta.name)
                   1070:                return(1);
1.55      schwarze 1071:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME));
1.1       kristaps 1072: }
                   1073:
                   1074:
                   1075: static int
                   1076: post_at(POST_ARGS)
                   1077: {
                   1078:
                   1079:        if (NULL == mdoc->last->child)
                   1080:                return(1);
1.55      schwarze 1081:        assert(MDOC_TEXT == mdoc->last->child->type);
1.1       kristaps 1082:        if (mdoc_a2att(mdoc->last->child->string))
                   1083:                return(1);
1.55      schwarze 1084:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT));
1.1       kristaps 1085: }
                   1086:
                   1087:
                   1088: static int
                   1089: post_an(POST_ARGS)
                   1090: {
1.65      schwarze 1091:        struct mdoc_node *np;
1.1       kristaps 1092:
1.65      schwarze 1093:        np = mdoc->last;
                   1094:        if (AUTH__NONE != np->data.An.auth && np->child)
                   1095:                return(mdoc_nmsg(mdoc, np, MANDOCERR_ARGCOUNT));
                   1096:        if (AUTH__NONE != np->data.An.auth || np->child)
1.1       kristaps 1097:                return(1);
1.65      schwarze 1098:        return(mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS));
1.1       kristaps 1099: }
                   1100:
                   1101:
                   1102: static int
                   1103: post_it(POST_ARGS)
                   1104: {
1.60      schwarze 1105:        int               i, cols, rc;
                   1106:        enum mdoc_list    lt;
1.1       kristaps 1107:        struct mdoc_node *n, *c;
1.60      schwarze 1108:        enum mandocerr    er;
1.1       kristaps 1109:
                   1110:        if (MDOC_BLOCK != mdoc->last->type)
                   1111:                return(1);
                   1112:
                   1113:        n = mdoc->last->parent->parent;
1.65      schwarze 1114:        assert(n->data.Bl);
                   1115:        lt = n->data.Bl->type;
1.60      schwarze 1116:
                   1117:        if (LIST__NONE == lt) {
1.55      schwarze 1118:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
                   1119:                return(0);
                   1120:        }
1.1       kristaps 1121:
1.60      schwarze 1122:        switch (lt) {
                   1123:        case (LIST_tag):
                   1124:                if (mdoc->last->head->child)
1.1       kristaps 1125:                        break;
1.60      schwarze 1126:                /* FIXME: give this a dummy value. */
                   1127:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
                   1128:                        return(0);
1.1       kristaps 1129:                break;
1.60      schwarze 1130:        case (LIST_hang):
1.1       kristaps 1131:                /* FALLTHROUGH */
1.60      schwarze 1132:        case (LIST_ohang):
1.1       kristaps 1133:                /* FALLTHROUGH */
1.60      schwarze 1134:        case (LIST_inset):
1.1       kristaps 1135:                /* FALLTHROUGH */
1.60      schwarze 1136:        case (LIST_diag):
1.1       kristaps 1137:                if (NULL == mdoc->last->head->child)
1.55      schwarze 1138:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
1.1       kristaps 1139:                                return(0);
                   1140:                break;
1.60      schwarze 1141:        case (LIST_bullet):
1.1       kristaps 1142:                /* FALLTHROUGH */
1.60      schwarze 1143:        case (LIST_dash):
1.1       kristaps 1144:                /* FALLTHROUGH */
1.60      schwarze 1145:        case (LIST_enum):
1.1       kristaps 1146:                /* FALLTHROUGH */
1.60      schwarze 1147:        case (LIST_hyphen):
1.64      schwarze 1148:                if (NULL == mdoc->last->body->child)
                   1149:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
                   1150:                                return(0);
1.1       kristaps 1151:                /* FALLTHROUGH */
1.60      schwarze 1152:        case (LIST_item):
1.1       kristaps 1153:                if (mdoc->last->head->child)
1.55      schwarze 1154:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST))
1.1       kristaps 1155:                                return(0);
                   1156:                break;
1.60      schwarze 1157:        case (LIST_column):
1.65      schwarze 1158:                cols = (int)n->data.Bl->ncols;
1.60      schwarze 1159:
                   1160:                assert(NULL == mdoc->last->head->child);
                   1161:
                   1162:                if (NULL == mdoc->last->body->child)
                   1163:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
1.32      schwarze 1164:                                return(0);
1.60      schwarze 1165:
                   1166:                for (i = 0, c = mdoc->last->child; c; c = c->next)
                   1167:                        if (MDOC_BODY == c->type)
                   1168:                                i++;
                   1169:
                   1170:                if (i < cols)
                   1171:                        er = MANDOCERR_ARGCOUNT;
                   1172:                else if (i == cols || i == cols + 1)
1.1       kristaps 1173:                        break;
1.60      schwarze 1174:                else
                   1175:                        er = MANDOCERR_SYNTARGCOUNT;
1.32      schwarze 1176:
1.60      schwarze 1177:                rc = mdoc_vmsg(mdoc, er,
1.55      schwarze 1178:                                mdoc->last->line, mdoc->last->pos,
                   1179:                                "columns == %d (have %d)", cols, i);
1.60      schwarze 1180:                return(rc);
1.1       kristaps 1181:        default:
                   1182:                break;
                   1183:        }
                   1184:
                   1185:        return(1);
                   1186: }
                   1187:
                   1188:
                   1189: static int
1.11      schwarze 1190: post_bl_head(POST_ARGS)
                   1191: {
1.60      schwarze 1192:        int               i;
                   1193:        struct mdoc_node *n;
1.11      schwarze 1194:
1.60      schwarze 1195:        assert(mdoc->last->parent);
1.11      schwarze 1196:        n = mdoc->last->parent;
                   1197:
1.65      schwarze 1198:        if (LIST_column == n->data.Bl->type) {
                   1199:                if (n->data.Bl->ncols && mdoc->last->nchild) {
1.60      schwarze 1200:                        mdoc_nmsg(mdoc, n, MANDOCERR_COLUMNS);
                   1201:                        return(0);
1.48      schwarze 1202:                }
1.60      schwarze 1203:                return(1);
1.48      schwarze 1204:        }
1.11      schwarze 1205:
1.48      schwarze 1206:        if (0 == (i = mdoc->last->nchild))
1.11      schwarze 1207:                return(1);
1.48      schwarze 1208:        return(warn_count(mdoc, "==", 0, "line arguments", i));
1.11      schwarze 1209: }
                   1210:
                   1211:
                   1212: static int
1.1       kristaps 1213: post_bl(POST_ARGS)
                   1214: {
                   1215:        struct mdoc_node        *n;
                   1216:
1.11      schwarze 1217:        if (MDOC_HEAD == mdoc->last->type)
                   1218:                return(post_bl_head(mdoc));
1.1       kristaps 1219:        if (MDOC_BODY != mdoc->last->type)
                   1220:                return(1);
                   1221:        if (NULL == mdoc->last->child)
                   1222:                return(1);
                   1223:
1.41      schwarze 1224:        /*
                   1225:         * We only allow certain children of `Bl'.  This is usually on
                   1226:         * `It', but apparently `Sm' occurs here and there, so we let
                   1227:         * that one through, too.
                   1228:         */
                   1229:
1.1       kristaps 1230:        /* LINTED */
                   1231:        for (n = mdoc->last->child; n; n = n->next) {
1.41      schwarze 1232:                if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
                   1233:                        continue;
                   1234:                if (MDOC_Sm == n->tok)
                   1235:                        continue;
1.55      schwarze 1236:                mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
                   1237:                return(0);
1.1       kristaps 1238:        }
                   1239:
                   1240:        return(1);
                   1241: }
                   1242:
                   1243:
                   1244: static int
                   1245: ebool(struct mdoc *mdoc)
                   1246: {
                   1247:        struct mdoc_node *n;
                   1248:
                   1249:        /* LINTED */
                   1250:        for (n = mdoc->last->child; n; n = n->next) {
                   1251:                if (MDOC_TEXT != n->type)
                   1252:                        break;
                   1253:                if (0 == strcmp(n->string, "on"))
                   1254:                        continue;
                   1255:                if (0 == strcmp(n->string, "off"))
                   1256:                        continue;
                   1257:                break;
                   1258:        }
                   1259:
                   1260:        if (NULL == n)
                   1261:                return(1);
1.55      schwarze 1262:        return(mdoc_nmsg(mdoc, n, MANDOCERR_BADBOOL));
1.1       kristaps 1263: }
                   1264:
                   1265:
                   1266: static int
                   1267: post_root(POST_ARGS)
                   1268: {
                   1269:
                   1270:        if (NULL == mdoc->first->child)
1.55      schwarze 1271:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1272:        else if ( ! (MDOC_PBODY & mdoc->flags))
                   1273:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
                   1274:        else if (MDOC_BLOCK != mdoc->first->child->type)
                   1275:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1276:        else if (MDOC_Sh != mdoc->first->child->tok)
                   1277:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1278:        else
                   1279:                return(1);
1.1       kristaps 1280:
1.55      schwarze 1281:        return(0);
1.1       kristaps 1282: }
                   1283:
                   1284:
                   1285: static int
                   1286: post_st(POST_ARGS)
                   1287: {
                   1288:
                   1289:        if (mdoc_a2st(mdoc->last->child->string))
                   1290:                return(1);
1.55      schwarze 1291:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD));
1.36      schwarze 1292: }
                   1293:
                   1294:
                   1295: static int
                   1296: post_rs(POST_ARGS)
                   1297: {
                   1298:        struct mdoc_node        *nn;
                   1299:
                   1300:        if (MDOC_BODY != mdoc->last->type)
                   1301:                return(1);
                   1302:
                   1303:        for (nn = mdoc->last->child; nn; nn = nn->next)
                   1304:                switch (nn->tok) {
1.38      schwarze 1305:                case(MDOC__U):
                   1306:                        /* FALLTHROUGH */
1.36      schwarze 1307:                case(MDOC__Q):
                   1308:                        /* FALLTHROUGH */
                   1309:                case(MDOC__C):
                   1310:                        /* FALLTHROUGH */
                   1311:                case(MDOC__A):
                   1312:                        /* FALLTHROUGH */
                   1313:                case(MDOC__B):
                   1314:                        /* FALLTHROUGH */
                   1315:                case(MDOC__D):
                   1316:                        /* FALLTHROUGH */
                   1317:                case(MDOC__I):
                   1318:                        /* FALLTHROUGH */
                   1319:                case(MDOC__J):
                   1320:                        /* FALLTHROUGH */
                   1321:                case(MDOC__N):
                   1322:                        /* FALLTHROUGH */
                   1323:                case(MDOC__O):
                   1324:                        /* FALLTHROUGH */
                   1325:                case(MDOC__P):
                   1326:                        /* FALLTHROUGH */
                   1327:                case(MDOC__R):
                   1328:                        /* FALLTHROUGH */
                   1329:                case(MDOC__T):
                   1330:                        /* FALLTHROUGH */
                   1331:                case(MDOC__V):
                   1332:                        break;
                   1333:                default:
1.55      schwarze 1334:                        mdoc_nmsg(mdoc, nn, MANDOCERR_SYNTCHILD);
                   1335:                        return(0);
1.36      schwarze 1336:                }
                   1337:
                   1338:        return(1);
1.1       kristaps 1339: }
                   1340:
                   1341:
                   1342: static int
                   1343: post_sh(POST_ARGS)
                   1344: {
                   1345:
                   1346:        if (MDOC_HEAD == mdoc->last->type)
                   1347:                return(post_sh_head(mdoc));
                   1348:        if (MDOC_BODY == mdoc->last->type)
                   1349:                return(post_sh_body(mdoc));
                   1350:
                   1351:        return(1);
                   1352: }
                   1353:
                   1354:
                   1355: static int
                   1356: post_sh_body(POST_ARGS)
                   1357: {
                   1358:        struct mdoc_node *n;
                   1359:
1.26      schwarze 1360:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1361:                return(1);
                   1362:
                   1363:        /*
                   1364:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1365:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1366:         * children of the BODY declaration can also be "text".
                   1367:         */
                   1368:
                   1369:        if (NULL == (n = mdoc->last->child))
1.55      schwarze 1370:                return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1       kristaps 1371:
                   1372:        for ( ; n && n->next; n = n->next) {
                   1373:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1374:                        continue;
                   1375:                if (MDOC_TEXT == n->type)
                   1376:                        continue;
1.55      schwarze 1377:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC))
1.1       kristaps 1378:                        return(0);
                   1379:        }
                   1380:
1.34      schwarze 1381:        assert(n);
1.25      schwarze 1382:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1383:                return(1);
1.55      schwarze 1384:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1       kristaps 1385: }
                   1386:
                   1387:
                   1388: static int
                   1389: post_sh_head(POST_ARGS)
                   1390: {
1.57      schwarze 1391:        char                    buf[BUFSIZ];
1.1       kristaps 1392:        enum mdoc_sec           sec;
                   1393:        const struct mdoc_node *n;
                   1394:
                   1395:        /*
                   1396:         * Process a new section.  Sections are either "named" or
                   1397:         * "custom"; custom sections are user-defined, while named ones
                   1398:         * usually follow a conventional order and may only appear in
                   1399:         * certain manual sections.
                   1400:         */
                   1401:
1.55      schwarze 1402:        buf[0] = '\0';
                   1403:
                   1404:        /*
                   1405:         * FIXME: yes, these can use a dynamic buffer, but I don't do so
                   1406:         * in the interests of simplicity.
                   1407:         */
1.1       kristaps 1408:
                   1409:        for (n = mdoc->last->child; n; n = n->next) {
1.9       schwarze 1410:                /* XXX - copied from compact(). */
1.1       kristaps 1411:                assert(MDOC_TEXT == n->type);
1.9       schwarze 1412:
1.57      schwarze 1413:                if (strlcat(buf, n->string, BUFSIZ) >= BUFSIZ) {
1.55      schwarze 1414:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   1415:                        return(0);
                   1416:                }
1.1       kristaps 1417:                if (NULL == n->next)
                   1418:                        continue;
1.57      schwarze 1419:                if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.55      schwarze 1420:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   1421:                        return(0);
                   1422:                }
1.1       kristaps 1423:        }
                   1424:
1.51      schwarze 1425:        sec = mdoc_str2sec(buf);
1.1       kristaps 1426:
1.9       schwarze 1427:        /*
                   1428:         * Check: NAME should always be first, CUSTOM has no roles,
                   1429:         * non-CUSTOM has a conventional order to be followed.
                   1430:         */
1.1       kristaps 1431:
1.51      schwarze 1432:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.55      schwarze 1433:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST))
1.51      schwarze 1434:                        return(0);
                   1435:
1.1       kristaps 1436:        if (SEC_CUSTOM == sec)
                   1437:                return(1);
1.51      schwarze 1438:
1.1       kristaps 1439:        if (sec == mdoc->lastnamed)
1.55      schwarze 1440:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP))
1.23      schwarze 1441:                        return(0);
1.51      schwarze 1442:
1.1       kristaps 1443:        if (sec < mdoc->lastnamed)
1.55      schwarze 1444:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO))
1.23      schwarze 1445:                        return(0);
1.1       kristaps 1446:
1.9       schwarze 1447:        /*
                   1448:         * Check particular section/manual conventions.  LIBRARY can
1.54      schwarze 1449:         * only occur in manual section 2, 3, and 9.
1.9       schwarze 1450:         */
1.1       kristaps 1451:
                   1452:        switch (sec) {
                   1453:        case (SEC_LIBRARY):
1.54      schwarze 1454:                assert(mdoc->meta.msec);
                   1455:                if (*mdoc->meta.msec == '2')
                   1456:                        break;
                   1457:                if (*mdoc->meta.msec == '3')
                   1458:                        break;
                   1459:                if (*mdoc->meta.msec == '9')
1.1       kristaps 1460:                        break;
1.55      schwarze 1461:                return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC));
1.1       kristaps 1462:        default:
                   1463:                break;
                   1464:        }
                   1465:
1.70      schwarze 1466:        return(1);
                   1467: }
                   1468:
                   1469:
                   1470: static int
                   1471: pre_pp(PRE_ARGS)
                   1472: {
                   1473:
                   1474:        if (NULL == mdoc->last)
                   1475:                return(1);
                   1476:
                   1477:        /* Don't allow prior `Lp' or `Pp'. */
                   1478:
                   1479:        if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
                   1480:                return(1);
                   1481:
                   1482:        if (MDOC_Bl == n->tok && n->data.Bl->comp)
                   1483:                return(1);
                   1484:        if (MDOC_Bd == n->tok && n->data.Bd->comp)
                   1485:                return(1);
                   1486:
                   1487:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
                   1488:        mdoc_node_delete(mdoc, mdoc->last);
1.1       kristaps 1489:        return(1);
                   1490: }