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

Annotation of mandoc/mdoc_validate.c, Revision 1.121

1.121   ! kristaps    1: /*     $Id: mdoc_validate.c,v 1.120 2010/10/11 13:24:33 kristaps Exp $ */
1.1       kristaps    2: /*
1.110     schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.5       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.5       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.56      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <sys/types.h>
                     22:
                     23: #include <assert.h>
                     24: #include <ctype.h>
1.35      kristaps   25: #include <limits.h>
1.81      kristaps   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
1.2       kristaps   28: #include <string.h>
1.1       kristaps   29:
1.79      kristaps   30: #include "mandoc.h"
1.1       kristaps   31: #include "libmdoc.h"
1.18      kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
                     34: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
                     35:
1.91      kristaps   36: #define        PRE_ARGS  struct mdoc *mdoc, struct mdoc_node *n
1.32      kristaps   37: #define        POST_ARGS struct mdoc *mdoc
1.1       kristaps   38:
1.120     kristaps   39: enum   check_ineq {
                     40:        CHECK_LT,
                     41:        CHECK_GT,
                     42:        CHECK_EQ
                     43: };
                     44:
                     45: enum   check_lvl {
                     46:        CHECK_WARN,
                     47:        CHECK_ERROR,
                     48:        CHECK_FATAL
                     49: };
                     50:
1.1       kristaps   51: typedef        int     (*v_pre)(PRE_ARGS);
                     52: typedef        int     (*v_post)(POST_ARGS);
                     53:
                     54: struct valids {
                     55:        v_pre   *pre;
                     56:        v_post  *post;
                     57: };
                     58:
1.120     kristaps   59: static int      check_count(struct mdoc *, enum mdoc_type,
                     60:                        enum check_lvl, enum check_ineq, int);
1.59      kristaps   61: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.32      kristaps   62: static int      check_stdarg(PRE_ARGS);
1.92      kristaps   63: static int      check_text(struct mdoc *, int, int, char *);
1.32      kristaps   64: static int      check_argv(struct mdoc *,
1.92      kristaps   65:                        struct mdoc_node *, struct mdoc_argv *);
                     66: static int      check_args(struct mdoc *, struct mdoc_node *);
1.32      kristaps   67:
1.120     kristaps   68: static int      ebool(POST_ARGS);
1.32      kristaps   69: static int      berr_ge1(POST_ARGS);
                     70: static int      bwarn_ge1(POST_ARGS);
1.120     kristaps   71: static int      eerr_eq0(POST_ARGS);
1.32      kristaps   72: static int      eerr_eq1(POST_ARGS);
                     73: static int      eerr_ge1(POST_ARGS);
1.46      kristaps   74: static int      eerr_le1(POST_ARGS);
1.116     kristaps   75: static int      ewarn_eq0(POST_ARGS);
1.32      kristaps   76: static int      ewarn_ge1(POST_ARGS);
                     77: static int      herr_eq0(POST_ARGS);
                     78: static int      herr_ge1(POST_ARGS);
1.120     kristaps   79: static int      hwarn_eq0(POST_ARGS);
1.32      kristaps   80: static int      hwarn_eq1(POST_ARGS);
                     81: static int      hwarn_le1(POST_ARGS);
                     82:
                     83: static int      post_an(POST_ARGS);
                     84: static int      post_at(POST_ARGS);
                     85: static int      post_bf(POST_ARGS);
                     86: static int      post_bl(POST_ARGS);
                     87: static int      post_bl_head(POST_ARGS);
1.85      kristaps   88: static int      post_dt(POST_ARGS);
1.32      kristaps   89: static int      post_it(POST_ARGS);
                     90: static int      post_lb(POST_ARGS);
                     91: static int      post_nm(POST_ARGS);
                     92: static int      post_root(POST_ARGS);
1.43      kristaps   93: static int      post_rs(POST_ARGS);
1.32      kristaps   94: static int      post_sh(POST_ARGS);
                     95: static int      post_sh_body(POST_ARGS);
                     96: static int      post_sh_head(POST_ARGS);
                     97: static int      post_st(POST_ARGS);
1.84      kristaps   98: static int      post_eoln(POST_ARGS);
1.57      kristaps   99: static int      post_vt(POST_ARGS);
1.32      kristaps  100: static int      pre_an(PRE_ARGS);
                    101: static int      pre_bd(PRE_ARGS);
                    102: static int      pre_bl(PRE_ARGS);
                    103: static int      pre_dd(PRE_ARGS);
                    104: static int      pre_display(PRE_ARGS);
                    105: static int      pre_dt(PRE_ARGS);
                    106: static int      pre_it(PRE_ARGS);
                    107: static int      pre_os(PRE_ARGS);
1.117     kristaps  108: static int      pre_pp(PRE_ARGS);
1.32      kristaps  109: static int      pre_rv(PRE_ARGS);
                    110: static int      pre_sh(PRE_ARGS);
                    111: static int      pre_ss(PRE_ARGS);
                    112:
                    113: static v_post   posts_an[] = { post_an, NULL };
                    114: static v_post   posts_at[] = { post_at, NULL };
1.101     schwarze  115: static v_post   posts_bd_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.32      kristaps  116: static v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
                    117: static v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
                    118: static v_post   posts_bool[] = { eerr_eq1, ebool, NULL };
1.84      kristaps  119: static v_post   posts_eoln[] = { post_eoln, NULL };
1.85      kristaps  120: static v_post   posts_dt[] = { post_dt, NULL };
1.32      kristaps  121: static v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
                    122: static v_post   posts_it[] = { post_it, NULL };
                    123: static v_post   posts_lb[] = { eerr_eq1, post_lb, NULL };
                    124: static v_post   posts_nd[] = { berr_ge1, NULL };
                    125: static v_post   posts_nm[] = { post_nm, NULL };
1.116     kristaps  126: static v_post   posts_notext[] = { ewarn_eq0, NULL };
1.44      kristaps  127: static v_post   posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
1.32      kristaps  128: static v_post   posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
1.46      kristaps  129: static v_post   posts_sp[] = { eerr_le1, NULL };
1.32      kristaps  130: static v_post   posts_ss[] = { herr_ge1, NULL };
                    131: static v_post   posts_st[] = { eerr_eq1, post_st, NULL };
                    132: static v_post   posts_text[] = { eerr_ge1, NULL };
1.51      kristaps  133: static v_post   posts_text1[] = { eerr_eq1, NULL };
1.57      kristaps  134: static v_post   posts_vt[] = { post_vt, NULL };
1.32      kristaps  135: static v_post   posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
                    136: static v_post   posts_wtext[] = { ewarn_ge1, NULL };
                    137: static v_pre    pres_an[] = { pre_an, NULL };
1.117     kristaps  138: static v_pre    pres_bd[] = { pre_display, pre_bd, pre_pp, NULL };
                    139: static v_pre    pres_bl[] = { pre_bl, pre_pp, NULL };
1.32      kristaps  140: static v_pre    pres_d1[] = { pre_display, NULL };
                    141: static v_pre    pres_dd[] = { pre_dd, NULL };
                    142: static v_pre    pres_dt[] = { pre_dt, NULL };
1.67      kristaps  143: static v_pre    pres_er[] = { NULL, NULL };
1.76      kristaps  144: static v_pre    pres_ex[] = { NULL, NULL };
1.77      kristaps  145: static v_pre    pres_fd[] = { NULL, NULL };
1.32      kristaps  146: static v_pre    pres_it[] = { pre_it, NULL };
                    147: static v_pre    pres_os[] = { pre_os, NULL };
1.117     kristaps  148: static v_pre    pres_pp[] = { pre_pp, NULL };
1.32      kristaps  149: static v_pre    pres_rv[] = { pre_rv, NULL };
                    150: static v_pre    pres_sh[] = { pre_sh, NULL };
                    151: static v_pre    pres_ss[] = { pre_ss, NULL };
1.1       kristaps  152:
                    153: const  struct valids mdoc_valids[MDOC_MAX] = {
1.10      kristaps  154:        { NULL, NULL },                         /* Ap */
1.114     kristaps  155:        { pres_dd, posts_wtext },               /* Dd */
1.85      kristaps  156:        { pres_dt, posts_dt },                  /* Dt */
1.1       kristaps  157:        { pres_os, NULL },                      /* Os */
                    158:        { pres_sh, posts_sh },                  /* Sh */
                    159:        { pres_ss, posts_ss },                  /* Ss */
