[BACK]Return to mdoc_validate.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_validate.c, Revision 1.80

1.80    ! schwarze    1: /*     $Id: mdoc_validate.c,v 1.79 2010/12/21 23:46:18 schwarze Exp $ */
1.1       kristaps    2: /*
1.65      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.2       schwarze    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.2       schwarze    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.76      schwarze   17: #ifndef        OSNAME
                     18: #include <sys/utsname.h>
                     19: #endif
                     20:
1.1       kristaps   21: #include <sys/types.h>
                     22:
                     23: #include <assert.h>
                     24: #include <ctype.h>
1.31      schwarze   25: #include <limits.h>
1.57      schwarze   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
                     28: #include <string.h>
1.76      schwarze   29: #include <time.h>
1.1       kristaps   30:
1.55      schwarze   31: #include "mandoc.h"
1.1       kristaps   32: #include "libmdoc.h"
1.15      schwarze   33: #include "libmandoc.h"
1.1       kristaps   34:
1.76      schwarze   35: #include "out.h"
                     36: #include "term.h"
                     37: #include "tbl.h"
                     38:
1.1       kristaps   39: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
                     40:
1.60      schwarze   41: #define        PRE_ARGS  struct mdoc *mdoc, struct mdoc_node *n
1.28      schwarze   42: #define        POST_ARGS struct mdoc *mdoc
1.1       kristaps   43:
1.76      schwarze   44: #define        NUMSIZ    32
                     45: #define        DATESIZE  32
                     46:
1.72      schwarze   47: enum   check_ineq {
                     48:        CHECK_LT,
                     49:        CHECK_GT,
                     50:        CHECK_EQ
                     51: };
                     52:
                     53: enum   check_lvl {
                     54:        CHECK_WARN,
                     55:        CHECK_ERROR,
                     56:        CHECK_FATAL
                     57: };
                     58:
1.1       kristaps   59: typedef        int     (*v_pre)(PRE_ARGS);
                     60: typedef        int     (*v_post)(POST_ARGS);
                     61:
                     62: struct valids {
                     63:        v_pre   *pre;
                     64:        v_post  *post;
                     65: };
                     66:
1.72      schwarze   67: static int      check_count(struct mdoc *, enum mdoc_type,
                     68:                        enum check_lvl, enum check_ineq, int);
1.44      schwarze   69: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.76      schwarze   70: static void     check_text(struct mdoc *, int, int, char *);
                     71: static void     check_argv(struct mdoc *,
1.61      schwarze   72:                        struct mdoc_node *, struct mdoc_argv *);
1.76      schwarze   73: static void     check_args(struct mdoc *, struct mdoc_node *);
                     74:
                     75: static int      concat(struct mdoc *, char *,
                     76:                        const struct mdoc_node *, size_t);
1.28      schwarze   77:
1.72      schwarze   78: static int      ebool(POST_ARGS);
1.28      schwarze   79: static int      berr_ge1(POST_ARGS);
                     80: static int      bwarn_ge1(POST_ARGS);
1.72      schwarze   81: static int      eerr_eq0(POST_ARGS);
1.28      schwarze   82: static int      eerr_eq1(POST_ARGS);
                     83: static int      eerr_ge1(POST_ARGS);
1.37      schwarze   84: static int      eerr_le1(POST_ARGS);
1.69      schwarze   85: static int      ewarn_eq0(POST_ARGS);
1.28      schwarze   86: static int      ewarn_ge1(POST_ARGS);
                     87: static int      herr_eq0(POST_ARGS);
                     88: static int      herr_ge1(POST_ARGS);
1.72      schwarze   89: static int      hwarn_eq0(POST_ARGS);
1.28      schwarze   90: static int      hwarn_eq1(POST_ARGS);
                     91: static int      hwarn_le1(POST_ARGS);
                     92:
                     93: static int      post_an(POST_ARGS);
                     94: static int      post_at(POST_ARGS);
                     95: static int      post_bf(POST_ARGS);
                     96: static int      post_bl(POST_ARGS);
1.76      schwarze   97: static int      post_bl_block(POST_ARGS);
                     98: static int      post_bl_block_width(POST_ARGS);
                     99: static int      post_bl_block_tag(POST_ARGS);
1.28      schwarze  100: static int      post_bl_head(POST_ARGS);
1.76      schwarze  101: static int      post_dd(POST_ARGS);
1.60      schwarze  102: static int      post_dt(POST_ARGS);
1.76      schwarze  103: static int      post_defaults(POST_ARGS);
                    104: static int      post_literal(POST_ARGS);
                    105: static int      post_eoln(POST_ARGS);
1.28      schwarze  106: static int      post_it(POST_ARGS);
                    107: static int      post_lb(POST_ARGS);
                    108: static int      post_nm(POST_ARGS);
1.76      schwarze  109: static int      post_os(POST_ARGS);
1.79      schwarze  110: static int      post_ignpar(POST_ARGS);
1.76      schwarze  111: static int      post_prol(POST_ARGS);
1.28      schwarze  112: static int      post_root(POST_ARGS);
1.36      schwarze  113: static int      post_rs(POST_ARGS);
1.28      schwarze  114: static int      post_sh(POST_ARGS);
                    115: static int      post_sh_body(POST_ARGS);
                    116: static int      post_sh_head(POST_ARGS);
                    117: static int      post_st(POST_ARGS);
1.76      schwarze  118: static int      post_std(POST_ARGS);
1.42      schwarze  119: static int      post_vt(POST_ARGS);
1.28      schwarze  120: static int      pre_an(PRE_ARGS);
                    121: static int      pre_bd(PRE_ARGS);
                    122: static int      pre_bl(PRE_ARGS);
                    123: static int      pre_dd(PRE_ARGS);
                    124: static int      pre_display(PRE_ARGS);
                    125: static int      pre_dt(PRE_ARGS);
                    126: static int      pre_it(PRE_ARGS);
1.76      schwarze  127: static int      pre_literal(PRE_ARGS);
1.28      schwarze  128: static int      pre_os(PRE_ARGS);
1.76      schwarze  129: static int      pre_par(PRE_ARGS);
1.28      schwarze  130: static int      pre_sh(PRE_ARGS);
                    131: static int      pre_ss(PRE_ARGS);
1.76      schwarze  132: static int      pre_std(PRE_ARGS);
                    133: static int      pre_ts(PRE_ARGS);
1.28      schwarze  134:
                    135: static v_post   posts_an[] = { post_an, NULL };
1.76      schwarze  136: static v_post   posts_at[] = { post_at, post_defaults, NULL };
                    137: static v_post   posts_bd[] = { post_literal, hwarn_eq0, bwarn_ge1, NULL };
1.28      schwarze  138: static v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
1.76      schwarze  139: static v_post   posts_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.28      schwarze  140: static v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
                    141: static v_post   posts_bool[] = { eerr_eq1, ebool, NULL };
1.59      schwarze  142: static v_post   posts_eoln[] = { post_eoln, NULL };
1.76      schwarze  143: static v_post   posts_defaults[] = { post_defaults, NULL };
                    144: static v_post   posts_dd[] = { ewarn_ge1, post_dd, post_prol, NULL };
                    145: static v_post   posts_dl[] = { post_literal, bwarn_ge1, herr_eq0, NULL };
                    146: static v_post   posts_dt[] = { post_dt, post_prol, NULL };
1.28      schwarze  147: static v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
                    148: static v_post   posts_it[] = { post_it, NULL };
                    149: static v_post   posts_lb[] = { eerr_eq1, post_lb, NULL };
                    150: static v_post   posts_nd[] = { berr_ge1, NULL };
                    151: static v_post   posts_nm[] = { post_nm, NULL };
1.69      schwarze  152: static v_post   posts_notext[] = { ewarn_eq0, NULL };
1.76      schwarze  153: static v_post   posts_os[] = { post_os, post_prol, NULL };
1.36      schwarze  154: static v_post   posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
1.79      schwarze  155: static v_post   posts_sh[] = { post_ignpar, herr_ge1, bwarn_ge1, post_sh, NULL };
1.37      schwarze  156: static v_post   posts_sp[] = { eerr_le1, NULL };
1.79      schwarze  157: static v_post   posts_ss[] = { post_ignpar, herr_ge1, bwarn_ge1, NULL };
1.28      schwarze  158: static v_post   posts_st[] = { eerr_eq1, post_st, NULL };
1.76      schwarze  159: static v_post   posts_std[] = { post_std, NULL };
1.28      schwarze  160: static v_post   posts_text[] = { eerr_ge1, NULL };
1.38      schwarze  161: static v_post   posts_text1[] = { eerr_eq1, NULL };
1.42      schwarze  162: static v_post   posts_vt[] = { post_vt, NULL };
1.28      schwarze  163: static v_post   posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
                    164: static v_post   posts_wtext[] = { ewarn_ge1, NULL };
                    165: static v_pre    pres_an[] = { pre_an, NULL };
1.76      schwarze  166: static v_pre    pres_bd[] = { pre_display, pre_bd, pre_literal, pre_par, NULL };
                    167: static v_pre    pres_bl[] = { pre_bl, pre_par, NULL };
1.28      schwarze  168: static v_pre    pres_d1[] = { pre_display, NULL };
1.76      schwarze  169: static v_pre    pres_dl[] = { pre_literal, pre_display, NULL };
1.28      schwarze  170: static v_pre    pres_dd[] = { pre_dd, NULL };
                    171: static v_pre    pres_dt[] = { pre_dt, NULL };
1.48      schwarze  172: static v_pre    pres_er[] = { NULL, NULL };
1.53      schwarze  173: static v_pre    pres_fd[] = { NULL, NULL };
1.79      schwarze  174: static v_pre    pres_it[] = { pre_it, pre_par, NULL };
1.28      schwarze  175: static v_pre    pres_os[] = { pre_os, NULL };
1.76      schwarze  176: static v_pre    pres_pp[] = { pre_par, NULL };
1.28      schwarze  177: static v_pre    pres_sh[] = { pre_sh, NULL };
                    178: static v_pre    pres_ss[] = { pre_ss, NULL };
1.76      schwarze  179: static v_pre    pres_std[] = { pre_std, NULL };
                    180: static v_pre    pres_ts[] = { pre_ts, NULL };
1.1       kristaps  181:
                    182: const  struct valids mdoc_valids[MDOC_MAX] = {
1.7       schwarze  183:        { NULL, NULL },                         /* Ap */
1.76      schwarze  184:        { pres_dd, posts_dd },                  /* Dd */
1.60      schwarze  185:        { pres_dt, posts_dt },                  /* Dt */
1.76      schwarze  186:        { pres_os, posts_os },                  /* Os */
1.1       kristaps  187:        { pres_sh, posts_sh },                  /* Sh */
                    188:        { pres_ss, posts_ss },                  /* Ss */
1.70      schwarze  189:        { pres_pp, posts_notext },              /* Pp */
1.1       kristaps  190:        { pres_d1, posts_wline },               /* D1 */
1.76      schwarze  191:        { pres_dl, posts_dl },                  /* Dl */
                    192:        { pres_bd, posts_bd },                  /* Bd */
1.1       kristaps  193:        { NULL, NULL },                         /* Ed */
                    194:        { pres_bl, posts_bl },                  /* Bl */
                    195:        { NULL, NULL },                         /* El */
                    196:        { pres_it, posts_it },                  /* It */
                    197:        { NULL, posts_text },                   /* Ad */
                    198:        { pres_an, posts_an },                  /* An */
