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

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