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

Annotation of mandoc/validate.c, Revision 1.82

1.82    ! kristaps    1: /* $Id: validate.c,v 1.81 2009/03/12 06:32:17 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      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 {
                     43:        ENODATA,
                     44:        ENOPROLOGUE,
                     45:        ELINE,
                     46:        EATT,
                     47:        ENAME,
                     48:        ELISTTYPE,
                     49:        EDISPTYPE,
                     50:        EMULTIDISP,
                     51:        EMULTILIST,
                     52:        EARGREP,
                     53:        EBOOL,
                     54:        ENESTDISP
                     55: };
                     56:
                     57: enum   mwarn {
                     58:        WWRONGMSEC,
                     59:        WSECOOO,
                     60:        WSECREP,
                     61:        WBADSTAND,
                     62:        WNAMESECINC,
                     63:        WNOMULTILINE,
                     64:        WMULTILINE,
                     65:        WLINE,
                     66:        WNOLINE,
                     67:        WPROLOOO,
                     68:        WPROLREP,
                     69:        WARGVAL,
                     70:        WBADSEC,
                     71:        WBADMSEC
                     72: };
                     73:
1.55      kristaps   74: typedef        int     (*v_pre)(PRE_ARGS);
                     75: typedef        int     (*v_post)(POST_ARGS);
1.14      kristaps   76:
                     77: struct valids {
1.24      kristaps   78:        v_pre   *pre;
1.17      kristaps   79:        v_post  *post;
1.14      kristaps   80: };
1.1       kristaps   81:
1.37      kristaps   82: /* Utility checks. */
                     83:
1.71      kristaps   84: static int     nwarn(struct mdoc *,
                     85:                        const struct mdoc_node *, enum mwarn);
                     86: static int     nerr(struct mdoc *,
                     87:                        const struct mdoc_node *, enum merr);
1.55      kristaps   88: static int     check_parent(PRE_ARGS, int, enum mdoc_type);
1.69      kristaps   89: static int     check_msec(PRE_ARGS, ...);
                     90: static int     check_sec(PRE_ARGS, ...);
