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

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

1.66    ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.65 2011/04/21 22:59:54 schwarze Exp $ */
1.1       kristaps    2: /*
1.64      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.54      schwarze    4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.2       schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
                     18: #include <assert.h>
                     19: #include <ctype.h>
                     20: #include <stdlib.h>
                     21: #include <stdio.h>
                     22: #include <string.h>
1.26      schwarze   23: #include <time.h>
1.1       kristaps   24:
1.66    ! schwarze   25: #include "mdoc.h"
1.44      schwarze   26: #include "mandoc.h"
1.1       kristaps   27: #include "libmdoc.h"
1.39      schwarze   28: #include "libmandoc.h"
1.1       kristaps   29:
1.48      schwarze   30: enum   rew {   /* see rew_dohalt() */
                     31:        REWIND_NONE,
                     32:        REWIND_THIS,
                     33:        REWIND_MORE,
1.59      schwarze   34:        REWIND_FORCE,
1.48      schwarze   35:        REWIND_LATER,
1.54      schwarze   36:        REWIND_ERROR
1.35      schwarze   37: };
1.1       kristaps   38:
1.36      schwarze   39: static int             blk_full(MACRO_PROT_ARGS);
                     40: static int             blk_exp_close(MACRO_PROT_ARGS);
                     41: static int             blk_part_exp(MACRO_PROT_ARGS);
                     42: static int             blk_part_imp(MACRO_PROT_ARGS);
                     43: static int             ctx_synopsis(MACRO_PROT_ARGS);
                     44: static int             in_line_eoln(MACRO_PROT_ARGS);
                     45: static int             in_line_argn(MACRO_PROT_ARGS);
                     46: static int             in_line(MACRO_PROT_ARGS);
                     47: static int             obsolete(MACRO_PROT_ARGS);
1.46      schwarze   48: static int             phrase_ta(MACRO_PROT_ARGS);
1.36      schwarze   49:
1.66    ! schwarze   50: static int             dword(struct mdoc *, int, int,
        !            51:                                const char *, enum mdelim);
1.36      schwarze   52: static int             append_delims(struct mdoc *,
                     53:                                int, int *, char *);
                     54: static enum mdoct      lookup(enum mdoct, const char *);
                     55: static enum mdoct      lookup_raw(const char *);
1.49      schwarze   56: static int             make_pending(struct mdoc_node *, enum mdoct,
1.47      schwarze   57:                                struct mdoc *, int, int);
1.46      schwarze   58: static int             phrase(struct mdoc *, int, int, char *);
1.36      schwarze   59: static enum mdoct      rew_alt(enum mdoct);
                     60: static enum rew        rew_dohalt(enum mdoct, enum mdoc_type,
                     61:                                const struct mdoc_node *);
                     62: static int             rew_elem(struct mdoc *, enum mdoct);
                     63: static int             rew_last(struct mdoc *,
                     64:                                const struct mdoc_node *);
                     65: static int             rew_sub(enum mdoc_type, struct mdoc *,
                     66:                                enum mdoct, int, int);
1.1       kristaps   67:
                     68: const  struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.4       schwarze   69:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