1.117     kristaps  160:        { pres_pp, posts_notext },              /* Pp */
1.1       kristaps  161:        { pres_d1, posts_wline },               /* D1 */
                    162:        { pres_d1, posts_wline },               /* Dl */
1.104     kristaps  163:        { pres_bd, posts_bd_bk },               /* Bd */
1.1       kristaps  164:        { NULL, NULL },                         /* Ed */
                    165:        { pres_bl, posts_bl },                  /* Bl */
                    166:        { NULL, NULL },                         /* El */
                    167:        { pres_it, posts_it },                  /* It */
                    168:        { NULL, posts_text },                   /* Ad */
                    169:        { pres_an, posts_an },                  /* An */
                    170:        { NULL, NULL },                         /* Ar */
1.69      kristaps  171:        { NULL, posts_text },                   /* Cd */
1.1       kristaps  172:        { NULL, NULL },                         /* Cm */
                    173:        { NULL, NULL },                         /* Dv */
                    174:        { pres_er, posts_text },                /* Er */
                    175:        { NULL, NULL },                         /* Ev */
1.42      kristaps  176:        { pres_ex, NULL },                      /* Ex */
1.1       kristaps  177:        { NULL, NULL },                         /* Fa */
                    178:        { pres_fd, posts_wtext },               /* Fd */
                    179:        { NULL, NULL },                         /* Fl */
                    180:        { NULL, posts_text },                   /* Fn */
                    181:        { NULL, posts_wtext },                  /* Ft */
                    182:        { NULL, posts_text },                   /* Ic */
1.51      kristaps  183:        { NULL, posts_text1 },                  /* In */
1.1       kristaps  184:        { NULL, NULL },                         /* Li */
1.27      kristaps  185:        { NULL, posts_nd },                     /* Nd */
1.1       kristaps  186:        { NULL, posts_nm },                     /* Nm */
                    187:        { NULL, posts_wline },                  /* Op */
                    188:        { NULL, NULL },                         /* Ot */
                    189:        { NULL, NULL },                         /* Pa */
1.42      kristaps  190:        { pres_rv, NULL },                      /* Rv */
1.1       kristaps  191:        { NULL, posts_st },                     /* St */
                    192:        { NULL, NULL },                         /* Va */
1.57      kristaps  193:        { NULL, posts_vt },                     /* Vt */
1.84      kristaps  194:        { NULL, posts_wtext },                  /* Xr */
1.1       kristaps  195:        { NULL, posts_text },                   /* %A */
1.47      kristaps  196:        { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
1.54      kristaps  197:        { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1       kristaps  198:        { NULL, posts_text },                   /* %I */
                    199:        { NULL, posts_text },                   /* %J */
                    200:        { NULL, posts_text },                   /* %N */
                    201:        { NULL, posts_text },                   /* %O */
                    202:        { NULL, posts_text },                   /* %P */
                    203:        { NULL, posts_text },                   /* %R */
1.47      kristaps  204:        { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1       kristaps  205:        { NULL, posts_text },                   /* %V */
                    206:        { NULL, NULL },                         /* Ac */
                    207:        { NULL, NULL },                         /* Ao */
                    208:        { NULL, posts_wline },                  /* Aq */
                    209:        { NULL, posts_at },                     /* At */
                    210:        { NULL, NULL },                         /* Bc */
                    211:        { NULL, posts_bf },                     /* Bf */
                    212:        { NULL, NULL },                         /* Bo */
                    213:        { NULL, posts_wline },                  /* Bq */
                    214:        { NULL, NULL },                         /* Bsx */
                    215:        { NULL, NULL },                         /* Bx */
                    216:        { NULL, posts_bool },                   /* Db */
                    217:        { NULL, NULL },                         /* Dc */
                    218:        { NULL, NULL },                         /* Do */
                    219:        { NULL, posts_wline },                  /* Dq */
                    220:        { NULL, NULL },                         /* Ec */
                    221:        { NULL, NULL },                         /* Ef */
                    222:        { NULL, NULL },                         /* Em */
                    223:        { NULL, NULL },                         /* Eo */
                    224:        { NULL, NULL },                         /* Fx */
                    225:        { NULL, posts_text },                   /* Ms */
                    226:        { NULL, posts_notext },                 /* No */
                    227:        { NULL, posts_notext },                 /* Ns */
                    228:        { NULL, NULL },                         /* Nx */
                    229:        { NULL, NULL },                         /* Ox */
                    230:        { NULL, NULL },                         /* Pc */
1.51      kristaps  231:        { NULL, posts_text1 },                  /* Pf */
1.1       kristaps  232:        { NULL, NULL },                         /* Po */
                    233:        { NULL, posts_wline },                  /* Pq */
                    234:        { NULL, NULL },                         /* Qc */
                    235:        { NULL, posts_wline },                  /* Ql */
                    236:        { NULL, NULL },                         /* Qo */
                    237:        { NULL, posts_wline },                  /* Qq */
                    238:        { NULL, NULL },                         /* Re */
1.43      kristaps  239:        { NULL, posts_rs },                     /* Rs */
1.1       kristaps  240:        { NULL, NULL },                         /* Sc */
                    241:        { NULL, NULL },                         /* So */
                    242:        { NULL, posts_wline },                  /* Sq */
                    243:        { NULL, posts_bool },                   /* Sm */
                    244:        { NULL, posts_text },                   /* Sx */
                    245:        { NULL, posts_text },                   /* Sy */
                    246:        { NULL, posts_text },                   /* Tn */
                    247:        { NULL, NULL },                         /* Ux */
                    248:        { NULL, NULL },                         /* Xc */
                    249:        { NULL, NULL },                         /* Xo */
                    250:        { NULL, posts_fo },                     /* Fo */
                    251:        { NULL, NULL },                         /* Fc */
                    252:        { NULL, NULL },                         /* Oo */
                    253:        { NULL, NULL },                         /* Oc */
1.101     schwarze  254:        { NULL, posts_bd_bk },                  /* Bk */
1.1       kristaps  255:        { NULL, NULL },                         /* Ek */
1.84      kristaps  256:        { NULL, posts_eoln },                   /* Bt */
1.1       kristaps  257:        { NULL, NULL },                         /* Hf */
                    258:        { NULL, NULL },                         /* Fr */
1.84      kristaps  259:        { NULL, posts_eoln },                   /* Ud */
1.83      kristaps  260:        { NULL, posts_lb },                     /* Lb */
1.34      kristaps  261:        { NULL, posts_notext },                 /* Lp */
1.51      kristaps  262:        { NULL, posts_text },                   /* Lk */
1.1       kristaps  263:        { NULL, posts_text },                   /* Mt */
                    264:        { NULL, posts_wline },                  /* Brq */
                    265:        { NULL, NULL },                         /* Bro */
                    266:        { NULL, NULL },                         /* Brc */
                    267:        { NULL, posts_text },                   /* %C */
                    268:        { NULL, NULL },                         /* Es */
                    269:        { NULL, NULL },                         /* En */
                    270:        { NULL, NULL },                         /* Dx */
                    271:        { NULL, posts_text },                   /* %Q */
1.33      kristaps  272:        { NULL, posts_notext },                 /* br */
1.119     schwarze  273:        { pres_pp, posts_sp },                  /* sp */
1.51      kristaps  274:        { NULL, posts_text1 },                  /* %U */
1.88      kristaps  275:        { NULL, NULL },                         /* Ta */
1.1       kristaps  276: };
                    277:
                    278:
                    279: int
1.91      kristaps  280: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1       kristaps  281: {
                    282:        v_pre           *p;
                    283:        int              line, pos;
1.92      kristaps  284:        char            *tp;
1.1       kristaps  285:
                    286:        if (MDOC_TEXT == n->type) {
                    287:                tp = n->string;
                    288:                line = n->line;
                    289:                pos = n->pos;
                    290:                return(check_text(mdoc, line, pos, tp));
                    291:        }
                    292:
                    293:        if ( ! check_args(mdoc, n))
                    294:                return(0);
                    295:        if (NULL == mdoc_valids[n->tok].pre)
                    296:                return(1);
                    297:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    298:                if ( ! (*p)(mdoc, n))
                    299:                        return(0);
                    300:        return(1);
                    301: }
                    302:
                    303:
                    304: int
                    305: mdoc_valid_post(struct mdoc *mdoc)
                    306: {
                    307:        v_post          *p;
                    308:
                    309:        if (MDOC_VALID & mdoc->last->flags)
                    310:                return(1);
                    311:        mdoc->last->flags |= MDOC_VALID;
                    312:
                    313:        if (MDOC_TEXT == mdoc->last->type)
                    314:                return(1);
                    315:        if (MDOC_ROOT == mdoc->last->type)
                    316:                return(post_root(mdoc));
                    317:
                    318:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    319:                return(1);
                    320:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    321:                if ( ! (*p)(mdoc))
                    322:                        return(0);
                    323:
                    324:        return(1);
                    325: }
                    326:
1.120     kristaps  327: static int
                    328: check_count(struct mdoc *m, enum mdoc_type type,
                    329:                enum check_lvl lvl, enum check_ineq ineq, int val)
                    330: {
                    331:        const char      *p;
                    332:
                    333:        if (m->last->type != type)
                    334:                return(1);
                    335:
                    336:        switch (ineq) {
                    337:        case (CHECK_LT):
                    338:                p = "less than ";
                    339:                if (m->last->nchild < val)
                    340:                        return(1);
                    341:                break;
                    342:        case (CHECK_GT):
                    343:                p = "greater than ";
                    344:                if (m->last->nchild > val)
                    345:                        return(1);
                    346:                break;
                    347:        case (CHECK_EQ):
                    348:                p = "";
                    349:                if (val == m->last->nchild)
                    350:                        return(1);
                    351:                break;
                    352:        }
                    353:
                    354:        if (CHECK_WARN == lvl) {
                    355:                return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
                    356:                                m->last->line, m->last->pos,
                    357:                                "want %s%d children (have %d)",
                    358:                                p, val, m->last->nchild));
                    359:        }
                    360:
                    361:        return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
                    362:                        m->last->line, m->last->pos,
                    363:                        "require %s%d children (have %d)",
                    364:                        p, val, m->last->nchild));
                    365: }
                    366:
                    367: static int
                    368: berr_ge1(POST_ARGS)
                    369: {
                    370:
                    371:        return(check_count(mdoc, MDOC_BODY, CHECK_FATAL, CHECK_GT, 0));
                    372: }
                    373:
                    374: static int
                    375: bwarn_ge1(POST_ARGS)
                    376: {
                    377:        return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
                    378: }
                    379:
                    380: static int
                    381: eerr_eq0(POST_ARGS)
                    382: {
                    383:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 0));
                    384: }
