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

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