1.1       kristaps   70:        { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
                     71:        { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
                     72:        { in_line_eoln, MDOC_PROLOGUE }, /* Os */
                     73:        { blk_full, 0 }, /* Sh */
                     74:        { blk_full, 0 }, /* Ss */
1.16      schwarze   75:        { in_line_eoln, 0 }, /* Pp */
1.1       kristaps   76:        { blk_part_imp, MDOC_PARSED }, /* D1 */
                     77:        { blk_part_imp, MDOC_PARSED }, /* Dl */
                     78:        { blk_full, MDOC_EXPLICIT }, /* Bd */
                     79:        { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
                     80:        { blk_full, MDOC_EXPLICIT }, /* Bl */
                     81:        { blk_exp_close, MDOC_EXPLICIT }, /* El */
                     82:        { blk_full, MDOC_PARSED }, /* It */
                     83:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.3       schwarze   84:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
1.1       kristaps   85:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.15      schwarze   86:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
1.1       kristaps   87:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
                     88:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
                     89:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
                     90:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
                     91:        { in_line_eoln, 0 }, /* Ex */
                     92:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
                     93:        { in_line_eoln, 0 }, /* Fd */
                     94:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
                     95:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
1.3       schwarze   96:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.1       kristaps   97:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.11      schwarze   98:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.1       kristaps   99:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.12      schwarze  100:        { blk_full, 0 }, /* Nd */
1.51      schwarze  101:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1       kristaps  102:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
                    103:        { obsolete, 0 }, /* Ot */
                    104:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
                    105:        { in_line_eoln, 0 }, /* Rv */
                    106:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
                    107:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.28      schwarze  108:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
                    109:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.1       kristaps  110:        { in_line_eoln, 0 }, /* %A */
                    111:        { in_line_eoln, 0 }, /* %B */
                    112:        { in_line_eoln, 0 }, /* %D */
                    113:        { in_line_eoln, 0 }, /* %I */
                    114:        { in_line_eoln, 0 }, /* %J */
                    115:        { in_line_eoln, 0 }, /* %N */
                    116:        { in_line_eoln, 0 }, /* %O */
                    117:        { in_line_eoln, 0 }, /* %P */
                    118:        { in_line_eoln, 0 }, /* %R */
                    119:        { in_line_eoln, 0 }, /* %T */
                    120:        { in_line_eoln, 0 }, /* %V */
                    121:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
                    122:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
                    123:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
                    124:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
                    125:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
                    126:        { blk_full, MDOC_EXPLICIT }, /* Bf */
                    127:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
                    128:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
                    129:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
                    130:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
                    131:        { in_line_eoln, 0 }, /* Db */
                    132:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
                    133:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
                    134:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
                    135:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
                    136:        { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
                    137:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
                    138:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
                    139:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.3       schwarze  140:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.57      schwarze  141:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* No */
                    142:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Ns */
1.1       kristaps  143:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
                    144:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
                    145:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
1.35      schwarze  146:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
1.1       kristaps  147:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
                    148:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
                    149:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
                    150:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
                    151:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
                    152:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
                    153:        { blk_exp_close, MDOC_EXPLICIT }, /* Re */
                    154:        { blk_full, MDOC_EXPLICIT }, /* Rs */
                    155:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
                    156:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
                    157:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
                    158:        { in_line_eoln, 0 }, /* Sm */
                    159:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
                    160:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
                    161:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
                    162:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
                    163:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
                    164:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
                    165:        { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
                    166:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
                    167:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
                    168:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
                    169:        { blk_full, MDOC_EXPLICIT }, /* Bk */
                    170:        { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
                    171:        { in_line_eoln, 0 }, /* Bt */
                    172:        { in_line_eoln, 0 }, /* Hf */
                    173:        { obsolete, 0 }, /* Fr */
                    174:        { in_line_eoln, 0 }, /* Ud */
1.45      schwarze  175:        { in_line, 0 }, /* Lb */
1.16      schwarze  176:        { in_line_eoln, 0 }, /* Lp */
1.3       schwarze  177:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
                    178:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
1.1       kristaps  179:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
                    180:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
                    181:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
                    182:        { in_line_eoln, 0 }, /* %C */
                    183:        { obsolete, 0 }, /* Es */
                    184:        { obsolete, 0 }, /* En */
                    185:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
                    186:        { in_line_eoln, 0 }, /* %Q */
1.13      schwarze  187:        { in_line_eoln, 0 }, /* br */
                    188:        { in_line_eoln, 0 }, /* sp */
1.25      schwarze  189:        { in_line_eoln, 0 }, /* %U */
1.46      schwarze  190:        { phrase_ta, MDOC_CALLABLE | MDOC_PARSED }, /* Ta */
1.1       kristaps  191: };
                    192:
                    193: const  struct mdoc_macro * const mdoc_macros = __mdoc_macros;
                    194:
                    195:
                    196: /*
                    197:  * This is called at the end of parsing.  It must traverse up the tree,
                    198:  * closing out open [implicit] scopes.  Obviously, open explicit scopes
                    199:  * are errors.
                    200:  */
                    201: int
1.22      schwarze  202: mdoc_macroend(struct mdoc *m)
1.1       kristaps  203: {
                    204:        struct mdoc_node *n;
                    205:
                    206:        /* Scan for open explicit scopes. */
                    207:
1.22      schwarze  208:        n = MDOC_VALID & m->last->flags ?  m->last->parent : m->last;
1.1       kristaps  209:
1.56      schwarze  210:        for ( ; n; n = n->parent)
                    211:                if (MDOC_BLOCK == n->type &&
                    212:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
                    213:                        mdoc_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.1       kristaps  214:
1.22      schwarze  215:        /* Rewind to the first. */
                    216:
                    217:        return(rew_last(m, m->first));
1.1       kristaps  218: }
                    219:
1.22      schwarze  220:
                    221: /*
                    222:  * Look up a macro from within a subsequent context.
                    223:  */
1.36      schwarze  224: static enum mdoct
                    225: lookup(enum mdoct from, const char *p)
1.21      schwarze  226: {
1.24      schwarze  227:        /* FIXME: make -diag lists be un-PARSED. */
1.21      schwarze  228:
                    229:        if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
                    230:                return(MDOC_MAX);
1.24      schwarze  231:        return(lookup_raw(p));
1.21      schwarze  232: }
                    233:
                    234:
1.22      schwarze  235: /*
                    236:  * Lookup a macro following the initial line macro.
                    237:  */
1.36      schwarze  238: static enum mdoct
1.24      schwarze  239: lookup_raw(const char *p)
1.1       kristaps  240: {
1.36      schwarze  241:        enum mdoct       res;
1.1       kristaps  242:
1.24      schwarze  243:        if (MDOC_MAX == (res = mdoc_hash_find(p)))
1.21      schwarze  244:                return(MDOC_MAX);
                    245:        if (MDOC_CALLABLE & mdoc_macros[res].flags)
1.1       kristaps  246:                return(res);
                    247:        return(MDOC_MAX);
                    248: }
                    249:
                    250:
                    251: static int
1.22      schwarze  252: rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
1.1       kristaps  253: {
1.63      schwarze  254:        struct mdoc_node *n, *np;
1.1       kristaps  255:
                    256:        assert(to);
                    257:        mdoc->next = MDOC_NEXT_SIBLING;
                    258:
                    259:        /* LINTED */
                    260:        while (mdoc->last != to) {
1.63      schwarze  261:                /*
                    262:                 * Save the parent here, because we may delete the
                    263:                 * m->last node in the post-validation phase and reset
                    264:                 * it to m->last->parent, causing a step in the closing
                    265:                 * out to be lost.
                    266:                 */
                    267:                np = mdoc->last->parent;
1.1       kristaps  268:                if ( ! mdoc_valid_post(mdoc))
                    269:                        return(0);
1.61      schwarze  270:                n = mdoc->last;
1.63      schwarze  271:                mdoc->last = np;
1.1       kristaps  272:                assert(mdoc->last);
1.61      schwarze  273:                mdoc->last->last = n;
1.1       kristaps  274:        }
                    275:
1.60      schwarze  276:        return(mdoc_valid_post(mdoc));
1.1       kristaps  277: }
                    278:
                    279:
1.22      schwarze  280: /*
1.48      schwarze  281:  * For a block closing macro, return the corresponding opening one.
                    282:  * Otherwise, return the macro itself.
1.22      schwarze  283:  */
1.34      schwarze  284: static enum mdoct
                    285: rew_alt(enum mdoct tok)
1.1       kristaps  286: {
                    287:        switch (tok) {
                    288:        case (MDOC_Ac):
                    289:                return(MDOC_Ao);
                    290:        case (MDOC_Bc):
                    291:                return(MDOC_Bo);
                    292:        case (MDOC_Brc):
                    293:                return(MDOC_Bro);
                    294:        case (MDOC_Dc):
                    295:                return(MDOC_Do);
                    296:        case (MDOC_Ec):
                    297:                return(MDOC_Eo);
                    298:        case (MDOC_Ed):
                    299:                return(MDOC_Bd);
                    300:        case (MDOC_Ef):
                    301:                return(MDOC_Bf);
                    302:        case (MDOC_Ek):
                    303:                return(MDOC_Bk);
                    304:        case (MDOC_El):
                    305:                return(MDOC_Bl);
                    306:        case (MDOC_Fc):
                    307:                return(MDOC_Fo);
                    308:        case (MDOC_Oc):
                    309:                return(MDOC_Oo);
                    310:        case (MDOC_Pc):
                    311:                return(MDOC_Po);
                    312:        case (MDOC_Qc):
                    313:                return(MDOC_Qo);
                    314:        case (MDOC_Re):
                    315:                return(MDOC_Rs);
                    316:        case (MDOC_Sc):
                    317:                return(MDOC_So);
                    318:        case (MDOC_Xc):
                    319:                return(MDOC_Xo);
                    320:        default:
1.48      schwarze  321:                return(tok);
1.1       kristaps  322:        }
                    323:        /* NOTREACHED */
                    324: }
                    325:
                    326:
1.48      schwarze  327: /*
                    328:  * Rewinding to tok, how do we have to handle *p?
                    329:  * REWIND_NONE: *p would delimit tok, but no tok scope is open
                    330:  *   inside *p, so there is no need to rewind anything at all.
                    331:  * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
                    332:  * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
1.59      schwarze  333:  * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
1.48      schwarze  334:  * REWIND_LATER: *p is explicit and still open, postpone rewinding.
                    335:  * REWIND_ERROR: No tok block is open at all.
1.1       kristaps  336:  */
1.35      schwarze  337: static enum rew
1.34      schwarze  338: rew_dohalt(enum mdoct tok, enum mdoc_type type,
                    339:                const struct mdoc_node *p)
1.1       kristaps  340: {
                    341:
1.50      schwarze  342:        /*
                    343:         * No matching token, no delimiting block, no broken block.
                    344:         * This can happen when full implicit macros are called for
                    345:         * the first time but try to rewind their previous
                    346:         * instance anyway.
                    347:         */
1.1       kristaps  348:        if (MDOC_ROOT == p->type)
1.48      schwarze  349:                return(MDOC_BLOCK == type &&
                    350:                    MDOC_EXPLICIT & mdoc_macros[tok].flags ?
                    351:                    REWIND_ERROR : REWIND_NONE);
1.50      schwarze  352:
                    353:        /*
                    354:         * When starting to rewind, skip plain text
                    355:         * and nodes that have already been rewound.
                    356:         */
1.48      schwarze  357:        if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
                    358:                return(REWIND_MORE);
                    359:
1.50      schwarze  360:        /*
                    361:         * The easiest case:  Found a matching token.
                    362:         * This applies to both blocks and elements.
                    363:         */
1.48      schwarze  364:        tok = rew_alt(tok);
                    365:        if (tok == p->tok)
                    366:                return(p->end ? REWIND_NONE :
                    367:                    type == p->type ? REWIND_THIS : REWIND_MORE);
                    368:
1.50      schwarze  369:        /*
                    370:         * While elements do require rewinding for themselves,
                    371:         * they never affect rewinding of other nodes.
                    372:         */
1.48      schwarze  373:        if (MDOC_ELEM == p->type)
                    374:                return(REWIND_MORE);
1.1       kristaps  375:
1.50      schwarze  376:        /*
                    377:         * Blocks delimited by our target token get REWIND_MORE.
                    378:         * Blocks delimiting our target token get REWIND_NONE.
                    379:         */
1.1       kristaps  380:        switch (tok) {
1.48      schwarze  381:        case (MDOC_Bl):
                    382:                if (MDOC_It == p->tok)
                    383:                        return(REWIND_MORE);
1.1       kristaps  384:                break;
                    385:        case (MDOC_It):
                    386:                if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
1.48      schwarze  387:                        return(REWIND_NONE);
1.1       kristaps  388:                break;
1.48      schwarze  389:        /*
                    390:         * XXX Badly nested block handling still fails badly
                    391:         * when one block is breaking two blocks of the same type.
                    392:         * This is an incomplete and extremely ugly workaround,
                    393:         * required to let the OpenBSD tree build.
                    394:         */
                    395:        case (MDOC_Oo):
                    396:                if (MDOC_Op == p->tok)
                    397:                        return(REWIND_MORE);
1.1       kristaps  398:                break;
1.51      schwarze  399:        case (MDOC_Nm):
                    400:                return(REWIND_NONE);
1.12      schwarze  401:        case (MDOC_Nd):
                    402:                /* FALLTHROUGH */
1.1       kristaps  403:        case (MDOC_Ss):
                    404:                if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
1.48      schwarze  405:                        return(REWIND_NONE);
1.1       kristaps  406:                /* FALLTHROUGH */
                    407:        case (MDOC_Sh):
1.48      schwarze  408:                if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
                    409:                    MDOC_Sh == p->tok)
                    410:                        return(REWIND_MORE);
1.1       kristaps  411:                break;
                    412:        default:
                    413:                break;
                    414:        }
                    415:
1.50      schwarze  416:        /*
                    417:         * Default block rewinding rules.
1.51      schwarze  418:         * In particular, always skip block end markers,
                    419:         * and let all blocks rewind Nm children.
1.50      schwarze  420:         */
1.54      schwarze  421:        if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
1.51      schwarze  422:            (MDOC_BLOCK == p->type &&
1.50      schwarze  423:            ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
                    424:                return(REWIND_MORE);
                    425:
                    426:        /*
1.59      schwarze  427:         * By default, closing out full blocks
                    428:         * forces closing of broken explicit blocks,
                    429:         * while closing out partial blocks
                    430:         * allows delayed rewinding by default.
1.50      schwarze  431:         */
1.59      schwarze  432:        return (&blk_full == mdoc_macros[tok].fp ?
                    433:            REWIND_FORCE : REWIND_LATER);
1.1       kristaps  434: }
                    435:
                    436:
                    437: static int
1.34      schwarze  438: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1       kristaps  439: {
                    440:        struct mdoc_node *n;
                    441:
                    442:        n = mdoc->last;
                    443:        if (MDOC_ELEM != n->type)
                    444:                n = n->parent;
                    445:        assert(MDOC_ELEM == n->type);
                    446:        assert(tok == n->tok);
                    447:
                    448:        return(rew_last(mdoc, n));
                    449: }
                    450:
                    451:
1.47      schwarze  452: /*
                    453:  * We are trying to close a block identified by tok,
                    454:  * but the child block *broken is still open.
                    455:  * Thus, postpone closing the tok block
                    456:  * until the rew_sub call closing *broken.
                    457:  */
                    458: static int
                    459: make_pending(struct mdoc_node *broken, enum mdoct tok,
                    460:                struct mdoc *m, int line, int ppos)
                    461: {
                    462:        struct mdoc_node *breaker;
                    463:
                    464:        /*
                    465:         * Iterate backwards, searching for the block matching tok,
                    466:         * that is, the block breaking the *broken block.
                    467:         */
                    468:        for (breaker = broken->parent; breaker; breaker = breaker->parent) {
                    469:
                    470:                /*
                    471:                 * If the *broken block had already been broken before
                    472:                 * and we encounter its breaker, make the tok block
                    473:                 * pending on the inner breaker.
                    474:                 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
                    475:                 * becomes "[A broken=[B [C->B B] tok=A] C]"
                    476:                 * and finally "[A [B->A [C->B B] A] C]".
                    477:                 */
                    478:                if (breaker == broken->pending) {
                    479:                        broken = breaker;
                    480:                        continue;
                    481:                }
                    482:
1.48      schwarze  483:                if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
1.47      schwarze  484:                        continue;
                    485:                if (MDOC_BODY == broken->type)
                    486:                        broken = broken->parent;
                    487:
                    488:                /*
                    489:                 * Found the breaker.
                    490:                 * If another, outer breaker is already pending on
                    491:                 * the *broken block, we must not clobber the link
                    492:                 * to the outer breaker, but make it pending on the
                    493:                 * new, now inner breaker.
                    494:                 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
                    495:                 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
                    496:                 * and finally "[A [B->A [C->B A] B] C]".
                    497:                 */
                    498:                if (broken->pending) {
                    499:                        struct mdoc_node *taker;
                    500:
                    501:                        /*
                    502:                         * If the breaker had also been broken before,
                    503:                         * it cannot take on the outer breaker itself,
                    504:                         * but must hand it on to its own breakers.
                    505:                         * Graphically, this is the following situation:
                    506:                         * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
                    507:                         * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
                    508:                         */
                    509:                        taker = breaker;
                    510:                        while (taker->pending)
                    511:                                taker = taker->pending;
                    512:                        taker->pending = broken->pending;
                    513:                }
                    514:                broken->pending = breaker;
1.66    ! schwarze  515:                mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
        !           516:                                "%s breaks %s", mdoc_macronames[tok],
        !           517:                                mdoc_macronames[broken->tok]);
1.47      schwarze  518:                return(1);
                    519:        }
                    520:
                    521:        /*
                    522:         * Found no matching block for tok.
                    523:         * Are you trying to close a block that is not open?
                    524:         */
                    525:        return(0);
                    526: }
                    527:
1.48      schwarze  528:
1.1       kristaps  529: static int
1.22      schwarze  530: rew_sub(enum mdoc_type t, struct mdoc *m,
1.34      schwarze  531:                enum mdoct tok, int line, int ppos)
1.1       kristaps  532: {
                    533:        struct mdoc_node *n;
                    534:
1.48      schwarze  535:        n = m->last;
                    536:        while (n) {
                    537:                switch (rew_dohalt(tok, t, n)) {
                    538:                case (REWIND_NONE):
                    539:                        return(1);
                    540:                case (REWIND_THIS):
                    541:                        break;
1.59      schwarze  542:                case (REWIND_FORCE):
1.66    ! schwarze  543:                        mandoc_vmsg(MANDOCERR_SCOPEBROKEN, m->parse,
        !           544:                                        line, ppos, "%s breaks %s",
        !           545:                                        mdoc_macronames[tok],
        !           546:                                        mdoc_macronames[n->tok]);
1.59      schwarze  547:                        /* FALLTHROUGH */
1.48      schwarze  548:                case (REWIND_MORE):
                    549:                        n = n->parent;
                    550:                        continue;
                    551:                case (REWIND_LATER):
1.59      schwarze  552:                        if (make_pending(n, tok, m, line, ppos) ||
                    553:                            MDOC_BLOCK != t)
                    554:                                return(1);
                    555:                        /* FALLTHROUGH */
1.48      schwarze  556:                case (REWIND_ERROR):
1.59      schwarze  557:                        mdoc_pmsg(m, line, ppos, MANDOCERR_NOSCOPE);
                    558:                        return(1);
1.22      schwarze  559:                }
1.48      schwarze  560:                break;
1.1       kristaps  561:        }
                    562:
                    563:        assert(n);
1.29      schwarze  564:        if ( ! rew_last(m, n))
                    565:                return(0);
                    566:
                    567:        /*
1.47      schwarze  568:         * The current block extends an enclosing block.
                    569:         * Now that the current block ends, close the enclosing block, too.
1.29      schwarze  570:         */
1.47      schwarze  571:        while (NULL != (n = n->pending)) {
1.29      schwarze  572:                if ( ! rew_last(m, n))
                    573:                        return(0);
1.47      schwarze  574:                if (MDOC_HEAD == n->type &&
                    575:                    ! mdoc_body_alloc(m, n->line, n->pos, n->tok))
1.29      schwarze  576:                        return(0);
                    577:        }
1.54      schwarze  578:
1.29      schwarze  579:        return(1);
1.1       kristaps  580: }
                    581:
1.66    ! schwarze  582: /*
        !           583:  * Allocate a word and check whether it's punctuation or not.
        !           584:  * Punctuation consists of those tokens found in mdoc_isdelim().
        !           585:  */
        !           586: static int
        !           587: dword(struct mdoc *m, int line,
        !           588:                int col, const char *p, enum mdelim d)
        !           589: {
        !           590:
        !           591:        if (DELIM_MAX == d)
        !           592:                d = mdoc_isdelim(p);
        !           593:
        !           594:        if ( ! mdoc_word_alloc(m, line, col, p))
        !           595:                return(0);
        !           596:
        !           597:        if (DELIM_OPEN == d)
        !           598:                m->last->flags |= MDOC_DELIMO;
        !           599:        else if (DELIM_CLOSE == d)
        !           600:                m->last->flags |= MDOC_DELIMC;
        !           601:
        !           602:        return(1);
        !           603: }
1.1       kristaps  604:
                    605: static int
1.42      schwarze  606: append_delims(struct mdoc *m, int line, int *pos, char *buf)
1.1       kristaps  607: {
1.42      schwarze  608:        int              la;
1.36      schwarze  609:        enum margserr    ac;
1.1       kristaps  610:        char            *p;
                    611:
1.42      schwarze  612:        if ('\0' == buf[*pos])
1.1       kristaps  613:                return(1);
                    614:
                    615:        for (;;) {
1.42      schwarze  616:                la = *pos;
                    617:                ac = mdoc_zargs(m, line, pos, buf, ARGS_NOWARN, &p);
1.1       kristaps  618:
1.36      schwarze  619:                if (ARGS_ERROR == ac)
1.1       kristaps  620:                        return(0);
1.36      schwarze  621:                else if (ARGS_EOLN == ac)
1.1       kristaps  622:                        break;
1.42      schwarze  623:
1.66    ! schwarze  624:                dword(m, line, la, p, DELIM_MAX);
1.43      schwarze  625:
1.42      schwarze  626:                /*
                    627:                 * If we encounter end-of-sentence symbols, then trigger
                    628:                 * the double-space.
                    629:                 *
                    630:                 * XXX: it's easy to allow this to propogate outward to
                    631:                 * the last symbol, such that `. )' will cause the
                    632:                 * correct double-spacing.  However, (1) groff isn't
                    633:                 * smart enough to do this and (2) it would require
                    634:                 * knowing which symbols break this behaviour, for
                    635:                 * example, `.  ;' shouldn't propogate the double-space.
                    636:                 */
1.55      schwarze  637:                if (mandoc_eos(p, strlen(p), 0))
1.42      schwarze  638:                        m->last->flags |= MDOC_EOS;
1.1       kristaps  639:        }
                    640:
                    641:        return(1);
                    642: }
                    643:
                    644:
                    645: /*
                    646:  * Close out block partial/full explicit.
                    647:  */
                    648: static int
                    649: blk_exp_close(MACRO_PROT_ARGS)
                    650: {
1.47      schwarze  651:        struct mdoc_node *body;         /* Our own body. */
                    652:        struct mdoc_node *later;        /* A sub-block starting later. */
                    653:        struct mdoc_node *n;            /* For searching backwards. */
                    654:
1.37      schwarze  655:        int              j, lastarg, maxargs, flushed, nl;
1.36      schwarze  656:        enum margserr    ac;
1.47      schwarze  657:        enum mdoct       atok, ntok;
1.1       kristaps  658:        char            *p;
                    659:
1.37      schwarze  660:        nl = MDOC_NEWLINE & m->flags;
                    661:
1.1       kristaps  662:        switch (tok) {
                    663:        case (MDOC_Ec):
                    664:                maxargs = 1;
                    665:                break;
                    666:        default:
                    667:                maxargs = 0;
                    668:                break;
                    669:        }
                    670:
1.47      schwarze  671:        /*
                    672:         * Search backwards for beginnings of blocks,
                    673:         * both of our own and of pending sub-blocks.
                    674:         */
                    675:        atok = rew_alt(tok);
                    676:        body = later = NULL;
                    677:        for (n = m->last; n; n = n->parent) {
                    678:                if (MDOC_VALID & n->flags)
                    679:                        continue;
                    680:
                    681:                /* Remember the start of our own body. */
                    682:                if (MDOC_BODY == n->type && atok == n->tok) {
1.54      schwarze  683:                        if (ENDBODY_NOT == n->end)
1.47      schwarze  684:                                body = n;
                    685:                        continue;
                    686:                }
                    687:
1.51      schwarze  688:                if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.47      schwarze  689:                        continue;
                    690:                if (atok == n->tok) {
                    691:                        assert(body);
                    692:
                    693:                        /*
                    694:                         * Found the start of our own block.
                    695:                         * When there is no pending sub block,
                    696:                         * just proceed to closing out.
                    697:                         */
                    698:                        if (NULL == later)
                    699:                                break;
                    700:
                    701:                        /*
                    702:                         * When there is a pending sub block,
                    703:                         * postpone closing out the current block
                    704:                         * until the rew_sub() closing out the sub-block.
                    705:                         */
1.59      schwarze  706:                        make_pending(later, tok, m, line, ppos);
1.47      schwarze  707:
                    708:                        /*
                    709:                         * Mark the place where the formatting - but not
                    710:                         * the scope - of the current block ends.
                    711:                         */
                    712:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                    713:                            atok, body, ENDBODY_SPACE))
                    714:                                return(0);
                    715:                        break;
                    716:                }
                    717:
                    718:                /*
                    719:                 * When finding an open sub block, remember the last
                    720:                 * open explicit block, or, in case there are only
                    721:                 * implicit ones, the first open implicit block.
                    722:                 */
                    723:                if (later &&
                    724:                    MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
                    725:                        continue;
1.60      schwarze  726:                if (MDOC_CALLABLE & mdoc_macros[n->tok].flags)
1.47      schwarze  727:                        later = n;
                    728:        }
                    729:
1.1       kristaps  730:        if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.44      schwarze  731:                /* FIXME: do this in validate */
1.14      schwarze  732:                if (buf[*pos])
1.65      schwarze  733:                        mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST);
1.14      schwarze  734:
1.22      schwarze  735:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.14      schwarze  736:                        return(0);
1.22      schwarze  737:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  738:        }
                    739:
1.22      schwarze  740:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  741:                return(0);
                    742:
1.47      schwarze  743:        if (NULL == later && maxargs > 0)
1.22      schwarze  744:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  745:                        return(0);
                    746:
1.20      schwarze  747:        for (flushed = j = 0; ; j++) {
1.1       kristaps  748:                lastarg = *pos;
                    749:
                    750:                if (j == maxargs && ! flushed) {
1.22      schwarze  751:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  752:                                return(0);
                    753:                        flushed = 1;
                    754:                }
                    755:
1.36      schwarze  756:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  757:
1.36      schwarze  758:                if (ARGS_ERROR == ac)
1.1       kristaps  759:                        return(0);
1.36      schwarze  760:                if (ARGS_PUNCT == ac)
1.1       kristaps  761:                        break;
1.36      schwarze  762:                if (ARGS_EOLN == ac)
1.1       kristaps  763:                        break;
                    764:
1.36      schwarze  765:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                    766:
                    767:                if (MDOC_MAX == ntok) {
1.66    ! schwarze  768:                        if ( ! dword(m, line, lastarg, p, DELIM_MAX))
1.1       kristaps  769:                                return(0);
1.36      schwarze  770:                        continue;
                    771:                }
1.1       kristaps  772:
1.36      schwarze  773:                if ( ! flushed) {
                    774:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                    775:                                return(0);
                    776:                        flushed = 1;
                    777:                }
                    778:                if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
1.1       kristaps  779:                        return(0);
1.36      schwarze  780:                break;
1.1       kristaps  781:        }
                    782:
1.22      schwarze  783:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  784:                return(0);
                    785:
1.37      schwarze  786:        if ( ! nl)
1.1       kristaps  787:                return(1);
1.22      schwarze  788:        return(append_delims(m, line, pos, buf));
1.1       kristaps  789: }
                    790:
                    791:
                    792: static int
                    793: in_line(MACRO_PROT_ARGS)
                    794: {
1.45      schwarze  795:        int              la, scope, cnt, nc, nl;
1.36      schwarze  796:        enum margverr    av;
                    797:        enum mdoct       ntok;
                    798:        enum margserr    ac;
1.40      schwarze  799:        enum mdelim      d;
1.36      schwarze  800:        struct mdoc_arg *arg;
                    801:        char            *p;
1.1       kristaps  802:
1.37      schwarze  803:        nl = MDOC_NEWLINE & m->flags;
                    804:
1.1       kristaps  805:        /*
                    806:         * Whether we allow ignored elements (those without content,
                    807:         * usually because of reserved words) to squeak by.
                    808:         */
1.32      schwarze  809:
1.1       kristaps  810:        switch (tok) {
1.19      schwarze  811:        case (MDOC_An):
                    812:                /* FALLTHROUGH */
                    813:        case (MDOC_Ar):
1.1       kristaps  814:                /* FALLTHROUGH */
                    815:        case (MDOC_Fl):
                    816:                /* FALLTHROUGH */
1.60      schwarze  817:        case (MDOC_Mt):
1.18      schwarze  818:                /* FALLTHROUGH */
1.19      schwarze  819:        case (MDOC_Nm):
                    820:                /* FALLTHROUGH */
1.18      schwarze  821:        case (MDOC_Pa):
1.1       kristaps  822:                nc = 1;
                    823:                break;
                    824:        default:
                    825:                nc = 0;
                    826:                break;
                    827:        }
                    828:
1.20      schwarze  829:        for (arg = NULL;; ) {
1.1       kristaps  830:                la = *pos;
1.36      schwarze  831:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  832:
1.36      schwarze  833:                if (ARGV_WORD == av) {
1.1       kristaps  834:                        *pos = la;
                    835:                        break;
                    836:                }
1.36      schwarze  837:                if (ARGV_EOLN == av)
1.1       kristaps  838:                        break;
1.36      schwarze  839:                if (ARGV_ARG == av)
1.1       kristaps  840:                        continue;
                    841:
                    842:                mdoc_argv_free(arg);
                    843:                return(0);
                    844:        }
                    845:
1.45      schwarze  846:        for (cnt = scope = 0;; ) {
1.1       kristaps  847:                la = *pos;
1.36      schwarze  848:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  849:
1.36      schwarze  850:                if (ARGS_ERROR == ac)
1.1       kristaps  851:                        return(0);
1.36      schwarze  852:                if (ARGS_EOLN == ac)
1.1       kristaps  853:                        break;
1.36      schwarze  854:                if (ARGS_PUNCT == ac)
1.1       kristaps  855:                        break;
                    856:
1.36      schwarze  857:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  858:
                    859:                /*
                    860:                 * In this case, we've located a submacro and must
                    861:                 * execute it.  Close out scope, if open.  If no
                    862:                 * elements have been generated, either create one (nc)
                    863:                 * or raise a warning.
                    864:                 */
                    865:
1.36      schwarze  866:                if (MDOC_MAX != ntok) {
1.45      schwarze  867:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  868:                                return(0);
                    869:                        if (nc && 0 == cnt) {
1.22      schwarze  870:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  871:                                        return(0);
1.22      schwarze  872:                                if ( ! rew_last(m, m->last))
1.3       schwarze  873:                                        return(0);
1.1       kristaps  874:                        } else if ( ! nc && 0 == cnt) {
                    875:                                mdoc_argv_free(arg);
1.65      schwarze  876:                                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  877:                        }
1.65      schwarze  878:
1.36      schwarze  879:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps  880:                                return(0);
1.37      schwarze  881:                        if ( ! nl)
1.1       kristaps  882:                                return(1);
1.22      schwarze  883:                        return(append_delims(m, line, pos, buf));
1.21      schwarze  884:                }
1.1       kristaps  885:
                    886:                /*
                    887:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    888:                 * a word; rewind the scope, if a delimiter; then append
                    889:                 * the word.
                    890:                 */
                    891:
1.66    ! schwarze  892:                d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.1       kristaps  893:
1.45      schwarze  894:                if (DELIM_NONE != d) {
                    895:                        /*
                    896:                         * If we encounter closing punctuation, no word
                    897:                         * has been omitted, no scope is open, and we're
                    898:                         * allowed to have an empty element, then start
                    899:                         * a new scope.  `Ar', `Fl', and `Li', only do
                    900:                         * this once per invocation.  There may be more
                    901:                         * of these (all of them?).
                    902:                         */
                    903:                        if (0 == cnt && (nc || MDOC_Li == tok) &&
                    904:                                        DELIM_CLOSE == d && ! scope) {
                    905:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
                    906:                                        return(0);
                    907:                                if (MDOC_Ar == tok || MDOC_Li == tok ||
                    908:                                                MDOC_Fl == tok)
                    909:                                        cnt++;
                    910:                                scope = 1;
                    911:                        }
                    912:                        /*
                    913:                         * Close out our scope, if one is open, before
                    914:                         * any punctuation.
                    915:                         */
                    916:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  917:                                return(0);
1.45      schwarze  918:                        scope = 0;
                    919:                } else if ( ! scope) {
1.22      schwarze  920:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  921:                                return(0);
1.45      schwarze  922:                        scope = 1;
1.1       kristaps  923:                }
                    924:
1.40      schwarze  925:                if (DELIM_NONE == d)
1.1       kristaps  926:                        cnt++;
1.66    ! schwarze  927:
        !           928:                if ( ! dword(m, line, la, p, d))
1.1       kristaps  929:                        return(0);
1.27      schwarze  930:
                    931:                /*
                    932:                 * `Fl' macros have their scope re-opened with each new
                    933:                 * word so that the `-' can be added to each one without
                    934:                 * having to parse out spaces.
                    935:                 */
1.45      schwarze  936:                if (scope && MDOC_Fl == tok) {
1.27      schwarze  937:                        if ( ! rew_elem(m, tok))
                    938:                                return(0);
1.45      schwarze  939:                        scope = 0;
1.27      schwarze  940:                }
1.1       kristaps  941:        }
                    942:
1.45      schwarze  943:        if (scope && ! rew_elem(m, tok))
1.1       kristaps  944:                return(0);
                    945:
                    946:        /*
                    947:         * If no elements have been collected and we're allowed to have
                    948:         * empties (nc), open a scope and close it out.  Otherwise,
                    949:         * raise a warning.
                    950:         */
1.32      schwarze  951:
1.1       kristaps  952:        if (nc && 0 == cnt) {
1.22      schwarze  953:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  954:                        return(0);
1.22      schwarze  955:                if ( ! rew_last(m, m->last))
1.3       schwarze  956:                        return(0);
                    957:        } else if ( ! nc && 0 == cnt) {
1.1       kristaps  958:                mdoc_argv_free(arg);
1.65      schwarze  959:                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  960:        }
                    961:
1.37      schwarze  962:        if ( ! nl)
1.1       kristaps  963:                return(1);
1.22      schwarze  964:        return(append_delims(m, line, pos, buf));
1.1       kristaps  965: }
                    966:
                    967:
                    968: static int
                    969: blk_full(MACRO_PROT_ARGS)
                    970: {
1.43      schwarze  971:        int               la, nl;
1.1       kristaps  972:        struct mdoc_arg  *arg;
1.32      schwarze  973:        struct mdoc_node *head; /* save of head macro */
1.34      schwarze  974:        struct mdoc_node *body; /* save of body macro */
1.32      schwarze  975:        struct mdoc_node *n;
1.46      schwarze  976:        enum mdoc_type    mtt;
1.36      schwarze  977:        enum mdoct        ntok;
1.37      schwarze  978:        enum margserr     ac, lac;
1.36      schwarze  979:        enum margverr     av;
1.1       kristaps  980:        char             *p;
                    981:
1.43      schwarze  982:        nl = MDOC_NEWLINE & m->flags;
                    983:
1.32      schwarze  984:        /* Close out prior implicit scope. */
1.12      schwarze  985:
1.1       kristaps  986:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.22      schwarze  987:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  988:                        return(0);
1.22      schwarze  989:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  990:                        return(0);
                    991:        }
                    992:
1.32      schwarze  993:        /*
                    994:         * This routine accomodates implicitly- and explicitly-scoped
                    995:         * macro openings.  Implicit ones first close out prior scope
                    996:         * (seen above).  Delay opening the head until necessary to
                    997:         * allow leading punctuation to print.  Special consideration
                    998:         * for `It -column', which has phrase-part syntax instead of
                    999:         * regular child nodes.
                   1000:         */
                   1001:
1.1       kristaps 1002:        for (arg = NULL;; ) {
1.32      schwarze 1003:                la = *pos;
1.36      schwarze 1004:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1005:
1.36      schwarze 1006:                if (ARGV_WORD == av) {
1.32      schwarze 1007:                        *pos = la;
1.1       kristaps 1008:                        break;
                   1009:                }
                   1010:
1.36      schwarze 1011:                if (ARGV_EOLN == av)
1.1       kristaps 1012:                        break;
1.36      schwarze 1013:                if (ARGV_ARG == av)
1.1       kristaps 1014:                        continue;
                   1015:
                   1016:                mdoc_argv_free(arg);
                   1017:                return(0);
                   1018:        }
                   1019:
1.22      schwarze 1020:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1021:                return(0);
                   1022:
1.34      schwarze 1023:        head = body = NULL;
1.1       kristaps 1024:
1.32      schwarze 1025:        /*
                   1026:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1027:         * of block partial-explicit and full-implicit.  Stupid.
                   1028:         */
1.12      schwarze 1029:
1.32      schwarze 1030:        if (MDOC_Nd == tok) {
                   1031:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1032:                        return(0);
                   1033:                head = m->last;
1.22      schwarze 1034:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.12      schwarze 1035:                        return(0);
1.22      schwarze 1036:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.12      schwarze 1037:                        return(0);
1.34      schwarze 1038:                body = m->last;
1.12      schwarze 1039:        }
                   1040:
1.37      schwarze 1041:        ac = ARGS_ERROR;
                   1042:
1.41      schwarze 1043:        for ( ; ; ) {
1.32      schwarze 1044:                la = *pos;
1.46      schwarze 1045:                /* Initialise last-phrase-type with ARGS_PEND. */
                   1046:                lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.36      schwarze 1047:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.52      schwarze 1048:
                   1049:                if (ARGS_PUNCT == ac)
                   1050:                        break;
1.1       kristaps 1051:
1.36      schwarze 1052:                if (ARGS_ERROR == ac)
1.1       kristaps 1053:                        return(0);
1.46      schwarze 1054:
                   1055:                if (ARGS_EOLN == ac) {
                   1056:                        if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
                   1057:                                break;
                   1058:                        /*
                   1059:                         * This is necessary: if the last token on a
                   1060:                         * line is a `Ta' or tab, then we'll get
                   1061:                         * ARGS_EOLN, so we must be smart enough to
                   1062:                         * reopen our scope if the last parse was a
                   1063:                         * phrase or partial phrase.
                   1064:                         */
                   1065:                        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1066:                                return(0);
                   1067:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1068:                                return(0);
                   1069:                        body = m->last;
1.1       kristaps 1070:                        break;
1.41      schwarze 1071:                }
                   1072:
1.46      schwarze 1073:                /*
                   1074:                 * Emit leading punctuation (i.e., punctuation before
                   1075:                 * the MDOC_HEAD) for non-phrase types.
                   1076:                 */
1.32      schwarze 1077:
1.37      schwarze 1078:                if (NULL == head &&
1.46      schwarze 1079:                                ARGS_PEND != ac &&
1.37      schwarze 1080:                                ARGS_PHRASE != ac &&
1.36      schwarze 1081:                                ARGS_PPHRASE != ac &&
                   1082:                                ARGS_QWORD != ac &&
1.66    ! schwarze 1083:                                DELIM_OPEN == mdoc_isdelim(p)) {
        !          1084:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1085:                                return(0);
                   1086:                        continue;
                   1087:                }
                   1088:
1.46      schwarze 1089:                /* Open a head if one hasn't been opened. */
1.32      schwarze 1090:
1.46      schwarze 1091:                if (NULL == head) {
1.32      schwarze 1092:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1093:                                return(0);
1.29      schwarze 1094:                        head = m->last;
1.32      schwarze 1095:                }
                   1096:
1.46      schwarze 1097:                if (ARGS_PHRASE == ac ||
                   1098:                                ARGS_PEND == ac ||
                   1099:                                ARGS_PPHRASE == ac) {
                   1100:                        /*
                   1101:                         * If we haven't opened a body yet, rewind the
                   1102:                         * head; if we have, rewind that instead.
                   1103:                         */
                   1104:
                   1105:                        mtt = body ? MDOC_BODY : MDOC_HEAD;
                   1106:                        if ( ! rew_sub(mtt, m, tok, line, ppos))
                   1107:                                return(0);
                   1108:
                   1109:                        /* Then allocate our body context. */
                   1110:
                   1111:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1112:                                return(0);
                   1113:                        body = m->last;
                   1114:
                   1115:                        /*
                   1116:                         * Process phrases: set whether we're in a
                   1117:                         * partial-phrase (this effects line handling)
                   1118:                         * then call down into the phrase parser.
                   1119:                         */
                   1120:
1.41      schwarze 1121:                        if (ARGS_PPHRASE == ac)
                   1122:                                m->flags |= MDOC_PPHRASE;
1.46      schwarze 1123:                        if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
                   1124:                                m->flags |= MDOC_PPHRASE;
                   1125:
                   1126:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps 1127:                                return(0);
1.46      schwarze 1128:
1.41      schwarze 1129:                        m->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1130:                        continue;
                   1131:                }
                   1132:
1.36      schwarze 1133:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1134:
                   1135:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1136:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1137:                                return(0);
1.36      schwarze 1138:                        continue;
1.32      schwarze 1139:                }
1.36      schwarze 1140:
                   1141:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1142:                        return(0);
1.36      schwarze 1143:                break;
1.32      schwarze 1144:        }
1.1       kristaps 1145:
1.32      schwarze 1146:        if (NULL == head) {
                   1147:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1148:                        return(0);
1.32      schwarze 1149:                head = m->last;
1.1       kristaps 1150:        }
                   1151:
1.43      schwarze 1152:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1153:                return(0);
1.12      schwarze 1154:
1.34      schwarze 1155:        /* If we've already opened our body, exit now. */
1.32      schwarze 1156:
1.34      schwarze 1157:        if (NULL != body)
1.46      schwarze 1158:                goto out;
1.29      schwarze 1159:
                   1160:        /*
1.34      schwarze 1161:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1162:         * explicit close-out, postpone switching the current block from
                   1163:         * head to body until the rew_sub() call closing out that
                   1164:         * sub-block.
1.29      schwarze 1165:         */
                   1166:        for (n = m->last; n && n != head; n = n->parent) {
1.34      schwarze 1167:                if (MDOC_BLOCK == n->type &&
                   1168:                                MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1169:                                ! (MDOC_VALID & n->flags)) {
1.29      schwarze 1170:                        n->pending = head;
                   1171:                        return(1);
                   1172:                }
                   1173:        }
1.54      schwarze 1174:
1.32      schwarze 1175:        /* Close out scopes to remain in a consistent state. */
1.12      schwarze 1176:
1.22      schwarze 1177:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1178:                return(0);
1.22      schwarze 1179:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1180:                return(0);
                   1181:
1.46      schwarze 1182: out:
                   1183:        if ( ! (MDOC_FREECOL & m->flags))
                   1184:                return(1);
                   1185:
                   1186:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1187:                return(0);
                   1188:        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                   1189:                return(0);
                   1190:
                   1191:        m->flags &= ~MDOC_FREECOL;
1.1       kristaps 1192:        return(1);
                   1193: }
                   1194:
                   1195:
                   1196: static int
                   1197: blk_part_imp(MACRO_PROT_ARGS)
                   1198: {
1.43      schwarze 1199:        int               la, nl;
1.36      schwarze 1200:        enum mdoct        ntok;
                   1201:        enum margserr     ac;
1.1       kristaps 1202:        char             *p;
1.32      schwarze 1203:        struct mdoc_node *blk; /* saved block context */
                   1204:        struct mdoc_node *body; /* saved body context */
                   1205:        struct mdoc_node *n;
1.1       kristaps 1206:
1.43      schwarze 1207:        nl = MDOC_NEWLINE & m->flags;
                   1208:
1.32      schwarze 1209:        /*
                   1210:         * A macro that spans to the end of the line.  This is generally
                   1211:         * (but not necessarily) called as the first macro.  The block
                   1212:         * has a head as the immediate child, which is always empty,
                   1213:         * followed by zero or more opening punctuation nodes, then the
                   1214:         * body (which may be empty, depending on the macro), then zero
                   1215:         * or more closing punctuation nodes.
                   1216:         */
1.22      schwarze 1217:
                   1218:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1219:                return(0);
1.32      schwarze 1220:
1.22      schwarze 1221:        blk = m->last;
1.32      schwarze 1222:
1.22      schwarze 1223:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1224:                return(0);
1.22      schwarze 1225:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1226:                return(0);
                   1227:
1.32      schwarze 1228:        /*
                   1229:         * Open the body scope "on-demand", that is, after we've
                   1230:         * processed all our the leading delimiters (open parenthesis,
                   1231:         * etc.).
                   1232:         */
1.1       kristaps 1233:
1.32      schwarze 1234:        for (body = NULL; ; ) {
1.22      schwarze 1235:                la = *pos;
1.36      schwarze 1236:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.32      schwarze 1237:
1.36      schwarze 1238:                if (ARGS_ERROR == ac)
1.1       kristaps 1239:                        return(0);
1.36      schwarze 1240:                if (ARGS_EOLN == ac)
1.32      schwarze 1241:                        break;
1.36      schwarze 1242:                if (ARGS_PUNCT == ac)
1.1       kristaps 1243:                        break;
                   1244:
1.36      schwarze 1245:                if (NULL == body && ARGS_QWORD != ac &&
1.66    ! schwarze 1246:                                DELIM_OPEN == mdoc_isdelim(p)) {
        !          1247:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.1       kristaps 1248:                                return(0);
                   1249:                        continue;
                   1250:                }
                   1251:
1.32      schwarze 1252:                if (NULL == body) {
                   1253:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1254:                               return(0);
                   1255:                        body = m->last;
                   1256:                }
                   1257:
1.36      schwarze 1258:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1259:
                   1260:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1261:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1262:                                return(0);
1.36      schwarze 1263:                        continue;
                   1264:                }
1.32      schwarze 1265:
1.36      schwarze 1266:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1267:                        return(0);
1.36      schwarze 1268:                break;
1.32      schwarze 1269:        }
                   1270:
                   1271:        /* Clean-ups to leave in a consistent state. */
                   1272:
                   1273:        if (NULL == body) {
                   1274:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1275:                        return(0);
1.32      schwarze 1276:                body = m->last;
1.39      schwarze 1277:        }
                   1278:
                   1279:        for (n = body->child; n && n->next; n = n->next)
                   1280:                /* Do nothing. */ ;
                   1281:
                   1282:        /*
                   1283:         * End of sentence spacing: if the last node is a text node and
                   1284:         * has a trailing period, then mark it as being end-of-sentence.
                   1285:         */
                   1286:
                   1287:        if (n && MDOC_TEXT == n->type && n->string)
