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

Annotation of mandoc/mdoc_validate.c, Revision 1.118

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

CVSweb