1.76      schwarze  199:        { NULL, posts_defaults },               /* Ar */
1.50      schwarze  200:        { NULL, posts_text },                   /* Cd */
1.1       kristaps  201:        { NULL, NULL },                         /* Cm */
                    202:        { NULL, NULL },                         /* Dv */
                    203:        { pres_er, posts_text },                /* Er */
                    204:        { NULL, NULL },                         /* Ev */
1.76      schwarze  205:        { pres_std, posts_std },                /* Ex */
1.1       kristaps  206:        { NULL, NULL },                         /* Fa */
                    207:        { pres_fd, posts_wtext },               /* Fd */
                    208:        { NULL, NULL },                         /* Fl */
                    209:        { NULL, posts_text },                   /* Fn */
                    210:        { NULL, posts_wtext },                  /* Ft */
                    211:        { NULL, posts_text },                   /* Ic */
1.38      schwarze  212:        { NULL, posts_text1 },                  /* In */
1.76      schwarze  213:        { NULL, posts_defaults },               /* Li */
1.25      schwarze  214:        { NULL, posts_nd },                     /* Nd */
1.1       kristaps  215:        { NULL, posts_nm },                     /* Nm */
1.78      schwarze  216:        { NULL, NULL },                         /* Op */
1.1       kristaps  217:        { NULL, NULL },                         /* Ot */
1.76      schwarze  218:        { NULL, posts_defaults },               /* Pa */
                    219:        { pres_std, posts_std },                /* Rv */
1.1       kristaps  220:        { NULL, posts_st },                     /* St */
                    221:        { NULL, NULL },                         /* Va */
1.42      schwarze  222:        { NULL, posts_vt },                     /* Vt */
1.59      schwarze  223:        { NULL, posts_wtext },                  /* Xr */
1.1       kristaps  224:        { NULL, posts_text },                   /* %A */
1.37      schwarze  225:        { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
1.40      schwarze  226:        { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1       kristaps  227:        { NULL, posts_text },                   /* %I */
                    228:        { NULL, posts_text },                   /* %J */
                    229:        { NULL, posts_text },                   /* %N */
                    230:        { NULL, posts_text },                   /* %O */
                    231:        { NULL, posts_text },                   /* %P */
                    232:        { NULL, posts_text },                   /* %R */
1.37      schwarze  233:        { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1       kristaps  234:        { NULL, posts_text },                   /* %V */
                    235:        { NULL, NULL },                         /* Ac */
                    236:        { NULL, NULL },                         /* Ao */
1.78      schwarze  237:        { NULL, NULL },                         /* Aq */
1.1       kristaps  238:        { NULL, posts_at },                     /* At */
                    239:        { NULL, NULL },                         /* Bc */
                    240:        { NULL, posts_bf },                     /* Bf */
                    241:        { NULL, NULL },                         /* Bo */
1.78      schwarze  242:        { NULL, NULL },                         /* Bq */
1.1       kristaps  243:        { NULL, NULL },                         /* Bsx */
                    244:        { NULL, NULL },                         /* Bx */
                    245:        { NULL, posts_bool },                   /* Db */
                    246:        { NULL, NULL },                         /* Dc */
                    247:        { NULL, NULL },                         /* Do */
1.78      schwarze  248:        { NULL, NULL },                         /* Dq */
1.1       kristaps  249:        { NULL, NULL },                         /* Ec */
                    250:        { NULL, NULL },                         /* Ef */
                    251:        { NULL, NULL },                         /* Em */
                    252:        { NULL, NULL },                         /* Eo */
                    253:        { NULL, NULL },                         /* Fx */
                    254:        { NULL, posts_text },                   /* Ms */
                    255:        { NULL, posts_notext },                 /* No */
                    256:        { NULL, posts_notext },                 /* Ns */
                    257:        { NULL, NULL },                         /* Nx */
                    258:        { NULL, NULL },                         /* Ox */
                    259:        { NULL, NULL },                         /* Pc */
1.38      schwarze  260:        { NULL, posts_text1 },                  /* Pf */
1.1       kristaps  261:        { NULL, NULL },                         /* Po */
1.78      schwarze  262:        { NULL, NULL },                         /* Pq */
1.1       kristaps  263:        { NULL, NULL },                         /* Qc */
1.78      schwarze  264:        { NULL, NULL },                         /* Ql */
1.1       kristaps  265:        { NULL, NULL },                         /* Qo */
1.78      schwarze  266:        { NULL, NULL },                         /* Qq */
1.1       kristaps  267:        { NULL, NULL },                         /* Re */
1.36      schwarze  268:        { NULL, posts_rs },                     /* Rs */
1.1       kristaps  269:        { NULL, NULL },                         /* Sc */
                    270:        { NULL, NULL },                         /* So */
1.78      schwarze  271:        { NULL, NULL },                         /* Sq */
1.1       kristaps  272:        { NULL, posts_bool },                   /* Sm */
                    273:        { NULL, posts_text },                   /* Sx */
                    274:        { NULL, posts_text },                   /* Sy */
                    275:        { NULL, posts_text },                   /* Tn */
                    276:        { NULL, NULL },                         /* Ux */
                    277:        { NULL, NULL },                         /* Xc */
                    278:        { NULL, NULL },                         /* Xo */
                    279:        { NULL, posts_fo },                     /* Fo */
                    280:        { NULL, NULL },                         /* Fc */
                    281:        { NULL, NULL },                         /* Oo */
                    282:        { NULL, NULL },                         /* Oc */
1.76      schwarze  283:        { NULL, posts_bk },                     /* Bk */
1.1       kristaps  284:        { NULL, NULL },                         /* Ek */
1.59      schwarze  285:        { NULL, posts_eoln },                   /* Bt */
1.1       kristaps  286:        { NULL, NULL },                         /* Hf */
                    287:        { NULL, NULL },                         /* Fr */
1.59      schwarze  288:        { NULL, posts_eoln },                   /* Ud */
                    289:        { NULL, posts_lb },                     /* Lb */
1.31      schwarze  290:        { NULL, posts_notext },                 /* Lp */
1.38      schwarze  291:        { NULL, posts_text },                   /* Lk */
1.76      schwarze  292:        { NULL, posts_defaults },               /* Mt */
1.78      schwarze  293:        { NULL, NULL },                         /* Brq */
1.1       kristaps  294:        { NULL, NULL },                         /* Bro */
                    295:        { NULL, NULL },                         /* Brc */
                    296:        { NULL, posts_text },                   /* %C */
                    297:        { NULL, NULL },                         /* Es */
                    298:        { NULL, NULL },                         /* En */
                    299:        { NULL, NULL },                         /* Dx */
                    300:        { NULL, posts_text },                   /* %Q */
1.31      schwarze  301:        { NULL, posts_notext },                 /* br */
1.70      schwarze  302:        { pres_pp, posts_sp },                  /* sp */
1.38      schwarze  303:        { NULL, posts_text1 },                  /* %U */
1.60      schwarze  304:        { NULL, NULL },                         /* Ta */
1.76      schwarze  305:        { pres_ts, NULL },                      /* TS */
1.71      schwarze  306:        { NULL, NULL },                         /* TE */
1.1       kristaps  307: };
                    308:
1.76      schwarze  309: #define        RSORD_MAX 14 /* Number of `Rs' blocks. */
                    310:
                    311: static const enum mdoct rsord[RSORD_MAX] = {
                    312:        MDOC__A,
                    313:        MDOC__T,
                    314:        MDOC__B,
                    315:        MDOC__I,
                    316:        MDOC__J,
                    317:        MDOC__R,
                    318:        MDOC__N,
                    319:        MDOC__V,
                    320:        MDOC__P,
                    321:        MDOC__Q,
                    322:        MDOC__D,
                    323:        MDOC__O,
                    324:        MDOC__C,
                    325:        MDOC__U
                    326: };
                    327:
1.1       kristaps  328:
                    329: int
1.60      schwarze  330: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1       kristaps  331: {
                    332:        v_pre           *p;
                    333:        int              line, pos;
1.61      schwarze  334:        char            *tp;
1.1       kristaps  335:
                    336:        if (MDOC_TEXT == n->type) {
                    337:                tp = n->string;
                    338:                line = n->line;
                    339:                pos = n->pos;
1.76      schwarze  340:                check_text(mdoc, line, pos, tp);
                    341:                return(1);
1.1       kristaps  342:        }
                    343:
1.76      schwarze  344:        check_args(mdoc, n);
                    345:
1.1       kristaps  346:        if (NULL == mdoc_valids[n->tok].pre)
                    347:                return(1);
                    348:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    349:                if ( ! (*p)(mdoc, n))
                    350:                        return(0);
                    351:        return(1);
                    352: }
                    353:
                    354:
                    355: int
                    356: mdoc_valid_post(struct mdoc *mdoc)
                    357: {
                    358:        v_post          *p;
                    359:
                    360:        if (MDOC_VALID & mdoc->last->flags)
                    361:                return(1);
                    362:        mdoc->last->flags |= MDOC_VALID;
                    363:
                    364:        if (MDOC_TEXT == mdoc->last->type)
                    365:                return(1);
                    366:        if (MDOC_ROOT == mdoc->last->type)
                    367:                return(post_root(mdoc));
                    368:
                    369:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    370:                return(1);
                    371:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    372:                if ( ! (*p)(mdoc))
                    373:                        return(0);
                    374:
                    375:        return(1);
                    376: }
                    377:
1.72      schwarze  378: static int
                    379: check_count(struct mdoc *m, enum mdoc_type type,
                    380:                enum check_lvl lvl, enum check_ineq ineq, int val)
                    381: {
                    382:        const char      *p;
                    383:
                    384:        if (m->last->type != type)
                    385:                return(1);
                    386:
                    387:        switch (ineq) {
                    388:        case (CHECK_LT):
                    389:                p = "less than ";
                    390:                if (m->last->nchild < val)
                    391:                        return(1);
                    392:                break;
                    393:        case (CHECK_GT):
                    394:                p = "greater than ";
                    395:                if (m->last->nchild > val)
                    396:                        return(1);
                    397:                break;
                    398:        case (CHECK_EQ):
                    399:                p = "";
                    400:                if (val == m->last->nchild)
                    401:                        return(1);
                    402:                break;
1.76      schwarze  403:        default:
                    404:                abort();
                    405:                /* NOTREACHED */
1.72      schwarze  406:        }
                    407:
                    408:        if (CHECK_WARN == lvl) {
                    409:                return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
                    410:                                m->last->line, m->last->pos,
                    411:                                "want %s%d children (have %d)",
                    412:                                p, val, m->last->nchild));
                    413:        }
                    414:
1.76      schwarze  415:        /* FIXME: THIS IS THE SAME AS THE ABOVE. */
                    416:
1.72      schwarze  417:        return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
                    418:                        m->last->line, m->last->pos,
                    419:                        "require %s%d children (have %d)",
                    420:                        p, val, m->last->nchild));
                    421: }
                    422:
                    423: static int
                    424: berr_ge1(POST_ARGS)
                    425: {
                    426:
                    427:        return(check_count(mdoc, MDOC_BODY, CHECK_FATAL, CHECK_GT, 0));
                    428: }
                    429:
                    430: static int
                    431: bwarn_ge1(POST_ARGS)
                    432: {
                    433:        return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
                    434: }
                    435:
                    436: static int
                    437: eerr_eq0(POST_ARGS)
                    438: {
                    439:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 0));
                    440: }
1.1       kristaps  441:
1.72      schwarze  442: static int
                    443: eerr_eq1(POST_ARGS)
1.1       kristaps  444: {
1.72      schwarze  445:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_EQ, 1));
                    446: }