1.1       kristaps  385:
1.120     kristaps  386: static int
                    387: eerr_eq1(POST_ARGS)
1.1       kristaps  388: {
1.120     kristaps  389:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 1));
                    390: }
1.1       kristaps  391:
1.120     kristaps  392: static int
                    393: eerr_ge1(POST_ARGS)
                    394: {
                    395:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_GT, 0));
1.1       kristaps  396: }
                    397:
1.120     kristaps  398: static int
                    399: eerr_le1(POST_ARGS)
                    400: {
                    401:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_LT, 2));
                    402: }
1.1       kristaps  403:
1.120     kristaps  404: static int
                    405: ewarn_eq0(POST_ARGS)
1.1       kristaps  406: {
1.120     kristaps  407:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
                    408: }
1.1       kristaps  409:
1.120     kristaps  410: static int
                    411: ewarn_ge1(POST_ARGS)
                    412: {
                    413:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1       kristaps  414: }
                    415:
1.120     kristaps  416: static int
                    417: herr_eq0(POST_ARGS)
                    418: {
                    419:        return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_EQ, 0));
                    420: }
1.1       kristaps  421:
1.120     kristaps  422: static int
                    423: herr_ge1(POST_ARGS)
                    424: {
                    425:        return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_GT, 0));
                    426: }
1.1       kristaps  427:
1.120     kristaps  428: static int
                    429: hwarn_eq0(POST_ARGS)
                    430: {
                    431:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.1       kristaps  432: }
                    433:
1.120     kristaps  434: static int
                    435: hwarn_eq1(POST_ARGS)
                    436: {
                    437:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
                    438: }
1.1       kristaps  439:
1.120     kristaps  440: static int
                    441: hwarn_le1(POST_ARGS)
                    442: {
                    443:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
                    444: }
1.1       kristaps  445:
                    446:
                    447: static int
                    448: check_stdarg(PRE_ARGS)
                    449: {
                    450:
                    451:        if (n->args && 1 == n->args->argc)
                    452:                if (MDOC_Std == n->args->argv[0].arg)
                    453:                        return(1);
1.79      kristaps  454:        return(mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV));
1.1       kristaps  455: }
                    456:
                    457:
                    458: static int
1.92      kristaps  459: check_args(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  460: {
                    461:        int              i;
                    462:
                    463:        if (NULL == n->args)
                    464:                return(1);
                    465:
                    466:        assert(n->args->argc);
                    467:        for (i = 0; i < (int)n->args->argc; i++)
                    468:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    469:                        return(0);
                    470:
                    471:        return(1);
                    472: }
                    473:
                    474:
                    475: static int
1.92      kristaps  476: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1       kristaps  477: {
                    478:        int              i;
                    479:
                    480:        for (i = 0; i < (int)v->sz; i++)
                    481:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    482:                        return(0);
                    483:
                    484:        if (MDOC_Std == v->arg) {
                    485:                if (v->sz || m->meta.name)
                    486:                        return(1);
1.79      kristaps  487:                if ( ! mdoc_nmsg(m, n, MANDOCERR_NONAME))
                    488:                        return(0);
1.1       kristaps  489:        }
                    490:
                    491:        return(1);
                    492: }
                    493:
                    494:
                    495: static int
1.112     kristaps  496: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1       kristaps  497: {
1.18      kristaps  498:        int              c;
1.112     kristaps  499:        size_t           sz;
1.102     kristaps  500:
1.112     kristaps  501:        for ( ; *p; p++, pos++) {
                    502:                sz = strcspn(p, "\t\\");
                    503:                p += (int)sz;
                    504:
                    505:                if ('\0' == *p)
                    506:                        break;
                    507:
                    508:                pos += (int)sz;
1.1       kristaps  509:
                    510:                if ('\t' == *p) {
1.112     kristaps  511:                        if (MDOC_LITERAL & m->flags)
                    512:                                continue;
                    513:                        if (mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB))
                    514:                                continue;
                    515:                        return(0);
                    516:                }
1.1       kristaps  517:
1.112     kristaps  518:                /* Check the special character. */
1.1       kristaps  519:
1.18      kristaps  520:                c = mandoc_special(p);
1.1       kristaps  521:                if (c) {
1.18      kristaps  522:                        p += c - 1;
                    523:                        pos += c - 1;
1.115     schwarze  524:                } else
                    525:                        mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
1.1       kristaps  526:        }
                    527:
                    528:        return(1);
                    529: }
                    530:
                    531:
                    532: static int
1.59      kristaps  533: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1       kristaps  534: {
                    535:
                    536:        assert(n->parent);
                    537:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    538:                        (t == n->parent->type))
                    539:                return(1);
                    540:
1.79      kristaps  541:        mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
                    542:                                n->line, n->pos, "want parent %s",
                    543:                                MDOC_ROOT == t ? "<root>" :
                    544:                                        mdoc_macronames[tok]);
                    545:        return(0);
1.1       kristaps  546: }
                    547:
                    548:
                    549: static int
                    550: pre_display(PRE_ARGS)
                    551: {
                    552:        struct mdoc_node *node;
                    553:
                    554:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    555:
                    556:        if (MDOC_BLOCK != n->type)
                    557:                return(1);
                    558:
                    559:        /* LINTED */
                    560:        for (node = mdoc->last->parent; node; node = node->parent)
                    561:                if (MDOC_BLOCK == node->type)
                    562:                        if (MDOC_Bd == node->tok)
                    563:                                break;
                    564:        if (NULL == node)
                    565:                return(1);
                    566:
1.79      kristaps  567:        mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
                    568:        return(0);
1.1       kristaps  569: }
                    570:
                    571:
                    572: static int
                    573: pre_bl(PRE_ARGS)
                    574: {
1.104     kristaps  575:        int               i, comp, dup;
                    576:        const char       *offs, *width;
                    577:        enum mdoc_list    lt;
                    578:        struct mdoc_node *np;
1.1       kristaps  579:
1.91      kristaps  580:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  581:                if (ENDBODY_NOT != n->end) {
                    582:                        assert(n->pending);
                    583:                        np = n->pending->parent;
                    584:                } else
                    585:                        np = n->parent;
                    586:
                    587:                assert(np);
                    588:                assert(MDOC_BLOCK == np->type);
                    589:                assert(MDOC_Bl == np->tok);
                    590:                assert(np->data.Bl);
                    591:                n->data.Bl = np->data.Bl;
1.1       kristaps  592:                return(1);
1.79      kristaps  593:        }
1.1       kristaps  594:
1.91      kristaps  595:        /*
                    596:         * First figure out which kind of list to use: bind ourselves to
                    597:         * the first mentioned list type and warn about any remaining
                    598:         * ones.  If we find no list type, we default to LIST_item.
                    599:         */
1.1       kristaps  600:
1.104     kristaps  601:        assert(NULL == n->data.Bl);
                    602:        n->data.Bl = mandoc_calloc(1, sizeof(struct mdoc_bl));
1.1       kristaps  603:
                    604:        /* LINTED */
1.91      kristaps  605:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    606:                lt = LIST__NONE;
1.97      kristaps  607:                dup = comp = 0;
1.99      kristaps  608:                width = offs = NULL;
1.91      kristaps  609:                switch (n->args->argv[i].arg) {
                    610:                /* Set list types. */
1.1       kristaps  611:                case (MDOC_Bullet):
1.91      kristaps  612:                        lt = LIST_bullet;
                    613:                        break;
1.1       kristaps  614:                case (MDOC_Dash):
1.91      kristaps  615:                        lt = LIST_dash;
                    616:                        break;
1.1       kristaps  617:                case (MDOC_Enum):
1.91      kristaps  618:                        lt = LIST_enum;
                    619:                        break;
1.1       kristaps  620:                case (MDOC_Hyphen):
1.91      kristaps  621:                        lt = LIST_hyphen;
                    622:                        break;
1.1       kristaps  623:                case (MDOC_Item):
1.91      kristaps  624:                        lt = LIST_item;
                    625:                        break;
1.1       kristaps  626:                case (MDOC_Tag):
1.91      kristaps  627:                        lt = LIST_tag;
                    628:                        break;
1.1       kristaps  629:                case (MDOC_Diag):
1.91      kristaps  630:                        lt = LIST_diag;
                    631:                        break;
1.1       kristaps  632:                case (MDOC_Hang):
1.91      kristaps  633:                        lt = LIST_hang;
                    634:                        break;
1.1       kristaps  635:                case (MDOC_Ohang):
1.91      kristaps  636:                        lt = LIST_ohang;
                    637:                        break;
1.1       kristaps  638:                case (MDOC_Inset):
1.91      kristaps  639:                        lt = LIST_inset;
                    640:                        break;
1.1       kristaps  641:                case (MDOC_Column):
1.91      kristaps  642:                        lt = LIST_column;
                    643:                        break;
                    644:                /* Set list arguments. */
1.45      kristaps  645:                case (MDOC_Compact):
1.104     kristaps  646:                        dup = n->data.Bl->comp;
1.97      kristaps  647:                        comp = 1;
1.91      kristaps  648:                        break;
1.1       kristaps  649:                case (MDOC_Width):
1.104     kristaps  650:                        dup = (NULL != n->data.Bl->width);
1.99      kristaps  651:                        width = n->args->argv[i].value[0];
1.8       kristaps  652:                        break;
1.1       kristaps  653:                case (MDOC_Offset):
1.98      kristaps  654:                        /* NB: this can be empty! */
                    655:                        if (n->args->argv[i].sz) {
                    656:                                offs = n->args->argv[i].value[0];
1.104     kristaps  657:                                dup = (NULL != n->data.Bl->offs);
1.98      kristaps  658:                                break;
                    659:                        }
                    660:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
                    661:                                return(0);
1.1       kristaps  662:                        break;
1.113     kristaps  663:                default:
                    664:                        continue;
1.1       kristaps  665:                }
                    666:
1.91      kristaps  667:                /* Check: duplicate auxiliary arguments. */
                    668:
1.97      kristaps  669:                if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
                    670:                        return(0);
                    671:
                    672:                if (comp && ! dup)
1.104     kristaps  673:                        n->data.Bl->comp = comp;
1.98      kristaps  674:                if (offs && ! dup)
1.104     kristaps  675:                        n->data.Bl->offs = offs;
1.99      kristaps  676:                if (width && ! dup)
1.104     kristaps  677:                        n->data.Bl->width = width;
1.91      kristaps  678:
                    679:                /* Check: multiple list types. */
                    680:
1.104     kristaps  681:                if (LIST__NONE != lt && n->data.Bl->type != LIST__NONE)
1.91      kristaps  682:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP))
                    683:                                return(0);
                    684:
                    685:                /* Assign list type. */
                    686:
1.109     kristaps  687:                if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
1.104     kristaps  688:                        n->data.Bl->type = lt;
1.109     kristaps  689:                        /* Set column information, too. */
                    690:                        if (LIST_column == lt) {
                    691:                                n->data.Bl->ncols =
                    692:                                        n->args->argv[i].sz;
                    693:                                n->data.Bl->cols = (const char **)
                    694:                                        n->args->argv[i].value;
                    695:                        }
                    696:                }
1.91      kristaps  697:
                    698:                /* The list type should come first. */
                    699:
1.104     kristaps  700:                if (n->data.Bl->type == LIST__NONE)
                    701:                        if (n->data.Bl->width ||
                    702:                                        n->data.Bl->offs ||
                    703:                                        n->data.Bl->comp)
1.91      kristaps  704:                                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
                    705:                                        return(0);
                    706:
                    707:                continue;
                    708:        }
                    709:
                    710:        /* Allow lists to default to LIST_item. */
                    711:
1.104     kristaps  712:        if (LIST__NONE == n->data.Bl->type) {
1.91      kristaps  713:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE))
                    714:                        return(0);
1.104     kristaps  715:                n->data.Bl->type = LIST_item;
1.79      kristaps  716:        }
1.1       kristaps  717:
1.8       kristaps  718:        /*
                    719:         * Validate the width field.  Some list types don't need width
                    720:         * types and should be warned about them.  Others should have it
                    721:         * and must also be warned.
                    722:         */
                    723:
1.104     kristaps  724:        switch (n->data.Bl->type) {
1.91      kristaps  725:        case (LIST_tag):
1.104     kristaps  726:                if (n->data.Bl->width)
1.91      kristaps  727:                        break;
                    728:                if (mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG))
                    729:                        break;
                    730:                return(0);
                    731:        case (LIST_column):
