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

Annotation of src/usr.bin/mandoc/mdoc_macro.c, Revision 1.33

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