1.55      schwarze 1288:                if (mandoc_eos(n->string, strlen(n->string), 1))
1.39      schwarze 1289:                        n->flags |= MDOC_EOS;
                   1290:
                   1291:        /* Up-propogate the end-of-space flag. */
                   1292:
                   1293:        if (n && (MDOC_EOS & n->flags)) {
                   1294:                body->flags |= MDOC_EOS;
                   1295:                body->parent->flags |= MDOC_EOS;
1.1       kristaps 1296:        }
                   1297:
1.47      schwarze 1298:        /*
                   1299:         * If there is an open sub-block requiring explicit close-out,
                   1300:         * postpone closing out the current block
                   1301:         * until the rew_sub() call closing out the sub-block.
                   1302:         */
                   1303:        for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
                   1304:                if (MDOC_BLOCK == n->type &&
                   1305:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1306:                    ! (MDOC_VALID & n->flags)) {
1.59      schwarze 1307:                        make_pending(n, tok, m, line, ppos);
1.47      schwarze 1308:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                   1309:                            tok, body, ENDBODY_NOSPACE))
                   1310:                                return(0);
                   1311:                        return(1);
                   1312:                }
                   1313:        }
                   1314:
1.22      schwarze 1315:        /*
                   1316:         * If we can't rewind to our body, then our scope has already
                   1317:         * been closed by another macro (like `Oc' closing `Op').  This
                   1318:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.34      schwarze 1319:         * crufty use of `Op' breakage.
1.1       kristaps 1320:         */
1.65      schwarze 1321:        if (n != body)
1.66    ! schwarze 1322:                mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
1.65      schwarze 1323:                                "%s broken", mdoc_macronames[tok]);
1.32      schwarze 1324:
1.47      schwarze 1325:        if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      schwarze 1326:                return(0);
1.1       kristaps 1327:
1.22      schwarze 1328:        /* Standard appending of delimiters. */
1.1       kristaps 1329:
1.43      schwarze 1330:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1331:                return(0);
                   1332:
1.22      schwarze 1333:        /* Rewind scope, if applicable. */
1.1       kristaps 1334:
1.47      schwarze 1335:        if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1336:                return(0);
                   1337:
                   1338:        return(1);
                   1339: }
                   1340:
                   1341:
                   1342: static int
                   1343: blk_part_exp(MACRO_PROT_ARGS)
                   1344: {
1.37      schwarze 1345:        int               la, nl;
1.36      schwarze 1346:        enum margserr     ac;
1.32      schwarze 1347:        struct mdoc_node *head; /* keep track of head */
                   1348:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1349:        char             *p;
1.36      schwarze 1350:        enum mdoct        ntok;
1.1       kristaps 1351:
1.37      schwarze 1352:        nl = MDOC_NEWLINE & m->flags;
                   1353:
1.32      schwarze 1354:        /*
                   1355:         * The opening of an explicit macro having zero or more leading
                   1356:         * punctuation nodes; a head with optional single element (the
                   1357:         * case of `Eo'); and a body that may be empty.
                   1358:         */
1.22      schwarze 1359:
                   1360:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1361:                return(0);
1.22      schwarze 1362:
1.32      schwarze 1363:        for (head = body = NULL; ; ) {
1.22      schwarze 1364:                la = *pos;
1.36      schwarze 1365:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1366:
1.36      schwarze 1367:                if (ARGS_ERROR == ac)
1.1       kristaps 1368:                        return(0);
1.36      schwarze 1369:                if (ARGS_PUNCT == ac)
1.1       kristaps 1370:                        break;
1.36      schwarze 1371:                if (ARGS_EOLN == ac)
1.1       kristaps 1372:                        break;
                   1373:
1.32      schwarze 1374:                /* Flush out leading punctuation. */
                   1375:
1.36      schwarze 1376:                if (NULL == head && ARGS_QWORD != ac &&
1.66    ! schwarze 1377:                                DELIM_OPEN == mdoc_isdelim(p)) {
1.32      schwarze 1378:                        assert(NULL == body);
1.66    ! schwarze 1379:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1380:                                return(0);
                   1381:                        continue;
                   1382:                }
                   1383:
                   1384:                if (NULL == head) {
                   1385:                        assert(NULL == body);
                   1386:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1387:                                return(0);
1.32      schwarze 1388:                        head = m->last;
1.1       kristaps 1389:                }
                   1390:
1.32      schwarze 1391:                /*
                   1392:                 * `Eo' gobbles any data into the head, but most other
                   1393:                 * macros just immediately close out and begin the body.
                   1394:                 */
                   1395:
                   1396:                if (NULL == body) {
                   1397:                        assert(head);
                   1398:                        /* No check whether it's a macro! */
                   1399:                        if (MDOC_Eo == tok)
1.66    ! schwarze 1400:                                if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1401:                                        return(0);
                   1402:
1.22      schwarze 1403:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1404:                                return(0);
1.22      schwarze 1405:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1406:                                return(0);
1.32      schwarze 1407:                        body = m->last;
                   1408:
                   1409:                        if (MDOC_Eo == tok)
                   1410:                                continue;
                   1411:                }
                   1412:
                   1413:                assert(NULL != head && NULL != body);
                   1414:
1.36      schwarze 1415:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1416:
                   1417:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1418:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1419:                                return(0);
1.36      schwarze 1420:                        continue;
1.1       kristaps 1421:                }
1.32      schwarze 1422:
1.36      schwarze 1423:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1424:                        return(0);
1.36      schwarze 1425:                break;
1.1       kristaps 1426:        }
                   1427:
1.32      schwarze 1428:        /* Clean-up to leave in a consistent state. */
                   1429:
                   1430:        if (NULL == head) {
                   1431:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1432:                        return(0);
                   1433:                head = m->last;
                   1434:        }
1.22      schwarze 1435:
1.32      schwarze 1436:        if (NULL == body) {
1.22      schwarze 1437:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1438:                        return(0);
1.22      schwarze 1439:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1440:                        return(0);
1.32      schwarze 1441:                body = m->last;
1.1       kristaps 1442:        }
                   1443:
1.22      schwarze 1444:        /* Standard appending of delimiters. */
                   1445:
1.37      schwarze 1446:        if ( ! nl)
1.1       kristaps 1447:                return(1);
1.22      schwarze 1448:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1449: }
                   1450:
                   1451:
1.37      schwarze 1452: /* ARGSUSED */
1.1       kristaps 1453: static int
                   1454: in_line_argn(MACRO_PROT_ARGS)
                   1455: {
1.37      schwarze 1456:        int              la, flushed, j, maxargs, nl;
1.36      schwarze 1457:        enum margserr    ac;
                   1458:        enum margverr    av;
                   1459:        struct mdoc_arg *arg;
                   1460:        char            *p;
                   1461:        enum mdoct       ntok;
1.1       kristaps 1462:
1.37      schwarze 1463:        nl = MDOC_NEWLINE & m->flags;
                   1464:
1.32      schwarze 1465:        /*
                   1466:         * A line macro that has a fixed number of arguments (maxargs).
                   1467:         * Only open the scope once the first non-leading-punctuation is
                   1468:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1469:         * keep it open until the maximum number of arguments are
                   1470:         * exhausted.
                   1471:         */
1.1       kristaps 1472:
                   1473:        switch (tok) {
                   1474:        case (MDOC_Ap):
                   1475:                /* FALLTHROUGH */
                   1476:        case (MDOC_No):
                   1477:                /* FALLTHROUGH */
                   1478:        case (MDOC_Ns):
                   1479:                /* FALLTHROUGH */
                   1480:        case (MDOC_Ux):
                   1481:                maxargs = 0;
                   1482:                break;
1.64      schwarze 1483:        case (MDOC_Bx):
                   1484:                /* FALLTHROUGH */
1.28      schwarze 1485:        case (MDOC_Xr):
                   1486:                maxargs = 2;
                   1487:                break;
1.1       kristaps 1488:        default:
                   1489:                maxargs = 1;
                   1490:                break;
                   1491:        }
                   1492:
1.32      schwarze 1493:        for (arg = NULL; ; ) {
1.22      schwarze 1494:                la = *pos;
1.36      schwarze 1495:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1496:
1.36      schwarze 1497:                if (ARGV_WORD == av) {
1.22      schwarze 1498:                        *pos = la;
1.1       kristaps 1499:                        break;
                   1500:                }
                   1501:
1.36      schwarze 1502:                if (ARGV_EOLN == av)
1.1       kristaps 1503:                        break;
1.36      schwarze 1504:                if (ARGV_ARG == av)
1.1       kristaps 1505:                        continue;
                   1506:
                   1507:                mdoc_argv_free(arg);
                   1508:                return(0);
                   1509:        }
                   1510:
1.32      schwarze 1511:        for (flushed = j = 0; ; ) {
1.22      schwarze 1512:                la = *pos;
1.36      schwarze 1513:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1514:
1.36      schwarze 1515:                if (ARGS_ERROR == ac)
1.1       kristaps 1516:                        return(0);
1.36      schwarze 1517:                if (ARGS_PUNCT == ac)
1.1       kristaps 1518:                        break;
1.36      schwarze 1519:                if (ARGS_EOLN == ac)
1.1       kristaps 1520:                        break;
                   1521:
1.32      schwarze 1522:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.66    ! schwarze 1523:                                ARGS_QWORD != ac && 0 == j &&
        !          1524:                                DELIM_OPEN == mdoc_isdelim(p)) {
        !          1525:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1526:                                return(0);
                   1527:                        continue;
1.40      schwarze 1528:                } else if (0 == j)
1.32      schwarze 1529:                       if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1530:                               return(0);
                   1531:
                   1532:                if (j == maxargs && ! flushed) {
                   1533:                        if ( ! rew_elem(m, tok))
                   1534:                                return(0);
                   1535:                        flushed = 1;
                   1536:                }
                   1537:
1.36      schwarze 1538:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1539:
                   1540:                if (MDOC_MAX != ntok) {
1.22      schwarze 1541:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1542:                                return(0);
                   1543:                        flushed = 1;
1.36      schwarze 1544:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1545:                                return(0);
1.32      schwarze 1546:                        j++;
1.1       kristaps 1547:                        break;
                   1548:                }
                   1549:
                   1550:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.36      schwarze 1551:                                ARGS_QWORD != ac &&
1.40      schwarze 1552:                                ! flushed &&
1.66    ! schwarze 1553:                                DELIM_NONE != mdoc_isdelim(p)) {
1.22      schwarze 1554:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1555:                                return(0);
                   1556:                        flushed = 1;
                   1557:                }
