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

Annotation of mandoc/mdoc_macro.c, Revision 1.103

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

CVSweb