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

Annotation of mandoc/mdoc_macro.c, Revision 1.38

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

CVSweb