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

Annotation of mandoc/validate.c, Revision 1.86

1.86    ! kristaps    1: /* $Id: validate.c,v 1.85 2009/03/16 23:37:28 kristaps Exp $ */
1.1       kristaps    2: /*
1.85      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.77      kristaps   19: #include <sys/types.h>
                     20:
1.1       kristaps   21: #include <assert.h>
1.56      kristaps   22: #include <ctype.h>
1.69      kristaps   23: #include <stdarg.h>
1.8       kristaps   24: #include <stdlib.h>
1.1       kristaps   25:
                     26: #include "private.h"
                     27:
1.67      kristaps   28: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
1.71      kristaps   29: /* TODO: ignoring Pp (it's superfluous in some invocations). */
1.67      kristaps   30:
1.44      kristaps   31: /*
                     32:  * Pre- and post-validate macros as they're parsed.  Pre-validation
                     33:  * occurs when the macro has been detected and its arguments parsed.
                     34:  * Post-validation occurs when all child macros have also been parsed.
                     35:  * In the ELEMENT case, this is simply the parameters of the macro; in
                     36:  * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
                     37:  */
                     38:
1.55      kristaps   39: #define        PRE_ARGS        struct mdoc *mdoc, const struct mdoc_node *n
                     40: #define        POST_ARGS       struct mdoc *mdoc
                     41:
1.71      kristaps   42: enum   merr {
1.86    ! kristaps   43:        EESCAPE,
1.84      kristaps   44:        EPRINT,
1.71      kristaps   45:        ENODATA,
                     46:        ENOPROLOGUE,
                     47:        ELINE,
                     48:        EATT,
                     49:        ENAME,
                     50:        ELISTTYPE,
                     51:        EDISPTYPE,
                     52:        EMULTIDISP,
                     53:        EMULTILIST,
                     54:        EARGREP,
                     55:        EBOOL,
                     56:        ENESTDISP
                     57: };
                     58:
                     59: enum   mwarn {
1.86    ! kristaps   60:        WESCAPE,
1.71      kristaps   61:        WWRONGMSEC,
                     62:        WSECOOO,
                     63:        WSECREP,
                     64:        WBADSTAND,
                     65:        WNAMESECINC,
                     66:        WNOMULTILINE,
                     67:        WMULTILINE,
                     68:        WLINE,
                     69:        WNOLINE,
                     70:        WPROLOOO,
                     71:        WPROLREP,
                     72:        WARGVAL,
                     73:        WBADSEC,
                     74:        WBADMSEC
                     75: };
                     76:
1.55      kristaps   77: typedef        int     (*v_pre)(PRE_ARGS);
                     78: typedef        int     (*v_post)(POST_ARGS);
1.14      kristaps   79:
                     80: struct valids {
1.24      kristaps   81:        v_pre   *pre;
1.17      kristaps   82:        v_post  *post;
1.14      kristaps   83: };
1.1       kristaps   84:
1.37      kristaps   85: /* Utility checks. */
                     86:
1.84      kristaps   87: static int     pwarn(struct mdoc *, int, int, enum mwarn);
                     88: static int     perr(struct mdoc *, int, int, enum merr);
1.55      kristaps   89: static int     check_parent(PRE_ARGS, int, enum mdoc_type);
1.69      kristaps   90: static int     check_msec(PRE_ARGS, ...);
                     91: static int     check_sec(PRE_ARGS, ...);
1.55      kristaps   92: static int     check_stdarg(PRE_ARGS);
1.84      kristaps   93: static int     check_text(struct mdoc *, int, int, const char *);
1.68      kristaps   94: static int     check_argv(struct mdoc *,
                     95:                        const struct mdoc_node *,
1.71      kristaps   96:                        const struct mdoc_argv *);
                     97: static int     check_args(struct mdoc *,
                     98:                        const struct mdoc_node *);
1.51      kristaps   99: static int     err_child_lt(struct mdoc *, const char *, int);
1.59      kristaps  100: static int     warn_child_lt(struct mdoc *, const char *, int);
1.51      kristaps  101: static int     err_child_gt(struct mdoc *, const char *, int);
                    102: static int     warn_child_gt(struct mdoc *, const char *, int);
                    103: static int     err_child_eq(struct mdoc *, const char *, int);
                    104: static int     warn_child_eq(struct mdoc *, const char *, int);
                    105: static inline int count_child(struct mdoc *);
                    106: static inline int warn_count(struct mdoc *, const char *,
                    107:                        int, const char *, int);
                    108: static inline int err_count(struct mdoc *, const char *,
                    109:                        int, const char *, int);
1.69      kristaps  110: static int     pre_an(PRE_ARGS);
1.55      kristaps  111: static int     pre_bd(PRE_ARGS);
                    112: static int     pre_bl(PRE_ARGS);
                    113: static int     pre_cd(PRE_ARGS);
1.69      kristaps  114: static int     pre_dd(PRE_ARGS);
                    115: static int     pre_display(PRE_ARGS);
                    116: static int     pre_dt(PRE_ARGS);
1.55      kristaps  117: static int     pre_er(PRE_ARGS);
                    118: static int     pre_ex(PRE_ARGS);
1.69      kristaps  119: static int     pre_fd(PRE_ARGS);
                    120: static int     pre_it(PRE_ARGS);
                    121: static int     pre_lb(PRE_ARGS);
                    122: static int     pre_os(PRE_ARGS);
                    123: static int     pre_prologue(PRE_ARGS);
1.55      kristaps  124: static int     pre_rv(PRE_ARGS);
1.69      kristaps  125: static int     pre_sh(PRE_ARGS);
                    126: static int     pre_ss(PRE_ARGS);
1.55      kristaps  127: static int     herr_ge1(POST_ARGS);
1.59      kristaps  128: static int     hwarn_le1(POST_ARGS);
1.55      kristaps  129: static int     herr_eq0(POST_ARGS);
                    130: static int     eerr_eq0(POST_ARGS);
                    131: static int     eerr_le2(POST_ARGS);
                    132: static int     eerr_eq1(POST_ARGS);
                    133: static int     eerr_ge1(POST_ARGS);
                    134: static int     ewarn_eq0(POST_ARGS);
                    135: static int     ewarn_eq1(POST_ARGS);
                    136: static int     bwarn_ge1(POST_ARGS);
1.58      kristaps  137: static int     hwarn_eq1(POST_ARGS);
1.55      kristaps  138: static int     ewarn_ge1(POST_ARGS);
                    139: static int     ebool(POST_ARGS);
1.78      kristaps  140:
1.69      kristaps  141: static int     post_an(POST_ARGS);
1.82      kristaps  142: static int     post_args(POST_ARGS);
1.69      kristaps  143: static int     post_at(POST_ARGS);
                    144: static int     post_bf(POST_ARGS);
1.55      kristaps  145: static int     post_bl(POST_ARGS);
                    146: static int     post_it(POST_ARGS);
                    147: static int     post_nm(POST_ARGS);
                    148: static int     post_root(POST_ARGS);
1.69      kristaps  149: static int     post_sh(POST_ARGS);
                    150: static int     post_sh_body(POST_ARGS);
                    151: static int     post_sh_head(POST_ARGS);
                    152: static int     post_st(POST_ARGS);
1.37      kristaps  153:
1.84      kristaps  154: #define        mwarn(m, t) nwarn((m), (m)->last, (t))
                    155: #define        merr(m, t) nerr((m), (m)->last, (t))
                    156: #define        nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
                    157: #define        nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
                    158:
1.69      kristaps  159: static v_pre   pres_an[] = { pre_an, NULL };
1.24      kristaps  160: static v_pre   pres_bd[] = { pre_display, pre_bd, NULL };
                    161: static v_pre   pres_bl[] = { pre_bl, NULL };
