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

Annotation of mandoc/mdoc_macro.c, Revision 1.47

1.47    ! kristaps    1: /*     $Id: mdoc_macro.c,v 1.46 2010/03/30 08:24:01 kristaps Exp $ */
1.1       kristaps    2: /*
1.9       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.8       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.40      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <assert.h>
                     22: #include <ctype.h>
                     23: #include <stdlib.h>
                     24: #include <stdio.h>
                     25: #include <string.h>
1.38      kristaps   26: #include <time.h>
1.1       kristaps   27:
                     28: #include "libmdoc.h"
                     29:
                     30: #define        REWIND_REWIND   (1 << 0)
                     31: #define        REWIND_NOHALT   (1 << 1)
                     32: #define        REWIND_HALT     (1 << 2)
                     33:
1.41      kristaps   34: static int       ctx_synopsis(MACRO_PROT_ARGS);
1.1       kristaps   35: static int       obsolete(MACRO_PROT_ARGS);
                     36: static int       blk_part_exp(MACRO_PROT_ARGS);
                     37: static int       in_line_eoln(MACRO_PROT_ARGS);
                     38: static int       in_line_argn(MACRO_PROT_ARGS);
                     39: static int       in_line(MACRO_PROT_ARGS);
                     40: static int       blk_full(MACRO_PROT_ARGS);
                     41: static int       blk_exp_close(MACRO_PROT_ARGS);
                     42: static int       blk_part_imp(MACRO_PROT_ARGS);
                     43:
                     44: static int       phrase(struct mdoc *, int, int, char *);
1.47    ! kristaps   45: static int       rew_dohalt(enum mdoct, enum mdoc_type,
1.1       kristaps   46:                        const struct mdoc_node *);
1.47    ! kristaps   47: static enum mdoct rew_alt(enum mdoct);
        !            48: static int       rew_dobreak(enum mdoct, const struct mdoc_node *);
        !            49: static int       rew_elem(struct mdoc *, enum mdoct);
1.32      kristaps   50: static int       rew_sub(enum mdoc_type, struct mdoc *,
1.47    ! kristaps   51:                        enum mdoct, int, int);
1.32      kristaps   52: static int       rew_last(struct mdoc *,
                     53:                        const struct mdoc_node *);
1.1       kristaps   54: static int       append_delims(struct mdoc *, int, int *, char *);
1.35      kristaps   55: static int       lookup(int, const char *);
                     56: static int       lookup_raw(const char *);
1.1       kristaps   57: static int       swarn(struct mdoc *, enum mdoc_type, int, int,
                     58:                        const struct mdoc_node *);
                     59:
                     60: /* Central table of library: who gets parsed how. */
                     61:
                     62: const  struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.13      kristaps   63:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