1.1       kristaps  732:                /* FALLTHROUGH */
1.91      kristaps  733:        case (LIST_diag):
1.1       kristaps  734:                /* FALLTHROUGH */
1.91      kristaps  735:        case (LIST_ohang):
1.53      kristaps  736:                /* FALLTHROUGH */
1.91      kristaps  737:        case (LIST_inset):
1.1       kristaps  738:                /* FALLTHROUGH */
1.91      kristaps  739:        case (LIST_item):
1.104     kristaps  740:                if (NULL == n->data.Bl->width)
1.91      kristaps  741:                        break;
                    742:                if (mdoc_nmsg(mdoc, n, MANDOCERR_WIDTHARG))
                    743:                        break;
                    744:                return(0);
1.8       kristaps  745:        default:
                    746:                break;
                    747:        }
                    748:
1.1       kristaps  749:        return(1);
                    750: }
                    751:
                    752:
                    753: static int
                    754: pre_bd(PRE_ARGS)
                    755: {
1.104     kristaps  756:        int               i, dup, comp;
                    757:        enum mdoc_disp    dt;
                    758:        const char       *offs;
                    759:        struct mdoc_node *np;
1.1       kristaps  760:
1.93      kristaps  761:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  762:                if (ENDBODY_NOT != n->end) {
                    763:                        assert(n->pending);
                    764:                        np = n->pending->parent;
                    765:                } else
                    766:                        np = n->parent;
                    767:
                    768:                assert(np);
                    769:                assert(MDOC_BLOCK == np->type);
                    770:                assert(MDOC_Bd == np->tok);
                    771:                assert(np->data.Bd);
                    772:                n->data.Bd = np->data.Bd;
1.1       kristaps  773:                return(1);
1.79      kristaps  774:        }
1.1       kristaps  775:
1.104     kristaps  776:        assert(NULL == n->data.Bd);
                    777:        n->data.Bd = mandoc_calloc(1, sizeof(struct mdoc_bd));
1.1       kristaps  778:
                    779:        /* LINTED */
1.93      kristaps  780:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    781:                dt = DISP__NONE;
1.94      kristaps  782:                dup = comp = 0;
                    783:                offs = NULL;
                    784:
1.1       kristaps  785:                switch (n->args->argv[i].arg) {
1.49      kristaps  786:                case (MDOC_Centred):
1.93      kristaps  787:                        dt = DISP_centred;
                    788:                        break;
1.1       kristaps  789:                case (MDOC_Ragged):
1.93      kristaps  790:                        dt = DISP_ragged;
                    791:                        break;
1.1       kristaps  792:                case (MDOC_Unfilled):
1.93      kristaps  793:                        dt = DISP_unfilled;
                    794:                        break;
1.1       kristaps  795:                case (MDOC_Filled):
1.93      kristaps  796:                        dt = DISP_filled;
                    797:                        break;
1.1       kristaps  798:                case (MDOC_Literal):
1.93      kristaps  799:                        dt = DISP_literal;
1.79      kristaps  800:                        break;
1.93      kristaps  801:                case (MDOC_File):
                    802:                        mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
                    803:                        return(0);
                    804:                case (MDOC_Offset):
1.94      kristaps  805:                        /* NB: this can be empty! */
                    806:                        if (n->args->argv[i].sz) {
                    807:                                offs = n->args->argv[i].value[0];
1.104     kristaps  808:                                dup = (NULL != n->data.Bd->offs);
1.94      kristaps  809:                                break;
                    810:                        }
1.95      kristaps  811:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
                    812:                                return(0);
1.94      kristaps  813:                        break;
1.93      kristaps  814:                case (MDOC_Compact):
1.94      kristaps  815:                        comp = 1;
1.104     kristaps  816:                        dup = n->data.Bd->comp;
1.94      kristaps  817:                        break;
1.1       kristaps  818:                default:
1.94      kristaps  819:                        abort();
                    820:                        /* NOTREACHED */
1.1       kristaps  821:                }
                    822:
1.94      kristaps  823:                /* Check whether we have duplicates. */
                    824:
                    825:                if (dup && ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
                    826:                        return(0);
                    827:
                    828:                /* Make our auxiliary assignments. */
                    829:
                    830:                if (offs && ! dup)
1.104     kristaps  831:                        n->data.Bd->offs = offs;
1.94      kristaps  832:                if (comp && ! dup)
1.104     kristaps  833:                        n->data.Bd->comp = comp;
1.94      kristaps  834:
                    835:                /* Check whether a type has already been assigned. */
                    836:
1.104     kristaps  837:                if (DISP__NONE != dt && n->data.Bd->type != DISP__NONE)
1.93      kristaps  838:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP))
                    839:                                return(0);
                    840:
1.94      kristaps  841:                /* Make our type assignment. */
                    842:
1.104     kristaps  843:                if (DISP__NONE != dt && n->data.Bd->type == DISP__NONE)
                    844:                        n->data.Bd->type = dt;
1.93      kristaps  845:        }
                    846:
1.104     kristaps  847:        if (DISP__NONE == n->data.Bd->type) {
1.93      kristaps  848:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE))
                    849:                        return(0);
1.104     kristaps  850:                n->data.Bd->type = DISP_ragged;
1.93      kristaps  851:        }
                    852:
                    853:        return(1);
1.1       kristaps  854: }
                    855:
                    856:
                    857: static int
                    858: pre_ss(PRE_ARGS)
                    859: {
                    860:
                    861:        if (MDOC_BLOCK != n->type)
                    862:                return(1);
                    863:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    864: }
                    865:
                    866:
                    867: static int
                    868: pre_sh(PRE_ARGS)
                    869: {
                    870:
                    871:        if (MDOC_BLOCK != n->type)
                    872:                return(1);
1.100     kristaps  873:
                    874:        mdoc->regs->regs[(int)REG_nS].set = 0;
1.70      kristaps  875:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  876: }
                    877:
                    878:
                    879: static int
                    880: pre_it(PRE_ARGS)
                    881: {
                    882:
                    883:        if (MDOC_BLOCK != n->type)
                    884:                return(1);
1.79      kristaps  885:        /*
                    886:         * FIXME: this can probably be lifted if we make the It into
                    887:         * something else on-the-fly?
                    888:         */
1.1       kristaps  889:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    890: }
                    891:
                    892:
                    893: static int
                    894: pre_an(PRE_ARGS)
                    895: {
1.121   ! kristaps  896:        int              i;
1.1       kristaps  897:
1.107     kristaps  898:        if (NULL == n->args)
1.1       kristaps  899:                return(1);
1.121   ! kristaps  900:
        !           901:        for (i = 1; i < (int)n->args->argc; i++)
        !           902:                if ( ! mdoc_pmsg(mdoc, n->args->argv[i].line,
        !           903:                        n->args->argv[i].pos, MANDOCERR_IGNARGV))
1.107     kristaps  904:                        return(0);
1.120     kristaps  905:
1.107     kristaps  906:        if (MDOC_Split == n->args->argv[0].arg)
                    907:                n->data.An.auth = AUTH_split;
                    908:        else if (MDOC_Nosplit == n->args->argv[0].arg)
                    909:                n->data.An.auth = AUTH_nosplit;
                    910:        else
                    911:                abort();
                    912:
                    913:        return(1);
1.1       kristaps  914: }
                    915:
                    916:
                    917: static int
                    918: pre_rv(PRE_ARGS)
                    919: {
                    920:
                    921:        return(check_stdarg(mdoc, n));
                    922: }
                    923:
                    924:
                    925: static int
1.85      kristaps  926: post_dt(POST_ARGS)
                    927: {
                    928:        const struct mdoc_node *nn;
                    929:        const char      *p;
                    930:
                    931:        if (NULL != (nn = mdoc->last->child))
                    932:                for (p = nn->string; *p; p++) {
1.86      kristaps  933:                        if (toupper((u_char)*p) == *p)
1.85      kristaps  934:                                continue;
                    935:                        if ( ! mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE))
                    936:                                return(0);
                    937:                        break;
                    938:                }
                    939:
                    940:        return(1);
                    941: }
                    942:
                    943:
                    944: static int