1.55      kristaps   91: static int     check_stdarg(PRE_ARGS);
                     92: static int     check_text(struct mdoc *,
1.63      kristaps   93:                        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.69      kristaps  154: static v_pre   pres_an[] = { pre_an, NULL };
1.24      kristaps  155: static v_pre   pres_bd[] = { pre_display, pre_bd, NULL };
                    156: static v_pre   pres_bl[] = { pre_bl, NULL };
1.33      kristaps  157: static v_pre   pres_cd[] = { pre_cd, NULL };
1.69      kristaps  158: static v_pre   pres_dd[] = { pre_prologue, pre_dd, NULL };
                    159: static v_pre   pres_d1[] = { pre_display, NULL };
                    160: static v_pre   pres_dt[] = { pre_prologue, pre_dt, NULL };
1.33      kristaps  161: static v_pre   pres_er[] = { pre_er, NULL };
                    162: static v_pre   pres_ex[] = { pre_ex, NULL };
1.69      kristaps  163: static v_pre   pres_fd[] = { pre_fd, NULL };
                    164: static v_pre   pres_it[] = { pre_it, NULL };
                    165: static v_pre   pres_lb[] = { pre_lb, NULL };
                    166: static v_pre   pres_os[] = { pre_prologue, pre_os, NULL };
1.36      kristaps  167: static v_pre   pres_rv[] = { pre_rv, NULL };
1.69      kristaps  168: static v_pre   pres_sh[] = { pre_sh, NULL };
                    169: static v_pre   pres_ss[] = { pre_ss, NULL };
1.37      kristaps  170: static v_post  posts_bool[] = { eerr_eq1, ebool, NULL };
                    171: static v_post  posts_bd[] = { herr_eq0, bwarn_ge1, NULL };
                    172: static v_post  posts_text[] = { eerr_ge1, NULL };
                    173: static v_post  posts_wtext[] = { ewarn_ge1, NULL };
                    174: static v_post  posts_notext[] = { eerr_eq0, NULL };
1.43      kristaps  175: static v_post  posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
1.37      kristaps  176: static v_post  posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
                    177: static v_post  posts_bl[] = { herr_eq0, bwarn_ge1, post_bl, NULL };
1.25      kristaps  178: static v_post  posts_it[] = { post_it, NULL };
1.39      kristaps  179: static v_post  posts_in[] = { ewarn_eq1, NULL };
1.37      kristaps  180: static v_post  posts_ss[] = { herr_ge1, NULL };
1.52      kristaps  181: static v_post  posts_pf[] = { eerr_eq1, NULL };
1.69      kristaps  182: static v_post  posts_lb[] = { eerr_eq1, NULL };
1.78      kristaps  183: static v_post  posts_mt[] = { eerr_ge1, NULL };
1.69      kristaps  184: static v_post  posts_st[] = { eerr_eq1, post_st, NULL };
1.37      kristaps  185: static v_post  posts_pp[] = { ewarn_eq0, NULL };
1.82    ! kristaps  186: static v_post  posts_ex[] = { eerr_eq0, post_args, NULL };
        !           187: static v_post  posts_rv[] = { eerr_eq0, post_args, NULL };
1.35      kristaps  188: static v_post  posts_an[] = { post_an, NULL };
1.37      kristaps  189: static v_post  posts_at[] = { post_at, NULL };
1.69      kristaps  190: static v_post  posts_xr[] = { eerr_ge1, eerr_le2, NULL };
1.78      kristaps  191: static v_post  posts_lk[] = { eerr_ge1, NULL };
1.37      kristaps  192: static v_post  posts_nm[] = { post_nm, NULL };
1.59      kristaps  193: static v_post  posts_bf[] = { hwarn_le1, post_bf, NULL };
1.45      kristaps  194: static v_post  posts_rs[] = { herr_eq0, bwarn_ge1, NULL };
1.58      kristaps  195: static v_post  posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
1.45      kristaps  196: static v_post  posts_bk[] = { herr_eq0, bwarn_ge1, NULL };
1.69      kristaps  197: static v_post  posts_fd[] = { ewarn_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.69      kristaps  224:        { pres_fd, posts_fd },                  /* 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 */
                    285:        { NULL, posts_rs },                     /* Rs */
                    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 */
                    300:        { NULL, posts_bk },                     /* Bk */
                    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.75      kristaps  307:        { NULL, NULL },                         /* Lb */
1.76      kristaps  308:        { NULL, posts_pp },                     /* Pp */
1.78      kristaps  309:        { NULL, posts_lk },                     /* Lk */
                    310:        { NULL, posts_mt },                     /* 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.9       kristaps  315: };
1.6       kristaps  316:
                    317:
1.57      kristaps  318: int
                    319: mdoc_valid_pre(struct mdoc *mdoc,
                    320:                const struct mdoc_node *node)
                    321: {
                    322:        v_pre           *p;
1.71      kristaps  323:        int              line, pos;
1.57      kristaps  324:        const char      *tp;
                    325:
                    326:        if (MDOC_TEXT == node->type) {
1.71      kristaps  327:                tp = node->string;
1.57      kristaps  328:                line = node->line;
                    329:                pos = node->pos;
                    330:                return(check_text(mdoc, line, pos, tp));
                    331:        }
                    332:
1.71      kristaps  333:        if ( ! check_args(mdoc, node))
                    334:                return(0);
1.57      kristaps  335:        if (NULL == mdoc_valids[node->tok].pre)
                    336:                return(1);
                    337:        for (p = mdoc_valids[node->tok].pre; *p; p++)
                    338:                if ( ! (*p)(mdoc, node))
                    339:                        return(0);
                    340:        return(1);
                    341: }
                    342:
                    343:
                    344: int
                    345: mdoc_valid_post(struct mdoc *mdoc)
                    346: {
                    347:        v_post          *p;
                    348:
                    349:        /*
                    350:         * This check occurs after the macro's children have been filled
                    351:         * in: postfix validation.  Since this happens when we're
                    352:         * rewinding the scope tree, it's possible to have multiple
                    353:         * invocations (as by design, for now), we set bit MDOC_VALID to
                    354:         * indicate that we've validated.
                    355:         */
                    356:
                    357:        if (MDOC_VALID & mdoc->last->flags)
                    358:                return(1);
                    359:        mdoc->last->flags |= MDOC_VALID;
                    360:
                    361:        if (MDOC_TEXT == mdoc->last->type)
                    362:                return(1);
                    363:        if (MDOC_ROOT == mdoc->last->type)
                    364:                return(post_root(mdoc));
                    365:
                    366:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    367:                return(1);
                    368:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    369:                if ( ! (*p)(mdoc))
                    370:                        return(0);
                    371:
                    372:        return(1);
                    373: }
                    374:
                    375:
1.71      kristaps  376: #define        merr(m, t) nerr((m), (m)->last, (t))
                    377: static int
                    378: nerr(struct mdoc *m, const struct mdoc_node *n, enum merr type)
                    379: {
                    380:        char             *p;
                    381:
                    382:        p = NULL;
                    383:
                    384:        switch (type) {
                    385:        case (ENESTDISP):
                    386:                p = "displays may not be nested";
                    387:                break;
                    388:        case (EBOOL):
                    389:                p = "expected boolean value";
                    390:                break;
                    391:        case (EARGREP):
                    392:                p = "argument repeated";
                    393:                break;
                    394:        case (EMULTIDISP):
                    395:                p = "multiple display types specified";
                    396:                break;
                    397:        case (EMULTILIST):
                    398:                p = "multiple list types specified";
                    399:                break;
                    400:        case (ELISTTYPE):
                    401:                p = "missing list type";
                    402:                break;
                    403:        case (EDISPTYPE):
                    404:                p = "missing display type";
                    405:                break;
                    406:        case (ELINE):
                    407:                p = "expected line arguments";
                    408:                break;
                    409:        case (ENOPROLOGUE):
                    410:                p = "document has no prologue";
                    411:                break;
                    412:        case (ENODATA):
                    413:                p = "document has no data";
                    414:                break;
                    415:        case (EATT):
                    416:                p = "expected valid AT&T symbol";
                    417:                break;
                    418:        case (ENAME):
                    419:                p = "default name not yet set";
                    420:                break;
                    421:        }
                    422:
                    423:        assert(p);
                    424:        return(mdoc_nerr(m, n, p));
                    425: }
                    426:
                    427:
                    428: #define        mwarn(m, t) nwarn((m), (m)->last, (t))
                    429: static int
                    430: nwarn(struct mdoc *m, const struct mdoc_node *n, enum mwarn type)
                    431: {
                    432:        char             *p;
                    433:        enum mdoc_warn    c;
                    434:
                    435:        c = WARN_SYNTAX;
                    436:        p = NULL;
                    437:
                    438:        switch (type) {
                    439:        case (WBADMSEC):
                    440:                p = "inappropriate manual section";
                    441:                c = WARN_COMPAT;
                    442:                break;
                    443:        case (WBADSEC):
                    444:                p = "inappropriate document section";
                    445:                c = WARN_COMPAT;
                    446:                break;
                    447:        case (WARGVAL):
                    448:                p = "argument value suggested";
                    449:                c = WARN_COMPAT;
                    450:                break;
                    451:        case (WPROLREP):
                    452:                p = "prologue macros repeated";
                    453:                c = WARN_COMPAT;
                    454:                break;
                    455:        case (WPROLOOO):
                    456:                p = "prologue macros out-of-order";
                    457:                c = WARN_COMPAT;
                    458:                break;
                    459:        case (WNOLINE):
                    460:                p = "suggested no line arguments";
                    461:                break;
                    462:        case (WLINE):
                    463:                p = "suggested line arguments";
                    464:                break;
                    465:        case (WMULTILINE):
                    466:                p = "suggested multi-line arguments";
                    467:                break;
                    468:        case (WNOMULTILINE):
                    469:                p = "suggested no multi-line arguments";
                    470:                break;
                    471:        case (WWRONGMSEC):
                    472:                p = "document section in wrong manual section";
                    473:                c = WARN_COMPAT;
                    474:                break;
                    475:        case (WSECOOO):
                    476:                p = "document section out of conventional order";
                    477:                break;
                    478:        case (WSECREP):
                    479:                p = "document section repeated";
                    480:                break;
                    481:        case (WBADSTAND):
                    482:                p = "unknown standard";
                    483:                break;
                    484:        case (WNAMESECINC):
                    485:                p = "NAME section contents incomplete/badly-ordered";
                    486:                break;
                    487:        }
                    488:        assert(p);
                    489:        return(mdoc_nwarn(m, n, c, p));
                    490: }
                    491:
                    492:
1.57      kristaps  493:
1.51      kristaps  494: static inline int
                    495: warn_count(struct mdoc *m, const char *k,
                    496:                int want, const char *v, int has)
                    497: {
                    498:
1.55      kristaps  499:        return(mdoc_warn(m, WARN_SYNTAX,
1.71      kristaps  500:                "suggests %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  501: }
                    502:
                    503:
                    504: static inline int
                    505: err_count(struct mdoc *m, const char *k,
                    506:                int want, const char *v, int has)
                    507: {
                    508:
1.71      kristaps  509:        return(mdoc_err(m,
                    510:                "requires %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  511: }
                    512:
                    513:
                    514: static inline int
                    515: count_child(struct mdoc *mdoc)
1.36      kristaps  516: {
1.51      kristaps  517:        int               i;
1.36      kristaps  518:        struct mdoc_node *n;
                    519:
                    520:        for (i = 0, n = mdoc->last->child; n; n = n->next, i++)
                    521:                /* Do nothing */ ;
1.53      kristaps  522:
1.36      kristaps  523:        return(i);
                    524: }
                    525:
                    526:
1.53      kristaps  527: /*
                    528:  * Build these up with macros because they're basically the same check
                    529:  * for different inequalities.  Yes, this could be done with functions,
                    530:  * but this is reasonable for now.
                    531:  */
1.36      kristaps  532:
1.53      kristaps  533: #define CHECK_CHILD_DEFN(lvl, name, ineq)                      \
                    534: static int                                                     \
                    535: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)   \
                    536: {                                                              \
                    537:        int i;                                                  \
                    538:        if ((i = count_child(mdoc)) ineq sz)                    \
                    539:                return(1);                                      \
                    540:        return(lvl##_count(mdoc, #ineq, sz, p, i));             \
                    541: }
                    542:
                    543: #define CHECK_BODY_DEFN(name, lvl, func, num)                  \
                    544: static int                                                     \
1.55      kristaps  545: b##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  546: {                                                              \
                    547:        if (MDOC_BODY != mdoc->last->type)                      \
                    548:                return(1);                                      \
1.71      kristaps  549:        return(func(mdoc, "multi-line arguments", (num)));      \
1.53      kristaps  550: }
                    551:
                    552: #define CHECK_ELEM_DEFN(name, lvl, func, num)                  \
                    553: static int                                                     \
1.55      kristaps  554: e##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  555: {                                                              \
                    556:        assert(MDOC_ELEM == mdoc->last->type);                  \
1.71      kristaps  557:        return(func(mdoc, "line arguments", (num)));            \
1.53      kristaps  558: }
                    559:
                    560: #define CHECK_HEAD_DEFN(name, lvl, func, num)                  \
                    561: static int                                                     \
1.55      kristaps  562: h##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  563: {                                                              \
                    564:        if (MDOC_HEAD != mdoc->last->type)                      \
                    565:                return(1);                                      \
1.71      kristaps  566:        return(func(mdoc, "line arguments", (num)));            \
1.36      kristaps  567: }
                    568:
                    569:
1.53      kristaps  570: CHECK_CHILD_DEFN(warn, gt, >)                  /* warn_child_gt() */
                    571: CHECK_CHILD_DEFN(err, gt, >)                   /* err_child_gt() */
                    572: CHECK_CHILD_DEFN(warn, eq, ==)                 /* warn_child_eq() */
                    573: CHECK_CHILD_DEFN(err, eq, ==)                  /* err_child_eq() */
                    574: CHECK_CHILD_DEFN(err, lt, <)                   /* err_child_lt() */
1.59      kristaps  575: CHECK_CHILD_DEFN(warn, lt, <)                  /* warn_child_lt() */
1.53      kristaps  576: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)   /* bwarn_ge1() */
                    577: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1)   /* ewarn_eq1() */
                    578: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0)   /* ewarn_eq0() */
                    579: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)   /* ewarn_gt1() */
                    580: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)     /* eerr_eq1() */
                    581: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3)     /* eerr_le2() */
                    582: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0)     /* eerr_eq0() */
                    583: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)     /* eerr_ge1() */
                    584: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)     /* herr_eq0() */
