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

Annotation of mandoc/mdoc_macro.c, Revision 1.45

1.45    ! kristaps    1: /*     $Id: mdoc_macro.c,v 1.44 2010/03/29 19:45:42 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:         */
1.45    ! kristaps  717:
1.4       kristaps  718:        switch (tok) {
1.26      kristaps  719:        case (MDOC_An):
                    720:                /* FALLTHROUGH */
                    721:        case (MDOC_Ar):
1.4       kristaps  722:                /* FALLTHROUGH */
                    723:        case (MDOC_Fl):
                    724:                /* FALLTHROUGH */
1.12      kristaps  725:        case (MDOC_Lk):
                    726:                /* FALLTHROUGH */
1.26      kristaps  727:        case (MDOC_Nm):
                    728:                /* FALLTHROUGH */
1.25      kristaps  729:        case (MDOC_Pa):
1.4       kristaps  730:                nc = 1;
                    731:                break;
                    732:        default:
                    733:                nc = 0;
                    734:                break;
                    735:        }
                    736:
1.27      kristaps  737:        for (arg = NULL;; ) {
1.1       kristaps  738:                la = *pos;
1.32      kristaps  739:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  740:
                    741:                if (ARGV_WORD == c) {
                    742:                        *pos = la;
                    743:                        break;
                    744:                }
                    745:                if (ARGV_EOLN == c)
                    746:                        break;
                    747:                if (ARGV_ARG == c)
                    748:                        continue;
                    749:
                    750:                mdoc_argv_free(arg);
                    751:                return(0);
                    752:        }
                    753:
1.4       kristaps  754:        for (cnt = 0, lastpunct = 1;; ) {
1.1       kristaps  755:                la = *pos;
1.32      kristaps  756:                w = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  757:
                    758:                if (ARGS_ERROR == w)
                    759:                        return(0);
                    760:                if (ARGS_EOLN == w)
                    761:                        break;
                    762:                if (ARGS_PUNCT == w)
                    763:                        break;
                    764:
                    765:                /* Quoted words shouldn't be looked-up. */
                    766:
1.35      kristaps  767:                c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  768:
1.4       kristaps  769:                /*
                    770:                 * In this case, we've located a submacro and must
                    771:                 * execute it.  Close out scope, if open.  If no
                    772:                 * elements have been generated, either create one (nc)
                    773:                 * or raise a warning.
                    774:                 */
1.1       kristaps  775:
1.29      kristaps  776:                if (MDOC_MAX != c) {
1.32      kristaps  777:                        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  778:                                return(0);
1.4       kristaps  779:                        if (nc && 0 == cnt) {
1.32      kristaps  780:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  781:                                        return(0);
1.32      kristaps  782:                                if ( ! rew_last(m, m->last))
1.12      kristaps  783:                                        return(0);
1.7       kristaps  784:                        } else if ( ! nc && 0 == cnt) {
                    785:                                mdoc_argv_free(arg);
1.32      kristaps  786:                                if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3       kristaps  787:                                        return(0);
1.7       kristaps  788:                        }
1.32      kristaps  789:                        c = mdoc_macro(m, c, line, la, pos, buf);
1.1       kristaps  790:                        if (0 == c)
                    791:                                return(0);
                    792:                        if (ppos > 1)
                    793:                                return(1);
1.32      kristaps  794:                        return(append_delims(m, line, pos, buf));
1.29      kristaps  795:                }
1.1       kristaps  796:
1.4       kristaps  797:                /*
                    798:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    799:                 * a word; rewind the scope, if a delimiter; then append
                    800:                 * the word.
                    801:                 */
1.1       kristaps  802:
1.3       kristaps  803:                d = mdoc_isdelim(p);
                    804:
                    805:                if (ARGS_QWORD != w && d) {
1.32      kristaps  806:                        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  807:                                return(0);
                    808:                        lastpunct = 1;
                    809:                } else if (lastpunct) {
1.32      kristaps  810:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  811:                                return(0);
                    812:                        lastpunct = 0;
                    813:                }
                    814:
1.3       kristaps  815:                if ( ! d)
                    816:                        cnt++;
1.32      kristaps  817:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps  818:                        return(0);
1.39      kristaps  819:
                    820:                /*
                    821:                 * `Fl' macros have their scope re-opened with each new
                    822:                 * word so that the `-' can be added to each one without
                    823:                 * having to parse out spaces.
                    824:                 */
                    825:                if (0 == lastpunct && MDOC_Fl == tok) {
                    826:                        if ( ! rew_elem(m, tok))
                    827:                                return(0);
                    828:                        lastpunct = 1;
                    829:                }
1.1       kristaps  830:        }
                    831:
1.32      kristaps  832:        if (0 == lastpunct && ! rew_elem(m, tok))
1.1       kristaps  833:                return(0);
1.4       kristaps  834:
                    835:        /*
                    836:         * If no elements have been collected and we're allowed to have
                    837:         * empties (nc), open a scope and close it out.  Otherwise,
                    838:         * raise a warning.
                    839:         */
1.45    ! kristaps  840:
1.4       kristaps  841:        if (nc && 0 == cnt) {
1.32      kristaps  842:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  843:                        return(0);
1.32      kristaps  844:                if ( ! rew_last(m, m->last))
1.12      kristaps  845:                        return(0);
1.7       kristaps  846:        } else if ( ! nc && 0 == cnt) {
                    847:                mdoc_argv_free(arg);
1.32      kristaps  848:                if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3       kristaps  849:                        return(0);
1.7       kristaps  850:        }
1.4       kristaps  851:
1.1       kristaps  852:        if (ppos > 1)
                    853:                return(1);
1.32      kristaps  854:        return(append_delims(m, line, pos, buf));
1.1       kristaps  855: }
                    856:
                    857:
                    858: static int
                    859: blk_full(MACRO_PROT_ARGS)
                    860: {
1.45    ! kristaps  861:        int               c, la;
1.1       kristaps  862:        struct mdoc_arg  *arg;
1.45    ! kristaps  863:        struct mdoc_node *head; /* save of head macro */
1.1       kristaps  864:        char             *p;
                    865:
1.45    ! kristaps  866:        /* Close out prior implicit scope. */
1.19      kristaps  867:
1.1       kristaps  868:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32      kristaps  869:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  870:                        return(0);
1.32      kristaps  871:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  872:                        return(0);
                    873:        }
                    874:
1.45    ! kristaps  875:        /*
        !           876:         * This routine accomodates implicitly- and explicitly-scoped
        !           877:         * macro openings.  Implicit ones first close out prior scope
        !           878:         * (seen above).  Delay opening the head until necessary to
        !           879:         * allow leading punctuation to print.  Special consideration
        !           880:         * for `It -column', which has phrase-part syntax instead of
        !           881:         * regular child nodes.
        !           882:         */
        !           883:
1.1       kristaps  884:        for (arg = NULL;; ) {
1.45    ! kristaps  885:                la = *pos;
1.32      kristaps  886:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  887:
                    888:                if (ARGV_WORD == c) {
1.45    ! kristaps  889:                        *pos = la;
1.1       kristaps  890:                        break;
                    891:                }
                    892:
                    893:                if (ARGV_EOLN == c)
                    894:                        break;
                    895:                if (ARGV_ARG == c)
                    896:                        continue;
                    897:
                    898:                mdoc_argv_free(arg);
                    899:                return(0);
                    900:        }
                    901:
1.32      kristaps  902:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps  903:                return(0);
                    904:
1.45    ! kristaps  905:        head = NULL;
1.1       kristaps  906:
1.45    ! kristaps  907:        /*
        !           908:         * The `Nd' macro has all arguments in its body: it's a hybrid
        !           909:         * of block partial-explicit and full-implicit.  Stupid.
        !           910:         */