1.1       kristaps  945: pre_dt(PRE_ARGS)
                    946: {
1.54      kristaps  947:
1.1       kristaps  948:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.79      kristaps  949:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  950:                        return(0);
                    951:        if (mdoc->meta.title)
1.79      kristaps  952:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  953:                        return(0);
                    954:        return(1);
                    955: }
                    956:
                    957:
                    958: static int
                    959: pre_os(PRE_ARGS)
                    960: {
                    961:
                    962:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.79      kristaps  963:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  964:                        return(0);
                    965:        if (mdoc->meta.os)
1.79      kristaps  966:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  967:                        return(0);
                    968:        return(1);
                    969: }
                    970:
                    971:
                    972: static int
                    973: pre_dd(PRE_ARGS)
                    974: {
                    975:
                    976:        if (mdoc->meta.title || mdoc->meta.os)
1.79      kristaps  977:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
1.1       kristaps  978:                        return(0);
                    979:        if (mdoc->meta.date)
1.79      kristaps  980:                if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
1.1       kristaps  981:                        return(0);
                    982:        return(1);
                    983: }
                    984:
                    985:
                    986: static int
                    987: post_bf(POST_ARGS)
                    988: {
1.105     kristaps  989:        struct mdoc_node *np;
1.113     kristaps  990:        enum mdocargt     arg;
1.105     kristaps  991:
                    992:        /*
                    993:         * Unlike other data pointers, these are "housed" by the HEAD
                    994:         * element, which contains the goods.
                    995:         */
1.1       kristaps  996:
1.105     kristaps  997:        if (MDOC_HEAD != mdoc->last->type) {
                    998:                if (ENDBODY_NOT != mdoc->last->end) {
                    999:                        assert(mdoc->last->pending);
                   1000:                        np = mdoc->last->pending->parent->head;
                   1001:                } else if (MDOC_BLOCK != mdoc->last->type) {
                   1002:                        np = mdoc->last->parent->head;
                   1003:                } else
                   1004:                        np = mdoc->last->head;
                   1005:
                   1006:                assert(np);
                   1007:                assert(MDOC_HEAD == np->type);
                   1008:                assert(MDOC_Bf == np->tok);
                   1009:                assert(np->data.Bf);
                   1010:                mdoc->last->data.Bf = np->data.Bf;
1.1       kristaps 1011:                return(1);
1.105     kristaps 1012:        }
1.1       kristaps 1013:
1.105     kristaps 1014:        np = mdoc->last;
1.106     kristaps 1015:        assert(MDOC_BLOCK == np->parent->type);
                   1016:        assert(MDOC_Bf == np->parent->tok);
1.105     kristaps 1017:        np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
1.1       kristaps 1018:
1.105     kristaps 1019:        /*
                   1020:         * Cannot have both argument and parameter.
                   1021:         * If neither is specified, let it through with a warning.
                   1022:         */
                   1023:
1.106     kristaps 1024:        if (np->parent->args && np->child) {
1.105     kristaps 1025:                mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.79      kristaps 1026:                return(0);
1.106     kristaps 1027:        } else if (NULL == np->parent->args && NULL == np->child)
1.105     kristaps 1028:                return(mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE));
                   1029:
                   1030:        /* Extract argument into data. */
                   1031:
1.106     kristaps 1032:        if (np->parent->args) {
                   1033:                arg = np->parent->args->argv[0].arg;
1.105     kristaps 1034:                if (MDOC_Emphasis == arg)
                   1035:                        np->data.Bf->font = FONT_Em;
                   1036:                else if (MDOC_Literal == arg)
                   1037:                        np->data.Bf->font = FONT_Li;
                   1038:                else if (MDOC_Symbolic == arg)
                   1039:                        np->data.Bf->font = FONT_Sy;
                   1040:                else
                   1041:                        abort();
1.13      kristaps 1042:                return(1);
1.79      kristaps 1043:        }
1.1       kristaps 1044:
1.105     kristaps 1045:        /* Extract parameter into data. */
1.1       kristaps 1046:
1.105     kristaps 1047:        if (0 == strcmp(np->child->string, "Em"))
                   1048:                np->data.Bf->font = FONT_Em;
                   1049:        else if (0 == strcmp(np->child->string, "Li"))
                   1050:                np->data.Bf->font = FONT_Li;
                   1051:        else if (0 == strcmp(np->child->string, "Sy"))
                   1052:                np->data.Bf->font = FONT_Sy;
                   1053:        else if ( ! mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE))
                   1054:                return(0);
1.1       kristaps 1055:
1.105     kristaps 1056:        return(1);
1.1       kristaps 1057: }
                   1058:
                   1059:
                   1060: static int
1.31      kristaps 1061: post_lb(POST_ARGS)
                   1062: {
                   1063:
                   1064:        if (mdoc_a2lib(mdoc->last->child->string))
                   1065:                return(1);
1.79      kristaps 1066:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADLIB));
1.84      kristaps 1067: }
                   1068:
                   1069:
                   1070: static int
                   1071: post_eoln(POST_ARGS)
                   1072: {
                   1073:
                   1074:        if (NULL == mdoc->last->child)
                   1075:                return(1);
                   1076:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST));
1.31      kristaps 1077: }
                   1078:
                   1079:
                   1080: static int
1.57      kristaps 1081: post_vt(POST_ARGS)
                   1082: {
                   1083:        const struct mdoc_node *n;
                   1084:
                   1085:        /*
                   1086:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                   1087:         * have different syntaxes (yet more context-sensitive
                   1088:         * behaviour).  ELEM types must have a child; BLOCK types,
                   1089:         * specifically the BODY, should only have TEXT children.
                   1090:         */
                   1091:
                   1092:        if (MDOC_ELEM == mdoc->last->type)
                   1093:                return(eerr_ge1(mdoc));
                   1094:        if (MDOC_BODY != mdoc->last->type)
                   1095:                return(1);
                   1096:
                   1097:        for (n = mdoc->last->child; n; n = n->next)
                   1098:                if (MDOC_TEXT != n->type)
1.79      kristaps 1099:                        if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_CHILD))
1.57      kristaps 1100:                                return(0);
                   1101:
                   1102:        return(1);
                   1103: }
                   1104:
                   1105:
                   1106: static int
