[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.68

1.68    ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.67 2011/04/24 16:49:10 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;
1.67      schwarze  599:        else if (DELIM_CLOSE == d && m->last->prev &&
                    600:                        m->last->prev->tok != MDOC_No)
1.66      schwarze  601:                m->last->flags |= MDOC_DELIMC;
                    602:
                    603:        return(1);
                    604: }
1.1       kristaps  605:
                    606: static int
1.42      schwarze  607: append_delims(struct mdoc *m, int line, int *pos, char *buf)
1.1       kristaps  608: {
1.42      schwarze  609:        int              la;
1.36      schwarze  610:        enum margserr    ac;
1.1       kristaps  611:        char            *p;
                    612:
1.42      schwarze  613:        if ('\0' == buf[*pos])
1.1       kristaps  614:                return(1);
                    615:
                    616:        for (;;) {
1.42      schwarze  617:                la = *pos;
1.68    ! schwarze  618:                ac = mdoc_zargs(m, line, pos, buf, &p);
1.1       kristaps  619:
1.36      schwarze  620:                if (ARGS_ERROR == ac)
1.1       kristaps  621:                        return(0);
1.36      schwarze  622:                else if (ARGS_EOLN == ac)
1.1       kristaps  623:                        break;
1.42      schwarze  624:
1.66      schwarze  625:                dword(m, line, la, p, DELIM_MAX);
1.43      schwarze  626:
1.42      schwarze  627:                /*
                    628:                 * If we encounter end-of-sentence symbols, then trigger
                    629:                 * the double-space.
                    630:                 *
1.68    ! schwarze  631:                 * XXX: it's easy to allow this to propagate outward to
1.42      schwarze  632:                 * the last symbol, such that `. )' will cause the
                    633:                 * correct double-spacing.  However, (1) groff isn't
                    634:                 * smart enough to do this and (2) it would require
                    635:                 * knowing which symbols break this behaviour, for
1.68    ! schwarze  636:                 * example, `.  ;' shouldn't propagate the double-space.
1.42      schwarze  637:                 */
1.55      schwarze  638:                if (mandoc_eos(p, strlen(p), 0))
1.42      schwarze  639:                        m->last->flags |= MDOC_EOS;
1.1       kristaps  640:        }
                    641:
                    642:        return(1);
                    643: }
                    644:
                    645:
                    646: /*
                    647:  * Close out block partial/full explicit.
                    648:  */
                    649: static int
                    650: blk_exp_close(MACRO_PROT_ARGS)
                    651: {
1.47      schwarze  652:        struct mdoc_node *body;         /* Our own body. */
                    653:        struct mdoc_node *later;        /* A sub-block starting later. */
                    654:        struct mdoc_node *n;            /* For searching backwards. */
                    655:
1.37      schwarze  656:        int              j, lastarg, maxargs, flushed, nl;
1.36      schwarze  657:        enum margserr    ac;
1.47      schwarze  658:        enum mdoct       atok, ntok;
1.1       kristaps  659:        char            *p;
                    660:
1.37      schwarze  661:        nl = MDOC_NEWLINE & m->flags;
                    662:
1.1       kristaps  663:        switch (tok) {
                    664:        case (MDOC_Ec):
                    665:                maxargs = 1;
                    666:                break;
                    667:        default:
                    668:                maxargs = 0;
                    669:                break;
                    670:        }
                    671:
1.47      schwarze  672:        /*
                    673:         * Search backwards for beginnings of blocks,
                    674:         * both of our own and of pending sub-blocks.
                    675:         */
                    676:        atok = rew_alt(tok);
                    677:        body = later = NULL;
                    678:        for (n = m->last; n; n = n->parent) {
                    679:                if (MDOC_VALID & n->flags)
                    680:                        continue;
                    681:
                    682:                /* Remember the start of our own body. */
                    683:                if (MDOC_BODY == n->type && atok == n->tok) {
1.54      schwarze  684:                        if (ENDBODY_NOT == n->end)
1.47      schwarze  685:                                body = n;
                    686:                        continue;
                    687:                }
                    688:
1.51      schwarze  689:                if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.47      schwarze  690:                        continue;
                    691:                if (atok == n->tok) {
                    692:                        assert(body);
                    693:
                    694:                        /*
                    695:                         * Found the start of our own block.
                    696:                         * When there is no pending sub block,
                    697:                         * just proceed to closing out.
                    698:                         */
                    699:                        if (NULL == later)
                    700:                                break;
                    701:
                    702:                        /*
                    703:                         * When there is a pending sub block,
                    704:                         * postpone closing out the current block
                    705:                         * until the rew_sub() closing out the sub-block.
                    706:                         */
1.59      schwarze  707:                        make_pending(later, tok, m, line, ppos);
1.47      schwarze  708:
                    709:                        /*
                    710:                         * Mark the place where the formatting - but not
                    711:                         * the scope - of the current block ends.
                    712:                         */
                    713:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                    714:                            atok, body, ENDBODY_SPACE))
                    715:                                return(0);
                    716:                        break;
                    717:                }
                    718:
                    719:                /*
                    720:                 * When finding an open sub block, remember the last
                    721:                 * open explicit block, or, in case there are only
                    722:                 * implicit ones, the first open implicit block.
                    723:                 */
                    724:                if (later &&
                    725:                    MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
                    726:                        continue;
1.60      schwarze  727:                if (MDOC_CALLABLE & mdoc_macros[n->tok].flags)
1.47      schwarze  728:                        later = n;
                    729:        }
                    730:
1.1       kristaps  731:        if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.44      schwarze  732:                /* FIXME: do this in validate */
1.14      schwarze  733:                if (buf[*pos])
1.65      schwarze  734:                        mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST);
1.14      schwarze  735:
1.22      schwarze  736:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.14      schwarze  737:                        return(0);
1.22      schwarze  738:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  739:        }
                    740:
1.22      schwarze  741:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  742:                return(0);
                    743:
1.47      schwarze  744:        if (NULL == later && maxargs > 0)
1.22      schwarze  745:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  746:                        return(0);
                    747:
1.20      schwarze  748:        for (flushed = j = 0; ; j++) {
1.1       kristaps  749:                lastarg = *pos;
                    750:
                    751:                if (j == maxargs && ! flushed) {
1.22      schwarze  752:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  753:                                return(0);
                    754:                        flushed = 1;
                    755:                }
                    756:
1.36      schwarze  757:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  758:
1.36      schwarze  759:                if (ARGS_ERROR == ac)
1.1       kristaps  760:                        return(0);
1.36      schwarze  761:                if (ARGS_PUNCT == ac)
1.1       kristaps  762:                        break;
1.36      schwarze  763:                if (ARGS_EOLN == ac)
1.1       kristaps  764:                        break;
                    765:
1.36      schwarze  766:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                    767:
                    768:                if (MDOC_MAX == ntok) {
1.66      schwarze  769:                        if ( ! dword(m, line, lastarg, p, DELIM_MAX))
1.1       kristaps  770:                                return(0);
1.36      schwarze  771:                        continue;
                    772:                }
1.1       kristaps  773:
1.36      schwarze  774:                if ( ! flushed) {
                    775:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                    776:                                return(0);
                    777:                        flushed = 1;
                    778:                }
                    779:                if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
1.1       kristaps  780:                        return(0);
1.36      schwarze  781:                break;
1.1       kristaps  782:        }
                    783:
1.22      schwarze  784:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  785:                return(0);
                    786:
1.37      schwarze  787:        if ( ! nl)
1.1       kristaps  788:                return(1);
1.22      schwarze  789:        return(append_delims(m, line, pos, buf));
1.1       kristaps  790: }
                    791:
                    792:
                    793: static int
                    794: in_line(MACRO_PROT_ARGS)
                    795: {
1.45      schwarze  796:        int              la, scope, cnt, nc, nl;
1.36      schwarze  797:        enum margverr    av;
                    798:        enum mdoct       ntok;
                    799:        enum margserr    ac;
1.40      schwarze  800:        enum mdelim      d;
1.36      schwarze  801:        struct mdoc_arg *arg;
                    802:        char            *p;
1.1       kristaps  803:
1.37      schwarze  804:        nl = MDOC_NEWLINE & m->flags;
                    805:
1.1       kristaps  806:        /*
                    807:         * Whether we allow ignored elements (those without content,
                    808:         * usually because of reserved words) to squeak by.
                    809:         */
1.32      schwarze  810:
1.1       kristaps  811:        switch (tok) {
1.19      schwarze  812:        case (MDOC_An):
                    813:                /* FALLTHROUGH */
                    814:        case (MDOC_Ar):
1.1       kristaps  815:                /* FALLTHROUGH */
                    816:        case (MDOC_Fl):
                    817:                /* FALLTHROUGH */
1.60      schwarze  818:        case (MDOC_Mt):
1.18      schwarze  819:                /* FALLTHROUGH */
1.19      schwarze  820:        case (MDOC_Nm):
                    821:                /* FALLTHROUGH */
1.18      schwarze  822:        case (MDOC_Pa):
1.1       kristaps  823:                nc = 1;
                    824:                break;
                    825:        default:
                    826:                nc = 0;
                    827:                break;
                    828:        }
                    829:
1.20      schwarze  830:        for (arg = NULL;; ) {
1.1       kristaps  831:                la = *pos;
1.36      schwarze  832:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  833:
1.36      schwarze  834:                if (ARGV_WORD == av) {
1.1       kristaps  835:                        *pos = la;
                    836:                        break;
                    837:                }
1.36      schwarze  838:                if (ARGV_EOLN == av)
1.1       kristaps  839:                        break;
1.36      schwarze  840:                if (ARGV_ARG == av)
1.1       kristaps  841:                        continue;
                    842:
                    843:                mdoc_argv_free(arg);
                    844:                return(0);
                    845:        }
                    846:
1.45      schwarze  847:        for (cnt = scope = 0;; ) {
1.1       kristaps  848:                la = *pos;
1.36      schwarze  849:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  850:
1.36      schwarze  851:                if (ARGS_ERROR == ac)
1.1       kristaps  852:                        return(0);
1.36      schwarze  853:                if (ARGS_EOLN == ac)
1.1       kristaps  854:                        break;
1.36      schwarze  855:                if (ARGS_PUNCT == ac)
1.1       kristaps  856:                        break;
                    857:
1.36      schwarze  858:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  859:
                    860:                /*
                    861:                 * In this case, we've located a submacro and must
                    862:                 * execute it.  Close out scope, if open.  If no
                    863:                 * elements have been generated, either create one (nc)
                    864:                 * or raise a warning.
                    865:                 */
                    866:
1.36      schwarze  867:                if (MDOC_MAX != ntok) {
1.45      schwarze  868:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  869:                                return(0);
                    870:                        if (nc && 0 == cnt) {
1.22      schwarze  871:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  872:                                        return(0);
1.22      schwarze  873:                                if ( ! rew_last(m, m->last))
1.3       schwarze  874:                                        return(0);
1.1       kristaps  875:                        } else if ( ! nc && 0 == cnt) {
                    876:                                mdoc_argv_free(arg);
1.65      schwarze  877:                                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  878:                        }
1.65      schwarze  879:
1.36      schwarze  880:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps  881:                                return(0);
1.37      schwarze  882:                        if ( ! nl)
1.1       kristaps  883:                                return(1);
1.22      schwarze  884:                        return(append_delims(m, line, pos, buf));
1.21      schwarze  885:                }
1.1       kristaps  886:
                    887:                /*
                    888:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    889:                 * a word; rewind the scope, if a delimiter; then append
                    890:                 * the word.
                    891:                 */
                    892:
1.66      schwarze  893:                d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.1       kristaps  894:
1.45      schwarze  895:                if (DELIM_NONE != d) {
                    896:                        /*
                    897:                         * If we encounter closing punctuation, no word
                    898:                         * has been omitted, no scope is open, and we're
                    899:                         * allowed to have an empty element, then start
                    900:                         * a new scope.  `Ar', `Fl', and `Li', only do
                    901:                         * this once per invocation.  There may be more
                    902:                         * of these (all of them?).
                    903:                         */
                    904:                        if (0 == cnt && (nc || MDOC_Li == tok) &&
                    905:                                        DELIM_CLOSE == d && ! scope) {
                    906:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
                    907:                                        return(0);
                    908:                                if (MDOC_Ar == tok || MDOC_Li == tok ||
                    909:                                                MDOC_Fl == tok)
                    910:                                        cnt++;
                    911:                                scope = 1;
                    912:                        }
                    913:                        /*
                    914:                         * Close out our scope, if one is open, before
                    915:                         * any punctuation.
                    916:                         */
                    917:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  918:                                return(0);
1.45      schwarze  919:                        scope = 0;
                    920:                } else if ( ! scope) {
1.22      schwarze  921:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  922:                                return(0);
1.45      schwarze  923:                        scope = 1;
1.1       kristaps  924:                }
                    925:
1.40      schwarze  926:                if (DELIM_NONE == d)
1.1       kristaps  927:                        cnt++;
1.66      schwarze  928:
                    929:                if ( ! dword(m, line, la, p, d))
1.1       kristaps  930:                        return(0);
1.27      schwarze  931:
                    932:                /*
                    933:                 * `Fl' macros have their scope re-opened with each new
                    934:                 * word so that the `-' can be added to each one without
                    935:                 * having to parse out spaces.
                    936:                 */
1.45      schwarze  937:                if (scope && MDOC_Fl == tok) {
1.27      schwarze  938:                        if ( ! rew_elem(m, tok))
                    939:                                return(0);
1.45      schwarze  940:                        scope = 0;
1.27      schwarze  941:                }
1.1       kristaps  942:        }
                    943:
1.45      schwarze  944:        if (scope && ! rew_elem(m, tok))
1.1       kristaps  945:                return(0);
                    946:
                    947:        /*
                    948:         * If no elements have been collected and we're allowed to have
                    949:         * empties (nc), open a scope and close it out.  Otherwise,
                    950:         * raise a warning.
                    951:         */
1.32      schwarze  952:
1.1       kristaps  953:        if (nc && 0 == cnt) {
1.22      schwarze  954:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  955:                        return(0);
1.22      schwarze  956:                if ( ! rew_last(m, m->last))
1.3       schwarze  957:                        return(0);
                    958:        } else if ( ! nc && 0 == cnt) {
1.1       kristaps  959:                mdoc_argv_free(arg);
1.65      schwarze  960:                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  961:        }
                    962:
1.37      schwarze  963:        if ( ! nl)
1.1       kristaps  964:                return(1);
1.22      schwarze  965:        return(append_delims(m, line, pos, buf));
1.1       kristaps  966: }
                    967:
                    968:
                    969: static int
                    970: blk_full(MACRO_PROT_ARGS)
                    971: {
1.43      schwarze  972:        int               la, nl;
1.1       kristaps  973:        struct mdoc_arg  *arg;
1.32      schwarze  974:        struct mdoc_node *head; /* save of head macro */
1.34      schwarze  975:        struct mdoc_node *body; /* save of body macro */
1.32      schwarze  976:        struct mdoc_node *n;
1.46      schwarze  977:        enum mdoc_type    mtt;
1.36      schwarze  978:        enum mdoct        ntok;
1.37      schwarze  979:        enum margserr     ac, lac;
1.36      schwarze  980:        enum margverr     av;
1.1       kristaps  981:        char             *p;
                    982:
1.43      schwarze  983:        nl = MDOC_NEWLINE & m->flags;
                    984:
1.32      schwarze  985:        /* Close out prior implicit scope. */
1.12      schwarze  986:
1.1       kristaps  987:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.22      schwarze  988:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  989:                        return(0);
1.22      schwarze  990:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  991:                        return(0);
                    992:        }
                    993:
1.32      schwarze  994:        /*
1.68    ! schwarze  995:         * This routine accommodates implicitly- and explicitly-scoped
1.32      schwarze  996:         * macro openings.  Implicit ones first close out prior scope
                    997:         * (seen above).  Delay opening the head until necessary to
                    998:         * allow leading punctuation to print.  Special consideration
                    999:         * for `It -column', which has phrase-part syntax instead of
                   1000:         * regular child nodes.
                   1001:         */
                   1002:
1.1       kristaps 1003:        for (arg = NULL;; ) {
1.32      schwarze 1004:                la = *pos;
1.36      schwarze 1005:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1006:
1.36      schwarze 1007:                if (ARGV_WORD == av) {
1.32      schwarze 1008:                        *pos = la;
1.1       kristaps 1009:                        break;
                   1010:                }
                   1011:
1.36      schwarze 1012:                if (ARGV_EOLN == av)
1.1       kristaps 1013:                        break;
1.36      schwarze 1014:                if (ARGV_ARG == av)
1.1       kristaps 1015:                        continue;
                   1016:
                   1017:                mdoc_argv_free(arg);
                   1018:                return(0);
                   1019:        }
                   1020:
1.22      schwarze 1021:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1022:                return(0);
                   1023:
1.34      schwarze 1024:        head = body = NULL;
1.1       kristaps 1025:
1.32      schwarze 1026:        /*
                   1027:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1028:         * of block partial-explicit and full-implicit.  Stupid.
                   1029:         */
