[BACK]Return to mdoc_macro.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_macro.c, Revision 1.59

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