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

Annotation of mandoc/validate.c, Revision 1.76

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

CVSweb