1.1       kristaps   64:        { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
                     65:        { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
                     66:        { in_line_eoln, MDOC_PROLOGUE }, /* Os */
                     67:        { blk_full, 0 }, /* Sh */
                     68:        { blk_full, 0 }, /* Ss */
1.21      kristaps   69:        { in_line_eoln, 0 }, /* Pp */
1.1       kristaps   70:        { blk_part_imp, MDOC_PARSED }, /* D1 */
                     71:        { blk_part_imp, MDOC_PARSED }, /* Dl */
                     72:        { blk_full, MDOC_EXPLICIT }, /* Bd */
                     73:        { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
                     74:        { blk_full, MDOC_EXPLICIT }, /* Bl */
                     75:        { blk_exp_close, MDOC_EXPLICIT }, /* El */
                     76:        { blk_full, MDOC_PARSED }, /* It */
                     77:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.10      kristaps   78:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
1.1       kristaps   79:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.23      kristaps   80:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
1.1       kristaps   81:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
                     82:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
                     83:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
                     84:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
                     85:        { in_line_eoln, 0 }, /* Ex */
                     86:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
                     87:        { in_line_eoln, 0 }, /* Fd */
                     88:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
                     89:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
1.11      kristaps   90:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.1       kristaps   91:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.18      kristaps   92:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.1       kristaps   93:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.19      kristaps   94:        { blk_full, 0 }, /* Nd */
1.1       kristaps   95:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
                     96:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
                     97:        { obsolete, 0 }, /* Ot */
                     98:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
                     99:        { in_line_eoln, 0 }, /* Rv */
                    100:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
                    101:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.41      kristaps  102:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
1.42      kristaps  103:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.1       kristaps  104:        { in_line_eoln, 0 }, /* %A */
                    105:        { in_line_eoln, 0 }, /* %B */
                    106:        { in_line_eoln, 0 }, /* %D */
                    107:        { in_line_eoln, 0 }, /* %I */
                    108:        { in_line_eoln, 0 }, /* %J */
                    109:        { in_line_eoln, 0 }, /* %N */
                    110:        { in_line_eoln, 0 }, /* %O */
                    111:        { in_line_eoln, 0 }, /* %P */
                    112:        { in_line_eoln, 0 }, /* %R */
                    113:        { in_line_eoln, 0 }, /* %T */
                    114:        { in_line_eoln, 0 }, /* %V */
                    115:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
                    116:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
                    117:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
                    118:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
                    119:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
                    120:        { blk_full, MDOC_EXPLICIT }, /* Bf */
                    121:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
                    122:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
                    123:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
                    124:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
                    125:        { in_line_eoln, 0 }, /* Db */
                    126:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
                    127:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
                    128:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
                    129:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
                    130:        { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
                    131:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
                    132:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
                    133:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11      kristaps  134:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.1       kristaps  135:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* No */
                    136:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
                    137:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
                    138:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
                    139:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
                    140:        { in_line_argn, MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
                    141:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
                    142:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
                    143:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
                    144:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
                    145:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
                    146:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
                    147:        { blk_exp_close, MDOC_EXPLICIT }, /* Re */
                    148:        { blk_full, MDOC_EXPLICIT }, /* Rs */
                    149:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
                    150:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
                    151:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
                    152:        { in_line_eoln, 0 }, /* Sm */
                    153:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
                    154:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
                    155:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
                    156:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
                    157:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
                    158:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
                    159:        { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
                    160:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
                    161:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
                    162:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
                    163:        { blk_full, MDOC_EXPLICIT }, /* Bk */
                    164:        { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
                    165:        { in_line_eoln, 0 }, /* Bt */
                    166:        { in_line_eoln, 0 }, /* Hf */
                    167:        { obsolete, 0 }, /* Fr */
                    168:        { in_line_eoln, 0 }, /* Ud */
                    169:        { in_line_eoln, 0 }, /* Lb */
1.21      kristaps  170:        { in_line_eoln, 0 }, /* Lp */
1.12      kristaps  171:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
                    172:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
1.1       kristaps  173:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
                    174:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
                    175:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
                    176:        { in_line_eoln, 0 }, /* %C */
                    177:        { obsolete, 0 }, /* Es */
                    178:        { obsolete, 0 }, /* En */
                    179:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
                    180:        { in_line_eoln, 0 }, /* %Q */
1.20      kristaps  181:        { in_line_eoln, 0 }, /* br */
                    182:        { in_line_eoln, 0 }, /* sp */
1.37      kristaps  183:        { in_line_eoln, 0 }, /* %U */
1.1       kristaps  184: };
                    185:
                    186: const  struct mdoc_macro * const mdoc_macros = __mdoc_macros;
                    187:
                    188:
                    189: static int
                    190: swarn(struct mdoc *mdoc, enum mdoc_type type,
                    191:                int line, int pos, const struct mdoc_node *p)
                    192: {
                    193:        const char      *n, *t, *tt;
                    194:
                    195:        n = t = "<root>";
                    196:        tt = "block";
                    197:
                    198:        switch (type) {
                    199:        case (MDOC_BODY):
                    200:                tt = "multi-line";
                    201:                break;
                    202:        case (MDOC_HEAD):
                    203:                tt = "line";
                    204:                break;
                    205:        default:
                    206:                break;
                    207:        }
                    208:
                    209:        switch (p->type) {
                    210:        case (MDOC_BLOCK):
                    211:                n = mdoc_macronames[p->tok];
                    212:                t = "block";
                    213:                break;
                    214:        case (MDOC_BODY):
                    215:                n = mdoc_macronames[p->tok];
                    216:                t = "multi-line";
                    217:                break;
                    218:        case (MDOC_HEAD):
                    219:                n = mdoc_macronames[p->tok];
                    220:                t = "line";
                    221:                break;
                    222:        default:
                    223:                break;
                    224:        }
                    225:
                    226:        if ( ! (MDOC_IGN_SCOPE & mdoc->pflags))
1.16      kristaps  227:                return(mdoc_verr(mdoc, line, pos,
1.1       kristaps  228:                                "%s scope breaks %s scope of %s",
                    229:                                tt, t, n));
1.16      kristaps  230:        return(mdoc_vwarn(mdoc, line, pos,
1.1       kristaps  231:                                "%s scope breaks %s scope of %s",
                    232:                                tt, t, n));
                    233: }
                    234:
                    235:
                    236: /*
                    237:  * This is called at the end of parsing.  It must traverse up the tree,
                    238:  * closing out open [implicit] scopes.  Obviously, open explicit scopes
                    239:  * are errors.
                    240:  */
                    241: int
1.32      kristaps  242: mdoc_macroend(struct mdoc *m)
1.1       kristaps  243: {
                    244:        struct mdoc_node *n;
                    245:
                    246:        /* Scan for open explicit scopes. */
                    247:
1.32      kristaps  248:        n = MDOC_VALID & m->last->flags ?  m->last->parent : m->last;
1.1       kristaps  249:
                    250:        for ( ; n; n = n->parent) {
                    251:                if (MDOC_BLOCK != n->type)
                    252:                        continue;
                    253:                if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags))
                    254:                        continue;
1.32      kristaps  255:                return(mdoc_nerr(m, n, EOPEN));
1.1       kristaps  256:        }
                    257:
1.32      kristaps  258:        /* Rewind to the first. */
                    259:
                    260:        return(rew_last(m, m->first));
1.1       kristaps  261: }
                    262:
1.32      kristaps  263:
                    264: /*
                    265:  * Look up a macro from within a subsequent context.
                    266:  */
1.1       kristaps  267: static int
1.35      kristaps  268: lookup(int from, const char *p)
1.28      kristaps  269: {
1.36      kristaps  270:        /* FIXME: make -diag lists be un-PARSED. */
1.28      kristaps  271:
                    272:        if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
                    273:                return(MDOC_MAX);
1.35      kristaps  274:        return(lookup_raw(p));
1.28      kristaps  275: }
                    276:
                    277:
1.32      kristaps  278: /*
                    279:  * Lookup a macro following the initial line macro.
                    280:  */
1.28      kristaps  281: static int
1.35      kristaps  282: lookup_raw(const char *p)
1.1       kristaps  283: {
                    284:        int              res;
                    285:
1.34      kristaps  286:        if (MDOC_MAX == (res = mdoc_hash_find(p)))
1.28      kristaps  287:                return(MDOC_MAX);
                    288:        if (MDOC_CALLABLE & mdoc_macros[res].flags)
1.1       kristaps  289:                return(res);
                    290:        return(MDOC_MAX);
                    291: }
                    292:
                    293:
                    294: static int
1.32      kristaps  295: rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
1.1       kristaps  296: {
                    297:
                    298:        assert(to);
                    299:        mdoc->next = MDOC_NEXT_SIBLING;
                    300:
                    301:        /* LINTED */
                    302:        while (mdoc->last != to) {
                    303:                if ( ! mdoc_valid_post(mdoc))
                    304:                        return(0);
                    305:                if ( ! mdoc_action_post(mdoc))
                    306:                        return(0);
                    307:                mdoc->last = mdoc->last->parent;
                    308:                assert(mdoc->last);
                    309:        }
                    310:
                    311:        if ( ! mdoc_valid_post(mdoc))
                    312:                return(0);
                    313:        return(mdoc_action_post(mdoc));
                    314: }
                    315:
                    316:
1.32      kristaps  317: /*
                    318:  * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
                    319:  * matching pair.
                    320:  */
1.47    ! kristaps  321: static enum mdoct
        !           322: rew_alt(enum mdoct tok)
1.1       kristaps  323: {
                    324:        switch (tok) {
                    325:        case (MDOC_Ac):
                    326:                return(MDOC_Ao);
                    327:        case (MDOC_Bc):
                    328:                return(MDOC_Bo);
                    329:        case (MDOC_Brc):
                    330:                return(MDOC_Bro);
                    331:        case (MDOC_Dc):
                    332:                return(MDOC_Do);
                    333:        case (MDOC_Ec):
                    334:                return(MDOC_Eo);
                    335:        case (MDOC_Ed):
                    336:                return(MDOC_Bd);
                    337:        case (MDOC_Ef):
                    338:                return(MDOC_Bf);
                    339:        case (MDOC_Ek):
                    340:                return(MDOC_Bk);
                    341:        case (MDOC_El):
                    342:                return(MDOC_Bl);
                    343:        case (MDOC_Fc):
                    344:                return(MDOC_Fo);
                    345:        case (MDOC_Oc):
                    346:                return(MDOC_Oo);
                    347:        case (MDOC_Pc):
                    348:                return(MDOC_Po);
                    349:        case (MDOC_Qc):
                    350:                return(MDOC_Qo);
                    351:        case (MDOC_Re):
                    352:                return(MDOC_Rs);
                    353:        case (MDOC_Sc):
                    354:                return(MDOC_So);
                    355:        case (MDOC_Xc):
                    356:                return(MDOC_Xo);
                    357:        default:
                    358:                break;
                    359:        }
                    360:        abort();
                    361:        /* NOTREACHED */
                    362: }
                    363:
                    364:
                    365: /*
                    366:  * Rewind rules.  This indicates whether to stop rewinding
                    367:  * (REWIND_HALT) without touching our current scope, stop rewinding and
                    368:  * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
                    369:  * The scope-closing and so on occurs in the various rew_* routines.
                    370:  */
                    371: static int
1.47    ! kristaps  372: rew_dohalt(enum mdoct tok, enum mdoc_type type,
        !           373:                const struct mdoc_node *p)
1.1       kristaps  374: {
                    375:
                    376:        if (MDOC_ROOT == p->type)
                    377:                return(REWIND_HALT);
                    378:        if (MDOC_VALID & p->flags)
                    379:                return(REWIND_NOHALT);
                    380:
                    381:        switch (tok) {
                    382:        case (MDOC_Aq):
                    383:                /* FALLTHROUGH */
                    384:        case (MDOC_Bq):
                    385:                /* FALLTHROUGH */
                    386:        case (MDOC_Brq):
                    387:                /* FALLTHROUGH */
                    388:        case (MDOC_D1):
                    389:                /* FALLTHROUGH */
                    390:        case (MDOC_Dl):
                    391:                /* FALLTHROUGH */
                    392:        case (MDOC_Dq):
                    393:                /* FALLTHROUGH */
                    394:        case (MDOC_Op):
                    395:                /* FALLTHROUGH */
                    396:        case (MDOC_Pq):
                    397:                /* FALLTHROUGH */
                    398:        case (MDOC_Ql):
                    399:                /* FALLTHROUGH */
                    400:        case (MDOC_Qq):
                    401:                /* FALLTHROUGH */
                    402:        case (MDOC_Sq):
1.41      kristaps  403:                /* FALLTHROUGH */
                    404:        case (MDOC_Vt):
1.1       kristaps  405:                assert(MDOC_TAIL != type);
                    406:                if (type == p->type && tok == p->tok)
                    407:                        return(REWIND_REWIND);
                    408:                break;
                    409:        case (MDOC_It):
                    410:                assert(MDOC_TAIL != type);
                    411:                if (type == p->type && tok == p->tok)
                    412:                        return(REWIND_REWIND);
                    413:                if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
                    414:                        return(REWIND_HALT);
                    415:                break;
                    416:        case (MDOC_Sh):
                    417:                if (type == p->type && tok == p->tok)
                    418:                        return(REWIND_REWIND);
                    419:                break;
1.19      kristaps  420:        case (MDOC_Nd):
                    421:                /* FALLTHROUGH */
1.1       kristaps  422:        case (MDOC_Ss):
                    423:                assert(MDOC_TAIL != type);
                    424:                if (type == p->type && tok == p->tok)
                    425:                        return(REWIND_REWIND);
                    426:                if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
                    427:                        return(REWIND_HALT);
                    428:                break;
                    429:        case (MDOC_Ao):
                    430:                /* FALLTHROUGH */
                    431:        case (MDOC_Bd):
                    432:                /* FALLTHROUGH */
                    433:        case (MDOC_Bf):
                    434:                /* FALLTHROUGH */
                    435:        case (MDOC_Bk):
                    436:                /* FALLTHROUGH */
                    437:        case (MDOC_Bl):
                    438:                /* FALLTHROUGH */
                    439:        case (MDOC_Bo):
                    440:                /* FALLTHROUGH */
                    441:        case (MDOC_Bro):
                    442:                /* FALLTHROUGH */
                    443:        case (MDOC_Do):
                    444:                /* FALLTHROUGH */
                    445:        case (MDOC_Eo):
                    446:                /* FALLTHROUGH */
                    447:        case (MDOC_Fo):
                    448:                /* FALLTHROUGH */
                    449:        case (MDOC_Oo):
                    450:                /* FALLTHROUGH */
                    451:        case (MDOC_Po):
                    452:                /* FALLTHROUGH */
                    453:        case (MDOC_Qo):
                    454:                /* FALLTHROUGH */
                    455:        case (MDOC_Rs):
                    456:                /* FALLTHROUGH */
                    457:        case (MDOC_So):
                    458:                /* FALLTHROUGH */
                    459:        case (MDOC_Xo):
                    460:                if (type == p->type && tok == p->tok)
                    461:                        return(REWIND_REWIND);
                    462:                break;
                    463:        /* Multi-line explicit scope close. */
                    464:        case (MDOC_Ac):
                    465:                /* FALLTHROUGH */
                    466:        case (MDOC_Bc):
                    467:                /* FALLTHROUGH */
                    468:        case (MDOC_Brc):
                    469:                /* FALLTHROUGH */
                    470:        case (MDOC_Dc):
                    471:                /* FALLTHROUGH */
                    472:        case (MDOC_Ec):
                    473:                /* FALLTHROUGH */
                    474:        case (MDOC_Ed):
                    475:                /* FALLTHROUGH */
                    476:        case (MDOC_Ek):
                    477:                /* FALLTHROUGH */
                    478:        case (MDOC_El):
                    479:                /* FALLTHROUGH */
                    480:        case (MDOC_Fc):
                    481:                /* FALLTHROUGH */
                    482:        case (MDOC_Ef):
                    483:                /* FALLTHROUGH */
                    484:        case (MDOC_Oc):
                    485:                /* FALLTHROUGH */
                    486:        case (MDOC_Pc):
                    487:                /* FALLTHROUGH */
                    488:        case (MDOC_Qc):
                    489:                /* FALLTHROUGH */
                    490:        case (MDOC_Re):
                    491:                /* FALLTHROUGH */
                    492:        case (MDOC_Sc):
                    493:                /* FALLTHROUGH */
                    494:        case (MDOC_Xc):
                    495:                if (type == p->type && rew_alt(tok) == p->tok)
                    496:                        return(REWIND_REWIND);
                    497:                break;
                    498:        default:
                    499:                abort();
                    500:                /* NOTREACHED */
                    501:        }
                    502:
                    503:        return(REWIND_NOHALT);
                    504: }
                    505:
                    506:
                    507: /*
                    508:  * See if we can break an encountered scope (the rew_dohalt has returned
                    509:  * REWIND_NOHALT).
                    510:  */
                    511: static int
1.47    ! kristaps  512: rew_dobreak(enum mdoct tok, const struct mdoc_node *p)
1.1       kristaps  513: {
                    514:
                    515:        assert(MDOC_ROOT != p->type);
                    516:        if (MDOC_ELEM == p->type)
                    517:                return(1);
                    518:        if (MDOC_TEXT == p->type)
                    519:                return(1);
                    520:        if (MDOC_VALID & p->flags)
                    521:                return(1);
                    522:
                    523:        switch (tok) {
                    524:        case (MDOC_It):
                    525:                return(MDOC_It == p->tok);
1.19      kristaps  526:        case (MDOC_Nd):
                    527:                return(MDOC_Nd == p->tok);
1.1       kristaps  528:        case (MDOC_Ss):
                    529:                return(MDOC_Ss == p->tok);
                    530:        case (MDOC_Sh):
1.19      kristaps  531:                if (MDOC_Nd == p->tok)
                    532:                        return(1);
1.1       kristaps  533:                if (MDOC_Ss == p->tok)
                    534:                        return(1);
                    535:                return(MDOC_Sh == p->tok);
                    536:        case (MDOC_El):
                    537:                if (MDOC_It == p->tok)
                    538:                        return(1);
                    539:                break;
                    540:        case (MDOC_Oc):
1.47    ! kristaps  541: #ifdef UGLY
1.1       kristaps  542:                if (MDOC_Op == p->tok)
                    543:                        return(1);
1.47    ! kristaps  544: #endif
1.1       kristaps  545:                break;
                    546:        default:
                    547:                break;
                    548:        }
                    549:
                    550:        if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
                    551:                return(p->tok == rew_alt(tok));
                    552:        else if (MDOC_BLOCK == p->type)
                    553:                return(1);
                    554:
                    555:        return(tok == p->tok);
                    556: }
                    557:
                    558:
                    559: static int
1.47    ! kristaps  560: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1       kristaps  561: {
                    562:        struct mdoc_node *n;
                    563:
                    564:        n = mdoc->last;
                    565:        if (MDOC_ELEM != n->type)
                    566:                n = n->parent;
                    567:        assert(MDOC_ELEM == n->type);
                    568:        assert(tok == n->tok);
                    569:
                    570:        return(rew_last(mdoc, n));
                    571: }
                    572:
                    573:
                    574: static int
1.32      kristaps  575: rew_sub(enum mdoc_type t, struct mdoc *m,
1.47    ! kristaps  576:                enum mdoct tok, int line, int ppos)
1.1       kristaps  577: {
                    578:        struct mdoc_node *n;
                    579:        int               c;
                    580:
                    581:        /* LINTED */
1.32      kristaps  582:        for (n = m->last; n; n = n->parent) {
                    583:                c = rew_dohalt(tok, t, n);
                    584:                if (REWIND_HALT == c) {
                    585:                        if (MDOC_BLOCK != t)
                    586:                                return(1);
                    587:                        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags))
                    588:                                return(1);
                    589:                        return(mdoc_perr(m, line, ppos, ENOCTX));
                    590:                }
1.1       kristaps  591:                if (REWIND_REWIND == c)
                    592:                        break;
                    593:                else if (rew_dobreak(tok, n))
                    594:                        continue;
1.32      kristaps  595:                if ( ! swarn(m, t, line, ppos, n))
1.1       kristaps  596:                        return(0);
                    597:        }
                    598:
                    599:        assert(n);
1.47    ! kristaps  600:        if ( ! rew_last(m, n))
        !           601:                return(0);
        !           602:
        !           603: #ifdef UGLY
        !           604:        /*
        !           605:         * The current block extends an enclosing block beyond a line
        !           606:         * break.  Now that the current block ends, close the enclosing
        !           607:         * block, too.
        !           608:         */
        !           609:        if (NULL != (n = n->pending)) {
        !           610:                assert(MDOC_HEAD == n->type);
        !           611:                if ( ! rew_last(m, n))
        !           612:                        return(0);
        !           613:                if ( ! mdoc_body_alloc(m, n->line, n->pos, n->tok))
        !           614:                        return(0);
        !           615:        }
        !           616: #endif
        !           617:
        !           618:        return(1);
1.1       kristaps  619: }
                    620:
                    621:
                    622: static int
                    623: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
                    624: {
                    625:        int              c, lastarg;
                    626:        char            *p;
                    627:
                    628:        if (0 == buf[*pos])
                    629:                return(1);
                    630:
                    631:        for (;;) {
                    632:                lastarg = *pos;
1.33      kristaps  633:                c = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p);
1.1       kristaps  634:                assert(ARGS_PHRASE != c);
                    635:
                    636:                if (ARGS_ERROR == c)
                    637:                        return(0);
                    638:                else if (ARGS_EOLN == c)
                    639:                        break;
                    640:                assert(mdoc_isdelim(p));
                    641:                if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
                    642:                        return(0);
                    643:        }
                    644:
                    645:        return(1);
                    646: }
                    647:
                    648:
                    649: /*
                    650:  * Close out block partial/full explicit.
                    651:  */
                    652: static int
                    653: blk_exp_close(MACRO_PROT_ARGS)
                    654: {
                    655:        int              j, c, lastarg, maxargs, flushed;
                    656:        char            *p;
                    657:
                    658:        switch (tok) {
                    659:        case (MDOC_Ec):
                    660:                maxargs = 1;
                    661:                break;
                    662:        default:
                    663:                maxargs = 0;
                    664:                break;
                    665:        }
                    666:
                    667:        if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.22      kristaps  668:                if (buf[*pos])
1.32      kristaps  669:                        if ( ! mdoc_pwarn(m, line, ppos, ENOLINE))
1.1       kristaps  670:                                return(0);
1.22      kristaps  671:
1.32      kristaps  672:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      kristaps  673:                        return(0);
1.32      kristaps  674:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  675:        }
                    676:
1.32      kristaps  677:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  678:                return(0);
                    679:
1.32      kristaps  680:        if (maxargs > 0)
                    681:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  682:                        return(0);
                    683:
1.27      kristaps  684:        for (flushed = j = 0; ; j++) {
1.1       kristaps  685:                lastarg = *pos;
                    686:
                    687:                if (j == maxargs && ! flushed) {
1.32      kristaps  688:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  689:                                return(0);
                    690:                        flushed = 1;
                    691:                }
                    692:
1.32      kristaps  693:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  694:
                    695:                if (ARGS_ERROR == c)
                    696:                        return(0);
                    697:                if (ARGS_PUNCT == c)
                    698:                        break;
                    699:                if (ARGS_EOLN == c)
                    700:                        break;
                    701:
1.35      kristaps  702:                if (MDOC_MAX != (c = lookup(tok, p))) {
1.1       kristaps  703:                        if ( ! flushed) {
1.32      kristaps  704:                                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  705:                                        return(0);
                    706:                                flushed = 1;
                    707:                        }
1.32      kristaps  708:                        if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
1.1       kristaps  709:                                return(0);
                    710:                        break;
                    711:                }
                    712:
1.32      kristaps  713:                if ( ! mdoc_word_alloc(m, line, lastarg, p))
1.1       kristaps  714:                        return(0);
                    715:        }
                    716:
1.32      kristaps  717:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  718:                return(0);
                    719:
                    720:        if (ppos > 1)
                    721:                return(1);
1.32      kristaps  722:        return(append_delims(m, line, pos, buf));
1.1       kristaps  723: }
                    724:
                    725:
                    726: static int
                    727: in_line(MACRO_PROT_ARGS)
                    728: {
1.4       kristaps  729:        int               la, lastpunct, c, w, cnt, d, nc;
1.1       kristaps  730:        struct mdoc_arg  *arg;
                    731:        char             *p;
                    732:
1.4       kristaps  733:        /*
                    734:         * Whether we allow ignored elements (those without content,
                    735:         * usually because of reserved words) to squeak by.
                    736:         */
1.45      kristaps  737:
1.4       kristaps  738:        switch (tok) {
1.26      kristaps  739:        case (MDOC_An):
                    740:                /* FALLTHROUGH */
                    741:        case (MDOC_Ar):
1.4       kristaps  742:                /* FALLTHROUGH */
                    743:        case (MDOC_Fl):
                    744:                /* FALLTHROUGH */
1.12      kristaps  745:        case (MDOC_Lk):
                    746:                /* FALLTHROUGH */
1.26      kristaps  747:        case (MDOC_Nm):
                    748:                /* FALLTHROUGH */
1.25      kristaps  749:        case (MDOC_Pa):
1.4       kristaps  750:                nc = 1;
                    751:                break;
                    752:        default:
                    753:                nc = 0;
                    754:                break;
                    755:        }
                    756:
1.27      kristaps  757:        for (arg = NULL;; ) {
1.1       kristaps  758:                la = *pos;
1.32      kristaps  759:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  760:
                    761:                if (ARGV_WORD == c) {
                    762:                        *pos = la;
                    763:                        break;
                    764:                }
                    765:                if (ARGV_EOLN == c)
                    766:                        break;
                    767:                if (ARGV_ARG == c)
                    768:                        continue;
                    769:
                    770:                mdoc_argv_free(arg);
                    771:                return(0);
                    772:        }
                    773:
1.4       kristaps  774:        for (cnt = 0, lastpunct = 1;; ) {
1.1       kristaps  775:                la = *pos;
1.32      kristaps  776:                w = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  777:
                    778:                if (ARGS_ERROR == w)
                    779:                        return(0);
                    780:                if (ARGS_EOLN == w)
                    781:                        break;
                    782:                if (ARGS_PUNCT == w)
                    783:                        break;
                    784:
                    785:                /* Quoted words shouldn't be looked-up. */
                    786:
1.35      kristaps  787:                c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  788:
1.4       kristaps  789:                /*
                    790:                 * In this case, we've located a submacro and must
                    791:                 * execute it.  Close out scope, if open.  If no
                    792:                 * elements have been generated, either create one (nc)
                    793:                 * or raise a warning.
                    794:                 */
1.1       kristaps  795:
1.29      kristaps  796:                if (MDOC_MAX != c) {
1.32      kristaps  797:                        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  798:                                return(0);
1.4       kristaps  799:                        if (nc && 0 == cnt) {
1.32      kristaps  800:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  801:                                        return(0);
1.32      kristaps  802:                                if ( ! rew_last(m, m->last))
1.12      kristaps  803:                                        return(0);
1.7       kristaps  804:                        } else if ( ! nc && 0 == cnt) {
                    805:                                mdoc_argv_free(arg);
1.32      kristaps  806:                                if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3       kristaps  807:                                        return(0);
1.7       kristaps  808:                        }
1.32      kristaps  809:                        c = mdoc_macro(m, c, line, la, pos, buf);
1.1       kristaps  810:                        if (0 == c)
                    811:                                return(0);
                    812:                        if (ppos > 1)
                    813:                                return(1);
1.32      kristaps  814:                        return(append_delims(m, line, pos, buf));
1.29      kristaps  815:                }
1.1       kristaps  816:
1.4       kristaps  817:                /*
                    818:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    819:                 * a word; rewind the scope, if a delimiter; then append
                    820:                 * the word.
                    821:                 */
1.1       kristaps  822:
1.3       kristaps  823:                d = mdoc_isdelim(p);
                    824:
                    825:                if (ARGS_QWORD != w && d) {
1.32      kristaps  826:                        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  827:                                return(0);
                    828:                        lastpunct = 1;
                    829:                } else if (lastpunct) {
1.32      kristaps  830:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  831:                                return(0);
                    832:                        lastpunct = 0;
                    833:                }
                    834:
1.3       kristaps  835:                if ( ! d)
                    836:                        cnt++;
1.32      kristaps  837:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps  838:                        return(0);
1.39      kristaps  839:
                    840:                /*
                    841:                 * `Fl' macros have their scope re-opened with each new
                    842:                 * word so that the `-' can be added to each one without
                    843:                 * having to parse out spaces.
                    844:                 */
                    845:                if (0 == lastpunct && MDOC_Fl == tok) {
                    846:                        if ( ! rew_elem(m, tok))
                    847:                                return(0);
                    848:                        lastpunct = 1;
                    849:                }
1.1       kristaps  850:        }
                    851:
1.32      kristaps  852:        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  853:                return(0);
1.4       kristaps  854:
                    855:        /*
                    856:         * If no elements have been collected and we're allowed to have
                    857:         * empties (nc), open a scope and close it out.  Otherwise,
                    858:         * raise a warning.
                    859:         */
1.45      kristaps  860:
1.4       kristaps  861:        if (nc && 0 == cnt) {
1.32      kristaps  862:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  863:                        return(0);
1.32      kristaps  864:                if ( ! rew_last(m, m->last))
1.12      kristaps  865:                        return(0);
1.7       kristaps  866:        } else if ( ! nc && 0 == cnt) {
                    867:                mdoc_argv_free(arg);
1.32      kristaps  868:                if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3       kristaps  869:                        return(0);
1.7       kristaps  870:        }
1.4       kristaps  871:
1.1       kristaps  872:        if (ppos > 1)
                    873:                return(1);
1.32      kristaps  874:        return(append_delims(m, line, pos, buf));
1.1       kristaps  875: }
                    876:
                    877:
                    878: static int
                    879: blk_full(MACRO_PROT_ARGS)
                    880: {
1.45      kristaps  881:        int               c, la;
1.1       kristaps  882:        struct mdoc_arg  *arg;
1.45      kristaps  883:        struct mdoc_node *head; /* save of head macro */
1.47    ! kristaps  884:        struct mdoc_node *n;
1.1       kristaps  885:        char             *p;
                    886:
1.45      kristaps  887:        /* Close out prior implicit scope. */
1.19      kristaps  888:
1.1       kristaps  889:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32      kristaps  890:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  891:                        return(0);
1.32      kristaps  892:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  893:                        return(0);
                    894:        }
                    895:
1.45      kristaps  896:        /*
                    897:         * This routine accomodates implicitly- and explicitly-scoped
                    898:         * macro openings.  Implicit ones first close out prior scope
                    899:         * (seen above).  Delay opening the head until necessary to
                    900:         * allow leading punctuation to print.  Special consideration
                    901:         * for `It -column', which has phrase-part syntax instead of
                    902:         * regular child nodes.
                    903:         */
                    904:
1.1       kristaps  905:        for (arg = NULL;; ) {
1.45      kristaps  906:                la = *pos;
1.32      kristaps  907:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  908:
                    909:                if (ARGV_WORD == c) {
1.45      kristaps  910:                        *pos = la;
1.1       kristaps  911:                        break;
                    912:                }
                    913:
                    914:                if (ARGV_EOLN == c)
                    915:                        break;
                    916:                if (ARGV_ARG == c)
                    917:                        continue;
                    918:
                    919:                mdoc_argv_free(arg);
                    920:                return(0);
                    921:        }
                    922:
1.32      kristaps  923:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps  924:                return(0);
                    925:
1.45      kristaps  926:        head = NULL;
1.1       kristaps  927:
1.45      kristaps  928:        /*
                    929:         * The `Nd' macro has all arguments in its body: it's a hybrid
                    930:         * of block partial-explicit and full-implicit.  Stupid.
                    931:         */