1.1       kristaps 1107: post_nm(POST_ARGS)
                   1108: {
                   1109:
                   1110:        if (mdoc->last->child)
                   1111:                return(1);
                   1112:        if (mdoc->meta.name)
                   1113:                return(1);
1.79      kristaps 1114:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME));
1.1       kristaps 1115: }
                   1116:
                   1117:
                   1118: static int
                   1119: post_at(POST_ARGS)
                   1120: {
                   1121:
                   1122:        if (NULL == mdoc->last->child)
                   1123:                return(1);
1.79      kristaps 1124:        assert(MDOC_TEXT == mdoc->last->child->type);
1.1       kristaps 1125:        if (mdoc_a2att(mdoc->last->child->string))
                   1126:                return(1);
1.79      kristaps 1127:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT));
1.1       kristaps 1128: }
                   1129:
                   1130:
                   1131: static int
                   1132: post_an(POST_ARGS)
                   1133: {
1.107     kristaps 1134:        struct mdoc_node *np;
1.1       kristaps 1135:
1.107     kristaps 1136:        np = mdoc->last;
                   1137:        if (AUTH__NONE != np->data.An.auth && np->child)
1.120     kristaps 1138:                return(eerr_eq0(mdoc));
                   1139:        /*
                   1140:         * FIXME: make this ewarn and make sure that the front-ends
                   1141:         * don't print the arguments.
                   1142:         */
1.107     kristaps 1143:        if (AUTH__NONE != np->data.An.auth || np->child)
1.1       kristaps 1144:                return(1);
1.107     kristaps 1145:        return(mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS));
1.1       kristaps 1146: }
                   1147:
                   1148:
                   1149: static int
                   1150: post_it(POST_ARGS)
                   1151: {
1.89      kristaps 1152:        int               i, cols, rc;
                   1153:        enum mdoc_list    lt;
1.1       kristaps 1154:        struct mdoc_node *n, *c;
1.89      kristaps 1155:        enum mandocerr    er;
1.1       kristaps 1156:
                   1157:        if (MDOC_BLOCK != mdoc->last->type)
                   1158:                return(1);
                   1159:
                   1160:        n = mdoc->last->parent->parent;
1.104     kristaps 1161:        assert(n->data.Bl);
                   1162:        lt = n->data.Bl->type;
1.89      kristaps 1163:
                   1164:        if (LIST__NONE == lt) {
1.79      kristaps 1165:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
                   1166:                return(0);
                   1167:        }
1.1       kristaps 1168:
1.89      kristaps 1169:        switch (lt) {
                   1170:        case (LIST_tag):
                   1171:                if (mdoc->last->head->child)
1.1       kristaps 1172:                        break;
1.89      kristaps 1173:                /* FIXME: give this a dummy value. */
                   1174:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
                   1175:                        return(0);
1.1       kristaps 1176:                break;
1.89      kristaps 1177:        case (LIST_hang):
1.1       kristaps 1178:                /* FALLTHROUGH */
1.89      kristaps 1179:        case (LIST_ohang):
1.1       kristaps 1180:                /* FALLTHROUGH */
1.89      kristaps 1181:        case (LIST_inset):
1.1       kristaps 1182:                /* FALLTHROUGH */
1.89      kristaps 1183:        case (LIST_diag):
1.1       kristaps 1184:                if (NULL == mdoc->last->head->child)
1.79      kristaps 1185:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
1.1       kristaps 1186:                                return(0);
                   1187:                break;
1.89      kristaps 1188:        case (LIST_bullet):
1.1       kristaps 1189:                /* FALLTHROUGH */
1.89      kristaps 1190:        case (LIST_dash):
1.1       kristaps 1191:                /* FALLTHROUGH */
1.89      kristaps 1192:        case (LIST_enum):
1.1       kristaps 1193:                /* FALLTHROUGH */
1.89      kristaps 1194:        case (LIST_hyphen):
1.108     schwarze 1195:                if (NULL == mdoc->last->body->child)
                   1196:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
                   1197:                                return(0);
1.1       kristaps 1198:                /* FALLTHROUGH */
1.89      kristaps 1199:        case (LIST_item):
1.1       kristaps 1200:                if (mdoc->last->head->child)
1.79      kristaps 1201:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST))
1.1       kristaps 1202:                                return(0);
                   1203:                break;
1.89      kristaps 1204:        case (LIST_column):
1.109     kristaps 1205:                cols = (int)n->data.Bl->ncols;
1.89      kristaps 1206:
1.87      kristaps 1207:                assert(NULL == mdoc->last->head->child);
1.89      kristaps 1208:
1.87      kristaps 1209:                if (NULL == mdoc->last->body->child)
                   1210:                        if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
1.1       kristaps 1211:                                return(0);
1.87      kristaps 1212:
1.89      kristaps 1213:                for (i = 0, c = mdoc->last->child; c; c = c->next)
1.87      kristaps 1214:                        if (MDOC_BODY == c->type)
                   1215:                                i++;
1.38      kristaps 1216:
1.89      kristaps 1217:                if (i < cols)
                   1218:                        er = MANDOCERR_ARGCOUNT;
                   1219:                else if (i == cols || i == cols + 1)
1.38      kristaps 1220:                        break;
1.89      kristaps 1221:                else
                   1222:                        er = MANDOCERR_SYNTARGCOUNT;
1.38      kristaps 1223:
1.89      kristaps 1224:                rc = mdoc_vmsg(mdoc, er,
1.79      kristaps 1225:                                mdoc->last->line, mdoc->last->pos,
                   1226:                                "columns == %d (have %d)", cols, i);
1.89      kristaps 1227:                return(rc);
1.1       kristaps 1228:        default:
                   1229:                break;
                   1230:        }
                   1231:
                   1232:        return(1);
                   1233: }
                   1234:
                   1235:
                   1236: static int
1.14      kristaps 1237: post_bl_head(POST_ARGS)
                   1238: {
1.90      kristaps 1239:        struct mdoc_node *n;
1.14      kristaps 1240:
1.90      kristaps 1241:        assert(mdoc->last->parent);
1.14      kristaps 1242:        n = mdoc->last->parent;
                   1243:
1.104     kristaps 1244:        if (LIST_column == n->data.Bl->type) {
1.109     kristaps 1245:                if (n->data.Bl->ncols && mdoc->last->nchild) {
1.90      kristaps 1246:                        mdoc_nmsg(mdoc, n, MANDOCERR_COLUMNS);
                   1247:                        return(0);
1.65      kristaps 1248:                }
1.90      kristaps 1249:                return(1);
1.65      kristaps 1250:        }
1.14      kristaps 1251:
1.120     kristaps 1252:        /* FIXME: should be ERROR class. */
                   1253:        return(hwarn_eq0(mdoc));
1.14      kristaps 1254: }
                   1255:
                   1256:
                   1257: static int
1.1       kristaps 1258: post_bl(POST_ARGS)
                   1259: {
                   1260:        struct mdoc_node        *n;
                   1261:
1.14      kristaps 1262:        if (MDOC_HEAD == mdoc->last->type)
                   1263:                return(post_bl_head(mdoc));
1.1       kristaps 1264:        if (MDOC_BODY != mdoc->last->type)
                   1265:                return(1);
                   1266:        if (NULL == mdoc->last->child)
                   1267:                return(1);
                   1268:
1.55      kristaps 1269:        /*
                   1270:         * We only allow certain children of `Bl'.  This is usually on
                   1271:         * `It', but apparently `Sm' occurs here and there, so we let
                   1272:         * that one through, too.
                   1273:         */
                   1274:
1.1       kristaps 1275:        /* LINTED */
                   1276:        for (n = mdoc->last->child; n; n = n->next) {
1.55      kristaps 1277:                if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
                   1278:                        continue;
                   1279:                if (MDOC_Sm == n->tok)
                   1280:                        continue;
1.79      kristaps 1281:                mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
                   1282:                return(0);
1.1       kristaps 1283:        }
                   1284:
                   1285:        return(1);
                   1286: }
                   1287:
                   1288:
                   1289: static int
                   1290: ebool(struct mdoc *mdoc)
                   1291: {
                   1292:        struct mdoc_node *n;
                   1293:
                   1294:        /* LINTED */
                   1295:        for (n = mdoc->last->child; n; n = n->next) {
                   1296:                if (MDOC_TEXT != n->type)
                   1297:                        break;
1.2       kristaps 1298:                if (0 == strcmp(n->string, "on"))
1.1       kristaps 1299:                        continue;
1.2       kristaps 1300:                if (0 == strcmp(n->string, "off"))
1.1       kristaps 1301:                        continue;
                   1302:                break;
                   1303:        }
                   1304:
                   1305:        if (NULL == n)
                   1306:                return(1);
1.79      kristaps 1307:        return(mdoc_nmsg(mdoc, n, MANDOCERR_BADBOOL));
1.1       kristaps 1308: }
                   1309:
                   1310:
                   1311: static int
                   1312: post_root(POST_ARGS)
                   1313: {
                   1314:
                   1315:        if (NULL == mdoc->first->child)
1.79      kristaps 1316:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1317:        else if ( ! (MDOC_PBODY & mdoc->flags))
                   1318:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
                   1319:        else if (MDOC_BLOCK != mdoc->first->child->type)
                   1320:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1321:        else if (MDOC_Sh != mdoc->first->child->tok)
                   1322:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
                   1323:        else
                   1324:                return(1);
1.1       kristaps 1325:
1.79      kristaps 1326:        return(0);
1.1       kristaps 1327: }
                   1328:
                   1329:
                   1330: static int
                   1331: post_st(POST_ARGS)
                   1332: {
                   1333:
                   1334:        if (mdoc_a2st(mdoc->last->child->string))
                   1335:                return(1);
1.79      kristaps 1336:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD));
1.1       kristaps 1337: }
                   1338:
                   1339:
                   1340: static int
