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

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