1.33      kristaps  162: static v_pre   pres_cd[] = { pre_cd, NULL };
1.69      kristaps  163: static v_pre   pres_dd[] = { pre_prologue, pre_dd, NULL };
                    164: static v_pre   pres_d1[] = { pre_display, NULL };
                    165: static v_pre   pres_dt[] = { pre_prologue, pre_dt, NULL };
1.33      kristaps  166: static v_pre   pres_er[] = { pre_er, NULL };
                    167: static v_pre   pres_ex[] = { pre_ex, NULL };
1.69      kristaps  168: static v_pre   pres_fd[] = { pre_fd, NULL };
                    169: static v_pre   pres_it[] = { pre_it, NULL };
                    170: static v_pre   pres_lb[] = { pre_lb, NULL };
                    171: static v_pre   pres_os[] = { pre_prologue, pre_os, NULL };
1.36      kristaps  172: static v_pre   pres_rv[] = { pre_rv, NULL };
1.69      kristaps  173: static v_pre   pres_sh[] = { pre_sh, NULL };
                    174: static v_pre   pres_ss[] = { pre_ss, NULL };
1.37      kristaps  175: static v_post  posts_bool[] = { eerr_eq1, ebool, NULL };
                    176: static v_post  posts_bd[] = { herr_eq0, bwarn_ge1, NULL };
                    177: static v_post  posts_text[] = { eerr_ge1, NULL };
                    178: static v_post  posts_wtext[] = { ewarn_ge1, NULL };
                    179: static v_post  posts_notext[] = { eerr_eq0, NULL };
1.43      kristaps  180: static v_post  posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
1.37      kristaps  181: static v_post  posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
                    182: static v_post  posts_bl[] = { herr_eq0, bwarn_ge1, post_bl, NULL };
1.25      kristaps  183: static v_post  posts_it[] = { post_it, NULL };
1.39      kristaps  184: static v_post  posts_in[] = { ewarn_eq1, NULL };
1.37      kristaps  185: static v_post  posts_ss[] = { herr_ge1, NULL };
1.52      kristaps  186: static v_post  posts_pf[] = { eerr_eq1, NULL };
1.69      kristaps  187: static v_post  posts_lb[] = { eerr_eq1, NULL };
                    188: static v_post  posts_st[] = { eerr_eq1, post_st, NULL };
1.37      kristaps  189: static v_post  posts_pp[] = { ewarn_eq0, NULL };
1.82      kristaps  190: static v_post  posts_ex[] = { eerr_eq0, post_args, NULL };
                    191: static v_post  posts_rv[] = { eerr_eq0, post_args, NULL };
1.35      kristaps  192: static v_post  posts_an[] = { post_an, NULL };
1.37      kristaps  193: static v_post  posts_at[] = { post_at, NULL };
1.69      kristaps  194: static v_post  posts_xr[] = { eerr_ge1, eerr_le2, NULL };
1.37      kristaps  195: static v_post  posts_nm[] = { post_nm, NULL };
1.59      kristaps  196: static v_post  posts_bf[] = { hwarn_le1, post_bf, NULL };
1.58      kristaps  197: static v_post  posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
1.9       kristaps  198:
                    199: const  struct valids mdoc_valids[MDOC_MAX] = {
1.51      kristaps  200:        { NULL, NULL },                         /* \" */
1.69      kristaps  201:        { pres_dd, posts_text },                /* Dd */
                    202:        { pres_dt, NULL },                      /* Dt */
                    203:        { pres_os, NULL },                      /* Os */
1.51      kristaps  204:        { pres_sh, posts_sh },                  /* Sh */
                    205:        { pres_ss, posts_ss },                  /* Ss */
                    206:        { NULL, posts_pp },                     /* Pp */
                    207:        { pres_d1, posts_wline },               /* D1 */
                    208:        { pres_d1, posts_wline },               /* Dl */
                    209:        { pres_bd, posts_bd },                  /* Bd */
                    210:        { NULL, NULL },                         /* Ed */
                    211:        { pres_bl, posts_bl },                  /* Bl */
                    212:        { NULL, NULL },                         /* El */
                    213:        { pres_it, posts_it },                  /* It */
                    214:        { NULL, posts_text },                   /* Ad */
                    215:        { pres_an, posts_an },                  /* An */
                    216:        { NULL, NULL },                         /* Ar */
                    217:        { pres_cd, posts_text },                /* Cd */
                    218:        { NULL, NULL },                         /* Cm */
                    219:        { NULL, posts_text },                   /* Dv */
                    220:        { pres_er, posts_text },                /* Er */
                    221:        { NULL, posts_text },                   /* Ev */
                    222:        { pres_ex, posts_ex },                  /* Ex */
                    223:        { NULL, posts_text },                   /* Fa */
1.86    ! kristaps  224:        { pres_fd, posts_wtext },               /* Fd */
1.51      kristaps  225:        { NULL, NULL },                         /* Fl */
                    226:        { NULL, posts_text },                   /* Fn */
                    227:        { NULL, posts_wtext },                  /* Ft */
                    228:        { NULL, posts_text },                   /* Ic */
                    229:        { NULL, posts_in },                     /* In */
                    230:        { NULL, posts_text },                   /* Li */
                    231:        { NULL, posts_wtext },                  /* Nd */
                    232:        { NULL, posts_nm },                     /* Nm */
                    233:        { NULL, posts_wline },                  /* Op */
                    234:        { NULL, NULL },                         /* Ot */
                    235:        { NULL, NULL },                         /* Pa */
1.82      kristaps  236:        { pres_rv, posts_rv },                  /* Rv */
1.69      kristaps  237:        { NULL, posts_st },                     /* St */
1.51      kristaps  238:        { NULL, posts_text },                   /* Va */
                    239:        { NULL, posts_text },                   /* Vt */
                    240:        { NULL, posts_xr },                     /* Xr */
                    241:        { NULL, posts_text },                   /* %A */
                    242:        { NULL, posts_text },                   /* %B */
                    243:        { NULL, posts_text },                   /* %D */
                    244:        { NULL, posts_text },                   /* %I */
                    245:        { NULL, posts_text },                   /* %J */
                    246:        { NULL, posts_text },                   /* %N */
                    247:        { NULL, posts_text },                   /* %O */
                    248:        { NULL, posts_text },                   /* %P */
                    249:        { NULL, posts_text },                   /* %R */
                    250:        { NULL, posts_text },                   /* %T */
                    251:        { NULL, posts_text },                   /* %V */
                    252:        { NULL, NULL },                         /* Ac */
                    253:        { NULL, NULL },                         /* Ao */
                    254:        { NULL, posts_wline },                  /* Aq */
                    255:        { NULL, posts_at },                     /* At */
                    256:        { NULL, NULL },                         /* Bc */
                    257:        { NULL, posts_bf },                     /* Bf */
                    258:        { NULL, NULL },                         /* Bo */
                    259:        { NULL, posts_wline },                  /* Bq */
                    260:        { NULL, NULL },                         /* Bsx */
                    261:        { NULL, NULL },                         /* Bx */
                    262:        { NULL, posts_bool },                   /* Db */
                    263:        { NULL, NULL },                         /* Dc */
                    264:        { NULL, NULL },                         /* Do */
                    265:        { NULL, posts_wline },                  /* Dq */
                    266:        { NULL, NULL },                         /* Ec */
                    267:        { NULL, NULL },                         /* Ef */
                    268:        { NULL, posts_text },                   /* Em */
                    269:        { NULL, NULL },                         /* Eo */
                    270:        { NULL, NULL },                         /* Fx */
                    271:        { NULL, posts_text },                   /* Ms */
                    272:        { NULL, posts_notext },                 /* No */
                    273:        { NULL, posts_notext },                 /* Ns */
                    274:        { NULL, NULL },                         /* Nx */
                    275:        { NULL, NULL },                         /* Ox */
                    276:        { NULL, NULL },                         /* Pc */
1.52      kristaps  277:        { NULL, posts_pf },                     /* Pf */
1.51      kristaps  278:        { NULL, NULL },                         /* Po */
                    279:        { NULL, posts_wline },                  /* Pq */
                    280:        { NULL, NULL },                         /* Qc */
                    281:        { NULL, posts_wline },                  /* Ql */
                    282:        { NULL, NULL },                         /* Qo */
                    283:        { NULL, posts_wline },                  /* Qq */
                    284:        { NULL, NULL },                         /* Re */
1.86    ! kristaps  285:        { NULL, posts_wline },                  /* Rs */
1.51      kristaps  286:        { NULL, NULL },                         /* Sc */
                    287:        { NULL, NULL },                         /* So */
                    288:        { NULL, posts_wline },                  /* Sq */
                    289:        { NULL, posts_bool },                   /* Sm */
                    290:        { NULL, posts_text },                   /* Sx */
                    291:        { NULL, posts_text },                   /* Sy */
                    292:        { NULL, posts_text },                   /* Tn */
                    293:        { NULL, NULL },                         /* Ux */
                    294:        { NULL, NULL },                         /* Xc */
                    295:        { NULL, NULL },                         /* Xo */
                    296:        { NULL, posts_fo },                     /* Fo */
                    297:        { NULL, NULL },                         /* Fc */
                    298:        { NULL, NULL },                         /* Oo */
                    299:        { NULL, NULL },                         /* Oc */
1.86    ! kristaps  300:        { NULL, posts_wline },                  /* Bk */
1.51      kristaps  301:        { NULL, NULL },                         /* Ek */
                    302:        { NULL, posts_notext },                 /* Bt */
                    303:        { NULL, NULL },                         /* Hf */
                    304:        { NULL, NULL },                         /* Fr */
                    305:        { NULL, posts_notext },                 /* Ud */
1.69      kristaps  306:        { pres_lb, posts_lb },                  /* Lb */
1.83      kristaps  307:        { NULL, NULL },                         /* Ap */
                    308:        { NULL, posts_pp },                     /* Lp */
                    309:        { NULL, posts_text },                   /* Lk */
                    310:        { NULL, posts_text },                   /* Mt */
1.79      kristaps  311:        { NULL, posts_wline },                  /* Brq */
                    312:        { NULL, NULL },                         /* Bro */
                    313:        { NULL, NULL },                         /* Brc */
1.82      kristaps  314:        { NULL, posts_text },                   /* %C */
1.84      kristaps  315:        { NULL, NULL },                         /* Es */
                    316:        { NULL, NULL },                         /* En */
1.85      kristaps  317:        { NULL, NULL },                         /* Dx */
1.9       kristaps  318: };
1.6       kristaps  319:
                    320:
1.57      kristaps  321: int
                    322: mdoc_valid_pre(struct mdoc *mdoc,
1.84      kristaps  323:                const struct mdoc_node *n)
1.57      kristaps  324: {
                    325:        v_pre           *p;
1.71      kristaps  326:        int              line, pos;
1.57      kristaps  327:        const char      *tp;
                    328:
1.84      kristaps  329:        if (MDOC_TEXT == n->type) {
                    330:                tp = n->string;
                    331:                line = n->line;
                    332:                pos = n->pos;
1.57      kristaps  333:                return(check_text(mdoc, line, pos, tp));
                    334:        }
                    335:
1.84      kristaps  336:        if ( ! check_args(mdoc, n))
1.71      kristaps  337:                return(0);
1.84      kristaps  338:        if (NULL == mdoc_valids[n->tok].pre)
1.57      kristaps  339:                return(1);
1.84      kristaps  340:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    341:                if ( ! (*p)(mdoc, n))
1.57      kristaps  342:                        return(0);
                    343:        return(1);
                    344: }
                    345:
                    346:
                    347: int
                    348: mdoc_valid_post(struct mdoc *mdoc)
                    349: {
                    350:        v_post          *p;
                    351:
                    352:        /*
                    353:         * This check occurs after the macro's children have been filled
                    354:         * in: postfix validation.  Since this happens when we're
                    355:         * rewinding the scope tree, it's possible to have multiple
                    356:         * invocations (as by design, for now), we set bit MDOC_VALID to
                    357:         * indicate that we've validated.
                    358:         */
                    359:
                    360:        if (MDOC_VALID & mdoc->last->flags)
                    361:                return(1);
                    362:        mdoc->last->flags |= MDOC_VALID;
                    363:
                    364:        if (MDOC_TEXT == mdoc->last->type)
                    365:                return(1);
                    366:        if (MDOC_ROOT == mdoc->last->type)
                    367:                return(post_root(mdoc));
                    368:
                    369:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    370:                return(1);
                    371:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    372:                if ( ! (*p)(mdoc))
                    373:                        return(0);
                    374:
                    375:        return(1);
                    376: }
                    377:
                    378:
1.71      kristaps  379: static int
1.84      kristaps  380: perr(struct mdoc *m, int line, int pos, enum merr type)
1.71      kristaps  381: {
                    382:        char             *p;
                    383:
                    384:        p = NULL;
                    385:        switch (type) {
1.86    ! kristaps  386:        case (EESCAPE):
        !           387:                p = "invalid escape sequence";
        !           388:                break;
1.84      kristaps  389:        case (EPRINT):
                    390:                p = "invalid character";
                    391:                break;
1.71      kristaps  392:        case (ENESTDISP):
                    393:                p = "displays may not be nested";
                    394:                break;
                    395:        case (EBOOL):
                    396:                p = "expected boolean value";
                    397:                break;
                    398:        case (EARGREP):
                    399:                p = "argument repeated";
                    400:                break;
                    401:        case (EMULTIDISP):
                    402:                p = "multiple display types specified";
                    403:                break;
                    404:        case (EMULTILIST):
                    405:                p = "multiple list types specified";
                    406:                break;
                    407:        case (ELISTTYPE):
                    408:                p = "missing list type";
                    409:                break;
                    410:        case (EDISPTYPE):
                    411:                p = "missing display type";
                    412:                break;
                    413:        case (ELINE):
                    414:                p = "expected line arguments";
                    415:                break;
                    416:        case (ENOPROLOGUE):
                    417:                p = "document has no prologue";
                    418:                break;
                    419:        case (ENODATA):
                    420:                p = "document has no data";
                    421:                break;
                    422:        case (EATT):
                    423:                p = "expected valid AT&T symbol";
                    424:                break;
                    425:        case (ENAME):
                    426:                p = "default name not yet set";
                    427:                break;
                    428:        }
                    429:        assert(p);
1.84      kristaps  430:        return(mdoc_perr(m, line, pos, p));
1.71      kristaps  431: }
                    432:
                    433:
                    434: static int
