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

Annotation of mandoc/validate.c, Revision 1.77

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

CVSweb