1.59      kristaps  585: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)   /* hwarn_le1() */
1.53      kristaps  586: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)     /* herr_ge1() */
1.58      kristaps  587: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)   /* hwarn_eq1() */
1.36      kristaps  588:
                    589:
                    590: static int
1.55      kristaps  591: check_stdarg(PRE_ARGS)
1.36      kristaps  592: {
                    593:
1.71      kristaps  594:        if (n->args && 1 == n->args->argc)
                    595:                if (MDOC_Std == n->args->argv[0].arg)
                    596:                        return(1);
                    597:        return(nwarn(mdoc, n, WARGVAL));
1.36      kristaps  598: }
                    599:
                    600:
                    601: static int
1.69      kristaps  602: check_sec(PRE_ARGS, ...)
                    603: {
                    604:        enum mdoc_sec    sec;
                    605:        va_list          ap;
                    606:
                    607:        va_start(ap, n);
                    608:
                    609:        for (;;) {
1.71      kristaps  610:                /* LINTED */
1.70      kristaps  611:                sec = (enum mdoc_sec)va_arg(ap, int);
                    612:                if (SEC_CUSTOM == sec)
1.69      kristaps  613:                        break;
                    614:                if (sec != mdoc->lastsec)
                    615:                        continue;
                    616:                va_end(ap);
                    617:                return(1);
                    618:        }
                    619:
                    620:        va_end(ap);
1.71      kristaps  621:        return(nwarn(mdoc, n, WBADSEC));
1.69      kristaps  622: }
                    623:
                    624:
                    625: static int
                    626: check_msec(PRE_ARGS, ...)