1.28      schwarze 1558:
                   1559:                /*
                   1560:                 * XXX: this is a hack to work around groff's ugliness
                   1561:                 * as regards `Xr' and extraneous arguments.  It should
                   1562:                 * ideally be deprecated behaviour, but because this is
                   1563:                 * code is no here, it's unlikely to be removed.
                   1564:                 */
                   1565:                if (MDOC_Xr == tok && j == maxargs) {
1.32      schwarze 1566:                        if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1.28      schwarze 1567:                                return(0);
                   1568:                        if ( ! rew_elem(m, MDOC_Ns))
                   1569:                                return(0);
                   1570:                }
                   1571:
1.66    ! schwarze 1572:                if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1573:                        return(0);
1.32      schwarze 1574:                j++;
1.1       kristaps 1575:        }
                   1576:
1.32      schwarze 1577:        if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1578:               return(0);
                   1579:
                   1580:        /* Close out in a consistent state. */
1.22      schwarze 1581:
                   1582:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1583:                return(0);
1.37      schwarze 1584:        if ( ! nl)
1.1       kristaps 1585:                return(1);
1.22      schwarze 1586:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1587: }
                   1588:
                   1589:
                   1590: static int
                   1591: in_line_eoln(MACRO_PROT_ARGS)
                   1592: {
1.36      schwarze 1593:        int              la;
                   1594:        enum margserr    ac;
                   1595:        enum margverr    av;
                   1596:        struct mdoc_arg *arg;
                   1597:        char            *p;
                   1598:        enum mdoct       ntok;
1.1       kristaps 1599:
                   1600:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1601:
1.53      schwarze 1602:        if (tok == MDOC_Pp)
                   1603:                rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos);
                   1604:
1.22      schwarze 1605:        /* Parse macro arguments. */
1.1       kristaps 1606:
1.22      schwarze 1607:        for (arg = NULL; ; ) {
1.1       kristaps 1608:                la = *pos;
1.36      schwarze 1609:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1610:
1.36      schwarze 1611:                if (ARGV_WORD == av) {
1.1       kristaps 1612:                        *pos = la;
                   1613:                        break;
                   1614:                }
1.36      schwarze 1615:                if (ARGV_EOLN == av)
1.1       kristaps 1616:                        break;
1.36      schwarze 1617:                if (ARGV_ARG == av)
1.1       kristaps 1618:                        continue;
                   1619:
                   1620:                mdoc_argv_free(arg);
                   1621:                return(0);
                   1622:        }
                   1623:
1.22      schwarze 1624:        /* Open element scope. */
                   1625:
                   1626:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1627:                return(0);
                   1628:
1.22      schwarze 1629:        /* Parse argument terms. */
1.1       kristaps 1630:
                   1631:        for (;;) {
                   1632:                la = *pos;
1.36      schwarze 1633:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1634:
1.36      schwarze 1635:                if (ARGS_ERROR == ac)
1.1       kristaps 1636:                        return(0);
1.36      schwarze 1637:                if (ARGS_EOLN == ac)
1.1       kristaps 1638:                        break;
                   1639:
1.36      schwarze 1640:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1641:
1.36      schwarze 1642:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1643:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1644:                                return(0);
1.36      schwarze 1645:                        continue;
                   1646:                }