1.19      kristaps  911:
1.45    ! kristaps  912:        if (MDOC_Nd == tok) {
        !           913:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
        !           914:                        return(0);
        !           915:                head = m->last;
1.32      kristaps  916:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19      kristaps  917:                        return(0);
1.32      kristaps  918:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19      kristaps  919:                        return(0);
                    920:        }
                    921:
1.45    ! kristaps  922:        for (;;) {
        !           923:                la = *pos;
1.32      kristaps  924:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  925:
                    926:                if (ARGS_ERROR == c)
                    927:                        return(0);
                    928:                if (ARGS_EOLN == c)
                    929:                        break;
1.45    ! kristaps  930:
        !           931:                /* Don't emit leading punct. for phrases. */
        !           932:
        !           933:                if (NULL == head && ARGS_PHRASE != c &&
        !           934:                                1 == mdoc_isdelim(p)) {
        !           935:                        if ( ! mdoc_word_alloc(m, line, la, p))
        !           936:                                return(0);
        !           937:                        continue;
        !           938:                }
        !           939:
        !           940:                /* Always re-open head for phrases. */
        !           941:
        !           942:                if (NULL == head || ARGS_PHRASE == c) {
        !           943:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
        !           944:                                return(0);
        !           945:                        head = m->last;
        !           946:                }
        !           947:
1.1       kristaps  948:                if (ARGS_PHRASE == c) {
1.45    ! kristaps  949:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps  950:                                return(0);
1.32      kristaps  951:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  952:                                return(0);
                    953:                        continue;
                    954:                }
                    955:
1.45    ! kristaps  956:                c = lookup(tok, p);
        !           957:                if (MDOC_MAX != c) {
        !           958:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps  959:                                return(0);
1.45    ! kristaps  960:                        break;
        !           961:                }
        !           962:                if ( ! mdoc_word_alloc(m, line, la, p))
        !           963:                        return(0);
        !           964:
        !           965:        }
1.1       kristaps  966:
1.45    ! kristaps  967:        if (NULL == head) {
        !           968:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps  969:                        return(0);
1.45    ! kristaps  970:                head = m->last;
1.1       kristaps  971:        }
                    972:
1.32      kristaps  973:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps  974:                return(0);
1.19      kristaps  975:
1.45    ! kristaps  976:        /* See notes on `Nd' hybrid, above. */
        !           977:
        !           978:        if (MDOC_Nd == tok)
1.19      kristaps  979:                return(1);
                    980:
1.45    ! kristaps  981:        /* Close out scopes to remain in a consistent state. */
        !           982:
1.32      kristaps  983:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps  984:                return(0);
1.32      kristaps  985:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps  986:                return(0);
                    987:
                    988:        return(1);
                    989: }
                    990:
                    991:
                    992: static int
                    993: blk_part_imp(MACRO_PROT_ARGS)
                    994: {
1.32      kristaps  995:        int               la, c;
1.1       kristaps  996:        char             *p;
1.43      kristaps  997:        struct mdoc_node *blk; /* saved block context */
                    998:        struct mdoc_node *body; /* saved body context */
                    999:        struct mdoc_node *n;
1.1       kristaps 1000:
1.43      kristaps 1001:        /*
                   1002:         * A macro that spans to the end of the line.  This is generally
                   1003:         * (but not necessarily) called as the first macro.  The block
                   1004:         * has a head as the immediate child, which is always empty,
                   1005:         * followed by zero or more opening punctuation nodes, then the
                   1006:         * body (which may be empty, depending on the macro), then zero
                   1007:         * or more closing punctuation nodes.
                   1008:         */
1.32      kristaps 1009:
                   1010:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1011:                return(0);
1.43      kristaps 1012:
1.32      kristaps 1013:        blk = m->last;
1.43      kristaps 1014:
1.32      kristaps 1015:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1016:                return(0);
1.32      kristaps 1017:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1018:                return(0);
                   1019:
1.43      kristaps 1020:        /*
                   1021:         * Open the body scope "on-demand", that is, after we've
                   1022:         * processed all our the leading delimiters (open parenthesis,
                   1023:         * etc.).
                   1024:         */
1.1       kristaps 1025:
1.43      kristaps 1026:        for (body = NULL; ; ) {
1.32      kristaps 1027:                la = *pos;
                   1028:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.43      kristaps 1029:
1.1       kristaps 1030:                assert(ARGS_PHRASE != c);
                   1031:
                   1032:                if (ARGS_ERROR == c)
                   1033:                        return(0);
1.43      kristaps 1034:                if (ARGS_EOLN == c)
                   1035:                        break;
1.1       kristaps 1036:                if (ARGS_PUNCT == c)
                   1037:                        break;
                   1038:
1.45    ! kristaps 1039:                if (NULL == body && 1 == mdoc_isdelim(p)) {
1.32      kristaps 1040:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1041:                                return(0);
                   1042:                        continue;
                   1043:                }
                   1044:
1.43      kristaps 1045:                if (NULL == body) {
                   1046:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1047:                               return(0);
                   1048:                        body = m->last;
                   1049:                }
                   1050:
                   1051:                if (MDOC_MAX != (c = lookup(tok, p))) {
                   1052:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
                   1053:                                return(0);
                   1054:                        break;
                   1055:                }
                   1056:
                   1057:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1058:                        return(0);
                   1059:        }
                   1060:
1.43      kristaps 1061:        /* Clean-ups to leave in a consistent state. */
                   1062:
1.44      kristaps 1063:        if (NULL == body) {
                   1064:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1065:                        return(0);
                   1066:                body = m->last;
                   1067:        }
1.43      kristaps 1068:
1.32      kristaps 1069:        /*
                   1070:         * If we can't rewind to our body, then our scope has already
                   1071:         * been closed by another macro (like `Oc' closing `Op').  This
                   1072:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.43      kristaps 1073:         * crufty use of `Op' breakage.  XXX: DEPRECATE.
1.1       kristaps 1074:         */
1.43      kristaps 1075:
1.32      kristaps 1076:        for (n = m->last; n; n = n->parent)
1.1       kristaps 1077:                if (body == n)
                   1078:                        break;
1.43      kristaps 1079:
1.32      kristaps 1080:        if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
                   1081:                return(0);
1.43      kristaps 1082:
1.32      kristaps 1083:        if (n && ! rew_last(m, body))
                   1084:                return(0);
1.1       kristaps 1085:
1.32      kristaps 1086:        /* Standard appending of delimiters. */
1.1       kristaps 1087:
1.32      kristaps 1088:        if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1       kristaps 1089:                return(0);
                   1090:
1.32      kristaps 1091:        /* Rewind scope, if applicable. */
1.1       kristaps 1092:
1.32      kristaps 1093:        if (n && ! rew_last(m, blk))
1.1       kristaps 1094:                return(0);
                   1095:
                   1096:        return(1);
                   1097: }
                   1098:
                   1099:
                   1100: static int
                   1101: blk_part_exp(MACRO_PROT_ARGS)
                   1102: {
1.43      kristaps 1103:        int               la, c;
                   1104:        struct mdoc_node *head; /* keep track of head */
                   1105:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1106:        char             *p;
                   1107:
1.43      kristaps 1108:        /*
                   1109:         * The opening of an explicit macro having zero or more leading
                   1110:         * punctuation nodes; a head with optional single element (the
                   1111:         * case of `Eo'); and a body that may be empty.
                   1112:         */
1.32      kristaps 1113:
                   1114:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1115:                return(0);
1.32      kristaps 1116:
1.43      kristaps 1117:        for (head = body = NULL; ; ) {
1.32      kristaps 1118:                la = *pos;
                   1119:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1120:
                   1121:                if (ARGS_ERROR == c)
                   1122:                        return(0);
                   1123:                if (ARGS_PUNCT == c)
                   1124:                        break;
                   1125:                if (ARGS_EOLN == c)
                   1126:                        break;
                   1127:
1.43      kristaps 1128:                assert(ARGS_PHRASE != c);
                   1129:
                   1130:                /* Flush out leading punctuation. */
                   1131:
1.45    ! kristaps 1132:                if (NULL == head && 1 == mdoc_isdelim(p)) {
1.43      kristaps 1133:                        assert(NULL == body);
                   1134:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1135:                                return(0);
                   1136:                        continue;
                   1137:                }
                   1138:
                   1139:                if (NULL == head) {
                   1140:                        assert(NULL == body);
                   1141:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1142:                                return(0);
1.43      kristaps 1143:                        head = m->last;
1.1       kristaps 1144:                }
                   1145:
1.43      kristaps 1146:                /*
                   1147:                 * `Eo' gobbles any data into the head, but most other
                   1148:                 * macros just immediately close out and begin the body.
                   1149:                 */
                   1150:
                   1151:                if (NULL == body) {
                   1152:                        assert(head);
                   1153:                        /* No check whether it's a macro! */
                   1154:                        if (MDOC_Eo == tok)
                   1155:                                if ( ! mdoc_word_alloc(m, line, la, p))
                   1156:                                        return(0);
                   1157:
1.32      kristaps 1158:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1159:                                return(0);
1.32      kristaps 1160:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1161:                                return(0);
1.43      kristaps 1162:                        body = m->last;
                   1163:
                   1164:                        if (MDOC_Eo == tok)
                   1165:                                continue;
1.1       kristaps 1166:                }
1.43      kristaps 1167:
                   1168:                assert(NULL != head && NULL != body);
                   1169:
                   1170:                if (MDOC_MAX != (c = lookup(tok, p))) {
                   1171:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
                   1172:                                return(0);
                   1173:                        break;
                   1174:                }
                   1175:
1.32      kristaps 1176:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1177:                        return(0);
                   1178:        }
                   1179:
1.43      kristaps 1180:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1181:
1.43      kristaps 1182:        if (NULL == head) {
                   1183:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1184:                        return(0);
                   1185:                head = m->last;
                   1186:        }
                   1187:
                   1188:        if (NULL == body) {
1.32      kristaps 1189:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1190:                        return(0);
1.32      kristaps 1191:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1192:                        return(0);
1.43      kristaps 1193:                body = m->last;
1.1       kristaps 1194:        }
                   1195:
1.32      kristaps 1196:        /* Standard appending of delimiters. */
                   1197:
1.1       kristaps 1198:        if (ppos > 1)
                   1199:                return(1);
1.43      kristaps 1200:
1.32      kristaps 1201:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1202: }
                   1203:
                   1204:
                   1205: static int
                   1206: in_line_argn(MACRO_PROT_ARGS)
                   1207: {
1.32      kristaps 1208:        int               la, flushed, j, c, maxargs;
1.1       kristaps 1209:        struct mdoc_arg  *arg;
                   1210:        char             *p;
                   1211:
1.32      kristaps 1212:        /* Fixed maximum arguments per macro, if applicable. */
1.1       kristaps 1213:
                   1214:        switch (tok) {
                   1215:        case (MDOC_Ap):
                   1216:                /* FALLTHROUGH */
                   1217:        case (MDOC_No):
                   1218:                /* FALLTHROUGH */
                   1219:        case (MDOC_Ns):
                   1220:                /* FALLTHROUGH */
                   1221:        case (MDOC_Ux):
                   1222:                maxargs = 0;
                   1223:                break;
1.42      kristaps 1224:        case (MDOC_Xr):
                   1225:                maxargs = 2;
                   1226:                break;
1.1       kristaps 1227:        default:
                   1228:                maxargs = 1;
                   1229:                break;
                   1230:        }
                   1231:
1.32      kristaps 1232:        /* Macro argument processing. */
                   1233:
1.27      kristaps 1234:        for (arg = NULL;; ) {
1.32      kristaps 1235:                la = *pos;
                   1236:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1237:
                   1238:                if (ARGV_WORD == c) {
1.32      kristaps 1239:                        *pos = la;
1.1       kristaps 1240:                        break;
                   1241:                }
                   1242:
                   1243:                if (ARGV_EOLN == c)
                   1244:                        break;
                   1245:                if (ARGV_ARG == c)
                   1246:                        continue;
                   1247:
                   1248:                mdoc_argv_free(arg);
                   1249:                return(0);
                   1250:        }
                   1251:
1.32      kristaps 1252:        /* Open the element scope. */
                   1253:
                   1254:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1255:                return(0);
1.32      kristaps 1256:
                   1257:        /* Process element arguments. */
1.1       kristaps 1258:
                   1259:        for (flushed = j = 0; ; j++) {
1.32      kristaps 1260:                la = *pos;
1.1       kristaps 1261:
                   1262:                if (j == maxargs && ! flushed) {
1.32      kristaps 1263:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1264:                                return(0);
                   1265:                        flushed = 1;
                   1266:                }
                   1267:
1.32      kristaps 1268:                c = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1269:
                   1270:                if (ARGS_ERROR == c)
                   1271:                        return(0);
                   1272:                if (ARGS_PUNCT == c)
                   1273:                        break;
                   1274:                if (ARGS_EOLN == c)
                   1275:                        break;
                   1276:
1.35      kristaps 1277:                if (MDOC_MAX != (c = lookup(tok, p))) {
1.32      kristaps 1278:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1279:                                return(0);
                   1280:                        flushed = 1;
1.32      kristaps 1281:                        if ( ! mdoc_macro(m, c, line, la, pos, buf))
1.1       kristaps 1282:                                return(0);
                   1283:                        break;
                   1284:                }
                   1285:
                   1286:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
                   1287:                                ! flushed && mdoc_isdelim(p)) {
1.32      kristaps 1288:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1289:                                return(0);
                   1290:                        flushed = 1;
                   1291:                }
1.42      kristaps 1292:
                   1293:                /*
                   1294:                 * XXX: this is a hack to work around groff's ugliness
                   1295:                 * as regards `Xr' and extraneous arguments.  It should
                   1296:                 * ideally be deprecated behaviour, but because this is
                   1297:                 * code is no here, it's unlikely to be removed.
                   1298:                 */
1.43      kristaps 1299:
1.42      kristaps 1300:                if (MDOC_Xr == tok && j == maxargs) {
                   1301:                        if ( ! mdoc_elem_alloc(m, line, ppos, MDOC_Ns, NULL))
                   1302:                                return(0);
                   1303:                        if ( ! rew_elem(m, MDOC_Ns))
                   1304:                                return(0);
                   1305:                }
                   1306:
1.32      kristaps 1307:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1308:                        return(0);
                   1309:        }
                   1310:
1.32      kristaps 1311:        /* Close out and append delimiters. */
                   1312:
                   1313:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1314:                return(0);
                   1315:
                   1316:        if (ppos > 1)
                   1317:                return(1);
1.32      kristaps 1318:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1319: }
                   1320:
                   1321:
                   1322: static int
                   1323: in_line_eoln(MACRO_PROT_ARGS)
                   1324: {
                   1325:        int               c, w, la;
                   1326:        struct mdoc_arg  *arg;
                   1327:        char             *p;
                   1328:
                   1329:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1330:
1.32      kristaps 1331:        /* Parse macro arguments. */
1.1       kristaps 1332:
1.32      kristaps 1333:        for (arg = NULL; ; ) {
1.1       kristaps 1334:                la = *pos;
1.32      kristaps 1335:                c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1336:
                   1337:                if (ARGV_WORD == c) {
                   1338:                        *pos = la;
                   1339:                        break;
                   1340:                }
                   1341:                if (ARGV_EOLN == c)
                   1342:                        break;
                   1343:                if (ARGV_ARG == c)
                   1344:                        continue;
                   1345:
                   1346:                mdoc_argv_free(arg);
                   1347:                return(0);
                   1348:        }
                   1349:
1.32      kristaps 1350:        /* Open element scope. */
                   1351:
                   1352:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1353:                return(0);
                   1354:
1.32      kristaps 1355:        /* Parse argument terms. */
1.1       kristaps 1356:
                   1357:        for (;;) {
                   1358:                la = *pos;
1.32      kristaps 1359:                w = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1360:
                   1361:                if (ARGS_ERROR == w)
                   1362:                        return(0);
                   1363:                if (ARGS_EOLN == w)
                   1364:                        break;
                   1365:
1.35      kristaps 1366:                c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1367:
1.29      kristaps 1368:                if (MDOC_MAX != c) {
1.32      kristaps 1369:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1370:                                return(0);
1.32      kristaps 1371:                        return(mdoc_macro(m, c, line, la, pos, buf));
1.29      kristaps 1372:                }
1.1       kristaps 1373:
1.32      kristaps 1374:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1375:                        return(0);
                   1376:        }
                   1377:
1.32      kristaps 1378:        /* Close out (no delimiters). */
                   1379:
                   1380:        return(rew_elem(m, tok));
1.1       kristaps 1381: }
                   1382:
                   1383:
                   1384: /* ARGSUSED */
                   1385: static int