1.1       kristaps  447:
1.72      schwarze  448: static int
                    449: eerr_ge1(POST_ARGS)
                    450: {
                    451:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_GT, 0));
1.1       kristaps  452: }
                    453:
1.72      schwarze  454: static int
                    455: eerr_le1(POST_ARGS)
                    456: {
                    457:        return(check_count(mdoc, MDOC_ELEM, CHECK_FATAL, CHECK_LT, 2));
                    458: }
1.1       kristaps  459:
1.72      schwarze  460: static int
                    461: ewarn_eq0(POST_ARGS)
1.1       kristaps  462: {
1.72      schwarze  463:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
                    464: }
1.1       kristaps  465:
1.72      schwarze  466: static int
                    467: ewarn_ge1(POST_ARGS)
                    468: {
                    469:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1       kristaps  470: }
                    471:
1.72      schwarze  472: static int
                    473: herr_eq0(POST_ARGS)
                    474: {
                    475:        return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_EQ, 0));
                    476: }
1.1       kristaps  477:
1.72      schwarze  478: static int
                    479: herr_ge1(POST_ARGS)
                    480: {
                    481:        return(check_count(mdoc, MDOC_HEAD, CHECK_FATAL, CHECK_GT, 0));
                    482: }
1.1       kristaps  483:
1.72      schwarze  484: static int
                    485: hwarn_eq0(POST_ARGS)
                    486: {
                    487:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.1       kristaps  488: }
                    489:
1.72      schwarze  490: static int
                    491: hwarn_eq1(POST_ARGS)
                    492: {
                    493:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
                    494: }
1.1       kristaps  495:
1.72      schwarze  496: static int
                    497: hwarn_le1(POST_ARGS)
                    498: {
                    499:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
                    500: }
1.1       kristaps  501:
1.76      schwarze  502: static void
1.61      schwarze  503: check_args(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  504: {
                    505:        int              i;
                    506:
                    507:        if (NULL == n->args)
1.76      schwarze  508:                return;
1.1       kristaps  509:
                    510:        assert(n->args->argc);
                    511:        for (i = 0; i < (int)n->args->argc; i++)
1.76      schwarze  512:                check_argv(m, n, &n->args->argv[i]);
1.1       kristaps  513: }
                    514:
1.76      schwarze  515: static void
1.61      schwarze  516: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1       kristaps  517: {
                    518:        int              i;
                    519:
                    520:        for (i = 0; i < (int)v->sz; i++)
1.76      schwarze  521:                check_text(m, v->line, v->pos, v->value[i]);
1.1       kristaps  522:
1.76      schwarze  523:        /* FIXME: move to post_std(). */
1.1       kristaps  524:
1.76      schwarze  525:        if (MDOC_Std == v->arg)
                    526:                if ( ! (v->sz || m->meta.name))
                    527:                        mdoc_nmsg(m, n, MANDOCERR_NONAME);
1.1       kristaps  528: }
                    529:
1.76      schwarze  530: static void
1.66      schwarze  531: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1       kristaps  532: {
1.15      schwarze  533:        int              c;
1.66      schwarze  534:        size_t           sz;
1.1       kristaps  535:
1.66      schwarze  536:        for ( ; *p; p++, pos++) {
                    537:                sz = strcspn(p, "\t\\");
                    538:                p += (int)sz;
                    539:
                    540:                if ('\0' == *p)
                    541:                        break;
                    542:
                    543:                pos += (int)sz;
1.65      schwarze  544:
1.1       kristaps  545:                if ('\t' == *p) {
1.76      schwarze  546:                        if ( ! (MDOC_LITERAL & m->flags))
                    547:                                mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB);
                    548:                        continue;
1.66      schwarze  549:                }
1.1       kristaps  550:
1.76      schwarze  551:                if (0 == (c = mandoc_special(p))) {
                    552:                        mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
                    553:                        continue;
                    554:                }
1.1       kristaps  555:
1.76      schwarze  556:                p += c - 1;
                    557:                pos += c - 1;
1.1       kristaps  558:        }
                    559: }
                    560:
                    561: static int
1.44      schwarze  562: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1       kristaps  563: {
                    564:
                    565:        assert(n->parent);
                    566:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    567:                        (t == n->parent->type))
                    568:                return(1);
                    569:
1.55      schwarze  570:        mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
                    571:                                n->line, n->pos, "want parent %s",
                    572:                                MDOC_ROOT == t ? "<root>" :
                    573:                                        mdoc_macronames[tok]);
                    574:        return(0);
1.1       kristaps  575: }
                    576:
                    577:
                    578: static int
                    579: pre_display(PRE_ARGS)
                    580: {
                    581:        struct mdoc_node *node;
                    582:
                    583:        if (MDOC_BLOCK != n->type)
                    584:                return(1);
                    585:
                    586:        for (node = mdoc->last->parent; node; node = node->parent)
                    587:                if (MDOC_BLOCK == node->type)
                    588:                        if (MDOC_Bd == node->tok)
                    589:                                break;
1.76      schwarze  590:
1.74      schwarze  591:        if (node)
                    592:                mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
1.1       kristaps  593:
1.74      schwarze  594:        return(1);
1.1       kristaps  595: }
                    596:
                    597:
                    598: static int
                    599: pre_bl(PRE_ARGS)
                    600: {
1.65      schwarze  601:        int               i, comp, dup;
                    602:        const char       *offs, *width;
                    603:        enum mdoc_list    lt;
                    604:        struct mdoc_node *np;
1.1       kristaps  605:
1.60      schwarze  606:        if (MDOC_BLOCK != n->type) {
1.65      schwarze  607:                if (ENDBODY_NOT != n->end) {
                    608:                        assert(n->pending);
                    609:                        np = n->pending->parent;
                    610:                } else
                    611:                        np = n->parent;
                    612:
                    613:                assert(np);
                    614:                assert(MDOC_BLOCK == np->type);
                    615:                assert(MDOC_Bl == np->tok);
                    616:                assert(np->data.Bl);
                    617:                n->data.Bl = np->data.Bl;
1.1       kristaps  618:                return(1);
1.55      schwarze  619:        }
1.1       kristaps  620:
1.60      schwarze  621:        /*
                    622:         * First figure out which kind of list to use: bind ourselves to
                    623:         * the first mentioned list type and warn about any remaining
                    624:         * ones.  If we find no list type, we default to LIST_item.
                    625:         */
1.1       kristaps  626:
1.65      schwarze  627:        assert(NULL == n->data.Bl);
                    628:        n->data.Bl = mandoc_calloc(1, sizeof(struct mdoc_bl));
1.1       kristaps  629:
                    630:        /* LINTED */
1.60      schwarze  631:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    632:                lt = LIST__NONE;
1.61      schwarze  633:                dup = comp = 0;
                    634:                width = offs = NULL;
1.60      schwarze  635:                switch (n->args->argv[i].arg) {
                    636:                /* Set list types. */
1.1       kristaps  637:                case (MDOC_Bullet):
1.60      schwarze  638:                        lt = LIST_bullet;
                    639:                        break;
1.1       kristaps  640:                case (MDOC_Dash):
1.60      schwarze  641:                        lt = LIST_dash;
                    642:                        break;
1.1       kristaps  643:                case (MDOC_Enum):
1.60      schwarze  644:                        lt = LIST_enum;
                    645:                        break;
1.1       kristaps  646:                case (MDOC_Hyphen):
1.60      schwarze  647:                        lt = LIST_hyphen;
                    648:                        break;
1.1       kristaps  649:                case (MDOC_Item):
1.60      schwarze  650:                        lt = LIST_item;
                    651:                        break;
1.1       kristaps  652:                case (MDOC_Tag):
1.60      schwarze  653:                        lt = LIST_tag;
                    654:                        break;
1.1       kristaps  655:                case (MDOC_Diag):
1.60      schwarze  656:                        lt = LIST_diag;
                    657:                        break;
1.1       kristaps  658:                case (MDOC_Hang):
1.60      schwarze  659:                        lt = LIST_hang;
                    660:                        break;
1.1       kristaps  661:                case (MDOC_Ohang):
1.60      schwarze  662:                        lt = LIST_ohang;
                    663:                        break;
1.1       kristaps  664:                case (MDOC_Inset):
1.60      schwarze  665:                        lt = LIST_inset;
                    666:                        break;
1.1       kristaps  667:                case (MDOC_Column):
1.60      schwarze  668:                        lt = LIST_column;
                    669:                        break;
                    670:                /* Set list arguments. */
1.37      schwarze  671:                case (MDOC_Compact):
1.65      schwarze  672:                        dup = n->data.Bl->comp;
1.61      schwarze  673:                        comp = 1;
1.60      schwarze  674:                        break;
1.1       kristaps  675:                case (MDOC_Width):
1.65      schwarze  676:                        dup = (NULL != n->data.Bl->width);
1.61      schwarze  677:                        width = n->args->argv[i].value[0];
1.5       schwarze  678:                        break;
1.1       kristaps  679:                case (MDOC_Offset):
1.61      schwarze  680:                        /* NB: this can be empty! */
                    681:                        if (n->args->argv[i].sz) {
                    682:                                offs = n->args->argv[i].value[0];
1.65      schwarze  683:                                dup = (NULL != n->data.Bl->offs);
1.61      schwarze  684:                                break;
                    685:                        }
1.76      schwarze  686:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.1       kristaps  687:                        break;
1.66      schwarze  688:                default:
                    689:                        continue;
1.1       kristaps  690:                }
                    691:
1.60      schwarze  692:                /* Check: duplicate auxiliary arguments. */
                    693:
1.76      schwarze  694:                if (dup)
                    695:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.61      schwarze  696:
                    697:                if (comp && ! dup)
1.65      schwarze  698:                        n->data.Bl->comp = comp;
1.61      schwarze  699:                if (offs && ! dup)
1.65      schwarze  700:                        n->data.Bl->offs = offs;
1.61      schwarze  701:                if (width && ! dup)
1.65      schwarze  702:                        n->data.Bl->width = width;
1.60      schwarze  703:
                    704:                /* Check: multiple list types. */
                    705:
1.65      schwarze  706:                if (LIST__NONE != lt && n->data.Bl->type != LIST__NONE)
1.76      schwarze  707:                        mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
1.60      schwarze  708:
                    709:                /* Assign list type. */
                    710:
1.65      schwarze  711:                if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
                    712:                        n->data.Bl->type = lt;
                    713:                        /* Set column information, too. */
                    714:                        if (LIST_column == lt) {
                    715:                                n->data.Bl->ncols =
                    716:                                        n->args->argv[i].sz;
                    717:                                n->data.Bl->cols = (const char **)
                    718:                                        n->args->argv[i].value;
                    719:                        }
                    720:                }
1.60      schwarze  721:
                    722:                /* The list type should come first. */
                    723:
1.65      schwarze  724:                if (n->data.Bl->type == LIST__NONE)
                    725:                        if (n->data.Bl->width ||
                    726:                                        n->data.Bl->offs ||
                    727:                                        n->data.Bl->comp)
1.76      schwarze  728:                                mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
1.60      schwarze  729:
                    730:                continue;
                    731:        }
                    732:
                    733:        /* Allow lists to default to LIST_item. */
                    734:
1.65      schwarze  735:        if (LIST__NONE == n->data.Bl->type) {
1.76      schwarze  736:                mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
1.65      schwarze  737:                n->data.Bl->type = LIST_item;
1.55      schwarze  738:        }
1.1       kristaps  739:
1.5       schwarze  740:        /*
                    741:         * Validate the width field.  Some list types don't need width
                    742:         * types and should be warned about them.  Others should have it
                    743:         * and must also be warned.
                    744:         */
                    745:
1.65      schwarze  746:        switch (n->data.Bl->type) {
1.60      schwarze  747:        case (LIST_tag):
1.65      schwarze  748:                if (n->data.Bl->width)
1.60      schwarze  749:                        break;
1.76      schwarze  750:                mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
                    751:                break;
1.60      schwarze  752:        case (LIST_column):
1.1       kristaps  753:                /* FALLTHROUGH */
1.60      schwarze  754:        case (LIST_diag):
1.1       kristaps  755:                /* FALLTHROUGH */
1.60      schwarze  756:        case (LIST_ohang):
1.40      schwarze  757:                /* FALLTHROUGH */
1.60      schwarze  758:        case (LIST_inset):
1.1       kristaps  759:                /* FALLTHROUGH */
1.60      schwarze  760:        case (LIST_item):
1.75      schwarze  761:                if (n->data.Bl->width)
                    762:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
                    763:                break;
1.5       schwarze  764:        default:
                    765:                break;
                    766:        }
                    767:
1.1       kristaps  768:        return(1);
                    769: }
                    770:
                    771:
                    772: static int
                    773: pre_bd(PRE_ARGS)
                    774: {
1.65      schwarze  775:        int               i, dup, comp;
                    776:        enum mdoc_disp    dt;
                    777:        const char       *offs;
                    778:        struct mdoc_node *np;
1.1       kristaps  779:
1.61      schwarze  780:        if (MDOC_BLOCK != n->type) {
1.65      schwarze  781:                if (ENDBODY_NOT != n->end) {
                    782:                        assert(n->pending);
                    783:                        np = n->pending->parent;
                    784:                } else
                    785:                        np = n->parent;
                    786:
                    787:                assert(np);
                    788:                assert(MDOC_BLOCK == np->type);
                    789:                assert(MDOC_Bd == np->tok);
                    790:                assert(np->data.Bd);
                    791:                n->data.Bd = np->data.Bd;
1.1       kristaps  792:                return(1);
1.55      schwarze  793:        }
1.1       kristaps  794:
1.65      schwarze  795:        assert(NULL == n->data.Bd);
                    796:        n->data.Bd = mandoc_calloc(1, sizeof(struct mdoc_bd));
1.1       kristaps  797:
                    798:        /* LINTED */
1.61      schwarze  799:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    800:                dt = DISP__NONE;
                    801:                dup = comp = 0;
                    802:                offs = NULL;
                    803:
1.1       kristaps  804:                switch (n->args->argv[i].arg) {
1.38      schwarze  805:                case (MDOC_Centred):
1.61      schwarze  806:                        dt = DISP_centred;
                    807:                        break;
1.1       kristaps  808:                case (MDOC_Ragged):
1.61      schwarze  809:                        dt = DISP_ragged;
                    810:                        break;
1.1       kristaps  811:                case (MDOC_Unfilled):
1.61      schwarze  812:                        dt = DISP_unfilled;
                    813:                        break;
1.1       kristaps  814:                case (MDOC_Filled):
1.61      schwarze  815:                        dt = DISP_filled;
                    816:                        break;
1.1       kristaps  817:                case (MDOC_Literal):
1.61      schwarze  818:                        dt = DISP_literal;
                    819:                        break;
                    820:                case (MDOC_File):
                    821:                        mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
                    822:                        return(0);
                    823:                case (MDOC_Offset):
                    824:                        /* NB: this can be empty! */
                    825:                        if (n->args->argv[i].sz) {
                    826:                                offs = n->args->argv[i].value[0];
1.65      schwarze  827:                                dup = (NULL != n->data.Bd->offs);
1.1       kristaps  828:                                break;
1.61      schwarze  829:                        }
1.76      schwarze  830:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.55      schwarze  831:                        break;
1.61      schwarze  832:                case (MDOC_Compact):
                    833:                        comp = 1;
1.65      schwarze  834:                        dup = n->data.Bd->comp;
1.61      schwarze  835:                        break;
1.1       kristaps  836:                default:
1.61      schwarze  837:                        abort();
                    838:                        /* NOTREACHED */
1.1       kristaps  839:                }
                    840:
1.61      schwarze  841:                /* Check whether we have duplicates. */
                    842:
1.76      schwarze  843:                if (dup)
                    844:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.61      schwarze  845:
                    846:                /* Make our auxiliary assignments. */
                    847:
                    848:                if (offs && ! dup)
1.65      schwarze  849:                        n->data.Bd->offs = offs;
1.61      schwarze  850:                if (comp && ! dup)
1.65      schwarze  851:                        n->data.Bd->comp = comp;
1.61      schwarze  852:
                    853:                /* Check whether a type has already been assigned. */
                    854:
1.65      schwarze  855:                if (DISP__NONE != dt && n->data.Bd->type != DISP__NONE)
1.76      schwarze  856:                        mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
1.61      schwarze  857:
                    858:                /* Make our type assignment. */
                    859:
1.65      schwarze  860:                if (DISP__NONE != dt && n->data.Bd->type == DISP__NONE)
                    861:                        n->data.Bd->type = dt;
1.61      schwarze  862:        }
                    863:
1.65      schwarze  864:        if (DISP__NONE == n->data.Bd->type) {
1.76      schwarze  865:                mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
1.65      schwarze  866:                n->data.Bd->type = DISP_ragged;
1.61      schwarze  867:        }
                    868:
                    869:        return(1);
1.1       kristaps  870: }
                    871:
                    872:
                    873: static int
                    874: pre_ss(PRE_ARGS)
                    875: {
                    876:
                    877:        if (MDOC_BLOCK != n->type)
                    878:                return(1);
                    879:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    880: }
                    881:
                    882:
                    883: static int
                    884: pre_sh(PRE_ARGS)
                    885: {
                    886:
                    887:        if (MDOC_BLOCK != n->type)
                    888:                return(1);
1.63      schwarze  889:
                    890:        mdoc->regs->regs[(int)REG_nS].set = 0;
1.50      schwarze  891:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  892: }
                    893:
                    894:
                    895: static int
                    896: pre_it(PRE_ARGS)
                    897: {
                    898:
                    899:        if (MDOC_BLOCK != n->type)
                    900:                return(1);
1.76      schwarze  901:
1.1       kristaps  902:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    903: }
                    904:
                    905:
                    906: static int
                    907: pre_an(PRE_ARGS)
                    908: {
1.73      schwarze  909:        int              i;
1.1       kristaps  910:
1.80    ! schwarze  911:        assert(NULL == n->data.An);
        !           912:        n->data.An = mandoc_calloc(1, sizeof(struct mdoc_an));
        !           913:
1.65      schwarze  914:        if (NULL == n->args)
1.1       kristaps  915:                return(1);
1.73      schwarze  916:
                    917:        for (i = 1; i < (int)n->args->argc; i++)
1.76      schwarze  918:                mdoc_pmsg(mdoc, n->args->argv[i].line,
                    919:                        n->args->argv[i].pos, MANDOCERR_IGNARGV);
1.72      schwarze  920:
1.65      schwarze  921:        if (MDOC_Split == n->args->argv[0].arg)
1.80    ! schwarze  922:                n->data.An->auth = AUTH_split;
1.65      schwarze  923:        else if (MDOC_Nosplit == n->args->argv[0].arg)
1.80    ! schwarze  924:                n->data.An->auth = AUTH_nosplit;
1.65      schwarze  925:        else
                    926:                abort();
                    927:
                    928:        return(1);
1.1       kristaps  929: }
                    930:
                    931: static int
1.76      schwarze  932: pre_std(PRE_ARGS)
1.60      schwarze  933: {
                    934:
1.76      schwarze  935:        if (n->args && 1 == n->args->argc)
                    936:                if (MDOC_Std == n->args->argv[0].arg)
                    937:                        return(1);
1.60      schwarze  938:
1.76      schwarze  939:        mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
1.60      schwarze  940:        return(1);
                    941: }
                    942:
                    943: static int