1.19      kristaps  932:
1.45      kristaps  933:        if (MDOC_Nd == tok) {
                    934:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                    935:                        return(0);
                    936:                head = m->last;
1.32      kristaps  937:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19      kristaps  938:                        return(0);
1.32      kristaps  939:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19      kristaps  940:                        return(0);
                    941:        }
                    942:
1.45      kristaps  943:        for (;;) {
                    944:                la = *pos;
1.32      kristaps  945:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  946:
                    947:                if (ARGS_ERROR == c)
                    948:                        return(0);
                    949:                if (ARGS_EOLN == c)
                    950:                        break;
1.45      kristaps  951:
                    952:                /* Don't emit leading punct. for phrases. */
                    953:
                    954:                if (NULL == head && ARGS_PHRASE != c &&
                    955:                                1 == mdoc_isdelim(p)) {
                    956:                        if ( ! mdoc_word_alloc(m, line, la, p))
                    957:                                return(0);
                    958:                        continue;
                    959:                }
                    960:
                    961:                /* Always re-open head for phrases. */
                    962:
                    963:                if (NULL == head || ARGS_PHRASE == c) {
                    964:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
                    965:                                return(0);
                    966:                        head = m->last;
                    967:                }
                    968:
1.1       kristaps  969:                if (ARGS_PHRASE == c) {
1.45      kristaps  970:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps  971:                                return(0);
1.32      kristaps  972:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  973:                                return(0);
                    974:                        continue;
                    975:                }
                    976:
1.45      kristaps  977:                c = lookup(tok, p);
                    978:                if (MDOC_MAX != c) {
                    979:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps  980:                                return(0);
1.45      kristaps  981:                        break;
                    982:                }
                    983:                if ( ! mdoc_word_alloc(m, line, la, p))
                    984:                        return(0);
                    985:
                    986:        }
