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

1.65    ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.64 2011/01/30 18:28:01 schwarze Exp $ */
1.1       kristaps    2: /*
1.64      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.54      schwarze    4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.2       schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
                     18: #include <assert.h>
                     19: #include <ctype.h>
                     20: #include <stdlib.h>
                     21: #include <stdio.h>
                     22: #include <string.h>
1.26      schwarze   23: #include <time.h>
1.1       kristaps   24:
1.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.65    ! schwarze  598:                assert(DELIM_NONE != mandoc_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.65    ! schwarze  709:                        mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST);
1.14      schwarze  710:
1.22      schwarze  711:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.14      schwarze  712:                        return(0);
1.22      schwarze  713:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  714:        }
                    715:
1.22      schwarze  716:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  717:                return(0);
                    718:
1.47      schwarze  719:        if (NULL == later && maxargs > 0)
1.22      schwarze  720:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  721:                        return(0);
                    722:
1.20      schwarze  723:        for (flushed = j = 0; ; j++) {
1.1       kristaps  724:                lastarg = *pos;
                    725:
                    726:                if (j == maxargs && ! flushed) {
1.22      schwarze  727:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  728:                                return(0);
                    729:                        flushed = 1;
                    730:                }
                    731:
1.36      schwarze  732:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  733:
1.36      schwarze  734:                if (ARGS_ERROR == ac)
1.1       kristaps  735:                        return(0);
1.36      schwarze  736:                if (ARGS_PUNCT == ac)
1.1       kristaps  737:                        break;
1.36      schwarze  738:                if (ARGS_EOLN == ac)
1.1       kristaps  739:                        break;
                    740:
1.36      schwarze  741:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                    742:
                    743:                if (MDOC_MAX == ntok) {
                    744:                        if ( ! mdoc_word_alloc(m, line, lastarg, p))
1.1       kristaps  745:                                return(0);
1.36      schwarze  746:                        continue;
                    747:                }
1.1       kristaps  748:
1.36      schwarze  749:                if ( ! flushed) {
                    750:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                    751:                                return(0);
                    752:                        flushed = 1;
                    753:                }
                    754:                if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
1.1       kristaps  755:                        return(0);
1.36      schwarze  756:                break;
1.1       kristaps  757:        }
                    758:
1.22      schwarze  759:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  760:                return(0);
                    761:
1.37      schwarze  762:        if ( ! nl)
1.1       kristaps  763:                return(1);
1.22      schwarze  764:        return(append_delims(m, line, pos, buf));
1.1       kristaps  765: }
                    766:
                    767:
                    768: static int
                    769: in_line(MACRO_PROT_ARGS)
                    770: {
1.45      schwarze  771:        int              la, scope, cnt, nc, nl;
1.36      schwarze  772:        enum margverr    av;
                    773:        enum mdoct       ntok;
                    774:        enum margserr    ac;
1.40      schwarze  775:        enum mdelim      d;
1.36      schwarze  776:        struct mdoc_arg *arg;
                    777:        char            *p;
1.1       kristaps  778:
1.37      schwarze  779:        nl = MDOC_NEWLINE & m->flags;
                    780:
1.1       kristaps  781:        /*
                    782:         * Whether we allow ignored elements (those without content,
                    783:         * usually because of reserved words) to squeak by.
                    784:         */
1.32      schwarze  785:
1.1       kristaps  786:        switch (tok) {
1.19      schwarze  787:        case (MDOC_An):
                    788:                /* FALLTHROUGH */
                    789:        case (MDOC_Ar):
1.1       kristaps  790:                /* FALLTHROUGH */
                    791:        case (MDOC_Fl):
                    792:                /* FALLTHROUGH */
1.60      schwarze  793:        case (MDOC_Mt):
1.18      schwarze  794:                /* FALLTHROUGH */
1.19      schwarze  795:        case (MDOC_Nm):
                    796:                /* FALLTHROUGH */
1.18      schwarze  797:        case (MDOC_Pa):
1.1       kristaps  798:                nc = 1;
                    799:                break;
                    800:        default:
                    801:                nc = 0;
                    802:                break;
                    803:        }
                    804:
