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

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