1.33      kristaps  627: {
1.69      kristaps  628:        va_list          ap;
                    629:        int              msec;
1.33      kristaps  630:
1.69      kristaps  631:        va_start(ap, n);
                    632:        for (;;) {
1.71      kristaps  633:                /* LINTED */
1.69      kristaps  634:                if (0 == (msec = va_arg(ap, int)))
                    635:                        break;
                    636:                if (msec != mdoc->meta.msec)
                    637:                        continue;
                    638:                va_end(ap);
                    639:                return(1);
                    640:        }
                    641:
                    642:        va_end(ap);
1.71      kristaps  643:        return(nwarn(mdoc, n, WBADMSEC));
1.55      kristaps  644: }
                    645:
                    646:
1.68      kristaps  647: static int
1.71      kristaps  648: check_args(struct mdoc *m, const struct mdoc_node *n)
                    649: {
                    650:        int              i;
                    651:
                    652:        if (NULL == n->args)
                    653:                return(1);
                    654:
1.73      kristaps  655:        assert(n->args->argc);
1.71      kristaps  656:        for (i = 0; i < (int)n->args->argc; i++)
                    657:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    658:                        return(0);
                    659:
                    660:        return(1);
                    661: }
                    662:
                    663:
                    664: static int
                    665: check_argv(struct mdoc *m, const struct mdoc_node *n,
                    666:                const struct mdoc_argv *v)
1.68      kristaps  667: {
1.71      kristaps  668:        int              i;
1.68      kristaps  669:
1.71      kristaps  670:        for (i = 0; i < (int)v->sz; i++)
                    671:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    672:                        return(0);
1.68      kristaps  673:
1.82    ! kristaps  674:        if (MDOC_Std == v->arg) {
1.71      kristaps  675:                /* `Nm' name must be set. */
                    676:                if (v->sz || m->meta.name)
                    677:                        return(1);
                    678:                return(nerr(m, n, ENAME));
1.68      kristaps  679:        }
                    680:
                    681:        return(1);
                    682: }
                    683:
                    684:
1.55      kristaps  685: static int
1.63      kristaps  686: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
1.55      kristaps  687: {
                    688:        size_t           c;
                    689:
1.62      kristaps  690:        /* XXX - indicate deprecated escapes \*(xx and \*x. */
                    691:
1.55      kristaps  692:        for ( ; *p; p++) {
1.68      kristaps  693:                if ( ! isprint((u_char)*p) && '\t' != *p)
1.56      kristaps  694:                        return(mdoc_perr(mdoc, line, pos,
1.71      kristaps  695:                                "invalid non-printing character"));
1.55      kristaps  696:                if ('\\' != *p)
                    697:                        continue;
                    698:                if ((c = mdoc_isescape(p))) {
1.63      kristaps  699:                        p += (int)c - 1;
1.55      kristaps  700:                        continue;
                    701:                }
1.74      kristaps  702:                if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
                    703:                        return(mdoc_perr(mdoc, line, pos,
                    704:                                        "invalid escape sequence"));
                    705:                if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
                    706:                                        "invalid escape sequence"))
                    707:                        return(0);
1.55      kristaps  708:        }
                    709:
                    710:        return(1);
1.14      kristaps  711: }
                    712:
                    713:
1.55      kristaps  714:
                    715:
1.14      kristaps  716: static int
1.55      kristaps  717: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
1.54      kristaps  718: {
                    719:
                    720:        assert(n->parent);
                    721:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    722:                        (t == n->parent->type))
                    723:                return(1);
                    724:
                    725:        return(mdoc_nerr(mdoc, n, "require parent %s",
                    726:                MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
                    727: }
                    728:
                    729:
                    730:
                    731: static int
1.55      kristaps  732: pre_display(PRE_ARGS)
1.23      kristaps  733: {
1.55      kristaps  734:        struct mdoc_node *node;
1.23      kristaps  735:
1.53      kristaps  736:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    737:
1.55      kristaps  738:        if (MDOC_BLOCK != n->type)
1.24      kristaps  739:                return(1);
                    740:
1.38      kristaps  741:        /* LINTED */
1.55      kristaps  742:        for (node = mdoc->last->parent; node; node = node->parent)
                    743:                if (MDOC_BLOCK == node->type)
                    744:                        if (MDOC_Bd == node->tok)
1.23      kristaps  745:                                break;
1.55      kristaps  746:        if (NULL == node)
1.23      kristaps  747:                return(1);
1.53      kristaps  748:
1.71      kristaps  749:        return(nerr(mdoc, n, ENESTDISP));
1.23      kristaps  750: }
                    751:
                    752:
                    753: static int
1.55      kristaps  754: pre_bl(PRE_ARGS)
1.24      kristaps  755: {
1.71      kristaps  756:        int              i, type, width, offset;
1.24      kristaps  757:
1.55      kristaps  758:        if (MDOC_BLOCK != n->type)
1.24      kristaps  759:                return(1);
1.71      kristaps  760:        if (NULL == n->args)
                    761:                return(nerr(mdoc, n, ELISTTYPE));
1.24      kristaps  762:
1.53      kristaps  763:        /* Make sure that only one type of list is specified.  */
                    764:
1.61      kristaps  765:        type = offset = width = -1;
                    766:
1.38      kristaps  767:        /* LINTED */
1.71      kristaps  768:        for (i = 0; i < (int)n->args->argc; i++)
                    769:                switch (n->args->argv[i].arg) {
1.24      kristaps  770:                case (MDOC_Bullet):
                    771:                        /* FALLTHROUGH */
                    772:                case (MDOC_Dash):
                    773:                        /* FALLTHROUGH */
                    774:                case (MDOC_Enum):
                    775:                        /* FALLTHROUGH */
                    776:                case (MDOC_Hyphen):
                    777:                        /* FALLTHROUGH */
                    778:                case (MDOC_Item):
                    779:                        /* FALLTHROUGH */
                    780:                case (MDOC_Tag):
                    781:                        /* FALLTHROUGH */
                    782:                case (MDOC_Diag):
                    783:                        /* FALLTHROUGH */
                    784:                case (MDOC_Hang):
                    785:                        /* FALLTHROUGH */
                    786:                case (MDOC_Ohang):
                    787:                        /* FALLTHROUGH */
                    788:                case (MDOC_Inset):
1.26      kristaps  789:                        /* FALLTHROUGH */
                    790:                case (MDOC_Column):
1.61      kristaps  791:                        if (-1 == type) {
1.71      kristaps  792:                                type = n->args->argv[i].arg;
1.53      kristaps  793:                                break;
1.61      kristaps  794:                        }
1.71      kristaps  795:                        return(nerr(mdoc, n, EMULTILIST));
1.61      kristaps  796:                case (MDOC_Width):
                    797:                        if (-1 == width) {
1.71      kristaps  798:                                width = n->args->argv[i].arg;
1.61      kristaps  799:                                break;
                    800:                        }
1.71      kristaps  801:                        return(nerr(mdoc, n, EARGREP));
1.61      kristaps  802:                case (MDOC_Offset):
                    803:                        if (-1 == offset) {
1.71      kristaps  804:                                offset = n->args->argv[i].arg;
1.61      kristaps  805:                                break;
                    806:                        }
1.71      kristaps  807:                        return(nerr(mdoc, n, EARGREP));
1.24      kristaps  808:                default:
                    809:                        break;
                    810:                }
1.53      kristaps  811:
1.61      kristaps  812:        if (-1 == type)
1.71      kristaps  813:                return(nerr(mdoc, n, ELISTTYPE));
1.61      kristaps  814:
                    815:        switch (type) {
                    816:        case (MDOC_Column):
                    817:                /* FALLTHROUGH */
                    818:        case (MDOC_Diag):
                    819:                /* FALLTHROUGH */
                    820:        case (MDOC_Inset):
                    821:                /* FALLTHROUGH */
                    822:        case (MDOC_Item):
                    823:                if (-1 == width)
                    824:                        break;
                    825:                return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1.71      kristaps  826:                                "superfluous %s argument",
1.61      kristaps  827:                                mdoc_argnames[MDOC_Width]));
                    828:        case (MDOC_Tag):
1.71      kristaps  829:                if (-1 != width)
                    830:                        break;
                    831:                return(mdoc_nerr(mdoc, n, "missing %s argument",
                    832:                                mdoc_argnames[MDOC_Width]));
1.61      kristaps  833:        default:
                    834:                break;
                    835:        }
                    836:
                    837:        return(1);