1.84      kristaps  435: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
1.71      kristaps  436: {
                    437:        char             *p;
                    438:        enum mdoc_warn    c;
                    439:
                    440:        c = WARN_SYNTAX;
                    441:        p = NULL;
                    442:        switch (type) {
                    443:        case (WBADMSEC):
                    444:                p = "inappropriate manual section";
                    445:                c = WARN_COMPAT;
                    446:                break;
                    447:        case (WBADSEC):
                    448:                p = "inappropriate document section";
                    449:                c = WARN_COMPAT;
                    450:                break;
                    451:        case (WARGVAL):
                    452:                p = "argument value suggested";
                    453:                c = WARN_COMPAT;
                    454:                break;
                    455:        case (WPROLREP):
                    456:                p = "prologue macros repeated";
                    457:                c = WARN_COMPAT;
                    458:                break;
                    459:        case (WPROLOOO):
                    460:                p = "prologue macros out-of-order";
                    461:                c = WARN_COMPAT;
                    462:                break;
1.86    ! kristaps  463:        case (WESCAPE):
        !           464:                p = "invalid escape sequence";
        !           465:                break;
1.71      kristaps  466:        case (WNOLINE):
                    467:                p = "suggested no line arguments";
                    468:                break;
                    469:        case (WLINE):
                    470:                p = "suggested line arguments";
                    471:                break;
                    472:        case (WMULTILINE):
                    473:                p = "suggested multi-line arguments";
                    474:                break;
                    475:        case (WNOMULTILINE):
                    476:                p = "suggested no multi-line arguments";
                    477:                break;
                    478:        case (WWRONGMSEC):
                    479:                p = "document section in wrong manual section";
                    480:                c = WARN_COMPAT;
                    481:                break;
                    482:        case (WSECOOO):
                    483:                p = "document section out of conventional order";
                    484:                break;
                    485:        case (WSECREP):
                    486:                p = "document section repeated";
                    487:                break;
                    488:        case (WBADSTAND):
                    489:                p = "unknown standard";
                    490:                break;
                    491:        case (WNAMESECINC):
                    492:                p = "NAME section contents incomplete/badly-ordered";
                    493:                break;
                    494:        }
                    495:        assert(p);
1.84      kristaps  496:        return(mdoc_pwarn(m, line, pos, c, p));
1.71      kristaps  497: }
                    498:
                    499:
1.57      kristaps  500:
1.51      kristaps  501: static inline int
                    502: warn_count(struct mdoc *m, const char *k,
                    503:                int want, const char *v, int has)
                    504: {
                    505:
1.55      kristaps  506:        return(mdoc_warn(m, WARN_SYNTAX,
1.71      kristaps  507:                "suggests %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  508: }
                    509:
                    510:
                    511: static inline int
                    512: err_count(struct mdoc *m, const char *k,
                    513:                int want, const char *v, int has)
                    514: {
                    515:
1.71      kristaps  516:        return(mdoc_err(m,
                    517:                "requires %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  518: }
                    519:
                    520:
                    521: static inline int
                    522: count_child(struct mdoc *mdoc)
1.36      kristaps  523: {
1.51      kristaps  524:        int               i;
1.36      kristaps  525:        struct mdoc_node *n;
                    526:
                    527:        for (i = 0, n = mdoc->last->child; n; n = n->next, i++)
                    528:                /* Do nothing */ ;
1.53      kristaps  529:
1.36      kristaps  530:        return(i);
                    531: }
                    532:
                    533:
1.53      kristaps  534: /*
                    535:  * Build these up with macros because they're basically the same check
                    536:  * for different inequalities.  Yes, this could be done with functions,
                    537:  * but this is reasonable for now.
                    538:  */
1.36      kristaps  539:
1.53      kristaps  540: #define CHECK_CHILD_DEFN(lvl, name, ineq)                      \
                    541: static int                                                     \
                    542: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)   \
                    543: {                                                              \
                    544:        int i;                                                  \
                    545:        if ((i = count_child(mdoc)) ineq sz)                    \
                    546:                return(1);                                      \
                    547:        return(lvl##_count(mdoc, #ineq, sz, p, i));             \
                    548: }
                    549:
                    550: #define CHECK_BODY_DEFN(name, lvl, func, num)                  \
                    551: static int                                                     \
1.55      kristaps  552: b##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  553: {                                                              \
                    554:        if (MDOC_BODY != mdoc->last->type)                      \
                    555:                return(1);                                      \
1.71      kristaps  556:        return(func(mdoc, "multi-line arguments", (num)));      \
1.53      kristaps  557: }
                    558:
                    559: #define CHECK_ELEM_DEFN(name, lvl, func, num)                  \
                    560: static int                                                     \
1.55      kristaps  561: e##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  562: {                                                              \
                    563:        assert(MDOC_ELEM == mdoc->last->type);                  \
1.71      kristaps  564:        return(func(mdoc, "line arguments", (num)));            \
1.53      kristaps  565: }
                    566:
                    567: #define CHECK_HEAD_DEFN(name, lvl, func, num)                  \
                    568: static int                                                     \
1.55      kristaps  569: h##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  570: {                                                              \
                    571:        if (MDOC_HEAD != mdoc->last->type)                      \
                    572:                return(1);                                      \
1.71      kristaps  573:        return(func(mdoc, "line arguments", (num)));            \
1.36      kristaps  574: }
                    575:
                    576:
1.53      kristaps  577: CHECK_CHILD_DEFN(warn, gt, >)                  /* warn_child_gt() */
                    578: CHECK_CHILD_DEFN(err, gt, >)                   /* err_child_gt() */
                    579: CHECK_CHILD_DEFN(warn, eq, ==)                 /* warn_child_eq() */
                    580: CHECK_CHILD_DEFN(err, eq, ==)                  /* err_child_eq() */
                    581: CHECK_CHILD_DEFN(err, lt, <)                   /* err_child_lt() */
1.59      kristaps  582: CHECK_CHILD_DEFN(warn, lt, <)                  /* warn_child_lt() */
1.53      kristaps  583: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)   /* bwarn_ge1() */
                    584: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1)   /* ewarn_eq1() */
                    585: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0)   /* ewarn_eq0() */
                    586: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)   /* ewarn_gt1() */
                    587: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)     /* eerr_eq1() */
                    588: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3)     /* eerr_le2() */
                    589: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0)     /* eerr_eq0() */
                    590: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)     /* eerr_ge1() */
                    591: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)     /* herr_eq0() */
1.59      kristaps  592: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)   /* hwarn_le1() */
1.53      kristaps  593: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)     /* herr_ge1() */
1.58      kristaps  594: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)   /* hwarn_eq1() */
1.36      kristaps  595:
                    596:
                    597: static int
1.55      kristaps  598: check_stdarg(PRE_ARGS)
1.36      kristaps  599: {
                    600:
1.71      kristaps  601:        if (n->args && 1 == n->args->argc)
                    602:                if (MDOC_Std == n->args->argv[0].arg)
                    603:                        return(1);
                    604:        return(nwarn(mdoc, n, WARGVAL));
1.36      kristaps  605: }
                    606:
                    607:
                    608: static int
1.69      kristaps  609: check_sec(PRE_ARGS, ...)
                    610: {
                    611:        enum mdoc_sec    sec;
                    612:        va_list          ap;
                    613:
                    614:        va_start(ap, n);
                    615:
                    616:        for (;;) {
1.71      kristaps  617:                /* LINTED */
1.70      kristaps  618:                sec = (enum mdoc_sec)va_arg(ap, int);
                    619:                if (SEC_CUSTOM == sec)
1.69      kristaps  620:                        break;
                    621:                if (sec != mdoc->lastsec)
                    622:                        continue;
                    623:                va_end(ap);
                    624:                return(1);
                    625:        }
                    626:
                    627:        va_end(ap);
1.71      kristaps  628:        return(nwarn(mdoc, n, WBADSEC));
1.69      kristaps  629: }
                    630:
                    631:
                    632: static int
                    633: check_msec(PRE_ARGS, ...)
1.33      kristaps  634: {
1.69      kristaps  635:        va_list          ap;
                    636:        int              msec;
1.33      kristaps  637:
1.69      kristaps  638:        va_start(ap, n);
                    639:        for (;;) {
1.71      kristaps  640:                /* LINTED */
1.69      kristaps  641:                if (0 == (msec = va_arg(ap, int)))
                    642:                        break;
                    643:                if (msec != mdoc->meta.msec)
                    644:                        continue;
                    645:                va_end(ap);
                    646:                return(1);
                    647:        }
                    648:
                    649:        va_end(ap);
1.71      kristaps  650:        return(nwarn(mdoc, n, WBADMSEC));
1.55      kristaps  651: }
                    652:
                    653:
1.68      kristaps  654: static int
1.71      kristaps  655: check_args(struct mdoc *m, const struct mdoc_node *n)
                    656: {
                    657:        int              i;
                    658:
                    659:        if (NULL == n->args)
                    660:                return(1);
                    661:
1.73      kristaps  662:        assert(n->args->argc);
1.71      kristaps  663:        for (i = 0; i < (int)n->args->argc; i++)
                    664:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    665:                        return(0);
                    666:
                    667:        return(1);
                    668: }
                    669:
                    670:
                    671: static int
                    672: check_argv(struct mdoc *m, const struct mdoc_node *n,
                    673:                const struct mdoc_argv *v)
1.68      kristaps  674: {
1.71      kristaps  675:        int              i;
1.68      kristaps  676:
1.71      kristaps  677:        for (i = 0; i < (int)v->sz; i++)
                    678:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    679:                        return(0);
1.68      kristaps  680:
1.82      kristaps  681:        if (MDOC_Std == v->arg) {
1.71      kristaps  682:                /* `Nm' name must be set. */
                    683:                if (v->sz || m->meta.name)
                    684:                        return(1);
                    685:                return(nerr(m, n, ENAME));
1.68      kristaps  686:        }
                    687:
                    688:        return(1);
                    689: }
                    690:
                    691:
1.55      kristaps  692: static int
1.63      kristaps  693: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
1.55      kristaps  694: {
                    695:        size_t           c;
                    696:
1.84      kristaps  697:        /* FIXME: indicate deprecated escapes \*(xx and \*x. */
1.62      kristaps  698:
1.55      kristaps  699:        for ( ; *p; p++) {
1.86    ! kristaps  700:                if ('\t' == *p) {
        !           701:                        if ( ! (MDOC_LITERAL & mdoc->flags))
        !           702:                                return(perr(mdoc, line, pos, EPRINT));
        !           703:                } else if ( ! isprint((u_char)*p))
1.84      kristaps  704:                        return(perr(mdoc, line, pos, EPRINT));
1.86    ! kristaps  705:
1.55      kristaps  706:                if ('\\' != *p)
                    707:                        continue;
1.86    ! kristaps  708:
1.55      kristaps  709:                if ((c = mdoc_isescape(p))) {
1.63      kristaps  710:                        p += (int)c - 1;
1.55      kristaps  711:                        continue;
                    712:                }
1.74      kristaps  713:                if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
1.86    ! kristaps  714:                        return(perr(mdoc, line, pos, EESCAPE));
        !           715:                if ( ! pwarn(mdoc, line, pos, WESCAPE))
1.74      kristaps  716:                        return(0);
1.55      kristaps  717:        }
                    718:
                    719:        return(1);
1.14      kristaps  720: }
                    721:
                    722:
1.55      kristaps  723:
                    724:
1.14      kristaps  725: static int
1.55      kristaps  726: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
1.54      kristaps  727: {
                    728:
                    729:        assert(n->parent);
                    730:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    731:                        (t == n->parent->type))
                    732:                return(1);
                    733:
                    734:        return(mdoc_nerr(mdoc, n, "require parent %s",
                    735:                MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
                    736: }
                    737:
                    738:
                    739:
                    740: static int
1.55      kristaps  741: pre_display(PRE_ARGS)
1.23      kristaps  742: {
1.55      kristaps  743:        struct mdoc_node *node;
1.23      kristaps  744:
1.53      kristaps  745:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    746:
1.55      kristaps  747:        if (MDOC_BLOCK != n->type)
1.24      kristaps  748:                return(1);
                    749:
1.38      kristaps  750:        /* LINTED */
1.55      kristaps  751:        for (node = mdoc->last->parent; node; node = node->parent)
                    752:                if (MDOC_BLOCK == node->type)
                    753:                        if (MDOC_Bd == node->tok)
1.23      kristaps  754:                                break;
1.55      kristaps  755:        if (NULL == node)
1.23      kristaps  756:                return(1);
1.53      kristaps  757:
1.71      kristaps  758:        return(nerr(mdoc, n, ENESTDISP));
1.23      kristaps  759: }
                    760:
                    761:
                    762: static int
1.55      kristaps  763: pre_bl(PRE_ARGS)
1.24      kristaps  764: {
1.71      kristaps  765:        int              i, type, width, offset;
1.24      kristaps  766:
1.55      kristaps  767:        if (MDOC_BLOCK != n->type)
1.24      kristaps  768:                return(1);
1.71      kristaps  769:        if (NULL == n->args)
                    770:                return(nerr(mdoc, n, ELISTTYPE));
1.24      kristaps  771:
1.53      kristaps  772:        /* Make sure that only one type of list is specified.  */
                    773:
1.61      kristaps  774:        type = offset = width = -1;
                    775:
1.38      kristaps  776:        /* LINTED */
1.71      kristaps  777:        for (i = 0; i < (int)n->args->argc; i++)
                    778:                switch (n->args->argv[i].arg) {
1.24      kristaps  779:                case (MDOC_Bullet):
                    780:                        /* FALLTHROUGH */
                    781:                case (MDOC_Dash):
                    782:                        /* FALLTHROUGH */
                    783:                case (MDOC_Enum):
                    784:                        /* FALLTHROUGH */
                    785:                case (MDOC_Hyphen):
                    786:                        /* FALLTHROUGH */
                    787:                case (MDOC_Item):
                    788:                        /* FALLTHROUGH */
                    789:                case (MDOC_Tag):
                    790:                        /* FALLTHROUGH */
                    791:                case (MDOC_Diag):
                    792:                        /* FALLTHROUGH */
                    793:                case (MDOC_Hang):
                    794:                        /* FALLTHROUGH */
                    795:                case (MDOC_Ohang):
                    796:                        /* FALLTHROUGH */
                    797:                case (MDOC_Inset):
1.26      kristaps  798:                        /* FALLTHROUGH */
                    799:                case (MDOC_Column):
1.61      kristaps  800:                        if (-1 == type) {
1.71      kristaps  801:                                type = n->args->argv[i].arg;
1.53      kristaps  802:                                break;
1.61      kristaps  803:                        }
1.71      kristaps  804:                        return(nerr(mdoc, n, EMULTILIST));
1.61      kristaps  805:                case (MDOC_Width):
                    806:                        if (-1 == width) {
1.71      kristaps  807:                                width = n->args->argv[i].arg;
1.61      kristaps  808:                                break;
                    809:                        }
1.71      kristaps  810:                        return(nerr(mdoc, n, EARGREP));
1.61      kristaps  811:                case (MDOC_Offset):
                    812:                        if (-1 == offset) {
1.71      kristaps  813:                                offset = n->args->argv[i].arg;
1.61      kristaps  814:                                break;
                    815:                        }
1.71      kristaps  816:                        return(nerr(mdoc, n, EARGREP));
1.24      kristaps  817:                default:
                    818:                        break;
                    819:                }
1.53      kristaps  820:
1.61      kristaps  821:        if (-1 == type)
1.71      kristaps  822:                return(nerr(mdoc, n, ELISTTYPE));
1.61      kristaps  823:
                    824:        switch (type) {
                    825:        case (MDOC_Column):
                    826:                /* FALLTHROUGH */
                    827:        case (MDOC_Diag):
                    828:                /* FALLTHROUGH */
                    829:        case (MDOC_Inset):
                    830:                /* FALLTHROUGH */
                    831:        case (MDOC_Item):
                    832:                if (-1 == width)
                    833:                        break;
                    834:                return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1.71      kristaps  835:                                "superfluous %s argument",
1.61      kristaps  836:                                mdoc_argnames[MDOC_Width]));
                    837:        case (MDOC_Tag):
1.71      kristaps  838:                if (-1 != width)
                    839:                        break;
                    840:                return(mdoc_nerr(mdoc, n, "missing %s argument",
                    841:                                mdoc_argnames[MDOC_Width]));
1.61      kristaps  842:        default:
                    843:                break;
                    844:        }
                    845:
                    846:        return(1);