1.1       kristaps  944: pre_dt(PRE_ARGS)
                    945: {
1.40      schwarze  946:
1.1       kristaps  947:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.76      schwarze  948:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    949:
1.1       kristaps  950:        if (mdoc->meta.title)
1.76      schwarze  951:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    952:
1.1       kristaps  953:        return(1);
                    954: }
                    955:
                    956: static int
                    957: pre_os(PRE_ARGS)
                    958: {
                    959:
                    960:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.76      schwarze  961:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    962:
1.1       kristaps  963:        if (mdoc->meta.os)
1.76      schwarze  964:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    965:
1.1       kristaps  966:        return(1);
                    967: }
                    968:
                    969: static int
                    970: pre_dd(PRE_ARGS)
                    971: {
                    972:
                    973:        if (mdoc->meta.title || mdoc->meta.os)
1.76      schwarze  974:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    975:
1.1       kristaps  976:        if (mdoc->meta.date)
1.76      schwarze  977:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    978:
1.1       kristaps  979:        return(1);
                    980: }
                    981:
                    982:
                    983: static int
                    984: post_bf(POST_ARGS)
                    985: {
1.65      schwarze  986:        struct mdoc_node *np;
1.66      schwarze  987:        enum mdocargt     arg;
1.65      schwarze  988:
                    989:        /*
                    990:         * Unlike other data pointers, these are "housed" by the HEAD
                    991:         * element, which contains the goods.
                    992:         */
1.1       kristaps  993:
1.65      schwarze  994:        if (MDOC_HEAD != mdoc->last->type) {
                    995:                if (ENDBODY_NOT != mdoc->last->end) {
                    996:                        assert(mdoc->last->pending);
                    997:                        np = mdoc->last->pending->parent->head;
                    998:                } else if (MDOC_BLOCK != mdoc->last->type) {
                    999:                        np = mdoc->last->parent->head;
                   1000:                } else
                   1001:                        np = mdoc->last->head;
                   1002:
                   1003:                assert(np);
                   1004:                assert(MDOC_HEAD == np->type);
                   1005:                assert(MDOC_Bf == np->tok);
                   1006:                assert(np->data.Bf);
                   1007:                mdoc->last->data.Bf = np->data.Bf;
1.1       kristaps 1008:                return(1);
1.65      schwarze 1009:        }
                   1010:
                   1011:        np = mdoc->last;
                   1012:        assert(MDOC_BLOCK == np->parent->type);
                   1013:        assert(MDOC_Bf == np->parent->tok);
                   1014:        np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
1.1       kristaps 1015:
1.65      schwarze 1016:        /*
                   1017:         * Cannot have both argument and parameter.
                   1018:         * If neither is specified, let it through with a warning.
                   1019:         */
1.1       kristaps 1020:
1.65      schwarze 1021:        if (np->parent->args && np->child) {
                   1022:                mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.55      schwarze 1023:                return(0);
1.76      schwarze 1024:        } else if (NULL == np->parent->args && NULL == np->child) {
                   1025:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
                   1026:                return(1);
                   1027:        }
1.65      schwarze 1028:
                   1029:        /* Extract argument into data. */
                   1030:
                   1031:        if (np->parent->args) {
                   1032:                arg = np->parent->args->argv[0].arg;
                   1033:                if (MDOC_Emphasis == arg)
                   1034:                        np->data.Bf->font = FONT_Em;
                   1035:                else if (MDOC_Literal == arg)
                   1036:                        np->data.Bf->font = FONT_Li;
                   1037:                else if (MDOC_Symbolic == arg)
                   1038:                        np->data.Bf->font = FONT_Sy;
                   1039:                else
                   1040:                        abort();
1.10      schwarze 1041:                return(1);
1.55      schwarze 1042:        }
1.1       kristaps 1043:
1.65      schwarze 1044:        /* Extract parameter into data. */
1.1       kristaps 1045:
1.65      schwarze 1046:        if (0 == strcmp(np->child->string, "Em"))
                   1047:                np->data.Bf->font = FONT_Em;
                   1048:        else if (0 == strcmp(np->child->string, "Li"))
                   1049:                np->data.Bf->font = FONT_Li;
                   1050:        else if (0 == strcmp(np->child->string, "Sy"))
                   1051:                np->data.Bf->font = FONT_Sy;
1.76      schwarze 1052:        else
                   1053:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1.1       kristaps 1054:
1.65      schwarze 1055:        return(1);
1.27      schwarze 1056: }
                   1057:
                   1058: static int
                   1059: post_lb(POST_ARGS)
                   1060: {
1.76      schwarze 1061:        const char      *p;
                   1062:        char            *buf;
                   1063:        size_t           sz;
                   1064:
                   1065:        assert(mdoc->last->child);
                   1066:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1067:
                   1068:        p = mdoc_a2lib(mdoc->last->child->string);
1.27      schwarze 1069:
1.76      schwarze 1070:        /* If lookup ok, replace with table value. */
                   1071:
                   1072:        if (p) {
                   1073:                free(mdoc->last->child->string);
                   1074:                mdoc->last->child->string = mandoc_strdup(p);
1.27      schwarze 1075:                return(1);
1.76      schwarze 1076:        }
                   1077:
                   1078:        /* If not, use "library ``xxxx''. */
                   1079:
                   1080:        sz = strlen(mdoc->last->child->string) +
                   1081:                2 + strlen("\\(lqlibrary\\(rq");
                   1082:        buf = mandoc_malloc(sz);
                   1083:        snprintf(buf, sz, "library \\(lq%s\\(rq",
                   1084:                        mdoc->last->child->string);
                   1085:        free(mdoc->last->child->string);
                   1086:        mdoc->last->child->string = buf;
                   1087:        return(1);
1.59      schwarze 1088: }
                   1089:
                   1090: static int
                   1091: post_eoln(POST_ARGS)
                   1092: {
                   1093:
1.76      schwarze 1094:        if (mdoc->last->child)
                   1095:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
                   1096:        return(1);
1.42      schwarze 1097: }
                   1098:
                   1099:
                   1100: static int
                   1101: post_vt(POST_ARGS)
                   1102: {
                   1103:        const struct mdoc_node *n;
                   1104:
                   1105:        /*
                   1106:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                   1107:         * have different syntaxes (yet more context-sensitive
                   1108:         * behaviour).  ELEM types must have a child; BLOCK types,
                   1109:         * specifically the BODY, should only have TEXT children.
                   1110:         */
                   1111:
                   1112:        if (MDOC_ELEM == mdoc->last->type)
                   1113:                return(eerr_ge1(mdoc));
                   1114:        if (MDOC_BODY != mdoc->last->type)
                   1115:                return(1);
                   1116:
                   1117:        for (n = mdoc->last->child; n; n = n->next)
1.52      schwarze 1118:                if (MDOC_TEXT != n->type)
1.76      schwarze 1119:                        mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.42      schwarze 1120:
                   1121:        return(1);
1.1       kristaps 1122: }
                   1123:
                   1124:
                   1125: static int
                   1126: post_nm(POST_ARGS)
                   1127: {
1.76      schwarze 1128:        char             buf[BUFSIZ];
1.1       kristaps 1129:
1.76      schwarze 1130:        /* If no child specified, make sure we have the meta name. */
                   1131:
                   1132:        if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
                   1133:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1.1       kristaps 1134:                return(1);
1.76      schwarze 1135:        } else if (mdoc->meta.name)
1.1       kristaps 1136:                return(1);
1.76      schwarze 1137:
                   1138:        /* If no meta name, set it from the child. */
                   1139:
                   1140:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1141:                return(0);
                   1142:
                   1143:        mdoc->meta.name = mandoc_strdup(buf);
                   1144:
                   1145:        return(1);
                   1146: }
                   1147:
                   1148: static int
                   1149: post_literal(POST_ARGS)
                   1150: {
                   1151:
                   1152:        /*
                   1153:         * The `Dl' (note "el" not "one") and `Bd' macros unset the
                   1154:         * MDOC_LITERAL flag as they leave.  Note that `Bd' only sets
                   1155:         * this in literal mode, but it doesn't hurt to just switch it
                   1156:         * off in general since displays can't be nested.
                   1157:         */
                   1158:
                   1159:        if (MDOC_BODY == mdoc->last->type)
                   1160:                mdoc->flags &= ~MDOC_LITERAL;
                   1161:
                   1162:        return(1);
1.1       kristaps 1163: }
                   1164:
1.76      schwarze 1165: static int
                   1166: post_defaults(POST_ARGS)
                   1167: {
                   1168:        struct mdoc_node *nn;
                   1169:
                   1170:        /*
                   1171:         * The `Ar' defaults to "file ..." if no value is provided as an
                   1172:         * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
                   1173:         * gets an empty string.
                   1174:         */
                   1175:
                   1176:        if (mdoc->last->child)
                   1177:                return(1);
                   1178:
                   1179:        nn = mdoc->last;
                   1180:        mdoc->next = MDOC_NEXT_CHILD;
                   1181:
                   1182:        switch (nn->tok) {
                   1183:        case (MDOC_Ar):
                   1184:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
                   1185:                        return(0);
                   1186:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
                   1187:                        return(0);
                   1188:                break;
                   1189:        case (MDOC_At):
                   1190:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
                   1191:                        return(0);
                   1192:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
                   1193:                        return(0);
                   1194:                break;
                   1195:        case (MDOC_Li):
                   1196:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
                   1197:                        return(0);
                   1198:                break;
                   1199:        case (MDOC_Pa):
                   1200:                /* FALLTHROUGH */
                   1201:        case (MDOC_Mt):
                   1202:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
                   1203:                        return(0);
                   1204:                break;
                   1205:        default:
                   1206:                abort();
                   1207:                /* NOTREACHED */
                   1208:        }
                   1209:
                   1210:        mdoc->last = nn;
                   1211:        return(1);
                   1212: }
1.1       kristaps 1213:
                   1214: static int
                   1215: post_at(POST_ARGS)
                   1216: {
1.76      schwarze 1217:        const char       *p, *q;
                   1218:        char             *buf;
                   1219:        size_t            sz;
1.1       kristaps 1220:
1.76      schwarze 1221:        /*
                   1222:         * If we have a child, look it up in the standard keys.  If a
                   1223:         * key exist, use that instead of the child; if it doesn't,
                   1224:         * prefix "AT&T UNIX " to the existing data.
                   1225:         */
                   1226:
1.1       kristaps 1227:        if (NULL == mdoc->last->child)
                   1228:                return(1);
1.76      schwarze 1229:
1.55      schwarze 1230:        assert(MDOC_TEXT == mdoc->last->child->type);
1.76      schwarze 1231:        p = mdoc_a2att(mdoc->last->child->string);
                   1232:
                   1233:        if (p) {
                   1234:                free(mdoc->last->child->string);
                   1235:                mdoc->last->child->string = mandoc_strdup(p);
                   1236:        } else {
                   1237:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
                   1238:                p = "AT&T UNIX ";
                   1239:                q = mdoc->last->child->string;
                   1240:                sz = strlen(p) + strlen(q) + 1;
                   1241:                buf = mandoc_malloc(sz);
                   1242:                strlcpy(buf, p, sz);
                   1243:                strlcat(buf, q, sz);
                   1244:                free(mdoc->last->child->string);
                   1245:                mdoc->last->child->string = buf;
                   1246:        }
                   1247:
                   1248:        return(1);
1.1       kristaps 1249: }
                   1250:
                   1251: static int
                   1252: post_an(POST_ARGS)
                   1253: {
1.65      schwarze 1254:        struct mdoc_node *np;
1.1       kristaps 1255:
1.65      schwarze 1256:        np = mdoc->last;
1.80    ! schwarze 1257:        if (AUTH__NONE != np->data.An->auth && np->child)
1.72      schwarze 1258:                return(eerr_eq0(mdoc));
1.76      schwarze 1259:
1.72      schwarze 1260:        /*
                   1261:         * FIXME: make this ewarn and make sure that the front-ends
                   1262:         * don't print the arguments.
                   1263:         */
1.80    ! schwarze 1264:        if (AUTH__NONE != np->data.An->auth || np->child)
1.1       kristaps 1265:                return(1);
1.76      schwarze 1266:
                   1267:        mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS);
                   1268:        return(1);
1.1       kristaps 1269: }
                   1270:
                   1271:
                   1272: static int
                   1273: post_it(POST_ARGS)
                   1274: {
1.60      schwarze 1275:        int               i, cols, rc;
                   1276:        enum mdoc_list    lt;
1.1       kristaps 1277:        struct mdoc_node *n, *c;
1.60      schwarze 1278:        enum mandocerr    er;
1.1       kristaps 1279:
                   1280:        if (MDOC_BLOCK != mdoc->last->type)
                   1281:                return(1);
                   1282:
                   1283:        n = mdoc->last->parent->parent;
1.65      schwarze 1284:        assert(n->data.Bl);
                   1285:        lt = n->data.Bl->type;
1.60      schwarze 1286:
                   1287:        if (LIST__NONE == lt) {
1.55      schwarze 1288:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1.76      schwarze 1289:                return(1);
1.55      schwarze 1290:        }
1.1       kristaps 1291:
1.60      schwarze 1292:        switch (lt) {
                   1293:        case (LIST_tag):
                   1294:                if (mdoc->last->head->child)
1.1       kristaps 1295:                        break;
1.60      schwarze 1296:                /* FIXME: give this a dummy value. */
1.76      schwarze 1297:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1298:                break;
1.60      schwarze 1299:        case (LIST_hang):
1.1       kristaps 1300:                /* FALLTHROUGH */
1.60      schwarze 1301:        case (LIST_ohang):
1.1       kristaps 1302:                /* FALLTHROUGH */
1.60      schwarze 1303:        case (LIST_inset):
1.1       kristaps 1304:                /* FALLTHROUGH */
1.60      schwarze 1305:        case (LIST_diag):
1.1       kristaps 1306:                if (NULL == mdoc->last->head->child)
1.76      schwarze 1307:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1308:                break;
1.60      schwarze 1309:        case (LIST_bullet):
1.1       kristaps 1310:                /* FALLTHROUGH */
1.60      schwarze 1311:        case (LIST_dash):
1.1       kristaps 1312:                /* FALLTHROUGH */
1.60      schwarze 1313:        case (LIST_enum):
1.1       kristaps 1314:                /* FALLTHROUGH */
1.60      schwarze 1315:        case (LIST_hyphen):
1.64      schwarze 1316:                if (NULL == mdoc->last->body->child)
1.76      schwarze 1317:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.1       kristaps 1318:                /* FALLTHROUGH */
1.60      schwarze 1319:        case (LIST_item):
1.1       kristaps 1320:                if (mdoc->last->head->child)
1.76      schwarze 1321:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1.1       kristaps 1322:                break;
1.60      schwarze 1323:        case (LIST_column):
1.65      schwarze 1324:                cols = (int)n->data.Bl->ncols;
1.60      schwarze 1325:
                   1326:                assert(NULL == mdoc->last->head->child);
                   1327:
                   1328:                if (NULL == mdoc->last->body->child)
1.76      schwarze 1329:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.60      schwarze 1330:
                   1331:                for (i = 0, c = mdoc->last->child; c; c = c->next)
                   1332:                        if (MDOC_BODY == c->type)
                   1333:                                i++;
                   1334:
                   1335:                if (i < cols)
                   1336:                        er = MANDOCERR_ARGCOUNT;
                   1337:                else if (i == cols || i == cols + 1)
1.1       kristaps 1338:                        break;
1.60      schwarze 1339:                else
                   1340:                        er = MANDOCERR_SYNTARGCOUNT;
1.32      schwarze 1341:
1.60      schwarze 1342:                rc = mdoc_vmsg(mdoc, er,
1.55      schwarze 1343:                                mdoc->last->line, mdoc->last->pos,
                   1344:                                "columns == %d (have %d)", cols, i);
1.60      schwarze 1345:                return(rc);
1.1       kristaps 1346:        default:
                   1347:                break;
                   1348:        }
                   1349:
                   1350:        return(1);
                   1351: }
                   1352:
                   1353: static int