1.24      kristaps  838: }
                    839:
                    840:
                    841: static int
1.55      kristaps  842: pre_bd(PRE_ARGS)
1.24      kristaps  843: {
1.71      kristaps  844:        int              i, type, err;
1.24      kristaps  845:
1.55      kristaps  846:        if (MDOC_BLOCK != n->type)
1.24      kristaps  847:                return(1);
1.71      kristaps  848:        if (NULL == n->args)
                    849:                return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  850:
1.53      kristaps  851:        /* Make sure that only one type of display is specified.  */
                    852:
1.38      kristaps  853:        /* LINTED */
1.71      kristaps  854:        for (i = 0, err = type = 0; ! err &&
                    855:                        i < (int)n->args->argc; i++)
                    856:                switch (n->args->argv[i].arg) {
1.24      kristaps  857:                case (MDOC_Ragged):
                    858:                        /* FALLTHROUGH */
                    859:                case (MDOC_Unfilled):
                    860:                        /* FALLTHROUGH */
1.29      kristaps  861:                case (MDOC_Filled):
                    862:                        /* FALLTHROUGH */
1.24      kristaps  863:                case (MDOC_Literal):
                    864:                        /* FALLTHROUGH */
                    865:                case (MDOC_File):
1.53      kristaps  866:                        if (0 == type++)
                    867:                                break;
1.71      kristaps  868:                        return(nerr(mdoc, n, EMULTIDISP));
1.24      kristaps  869:                default:
                    870:                        break;
                    871:                }
1.53      kristaps  872:
                    873:        if (type)
                    874:                return(1);
1.71      kristaps  875:        return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  876: }
                    877:
                    878:
                    879: static int
1.55      kristaps  880: pre_ss(PRE_ARGS)
1.33      kristaps  881: {
                    882:
1.55      kristaps  883:        if (MDOC_BLOCK != n->type)
1.33      kristaps  884:                return(1);
1.55      kristaps  885:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
1.33      kristaps  886: }
                    887:
                    888:
                    889: static int
1.55      kristaps  890: pre_sh(PRE_ARGS)
1.33      kristaps  891: {
                    892:
1.55      kristaps  893:        if (MDOC_BLOCK != n->type)
1.33      kristaps  894:                return(1);
1.55      kristaps  895:        return(check_parent(mdoc, n, -1, MDOC_ROOT));
1.33      kristaps  896: }
                    897:
                    898:
                    899: static int
1.55      kristaps  900: pre_it(PRE_ARGS)
1.53      kristaps  901: {
                    902:
1.55      kristaps  903:        if (MDOC_BLOCK != n->type)
1.53      kristaps  904:                return(1);
1.55      kristaps  905:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
1.53      kristaps  906: }
                    907:
                    908:
                    909: static int
1.69      kristaps  910: pre_an(PRE_ARGS)
1.36      kristaps  911: {
                    912:
1.73      kristaps  913:        if (NULL == n->args || 1 == n->args->argc)
1.36      kristaps  914:                return(1);
1.71      kristaps  915:        return(mdoc_nerr(mdoc, n, "only one argument allowed"));
1.36      kristaps  916: }
                    917:
                    918:
                    919: static int
1.69      kristaps  920: pre_lb(PRE_ARGS)
1.35      kristaps  921: {
1.36      kristaps  922:
1.69      kristaps  923:        return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
1.35      kristaps  924: }
                    925:
                    926:
                    927: static int
1.55      kristaps  928: pre_rv(PRE_ARGS)
1.36      kristaps  929: {
                    930:
1.69      kristaps  931:        if ( ! check_msec(mdoc, n, 2, 3, 0))
1.36      kristaps  932:                return(0);
1.55      kristaps  933:        return(check_stdarg(mdoc, n));
1.36      kristaps  934: }
                    935:
                    936:
                    937: static int
1.55      kristaps  938: pre_ex(PRE_ARGS)
1.33      kristaps  939: {
1.35      kristaps  940:
1.69      kristaps  941:        if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
1.35      kristaps  942:                return(0);
1.55      kristaps  943:        return(check_stdarg(mdoc, n));
1.33      kristaps  944: }
                    945:
                    946:
                    947: static int