1.1       kristaps  987:
1.45      kristaps  988:        if (NULL == head) {
                    989:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  990:                        return(0);
1.45      kristaps  991:                head = m->last;
1.1       kristaps  992:        }
                    993:
1.32      kristaps  994:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps  995:                return(0);
1.19      kristaps  996:
1.45      kristaps  997:        /* See notes on `Nd' hybrid, above. */
                    998:
                    999:        if (MDOC_Nd == tok)
1.19      kristaps 1000:                return(1);
                   1001:
1.47    ! kristaps 1002: #ifdef UGLY
        !          1003:        /*
        !          1004:         * If there is an open sub-block requiring explicit close-out,
        !          1005:         * postpone switching the current block from head to body until
        !          1006:         * the rew_sub() call closing out that sub-block.
        !          1007:         */
        !          1008:        for (n = m->last; n && n != head; n = n->parent) {
        !          1009:                if (MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
        !          1010:                                MDOC_BLOCK == n->type) {
        !          1011:                        n->pending = head;
        !          1012:                        return(1);
        !          1013:                }
        !          1014:        }
        !          1015: #endif
        !          1016:
1.45      kristaps 1017:        /* Close out scopes to remain in a consistent state. */
                   1018:
1.32      kristaps 1019:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1020:                return(0);
1.32      kristaps 1021:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1022:                return(0);
                   1023:
                   1024:        return(1);
                   1025: }
                   1026:
                   1027:
                   1028: static int
                   1029: blk_part_imp(MACRO_PROT_ARGS)
                   1030: {
1.32      kristaps 1031:        int               la, c;
1.1       kristaps 1032:        char             *p;
1.43      kristaps 1033:        struct mdoc_node *blk; /* saved block context */
                   1034:        struct mdoc_node *body; /* saved body context */
                   1035:        struct mdoc_node *n;
1.1       kristaps 1036:
1.43      kristaps 1037:        /*
                   1038:         * A macro that spans to the end of the line.  This is generally
                   1039:         * (but not necessarily) called as the first macro.  The block
                   1040:         * has a head as the immediate child, which is always empty,
                   1041:         * followed by zero or more opening punctuation nodes, then the
                   1042:         * body (which may be empty, depending on the macro), then zero
                   1043:         * or more closing punctuation nodes.
                   1044:         */
1.32      kristaps 1045:
                   1046:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1047:                return(0);
1.43      kristaps 1048:
1.32      kristaps 1049:        blk = m->last;
1.43      kristaps 1050:
1.32      kristaps 1051:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1052:                return(0);
1.32      kristaps 1053:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1054:                return(0);
                   1055:
1.43      kristaps 1056:        /*
                   1057:         * Open the body scope "on-demand", that is, after we've
                   1058:         * processed all our the leading delimiters (open parenthesis,
                   1059:         * etc.).
                   1060:         */
1.1       kristaps 1061:
1.43      kristaps 1062:        for (body = NULL; ; ) {
1.32      kristaps 1063:                la = *pos;
                   1064:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.43      kristaps 1065:
1.1       kristaps 1066:                assert(ARGS_PHRASE != c);
                   1067:
                   1068:                if (ARGS_ERROR == c)
                   1069:                        return(0);
1.43      kristaps 1070:                if (ARGS_EOLN == c)
                   1071:                        break;
1.1       kristaps 1072:                if (ARGS_PUNCT == c)
                   1073:                        break;
                   1074:
1.45      kristaps 1075:                if (NULL == body && 1 == mdoc_isdelim(p)) {
1.32      kristaps 1076:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1077:                                return(0);
                   1078:                        continue;
                   1079:                }
                   1080:
1.43      kristaps 1081:                if (NULL == body) {
                   1082:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1083:                               return(0);
                   1084:                        body = m->last;
                   1085:                }
                   1086:
                   1087:                if (MDOC_MAX != (c = lookup(tok, p))) {
                   1088:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
                   1089:                                return(0);
                   1090:                        break;
                   1091:                }
                   1092:
                   1093:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1094:                        return(0);
                   1095:        }
                   1096:
1.43      kristaps 1097:        /* Clean-ups to leave in a consistent state. */
                   1098:
1.44      kristaps 1099:        if (NULL == body) {
                   1100:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1101:                        return(0);
                   1102:                body = m->last;
                   1103:        }
1.43      kristaps 1104:
1.47    ! kristaps 1105: #ifdef UGLY
1.32      kristaps 1106:        /*
                   1107:         * If we can't rewind to our body, then our scope has already
                   1108:         * been closed by another macro (like `Oc' closing `Op').  This
                   1109:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.47    ! kristaps 1110:         * crufty use of `Op' breakage.
1.1       kristaps 1111:         */
1.32      kristaps 1112:        for (n = m->last; n; n = n->parent)
1.1       kristaps 1113:                if (body == n)
                   1114:                        break;
1.47    ! kristaps 1115: #endif
1.43      kristaps 1116:
1.32      kristaps 1117:        if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
                   1118:                return(0);
1.43      kristaps 1119:
1.32      kristaps 1120:        if (n && ! rew_last(m, body))
                   1121:                return(0);
1.1       kristaps 1122:
1.32      kristaps 1123:        /* Standard appending of delimiters. */
1.1       kristaps 1124:
1.32      kristaps 1125:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps 1126:                return(0);
                   1127:
1.32      kristaps 1128:        /* Rewind scope, if applicable. */
1.1       kristaps 1129:
1.32      kristaps 1130:        if (n && ! rew_last(m, blk))
1.1       kristaps 1131:                return(0);
                   1132:
                   1133:        return(1);
                   1134: }
                   1135:
                   1136:
                   1137: static int
                   1138: blk_part_exp(MACRO_PROT_ARGS)
                   1139: {
1.43      kristaps 1140:        int               la, c;
                   1141:        struct mdoc_node *head; /* keep track of head */
                   1142:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1143:        char             *p;
                   1144:
1.43      kristaps 1145:        /*
                   1146:         * The opening of an explicit macro having zero or more leading
                   1147:         * punctuation nodes; a head with optional single element (the
                   1148:         * case of `Eo'); and a body that may be empty.
                   1149:         */
1.32      kristaps 1150:
                   1151:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1152:                return(0);
1.32      kristaps 1153:
1.43      kristaps 1154:        for (head = body = NULL; ; ) {
1.32      kristaps 1155:                la = *pos;
                   1156:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1157:
                   1158:                if (ARGS_ERROR == c)
                   1159:                        return(0);
                   1160:                if (ARGS_PUNCT == c)
                   1161:                        break;
                   1162:                if (ARGS_EOLN == c)
                   1163:                        break;
                   1164:
1.43      kristaps 1165:                assert(ARGS_PHRASE != c);
                   1166:
                   1167:                /* Flush out leading punctuation. */
                   1168:
1.45      kristaps 1169:                if (NULL == head && 1 == mdoc_isdelim(p)) {
1.43      kristaps 1170:                        assert(NULL == body);
                   1171:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1172:                                return(0);
                   1173:                        continue;
                   1174:                }
                   1175:
                   1176:                if (NULL == head) {
                   1177:                        assert(NULL == body);
                   1178:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1179:                                return(0);
1.43      kristaps 1180:                        head = m->last;
1.1       kristaps 1181:                }
                   1182:
1.43      kristaps 1183:                /*
                   1184:                 * `Eo' gobbles any data into the head, but most other
                   1185:                 * macros just immediately close out and begin the body.
                   1186:                 */
                   1187:
                   1188:                if (NULL == body) {
                   1189:                        assert(head);
                   1190:                        /* No check whether it's a macro! */
                   1191:                        if (MDOC_Eo == tok)
                   1192:                                if ( ! mdoc_word_alloc(m, line, la, p))
                   1193:                                        return(0);
                   1194:
1.32      kristaps 1195:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1196:                                return(0);
1.32      kristaps 1197:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1198:                                return(0);
1.43      kristaps 1199:                        body = m->last;
                   1200:
                   1201:                        if (MDOC_Eo == tok)
                   1202:                                continue;
1.1       kristaps 1203:                }
1.43      kristaps 1204:
                   1205:                assert(NULL != head && NULL != body);
                   1206:
                   1207:                if (MDOC_MAX != (c = lookup(tok, p))) {
                   1208:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
                   1209:                                return(0);
                   1210:                        break;
                   1211:                }
                   1212:
1.32      kristaps 1213:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1214:                        return(0);
                   1215:        }
                   1216:
1.43      kristaps 1217:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1218:
1.43      kristaps 1219:        if (NULL == head) {
                   1220:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1221:                        return(0);
                   1222:                head = m->last;
                   1223:        }
                   1224:
                   1225:        if (NULL == body) {
1.32      kristaps 1226:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1227:                        return(0);
1.32      kristaps 1228:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1229:                        return(0);
1.43      kristaps 1230:                body = m->last;
1.1       kristaps 1231:        }
                   1232:
1.32      kristaps 1233:        /* Standard appending of delimiters. */
                   1234:
1.1       kristaps 1235:        if (ppos > 1)
                   1236:                return(1);
1.43      kristaps 1237:
1.32      kristaps 1238:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1239: }
                   1240:
                   1241:
                   1242: static int
                   1243: in_line_argn(MACRO_PROT_ARGS)
                   1244: {
1.32      kristaps 1245:        int               la, flushed, j, c, maxargs;
1.1       kristaps 1246:        struct mdoc_arg  *arg;
                   1247:        char             *p;
                   1248:
1.46      kristaps 1249:        /*
                   1250:         * A line macro that has a fixed number of arguments (maxargs).
                   1251:         * Only open the scope once the first non-leading-punctuation is
                   1252:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1253:         * keep it open until the maximum number of arguments are
                   1254:         * exhausted.
                   1255:         */
1.1       kristaps 1256:
                   1257:        switch (tok) {
                   1258:        case (MDOC_Ap):
                   1259:                /* FALLTHROUGH */
                   1260:        case (MDOC_No):
                   1261:                /* FALLTHROUGH */
                   1262:        case (MDOC_Ns):
                   1263:                /* FALLTHROUGH */
                   1264:        case (MDOC_Ux):
                   1265:                maxargs = 0;
                   1266:                break;
1.42      kristaps 1267:        case (MDOC_Xr):
                   1268:                maxargs = 2;
                   1269:                break;
1.1       kristaps 1270:        default:
                   1271:                maxargs = 1;
                   1272:                break;
                   1273:        }
                   1274:
1.46      kristaps 1275:        for (arg = NULL; ; ) {
1.32      kristaps 1276:                la = *pos;
                   1277:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1278:
                   1279:                if (ARGV_WORD == c) {
1.32      kristaps 1280:                        *pos = la;
1.1       kristaps 1281:                        break;
                   1282:                }
                   1283:
                   1284:                if (ARGV_EOLN == c)
                   1285:                        break;
                   1286:                if (ARGV_ARG == c)
                   1287:                        continue;
                   1288:
                   1289:                mdoc_argv_free(arg);
                   1290:                return(0);
                   1291:        }
                   1292:
1.46      kristaps 1293:        for (flushed = j = 0; ; ) {
1.32      kristaps 1294:                la = *pos;
                   1295:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1296:
                   1297:                if (ARGS_ERROR == c)
                   1298:                        return(0);
                   1299:                if (ARGS_PUNCT == c)
                   1300:                        break;
                   1301:                if (ARGS_EOLN == c)
                   1302:                        break;
                   1303:
1.46      kristaps 1304:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
                   1305:                                0 == j && 1 == mdoc_isdelim(p)) {
                   1306:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1307:                                return(0);
                   1308:                        continue;
                   1309:                } else if (0 == j)
                   1310:                       if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1311:                               return(0);
                   1312:
                   1313:                if (j == maxargs && ! flushed) {
                   1314:                        if ( ! rew_elem(m, tok))
                   1315:                                return(0);
                   1316:                        flushed = 1;
                   1317:                }
                   1318:
1.35      kristaps 1319:                if (MDOC_MAX != (c = lookup(tok, p))) {
1.32      kristaps 1320:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1321:                                return(0);
                   1322:                        flushed = 1;
1.32      kristaps 1323:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps 1324:                                return(0);
1.46      kristaps 1325:                        j++;
1.1       kristaps 1326:                        break;
                   1327:                }
                   1328:
                   1329:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
                   1330:                                ! flushed && mdoc_isdelim(p)) {
1.32      kristaps 1331:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1332:                                return(0);
                   1333:                        flushed = 1;
                   1334:                }