1.76      schwarze 1354: post_bl_block(POST_ARGS)
1.11      schwarze 1355: {
1.60      schwarze 1356:        struct mdoc_node *n;
1.11      schwarze 1357:
1.76      schwarze 1358:        /*
                   1359:         * These are fairly complicated, so we've broken them into two
                   1360:         * functions.  post_bl_block_tag() is called when a -tag is
                   1361:         * specified, but no -width (it must be guessed).  The second
                   1362:         * when a -width is specified (macro indicators must be
                   1363:         * rewritten into real lengths).
                   1364:         */
                   1365:
                   1366:        n = mdoc->last;
1.11      schwarze 1367:
1.76      schwarze 1368:        if (LIST_tag == n->data.Bl->type &&
                   1369:                        NULL == n->data.Bl->width) {
                   1370:                if ( ! post_bl_block_tag(mdoc))
1.60      schwarze 1371:                        return(0);
1.76      schwarze 1372:        } else if (NULL != n->data.Bl->width) {
                   1373:                if ( ! post_bl_block_width(mdoc))
                   1374:                        return(0);
                   1375:        } else
1.60      schwarze 1376:                return(1);
1.11      schwarze 1377:
1.76      schwarze 1378:        assert(n->data.Bl->width);
                   1379:        return(1);
1.11      schwarze 1380: }
                   1381:
                   1382: static int
1.76      schwarze 1383: post_bl_block_width(POST_ARGS)
1.1       kristaps 1384: {
1.76      schwarze 1385:        size_t            width;
                   1386:        int               i;
                   1387:        enum mdoct        tok;
                   1388:        struct mdoc_node *n;
                   1389:        char              buf[NUMSIZ];
1.1       kristaps 1390:
1.76      schwarze 1391:        n = mdoc->last;
1.1       kristaps 1392:
1.41      schwarze 1393:        /*
1.76      schwarze 1394:         * Calculate the real width of a list from the -width string,
                   1395:         * which may contain a macro (with a known default width), a
                   1396:         * literal string, or a scaling width.
                   1397:         *
                   1398:         * If the value to -width is a macro, then we re-write it to be
                   1399:         * the macro's width as set in share/tmac/mdoc/doc-common.
1.41      schwarze 1400:         */
                   1401:
1.76      schwarze 1402:        if (0 == strcmp(n->data.Bl->width, "Ds"))
                   1403:                width = 6;
                   1404:        else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl->width)))
                   1405:                return(1);
                   1406:        else if (0 == (width = mdoc_macro2len(tok)))  {
                   1407:                mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
                   1408:                return(1);
1.1       kristaps 1409:        }
                   1410:
1.76      schwarze 1411:        /* The value already exists: free and reallocate it. */
                   1412:
                   1413:        assert(n->args);
                   1414:
                   1415:        for (i = 0; i < (int)n->args->argc; i++)
                   1416:                if (MDOC_Width == n->args->argv[i].arg)
                   1417:                        break;
                   1418:
                   1419:        assert(i < (int)n->args->argc);
                   1420:
                   1421:        snprintf(buf, NUMSIZ, "%zun", width);
                   1422:        free(n->args->argv[i].value[0]);
                   1423:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                   1424:
                   1425:        /* Set our width! */
                   1426:        n->data.Bl->width = n->args->argv[i].value[0];
1.1       kristaps 1427:        return(1);
                   1428: }
                   1429:
                   1430: static int
1.76      schwarze 1431: post_bl_block_tag(POST_ARGS)
1.1       kristaps 1432: {
1.76      schwarze 1433:        struct mdoc_node *n, *nn;
                   1434:        size_t            sz, ssz;
                   1435:        int               i;
                   1436:        char              buf[NUMSIZ];
                   1437:
                   1438:        /*
                   1439:         * Calculate the -width for a `Bl -tag' list if it hasn't been
                   1440:         * provided.  Uses the first head macro.  NOTE AGAIN: this is
                   1441:         * ONLY if the -width argument has NOT been provided.  See
                   1442:         * post_bl_block_width() for converting the -width string.
                   1443:         */
                   1444:
                   1445:        sz = 10;
                   1446:        n = mdoc->last;
                   1447:
                   1448:        for (nn = n->body->child; nn; nn = nn->next) {
                   1449:                if (MDOC_It != nn->tok)
                   1450:                        continue;
                   1451:
                   1452:                assert(MDOC_BLOCK == nn->type);
                   1453:                nn = nn->head->child;
                   1454:
                   1455:                if (nn == NULL)
                   1456:                        break;
1.1       kristaps 1457:
1.76      schwarze 1458:                if (MDOC_TEXT == nn->type) {
                   1459:                        sz = strlen(nn->string) + 1;
1.1       kristaps 1460:                        break;
1.76      schwarze 1461:                }
                   1462:
                   1463:                if (0 != (ssz = mdoc_macro2len(nn->tok)))
                   1464:                        sz = ssz;
                   1465:
1.1       kristaps 1466:                break;
1.76      schwarze 1467:        }
                   1468:
                   1469:        /* Defaults to ten ens. */
                   1470:
                   1471:        snprintf(buf, NUMSIZ, "%zun", sz);
1.1       kristaps 1472:
1.76      schwarze 1473:        /*
                   1474:         * We have to dynamically add this to the macro's argument list.
                   1475:         * We're guaranteed that a MDOC_Width doesn't already exist.
                   1476:         */
1.1       kristaps 1477:
1.76      schwarze 1478:        assert(n->args);
                   1479:        i = (int)(n->args->argc)++;
1.1       kristaps 1480:
1.76      schwarze 1481:        n->args->argv = mandoc_realloc(n->args->argv,
                   1482:                        n->args->argc * sizeof(struct mdoc_argv));
1.1       kristaps 1483:
1.76      schwarze 1484:        n->args->argv[i].arg = MDOC_Width;
                   1485:        n->args->argv[i].line = n->line;
                   1486:        n->args->argv[i].pos = n->pos;
                   1487:        n->args->argv[i].sz = 1;
                   1488:        n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                   1489:        n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1       kristaps 1490:
1.76      schwarze 1491:        /* Set our width! */
                   1492:        n->data.Bl->width = n->args->argv[i].value[0];
                   1493:        return(1);
1.1       kristaps 1494: }
                   1495:
                   1496:
                   1497: static int