1.55      kristaps  948: pre_er(PRE_ARGS)
1.33      kristaps  949: {
                    950:
1.69      kristaps  951:        return(check_msec(mdoc, n, 2, 0));
1.33      kristaps  952: }
                    953:
                    954:
                    955: static int
1.55      kristaps  956: pre_cd(PRE_ARGS)
1.33      kristaps  957: {
                    958:
1.69      kristaps  959:        return(check_msec(mdoc, n, 4, 0));
1.33      kristaps  960: }
                    961:
                    962:
                    963: static int
1.55      kristaps  964: pre_prologue(PRE_ARGS)
1.20      kristaps  965: {
                    966:
1.69      kristaps  967:        return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
                    968: }
1.20      kristaps  969:
                    970:
1.69      kristaps  971: static int
                    972: pre_dt(PRE_ARGS)
                    973: {
                    974:
                    975:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.71      kristaps  976:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  977:                        return(0);
                    978:        if (mdoc->meta.title)
1.71      kristaps  979:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  980:                        return(0);
                    981:        return(1);
                    982: }
                    983:
                    984:
                    985: static int
                    986: pre_os(PRE_ARGS)
                    987: {
                    988:
                    989:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.71      kristaps  990:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  991:                        return(0);
                    992:        if (mdoc->meta.os)
1.71      kristaps  993:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  994:                        return(0);
                    995:        return(1);
                    996: }
1.20      kristaps  997:
                    998:
1.69      kristaps  999: static int
                   1000: pre_dd(PRE_ARGS)
                   1001: {
1.20      kristaps 1002:
1.69      kristaps 1003:        if (mdoc->meta.title || mdoc->meta.os)
1.71      kristaps 1004:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps 1005:                        return(0);
                   1006:        if (mdoc->meta.date)
1.71      kristaps 1007:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps 1008:                        return(0);
                   1009:        return(1);
1.20      kristaps 1010: }
                   1011:
                   1012:
1.35      kristaps 1013: static int
1.55      kristaps 1014: post_bf(POST_ARGS)
1.41      kristaps 1015: {
                   1016:        char             *p;
                   1017:        struct mdoc_node *head;
                   1018:
                   1019:        if (MDOC_BLOCK != mdoc->last->type)
                   1020:                return(1);
1.53      kristaps 1021:
1.71      kristaps 1022:        head = mdoc->last->head;
1.41      kristaps 1023:
1.71      kristaps 1024:        if (NULL == mdoc->last->args) {
                   1025:                if (NULL == head->child ||
                   1026:                                MDOC_TEXT != head->child->type)
                   1027:                        return(mdoc_err(mdoc, "text argument expected"));
1.53      kristaps 1028:
1.71      kristaps 1029:                p = head->child->string;
1.53      kristaps 1030:                if (xstrcmp(p, "Em"))
                   1031:                        return(1);
                   1032:                else if (xstrcmp(p, "Li"))
                   1033:                        return(1);
                   1034:                else if (xstrcmp(p, "Sm"))
                   1035:                        return(1);
                   1036:                return(mdoc_nerr(mdoc, head->child, "invalid font"));
1.41      kristaps 1037:        }
1.53      kristaps 1038:
1.41      kristaps 1039:        if (head->child)
1.71      kristaps 1040:                return(mdoc_err(mdoc, "one argument expected"));
1.53      kristaps 1041:
1.71      kristaps 1042:        return(1);
1.41      kristaps 1043: }
                   1044:
                   1045:
                   1046: static int
1.55      kristaps 1047: post_nm(POST_ARGS)
1.37      kristaps 1048: {
                   1049:
                   1050:        if (mdoc->last->child)
                   1051:                return(1);
                   1052:        if (mdoc->meta.name)
                   1053:                return(1);
1.71      kristaps 1054:        return(merr(mdoc, ENAME));
1.37      kristaps 1055: }
                   1056:
                   1057:
                   1058: static int
1.55      kristaps 1059: post_at(POST_ARGS)
1.36      kristaps 1060: {
                   1061:
1.37      kristaps 1062:        if (NULL == mdoc->last->child)
                   1063:                return(1);
1.71      kristaps 1064:        if (MDOC_TEXT != mdoc->last->child->type)
                   1065:                return(merr(mdoc, EATT));
                   1066:        if (mdoc_a2att(mdoc->last->child->string))
1.36      kristaps 1067:                return(1);
1.71      kristaps 1068:        return(merr(mdoc, EATT));
1.36      kristaps 1069: }
                   1070:
                   1071:
                   1072: static int
1.55      kristaps 1073: post_an(POST_ARGS)
1.35      kristaps 1074: {
                   1075:
1.71      kristaps 1076:        if (mdoc->last->args) {
1.35      kristaps 1077:                if (NULL == mdoc->last->child)
                   1078:                        return(1);
1.71      kristaps 1079:                return(merr(mdoc, ELINE));
1.35      kristaps 1080:        }
                   1081:
                   1082:        if (mdoc->last->child)
                   1083:                return(1);
1.71      kristaps 1084:        return(merr(mdoc, ELINE));
1.35      kristaps 1085: }
                   1086:
                   1087:
                   1088: static int
1.82    ! kristaps 1089: post_args(POST_ARGS)
1.35      kristaps 1090: {
                   1091:
1.71      kristaps 1092:        if (mdoc->last->args)
                   1093:                return(1);
                   1094:        return(merr(mdoc, ELINE));
1.35      kristaps 1095: }
                   1096:
                   1097:
1.25      kristaps 1098: static int
1.55      kristaps 1099: post_it(POST_ARGS)
1.25      kristaps 1100: {
1.71      kristaps 1101:        int               type, i, cols;
                   1102:        struct mdoc_node *n, *c;
1.25      kristaps 1103:
                   1104:        if (MDOC_BLOCK != mdoc->last->type)
                   1105:                return(1);
                   1106:
1.53      kristaps 1107:        n = mdoc->last->parent->parent;
1.71      kristaps 1108:        if (NULL == n->args)
                   1109:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1110:
1.26      kristaps 1111:        /* Some types require block-head, some not. */
1.25      kristaps 1112:
1.38      kristaps 1113:        /* LINTED */
1.71      kristaps 1114:        for (cols = type = -1, i = 0; -1 == type &&
                   1115:                        i < (int)n->args->argc; i++)
                   1116:                switch (n->args->argv[i].arg) {
1.25      kristaps 1117:                case (MDOC_Tag):
                   1118:                        /* FALLTHROUGH */
                   1119:                case (MDOC_Diag):
                   1120:                        /* FALLTHROUGH */
                   1121:                case (MDOC_Hang):
                   1122:                        /* FALLTHROUGH */
                   1123:                case (MDOC_Ohang):
                   1124:                        /* FALLTHROUGH */
                   1125:                case (MDOC_Inset):
1.71      kristaps 1126:                        /* FALLTHROUGH */
1.25      kristaps 1127:                case (MDOC_Bullet):
                   1128:                        /* FALLTHROUGH */
                   1129:                case (MDOC_Dash):
                   1130:                        /* FALLTHROUGH */
                   1131:                case (MDOC_Enum):
                   1132:                        /* FALLTHROUGH */
                   1133:                case (MDOC_Hyphen):
                   1134:                        /* FALLTHROUGH */
                   1135:                case (MDOC_Item):
1.71      kristaps 1136:                        type = n->args->argv[i].arg;
1.47      kristaps 1137:                        break;
1.25      kristaps 1138:                case (MDOC_Column):
1.71      kristaps 1139:                        type = n->args->argv[i].arg;
                   1140:                        cols = (int)n->args->argv[i].sz;
1.25      kristaps 1141:                        break;
                   1142:                default:
                   1143:                        break;
                   1144:                }
                   1145:
1.71      kristaps 1146:        if (-1 == type)
                   1147:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1148:
1.71      kristaps 1149:        switch (type) {
                   1150:        case (MDOC_Tag):
                   1151:                if (NULL == mdoc->last->head->child)
                   1152:                        if ( ! mwarn(mdoc, WLINE))
                   1153:                                return(0);
                   1154:                break;
                   1155:        case (MDOC_Hang):
                   1156:                /* FALLTHROUGH */
                   1157:        case (MDOC_Ohang):
                   1158:                /* FALLTHROUGH */
                   1159:        case (MDOC_Inset):
                   1160:                /* FALLTHROUGH */
                   1161:        case (MDOC_Diag):
                   1162:                if (NULL == mdoc->last->head->child)
                   1163:                        if ( ! mwarn(mdoc, WLINE))
1.25      kristaps 1164:                                return(0);
1.71      kristaps 1165:                if (NULL == mdoc->last->body->child)
                   1166:                        if ( ! mwarn(mdoc, WMULTILINE))
1.25      kristaps 1167:                                return(0);
1.71      kristaps 1168:                break;
                   1169:        case (MDOC_Bullet):
                   1170:                /* FALLTHROUGH */
                   1171:        case (MDOC_Dash):
                   1172:                /* FALLTHROUGH */
                   1173:        case (MDOC_Enum):
                   1174:                /* FALLTHROUGH */
                   1175:        case (MDOC_Hyphen):
                   1176:                /* FALLTHROUGH */
                   1177:        case (MDOC_Item):
                   1178:                if (mdoc->last->head->child)
                   1179:                        if ( ! mwarn(mdoc, WNOLINE))
1.47      kristaps 1180:                                return(0);
1.71      kristaps 1181:                if (NULL == mdoc->last->body->child)
                   1182:                        if ( ! mwarn(mdoc, WMULTILINE))
1.47      kristaps 1183:                                return(0);
1.71      kristaps 1184:                break;
                   1185:        case (MDOC_Column):
                   1186:                if (NULL == mdoc->last->head->child)
                   1187:                        if ( ! mwarn(mdoc, WLINE))
1.47      kristaps 1188:                                return(0);
1.71      kristaps 1189:                if (mdoc->last->body->child)
                   1190:                        if ( ! mwarn(mdoc, WNOMULTILINE))
1.47      kristaps 1191:                                return(0);
1.81      kristaps 1192:                c = mdoc->last->child;
1.80      kristaps 1193:                for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
1.71      kristaps 1194:                        i++;
                   1195:                if (i == cols)
                   1196:                        break;
1.81      kristaps 1197:                return(mdoc_err(mdoc, "column mismatch (have "
                   1198:                                        "%d, want %d)", i, cols));
1.71      kristaps 1199:        default:
                   1200:                break;
1.25      kristaps 1201:        }
                   1202:
1.71      kristaps 1203:        return(1);
1.25      kristaps 1204: }
                   1205:
                   1206:
1.24      kristaps 1207: static int
1.55      kristaps 1208: post_bl(POST_ARGS)
1.24      kristaps 1209: {
1.53      kristaps 1210:        struct mdoc_node        *n;
1.24      kristaps 1211:
                   1212:        if (MDOC_BODY != mdoc->last->type)
                   1213:                return(1);
1.71      kristaps 1214:        if (NULL == mdoc->last->child)
1.64      kristaps 1215:                return(1);
                   1216:
1.38      kristaps 1217:        /* LINTED */
1.24      kristaps 1218:        for (n = mdoc->last->child; n; n = n->next) {
                   1219:                if (MDOC_BLOCK == n->type)
1.25      kristaps 1220:                        if (MDOC_It == n->tok)
1.24      kristaps 1221:                                continue;
1.64      kristaps 1222:                return(mdoc_nerr(mdoc, n, "bad child of parent %s",
                   1223:                                mdoc_macronames[mdoc->last->tok]));
1.24      kristaps 1224:        }
1.53      kristaps 1225:
1.64      kristaps 1226:        return(1);
1.24      kristaps 1227: }
                   1228:
                   1229:
1.34      kristaps 1230: static int
1.37      kristaps 1231: ebool(struct mdoc *mdoc)
1.34      kristaps 1232: {
                   1233:        struct mdoc_node *n;
                   1234:
1.38      kristaps 1235:        /* LINTED */
1.34      kristaps 1236:        for (n = mdoc->last->child; n; n = n->next) {
                   1237:                if (MDOC_TEXT != n->type)
                   1238:                        break;
1.71      kristaps 1239:                if (xstrcmp(n->string, "on"))
1.34      kristaps 1240:                        continue;
1.71      kristaps 1241:                if (xstrcmp(n->string, "off"))
1.34      kristaps 1242:                        continue;
                   1243:                break;
                   1244:        }
1.53      kristaps 1245:
1.34      kristaps 1246:        if (NULL == n)
                   1247:                return(1);
1.71      kristaps 1248:        return(nerr(mdoc, n, EBOOL));
1.37      kristaps 1249: }
                   1250:
                   1251:
                   1252: static int
1.55      kristaps 1253: post_root(POST_ARGS)
1.37      kristaps 1254: {
                   1255:
1.46      kristaps 1256:        if (NULL == mdoc->first->child)
1.71      kristaps 1257:                return(merr(mdoc, ENODATA));
1.46      kristaps 1258:        if (SEC_PROLOGUE == mdoc->lastnamed)
1.71      kristaps 1259:                return(merr(mdoc, ENOPROLOGUE));
1.53      kristaps 1260:
1.46      kristaps 1261:        if (MDOC_BLOCK != mdoc->first->child->type)
1.71      kristaps 1262:                return(merr(mdoc, ENODATA));
1.46      kristaps 1263:        if (MDOC_Sh != mdoc->first->child->tok)
1.71      kristaps 1264:                return(merr(mdoc, ENODATA));
1.53      kristaps 1265:
1.37      kristaps 1266:        return(1);
1.34      kristaps 1267: }
                   1268:
                   1269:
1.20      kristaps 1270: static int
1.69      kristaps 1271: post_st(POST_ARGS)
                   1272: {
                   1273:
1.71      kristaps 1274:        if (mdoc_a2st(mdoc->last->child->string))
1.69      kristaps 1275:                return(1);
1.71      kristaps 1276:        return(mwarn(mdoc, WBADSTAND));
1.69      kristaps 1277: }
                   1278:
                   1279:
                   1280: static int
1.55      kristaps 1281: post_sh(POST_ARGS)
1.14      kristaps 1282: {
1.46      kristaps 1283:
                   1284:        if (MDOC_HEAD == mdoc->last->type)
                   1285:                return(post_sh_head(mdoc));
                   1286:        if (MDOC_BODY == mdoc->last->type)
                   1287:                return(post_sh_body(mdoc));
1.53      kristaps 1288:
1.46      kristaps 1289:        return(1);
                   1290: }
                   1291:
                   1292:
                   1293: static int
1.55      kristaps 1294: post_sh_body(POST_ARGS)
1.46      kristaps 1295: {
                   1296:        struct mdoc_node *n;
                   1297:
                   1298:        if (SEC_NAME != mdoc->lastnamed)
                   1299:                return(1);
                   1300:
1.51      kristaps 1301:        /*
                   1302:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1303:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1304:         * children of the BODY declaration can also be "text".
                   1305:         */
                   1306:
1.46      kristaps 1307:        if (NULL == (n = mdoc->last->child))
1.71      kristaps 1308:                return(mwarn(mdoc, WNAMESECINC));
1.51      kristaps 1309:
                   1310:        for ( ; n && n->next; n = n->next) {
                   1311:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1312:                        continue;
                   1313:                if (MDOC_TEXT == n->type)
                   1314:                        continue;
1.71      kristaps 1315:                if ( ! mwarn(mdoc, WNAMESECINC))
1.51      kristaps 1316:                        return(0);
                   1317:        }
                   1318:
                   1319:        if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1.46      kristaps 1320:                return(1);
1.71      kristaps 1321:        return(mwarn(mdoc, WNAMESECINC));
1.46      kristaps 1322: }
                   1323:
                   1324:
                   1325: static int
1.55      kristaps 1326: post_sh_head(POST_ARGS)
1.46      kristaps 1327: {
1.36      kristaps 1328:        char              buf[64];
1.21      kristaps 1329:        enum mdoc_sec     sec;
                   1330:
1.69      kristaps 1331:        /*
                   1332:         * Process a new section.  Sections are either "named" or
                   1333:         * "custom"; custom sections are user-defined, while named ones
                   1334:         * usually follow a conventional order and may only appear in
                   1335:         * certain manual sections.
                   1336:         */
                   1337:
1.25      kristaps 1338:        assert(MDOC_Sh == mdoc->last->tok);
1.21      kristaps 1339:
1.69      kristaps 1340:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.14      kristaps 1341:
1.46      kristaps 1342:        sec = mdoc_atosec(buf);
                   1343:
1.69      kristaps 1344:        /* The NAME section should always be first. */
                   1345:
1.46      kristaps 1346:        if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1.71      kristaps 1347:                return(mwarn(mdoc, WSECOOO));
1.46      kristaps 1348:        if (SEC_CUSTOM == sec)
1.21      kristaps 1349:                return(1);
1.69      kristaps 1350:
                   1351:        /* Check for repeated or out-of-order sections. */
                   1352:
1.46      kristaps 1353:        if (sec == mdoc->lastnamed)
1.71      kristaps 1354:                return(mwarn(mdoc, WSECREP));
1.46      kristaps 1355:        if (sec < mdoc->lastnamed)
1.71      kristaps 1356:                return(mwarn(mdoc, WSECOOO));
1.69      kristaps 1357:
                   1358:        /* Check particular section/manual section conventions. */
                   1359:
                   1360:        switch (sec) {
                   1361:        case (SEC_LIBRARY):
                   1362:                switch (mdoc->meta.msec) {
                   1363:                case (2):
                   1364:                        /* FALLTHROUGH */
                   1365:                case (3):
                   1366:                        break;
                   1367:                default:
1.71      kristaps 1368:                        return(mwarn(mdoc, WWRONGMSEC));
1.69      kristaps 1369:                }
                   1370:                break;
                   1371:        default:
                   1372:                break;
                   1373:        }
1.46      kristaps 1374:
                   1375:        return(1);
1.11      kristaps 1376: }
                   1377:
                   1378:
1.57      kristaps 1379: static int
1.69      kristaps 1380: pre_fd(PRE_ARGS)
1.11      kristaps 1381: {
1.39      kristaps 1382:
1.69      kristaps 1383:        return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1.11      kristaps 1384: }

CVSweb