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

Annotation of mandoc/mdoc_validate.c, Revision 1.108

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

CVSweb