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

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