1.20      schwarze  805:        for (arg = NULL;; ) {
1.1       kristaps  806:                la = *pos;
1.36      schwarze  807:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  808:
1.36      schwarze  809:                if (ARGV_WORD == av) {
1.1       kristaps  810:                        *pos = la;
                    811:                        break;
                    812:                }
1.36      schwarze  813:                if (ARGV_EOLN == av)
1.1       kristaps  814:                        break;
1.36      schwarze  815:                if (ARGV_ARG == av)
1.1       kristaps  816:                        continue;
                    817:
                    818:                mdoc_argv_free(arg);
                    819:                return(0);
                    820:        }
                    821:
1.45      schwarze  822:        for (cnt = scope = 0;; ) {
1.1       kristaps  823:                la = *pos;
1.36      schwarze  824:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  825:
1.36      schwarze  826:                if (ARGS_ERROR == ac)
1.1       kristaps  827:                        return(0);
1.36      schwarze  828:                if (ARGS_EOLN == ac)
1.1       kristaps  829:                        break;
1.36      schwarze  830:                if (ARGS_PUNCT == ac)
1.1       kristaps  831:                        break;
                    832:
1.36      schwarze  833:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  834:
                    835:                /*
                    836:                 * In this case, we've located a submacro and must
                    837:                 * execute it.  Close out scope, if open.  If no
                    838:                 * elements have been generated, either create one (nc)
                    839:                 * or raise a warning.
                    840:                 */
                    841:
1.36      schwarze  842:                if (MDOC_MAX != ntok) {
1.45      schwarze  843:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  844:                                return(0);
                    845:                        if (nc && 0 == cnt) {
1.22      schwarze  846:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  847:                                        return(0);
1.22      schwarze  848:                                if ( ! rew_last(m, m->last))
1.3       schwarze  849:                                        return(0);
1.1       kristaps  850:                        } else if ( ! nc && 0 == cnt) {
                    851:                                mdoc_argv_free(arg);
1.65    ! schwarze  852:                                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  853:                        }
1.65    ! schwarze  854:
1.36      schwarze  855:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps  856:                                return(0);
1.37      schwarze  857:                        if ( ! nl)
1.1       kristaps  858:                                return(1);
1.22      schwarze  859:                        return(append_delims(m, line, pos, buf));
1.21      schwarze  860:                }
1.1       kristaps  861:
                    862:                /*
                    863:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    864:                 * a word; rewind the scope, if a delimiter; then append
                    865:                 * the word.
                    866:                 */
                    867:
1.65    ! schwarze  868:                d = ARGS_QWORD == ac ? DELIM_NONE : mandoc_isdelim(p);
1.1       kristaps  869:
1.45      schwarze  870:                if (DELIM_NONE != d) {
                    871:                        /*
                    872:                         * If we encounter closing punctuation, no word
                    873:                         * has been omitted, no scope is open, and we're
                    874:                         * allowed to have an empty element, then start
                    875:                         * a new scope.  `Ar', `Fl', and `Li', only do
                    876:                         * this once per invocation.  There may be more
                    877:                         * of these (all of them?).
                    878:                         */
                    879:                        if (0 == cnt && (nc || MDOC_Li == tok) &&
                    880:                                        DELIM_CLOSE == d && ! scope) {
                    881:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
                    882:                                        return(0);
                    883:                                if (MDOC_Ar == tok || MDOC_Li == tok ||
                    884:                                                MDOC_Fl == tok)
                    885:                                        cnt++;
                    886:                                scope = 1;
                    887:                        }
                    888:                        /*
                    889:                         * Close out our scope, if one is open, before
                    890:                         * any punctuation.
                    891:                         */
                    892:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  893:                                return(0);
1.45      schwarze  894:                        scope = 0;
                    895:                } else if ( ! scope) {
1.22      schwarze  896:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  897:                                return(0);
1.45      schwarze  898:                        scope = 1;
1.1       kristaps  899:                }
                    900:
1.40      schwarze  901:                if (DELIM_NONE == d)
1.1       kristaps  902:                        cnt++;
1.22      schwarze  903:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps  904:                        return(0);
1.27      schwarze  905:
                    906:                /*
                    907:                 * `Fl' macros have their scope re-opened with each new
                    908:                 * word so that the `-' can be added to each one without
                    909:                 * having to parse out spaces.
                    910:                 */
1.45      schwarze  911:                if (scope && MDOC_Fl == tok) {
1.27      schwarze  912:                        if ( ! rew_elem(m, tok))
                    913:                                return(0);
1.45      schwarze  914:                        scope = 0;
1.27      schwarze  915:                }
1.1       kristaps  916:        }
                    917:
1.45      schwarze  918:        if (scope && ! rew_elem(m, tok))
1.1       kristaps  919:                return(0);
                    920:
                    921:        /*
                    922:         * If no elements have been collected and we're allowed to have
                    923:         * empties (nc), open a scope and close it out.  Otherwise,
                    924:         * raise a warning.
                    925:         */
1.32      schwarze  926:
1.1       kristaps  927:        if (nc && 0 == cnt) {
1.22      schwarze  928:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  929:                        return(0);
1.22      schwarze  930:                if ( ! rew_last(m, m->last))
1.3       schwarze  931:                        return(0);
                    932:        } else if ( ! nc && 0 == cnt) {
1.1       kristaps  933:                mdoc_argv_free(arg);
1.65    ! schwarze  934:                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.1       kristaps  935:        }
                    936:
1.37      schwarze  937:        if ( ! nl)
1.1       kristaps  938:                return(1);
1.22      schwarze  939:        return(append_delims(m, line, pos, buf));
1.1       kristaps  940: }
                    941:
                    942:
                    943: static int
                    944: blk_full(MACRO_PROT_ARGS)
                    945: {
1.43      schwarze  946:        int               la, nl;
1.1       kristaps  947:        struct mdoc_arg  *arg;
1.32      schwarze  948:        struct mdoc_node *head; /* save of head macro */
1.34      schwarze  949:        struct mdoc_node *body; /* save of body macro */
1.32      schwarze  950:        struct mdoc_node *n;
1.46      schwarze  951:        enum mdoc_type    mtt;
1.36      schwarze  952:        enum mdoct        ntok;
1.37      schwarze  953:        enum margserr     ac, lac;
1.36      schwarze  954:        enum margverr     av;
1.1       kristaps  955:        char             *p;
                    956:
1.43      schwarze  957:        nl = MDOC_NEWLINE & m->flags;
                    958:
1.32      schwarze  959:        /* Close out prior implicit scope. */
1.12      schwarze  960:
1.1       kristaps  961:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.22      schwarze  962:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  963:                        return(0);
1.22      schwarze  964:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  965:                        return(0);
                    966:        }
                    967:
1.32      schwarze  968:        /*
                    969:         * This routine accomodates implicitly- and explicitly-scoped
                    970:         * macro openings.  Implicit ones first close out prior scope
                    971:         * (seen above).  Delay opening the head until necessary to
                    972:         * allow leading punctuation to print.  Special consideration
                    973:         * for `It -column', which has phrase-part syntax instead of
                    974:         * regular child nodes.
                    975:         */
                    976:
1.1       kristaps  977:        for (arg = NULL;; ) {
1.32      schwarze  978:                la = *pos;
1.36      schwarze  979:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  980:
1.36      schwarze  981:                if (ARGV_WORD == av) {
1.32      schwarze  982:                        *pos = la;
1.1       kristaps  983:                        break;
                    984:                }
                    985:
1.36      schwarze  986:                if (ARGV_EOLN == av)
1.1       kristaps  987:                        break;
1.36      schwarze  988:                if (ARGV_ARG == av)
1.1       kristaps  989:                        continue;
                    990:
                    991:                mdoc_argv_free(arg);
                    992:                return(0);
                    993:        }
                    994:
1.22      schwarze  995:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps  996:                return(0);
                    997:
1.34      schwarze  998:        head = body = NULL;
1.1       kristaps  999:
1.32      schwarze 1000:        /*
                   1001:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1002:         * of block partial-explicit and full-implicit.  Stupid.
                   1003:         */
1.12      schwarze 1004:
1.32      schwarze 1005:        if (MDOC_Nd == tok) {
                   1006:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1007:                        return(0);
                   1008:                head = m->last;
1.22      schwarze 1009:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.12      schwarze 1010:                        return(0);
1.22      schwarze 1011:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.12      schwarze 1012:                        return(0);
1.34      schwarze 1013:                body = m->last;
1.12      schwarze 1014:        }
                   1015:
1.37      schwarze 1016:        ac = ARGS_ERROR;
                   1017:
1.41      schwarze 1018:        for ( ; ; ) {
1.32      schwarze 1019:                la = *pos;
1.46      schwarze 1020:                /* Initialise last-phrase-type with ARGS_PEND. */
                   1021:                lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.36      schwarze 1022:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.52      schwarze 1023:
                   1024:                if (ARGS_PUNCT == ac)
                   1025:                        break;
1.1       kristaps 1026:
1.36      schwarze 1027:                if (ARGS_ERROR == ac)
1.1       kristaps 1028:                        return(0);
1.46      schwarze 1029:
                   1030:                if (ARGS_EOLN == ac) {
                   1031:                        if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
                   1032:                                break;
                   1033:                        /*
                   1034:                         * This is necessary: if the last token on a
                   1035:                         * line is a `Ta' or tab, then we'll get
                   1036:                         * ARGS_EOLN, so we must be smart enough to
                   1037:                         * reopen our scope if the last parse was a
                   1038:                         * phrase or partial phrase.
                   1039:                         */
                   1040:                        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1041:                                return(0);
                   1042:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1043:                                return(0);
                   1044:                        body = m->last;
1.1       kristaps 1045:                        break;
1.41      schwarze 1046:                }
                   1047:
1.46      schwarze 1048:                /*
                   1049:                 * Emit leading punctuation (i.e., punctuation before
                   1050:                 * the MDOC_HEAD) for non-phrase types.
                   1051:                 */
1.32      schwarze 1052:
1.37      schwarze 1053:                if (NULL == head &&
1.46      schwarze 1054:                                ARGS_PEND != ac &&
1.37      schwarze 1055:                                ARGS_PHRASE != ac &&
1.36      schwarze 1056:                                ARGS_PPHRASE != ac &&
                   1057:                                ARGS_QWORD != ac &&
1.65    ! schwarze 1058:                                DELIM_OPEN == mandoc_isdelim(p)) {
1.32      schwarze 1059:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1060:                                return(0);
                   1061:                        continue;
                   1062:                }
                   1063:
1.46      schwarze 1064:                /* Open a head if one hasn't been opened. */
1.32      schwarze 1065:
1.46      schwarze 1066:                if (NULL == head) {
1.32      schwarze 1067:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1068:                                return(0);
1.29      schwarze 1069:                        head = m->last;
1.32      schwarze 1070:                }
                   1071:
1.46      schwarze 1072:                if (ARGS_PHRASE == ac ||
                   1073:                                ARGS_PEND == ac ||
                   1074:                                ARGS_PPHRASE == ac) {
                   1075:                        /*
                   1076:                         * If we haven't opened a body yet, rewind the
                   1077:                         * head; if we have, rewind that instead.
                   1078:                         */
                   1079:
                   1080:                        mtt = body ? MDOC_BODY : MDOC_HEAD;
                   1081:                        if ( ! rew_sub(mtt, m, tok, line, ppos))
                   1082:                                return(0);
                   1083:
                   1084:                        /* Then allocate our body context. */
                   1085:
                   1086:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1087:                                return(0);
                   1088:                        body = m->last;
                   1089:
                   1090:                        /*
                   1091:                         * Process phrases: set whether we're in a
                   1092:                         * partial-phrase (this effects line handling)
                   1093:                         * then call down into the phrase parser.
                   1094:                         */
                   1095:
1.41      schwarze 1096:                        if (ARGS_PPHRASE == ac)
                   1097:                                m->flags |= MDOC_PPHRASE;
1.46      schwarze 1098:                        if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
                   1099:                                m->flags |= MDOC_PPHRASE;
                   1100:
                   1101:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps 1102:                                return(0);
1.46      schwarze 1103:
1.41      schwarze 1104:                        m->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1105:                        continue;
                   1106:                }
                   1107:
1.36      schwarze 1108:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1109:
                   1110:                if (MDOC_MAX == ntok) {
                   1111:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1112:                                return(0);
1.36      schwarze 1113:                        continue;
1.32      schwarze 1114:                }
1.36      schwarze 1115:
                   1116:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1117:                        return(0);
1.36      schwarze 1118:                break;
1.32      schwarze 1119:        }
1.1       kristaps 1120:
1.32      schwarze 1121:        if (NULL == head) {
                   1122:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1123:                        return(0);
1.32      schwarze 1124:                head = m->last;
1.1       kristaps 1125:        }
                   1126:
1.43      schwarze 1127:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1128:                return(0);
1.12      schwarze 1129:
1.34      schwarze 1130:        /* If we've already opened our body, exit now. */
1.32      schwarze 1131:
1.34      schwarze 1132:        if (NULL != body)
1.46      schwarze 1133:                goto out;
1.29      schwarze 1134:
                   1135:        /*
1.34      schwarze 1136:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1137:         * explicit close-out, postpone switching the current block from
                   1138:         * head to body until the rew_sub() call closing out that
                   1139:         * sub-block.
1.29      schwarze 1140:         */
                   1141:        for (n = m->last; n && n != head; n = n->parent) {
1.34      schwarze 1142:                if (MDOC_BLOCK == n->type &&
                   1143:                                MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1144:                                ! (MDOC_VALID & n->flags)) {
1.29      schwarze 1145:                        n->pending = head;
                   1146:                        return(1);
                   1147:                }
                   1148:        }
1.54      schwarze 1149:
1.32      schwarze 1150:        /* Close out scopes to remain in a consistent state. */
1.12      schwarze 1151:
1.22      schwarze 1152:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1153:                return(0);
1.22      schwarze 1154:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1155:                return(0);
                   1156:
1.46      schwarze 1157: out:
                   1158:        if ( ! (MDOC_FREECOL & m->flags))
                   1159:                return(1);
                   1160:
                   1161:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1162:                return(0);
                   1163:        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                   1164:                return(0);
                   1165:
                   1166:        m->flags &= ~MDOC_FREECOL;
1.1       kristaps 1167:        return(1);
                   1168: }
                   1169:
                   1170:
                   1171: static int
                   1172: blk_part_imp(MACRO_PROT_ARGS)
                   1173: {
1.43      schwarze 1174:        int               la, nl;
1.36      schwarze 1175:        enum mdoct        ntok;
                   1176:        enum margserr     ac;
1.1       kristaps 1177:        char             *p;
1.32      schwarze 1178:        struct mdoc_node *blk; /* saved block context */
                   1179:        struct mdoc_node *body; /* saved body context */
                   1180:        struct mdoc_node *n;
1.1       kristaps 1181:
1.43      schwarze 1182:        nl = MDOC_NEWLINE & m->flags;
                   1183:
1.32      schwarze 1184:        /*
                   1185:         * A macro that spans to the end of the line.  This is generally
                   1186:         * (but not necessarily) called as the first macro.  The block
                   1187:         * has a head as the immediate child, which is always empty,
                   1188:         * followed by zero or more opening punctuation nodes, then the
                   1189:         * body (which may be empty, depending on the macro), then zero
                   1190:         * or more closing punctuation nodes.
                   1191:         */
1.22      schwarze 1192:
                   1193:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1194:                return(0);
1.32      schwarze 1195:
1.22      schwarze 1196:        blk = m->last;
1.32      schwarze 1197:
1.22      schwarze 1198:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1199:                return(0);
1.22      schwarze 1200:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1201:                return(0);
                   1202:
1.32      schwarze 1203:        /*
                   1204:         * Open the body scope "on-demand", that is, after we've
                   1205:         * processed all our the leading delimiters (open parenthesis,
                   1206:         * etc.).
                   1207:         */
1.1       kristaps 1208:
1.32      schwarze 1209:        for (body = NULL; ; ) {
1.22      schwarze 1210:                la = *pos;
1.36      schwarze 1211:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.32      schwarze 1212:
1.36      schwarze 1213:                if (ARGS_ERROR == ac)
1.1       kristaps 1214:                        return(0);
1.36      schwarze 1215:                if (ARGS_EOLN == ac)
1.32      schwarze 1216:                        break;
1.36      schwarze 1217:                if (ARGS_PUNCT == ac)
1.1       kristaps 1218:                        break;
                   1219:
1.36      schwarze 1220:                if (NULL == body && ARGS_QWORD != ac &&
1.65    ! schwarze 1221:                    DELIM_OPEN == mandoc_isdelim(p)) {
1.22      schwarze 1222:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1223:                                return(0);
                   1224:                        continue;
                   1225:                }
                   1226:
1.32      schwarze 1227:                if (NULL == body) {
                   1228:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1229:                               return(0);
                   1230:                        body = m->last;
                   1231:                }
                   1232:
1.36      schwarze 1233:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1234:
                   1235:                if (MDOC_MAX == ntok) {
                   1236:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.32      schwarze 1237:                                return(0);
1.36      schwarze 1238:                        continue;
                   1239:                }
1.32      schwarze 1240:
1.36      schwarze 1241:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.32      schwarze 1242:                        return(0);
1.36      schwarze 1243:                break;
1.32      schwarze 1244:        }
                   1245:
                   1246:        /* Clean-ups to leave in a consistent state. */
                   1247:
                   1248:        if (NULL == body) {
                   1249:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1250:                        return(0);
1.32      schwarze 1251:                body = m->last;
1.39      schwarze 1252:        }
                   1253:
                   1254:        for (n = body->child; n && n->next; n = n->next)
                   1255:                /* Do nothing. */ ;
                   1256:
                   1257:        /*
                   1258:         * End of sentence spacing: if the last node is a text node and
                   1259:         * has a trailing period, then mark it as being end-of-sentence.
                   1260:         */
                   1261:
                   1262:        if (n && MDOC_TEXT == n->type && n->string)
1.55      schwarze 1263:                if (mandoc_eos(n->string, strlen(n->string), 1))
1.39      schwarze 1264:                        n->flags |= MDOC_EOS;
                   1265:
                   1266:        /* Up-propogate the end-of-space flag. */
                   1267:
                   1268:        if (n && (MDOC_EOS & n->flags)) {
                   1269:                body->flags |= MDOC_EOS;
                   1270:                body->parent->flags |= MDOC_EOS;
1.1       kristaps 1271:        }
                   1272:
1.47      schwarze 1273:        /*
                   1274:         * If there is an open sub-block requiring explicit close-out,
                   1275:         * postpone closing out the current block
                   1276:         * until the rew_sub() call closing out the sub-block.
                   1277:         */
                   1278:        for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
                   1279:                if (MDOC_BLOCK == n->type &&
                   1280:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1281:                    ! (MDOC_VALID & n->flags)) {
1.59      schwarze 1282:                        make_pending(n, tok, m, line, ppos);
1.47      schwarze 1283:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                   1284:                            tok, body, ENDBODY_NOSPACE))
                   1285:                                return(0);
                   1286:                        return(1);
                   1287:                }
                   1288:        }
                   1289:
1.22      schwarze 1290:        /*
                   1291:         * If we can't rewind to our body, then our scope has already
                   1292:         * been closed by another macro (like `Oc' closing `Op').  This
                   1293:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.34      schwarze 1294:         * crufty use of `Op' breakage.
1.1       kristaps 1295:         */
1.65    ! schwarze 1296:        if (n != body)
        !          1297:                mdoc_vmsg(m, MANDOCERR_SCOPENEST, line, ppos,
        !          1298:                                "%s broken", mdoc_macronames[tok]);
1.32      schwarze 1299:
1.47      schwarze 1300:        if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      schwarze 1301:                return(0);
1.1       kristaps 1302:
1.22      schwarze 1303:        /* Standard appending of delimiters. */
1.1       kristaps 1304:
1.43      schwarze 1305:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1306:                return(0);
                   1307:
1.22      schwarze 1308:        /* Rewind scope, if applicable. */
1.1       kristaps 1309:
1.47      schwarze 1310:        if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1311:                return(0);
                   1312:
                   1313:        return(1);
                   1314: }
                   1315:
                   1316:
                   1317: static int
                   1318: blk_part_exp(MACRO_PROT_ARGS)
                   1319: {
1.37      schwarze 1320:        int               la, nl;
1.36      schwarze 1321:        enum margserr     ac;
1.32      schwarze 1322:        struct mdoc_node *head; /* keep track of head */
                   1323:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1324:        char             *p;
1.36      schwarze 1325:        enum mdoct        ntok;
1.1       kristaps 1326:
1.37      schwarze 1327:        nl = MDOC_NEWLINE & m->flags;
                   1328:
1.32      schwarze 1329:        /*
                   1330:         * The opening of an explicit macro having zero or more leading
                   1331:         * punctuation nodes; a head with optional single element (the
                   1332:         * case of `Eo'); and a body that may be empty.
                   1333:         */
1.22      schwarze 1334:
                   1335:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1336:                return(0);
1.22      schwarze 1337:
1.32      schwarze 1338:        for (head = body = NULL; ; ) {
1.22      schwarze 1339:                la = *pos;
1.36      schwarze 1340:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1341:
1.36      schwarze 1342:                if (ARGS_ERROR == ac)
1.1       kristaps 1343:                        return(0);
1.36      schwarze 1344:                if (ARGS_PUNCT == ac)
1.1       kristaps 1345:                        break;
1.36      schwarze 1346:                if (ARGS_EOLN == ac)
1.1       kristaps 1347:                        break;
                   1348:
1.32      schwarze 1349:                /* Flush out leading punctuation. */
                   1350:
1.36      schwarze 1351:                if (NULL == head && ARGS_QWORD != ac &&
1.65    ! schwarze 1352:                    DELIM_OPEN == mandoc_isdelim(p)) {
1.32      schwarze 1353:                        assert(NULL == body);
                   1354:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1355:                                return(0);
                   1356:                        continue;
                   1357:                }
                   1358:
                   1359:                if (NULL == head) {
                   1360:                        assert(NULL == body);
                   1361:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1362:                                return(0);
1.32      schwarze 1363:                        head = m->last;
1.1       kristaps 1364:                }
                   1365:
1.32      schwarze 1366:                /*
                   1367:                 * `Eo' gobbles any data into the head, but most other
                   1368:                 * macros just immediately close out and begin the body.
                   1369:                 */
                   1370:
                   1371:                if (NULL == body) {
                   1372:                        assert(head);
                   1373:                        /* No check whether it's a macro! */
                   1374:                        if (MDOC_Eo == tok)
                   1375:                                if ( ! mdoc_word_alloc(m, line, la, p))
                   1376:                                        return(0);
                   1377:
1.22      schwarze 1378:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1379:                                return(0);
1.22      schwarze 1380:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1381:                                return(0);
1.32      schwarze 1382:                        body = m->last;
                   1383:
                   1384:                        if (MDOC_Eo == tok)
                   1385:                                continue;
                   1386:                }
                   1387:
                   1388:                assert(NULL != head && NULL != body);
                   1389:
1.36      schwarze 1390:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1391:
                   1392:                if (MDOC_MAX == ntok) {
                   1393:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.32      schwarze 1394:                                return(0);
1.36      schwarze 1395:                        continue;
1.1       kristaps 1396:                }
1.32      schwarze 1397:
1.36      schwarze 1398:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1399:                        return(0);
1.36      schwarze 1400:                break;
1.1       kristaps 1401:        }
                   1402:
1.32      schwarze 1403:        /* Clean-up to leave in a consistent state. */
                   1404:
                   1405:        if (NULL == head) {
                   1406:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1407:                        return(0);
                   1408:                head = m->last;
                   1409:        }
1.22      schwarze 1410:
1.32      schwarze 1411:        if (NULL == body) {
1.22      schwarze 1412:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1413:                        return(0);
1.22      schwarze 1414:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1415:                        return(0);
1.32      schwarze 1416:                body = m->last;
1.1       kristaps 1417:        }
                   1418:
1.22      schwarze 1419:        /* Standard appending of delimiters. */
                   1420:
1.37      schwarze 1421:        if ( ! nl)
1.1       kristaps 1422:                return(1);
1.22      schwarze 1423:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1424: }
                   1425:
                   1426:
1.37      schwarze 1427: /* ARGSUSED */
1.1       kristaps 1428: static int
                   1429: in_line_argn(MACRO_PROT_ARGS)
                   1430: {
1.37      schwarze 1431:        int              la, flushed, j, maxargs, nl;
1.36      schwarze 1432:        enum margserr    ac;
                   1433:        enum margverr    av;
                   1434:        struct mdoc_arg *arg;
                   1435:        char            *p;
                   1436:        enum mdoct       ntok;
1.1       kristaps 1437:
1.37      schwarze 1438:        nl = MDOC_NEWLINE & m->flags;
                   1439:
1.32      schwarze 1440:        /*
                   1441:         * A line macro that has a fixed number of arguments (maxargs).
                   1442:         * Only open the scope once the first non-leading-punctuation is
                   1443:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1444:         * keep it open until the maximum number of arguments are
                   1445:         * exhausted.
                   1446:         */
1.1       kristaps 1447:
                   1448:        switch (tok) {
                   1449:        case (MDOC_Ap):
                   1450:                /* FALLTHROUGH */
                   1451:        case (MDOC_No):
                   1452:                /* FALLTHROUGH */
                   1453:        case (MDOC_Ns):
                   1454:                /* FALLTHROUGH */
                   1455:        case (MDOC_Ux):
                   1456:                maxargs = 0;
                   1457:                break;
1.64      schwarze 1458:        case (MDOC_Bx):
                   1459:                /* FALLTHROUGH */
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.65    ! schwarze 1499:                                0 == j && DELIM_OPEN == mandoc_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 &&
1.65    ! schwarze 1528:                                DELIM_NONE != mandoc_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.65    ! schwarze 1667:        mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS);
        !          1668:        return(1);
1.1       kristaps 1669: }
                   1670:
                   1671:
1.17      schwarze 1672: /*
                   1673:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1674:  * They're unusual because they're basically free-form text until a
                   1675:  * macro is encountered.
                   1676:  */
1.1       kristaps 1677: static int
1.46      schwarze 1678: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1679: {
1.36      schwarze 1680:        int              la, pos;
1.46      schwarze 1681:        enum margserr    ac;
1.36      schwarze 1682:        enum mdoct       ntok;
                   1683:        char            *p;
1.1       kristaps 1684:
1.17      schwarze 1685:        for (pos = ppos; ; ) {
                   1686:                la = pos;
1.1       kristaps 1687:
1.46      schwarze 1688:                ac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1689:
1.46      schwarze 1690:                if (ARGS_ERROR == ac)
1.17      schwarze 1691:                        return(0);
1.46      schwarze 1692:                if (ARGS_EOLN == ac)
1.17      schwarze 1693:                        break;
1.1       kristaps 1694:
1.46      schwarze 1695:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1696:
1.36      schwarze 1697:                if (MDOC_MAX == ntok) {
                   1698:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1699:                                return(0);
1.36      schwarze 1700:                        continue;
                   1701:                }
1.1       kristaps 1702:
1.36      schwarze 1703:                if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1       kristaps 1704:                        return(0);
1.36      schwarze 1705:                return(append_delims(m, line, &pos, buf));
1.1       kristaps 1706:        }
                   1707:
                   1708:        return(1);
                   1709: }
1.17      schwarze 1710:
                   1711:
1.46      schwarze 1712: /* ARGSUSED */
                   1713: static int
                   1714: phrase_ta(MACRO_PROT_ARGS)
                   1715: {
                   1716:        int               la;
                   1717:        enum mdoct        ntok;
                   1718:        enum margserr     ac;
                   1719:        char             *p;
                   1720:
                   1721:        /*
                   1722:         * FIXME: this is overly restrictive: if the `Ta' is unexpected,
                   1723:         * it should simply error out with ARGSLOST.
                   1724:         */
                   1725:
                   1726:        if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
                   1727:                return(0);
                   1728:        if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
                   1729:                return(0);
                   1730:
                   1731:        for (;;) {
                   1732:                la = *pos;
                   1733:                ac = mdoc_zargs(m, line, pos, buf, 0, &p);
                   1734:
                   1735:                if (ARGS_ERROR == ac)
                   1736:                        return(0);
                   1737:                if (ARGS_EOLN == ac)
                   1738:                        break;
                   1739:
                   1740:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
                   1741:
                   1742:                if (MDOC_MAX == ntok) {
                   1743:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1744:                                return(0);
                   1745:                        continue;
                   1746:                }
                   1747:
                   1748:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
                   1749:                        return(0);
                   1750:                return(append_delims(m, line, pos, buf));
                   1751:        }
                   1752:
                   1753:        return(1);
                   1754: }