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

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