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

Annotation of mandoc/mdoc_macro.c, Revision 1.43

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

CVSweb