1.42      kristaps 1335:
                   1336:                /*
                   1337:                 * XXX: this is a hack to work around groff's ugliness
                   1338:                 * as regards `Xr' and extraneous arguments.  It should
                   1339:                 * ideally be deprecated behaviour, but because this is
                   1340:                 * code is no here, it's unlikely to be removed.
                   1341:                 */
1.43      kristaps 1342:
1.46      kristaps 1343: #ifdef __OpenBSD__
1.42      kristaps 1344:                if (MDOC_Xr == tok && j == maxargs) {
1.46      kristaps 1345:                        if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1.42      kristaps 1346:                                return(0);
                   1347:                        if ( ! rew_elem(m, MDOC_Ns))
                   1348:                                return(0);
                   1349:                }
1.46      kristaps 1350: #endif
1.42      kristaps 1351:
1.32      kristaps 1352:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1353:                        return(0);
1.46      kristaps 1354:                j++;
1.1       kristaps 1355:        }
                   1356:
1.46      kristaps 1357:        if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1358:               return(0);
                   1359:
                   1360:        /* Close out in a consistent state. */
1.32      kristaps 1361:
                   1362:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1363:                return(0);
                   1364:
                   1365:        if (ppos > 1)
                   1366:                return(1);
1.32      kristaps 1367:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1368: }
                   1369:
                   1370:
                   1371: static int
                   1372: in_line_eoln(MACRO_PROT_ARGS)
                   1373: {
                   1374:        int               c, w, la;
                   1375:        struct mdoc_arg  *arg;
                   1376:        char             *p;
                   1377:
                   1378:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1379:
1.32      kristaps 1380:        /* Parse macro arguments. */
1.1       kristaps 1381:
1.32      kristaps 1382:        for (arg = NULL; ; ) {
1.1       kristaps 1383:                la = *pos;
1.32      kristaps 1384:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1385:
                   1386:                if (ARGV_WORD == c) {
                   1387:                        *pos = la;
                   1388:                        break;
                   1389:                }
                   1390:                if (ARGV_EOLN == c)
                   1391:                        break;
                   1392:                if (ARGV_ARG == c)
                   1393:                        continue;
                   1394:
                   1395:                mdoc_argv_free(arg);
                   1396:                return(0);
                   1397:        }
                   1398:
1.32      kristaps 1399:        /* Open element scope. */
                   1400:
                   1401:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1402:                return(0);
                   1403:
1.32      kristaps 1404:        /* Parse argument terms. */
1.1       kristaps 1405:
                   1406:        for (;;) {
                   1407:                la = *pos;
1.32      kristaps 1408:                w = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1409:
                   1410:                if (ARGS_ERROR == w)
                   1411:                        return(0);
                   1412:                if (ARGS_EOLN == w)
                   1413:                        break;
                   1414:
1.35      kristaps 1415:                c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1416:
1.29      kristaps 1417:                if (MDOC_MAX != c) {
1.32      kristaps 1418:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1419:                                return(0);
1.32      kristaps 1420:                        return(mdoc_macro(m, c, line, la, pos, buf));
1.29      kristaps 1421:                }
1.1       kristaps 1422:
1.32      kristaps 1423:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1424:                        return(0);
                   1425:        }
                   1426:
1.32      kristaps 1427:        /* Close out (no delimiters). */
                   1428:
                   1429:        return(rew_elem(m, tok));
1.1       kristaps 1430: }
                   1431:
                   1432:
                   1433: /* ARGSUSED */
                   1434: static int