1.41      kristaps 1386: ctx_synopsis(MACRO_PROT_ARGS)
                   1387: {
                   1388:
                   1389:        /* If we're not in the SYNOPSIS, go straight to in-line. */
                   1390:        if (SEC_SYNOPSIS != m->lastsec)
                   1391:                return(in_line(m, tok, line, ppos, pos, buf));
                   1392:
                   1393:        /* If we're a nested call, same place. */
                   1394:        if (ppos > 1)
                   1395:                return(in_line(m, tok, line, ppos, pos, buf));
                   1396:
                   1397:        /*
                   1398:         * XXX: this will open a block scope; however, if later we end
                   1399:         * up formatting the block scope, then child nodes will inherit
                   1400:         * the formatting.  Be careful.
                   1401:         */
                   1402:
                   1403:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
                   1404: }
                   1405:
                   1406:
                   1407: /* ARGSUSED */
                   1408: static int
1.1       kristaps 1409: obsolete(MACRO_PROT_ARGS)
                   1410: {
                   1411:
1.32      kristaps 1412:        return(mdoc_pwarn(m, line, ppos, EOBS));
1.1       kristaps 1413: }
                   1414:
                   1415:
1.24      kristaps 1416: /*
                   1417:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1418:  * They're unusual because they're basically free-form text until a
                   1419:  * macro is encountered.
                   1420:  */
1.1       kristaps 1421: static int
1.32      kristaps 1422: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1423: {
1.24      kristaps 1424:        int               c, w, la, pos;
                   1425:        char             *p;
1.1       kristaps 1426:
1.24      kristaps 1427:        for (pos = ppos; ; ) {
                   1428:                la = pos;
1.1       kristaps 1429:
1.24      kristaps 1430:                /* Note: no calling context! */
1.33      kristaps 1431:                w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1432:
1.24      kristaps 1433:                if (ARGS_ERROR == w)
                   1434:                        return(0);
                   1435:                if (ARGS_EOLN == w)
                   1436:                        break;
1.1       kristaps 1437:
1.35      kristaps 1438:                c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1439:
1.29      kristaps 1440:                if (MDOC_MAX != c) {
1.32      kristaps 1441:                        if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1.1       kristaps 1442:                                return(0);
1.32      kristaps 1443:                        return(append_delims(m, line, &pos, buf));
1.29      kristaps 1444:                }
1.1       kristaps 1445:
1.32      kristaps 1446:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1447:                        return(0);
                   1448:        }
                   1449:
                   1450:        return(1);
                   1451: }
1.24      kristaps 1452:
                   1453:

CVSweb