1.76      schwarze 1498: post_bl_head(POST_ARGS)
1.1       kristaps 1499: {
1.76      schwarze 1500:        struct mdoc_node *np, *nn, *nnp;
                   1501:        int               i, j;
1.1       kristaps 1502:
1.76      schwarze 1503:        if (LIST_column != mdoc->last->data.Bl->type)
                   1504:                /* FIXME: this should be ERROR class... */
                   1505:                return(hwarn_eq0(mdoc));
1.36      schwarze 1506:
1.76      schwarze 1507:        /*
                   1508:         * Convert old-style lists, where the column width specifiers
                   1509:         * trail as macro parameters, to the new-style ("normal-form")
                   1510:         * lists where they're argument values following -column.
                   1511:         */
1.36      schwarze 1512:
1.76      schwarze 1513:        /* First, disallow both types and allow normal-form. */
1.36      schwarze 1514:
1.76      schwarze 1515:        /*
                   1516:         * TODO: technically, we can accept both and just merge the two
                   1517:         * lists, but I'll leave that for another day.
                   1518:         */
                   1519:
                   1520:        if (mdoc->last->data.Bl->ncols && mdoc->last->nchild) {
                   1521:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
                   1522:                return(0);
                   1523:        } else if (NULL == mdoc->last->child)
                   1524:                return(1);
                   1525:
                   1526:        np = mdoc->last->parent;
                   1527:        assert(np->args);
                   1528:
                   1529:        for (j = 0; j < (int)np->args->argc; j++)
                   1530:                if (MDOC_Column == np->args->argv[j].arg)
                   1531:                        break;
                   1532:
                   1533:        assert(j < (int)np->args->argc);
                   1534:        assert(0 == np->args->argv[j].sz);
                   1535:
                   1536:        /*
                   1537:         * Accomodate for new-style groff column syntax.  Shuffle the
                   1538:         * child nodes, all of which must be TEXT, as arguments for the
                   1539:         * column field.  Then, delete the head children.
                   1540:         */
                   1541:
                   1542:        np->args->argv[j].sz = (size_t)mdoc->last->nchild;
                   1543:        np->args->argv[j].value = mandoc_malloc
                   1544:                ((size_t)mdoc->last->nchild * sizeof(char *));
                   1545:
                   1546:        mdoc->last->data.Bl->ncols = np->args->argv[j].sz;
                   1547:        mdoc->last->data.Bl->cols = (const char **)np->args->argv[j].value;
                   1548:
                   1549:        for (i = 0, nn = mdoc->last->child; nn; i++) {
                   1550:                np->args->argv[j].value[i] = nn->string;
                   1551:                nn->string = NULL;
                   1552:                nnp = nn;
                   1553:                nn = nn->next;
                   1554:                mdoc_node_delete(NULL, nnp);
                   1555:        }
                   1556:
                   1557:        mdoc->last->nchild = 0;
                   1558:        mdoc->last->child = NULL;
                   1559:
                   1560:        return(1);
                   1561: }
                   1562:
                   1563: static int
                   1564: post_bl(POST_ARGS)
                   1565: {
                   1566:        struct mdoc_node        *n;
                   1567:
                   1568:        if (MDOC_HEAD == mdoc->last->type)
                   1569:                return(post_bl_head(mdoc));
                   1570:        if (MDOC_BLOCK == mdoc->last->type)
                   1571:                return(post_bl_block(mdoc));
1.36      schwarze 1572:        if (MDOC_BODY != mdoc->last->type)
                   1573:                return(1);
                   1574:
1.76      schwarze 1575:        for (n = mdoc->last->child; n; n = n->next) {
1.79      schwarze 1576:                switch (n->tok) {
                   1577:                case (MDOC_Lp):
                   1578:                        /* FALLTHROUGH */
                   1579:                case (MDOC_Pp):
                   1580:                        mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
                   1581:                        /* FALLTHROUGH */
                   1582:                case (MDOC_It):
                   1583:                        /* FALLTHROUGH */
                   1584:                case (MDOC_Sm):
1.76      schwarze 1585:                        continue;
1.79      schwarze 1586:                default:
                   1587:                        break;
                   1588:                }
                   1589:
1.76      schwarze 1590:                mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
                   1591:                return(0);
                   1592:        }
                   1593:
                   1594:        return(1);
                   1595: }
                   1596:
                   1597: static int
                   1598: ebool(struct mdoc *mdoc)
                   1599: {
                   1600:
                   1601:        if (NULL == mdoc->last->child)
                   1602:                return(1);
                   1603:
                   1604:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1605:
                   1606:        if (0 == strcmp(mdoc->last->child->string, "on"))
                   1607:                return(1);
                   1608:        if (0 == strcmp(mdoc->last->child->string, "off"))
                   1609:                return(1);
                   1610:
                   1611:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
                   1612:        return(1);
                   1613: }
                   1614:
                   1615: static int
                   1616: post_root(POST_ARGS)
                   1617: {
                   1618:        int               erc;
                   1619:        struct mdoc_node *n;
                   1620:
                   1621:        erc = 0;
                   1622:
                   1623:        /* Check that we have a finished prologue. */
                   1624:
                   1625:        if ( ! (MDOC_PBODY & mdoc->flags)) {
                   1626:                erc++;
                   1627:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
                   1628:        }
                   1629:
                   1630:        n = mdoc->first;
                   1631:        assert(n);
                   1632:
                   1633:        /* Check that we begin with a proper `Sh'. */
                   1634:
                   1635:        if (NULL == n->child) {
                   1636:                erc++;
                   1637:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1638:        } else if (MDOC_BLOCK != n->child->type ||
                   1639:                        MDOC_Sh != n->child->tok) {
                   1640:                erc++;
                   1641:                /* Can this be lifted?  See rxdebug.1 for example. */
                   1642:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1643:        }
                   1644:
                   1645:        return(erc ? 0 : 1);
                   1646: }
                   1647:
                   1648: static int
                   1649: post_st(POST_ARGS)
                   1650: {
                   1651:        const char      *p;
                   1652:
                   1653:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1654:
                   1655:        p = mdoc_a2st(mdoc->last->child->string);
                   1656:
                   1657:        if (p == NULL) {
                   1658:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
                   1659:                mdoc_node_delete(mdoc, mdoc->last);
                   1660:        } else {
                   1661:                free(mdoc->last->child->string);
                   1662:                mdoc->last->child->string = mandoc_strdup(p);
                   1663:        }
                   1664:
                   1665:        return(1);
                   1666: }
                   1667:
                   1668: static int
                   1669: post_rs(POST_ARGS)
                   1670: {
                   1671:        struct mdoc_node *nn, *next, *prev;
                   1672:        int               i, j;
                   1673:
                   1674:        if (MDOC_BODY != mdoc->last->type)
                   1675:                return(1);
                   1676:
                   1677:        /*
                   1678:         * Make sure only certain types of nodes are allowed within the
                   1679:         * the `Rs' body.  Delete offending nodes and raise a warning.
                   1680:         * Do this before re-ordering for the sake of clarity.
                   1681:         */
                   1682:
                   1683:        next = NULL;
                   1684:        for (nn = mdoc->last->child; nn; nn = next) {
                   1685:                for (i = 0; i < RSORD_MAX; i++)
                   1686:                        if (nn->tok == rsord[i])
                   1687:                                break;
                   1688:
                   1689:                if (i < RSORD_MAX) {
                   1690:                        next = nn->next;
                   1691:                        continue;
                   1692:                }
                   1693:
                   1694:                next = nn->next;
                   1695:                mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
                   1696:                mdoc_node_delete(mdoc, nn);
                   1697:        }
                   1698:
                   1699:        /*
                   1700:         * The full `Rs' block needs special handling to order the
                   1701:         * sub-elements according to `rsord'.  Pick through each element
                   1702:         * and correctly order it.  This is a insertion sort.
                   1703:         */
                   1704:
                   1705:        next = NULL;
                   1706:        for (nn = mdoc->last->child->next; nn; nn = next) {
                   1707:                /* Determine order of `nn'. */
                   1708:                for (i = 0; i < RSORD_MAX; i++)
                   1709:                        if (rsord[i] == nn->tok)
                   1710:                                break;
                   1711:
                   1712:                /*
                   1713:                 * Remove `nn' from the chain.  This somewhat
                   1714:                 * repeats mdoc_node_unlink(), but since we're
                   1715:                 * just re-ordering, there's no need for the
                   1716:                 * full unlink process.
                   1717:                 */
                   1718:
                   1719:                if (NULL != (next = nn->next))
                   1720:                        next->prev = nn->prev;
                   1721:
                   1722:                if (NULL != (prev = nn->prev))
                   1723:                        prev->next = nn->next;
                   1724:
                   1725:                nn->prev = nn->next = NULL;
                   1726:
                   1727:                /*
                   1728:                 * Scan back until we reach a node that's
                   1729:                 * ordered before `nn'.
                   1730:                 */
                   1731:
                   1732:                for ( ; prev ; prev = prev->prev) {
                   1733:                        /* Determine order of `prev'. */
                   1734:                        for (j = 0; j < RSORD_MAX; j++)
                   1735:                                if (rsord[j] == prev->tok)
                   1736:                                        break;
                   1737:
                   1738:                        if (j <= i)
                   1739:                                break;
                   1740:                }
                   1741:
                   1742:                /*
                   1743:                 * Set `nn' back into its correct place in front
                   1744:                 * of the `prev' node.
                   1745:                 */
                   1746:
                   1747:                nn->prev = prev;
                   1748:
                   1749:                if (prev) {
                   1750:                        if (prev->next)
                   1751:                                prev->next->prev = nn;
                   1752:                        nn->next = prev->next;
                   1753:                        prev->next = nn;
                   1754:                } else {
                   1755:                        mdoc->last->child->prev = nn;
                   1756:                        nn->next = mdoc->last->child;
                   1757:                        mdoc->last->child = nn;
1.36      schwarze 1758:                }
1.76      schwarze 1759:        }
1.36      schwarze 1760:
                   1761:        return(1);
1.1       kristaps 1762: }
                   1763:
                   1764: static int
                   1765: post_sh(POST_ARGS)
                   1766: {
                   1767:
                   1768:        if (MDOC_HEAD == mdoc->last->type)
                   1769:                return(post_sh_head(mdoc));
                   1770:        if (MDOC_BODY == mdoc->last->type)
                   1771:                return(post_sh_body(mdoc));
                   1772:
                   1773:        return(1);
                   1774: }
                   1775:
                   1776: static int
                   1777: post_sh_body(POST_ARGS)
                   1778: {
                   1779:        struct mdoc_node *n;
                   1780:
1.26      schwarze 1781:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1782:                return(1);
                   1783:
                   1784:        /*
                   1785:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1786:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1787:         * children of the BODY declaration can also be "text".
                   1788:         */
                   1789:
1.76      schwarze 1790:        if (NULL == (n = mdoc->last->child)) {
                   1791:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1792:                return(1);
                   1793:        }
1.1       kristaps 1794:
                   1795:        for ( ; n && n->next; n = n->next) {
                   1796:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1797:                        continue;
                   1798:                if (MDOC_TEXT == n->type)
                   1799:                        continue;
1.76      schwarze 1800:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1.1       kristaps 1801:        }
                   1802:
1.34      schwarze 1803:        assert(n);
1.25      schwarze 1804:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1805:                return(1);
1.76      schwarze 1806:
                   1807:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1808:        return(1);
1.1       kristaps 1809: }
                   1810:
                   1811: static int
                   1812: post_sh_head(POST_ARGS)
                   1813: {
1.76      schwarze 1814:        char             buf[BUFSIZ];
                   1815:        enum mdoc_sec    sec;
1.1       kristaps 1816:
                   1817:        /*
                   1818:         * Process a new section.  Sections are either "named" or
1.76      schwarze 1819:         * "custom".  Custom sections are user-defined, while named ones
                   1820:         * follow a conventional order and may only appear in certain
                   1821:         * manual sections.
1.1       kristaps 1822:         */
                   1823:
1.76      schwarze 1824:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1825:                return(0);
                   1826:
                   1827:        sec = mdoc_str2sec(buf);
                   1828:
                   1829:        /* The NAME should be first. */
1.55      schwarze 1830:
1.76      schwarze 1831:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
                   1832:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
1.1       kristaps 1833:
1.76      schwarze 1834:        /* The SYNOPSIS gets special attention in other areas. */
1.9       schwarze 1835:
1.76      schwarze 1836:        if (SEC_SYNOPSIS == sec)
                   1837:                mdoc->flags |= MDOC_SYNOPSIS;
                   1838:        else
                   1839:                mdoc->flags &= ~MDOC_SYNOPSIS;
1.1       kristaps 1840:
1.76      schwarze 1841:        /* Mark our last section. */
1.1       kristaps 1842:
1.76      schwarze 1843:        mdoc->lastsec = sec;
1.1       kristaps 1844:
1.76      schwarze 1845:        /* We don't care about custom sections after this. */
1.51      schwarze 1846:
1.1       kristaps 1847:        if (SEC_CUSTOM == sec)
                   1848:                return(1);
1.51      schwarze 1849:
1.76      schwarze 1850:        /*
                   1851:         * Check whether our non-custom section is being repeated or is
                   1852:         * out of order.
                   1853:         */
                   1854:
1.1       kristaps 1855:        if (sec == mdoc->lastnamed)
1.76      schwarze 1856:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.51      schwarze 1857:
1.1       kristaps 1858:        if (sec < mdoc->lastnamed)
1.76      schwarze 1859:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
                   1860:
                   1861:        /* Mark the last named section. */
                   1862:
                   1863:        mdoc->lastnamed = sec;
                   1864:
                   1865:        /* Check particular section/manual conventions. */
1.1       kristaps 1866:
1.76      schwarze 1867:        assert(mdoc->meta.msec);
1.1       kristaps 1868:
                   1869:        switch (sec) {
1.76      schwarze 1870:        case (SEC_RETURN_VALUES):
                   1871:                /* FALLTHROUGH */
                   1872:        case (SEC_ERRORS):
                   1873:                /* FALLTHROUGH */
1.1       kristaps 1874:        case (SEC_LIBRARY):
1.54      schwarze 1875:                if (*mdoc->meta.msec == '2')
                   1876:                        break;
                   1877:                if (*mdoc->meta.msec == '3')
                   1878:                        break;
                   1879:                if (*mdoc->meta.msec == '9')
1.1       kristaps 1880:                        break;
1.76      schwarze 1881:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
                   1882:                break;
1.1       kristaps 1883:        default:
                   1884:                break;
                   1885:        }
                   1886:
1.70      schwarze 1887:        return(1);
                   1888: }
                   1889:
1.76      schwarze 1890: static int
1.79      schwarze 1891: post_ignpar(POST_ARGS)
                   1892: {
                   1893:        struct mdoc_node *np;
                   1894:
                   1895:        if (MDOC_BODY != mdoc->last->type)
                   1896:                return(1);
                   1897:
                   1898:        if (NULL != (np = mdoc->last->child))
                   1899:                if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
                   1900:                        mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
                   1901:                        mdoc_node_delete(mdoc, np);
                   1902:                }
                   1903:
                   1904:        if (NULL != (np = mdoc->last->last))
                   1905:                if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
                   1906:                        mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
                   1907:                        mdoc_node_delete(mdoc, np);
                   1908:                }
                   1909:
                   1910:        return(1);
                   1911: }
                   1912:
                   1913: static int
1.76      schwarze 1914: pre_ts(PRE_ARGS)
                   1915: {
                   1916:
                   1917:        if (MDOC_BLOCK == mdoc->last->type)
                   1918:                mdoc->last->data.TS = tbl_alloc();
                   1919:
                   1920:        return(1);
                   1921: }
1.70      schwarze 1922:
                   1923: static int