1.12      schwarze 1030:
1.32      schwarze 1031:        if (MDOC_Nd == tok) {
                   1032:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1033:                        return(0);
                   1034:                head = m->last;
1.22      schwarze 1035:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.12      schwarze 1036:                        return(0);
1.22      schwarze 1037:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.12      schwarze 1038:                        return(0);
1.34      schwarze 1039:                body = m->last;
1.12      schwarze 1040:        }
                   1041:
1.37      schwarze 1042:        ac = ARGS_ERROR;
                   1043:
1.41      schwarze 1044:        for ( ; ; ) {
1.32      schwarze 1045:                la = *pos;
1.46      schwarze 1046:                /* Initialise last-phrase-type with ARGS_PEND. */
                   1047:                lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.36      schwarze 1048:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.52      schwarze 1049:
                   1050:                if (ARGS_PUNCT == ac)
                   1051:                        break;
1.1       kristaps 1052:
1.36      schwarze 1053:                if (ARGS_ERROR == ac)
1.1       kristaps 1054:                        return(0);
1.46      schwarze 1055:
                   1056:                if (ARGS_EOLN == ac) {
                   1057:                        if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
                   1058:                                break;
                   1059:                        /*
                   1060:                         * This is necessary: if the last token on a
                   1061:                         * line is a `Ta' or tab, then we'll get
                   1062:                         * ARGS_EOLN, so we must be smart enough to
                   1063:                         * reopen our scope if the last parse was a
                   1064:                         * phrase or partial phrase.
                   1065:                         */
                   1066:                        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1067:                                return(0);
                   1068:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1069:                                return(0);
                   1070:                        body = m->last;
1.1       kristaps 1071:                        break;
1.41      schwarze 1072:                }
                   1073:
1.46      schwarze 1074:                /*
                   1075:                 * Emit leading punctuation (i.e., punctuation before
                   1076:                 * the MDOC_HEAD) for non-phrase types.
                   1077:                 */
1.32      schwarze 1078:
1.37      schwarze 1079:                if (NULL == head &&
1.46      schwarze 1080:                                ARGS_PEND != ac &&
1.37      schwarze 1081:                                ARGS_PHRASE != ac &&
1.36      schwarze 1082:                                ARGS_PPHRASE != ac &&
                   1083:                                ARGS_QWORD != ac &&
1.66      schwarze 1084:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1085:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1086:                                return(0);
                   1087:                        continue;
                   1088:                }
                   1089:
1.46      schwarze 1090:                /* Open a head if one hasn't been opened. */
1.32      schwarze 1091:
1.46      schwarze 1092:                if (NULL == head) {
1.32      schwarze 1093:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1094:                                return(0);
1.29      schwarze 1095:                        head = m->last;
1.32      schwarze 1096:                }
                   1097:
1.46      schwarze 1098:                if (ARGS_PHRASE == ac ||
                   1099:                                ARGS_PEND == ac ||
                   1100:                                ARGS_PPHRASE == ac) {
                   1101:                        /*
                   1102:                         * If we haven't opened a body yet, rewind the
                   1103:                         * head; if we have, rewind that instead.
                   1104:                         */
                   1105:
                   1106:                        mtt = body ? MDOC_BODY : MDOC_HEAD;
                   1107:                        if ( ! rew_sub(mtt, m, tok, line, ppos))
                   1108:                                return(0);
                   1109:
                   1110:                        /* Then allocate our body context. */
                   1111:
                   1112:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1113:                                return(0);
                   1114:                        body = m->last;
                   1115:
                   1116:                        /*
                   1117:                         * Process phrases: set whether we're in a
                   1118:                         * partial-phrase (this effects line handling)
                   1119:                         * then call down into the phrase parser.
                   1120:                         */
                   1121:
1.41      schwarze 1122:                        if (ARGS_PPHRASE == ac)
                   1123:                                m->flags |= MDOC_PPHRASE;
1.46      schwarze 1124:                        if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
                   1125:                                m->flags |= MDOC_PPHRASE;
                   1126:
                   1127:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps 1128:                                return(0);
1.46      schwarze 1129:
1.41      schwarze 1130:                        m->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1131:                        continue;
                   1132:                }
                   1133:
1.36      schwarze 1134:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1135:
                   1136:                if (MDOC_MAX == ntok) {
1.66      schwarze 1137:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1138:                                return(0);
1.36      schwarze 1139:                        continue;
1.32      schwarze 1140:                }
1.36      schwarze 1141:
                   1142:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1143:                        return(0);
1.36      schwarze 1144:                break;
1.32      schwarze 1145:        }
1.1       kristaps 1146:
1.32      schwarze 1147:        if (NULL == head) {
                   1148:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1149:                        return(0);
1.32      schwarze 1150:                head = m->last;
1.1       kristaps 1151:        }
                   1152:
1.43      schwarze 1153:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1154:                return(0);
1.12      schwarze 1155:
1.34      schwarze 1156:        /* If we've already opened our body, exit now. */
1.32      schwarze 1157:
1.34      schwarze 1158:        if (NULL != body)
1.46      schwarze 1159:                goto out;
1.29      schwarze 1160:
                   1161:        /*
1.34      schwarze 1162:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1163:         * explicit close-out, postpone switching the current block from
                   1164:         * head to body until the rew_sub() call closing out that
                   1165:         * sub-block.
1.29      schwarze 1166:         */
                   1167:        for (n = m->last; n && n != head; n = n->parent) {
1.34      schwarze 1168:                if (MDOC_BLOCK == n->type &&
                   1169:                                MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1170:                                ! (MDOC_VALID & n->flags)) {
1.29      schwarze 1171:                        n->pending = head;
                   1172:                        return(1);
                   1173:                }
                   1174:        }
1.54      schwarze 1175:
1.32      schwarze 1176:        /* Close out scopes to remain in a consistent state. */
1.12      schwarze 1177:
1.22      schwarze 1178:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1179:                return(0);
1.22      schwarze 1180:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1181:                return(0);
                   1182:
1.46      schwarze 1183: out:
                   1184:        if ( ! (MDOC_FREECOL & m->flags))
                   1185:                return(1);
                   1186:
                   1187:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1188:                return(0);
                   1189:        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                   1190:                return(0);
                   1191:
                   1192:        m->flags &= ~MDOC_FREECOL;
1.1       kristaps 1193:        return(1);
                   1194: }
                   1195:
                   1196:
                   1197: static int
                   1198: blk_part_imp(MACRO_PROT_ARGS)
                   1199: {
1.43      schwarze 1200:        int               la, nl;
1.36      schwarze 1201:        enum mdoct        ntok;
                   1202:        enum margserr     ac;
1.1       kristaps 1203:        char             *p;
1.32      schwarze 1204:        struct mdoc_node *blk; /* saved block context */
                   1205:        struct mdoc_node *body; /* saved body context */
                   1206:        struct mdoc_node *n;
1.1       kristaps 1207:
1.43      schwarze 1208:        nl = MDOC_NEWLINE & m->flags;
                   1209:
1.32      schwarze 1210:        /*
                   1211:         * A macro that spans to the end of the line.  This is generally
                   1212:         * (but not necessarily) called as the first macro.  The block
                   1213:         * has a head as the immediate child, which is always empty,
                   1214:         * followed by zero or more opening punctuation nodes, then the
                   1215:         * body (which may be empty, depending on the macro), then zero
                   1216:         * or more closing punctuation nodes.
                   1217:         */
1.22      schwarze 1218:
                   1219:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1220:                return(0);
1.32      schwarze 1221:
1.22      schwarze 1222:        blk = m->last;
1.32      schwarze 1223:
1.22      schwarze 1224:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1225:                return(0);
1.22      schwarze 1226:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1227:                return(0);
                   1228:
1.32      schwarze 1229:        /*
                   1230:         * Open the body scope "on-demand", that is, after we've
                   1231:         * processed all our the leading delimiters (open parenthesis,
                   1232:         * etc.).
                   1233:         */
1.1       kristaps 1234:
1.32      schwarze 1235:        for (body = NULL; ; ) {
1.22      schwarze 1236:                la = *pos;
1.36      schwarze 1237:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.32      schwarze 1238:
1.36      schwarze 1239:                if (ARGS_ERROR == ac)
1.1       kristaps 1240:                        return(0);
1.36      schwarze 1241:                if (ARGS_EOLN == ac)
1.32      schwarze 1242:                        break;
1.36      schwarze 1243:                if (ARGS_PUNCT == ac)
1.1       kristaps 1244:                        break;
                   1245:
1.36      schwarze 1246:                if (NULL == body && ARGS_QWORD != ac &&
1.66      schwarze 1247:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1248:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.1       kristaps 1249:                                return(0);
                   1250:                        continue;
                   1251:                }
                   1252:
1.32      schwarze 1253:                if (NULL == body) {
                   1254:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1255:                               return(0);
                   1256:                        body = m->last;
                   1257:                }
                   1258:
1.36      schwarze 1259:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1260:
                   1261:                if (MDOC_MAX == ntok) {
1.66      schwarze 1262:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1263:                                return(0);
1.36      schwarze 1264:                        continue;
                   1265:                }
1.32      schwarze 1266:
1.36      schwarze 1267:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1268:                        return(0);
1.36      schwarze 1269:                break;
1.32      schwarze 1270:        }
                   1271:
                   1272:        /* Clean-ups to leave in a consistent state. */
                   1273:
                   1274:        if (NULL == body) {
                   1275:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1276:                        return(0);
1.32      schwarze 1277:                body = m->last;
1.39      schwarze 1278:        }
                   1279:
                   1280:        for (n = body->child; n && n->next; n = n->next)
                   1281:                /* Do nothing. */ ;
                   1282:
                   1283:        /*
                   1284:         * End of sentence spacing: if the last node is a text node and
                   1285:         * has a trailing period, then mark it as being end-of-sentence.
                   1286:         */
                   1287:
                   1288:        if (n && MDOC_TEXT == n->type && n->string)
1.55      schwarze 1289:                if (mandoc_eos(n->string, strlen(n->string), 1))
1.39      schwarze 1290:                        n->flags |= MDOC_EOS;
                   1291:
1.68    ! schwarze 1292:        /* Up-propagate the end-of-space flag. */
1.39      schwarze 1293:
                   1294:        if (n && (MDOC_EOS & n->flags)) {
                   1295:                body->flags |= MDOC_EOS;
                   1296:                body->parent->flags |= MDOC_EOS;
1.1       kristaps 1297:        }
                   1298:
1.47      schwarze 1299:        /*
                   1300:         * If there is an open sub-block requiring explicit close-out,
                   1301:         * postpone closing out the current block
                   1302:         * until the rew_sub() call closing out the sub-block.
                   1303:         */
                   1304:        for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
                   1305:                if (MDOC_BLOCK == n->type &&
                   1306:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1307:                    ! (MDOC_VALID & n->flags)) {
1.59      schwarze 1308:                        make_pending(n, tok, m, line, ppos);
1.47      schwarze 1309:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                   1310:                            tok, body, ENDBODY_NOSPACE))
                   1311:                                return(0);
                   1312:                        return(1);
                   1313:                }
                   1314:        }
                   1315:
1.22      schwarze 1316:        /*
                   1317:         * If we can't rewind to our body, then our scope has already
                   1318:         * been closed by another macro (like `Oc' closing `Op').  This
                   1319:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.34      schwarze 1320:         * crufty use of `Op' breakage.
1.1       kristaps 1321:         */
1.65      schwarze 1322:        if (n != body)
1.66      schwarze 1323:                mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
1.65      schwarze 1324:                                "%s broken", mdoc_macronames[tok]);
1.32      schwarze 1325:
1.47      schwarze 1326:        if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      schwarze 1327:                return(0);
1.1       kristaps 1328:
1.22      schwarze 1329:        /* Standard appending of delimiters. */
1.1       kristaps 1330:
1.43      schwarze 1331:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1332:                return(0);
                   1333:
1.22      schwarze 1334:        /* Rewind scope, if applicable. */
1.1       kristaps 1335:
1.47      schwarze 1336:        if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1337:                return(0);
                   1338:
                   1339:        return(1);
                   1340: }
                   1341:
                   1342:
                   1343: static int
                   1344: blk_part_exp(MACRO_PROT_ARGS)
                   1345: {
1.37      schwarze 1346:        int               la, nl;
1.36      schwarze 1347:        enum margserr     ac;
1.32      schwarze 1348:        struct mdoc_node *head; /* keep track of head */
                   1349:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1350:        char             *p;
1.36      schwarze 1351:        enum mdoct        ntok;
1.1       kristaps 1352:
1.37      schwarze 1353:        nl = MDOC_NEWLINE & m->flags;
                   1354:
1.32      schwarze 1355:        /*
                   1356:         * The opening of an explicit macro having zero or more leading
                   1357:         * punctuation nodes; a head with optional single element (the
                   1358:         * case of `Eo'); and a body that may be empty.
                   1359:         */
1.22      schwarze 1360:
                   1361:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1362:                return(0);
1.22      schwarze 1363:
1.32      schwarze 1364:        for (head = body = NULL; ; ) {
1.22      schwarze 1365:                la = *pos;
1.36      schwarze 1366:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1367:
1.36      schwarze 1368:                if (ARGS_ERROR == ac)
1.1       kristaps 1369:                        return(0);
1.36      schwarze 1370:                if (ARGS_PUNCT == ac)
1.1       kristaps 1371:                        break;
1.36      schwarze 1372:                if (ARGS_EOLN == ac)
1.1       kristaps 1373:                        break;
                   1374:
1.32      schwarze 1375:                /* Flush out leading punctuation. */
                   1376:
1.36      schwarze 1377:                if (NULL == head && ARGS_QWORD != ac &&
1.66      schwarze 1378:                                DELIM_OPEN == mdoc_isdelim(p)) {
1.32      schwarze 1379:                        assert(NULL == body);
1.66      schwarze 1380:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1381:                                return(0);
                   1382:                        continue;
                   1383:                }
                   1384:
                   1385:                if (NULL == head) {
                   1386:                        assert(NULL == body);
                   1387:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1388:                                return(0);
1.32      schwarze 1389:                        head = m->last;
1.1       kristaps 1390:                }
                   1391:
1.32      schwarze 1392:                /*
                   1393:                 * `Eo' gobbles any data into the head, but most other
                   1394:                 * macros just immediately close out and begin the body.
                   1395:                 */
                   1396:
                   1397:                if (NULL == body) {
                   1398:                        assert(head);
                   1399:                        /* No check whether it's a macro! */
                   1400:                        if (MDOC_Eo == tok)
1.66      schwarze 1401:                                if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1402:                                        return(0);
                   1403:
1.22      schwarze 1404:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1405:                                return(0);
1.22      schwarze 1406:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1407:                                return(0);
1.32      schwarze 1408:                        body = m->last;
                   1409:
                   1410:                        if (MDOC_Eo == tok)
                   1411:                                continue;
                   1412:                }
                   1413:
                   1414:                assert(NULL != head && NULL != body);
                   1415:
1.36      schwarze 1416:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1417:
                   1418:                if (MDOC_MAX == ntok) {
1.66      schwarze 1419:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.32      schwarze 1420:                                return(0);
1.36      schwarze 1421:                        continue;
1.1       kristaps 1422:                }
1.32      schwarze 1423:
1.36      schwarze 1424:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1425:                        return(0);
1.36      schwarze 1426:                break;
1.1       kristaps 1427:        }
                   1428:
1.32      schwarze 1429:        /* Clean-up to leave in a consistent state. */
                   1430:
                   1431:        if (NULL == head) {
                   1432:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1433:                        return(0);
                   1434:                head = m->last;
                   1435:        }
1.22      schwarze 1436:
1.32      schwarze 1437:        if (NULL == body) {
1.22      schwarze 1438:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1439:                        return(0);
1.22      schwarze 1440:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1441:                        return(0);
1.32      schwarze 1442:                body = m->last;
1.1       kristaps 1443:        }
                   1444:
1.22      schwarze 1445:        /* Standard appending of delimiters. */
                   1446:
1.37      schwarze 1447:        if ( ! nl)
1.1       kristaps 1448:                return(1);
1.22      schwarze 1449:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1450: }
                   1451:
                   1452:
1.37      schwarze 1453: /* ARGSUSED */
1.1       kristaps 1454: static int
                   1455: in_line_argn(MACRO_PROT_ARGS)
                   1456: {
1.37      schwarze 1457:        int              la, flushed, j, maxargs, nl;
1.36      schwarze 1458:        enum margserr    ac;
                   1459:        enum margverr    av;
                   1460:        struct mdoc_arg *arg;
                   1461:        char            *p;
                   1462:        enum mdoct       ntok;
1.1       kristaps 1463:
1.37      schwarze 1464:        nl = MDOC_NEWLINE & m->flags;
                   1465:
1.32      schwarze 1466:        /*
                   1467:         * A line macro that has a fixed number of arguments (maxargs).
                   1468:         * Only open the scope once the first non-leading-punctuation is
                   1469:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1470:         * keep it open until the maximum number of arguments are
                   1471:         * exhausted.
                   1472:         */
1.1       kristaps 1473:
                   1474:        switch (tok) {
                   1475:        case (MDOC_Ap):
                   1476:                /* FALLTHROUGH */
                   1477:        case (MDOC_No):
                   1478:                /* FALLTHROUGH */
                   1479:        case (MDOC_Ns):
                   1480:                /* FALLTHROUGH */
                   1481:        case (MDOC_Ux):
                   1482:                maxargs = 0;
                   1483:                break;
1.64      schwarze 1484:        case (MDOC_Bx):
                   1485:                /* FALLTHROUGH */
1.28      schwarze 1486:        case (MDOC_Xr):
                   1487:                maxargs = 2;
                   1488:                break;
1.1       kristaps 1489:        default:
                   1490:                maxargs = 1;
                   1491:                break;
                   1492:        }
                   1493:
1.32      schwarze 1494:        for (arg = NULL; ; ) {
1.22      schwarze 1495:                la = *pos;
1.36      schwarze 1496:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1497:
1.36      schwarze 1498:                if (ARGV_WORD == av) {
1.22      schwarze 1499:                        *pos = la;
1.1       kristaps 1500:                        break;
                   1501:                }
                   1502:
1.36      schwarze 1503:                if (ARGV_EOLN == av)
1.1       kristaps 1504:                        break;
1.36      schwarze 1505:                if (ARGV_ARG == av)
1.1       kristaps 1506:                        continue;
                   1507:
                   1508:                mdoc_argv_free(arg);
                   1509:                return(0);
                   1510:        }
                   1511:
1.32      schwarze 1512:        for (flushed = j = 0; ; ) {
1.22      schwarze 1513:                la = *pos;
1.36      schwarze 1514:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1515:
1.36      schwarze 1516:                if (ARGS_ERROR == ac)
1.1       kristaps 1517:                        return(0);
1.36      schwarze 1518:                if (ARGS_PUNCT == ac)
1.1       kristaps 1519:                        break;
1.36      schwarze 1520:                if (ARGS_EOLN == ac)
1.1       kristaps 1521:                        break;
                   1522:
1.32      schwarze 1523:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.66      schwarze 1524:                                ARGS_QWORD != ac && 0 == j &&
                   1525:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1526:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.32      schwarze 1527:                                return(0);
                   1528:                        continue;
1.40      schwarze 1529:                } else if (0 == j)
1.32      schwarze 1530:                       if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1531:                               return(0);
                   1532:
                   1533:                if (j == maxargs && ! flushed) {
                   1534:                        if ( ! rew_elem(m, tok))
                   1535:                                return(0);
                   1536:                        flushed = 1;
                   1537:                }
                   1538:
1.36      schwarze 1539:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1540:
                   1541:                if (MDOC_MAX != ntok) {
1.22      schwarze 1542:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1543:                                return(0);
                   1544:                        flushed = 1;
1.36      schwarze 1545:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1546:                                return(0);
1.32      schwarze 1547:                        j++;
1.1       kristaps 1548:                        break;
                   1549:                }
                   1550:
                   1551:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.36      schwarze 1552:                                ARGS_QWORD != ac &&
1.40      schwarze 1553:                                ! flushed &&
1.66      schwarze 1554:                                DELIM_NONE != mdoc_isdelim(p)) {
1.22      schwarze 1555:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1556:                                return(0);
                   1557:                        flushed = 1;
                   1558:                }
1.28      schwarze 1559:
                   1560:                /*
                   1561:                 * XXX: this is a hack to work around groff's ugliness
                   1562:                 * as regards `Xr' and extraneous arguments.  It should
                   1563:                 * ideally be deprecated behaviour, but because this is
                   1564:                 * code is no here, it's unlikely to be removed.
                   1565:                 */
                   1566:                if (MDOC_Xr == tok && j == maxargs) {
1.32      schwarze 1567:                        if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1.28      schwarze 1568:                                return(0);
                   1569:                        if ( ! rew_elem(m, MDOC_Ns))
                   1570:                                return(0);
                   1571:                }
                   1572:
1.66      schwarze 1573:                if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1574:                        return(0);
1.32      schwarze 1575:                j++;
1.1       kristaps 1576:        }
                   1577:
1.32      schwarze 1578:        if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1579:               return(0);
                   1580:
                   1581:        /* Close out in a consistent state. */
1.22      schwarze 1582:
                   1583:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1584:                return(0);
1.37      schwarze 1585:        if ( ! nl)
1.1       kristaps 1586:                return(1);
1.22      schwarze 1587:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1588: }
                   1589:
                   1590:
                   1591: static int
                   1592: in_line_eoln(MACRO_PROT_ARGS)
                   1593: {
1.36      schwarze 1594:        int              la;
                   1595:        enum margserr    ac;
                   1596:        enum margverr    av;
                   1597:        struct mdoc_arg *arg;
                   1598:        char            *p;
                   1599:        enum mdoct       ntok;
1.1       kristaps 1600:
                   1601:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1602:
1.53      schwarze 1603:        if (tok == MDOC_Pp)
                   1604:                rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos);
                   1605:
1.22      schwarze 1606:        /* Parse macro arguments. */
1.1       kristaps 1607:
1.22      schwarze 1608:        for (arg = NULL; ; ) {
1.1       kristaps 1609:                la = *pos;
1.36      schwarze 1610:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1611:
1.36      schwarze 1612:                if (ARGV_WORD == av) {
1.1       kristaps 1613:                        *pos = la;
                   1614:                        break;
                   1615:                }
1.36      schwarze 1616:                if (ARGV_EOLN == av)
1.1       kristaps 1617:                        break;
1.36      schwarze 1618:                if (ARGV_ARG == av)
1.1       kristaps 1619:                        continue;
                   1620:
                   1621:                mdoc_argv_free(arg);
                   1622:                return(0);
                   1623:        }
                   1624:
1.22      schwarze 1625:        /* Open element scope. */
                   1626:
                   1627:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1628:                return(0);
                   1629:
1.22      schwarze 1630:        /* Parse argument terms. */
1.1       kristaps 1631:
                   1632:        for (;;) {
                   1633:                la = *pos;
1.36      schwarze 1634:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1635:
1.36      schwarze 1636:                if (ARGS_ERROR == ac)
1.1       kristaps 1637:                        return(0);
1.36      schwarze 1638:                if (ARGS_EOLN == ac)
1.1       kristaps 1639:                        break;
                   1640:
1.36      schwarze 1641:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1642:
1.36      schwarze 1643:                if (MDOC_MAX == ntok) {
1.66      schwarze 1644:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1645:                                return(0);
1.36      schwarze 1646:                        continue;
                   1647:                }
1.1       kristaps 1648:
1.36      schwarze 1649:                if ( ! rew_elem(m, tok))
1.1       kristaps 1650:                        return(0);
1.36      schwarze 1651:                return(mdoc_macro(m, ntok, line, la, pos, buf));
1.1       kristaps 1652:        }
                   1653:
1.22      schwarze 1654:        /* Close out (no delimiters). */
                   1655:
                   1656:        return(rew_elem(m, tok));
1.28      schwarze 1657: }
                   1658:
                   1659:
                   1660: /* ARGSUSED */
                   1661: static int
                   1662: ctx_synopsis(MACRO_PROT_ARGS)
                   1663: {
1.37      schwarze 1664:        int              nl;
                   1665:
                   1666:        nl = MDOC_NEWLINE & m->flags;
1.28      schwarze 1667:
                   1668:        /* If we're not in the SYNOPSIS, go straight to in-line. */
1.53      schwarze 1669:        if ( ! (MDOC_SYNOPSIS & m->flags))
1.28      schwarze 1670:                return(in_line(m, tok, line, ppos, pos, buf));
                   1671:
                   1672:        /* If we're a nested call, same place. */
1.37      schwarze 1673:        if ( ! nl)
1.28      schwarze 1674:                return(in_line(m, tok, line, ppos, pos, buf));
                   1675:
                   1676:        /*
                   1677:         * XXX: this will open a block scope; however, if later we end
                   1678:         * up formatting the block scope, then child nodes will inherit
                   1679:         * the formatting.  Be careful.
                   1680:         */
1.51      schwarze 1681:        if (MDOC_Nm == tok)
                   1682:                return(blk_full(m, tok, line, ppos, pos, buf));
                   1683:        assert(MDOC_Vt == tok);
1.28      schwarze 1684:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
1.1       kristaps 1685: }
                   1686:
                   1687:
                   1688: /* ARGSUSED */
                   1689: static int
                   1690: obsolete(MACRO_PROT_ARGS)
                   1691: {
                   1692:
1.65      schwarze 1693:        mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS);
                   1694:        return(1);
1.1       kristaps 1695: }
                   1696:
                   1697:
1.17      schwarze 1698: /*
                   1699:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1700:  * They're unusual because they're basically free-form text until a
                   1701:  * macro is encountered.
                   1702:  */
1.1       kristaps 1703: static int
1.46      schwarze 1704: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1705: {
1.36      schwarze 1706:        int              la, pos;
1.46      schwarze 1707:        enum margserr    ac;
1.36      schwarze 1708:        enum mdoct       ntok;
                   1709:        char            *p;
1.1       kristaps 1710:
1.17      schwarze 1711:        for (pos = ppos; ; ) {
                   1712:                la = pos;
1.1       kristaps 1713:
1.68    ! schwarze 1714:                ac = mdoc_zargs(m, line, &pos, buf, &p);
1.1       kristaps 1715:
1.46      schwarze 1716:                if (ARGS_ERROR == ac)
1.17      schwarze 1717:                        return(0);
1.46      schwarze 1718:                if (ARGS_EOLN == ac)
1.17      schwarze 1719:                        break;
1.1       kristaps 1720:
1.46      schwarze 1721:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1722:
1.36      schwarze 1723:                if (MDOC_MAX == ntok) {
1.66      schwarze 1724:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1725:                                return(0);
1.36      schwarze 1726:                        continue;
                   1727:                }
1.1       kristaps 1728:
1.36      schwarze 1729:                if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1       kristaps 1730:                        return(0);
1.36      schwarze 1731:                return(append_delims(m, line, &pos, buf));
1.1       kristaps 1732:        }
                   1733:
                   1734:        return(1);
                   1735: }
1.17      schwarze 1736:
                   1737:
1.46      schwarze 1738: /* ARGSUSED */
                   1739: static int
                   1740: phrase_ta(MACRO_PROT_ARGS)
                   1741: {
                   1742:        int               la;
                   1743:        enum mdoct        ntok;
                   1744:        enum margserr     ac;
                   1745:        char             *p;
                   1746:
                   1747:        /*
                   1748:         * FIXME: this is overly restrictive: if the `Ta' is unexpected,
                   1749:         * it should simply error out with ARGSLOST.
                   1750:         */
                   1751:
                   1752:        if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
                   1753:                return(0);
                   1754:        if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
                   1755:                return(0);
                   1756:
                   1757:        for (;;) {
                   1758:                la = *pos;
1.68    ! schwarze 1759:                ac = mdoc_zargs(m, line, pos, buf, &p);
1.46      schwarze 1760:
                   1761:                if (ARGS_ERROR == ac)
                   1762:                        return(0);
                   1763:                if (ARGS_EOLN == ac)
                   1764:                        break;
                   1765:
                   1766:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
                   1767:
                   1768:                if (MDOC_MAX == ntok) {
1.66      schwarze 1769:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.46      schwarze 1770:                                return(0);
                   1771:                        continue;
                   1772:                }
                   1773:
                   1774:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
                   1775:                        return(0);
                   1776:                return(append_delims(m, line, pos, buf));
                   1777:        }
                   1778:
                   1779:        return(1);
                   1780: }