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

Annotation of mandoc/mdoc_validate.c, Revision 1.13

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

CVSweb