1.76      schwarze 1924: pre_par(PRE_ARGS)
1.70      schwarze 1925: {
                   1926:
                   1927:        if (NULL == mdoc->last)
                   1928:                return(1);
1.79      schwarze 1929:        if (MDOC_ELEM != n->type && MDOC_BLOCK != n->type)
                   1930:                return(1);
1.70      schwarze 1931:
1.76      schwarze 1932:        /*
                   1933:         * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
                   1934:         * block:  `Lp', `Pp', or non-compact `Bd' or `Bl'.
                   1935:         */
1.70      schwarze 1936:
                   1937:        if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
                   1938:                return(1);
                   1939:        if (MDOC_Bl == n->tok && n->data.Bl->comp)
                   1940:                return(1);
                   1941:        if (MDOC_Bd == n->tok && n->data.Bd->comp)
1.79      schwarze 1942:                return(1);
                   1943:        if (MDOC_It == n->tok && n->parent->data.Bl->comp)
1.70      schwarze 1944:                return(1);
                   1945:
                   1946:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
                   1947:        mdoc_node_delete(mdoc, mdoc->last);
1.1       kristaps 1948:        return(1);
                   1949: }
1.76      schwarze 1950:
                   1951: static int
                   1952: pre_literal(PRE_ARGS)
                   1953: {
                   1954:
                   1955:        if (MDOC_BODY != n->type)
                   1956:                return(1);
                   1957:
                   1958:        /*
                   1959:         * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
                   1960:         * -unfilled' macros set MDOC_LITERAL on entrance to the body.
                   1961:         */
                   1962:
                   1963:        switch (n->tok) {
                   1964:        case (MDOC_Dl):
                   1965:                mdoc->flags |= MDOC_LITERAL;
                   1966:                break;
                   1967:        case (MDOC_Bd):
                   1968:                assert(n->data.Bd);
                   1969:                if (DISP_literal == n->data.Bd->type)
                   1970:                        mdoc->flags |= MDOC_LITERAL;
                   1971:                if (DISP_unfilled == n->data.Bd->type)
                   1972:                        mdoc->flags |= MDOC_LITERAL;
                   1973:                break;
                   1974:        default:
                   1975:                abort();
                   1976:                /* NOTREACHED */
                   1977:        }
                   1978:
                   1979:        return(1);
                   1980: }
                   1981:
                   1982: static int
                   1983: post_dd(POST_ARGS)
                   1984: {
                   1985:        char              buf[DATESIZE];
                   1986:        struct mdoc_node *n;
                   1987:
                   1988:        n = mdoc->last;
                   1989:
                   1990:        if (NULL == n->child) {
                   1991:                mdoc->meta.date = time(NULL);
                   1992:                return(1);
                   1993:        }
                   1994:
                   1995:        if ( ! concat(mdoc, buf, n->child, DATESIZE))
                   1996:                return(0);
                   1997:
                   1998:        mdoc->meta.date = mandoc_a2time
                   1999:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                   2000:
                   2001:        if (0 == mdoc->meta.date) {
                   2002:                mdoc_nmsg(mdoc, n, MANDOCERR_BADDATE);
                   2003:                mdoc->meta.date = time(NULL);
                   2004:        }
                   2005:
                   2006:        return(1);
                   2007: }
                   2008:
                   2009: static int
                   2010: post_dt(POST_ARGS)
                   2011: {
                   2012:        struct mdoc_node *nn, *n;
                   2013:        const char       *cp;
                   2014:        char             *p;
                   2015:
                   2016:        n = mdoc->last;
                   2017:
                   2018:        if (mdoc->meta.title)
                   2019:                free(mdoc->meta.title);
                   2020:        if (mdoc->meta.vol)
                   2021:                free(mdoc->meta.vol);
                   2022:        if (mdoc->meta.arch)
                   2023:                free(mdoc->meta.arch);
                   2024:
                   2025:        mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
                   2026:
                   2027:        /* First make all characters uppercase. */
                   2028:
                   2029:        if (NULL != (nn = n->child))
                   2030:                for (p = nn->string; *p; p++) {
                   2031:                        if (toupper((u_char)*p) == *p)
                   2032:                                continue;
                   2033:
                   2034:                        /*
                   2035:                         * FIXME: don't be lazy: have this make all
                   2036:                         * characters be uppercase and just warn once.
                   2037:                         */
                   2038:                        mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
                   2039:                        break;
                   2040:                }
                   2041:
                   2042:        /* Handles: `.Dt'
                   2043:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                   2044:         */
                   2045:
                   2046:        if (NULL == (nn = n->child)) {
                   2047:                /* XXX: make these macro values. */
                   2048:                /* FIXME: warn about missing values. */
                   2049:                mdoc->meta.title = mandoc_strdup("UNKNOWN");
                   2050:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2051:                mdoc->meta.msec = mandoc_strdup("1");
                   2052:                return(1);
                   2053:        }
                   2054:
                   2055:        /* Handles: `.Dt TITLE'
                   2056:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                   2057:         */
                   2058:
                   2059:        mdoc->meta.title = mandoc_strdup
                   2060:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
                   2061:
                   2062:        if (NULL == (nn = nn->next)) {
                   2063:                /* FIXME: warn about missing msec. */
                   2064:                /* XXX: make this a macro value. */
                   2065:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2066:                mdoc->meta.msec = mandoc_strdup("1");
                   2067:                return(1);
                   2068:        }
                   2069:
                   2070:        /* Handles: `.Dt TITLE SEC'
                   2071:         *   --> title = TITLE, volume = SEC is msec ?
                   2072:         *           format(msec) : SEC,
                   2073:         *       msec = SEC is msec ? atoi(msec) : 0,
                   2074:         *       arch = NULL
                   2075:         */
                   2076:
                   2077:        cp = mdoc_a2msec(nn->string);
                   2078:        if (cp) {
                   2079:                mdoc->meta.vol = mandoc_strdup(cp);
                   2080:                mdoc->meta.msec = mandoc_strdup(nn->string);
                   2081:        } else {
                   2082:                mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
                   2083:                mdoc->meta.vol = mandoc_strdup(nn->string);
                   2084:                mdoc->meta.msec = mandoc_strdup(nn->string);
                   2085:        }
                   2086:
                   2087:        if (NULL == (nn = nn->next))
                   2088:                return(1);
                   2089:
                   2090:        /* Handles: `.Dt TITLE SEC VOL'
                   2091:         *   --> title = TITLE, volume = VOL is vol ?
                   2092:         *       format(VOL) :
                   2093:         *           VOL is arch ? format(arch) :
                   2094:         *               VOL
                   2095:         */
                   2096:
                   2097:        cp = mdoc_a2vol(nn->string);
                   2098:        if (cp) {
                   2099:                free(mdoc->meta.vol);
                   2100:                mdoc->meta.vol = mandoc_strdup(cp);
                   2101:        } else {
                   2102:                /* FIXME: warn about bad arch. */
                   2103:                cp = mdoc_a2arch(nn->string);
                   2104:                if (NULL == cp) {
                   2105:                        free(mdoc->meta.vol);
                   2106:                        mdoc->meta.vol = mandoc_strdup(nn->string);
                   2107:                } else
                   2108:                        mdoc->meta.arch = mandoc_strdup(cp);
                   2109:        }
                   2110:
                   2111:        /* Ignore any subsequent parameters... */
                   2112:        /* FIXME: warn about subsequent parameters. */
                   2113:
                   2114:        return(1);
                   2115: }
                   2116:
                   2117: static int
                   2118: post_prol(POST_ARGS)
                   2119: {
                   2120:        /*
                   2121:         * Remove prologue macros from the document after they're
                   2122:         * processed.  The final document uses mdoc_meta for these
                   2123:         * values and discards the originals.
                   2124:         */
                   2125:
                   2126:        mdoc_node_delete(mdoc, mdoc->last);
                   2127:        if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
                   2128:                mdoc->flags |= MDOC_PBODY;
                   2129:
                   2130:        return(1);
                   2131: }
                   2132:
                   2133: static int
                   2134: post_os(POST_ARGS)
                   2135: {
                   2136:        struct mdoc_node *n;
                   2137:        char              buf[BUFSIZ];
                   2138: #ifndef OSNAME
                   2139:        struct utsname    utsname;
                   2140: #endif
                   2141:
                   2142:        n = mdoc->last;
                   2143:
                   2144:        /*
                   2145:         * Set the operating system by way of the `Os' macro.  Note that
                   2146:         * if an argument isn't provided and -DOSNAME="\"foo\"" is
                   2147:         * provided during compilation, this value will be used instead
                   2148:         * of filling in "sysname release" from uname().
                   2149:         */
                   2150:
1.77      schwarze 2151:        if (mdoc->meta.os)
                   2152:                free(mdoc->meta.os);
1.76      schwarze 2153:
                   2154:        if ( ! concat(mdoc, buf, n->child, BUFSIZ))
                   2155:                return(0);
                   2156:
                   2157:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                   2158:         * it's really not worth the extra hackery.
                   2159:         */
                   2160:
                   2161:        if ('\0' == buf[0]) {
                   2162: #ifdef OSNAME
                   2163:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                   2164:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2165:                        return(0);
                   2166:                }
                   2167: #else /*!OSNAME */
                   2168:                if (uname(&utsname)) {
                   2169:                        mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
                   2170:                         mdoc->meta.os = mandoc_strdup("UNKNOWN");
                   2171:                         return(post_prol(mdoc));
                   2172:                 }
                   2173:
                   2174:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                   2175:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2176:                        return(0);
                   2177:                }
                   2178:                if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
                   2179:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2180:                        return(0);
                   2181:                }
                   2182:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                   2183:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2184:                        return(0);
                   2185:                }
                   2186: #endif /*!OSNAME*/
                   2187:        }
                   2188:
                   2189:        mdoc->meta.os = mandoc_strdup(buf);
                   2190:        return(1);
                   2191: }
                   2192:
                   2193: static int
                   2194: post_std(POST_ARGS)
                   2195: {
                   2196:        struct mdoc_node *nn, *n;
                   2197:
                   2198:        n = mdoc->last;
                   2199:
                   2200:        /*
                   2201:         * Macros accepting `-std' as an argument have the name of the
                   2202:         * current document (`Nm') filled in as the argument if it's not
                   2203:         * provided.
                   2204:         */
                   2205:
                   2206:        if (n->child)
                   2207:                return(1);
                   2208:
                   2209:        if (NULL == mdoc->meta.name)
                   2210:                return(1);
                   2211:
                   2212:        nn = n;
                   2213:        mdoc->next = MDOC_NEXT_CHILD;
                   2214:
                   2215:        if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
                   2216:                return(0);
                   2217:
                   2218:        mdoc->last = nn;
                   2219:        return(1);
                   2220: }
                   2221:
                   2222: static int
                   2223: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
                   2224: {
                   2225:
                   2226:        p[0] = '\0';
                   2227:
                   2228:        /*
                   2229:         * Concatenate sibling nodes together.  All siblings must be of
                   2230:         * type MDOC_TEXT or an assertion is raised.  Concatenation is
                   2231:         * separated by a single whitespace.  Returns 0 on fatal (string
                   2232:         * overrun) error.
                   2233:         */
                   2234:
                   2235:        for ( ; n; n = n->next) {
                   2236:                assert(MDOC_TEXT == n->type);
                   2237:
                   2238:                if (strlcat(p, n->string, sz) >= sz) {
                   2239:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2240:                        return(0);
                   2241:                }
                   2242:
                   2243:                if (NULL == n->next)
                   2244:                        continue;
                   2245:
                   2246:                if (strlcat(p, " ", sz) >= sz) {
                   2247:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2248:                        return(0);
                   2249:                }
                   2250:        }
                   2251:
                   2252:        return(1);
                   2253: }
                   2254: