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

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