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

Annotation of mandoc/mdoc_macro.c, Revision 1.119

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

CVSweb