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

Annotation of mandoc/validate.c, Revision 1.78

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

CVSweb