[BACK]Return to mdoc_macro.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc_macro.c, Revision 1.130

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

CVSweb