1.24      kristaps  847: }
                    848:
                    849:
                    850: static int
1.55      kristaps  851: pre_bd(PRE_ARGS)
1.24      kristaps  852: {
1.71      kristaps  853:        int              i, type, err;
1.24      kristaps  854:
1.55      kristaps  855:        if (MDOC_BLOCK != n->type)
1.24      kristaps  856:                return(1);
1.71      kristaps  857:        if (NULL == n->args)
                    858:                return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  859:
1.53      kristaps  860:        /* Make sure that only one type of display is specified.  */
                    861:
1.38      kristaps  862:        /* LINTED */
1.71      kristaps  863:        for (i = 0, err = type = 0; ! err &&
                    864:                        i < (int)n->args->argc; i++)
                    865:                switch (n->args->argv[i].arg) {
1.24      kristaps  866:                case (MDOC_Ragged):
                    867:                        /* FALLTHROUGH */
                    868:                case (MDOC_Unfilled):
                    869:                        /* FALLTHROUGH */
1.29      kristaps  870:                case (MDOC_Filled):
                    871:                        /* FALLTHROUGH */
1.24      kristaps  872:                case (MDOC_Literal):
                    873:                        /* FALLTHROUGH */
                    874:                case (MDOC_File):
1.53      kristaps  875:                        if (0 == type++)
                    876:                                break;
1.71      kristaps  877:                        return(nerr(mdoc, n, EMULTIDISP));
1.24      kristaps  878:                default:
                    879:                        break;
                    880:                }
1.53      kristaps  881:
                    882:        if (type)
                    883:                return(1);
1.71      kristaps  884:        return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  885: }
                    886:
                    887:
                    888: static int
1.55      kristaps  889: pre_ss(PRE_ARGS)
1.33      kristaps  890: {
                    891:
1.55      kristaps  892:        if (MDOC_BLOCK != n->type)
1.33      kristaps  893:                return(1);
1.55      kristaps  894:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
1.33      kristaps  895: }
                    896:
                    897:
                    898: static int
1.55      kristaps  899: pre_sh(PRE_ARGS)
1.33      kristaps  900: {
                    901:
1.55      kristaps  902:        if (MDOC_BLOCK != n->type)
1.33      kristaps  903:                return(1);
1.55      kristaps  904:        return(check_parent(mdoc, n, -1, MDOC_ROOT));
1.33      kristaps  905: }
                    906:
                    907:
                    908: static int
1.55      kristaps  909: pre_it(PRE_ARGS)
1.53      kristaps  910: {
                    911:
1.55      kristaps  912:        if (MDOC_BLOCK != n->type)
1.53      kristaps  913:                return(1);
1.55      kristaps  914:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
1.53      kristaps  915: }
                    916:
                    917:
                    918: static int
1.69      kristaps  919: pre_an(PRE_ARGS)
1.36      kristaps  920: {
                    921:
1.73      kristaps  922:        if (NULL == n->args || 1 == n->args->argc)
1.36      kristaps  923:                return(1);
1.71      kristaps  924:        return(mdoc_nerr(mdoc, n, "only one argument allowed"));
1.36      kristaps  925: }
                    926:
                    927:
                    928: static int
1.69      kristaps  929: pre_lb(PRE_ARGS)
1.35      kristaps  930: {
1.36      kristaps  931:
1.69      kristaps  932:        return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
1.35      kristaps  933: }
                    934:
                    935:
                    936: static int
1.55      kristaps  937: pre_rv(PRE_ARGS)
1.36      kristaps  938: {
                    939:
1.69      kristaps  940:        if ( ! check_msec(mdoc, n, 2, 3, 0))
1.36      kristaps  941:                return(0);
1.55      kristaps  942:        return(check_stdarg(mdoc, n));
1.36      kristaps  943: }
                    944:
                    945:
                    946: static int
1.55      kristaps  947: pre_ex(PRE_ARGS)
1.33      kristaps  948: {
1.35      kristaps  949:
1.69      kristaps  950:        if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
1.35      kristaps  951:                return(0);
1.55      kristaps  952:        return(check_stdarg(mdoc, n));
1.33      kristaps  953: }
                    954:
                    955:
                    956: static int
1.55      kristaps  957: pre_er(PRE_ARGS)
1.33      kristaps  958: {
                    959:
1.69      kristaps  960:        return(check_msec(mdoc, n, 2, 0));
1.33      kristaps  961: }
                    962:
                    963:
                    964: static int
1.55      kristaps  965: pre_cd(PRE_ARGS)
1.33      kristaps  966: {
                    967:
1.69      kristaps  968:        return(check_msec(mdoc, n, 4, 0));
1.33      kristaps  969: }
                    970:
                    971:
                    972: static int
1.55      kristaps  973: pre_prologue(PRE_ARGS)
1.20      kristaps  974: {
                    975:
1.69      kristaps  976:        return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
                    977: }
1.20      kristaps  978:
                    979:
1.69      kristaps  980: static int
                    981: pre_dt(PRE_ARGS)
                    982: {
                    983:
                    984:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.71      kristaps  985:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  986:                        return(0);
                    987:        if (mdoc->meta.title)
1.71      kristaps  988:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  989:                        return(0);
                    990:        return(1);
                    991: }
                    992:
                    993:
                    994: static int
                    995: pre_os(PRE_ARGS)
                    996: {
                    997:
                    998:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.71      kristaps  999:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps 1000:                        return(0);
                   1001:        if (mdoc->meta.os)
1.71      kristaps 1002:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps 1003:                        return(0);
                   1004:        return(1);
                   1005: }
1.20      kristaps 1006:
                   1007:
1.69      kristaps 1008: static int
                   1009: pre_dd(PRE_ARGS)
                   1010: {
1.20      kristaps 1011:
1.69      kristaps 1012:        if (mdoc->meta.title || mdoc->meta.os)
1.71      kristaps 1013:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps 1014:                        return(0);
                   1015:        if (mdoc->meta.date)
1.71      kristaps 1016:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps 1017:                        return(0);
                   1018:        return(1);
1.20      kristaps 1019: }
                   1020:
                   1021:
1.35      kristaps 1022: static int
1.55      kristaps 1023: post_bf(POST_ARGS)
1.41      kristaps 1024: {
                   1025:        char             *p;
                   1026:        struct mdoc_node *head;
                   1027:
                   1028:        if (MDOC_BLOCK != mdoc->last->type)
                   1029:                return(1);
1.53      kristaps 1030:
1.71      kristaps 1031:        head = mdoc->last->head;
1.41      kristaps 1032:
1.71      kristaps 1033:        if (NULL == mdoc->last->args) {
                   1034:                if (NULL == head->child ||
                   1035:                                MDOC_TEXT != head->child->type)
                   1036:                        return(mdoc_err(mdoc, "text argument expected"));
1.53      kristaps 1037:
1.71      kristaps 1038:                p = head->child->string;
1.53      kristaps 1039:                if (xstrcmp(p, "Em"))
                   1040:                        return(1);
                   1041:                else if (xstrcmp(p, "Li"))
                   1042:                        return(1);
                   1043:                else if (xstrcmp(p, "Sm"))
                   1044:                        return(1);
                   1045:                return(mdoc_nerr(mdoc, head->child, "invalid font"));
1.41      kristaps 1046:        }
1.53      kristaps 1047:
1.41      kristaps 1048:        if (head->child)
1.71      kristaps 1049:                return(mdoc_err(mdoc, "one argument expected"));
1.53      kristaps 1050:
1.71      kristaps 1051:        return(1);
1.41      kristaps 1052: }
                   1053:
                   1054:
                   1055: static int
