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

Annotation of mandoc/mdoc_macro.c, Revision 1.87

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

CVSweb