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

Annotation of mandoc/mdoc_macro.c, Revision 1.39

1.39    ! kristaps    1: /*     $Id: mdoc_macro.c,v 1.38 2009/10/26 17:05:44 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);
1.39    ! kristaps  811:
        !           812:                /*
        !           813:                 * `Fl' macros have their scope re-opened with each new
        !           814:                 * word so that the `-' can be added to each one without
        !           815:                 * having to parse out spaces.
        !           816:                 */
        !           817:                if (0 == lastpunct && MDOC_Fl == tok) {
        !           818:                        if ( ! rew_elem(m, tok))
        !           819:                                return(0);
        !           820:                        lastpunct = 1;
        !           821:                }
1.1       kristaps  822:        }
                    823:
1.32      kristaps  824:        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  825:                return(0);
1.4       kristaps  826:
                    827:        /*
                    828:         * If no elements have been collected and we're allowed to have
                    829:         * empties (nc), open a scope and close it out.  Otherwise,
                    830:         * raise a warning.
                    831:         *
                    832:         */
                    833:        if (nc && 0 == cnt) {
1.32      kristaps  834:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  835:                        return(0);
1.32      kristaps  836:                if ( ! rew_last(m, m->last))
1.12      kristaps  837:                        return(0);
1.7       kristaps  838:        } else if ( ! nc && 0 == cnt) {
                    839:                mdoc_argv_free(arg);
1.32      kristaps  840:                if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3       kristaps  841:                        return(0);
1.7       kristaps  842:        }
1.4       kristaps  843:
1.1       kristaps  844:        if (ppos > 1)
                    845:                return(1);
1.32      kristaps  846:        return(append_delims(m, line, pos, buf));
1.1       kristaps  847: }
                    848:
                    849:
                    850: static int
                    851: blk_full(MACRO_PROT_ARGS)
                    852: {
1.19      kristaps  853:        int               c, lastarg, reopen, dohead;
1.1       kristaps  854:        struct mdoc_arg  *arg;
                    855:        char             *p;
                    856:
1.19      kristaps  857:        /*
                    858:         * Whether to process a block-head section.  If this is
                    859:         * non-zero, then a head will be opened for all line arguments.
                    860:         * If not, then the head will always be empty and only a body
                    861:         * will be opened, which will stay open at the eoln.
                    862:         */
                    863:
                    864:        switch (tok) {
                    865:        case (MDOC_Nd):
                    866:                dohead = 0;
                    867:                break;
                    868:        default:
                    869:                dohead = 1;
                    870:                break;
                    871:        }
                    872:
1.1       kristaps  873:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32      kristaps  874:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  875:                        return(0);
1.32      kristaps  876:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  877:                        return(0);
                    878:        }
                    879:
                    880:        for (arg = NULL;; ) {
                    881:                lastarg = *pos;
1.32      kristaps  882:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  883:
                    884:                if (ARGV_WORD == c) {
                    885:                        *pos = lastarg;
                    886:                        break;
                    887:                }
                    888:
                    889:                if (ARGV_EOLN == c)
                    890:                        break;
                    891:                if (ARGV_ARG == c)
                    892:                        continue;
                    893:
                    894:                mdoc_argv_free(arg);
                    895:                return(0);
                    896:        }
                    897:
1.32      kristaps  898:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps  899:                return(0);
                    900:
                    901:        if (0 == buf[*pos]) {
1.32      kristaps  902:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  903:                        return(0);
1.32      kristaps  904:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  905:                        return(0);
1.32      kristaps  906:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps  907:                        return(0);
                    908:                return(1);
                    909:        }
                    910:
1.32      kristaps  911:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  912:                return(0);
1.19      kristaps  913:
                    914:        /* Immediately close out head and enter body, if applicable. */
                    915:
                    916:        if (0 == dohead) {
1.32      kristaps  917:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19      kristaps  918:                        return(0);
1.32      kristaps  919:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19      kristaps  920:                        return(0);
                    921:        }
                    922:
1.1       kristaps  923:        for (reopen = 0;; ) {
                    924:                lastarg = *pos;
1.32      kristaps  925:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  926:
                    927:                if (ARGS_ERROR == c)
                    928:                        return(0);
                    929:                if (ARGS_EOLN == c)
                    930:                        break;
                    931:                if (ARGS_PHRASE == c) {
1.19      kristaps  932:                        assert(dohead);
1.32      kristaps  933:                        if (reopen && ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  934:                                return(0);
                    935:                        /*
                    936:                         * Phrases are self-contained macro phrases used
                    937:                         * in the columnar output of a macro. They need
                    938:                         * special handling.
                    939:                         */
1.32      kristaps  940:                        if ( ! phrase(m, line, lastarg, buf))
1.1       kristaps  941:                                return(0);
1.32      kristaps  942:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  943:                                return(0);
                    944:
                    945:                        reopen = 1;
                    946:                        continue;
                    947:                }
                    948:
1.35      kristaps  949:                if (MDOC_MAX == (c = lookup(tok, p))) {
1.32      kristaps  950:                        if ( ! mdoc_word_alloc(m, line, lastarg, p))
1.1       kristaps  951:                                return(0);
                    952:                        continue;
                    953:                }
                    954:
1.32      kristaps  955:                if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
1.1       kristaps  956:                        return(0);
                    957:                break;
                    958:        }
                    959:
1.32      kristaps  960:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps  961:                return(0);
1.19      kristaps  962:
                    963:        /* If the body's already open, then just return. */
                    964:        if (0 == dohead)
                    965:                return(1);
                    966:
1.32      kristaps  967:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  968:                return(0);
1.32      kristaps  969:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps  970:                return(0);
                    971:
                    972:        return(1);
                    973: }
                    974:
                    975:
                    976: static int
                    977: blk_part_imp(MACRO_PROT_ARGS)
                    978: {
1.32      kristaps  979:        int               la, c;
1.1       kristaps  980:        char             *p;
                    981:        struct mdoc_node *blk, *body, *n;
                    982:
1.32      kristaps  983:        /* If applicable, close out prior scopes. */
                    984:
                    985:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                    986:                return(0);
                    987:        /* Saved for later close-out. */
                    988:        blk = m->last;
                    989:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  990:                return(0);
1.32      kristaps  991:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  992:                return(0);
1.32      kristaps  993:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps  994:                return(0);
1.32      kristaps  995:        /* Saved for later close-out. */
                    996:        body = m->last;
1.1       kristaps  997:
1.32      kristaps  998:        /* Body argument processing. */
1.1       kristaps  999:
1.27      kristaps 1000:        for (;;) {
1.32      kristaps 1001:                la = *pos;
                   1002:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1003:                assert(ARGS_PHRASE != c);
                   1004:
                   1005:                if (ARGS_ERROR == c)
                   1006:                        return(0);
                   1007:                if (ARGS_PUNCT == c)
                   1008:                        break;
                   1009:                if (ARGS_EOLN == c)
                   1010:                        break;
                   1011:
1.35      kristaps 1012:                if (MDOC_MAX == (c = lookup(tok, p))) {
1.32      kristaps 1013:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1014:                                return(0);
                   1015:                        continue;
                   1016:                }
                   1017:
1.32      kristaps 1018:                if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps 1019:                        return(0);
                   1020:                break;
                   1021:        }
                   1022:
1.32      kristaps 1023:        /*
                   1024:         * If we can't rewind to our body, then our scope has already
                   1025:         * been closed by another macro (like `Oc' closing `Op').  This
                   1026:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
                   1027:         * crufty use of `Op' breakage--XXX, deprecate in time.
1.1       kristaps 1028:         */
1.32      kristaps 1029:        for (n = m->last; n; n = n->parent)
1.1       kristaps 1030:                if (body == n)
                   1031:                        break;
1.32      kristaps 1032:        if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
                   1033:                return(0);
                   1034:        if (n && ! rew_last(m, body))
                   1035:                return(0);
1.1       kristaps 1036:
1.32      kristaps 1037:        /* Standard appending of delimiters. */
1.1       kristaps 1038:
1.32      kristaps 1039:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps 1040:                return(0);
                   1041:
1.32      kristaps 1042:        /* Rewind scope, if applicable. */
1.1       kristaps 1043:
1.32      kristaps 1044:        if (n && ! rew_last(m, blk))
1.1       kristaps 1045:                return(0);
                   1046:
                   1047:        return(1);
                   1048: }
                   1049:
                   1050:
                   1051: static int
                   1052: blk_part_exp(MACRO_PROT_ARGS)
                   1053: {
1.32      kristaps 1054:        int               la, flushed, j, c, maxargs;
1.1       kristaps 1055:        char             *p;
                   1056:
1.32      kristaps 1057:        /* Number of head arguments.  Only `Eo' has these, */
1.1       kristaps 1058:
                   1059:        switch (tok) {
                   1060:        case (MDOC_Eo):
                   1061:                maxargs = 1;
                   1062:                break;
                   1063:        default:
                   1064:                maxargs = 0;
                   1065:                break;
                   1066:        }
                   1067:
1.32      kristaps 1068:        /* Begin the block scope. */
                   1069:
                   1070:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1071:                return(0);
1.32      kristaps 1072:
                   1073:        /*
                   1074:         * If no head arguments, open and then close out a head, noting
                   1075:         * that we've flushed our terms.  `flushed' means that we've
                   1076:         * flushed out the head and the body is open.
                   1077:         */
1.1       kristaps 1078:
                   1079:        if (0 == maxargs) {
1.32      kristaps 1080:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1081:                        return(0);
1.32      kristaps 1082:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1083:                        return(0);
1.32      kristaps 1084:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1085:                        return(0);
                   1086:                flushed = 1;
1.32      kristaps 1087:        } else {
                   1088:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1089:                        return(0);
                   1090:                flushed = 0;
                   1091:        }
1.1       kristaps 1092:
1.32      kristaps 1093:        /* Process the head/head+body line arguments. */
1.1       kristaps 1094:
                   1095:        for (j = 0; ; j++) {
1.32      kristaps 1096:                la = *pos;
1.1       kristaps 1097:                if (j == maxargs && ! flushed) {
1.32      kristaps 1098:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1099:                                return(0);
                   1100:                        flushed = 1;
1.32      kristaps 1101:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1102:                                return(0);
                   1103:                }
                   1104:
1.32      kristaps 1105:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1106:                assert(ARGS_PHRASE != c);
                   1107:
                   1108:                if (ARGS_ERROR == c)
                   1109:                        return(0);
                   1110:                if (ARGS_PUNCT == c)
                   1111:                        break;
                   1112:                if (ARGS_EOLN == c)
                   1113:                        break;
                   1114:
1.35      kristaps 1115:                if (MDOC_MAX != (c = lookup(tok, p))) {
1.1       kristaps 1116:                        if ( ! flushed) {
1.32      kristaps 1117:                                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1118:                                        return(0);
                   1119:                                flushed = 1;
1.32      kristaps 1120:                                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1121:                                        return(0);
                   1122:                        }
1.32      kristaps 1123:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps 1124:                                return(0);
                   1125:                        break;
                   1126:                }
                   1127:
                   1128:                if ( ! flushed && mdoc_isdelim(p)) {
1.32      kristaps 1129:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1130:                                return(0);
                   1131:                        flushed = 1;
1.32      kristaps 1132:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1133:                                return(0);
                   1134:                }
                   1135:
1.32      kristaps 1136:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1137:                        return(0);
                   1138:        }
                   1139:
1.32      kristaps 1140:        /* Close the head and open the body, if applicable. */
                   1141:
1.1       kristaps 1142:        if ( ! flushed) {
1.32      kristaps 1143:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1144:                        return(0);
1.32      kristaps 1145:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1146:                        return(0);
                   1147:        }
                   1148:
1.32      kristaps 1149:        /* Standard appending of delimiters. */
                   1150:
1.1       kristaps 1151:        if (ppos > 1)
                   1152:                return(1);
1.32      kristaps 1153:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1154: }
                   1155:
                   1156:
                   1157: static int
                   1158: in_line_argn(MACRO_PROT_ARGS)
                   1159: {
1.32      kristaps 1160:        int               la, flushed, j, c, maxargs;
1.1       kristaps 1161:        struct mdoc_arg  *arg;
                   1162:        char             *p;
                   1163:
1.32      kristaps 1164:        /* Fixed maximum arguments per macro, if applicable. */
1.1       kristaps 1165:
                   1166:        switch (tok) {
                   1167:        case (MDOC_Ap):
                   1168:                /* FALLTHROUGH */
                   1169:        case (MDOC_No):
                   1170:                /* FALLTHROUGH */
                   1171:        case (MDOC_Ns):
                   1172:                /* FALLTHROUGH */
                   1173:        case (MDOC_Ux):
                   1174:                maxargs = 0;
                   1175:                break;
                   1176:        default:
                   1177:                maxargs = 1;
                   1178:                break;
                   1179:        }
                   1180:
1.32      kristaps 1181:        /* Macro argument processing. */
                   1182:
1.27      kristaps 1183:        for (arg = NULL;; ) {
1.32      kristaps 1184:                la = *pos;
                   1185:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1186:
                   1187:                if (ARGV_WORD == c) {
1.32      kristaps 1188:                        *pos = la;
1.1       kristaps 1189:                        break;
                   1190:                }
                   1191:
                   1192:                if (ARGV_EOLN == c)
                   1193:                        break;
                   1194:                if (ARGV_ARG == c)
                   1195:                        continue;
                   1196:
                   1197:                mdoc_argv_free(arg);
                   1198:                return(0);
                   1199:        }
                   1200:
1.32      kristaps 1201:        /* Open the element scope. */
                   1202:
                   1203:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1204:                return(0);
1.32      kristaps 1205:
                   1206:        /* Process element arguments. */
1.1       kristaps 1207:
                   1208:        for (flushed = j = 0; ; j++) {
1.32      kristaps 1209:                la = *pos;
1.1       kristaps 1210:
                   1211:                if (j == maxargs && ! flushed) {
1.32      kristaps 1212:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1213:                                return(0);
                   1214:                        flushed = 1;
                   1215:                }
                   1216:
1.32      kristaps 1217:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1218:
                   1219:                if (ARGS_ERROR == c)
                   1220:                        return(0);
                   1221:                if (ARGS_PUNCT == c)
                   1222:                        break;
                   1223:                if (ARGS_EOLN == c)
                   1224:                        break;
                   1225:
1.35      kristaps 1226:                if (MDOC_MAX != (c = lookup(tok, p))) {
1.32      kristaps 1227:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1228:                                return(0);
                   1229:                        flushed = 1;
1.32      kristaps 1230:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps 1231:                                return(0);
                   1232:                        break;
                   1233:                }
                   1234:
                   1235:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
                   1236:                                ! flushed && mdoc_isdelim(p)) {
1.32      kristaps 1237:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1238:                                return(0);
                   1239:                        flushed = 1;
                   1240:                }
                   1241:
1.32      kristaps 1242:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1243:                        return(0);
                   1244:        }
                   1245:
1.32      kristaps 1246:        /* Close out and append delimiters. */
                   1247:
                   1248:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1249:                return(0);
                   1250:
                   1251:        if (ppos > 1)
                   1252:                return(1);
1.32      kristaps 1253:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1254: }
                   1255:
                   1256:
                   1257: static int
                   1258: in_line_eoln(MACRO_PROT_ARGS)
                   1259: {
                   1260:        int               c, w, la;
                   1261:        struct mdoc_arg  *arg;
                   1262:        char             *p;
                   1263:
                   1264:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1265:
1.32      kristaps 1266:        /* Parse macro arguments. */
1.1       kristaps 1267:
1.32      kristaps 1268:        for (arg = NULL; ; ) {
1.1       kristaps 1269:                la = *pos;
1.32      kristaps 1270:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1271:
                   1272:                if (ARGV_WORD == c) {
                   1273:                        *pos = la;
                   1274:                        break;
                   1275:                }
                   1276:                if (ARGV_EOLN == c)
                   1277:                        break;
                   1278:                if (ARGV_ARG == c)
                   1279:                        continue;
                   1280:
                   1281:                mdoc_argv_free(arg);
                   1282:                return(0);
                   1283:        }
                   1284:
1.32      kristaps 1285:        /* Open element scope. */
                   1286:
                   1287:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1288:                return(0);
                   1289:
1.32      kristaps 1290:        /* Parse argument terms. */
1.1       kristaps 1291:
                   1292:        for (;;) {
                   1293:                la = *pos;
1.32      kristaps 1294:                w = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1295:
                   1296:                if (ARGS_ERROR == w)
                   1297:                        return(0);
                   1298:                if (ARGS_EOLN == w)
                   1299:                        break;
                   1300:
1.35      kristaps 1301:                c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1302:
1.29      kristaps 1303:                if (MDOC_MAX != c) {
1.32      kristaps 1304:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1305:                                return(0);
1.32      kristaps 1306:                        return(mdoc_macro(m, c, line, la, pos, buf));
1.29      kristaps 1307:                }
1.1       kristaps 1308:
1.32      kristaps 1309:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1310:                        return(0);
                   1311:        }
                   1312:
1.32      kristaps 1313:        /* Close out (no delimiters). */
                   1314:
                   1315:        return(rew_elem(m, tok));
1.1       kristaps 1316: }
                   1317:
                   1318:
                   1319: /* ARGSUSED */
                   1320: static int
                   1321: obsolete(MACRO_PROT_ARGS)
                   1322: {
                   1323:
1.32      kristaps 1324:        return(mdoc_pwarn(m, line, ppos, EOBS));
1.1       kristaps 1325: }
                   1326:
                   1327:
1.24      kristaps 1328: /*
                   1329:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1330:  * They're unusual because they're basically free-form text until a
                   1331:  * macro is encountered.
                   1332:  */
1.1       kristaps 1333: static int
1.32      kristaps 1334: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1335: {
1.24      kristaps 1336:        int               c, w, la, pos;
                   1337:        char             *p;
1.1       kristaps 1338:
1.24      kristaps 1339:        for (pos = ppos; ; ) {
                   1340:                la = pos;
1.1       kristaps 1341:
1.24      kristaps 1342:                /* Note: no calling context! */
1.33      kristaps 1343:                w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1344:
1.24      kristaps 1345:                if (ARGS_ERROR == w)
                   1346:                        return(0);
                   1347:                if (ARGS_EOLN == w)
                   1348:                        break;
1.1       kristaps 1349:
1.35      kristaps 1350:                c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1351:
1.29      kristaps 1352:                if (MDOC_MAX != c) {
1.32      kristaps 1353:                        if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1.1       kristaps 1354:                                return(0);
1.32      kristaps 1355:                        return(append_delims(m, line, &pos, buf));
1.29      kristaps 1356:                }
1.1       kristaps 1357:
1.32      kristaps 1358:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1359:                        return(0);
                   1360:        }
                   1361:
                   1362:        return(1);
                   1363: }
1.24      kristaps 1364:
                   1365:

CVSweb