1.1       kristaps 1647:
1.36      schwarze 1648:                if ( ! rew_elem(m, tok))
1.1       kristaps 1649:                        return(0);
1.36      schwarze 1650:                return(mdoc_macro(m, ntok, line, la, pos, buf));
1.1       kristaps 1651:        }
                   1652:
1.22      schwarze 1653:        /* Close out (no delimiters). */
                   1654:
                   1655:        return(rew_elem(m, tok));
1.28      schwarze 1656: }
                   1657:
                   1658:
                   1659: /* ARGSUSED */
                   1660: static int
                   1661: ctx_synopsis(MACRO_PROT_ARGS)
                   1662: {
1.37      schwarze 1663:        int              nl;
                   1664:
                   1665:        nl = MDOC_NEWLINE & m->flags;
1.28      schwarze 1666:
                   1667:        /* If we're not in the SYNOPSIS, go straight to in-line. */
1.53      schwarze 1668:        if ( ! (MDOC_SYNOPSIS & m->flags))
1.28      schwarze 1669:                return(in_line(m, tok, line, ppos, pos, buf));
                   1670:
                   1671:        /* If we're a nested call, same place. */
1.37      schwarze 1672:        if ( ! nl)
1.28      schwarze 1673:                return(in_line(m, tok, line, ppos, pos, buf));
                   1674:
                   1675:        /*
                   1676:         * XXX: this will open a block scope; however, if later we end
                   1677:         * up formatting the block scope, then child nodes will inherit
                   1678:         * the formatting.  Be careful.
                   1679:         */
1.51      schwarze 1680:        if (MDOC_Nm == tok)
                   1681:                return(blk_full(m, tok, line, ppos, pos, buf));
                   1682:        assert(MDOC_Vt == tok);
1.28      schwarze 1683:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
1.1       kristaps 1684: }
                   1685:
                   1686:
                   1687: /* ARGSUSED */
                   1688: static int
                   1689: obsolete(MACRO_PROT_ARGS)
                   1690: {
                   1691:
1.65      schwarze 1692:        mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS);
                   1693:        return(1);
1.1       kristaps 1694: }
                   1695:
                   1696:
1.17      schwarze 1697: /*
                   1698:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1699:  * They're unusual because they're basically free-form text until a
                   1700:  * macro is encountered.
                   1701:  */
1.1       kristaps 1702: static int
1.46      schwarze 1703: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1704: {
1.36      schwarze 1705:        int              la, pos;
1.46      schwarze 1706:        enum margserr    ac;
1.36      schwarze 1707:        enum mdoct       ntok;
                   1708:        char            *p;
1.1       kristaps 1709:
1.17      schwarze 1710:        for (pos = ppos; ; ) {
                   1711:                la = pos;
1.1       kristaps 1712:
1.46      schwarze 1713:                ac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1714:
1.46      schwarze 1715:                if (ARGS_ERROR == ac)
1.17      schwarze 1716:                        return(0);
1.46      schwarze 1717:                if (ARGS_EOLN == ac)
1.17      schwarze 1718:                        break;
1.1       kristaps 1719:
1.46      schwarze 1720:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1721:
1.36      schwarze 1722:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1723:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1724:                                return(0);
1.36      schwarze 1725:                        continue;
                   1726:                }
1.1       kristaps 1727:
1.36      schwarze 1728:                if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1       kristaps 1729:                        return(0);
1.36      schwarze 1730:                return(append_delims(m, line, &pos, buf));
1.1       kristaps 1731:        }
                   1732:
                   1733:        return(1);
                   1734: }
1.17      schwarze 1735:
                   1736:
1.46      schwarze 1737: /* ARGSUSED */
                   1738: static int
                   1739: phrase_ta(MACRO_PROT_ARGS)
                   1740: {
                   1741:        int               la;
                   1742:        enum mdoct        ntok;
                   1743:        enum margserr     ac;
                   1744:        char             *p;
                   1745:
                   1746:        /*
                   1747:         * FIXME: this is overly restrictive: if the `Ta' is unexpected,
                   1748:         * it should simply error out with ARGSLOST.
                   1749:         */
                   1750:
                   1751:        if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
                   1752:                return(0);
                   1753:        if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
                   1754:                return(0);
                   1755:
                   1756:        for (;;) {
                   1757:                la = *pos;
                   1758:                ac = mdoc_zargs(m, line, pos, buf, 0, &p);
                   1759:
                   1760:                if (ARGS_ERROR == ac)
                   1761:                        return(0);
                   1762:                if (ARGS_EOLN == ac)
                   1763:                        break;
                   1764:
                   1765:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
                   1766:
                   1767:                if (MDOC_MAX == ntok) {
1.66    ! schwarze 1768:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.46      schwarze 1769:                                return(0);
                   1770:                        continue;
                   1771:                }
                   1772:
                   1773:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
                   1774:                        return(0);
                   1775:                return(append_delims(m, line, pos, buf));
                   1776:        }
                   1777:
                   1778:        return(1);
                   1779: }