1.43      kristaps 1341: post_rs(POST_ARGS)
                   1342: {
                   1343:        struct mdoc_node        *nn;
                   1344:
                   1345:        if (MDOC_BODY != mdoc->last->type)
                   1346:                return(1);
                   1347:
                   1348:        for (nn = mdoc->last->child; nn; nn = nn->next)
                   1349:                switch (nn->tok) {
1.50      kristaps 1350:                case(MDOC__U):
                   1351:                        /* FALLTHROUGH */
1.43      kristaps 1352:                case(MDOC__Q):
                   1353:                        /* FALLTHROUGH */
                   1354:                case(MDOC__C):
                   1355:                        /* FALLTHROUGH */
                   1356:                case(MDOC__A):
                   1357:                        /* FALLTHROUGH */
                   1358:                case(MDOC__B):
                   1359:                        /* FALLTHROUGH */
                   1360:                case(MDOC__D):
                   1361:                        /* FALLTHROUGH */
                   1362:                case(MDOC__I):
                   1363:                        /* FALLTHROUGH */
                   1364:                case(MDOC__J):
                   1365:                        /* FALLTHROUGH */
                   1366:                case(MDOC__N):
                   1367:                        /* FALLTHROUGH */
                   1368:                case(MDOC__O):
                   1369:                        /* FALLTHROUGH */
                   1370:                case(MDOC__P):
                   1371:                        /* FALLTHROUGH */
                   1372:                case(MDOC__R):
                   1373:                        /* FALLTHROUGH */
                   1374:                case(MDOC__T):
                   1375:                        /* FALLTHROUGH */
                   1376:                case(MDOC__V):
                   1377:                        break;
                   1378:                default:
1.79      kristaps 1379:                        mdoc_nmsg(mdoc, nn, MANDOCERR_SYNTCHILD);
                   1380:                        return(0);
1.43      kristaps 1381:                }
                   1382:
                   1383:        return(1);
                   1384: }
                   1385:
                   1386:
                   1387: static int
1.1       kristaps 1388: post_sh(POST_ARGS)
                   1389: {
                   1390:
                   1391:        if (MDOC_HEAD == mdoc->last->type)
                   1392:                return(post_sh_head(mdoc));
                   1393:        if (MDOC_BODY == mdoc->last->type)
                   1394:                return(post_sh_body(mdoc));
                   1395:
                   1396:        return(1);
                   1397: }
                   1398:
                   1399:
                   1400: static int
                   1401: post_sh_body(POST_ARGS)
                   1402: {
                   1403:        struct mdoc_node *n;
                   1404:
1.29      kristaps 1405:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1406:                return(1);
                   1407:
                   1408:        /*
                   1409:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1410:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1411:         * children of the BODY declaration can also be "text".
                   1412:         */
                   1413:
                   1414:        if (NULL == (n = mdoc->last->child))
1.79      kristaps 1415:                return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1       kristaps 1416:
                   1417:        for ( ; n && n->next; n = n->next) {
                   1418:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1419:                        continue;
1.28      kristaps 1420:                if (MDOC_TEXT == n->type)
                   1421:                        continue;
1.79      kristaps 1422:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC))
1.1       kristaps 1423:                        return(0);
                   1424:        }
                   1425:
1.41      kristaps 1426:        assert(n);
1.27      kristaps 1427:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1428:                return(1);
1.79      kristaps 1429:        return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1.1       kristaps 1430: }
                   1431:
                   1432:
                   1433: static int
                   1434: post_sh_head(POST_ARGS)
                   1435: {
1.81      kristaps 1436:        char                    buf[BUFSIZ];
1.2       kristaps 1437:        enum mdoc_sec           sec;
                   1438:        const struct mdoc_node *n;
1.1       kristaps 1439:
                   1440:        /*
                   1441:         * Process a new section.  Sections are either "named" or
                   1442:         * "custom"; custom sections are user-defined, while named ones
                   1443:         * usually follow a conventional order and may only appear in
                   1444:         * certain manual sections.
                   1445:         */
                   1446:
1.79      kristaps 1447:        buf[0] = '\0';
                   1448:
                   1449:        /*
                   1450:         * FIXME: yes, these can use a dynamic buffer, but I don't do so
                   1451:         * in the interests of simplicity.
                   1452:         */
1.1       kristaps 1453:
1.2       kristaps 1454:        for (n = mdoc->last->child; n; n = n->next) {
1.12      kristaps 1455:                /* XXX - copied from compact(). */
1.2       kristaps 1456:                assert(MDOC_TEXT == n->type);
1.12      kristaps 1457:
1.81      kristaps 1458:                if (strlcat(buf, n->string, BUFSIZ) >= BUFSIZ) {
1.79      kristaps 1459:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   1460:                        return(0);
                   1461:                }
1.2       kristaps 1462:                if (NULL == n->next)
                   1463:                        continue;
1.81      kristaps 1464:                if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.79      kristaps 1465:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   1466:                        return(0);
                   1467:                }
1.2       kristaps 1468:        }
1.1       kristaps 1469:
1.72      kristaps 1470:        sec = mdoc_str2sec(buf);
1.1       kristaps 1471:
1.12      kristaps 1472:        /*
                   1473:         * Check: NAME should always be first, CUSTOM has no roles,
                   1474:         * non-CUSTOM has a conventional order to be followed.
                   1475:         */
1.1       kristaps 1476:
1.72      kristaps 1477:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.79      kristaps 1478:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST))
1.72      kristaps 1479:                        return(0);
                   1480:
1.1       kristaps 1481:        if (SEC_CUSTOM == sec)
                   1482:                return(1);
1.72      kristaps 1483:
1.1       kristaps 1484:        if (sec == mdoc->lastnamed)
1.79      kristaps 1485:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP))
1.26      kristaps 1486:                        return(0);
1.72      kristaps 1487:
1.1       kristaps 1488:        if (sec < mdoc->lastnamed)
1.79      kristaps 1489:                if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO))
1.26      kristaps 1490:                        return(0);
1.1       kristaps 1491:
1.12      kristaps 1492:        /*
                   1493:         * Check particular section/manual conventions.  LIBRARY can
1.78      kristaps 1494:         * only occur in manual section 2, 3, and 9.
1.12      kristaps 1495:         */
1.1       kristaps 1496:
                   1497:        switch (sec) {
                   1498:        case (SEC_LIBRARY):
1.78      kristaps 1499:                assert(mdoc->meta.msec);
                   1500:                if (*mdoc->meta.msec == '2')
                   1501:                        break;
                   1502:                if (*mdoc->meta.msec == '3')
                   1503:                        break;
                   1504:                if (*mdoc->meta.msec == '9')
1.1       kristaps 1505:                        break;
1.79      kristaps 1506:                return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC));
1.1       kristaps 1507:        default:
                   1508:                break;
                   1509:        }
                   1510:
1.117     kristaps 1511:        return(1);
                   1512: }
                   1513:
                   1514:
                   1515: static int
                   1516: pre_pp(PRE_ARGS)
                   1517: {
                   1518:
1.118     kristaps 1519:        if (NULL == mdoc->last)
                   1520:                return(1);
                   1521:
                   1522:        /* Don't allow prior `Lp' or `Pp'. */
                   1523:
                   1524:        if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1.117     kristaps 1525:                return(1);
                   1526:
                   1527:        if (MDOC_Bl == n->tok && n->data.Bl->comp)
                   1528:                return(1);
                   1529:        if (MDOC_Bd == n->tok && n->data.Bd->comp)
                   1530:                return(1);
                   1531:
                   1532:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
                   1533:        mdoc_node_delete(mdoc, mdoc->last);
1.1       kristaps 1534:        return(1);
                   1535: }

CVSweb