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

Annotation of mandoc/mdoc_validate.c, Revision 1.21

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

CVSweb