1.55      kristaps 1056: post_nm(POST_ARGS)
1.37      kristaps 1057: {
                   1058:
                   1059:        if (mdoc->last->child)
                   1060:                return(1);
                   1061:        if (mdoc->meta.name)
                   1062:                return(1);
1.71      kristaps 1063:        return(merr(mdoc, ENAME));
1.37      kristaps 1064: }
                   1065:
                   1066:
                   1067: static int
1.55      kristaps 1068: post_at(POST_ARGS)
1.36      kristaps 1069: {
                   1070:
1.37      kristaps 1071:        if (NULL == mdoc->last->child)
                   1072:                return(1);
1.71      kristaps 1073:        if (MDOC_TEXT != mdoc->last->child->type)
                   1074:                return(merr(mdoc, EATT));
                   1075:        if (mdoc_a2att(mdoc->last->child->string))
1.36      kristaps 1076:                return(1);
1.71      kristaps 1077:        return(merr(mdoc, EATT));
1.36      kristaps 1078: }
                   1079:
                   1080:
                   1081: static int
1.55      kristaps 1082: post_an(POST_ARGS)
1.35      kristaps 1083: {
                   1084:
1.71      kristaps 1085:        if (mdoc->last->args) {
1.35      kristaps 1086:                if (NULL == mdoc->last->child)
                   1087:                        return(1);
1.71      kristaps 1088:                return(merr(mdoc, ELINE));
1.35      kristaps 1089:        }
                   1090:
                   1091:        if (mdoc->last->child)
                   1092:                return(1);
1.71      kristaps 1093:        return(merr(mdoc, ELINE));
1.35      kristaps 1094: }
                   1095:
                   1096:
                   1097: static int
1.82      kristaps 1098: post_args(POST_ARGS)
1.35      kristaps 1099: {
                   1100:
1.71      kristaps 1101:        if (mdoc->last->args)
                   1102:                return(1);
                   1103:        return(merr(mdoc, ELINE));
1.35      kristaps 1104: }
                   1105:
                   1106:
1.25      kristaps 1107: static int
1.55      kristaps 1108: post_it(POST_ARGS)
1.25      kristaps 1109: {
1.71      kristaps 1110:        int               type, i, cols;
                   1111:        struct mdoc_node *n, *c;
1.25      kristaps 1112:
                   1113:        if (MDOC_BLOCK != mdoc->last->type)
                   1114:                return(1);
                   1115:
1.53      kristaps 1116:        n = mdoc->last->parent->parent;
1.71      kristaps 1117:        if (NULL == n->args)
                   1118:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1119:
1.26      kristaps 1120:        /* Some types require block-head, some not. */
1.25      kristaps 1121:
1.38      kristaps 1122:        /* LINTED */
1.71      kristaps 1123:        for (cols = type = -1, i = 0; -1 == type &&
                   1124:                        i < (int)n->args->argc; i++)
                   1125:                switch (n->args->argv[i].arg) {
1.25      kristaps 1126:                case (MDOC_Tag):
                   1127:                        /* FALLTHROUGH */
                   1128:                case (MDOC_Diag):
                   1129:                        /* FALLTHROUGH */
                   1130:                case (MDOC_Hang):
                   1131:                        /* FALLTHROUGH */
                   1132:                case (MDOC_Ohang):
                   1133:                        /* FALLTHROUGH */
                   1134:                case (MDOC_Inset):
1.71      kristaps 1135:                        /* FALLTHROUGH */
1.25      kristaps 1136:                case (MDOC_Bullet):
                   1137:                        /* FALLTHROUGH */
                   1138:                case (MDOC_Dash):
                   1139:                        /* FALLTHROUGH */
                   1140:                case (MDOC_Enum):
                   1141:                        /* FALLTHROUGH */
                   1142:                case (MDOC_Hyphen):
                   1143:                        /* FALLTHROUGH */
                   1144:                case (MDOC_Item):
1.71      kristaps 1145:                        type = n->args->argv[i].arg;
1.47      kristaps 1146:                        break;
1.25      kristaps 1147:                case (MDOC_Column):
1.71      kristaps 1148:                        type = n->args->argv[i].arg;
                   1149:                        cols = (int)n->args->argv[i].sz;
1.25      kristaps 1150:                        break;
                   1151:                default:
                   1152:                        break;
                   1153:                }
                   1154:
1.71      kristaps 1155:        if (-1 == type)
                   1156:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1157:
1.71      kristaps 1158:        switch (type) {
                   1159:        case (MDOC_Tag):
                   1160:                if (NULL == mdoc->last->head->child)
                   1161:                        if ( ! mwarn(mdoc, WLINE))
                   1162:                                return(0);
                   1163:                break;
                   1164:        case (MDOC_Hang):
                   1165:                /* FALLTHROUGH */
                   1166:        case (MDOC_Ohang):
                   1167:                /* FALLTHROUGH */
                   1168:        case (MDOC_Inset):
                   1169:                /* FALLTHROUGH */
                   1170:        case (MDOC_Diag):
                   1171:                if (NULL == mdoc->last->head->child)
                   1172:                        if ( ! mwarn(mdoc, WLINE))
1.25      kristaps 1173:                                return(0);
1.71      kristaps 1174:                if (NULL == mdoc->last->body->child)
                   1175:                        if ( ! mwarn(mdoc, WMULTILINE))
1.25      kristaps 1176:                                return(0);
1.71      kristaps 1177:                break;
                   1178:        case (MDOC_Bullet):
                   1179:                /* FALLTHROUGH */
                   1180:        case (MDOC_Dash):
                   1181:                /* FALLTHROUGH */
                   1182:        case (MDOC_Enum):
                   1183:                /* FALLTHROUGH */
                   1184:        case (MDOC_Hyphen):
                   1185:                /* FALLTHROUGH */
                   1186:        case (MDOC_Item):
                   1187:                if (mdoc->last->head->child)
                   1188:                        if ( ! mwarn(mdoc, WNOLINE))
1.47      kristaps 1189:                                return(0);
1.71      kristaps 1190:                if (NULL == mdoc->last->body->child)
                   1191:                        if ( ! mwarn(mdoc, WMULTILINE))
1.47      kristaps 1192:                                return(0);
1.71      kristaps 1193:                break;
                   1194:        case (MDOC_Column):
                   1195:                if (NULL == mdoc->last->head->child)
                   1196:                        if ( ! mwarn(mdoc, WLINE))
1.47      kristaps 1197:                                return(0);
1.71      kristaps 1198:                if (mdoc->last->body->child)
                   1199:                        if ( ! mwarn(mdoc, WNOMULTILINE))
1.47      kristaps 1200:                                return(0);
1.81      kristaps 1201:                c = mdoc->last->child;
1.80      kristaps 1202:                for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
1.71      kristaps 1203:                        i++;
                   1204:                if (i == cols)
                   1205:                        break;
1.81      kristaps 1206:                return(mdoc_err(mdoc, "column mismatch (have "
                   1207:                                        "%d, want %d)", i, cols));
1.71      kristaps 1208:        default:
                   1209:                break;
1.25      kristaps 1210:        }
                   1211:
1.71      kristaps 1212:        return(1);
1.25      kristaps 1213: }
                   1214:
                   1215:
1.24      kristaps 1216: static int
1.55      kristaps 1217: post_bl(POST_ARGS)
1.24      kristaps 1218: {
1.53      kristaps 1219:        struct mdoc_node        *n;
1.24      kristaps 1220:
                   1221:        if (MDOC_BODY != mdoc->last->type)
                   1222:                return(1);
1.71      kristaps 1223:        if (NULL == mdoc->last->child)
1.64      kristaps 1224:                return(1);
                   1225:
1.38      kristaps 1226:        /* LINTED */
1.24      kristaps 1227:        for (n = mdoc->last->child; n; n = n->next) {
                   1228:                if (MDOC_BLOCK == n->type)
1.25      kristaps 1229:                        if (MDOC_It == n->tok)
1.24      kristaps 1230:                                continue;
1.64      kristaps 1231:                return(mdoc_nerr(mdoc, n, "bad child of parent %s",
                   1232:                                mdoc_macronames[mdoc->last->tok]));
1.24      kristaps 1233:        }
1.53      kristaps 1234:
1.64      kristaps 1235:        return(1);
1.24      kristaps 1236: }
                   1237:
                   1238:
1.34      kristaps 1239: static int
1.37      kristaps 1240: ebool(struct mdoc *mdoc)
1.34      kristaps 1241: {
                   1242:        struct mdoc_node *n;
                   1243:
1.38      kristaps 1244:        /* LINTED */
1.34      kristaps 1245:        for (n = mdoc->last->child; n; n = n->next) {
                   1246:                if (MDOC_TEXT != n->type)
                   1247:                        break;
1.71      kristaps 1248:                if (xstrcmp(n->string, "on"))
1.34      kristaps 1249:                        continue;
1.71      kristaps 1250:                if (xstrcmp(n->string, "off"))
1.34      kristaps 1251:                        continue;
                   1252:                break;
                   1253:        }
1.53      kristaps 1254:
1.34      kristaps 1255:        if (NULL == n)
                   1256:                return(1);
1.71      kristaps 1257:        return(nerr(mdoc, n, EBOOL));
1.37      kristaps 1258: }
                   1259:
                   1260:
                   1261: static int
1.55      kristaps 1262: post_root(POST_ARGS)
1.37      kristaps 1263: {
                   1264:
1.46      kristaps 1265:        if (NULL == mdoc->first->child)
1.71      kristaps 1266:                return(merr(mdoc, ENODATA));
1.46      kristaps 1267:        if (SEC_PROLOGUE == mdoc->lastnamed)
1.71      kristaps 1268:                return(merr(mdoc, ENOPROLOGUE));
1.53      kristaps 1269:
1.46      kristaps 1270:        if (MDOC_BLOCK != mdoc->first->child->type)
1.71      kristaps 1271:                return(merr(mdoc, ENODATA));
1.46      kristaps 1272:        if (MDOC_Sh != mdoc->first->child->tok)
1.71      kristaps 1273:                return(merr(mdoc, ENODATA));
1.53      kristaps 1274:
1.37      kristaps 1275:        return(1);
1.34      kristaps 1276: }
                   1277:
                   1278:
1.20      kristaps 1279: static int
1.69      kristaps 1280: post_st(POST_ARGS)
                   1281: {
                   1282:
1.71      kristaps 1283:        if (mdoc_a2st(mdoc->last->child->string))
1.69      kristaps 1284:                return(1);
1.71      kristaps 1285:        return(mwarn(mdoc, WBADSTAND));
1.69      kristaps 1286: }
                   1287:
                   1288:
                   1289: static int
1.55      kristaps 1290: post_sh(POST_ARGS)
1.14      kristaps 1291: {
1.46      kristaps 1292:
                   1293:        if (MDOC_HEAD == mdoc->last->type)
                   1294:                return(post_sh_head(mdoc));
                   1295:        if (MDOC_BODY == mdoc->last->type)
                   1296:                return(post_sh_body(mdoc));
1.53      kristaps 1297:
1.46      kristaps 1298:        return(1);
                   1299: }
                   1300:
                   1301:
                   1302: static int
1.55      kristaps 1303: post_sh_body(POST_ARGS)
1.46      kristaps 1304: {
                   1305:        struct mdoc_node *n;
                   1306:
                   1307:        if (SEC_NAME != mdoc->lastnamed)
                   1308:                return(1);
                   1309:
1.51      kristaps 1310:        /*
                   1311:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1312:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1313:         * children of the BODY declaration can also be "text".
                   1314:         */
                   1315:
1.46      kristaps 1316:        if (NULL == (n = mdoc->last->child))
1.71      kristaps 1317:                return(mwarn(mdoc, WNAMESECINC));
1.51      kristaps 1318:
                   1319:        for ( ; n && n->next; n = n->next) {
                   1320:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1321:                        continue;
                   1322:                if (MDOC_TEXT == n->type)
                   1323:                        continue;
1.71      kristaps 1324:                if ( ! mwarn(mdoc, WNAMESECINC))
1.51      kristaps 1325:                        return(0);
                   1326:        }
                   1327:
                   1328:        if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1.46      kristaps 1329:                return(1);
1.71      kristaps 1330:        return(mwarn(mdoc, WNAMESECINC));
1.46      kristaps 1331: }
                   1332:
                   1333:
                   1334: static int
1.55      kristaps 1335: post_sh_head(POST_ARGS)
1.46      kristaps 1336: {
1.36      kristaps 1337:        char              buf[64];
1.21      kristaps 1338:        enum mdoc_sec     sec;
                   1339:
1.69      kristaps 1340:        /*
                   1341:         * Process a new section.  Sections are either "named" or
                   1342:         * "custom"; custom sections are user-defined, while named ones
                   1343:         * usually follow a conventional order and may only appear in
                   1344:         * certain manual sections.
                   1345:         */
                   1346:
1.25      kristaps 1347:        assert(MDOC_Sh == mdoc->last->tok);
1.21      kristaps 1348:
1.69      kristaps 1349:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.14      kristaps 1350:
1.46      kristaps 1351:        sec = mdoc_atosec(buf);
                   1352:
1.69      kristaps 1353:        /* The NAME section should always be first. */
                   1354:
1.46      kristaps 1355:        if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1.71      kristaps 1356:                return(mwarn(mdoc, WSECOOO));
1.46      kristaps 1357:        if (SEC_CUSTOM == sec)
1.21      kristaps 1358:                return(1);
1.69      kristaps 1359:
                   1360:        /* Check for repeated or out-of-order sections. */
                   1361:
1.46      kristaps 1362:        if (sec == mdoc->lastnamed)
1.71      kristaps 1363:                return(mwarn(mdoc, WSECREP));
1.46      kristaps 1364:        if (sec < mdoc->lastnamed)
1.71      kristaps 1365:                return(mwarn(mdoc, WSECOOO));
1.69      kristaps 1366:
                   1367:        /* Check particular section/manual section conventions. */
                   1368:
                   1369:        switch (sec) {
                   1370:        case (SEC_LIBRARY):
                   1371:                switch (mdoc->meta.msec) {
                   1372:                case (2):
                   1373:                        /* FALLTHROUGH */
                   1374:                case (3):
                   1375:                        break;
                   1376:                default:
1.71      kristaps 1377:                        return(mwarn(mdoc, WWRONGMSEC));
1.69      kristaps 1378:                }
                   1379:                break;
                   1380:        default:
                   1381:                break;
                   1382:        }
1.46      kristaps 1383:
                   1384:        return(1);
1.11      kristaps 1385: }
                   1386:
                   1387:
1.57      kristaps 1388: static int
1.69      kristaps 1389: pre_fd(PRE_ARGS)
1.11      kristaps 1390: {
1.39      kristaps 1391:
1.69      kristaps 1392:        return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1.11      kristaps 1393: }

CVSweb