1.41      kristaps 1435: ctx_synopsis(MACRO_PROT_ARGS)
                   1436: {
                   1437:
                   1438:        /* If we're not in the SYNOPSIS, go straight to in-line. */
                   1439:        if (SEC_SYNOPSIS != m->lastsec)
                   1440:                return(in_line(m, tok, line, ppos, pos, buf));
                   1441:
                   1442:        /* If we're a nested call, same place. */
                   1443:        if (ppos > 1)
                   1444:                return(in_line(m, tok, line, ppos, pos, buf));
                   1445:
                   1446:        /*
                   1447:         * XXX: this will open a block scope; however, if later we end
                   1448:         * up formatting the block scope, then child nodes will inherit
                   1449:         * the formatting.  Be careful.
                   1450:         */
                   1451:
                   1452:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
                   1453: }
                   1454:
                   1455:
                   1456: /* ARGSUSED */
                   1457: static int
1.1       kristaps 1458: obsolete(MACRO_PROT_ARGS)
                   1459: {
                   1460:
1.32      kristaps 1461:        return(mdoc_pwarn(m, line, ppos, EOBS));
1.1       kristaps 1462: }
                   1463:
                   1464:
1.24      kristaps 1465: /*
                   1466:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1467:  * They're unusual because they're basically free-form text until a
                   1468:  * macro is encountered.
                   1469:  */
1.1       kristaps 1470: static int
1.32      kristaps 1471: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1472: {
1.24      kristaps 1473:        int               c, w, la, pos;
                   1474:        char             *p;
1.1       kristaps 1475:
1.24      kristaps 1476:        for (pos = ppos; ; ) {
                   1477:                la = pos;
1.1       kristaps 1478:
1.24      kristaps 1479:                /* Note: no calling context! */
1.33      kristaps 1480:                w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1481:
1.24      kristaps 1482:                if (ARGS_ERROR == w)
                   1483:                        return(0);
                   1484:                if (ARGS_EOLN == w)
                   1485:                        break;
1.1       kristaps 1486:
1.35      kristaps 1487:                c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1488:
1.29      kristaps 1489:                if (MDOC_MAX != c) {
1.32      kristaps 1490:                        if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1.1       kristaps 1491:                                return(0);
1.32      kristaps 1492:                        return(append_delims(m, line, &pos, buf));
1.29      kristaps 1493:                }
1.1       kristaps 1494:
1.32      kristaps 1495:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1496:                        return(0);
                   1497:        }
                   1498:
                   1499:        return(1);
                   1500: }
1.24      kristaps 1501:
                   1502:

CVSweb