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

Annotation of mandoc/mdoc_macro.c, Revision 1.88

1.88    ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.87 2010/07/01 14:28:12 kristaps 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.88    ! schwarze  100:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1       kristaps  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.88    ! schwarze  395:        case (MDOC_Nm):
        !           396:                return(REWIND_NONE);
1.19      kristaps  397:        case (MDOC_Nd):
                    398:                /* FALLTHROUGH */
1.1       kristaps  399:        case (MDOC_Ss):
                    400:                if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
1.84      schwarze  401:                        return(REWIND_NONE);
1.1       kristaps  402:                /* FALLTHROUGH */
                    403:        case (MDOC_Sh):
1.84      schwarze  404:                if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
                    405:                    MDOC_Sh == p->tok)
                    406:                        return(REWIND_MORE);
1.1       kristaps  407:                break;
                    408:        default:
                    409:                break;
                    410:        }
                    411:
1.86      schwarze  412:        /*
                    413:         * Default block rewinding rules.
1.88    ! schwarze  414:         * In particular, always skip block end markers,
        !           415:         * and let all blocks rewind Nm children.
1.86      schwarze  416:         */
1.88    ! schwarze  417:        if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
        !           418:            (MDOC_BLOCK == p->type &&
1.86      schwarze  419:            ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
                    420:                return(REWIND_MORE);
                    421:
                    422:        /*
                    423:         * Partial blocks allow delayed rewinding by default.
                    424:         */
                    425:        if (&blk_full != mdoc_macros[tok].fp)
                    426:                return (REWIND_LATER);
                    427:
                    428:        /*
                    429:         * Full blocks can only be rewound when matching
                    430:         * or when there is an explicit rule.
                    431:         */
                    432:        return(REWIND_ERROR);
1.1       kristaps  433: }
                    434:
                    435:
                    436: static int
1.47      kristaps  437: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1       kristaps  438: {
                    439:        struct mdoc_node *n;
                    440:
                    441:        n = mdoc->last;
                    442:        if (MDOC_ELEM != n->type)
                    443:                n = n->parent;
                    444:        assert(MDOC_ELEM == n->type);
                    445:        assert(tok == n->tok);
                    446:
                    447:        return(rew_last(mdoc, n));
                    448: }
                    449:
                    450:
1.83      schwarze  451: /*
                    452:  * We are trying to close a block identified by tok,
                    453:  * but the child block *broken is still open.
                    454:  * Thus, postpone closing the tok block
                    455:  * until the rew_sub call closing *broken.
                    456:  */
                    457: static int
                    458: make_pending(struct mdoc_node *broken, enum mdoct tok,
                    459:                struct mdoc *m, int line, int ppos)
                    460: {
                    461:        struct mdoc_node *breaker;
                    462:
                    463:        /*
                    464:         * Iterate backwards, searching for the block matching tok,
                    465:         * that is, the block breaking the *broken block.
                    466:         */
                    467:        for (breaker = broken->parent; breaker; breaker = breaker->parent) {
                    468:
                    469:                /*
                    470:                 * If the *broken block had already been broken before
                    471:                 * and we encounter its breaker, make the tok block
                    472:                 * pending on the inner breaker.
                    473:                 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
                    474:                 * becomes "[A broken=[B [C->B B] tok=A] C]"
                    475:                 * and finally "[A [B->A [C->B B] A] C]".
                    476:                 */
                    477:                if (breaker == broken->pending) {
                    478:                        broken = breaker;
                    479:                        continue;
                    480:                }
                    481:
1.84      schwarze  482:                if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
1.83      schwarze  483:                        continue;
                    484:                if (MDOC_BODY == broken->type)
                    485:                        broken = broken->parent;
                    486:
                    487:                /*
                    488:                 * Found the breaker.
                    489:                 * If another, outer breaker is already pending on
                    490:                 * the *broken block, we must not clobber the link
                    491:                 * to the outer breaker, but make it pending on the
                    492:                 * new, now inner breaker.
                    493:                 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
                    494:                 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
                    495:                 * and finally "[A [B->A [C->B A] B] C]".
                    496:                 */
                    497:                if (broken->pending) {
                    498:                        struct mdoc_node *taker;
                    499:
                    500:                        /*
                    501:                         * If the breaker had also been broken before,
                    502:                         * it cannot take on the outer breaker itself,
                    503:                         * but must hand it on to its own breakers.
                    504:                         * Graphically, this is the following situation:
                    505:                         * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
                    506:                         * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
                    507:                         */
                    508:                        taker = breaker;
                    509:                        while (taker->pending)
                    510:                                taker = taker->pending;
                    511:                        taker->pending = broken->pending;
                    512:                }
                    513:                broken->pending = breaker;
1.88    ! schwarze  514:                mdoc_vmsg(m, MANDOCERR_SCOPENEST, line, ppos,
        !           515:                    "%s breaks %s", mdoc_macronames[tok],
        !           516:                    mdoc_macronames[broken->tok]);
1.83      schwarze  517:                return(1);
                    518:        }
                    519:
                    520:        /*
                    521:         * Found no matching block for tok.
                    522:         * Are you trying to close a block that is not open?
1.84      schwarze  523:         * XXX Make this non-fatal.
1.83      schwarze  524:         */
                    525:        mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTNOSCOPE);
                    526:        return(0);
                    527: }
                    528:
1.84      schwarze  529:
1.1       kristaps  530: static int
1.32      kristaps  531: rew_sub(enum mdoc_type t, struct mdoc *m,
1.47      kristaps  532:                enum mdoct tok, int line, int ppos)
1.1       kristaps  533: {
                    534:        struct mdoc_node *n;
                    535:
1.84      schwarze  536:        n = m->last;
                    537:        while (n) {
                    538:                switch (rew_dohalt(tok, t, n)) {
                    539:                case (REWIND_NONE):
                    540:                        return(1);
                    541:                case (REWIND_THIS):
                    542:                        break;
                    543:                case (REWIND_MORE):
                    544:                        n = n->parent;
                    545:                        continue;
                    546:                case (REWIND_LATER):
                    547:                        return(make_pending(n, tok, m, line, ppos));
                    548:                case (REWIND_ERROR):
                    549:                        /* XXX Make this non-fatal. */
1.88    ! schwarze  550:                        mdoc_vmsg(m, MANDOCERR_SCOPEFATAL, line, ppos,
        !           551:                            "%s cannot break %s", mdoc_macronames[tok],
        !           552:                            mdoc_macronames[n->tok]);
1.84      schwarze  553:                        return 0;
1.32      kristaps  554:                }
1.84      schwarze  555:                break;
1.1       kristaps  556:        }
                    557:
                    558:        assert(n);
1.47      kristaps  559:        if ( ! rew_last(m, n))
                    560:                return(0);
                    561:
                    562:        /*
1.83      schwarze  563:         * The current block extends an enclosing block.
                    564:         * Now that the current block ends, close the enclosing block, too.
1.47      kristaps  565:         */
1.83      schwarze  566:        while (NULL != (n = n->pending)) {
1.47      kristaps  567:                if ( ! rew_last(m, n))
                    568:                        return(0);
1.83      schwarze  569:                if (MDOC_HEAD == n->type &&
                    570:                    ! mdoc_body_alloc(m, n->line, n->pos, n->tok))
1.47      kristaps  571:                        return(0);
                    572:        }
                    573:
                    574:        return(1);
1.1       kristaps  575: }
                    576:
                    577:
                    578: static int
1.66      kristaps  579: append_delims(struct mdoc *m, int line, int *pos, char *buf)
1.1       kristaps  580: {
1.66      kristaps  581:        int              la;
1.55      kristaps  582:        enum margserr    ac;
1.1       kristaps  583:        char            *p;
                    584:
1.66      kristaps  585:        if ('\0' == buf[*pos])
1.1       kristaps  586:                return(1);
                    587:
                    588:        for (;;) {
1.66      kristaps  589:                la = *pos;
                    590:                ac = mdoc_zargs(m, line, pos, buf, ARGS_NOWARN, &p);
1.1       kristaps  591:
1.53      kristaps  592:                if (ARGS_ERROR == ac)
1.1       kristaps  593:                        return(0);
1.53      kristaps  594:                else if (ARGS_EOLN == ac)
1.1       kristaps  595:                        break;
1.66      kristaps  596:
1.67      schwarze  597:                assert(DELIM_NONE != mdoc_isdelim(p));
1.66      kristaps  598:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps  599:                        return(0);
1.66      kristaps  600:
                    601:                /*
                    602:                 * If we encounter end-of-sentence symbols, then trigger
                    603:                 * the double-space.
                    604:                 *
                    605:                 * XXX: it's easy to allow this to propogate outward to
                    606:                 * the last symbol, such that `. )' will cause the
                    607:                 * correct double-spacing.  However, (1) groff isn't
                    608:                 * smart enough to do this and (2) it would require
                    609:                 * knowing which symbols break this behaviour, for
                    610:                 * example, `.  ;' shouldn't propogate the double-space.
                    611:                 */
                    612:                if (mandoc_eos(p, strlen(p)))
                    613:                        m->last->flags |= MDOC_EOS;
1.1       kristaps  614:        }
                    615:
                    616:        return(1);
                    617: }
                    618:
                    619:
                    620: /*
                    621:  * Close out block partial/full explicit.
                    622:  */
                    623: static int
                    624: blk_exp_close(MACRO_PROT_ARGS)
                    625: {
1.83      schwarze  626:        struct mdoc_node *body;         /* Our own body. */
                    627:        struct mdoc_node *later;        /* A sub-block starting later. */
                    628:        struct mdoc_node *n;            /* For searching backwards. */
                    629:
1.60      kristaps  630:        int              j, lastarg, maxargs, flushed, nl;
1.55      kristaps  631:        enum margserr    ac;
1.83      schwarze  632:        enum mdoct       atok, ntok;
1.1       kristaps  633:        char            *p;
                    634:
1.60      kristaps  635:        nl = MDOC_NEWLINE & m->flags;
                    636:
1.1       kristaps  637:        switch (tok) {
                    638:        case (MDOC_Ec):
                    639:                maxargs = 1;
                    640:                break;
                    641:        default:
                    642:                maxargs = 0;
                    643:                break;
                    644:        }
                    645:
1.83      schwarze  646:        /*
                    647:         * Search backwards for beginnings of blocks,
                    648:         * both of our own and of pending sub-blocks.
                    649:         */
                    650:        atok = rew_alt(tok);
                    651:        body = later = NULL;
                    652:        for (n = m->last; n; n = n->parent) {
                    653:                if (MDOC_VALID & n->flags)
                    654:                        continue;
                    655:
                    656:                /* Remember the start of our own body. */
                    657:                if (MDOC_BODY == n->type && atok == n->tok) {
1.87      kristaps  658:                        if (ENDBODY_NOT == n->end)
1.83      schwarze  659:                                body = n;
                    660:                        continue;
                    661:                }
                    662:
1.88    ! schwarze  663:                if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.83      schwarze  664:                        continue;
                    665:                if (atok == n->tok) {
                    666:                        assert(body);
                    667:
                    668:                        /*
                    669:                         * Found the start of our own block.
                    670:                         * When there is no pending sub block,
                    671:                         * just proceed to closing out.
                    672:                         */
                    673:                        if (NULL == later)
                    674:                                break;
                    675:
                    676:                        /*
                    677:                         * When there is a pending sub block,
                    678:                         * postpone closing out the current block
                    679:                         * until the rew_sub() closing out the sub-block.
                    680:                         */
                    681:                        if ( ! make_pending(later, tok, m, line, ppos))
                    682:                                return(0);
                    683:
                    684:                        /*
                    685:                         * Mark the place where the formatting - but not
                    686:                         * the scope - of the current block ends.
                    687:                         */
                    688:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                    689:                            atok, body, ENDBODY_SPACE))
                    690:                                return(0);
                    691:                        break;
                    692:                }
                    693:
                    694:                /*
                    695:                 * When finding an open sub block, remember the last
                    696:                 * open explicit block, or, in case there are only
                    697:                 * implicit ones, the first open implicit block.
                    698:                 */
                    699:                if (later &&
                    700:                    MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
                    701:                        continue;
                    702:                if (MDOC_CALLABLE & mdoc_macros[n->tok].flags) {
                    703:                        assert( ! (MDOC_ACTED & n->flags));
                    704:                        later = n;
                    705:                }
                    706:        }
                    707:
1.1       kristaps  708:        if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.68      kristaps  709:                /* FIXME: do this in validate */
1.22      kristaps  710:                if (buf[*pos])
1.68      kristaps  711:                        if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST))
1.1       kristaps  712:                                return(0);
1.22      kristaps  713:
1.32      kristaps  714:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      kristaps  715:                        return(0);
1.32      kristaps  716:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  717:        }
                    718:
1.32      kristaps  719:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  720:                return(0);
                    721:
1.83      schwarze  722:        if (NULL == later && maxargs > 0)
1.32      kristaps  723:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  724:                        return(0);
                    725:
1.27      kristaps  726:        for (flushed = j = 0; ; j++) {
1.1       kristaps  727:                lastarg = *pos;
                    728:
                    729:                if (j == maxargs && ! flushed) {
1.32      kristaps  730:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  731:                                return(0);
                    732:                        flushed = 1;
                    733:                }
                    734:
1.53      kristaps  735:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  736:
1.53      kristaps  737:                if (ARGS_ERROR == ac)
1.1       kristaps  738:                        return(0);
1.53      kristaps  739:                if (ARGS_PUNCT == ac)
1.1       kristaps  740:                        break;
1.53      kristaps  741:                if (ARGS_EOLN == ac)
1.1       kristaps  742:                        break;
                    743:
1.54      kristaps  744:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                    745:
                    746:                if (MDOC_MAX == ntok) {
                    747:                        if ( ! mdoc_word_alloc(m, line, lastarg, p))
1.1       kristaps  748:                                return(0);
1.54      kristaps  749:                        continue;
                    750:                }
1.1       kristaps  751:
1.54      kristaps  752:                if ( ! flushed) {
                    753:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                    754:                                return(0);
                    755:                        flushed = 1;
                    756:                }
1.82      kristaps  757:                if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
1.1       kristaps  758:                        return(0);
1.54      kristaps  759:                break;
1.1       kristaps  760:        }
                    761:
1.32      kristaps  762:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  763:                return(0);
                    764:
1.60      kristaps  765:        if ( ! nl)
1.1       kristaps  766:                return(1);
1.32      kristaps  767:        return(append_delims(m, line, pos, buf));
1.1       kristaps  768: }
                    769:
                    770:
                    771: static int
                    772: in_line(MACRO_PROT_ARGS)
                    773: {
1.70      kristaps  774:        int              la, scope, cnt, nc, nl;
1.56      kristaps  775:        enum margverr    av;
1.53      kristaps  776:        enum mdoct       ntok;
1.55      kristaps  777:        enum margserr    ac;
1.67      schwarze  778:        enum mdelim      d;
1.53      kristaps  779:        struct mdoc_arg *arg;
                    780:        char            *p;
1.1       kristaps  781:
1.60      kristaps  782:        nl = MDOC_NEWLINE & m->flags;
                    783:
1.4       kristaps  784:        /*
                    785:         * Whether we allow ignored elements (those without content,
                    786:         * usually because of reserved words) to squeak by.
                    787:         */
1.45      kristaps  788:
1.4       kristaps  789:        switch (tok) {
1.26      kristaps  790:        case (MDOC_An):
                    791:                /* FALLTHROUGH */
                    792:        case (MDOC_Ar):
1.4       kristaps  793:                /* FALLTHROUGH */
                    794:        case (MDOC_Fl):
                    795:                /* FALLTHROUGH */
1.12      kristaps  796:        case (MDOC_Lk):
                    797:                /* FALLTHROUGH */
1.26      kristaps  798:        case (MDOC_Nm):
                    799:                /* FALLTHROUGH */
1.25      kristaps  800:        case (MDOC_Pa):
1.4       kristaps  801:                nc = 1;
                    802:                break;
                    803:        default:
                    804:                nc = 0;
                    805:                break;
                    806:        }
                    807:
1.27      kristaps  808:        for (arg = NULL;; ) {
1.1       kristaps  809:                la = *pos;
1.56      kristaps  810:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  811:
1.56      kristaps  812:                if (ARGV_WORD == av) {
1.1       kristaps  813:                        *pos = la;
                    814:                        break;
                    815:                }
1.56      kristaps  816:                if (ARGV_EOLN == av)
1.1       kristaps  817:                        break;
1.56      kristaps  818:                if (ARGV_ARG == av)
1.1       kristaps  819:                        continue;
                    820:
                    821:                mdoc_argv_free(arg);
                    822:                return(0);
                    823:        }
                    824:
1.70      kristaps  825:        for (cnt = scope = 0;; ) {
1.1       kristaps  826:                la = *pos;
1.53      kristaps  827:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  828:
1.53      kristaps  829:                if (ARGS_ERROR == ac)
1.1       kristaps  830:                        return(0);
1.53      kristaps  831:                if (ARGS_EOLN == ac)
1.1       kristaps  832:                        break;
1.53      kristaps  833:                if (ARGS_PUNCT == ac)
1.1       kristaps  834:                        break;
                    835:
1.53      kristaps  836:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  837:
1.4       kristaps  838:                /*
                    839:                 * In this case, we've located a submacro and must
                    840:                 * execute it.  Close out scope, if open.  If no
                    841:                 * elements have been generated, either create one (nc)
                    842:                 * or raise a warning.
                    843:                 */
1.1       kristaps  844:
1.53      kristaps  845:                if (MDOC_MAX != ntok) {
1.70      kristaps  846:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  847:                                return(0);
1.4       kristaps  848:                        if (nc && 0 == cnt) {
1.32      kristaps  849:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  850:                                        return(0);
1.32      kristaps  851:                                if ( ! rew_last(m, m->last))
1.12      kristaps  852:                                        return(0);
1.7       kristaps  853:                        } else if ( ! nc && 0 == cnt) {
                    854:                                mdoc_argv_free(arg);
1.68      kristaps  855:                                if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY))
1.3       kristaps  856:                                        return(0);
1.7       kristaps  857:                        }
1.82      kristaps  858:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps  859:                                return(0);
1.60      kristaps  860:                        if ( ! nl)
1.1       kristaps  861:                                return(1);
1.32      kristaps  862:                        return(append_delims(m, line, pos, buf));
1.29      kristaps  863:                }
1.1       kristaps  864:
1.4       kristaps  865:                /*
                    866:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    867:                 * a word; rewind the scope, if a delimiter; then append
                    868:                 * the word.
                    869:                 */
1.1       kristaps  870:
1.67      schwarze  871:                d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.3       kristaps  872:
1.70      kristaps  873:                if (DELIM_NONE != d) {
                    874:                        /*
                    875:                         * If we encounter closing punctuation, no word
                    876:                         * has been omitted, no scope is open, and we're
                    877:                         * allowed to have an empty element, then start
                    878:                         * a new scope.  `Ar', `Fl', and `Li', only do
                    879:                         * this once per invocation.  There may be more
                    880:                         * of these (all of them?).
                    881:                         */
                    882:                        if (0 == cnt && (nc || MDOC_Li == tok) &&
                    883:                                        DELIM_CLOSE == d && ! scope) {
                    884:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
                    885:                                        return(0);
                    886:                                if (MDOC_Ar == tok || MDOC_Li == tok ||
                    887:                                                MDOC_Fl == tok)
                    888:                                        cnt++;
                    889:                                scope = 1;
                    890:                        }
                    891:                        /*
                    892:                         * Close out our scope, if one is open, before
                    893:                         * any punctuation.
                    894:                         */
                    895:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  896:                                return(0);
1.70      kristaps  897:                        scope = 0;
                    898:                } else if ( ! scope) {
1.32      kristaps  899:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  900:                                return(0);
1.70      kristaps  901:                        scope = 1;
1.1       kristaps  902:                }
                    903:
1.67      schwarze  904:                if (DELIM_NONE == d)
1.3       kristaps  905:                        cnt++;
1.32      kristaps  906:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps  907:                        return(0);
1.39      kristaps  908:
                    909:                /*
                    910:                 * `Fl' macros have their scope re-opened with each new
                    911:                 * word so that the `-' can be added to each one without
                    912:                 * having to parse out spaces.
                    913:                 */
1.70      kristaps  914:                if (scope && MDOC_Fl == tok) {
1.39      kristaps  915:                        if ( ! rew_elem(m, tok))
                    916:                                return(0);
1.70      kristaps  917:                        scope = 0;
1.39      kristaps  918:                }
1.1       kristaps  919:        }
                    920:
1.70      kristaps  921:        if (scope && ! rew_elem(m, tok))
1.1       kristaps  922:                return(0);
1.4       kristaps  923:
                    924:        /*
                    925:         * If no elements have been collected and we're allowed to have
                    926:         * empties (nc), open a scope and close it out.  Otherwise,
                    927:         * raise a warning.
                    928:         */
1.45      kristaps  929:
1.4       kristaps  930:        if (nc && 0 == cnt) {
1.32      kristaps  931:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  932:                        return(0);
1.32      kristaps  933:                if ( ! rew_last(m, m->last))
1.12      kristaps  934:                        return(0);
1.7       kristaps  935:        } else if ( ! nc && 0 == cnt) {
                    936:                mdoc_argv_free(arg);
1.68      kristaps  937:                if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY))
1.3       kristaps  938:                        return(0);
1.7       kristaps  939:        }
1.4       kristaps  940:
1.60      kristaps  941:        if ( ! nl)
1.1       kristaps  942:                return(1);
1.32      kristaps  943:        return(append_delims(m, line, pos, buf));
1.1       kristaps  944: }
                    945:
                    946:
                    947: static int
                    948: blk_full(MACRO_PROT_ARGS)
                    949: {
1.63      kristaps  950:        int               la, nl;
1.1       kristaps  951:        struct mdoc_arg  *arg;
1.45      kristaps  952:        struct mdoc_node *head; /* save of head macro */
1.49      kristaps  953:        struct mdoc_node *body; /* save of body macro */
1.47      kristaps  954:        struct mdoc_node *n;
1.74      kristaps  955:        enum mdoc_type    mtt;
1.53      kristaps  956:        enum mdoct        ntok;
1.59      kristaps  957:        enum margserr     ac, lac;
1.56      kristaps  958:        enum margverr     av;
1.1       kristaps  959:        char             *p;
                    960:
1.63      kristaps  961:        nl = MDOC_NEWLINE & m->flags;
                    962:
1.45      kristaps  963:        /* Close out prior implicit scope. */
1.19      kristaps  964:
1.1       kristaps  965:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32      kristaps  966:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  967:                        return(0);
1.32      kristaps  968:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  969:                        return(0);
                    970:        }
                    971:
1.45      kristaps  972:        /*
                    973:         * This routine accomodates implicitly- and explicitly-scoped
                    974:         * macro openings.  Implicit ones first close out prior scope
                    975:         * (seen above).  Delay opening the head until necessary to
                    976:         * allow leading punctuation to print.  Special consideration
                    977:         * for `It -column', which has phrase-part syntax instead of
                    978:         * regular child nodes.
                    979:         */
                    980:
1.1       kristaps  981:        for (arg = NULL;; ) {
1.45      kristaps  982:                la = *pos;
1.56      kristaps  983:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  984:
1.56      kristaps  985:                if (ARGV_WORD == av) {
1.45      kristaps  986:                        *pos = la;
1.1       kristaps  987:                        break;
                    988:                }
                    989:
1.56      kristaps  990:                if (ARGV_EOLN == av)
1.1       kristaps  991:                        break;
1.56      kristaps  992:                if (ARGV_ARG == av)
1.1       kristaps  993:                        continue;
                    994:
                    995:                mdoc_argv_free(arg);
                    996:                return(0);
                    997:        }
                    998:
1.32      kristaps  999:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1000:                return(0);
                   1001:
1.49      kristaps 1002:        head = body = NULL;
1.1       kristaps 1003:
1.45      kristaps 1004:        /*
                   1005:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1006:         * of block partial-explicit and full-implicit.  Stupid.
                   1007:         */
1.19      kristaps 1008:
1.45      kristaps 1009:        if (MDOC_Nd == tok) {
                   1010:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1011:                        return(0);
                   1012:                head = m->last;
1.32      kristaps 1013:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19      kristaps 1014:                        return(0);
1.32      kristaps 1015:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19      kristaps 1016:                        return(0);
1.49      kristaps 1017:                body = m->last;
1.19      kristaps 1018:        }
                   1019:
1.59      kristaps 1020:        ac = ARGS_ERROR;
                   1021:
1.62      kristaps 1022:        for ( ; ; ) {
1.45      kristaps 1023:                la = *pos;
1.75      kristaps 1024:                /* Initialise last-phrase-type with ARGS_PEND. */
                   1025:                lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.53      kristaps 1026:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1027:
1.53      kristaps 1028:                if (ARGS_ERROR == ac)
1.1       kristaps 1029:                        return(0);
1.75      kristaps 1030:
                   1031:                if (ARGS_EOLN == ac) {
                   1032:                        if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
                   1033:                                break;
                   1034:                        /*
                   1035:                         * This is necessary: if the last token on a
                   1036:                         * line is a `Ta' or tab, then we'll get
                   1037:                         * ARGS_EOLN, so we must be smart enough to
                   1038:                         * reopen our scope if the last parse was a
                   1039:                         * phrase or partial phrase.
                   1040:                         */
                   1041:                        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1042:                                return(0);
                   1043:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1044:                                return(0);
                   1045:                        body = m->last;
1.1       kristaps 1046:                        break;
1.75      kristaps 1047:                }
1.45      kristaps 1048:
1.73      kristaps 1049:                /*
                   1050:                 * Emit leading punctuation (i.e., punctuation before
                   1051:                 * the MDOC_HEAD) for non-phrase types.
                   1052:                 */
1.45      kristaps 1053:
1.59      kristaps 1054:                if (NULL == head &&
1.71      kristaps 1055:                                ARGS_PEND != ac &&
1.59      kristaps 1056:                                ARGS_PHRASE != ac &&
1.57      kristaps 1057:                                ARGS_PPHRASE != ac &&
1.53      kristaps 1058:                                ARGS_QWORD != ac &&
1.67      schwarze 1059:                                DELIM_OPEN == mdoc_isdelim(p)) {
1.45      kristaps 1060:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1061:                                return(0);
                   1062:                        continue;
                   1063:                }
                   1064:
1.74      kristaps 1065:                /* Open a head if one hasn't been opened. */
1.45      kristaps 1066:
1.74      kristaps 1067:                if (NULL == head) {
1.45      kristaps 1068:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1069:                                return(0);
                   1070:                        head = m->last;
                   1071:                }
                   1072:
1.72      kristaps 1073:                if (ARGS_PHRASE == ac ||
                   1074:                                ARGS_PEND == ac ||
                   1075:                                ARGS_PPHRASE == ac) {
1.73      kristaps 1076:                        /*
1.74      kristaps 1077:                         * If we haven't opened a body yet, rewind the
                   1078:                         * head; if we have, rewind that instead.
                   1079:                         */
                   1080:
                   1081:                        mtt = body ? MDOC_BODY : MDOC_HEAD;
                   1082:                        if ( ! rew_sub(mtt, m, tok, line, ppos))
                   1083:                                return(0);
                   1084:
                   1085:                        /* Then allocate our body context. */
                   1086:
                   1087:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1088:                                return(0);
                   1089:                        body = m->last;
                   1090:
                   1091:                        /*
1.73      kristaps 1092:                         * Process phrases: set whether we're in a
                   1093:                         * partial-phrase (this effects line handling)
                   1094:                         * then call down into the phrase parser.
                   1095:                         */
1.74      kristaps 1096:
1.65      kristaps 1097:                        if (ARGS_PPHRASE == ac)
                   1098:                                m->flags |= MDOC_PPHRASE;
1.71      kristaps 1099:                        if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
                   1100:                                m->flags |= MDOC_PPHRASE;
                   1101:
1.82      kristaps 1102:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps 1103:                                return(0);
1.71      kristaps 1104:
1.65      kristaps 1105:                        m->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1106:                        continue;
                   1107:                }
                   1108:
1.54      kristaps 1109:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1110:
                   1111:                if (MDOC_MAX == ntok) {
1.53      kristaps 1112:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1113:                                return(0);
1.53      kristaps 1114:                        continue;
1.45      kristaps 1115:                }
1.53      kristaps 1116:
1.82      kristaps 1117:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.45      kristaps 1118:                        return(0);
1.53      kristaps 1119:                break;
1.45      kristaps 1120:        }
1.1       kristaps 1121:
1.45      kristaps 1122:        if (NULL == head) {
                   1123:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1124:                        return(0);
1.45      kristaps 1125:                head = m->last;
1.1       kristaps 1126:        }
                   1127:
1.63      kristaps 1128:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1129:                return(0);
1.19      kristaps 1130:
1.49      kristaps 1131:        /* If we've already opened our body, exit now. */
1.45      kristaps 1132:
1.49      kristaps 1133:        if (NULL != body)
1.77      kristaps 1134:                goto out;
1.19      kristaps 1135:
1.47      kristaps 1136:        /*
1.49      kristaps 1137:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1138:         * explicit close-out, postpone switching the current block from
                   1139:         * head to body until the rew_sub() call closing out that
                   1140:         * sub-block.
1.47      kristaps 1141:         */
                   1142:        for (n = m->last; n && n != head; n = n->parent) {
1.49      kristaps 1143:                if (MDOC_BLOCK == n->type &&
                   1144:                                MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1145:                                ! (MDOC_VALID & n->flags)) {
                   1146:                        assert( ! (MDOC_ACTED & n->flags));
1.47      kristaps 1147:                        n->pending = head;
                   1148:                        return(1);
                   1149:                }
                   1150:        }
                   1151:
1.45      kristaps 1152:        /* Close out scopes to remain in a consistent state. */
                   1153:
1.32      kristaps 1154:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1155:                return(0);
1.32      kristaps 1156:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1157:                return(0);
                   1158:
1.77      kristaps 1159: out:
                   1160:        if ( ! (MDOC_FREECOL & m->flags))
                   1161:                return(1);
                   1162:
                   1163:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1164:                return(0);
                   1165:        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                   1166:                return(0);
                   1167:
                   1168:        m->flags &= ~MDOC_FREECOL;
1.1       kristaps 1169:        return(1);
                   1170: }
                   1171:
                   1172:
                   1173: static int
                   1174: blk_part_imp(MACRO_PROT_ARGS)
                   1175: {
1.63      kristaps 1176:        int               la, nl;
1.53      kristaps 1177:        enum mdoct        ntok;
1.55      kristaps 1178:        enum margserr     ac;
1.1       kristaps 1179:        char             *p;
1.43      kristaps 1180:        struct mdoc_node *blk; /* saved block context */
                   1181:        struct mdoc_node *body; /* saved body context */
                   1182:        struct mdoc_node *n;
1.1       kristaps 1183:
1.63      kristaps 1184:        nl = MDOC_NEWLINE & m->flags;
                   1185:
1.43      kristaps 1186:        /*
                   1187:         * A macro that spans to the end of the line.  This is generally
                   1188:         * (but not necessarily) called as the first macro.  The block
                   1189:         * has a head as the immediate child, which is always empty,
                   1190:         * followed by zero or more opening punctuation nodes, then the
                   1191:         * body (which may be empty, depending on the macro), then zero
                   1192:         * or more closing punctuation nodes.
                   1193:         */
1.32      kristaps 1194:
                   1195:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1196:                return(0);
1.43      kristaps 1197:
1.32      kristaps 1198:        blk = m->last;
1.43      kristaps 1199:
1.32      kristaps 1200:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1201:                return(0);
1.32      kristaps 1202:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1203:                return(0);
                   1204:
1.43      kristaps 1205:        /*
                   1206:         * Open the body scope "on-demand", that is, after we've
                   1207:         * processed all our the leading delimiters (open parenthesis,
                   1208:         * etc.).
                   1209:         */
1.1       kristaps 1210:
1.43      kristaps 1211:        for (body = NULL; ; ) {
1.32      kristaps 1212:                la = *pos;
1.53      kristaps 1213:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.43      kristaps 1214:
1.53      kristaps 1215:                if (ARGS_ERROR == ac)
1.1       kristaps 1216:                        return(0);
1.53      kristaps 1217:                if (ARGS_EOLN == ac)
1.43      kristaps 1218:                        break;
1.53      kristaps 1219:                if (ARGS_PUNCT == ac)
1.1       kristaps 1220:                        break;
                   1221:
1.53      kristaps 1222:                if (NULL == body && ARGS_QWORD != ac &&
1.67      schwarze 1223:                    DELIM_OPEN == mdoc_isdelim(p)) {
1.32      kristaps 1224:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1225:                                return(0);
                   1226:                        continue;
                   1227:                }
                   1228:
1.43      kristaps 1229:                if (NULL == body) {
                   1230:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1231:                               return(0);
                   1232:                        body = m->last;
                   1233:                }
                   1234:
1.54      kristaps 1235:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1236:
                   1237:                if (MDOC_MAX == ntok) {
1.53      kristaps 1238:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.43      kristaps 1239:                                return(0);
1.53      kristaps 1240:                        continue;
                   1241:                }
1.43      kristaps 1242:
1.82      kristaps 1243:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1244:                        return(0);
1.53      kristaps 1245:                break;
1.1       kristaps 1246:        }
                   1247:
1.43      kristaps 1248:        /* Clean-ups to leave in a consistent state. */
                   1249:
1.44      kristaps 1250:        if (NULL == body) {
                   1251:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1252:                        return(0);
                   1253:                body = m->last;
1.64      kristaps 1254:        }
                   1255:
                   1256:        for (n = body->child; n && n->next; n = n->next)
                   1257:                /* Do nothing. */ ;
                   1258:
                   1259:        /*
                   1260:         * End of sentence spacing: if the last node is a text node and
                   1261:         * has a trailing period, then mark it as being end-of-sentence.
                   1262:         */
                   1263:
                   1264:        if (n && MDOC_TEXT == n->type && n->string)
                   1265:                if (mandoc_eos(n->string, strlen(n->string)))
                   1266:                        n->flags |= MDOC_EOS;
                   1267:
                   1268:        /* Up-propogate the end-of-space flag. */
                   1269:
                   1270:        if (n && (MDOC_EOS & n->flags)) {
                   1271:                body->flags |= MDOC_EOS;
                   1272:                body->parent->flags |= MDOC_EOS;
1.44      kristaps 1273:        }
1.43      kristaps 1274:
1.83      schwarze 1275:        /*
                   1276:         * If there is an open sub-block requiring explicit close-out,
                   1277:         * postpone closing out the current block
                   1278:         * until the rew_sub() call closing out the sub-block.
                   1279:         */
                   1280:        for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
                   1281:                if (MDOC_BLOCK == n->type &&
                   1282:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1283:                    ! (MDOC_VALID & n->flags)) {
                   1284:                        assert( ! (MDOC_ACTED & n->flags));
                   1285:                        if ( ! make_pending(n, tok, m, line, ppos))
                   1286:                                return(0);
                   1287:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                   1288:                            tok, body, ENDBODY_NOSPACE))
                   1289:                                return(0);
                   1290:                        return(1);
                   1291:                }
                   1292:        }
                   1293:
1.32      kristaps 1294:        /*
                   1295:         * If we can't rewind to our body, then our scope has already
                   1296:         * been closed by another macro (like `Oc' closing `Op').  This
                   1297:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.47      kristaps 1298:         * crufty use of `Op' breakage.
1.1       kristaps 1299:         */
1.88    ! schwarze 1300:        if (n != body && ! mdoc_vmsg(m, MANDOCERR_SCOPENEST,
        !          1301:            line, ppos, "%s broken", mdoc_macronames[tok]))
1.32      kristaps 1302:                return(0);
1.43      kristaps 1303:
1.83      schwarze 1304:        if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.32      kristaps 1305:                return(0);
1.1       kristaps 1306:
1.32      kristaps 1307:        /* Standard appending of delimiters. */
1.1       kristaps 1308:
1.63      kristaps 1309:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1310:                return(0);
                   1311:
1.32      kristaps 1312:        /* Rewind scope, if applicable. */
1.1       kristaps 1313:
1.83      schwarze 1314:        if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1315:                return(0);
                   1316:
                   1317:        return(1);
                   1318: }
                   1319:
                   1320:
                   1321: static int
                   1322: blk_part_exp(MACRO_PROT_ARGS)
                   1323: {
1.60      kristaps 1324:        int               la, nl;
1.55      kristaps 1325:        enum margserr     ac;
1.43      kristaps 1326:        struct mdoc_node *head; /* keep track of head */
                   1327:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1328:        char             *p;
1.53      kristaps 1329:        enum mdoct        ntok;
1.1       kristaps 1330:
1.60      kristaps 1331:        nl = MDOC_NEWLINE & m->flags;
                   1332:
1.43      kristaps 1333:        /*
                   1334:         * The opening of an explicit macro having zero or more leading
                   1335:         * punctuation nodes; a head with optional single element (the
                   1336:         * case of `Eo'); and a body that may be empty.
                   1337:         */
1.32      kristaps 1338:
                   1339:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1340:                return(0);
1.32      kristaps 1341:
1.43      kristaps 1342:        for (head = body = NULL; ; ) {
1.32      kristaps 1343:                la = *pos;
1.53      kristaps 1344:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1345:
1.53      kristaps 1346:                if (ARGS_ERROR == ac)
1.1       kristaps 1347:                        return(0);
1.53      kristaps 1348:                if (ARGS_PUNCT == ac)
1.1       kristaps 1349:                        break;
1.53      kristaps 1350:                if (ARGS_EOLN == ac)
1.1       kristaps 1351:                        break;
1.43      kristaps 1352:
                   1353:                /* Flush out leading punctuation. */
                   1354:
1.53      kristaps 1355:                if (NULL == head && ARGS_QWORD != ac &&
1.67      schwarze 1356:                    DELIM_OPEN == mdoc_isdelim(p)) {
1.43      kristaps 1357:                        assert(NULL == body);
                   1358:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1359:                                return(0);
                   1360:                        continue;
                   1361:                }
                   1362:
                   1363:                if (NULL == head) {
                   1364:                        assert(NULL == body);
                   1365:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1366:                                return(0);
1.43      kristaps 1367:                        head = m->last;
1.1       kristaps 1368:                }
                   1369:
1.43      kristaps 1370:                /*
                   1371:                 * `Eo' gobbles any data into the head, but most other
                   1372:                 * macros just immediately close out and begin the body.
                   1373:                 */
                   1374:
                   1375:                if (NULL == body) {
                   1376:                        assert(head);
                   1377:                        /* No check whether it's a macro! */
                   1378:                        if (MDOC_Eo == tok)
                   1379:                                if ( ! mdoc_word_alloc(m, line, la, p))
                   1380:                                        return(0);
                   1381:
1.32      kristaps 1382:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1383:                                return(0);
1.32      kristaps 1384:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1385:                                return(0);
1.43      kristaps 1386:                        body = m->last;
                   1387:
                   1388:                        if (MDOC_Eo == tok)
                   1389:                                continue;
1.1       kristaps 1390:                }
1.43      kristaps 1391:
                   1392:                assert(NULL != head && NULL != body);
                   1393:
1.54      kristaps 1394:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1395:
                   1396:                if (MDOC_MAX == ntok) {
1.53      kristaps 1397:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.43      kristaps 1398:                                return(0);
1.53      kristaps 1399:                        continue;
1.43      kristaps 1400:                }
                   1401:
1.82      kristaps 1402:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1403:                        return(0);
1.53      kristaps 1404:                break;
1.1       kristaps 1405:        }
                   1406:
1.43      kristaps 1407:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1408:
1.43      kristaps 1409:        if (NULL == head) {
                   1410:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1411:                        return(0);
                   1412:                head = m->last;
                   1413:        }
                   1414:
                   1415:        if (NULL == body) {
1.32      kristaps 1416:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1417:                        return(0);
1.32      kristaps 1418:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1419:                        return(0);
1.43      kristaps 1420:                body = m->last;
1.1       kristaps 1421:        }
                   1422:
1.32      kristaps 1423:        /* Standard appending of delimiters. */
                   1424:
1.60      kristaps 1425:        if ( ! nl)
1.1       kristaps 1426:                return(1);
1.32      kristaps 1427:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1428: }
                   1429:
                   1430:
1.61      kristaps 1431: /* ARGSUSED */
1.1       kristaps 1432: static int
                   1433: in_line_argn(MACRO_PROT_ARGS)
                   1434: {
1.60      kristaps 1435:        int              la, flushed, j, maxargs, nl;
1.55      kristaps 1436:        enum margserr    ac;
1.56      kristaps 1437:        enum margverr    av;
1.53      kristaps 1438:        struct mdoc_arg *arg;
                   1439:        char            *p;
                   1440:        enum mdoct       ntok;
1.1       kristaps 1441:
1.60      kristaps 1442:        nl = MDOC_NEWLINE & m->flags;
                   1443:
1.46      kristaps 1444:        /*
                   1445:         * A line macro that has a fixed number of arguments (maxargs).
                   1446:         * Only open the scope once the first non-leading-punctuation is
                   1447:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1448:         * keep it open until the maximum number of arguments are
                   1449:         * exhausted.
                   1450:         */
1.1       kristaps 1451:
                   1452:        switch (tok) {
                   1453:        case (MDOC_Ap):
                   1454:                /* FALLTHROUGH */
                   1455:        case (MDOC_No):
                   1456:                /* FALLTHROUGH */
                   1457:        case (MDOC_Ns):
                   1458:                /* FALLTHROUGH */
                   1459:        case (MDOC_Ux):
                   1460:                maxargs = 0;
                   1461:                break;
1.42      kristaps 1462:        case (MDOC_Xr):
                   1463:                maxargs = 2;
                   1464:                break;
1.1       kristaps 1465:        default:
                   1466:                maxargs = 1;
                   1467:                break;
                   1468:        }
                   1469:
1.46      kristaps 1470:        for (arg = NULL; ; ) {
1.32      kristaps 1471:                la = *pos;
1.56      kristaps 1472:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1473:
1.56      kristaps 1474:                if (ARGV_WORD == av) {
1.32      kristaps 1475:                        *pos = la;
1.1       kristaps 1476:                        break;
                   1477:                }
                   1478:
1.56      kristaps 1479:                if (ARGV_EOLN == av)
1.1       kristaps 1480:                        break;
1.56      kristaps 1481:                if (ARGV_ARG == av)
1.1       kristaps 1482:                        continue;
                   1483:
                   1484:                mdoc_argv_free(arg);
                   1485:                return(0);
                   1486:        }
                   1487:
1.46      kristaps 1488:        for (flushed = j = 0; ; ) {
1.32      kristaps 1489:                la = *pos;
1.53      kristaps 1490:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1491:
1.53      kristaps 1492:                if (ARGS_ERROR == ac)
1.1       kristaps 1493:                        return(0);
1.53      kristaps 1494:                if (ARGS_PUNCT == ac)
1.1       kristaps 1495:                        break;
1.53      kristaps 1496:                if (ARGS_EOLN == ac)
1.1       kristaps 1497:                        break;
                   1498:
1.46      kristaps 1499:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.53      kristaps 1500:                                ARGS_QWORD != ac &&
1.67      schwarze 1501:                                0 == j && DELIM_OPEN == mdoc_isdelim(p)) {
1.46      kristaps 1502:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1503:                                return(0);
                   1504:                        continue;
                   1505:                } else if (0 == j)
                   1506:                       if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1507:                               return(0);
                   1508:
                   1509:                if (j == maxargs && ! flushed) {
                   1510:                        if ( ! rew_elem(m, tok))
                   1511:                                return(0);
                   1512:                        flushed = 1;
                   1513:                }
                   1514:
1.54      kristaps 1515:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1516:
                   1517:                if (MDOC_MAX != ntok) {
1.32      kristaps 1518:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1519:                                return(0);
                   1520:                        flushed = 1;
1.82      kristaps 1521:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1522:                                return(0);
1.46      kristaps 1523:                        j++;
1.1       kristaps 1524:                        break;
                   1525:                }
                   1526:
                   1527:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.53      kristaps 1528:                                ARGS_QWORD != ac &&
1.67      schwarze 1529:                                ! flushed &&
                   1530:                                DELIM_NONE != mdoc_isdelim(p)) {
1.32      kristaps 1531:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1532:                                return(0);
                   1533:                        flushed = 1;
                   1534:                }
1.42      kristaps 1535:
                   1536:                /*
                   1537:                 * XXX: this is a hack to work around groff's ugliness
                   1538:                 * as regards `Xr' and extraneous arguments.  It should
                   1539:                 * ideally be deprecated behaviour, but because this is
                   1540:                 * code is no here, it's unlikely to be removed.
                   1541:                 */
1.43      kristaps 1542:
1.46      kristaps 1543: #ifdef __OpenBSD__
1.42      kristaps 1544:                if (MDOC_Xr == tok && j == maxargs) {
1.46      kristaps 1545:                        if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1.42      kristaps 1546:                                return(0);
                   1547:                        if ( ! rew_elem(m, MDOC_Ns))
                   1548:                                return(0);
                   1549:                }
1.46      kristaps 1550: #endif
1.42      kristaps 1551:
1.32      kristaps 1552:                if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1553:                        return(0);
1.46      kristaps 1554:                j++;
1.1       kristaps 1555:        }
                   1556:
1.46      kristaps 1557:        if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1558:               return(0);
                   1559:
                   1560:        /* Close out in a consistent state. */
1.32      kristaps 1561:
                   1562:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1563:                return(0);
1.60      kristaps 1564:        if ( ! nl)
1.1       kristaps 1565:                return(1);
1.32      kristaps 1566:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1567: }
                   1568:
                   1569:
                   1570: static int
                   1571: in_line_eoln(MACRO_PROT_ARGS)
                   1572: {
1.56      kristaps 1573:        int              la;
1.55      kristaps 1574:        enum margserr    ac;
1.56      kristaps 1575:        enum margverr    av;
1.53      kristaps 1576:        struct mdoc_arg *arg;
                   1577:        char            *p;
                   1578:        enum mdoct       ntok;
1.1       kristaps 1579:
                   1580:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1581:
1.32      kristaps 1582:        /* Parse macro arguments. */
1.1       kristaps 1583:
1.32      kristaps 1584:        for (arg = NULL; ; ) {
1.1       kristaps 1585:                la = *pos;
1.56      kristaps 1586:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1587:
1.56      kristaps 1588:                if (ARGV_WORD == av) {
1.1       kristaps 1589:                        *pos = la;
                   1590:                        break;
                   1591:                }
1.56      kristaps 1592:                if (ARGV_EOLN == av)
1.1       kristaps 1593:                        break;
1.56      kristaps 1594:                if (ARGV_ARG == av)
1.1       kristaps 1595:                        continue;
                   1596:
                   1597:                mdoc_argv_free(arg);
                   1598:                return(0);
                   1599:        }
                   1600:
1.32      kristaps 1601:        /* Open element scope. */
                   1602:
                   1603:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1604:                return(0);
                   1605:
1.32      kristaps 1606:        /* Parse argument terms. */
1.1       kristaps 1607:
                   1608:        for (;;) {
                   1609:                la = *pos;
1.53      kristaps 1610:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1611:
1.53      kristaps 1612:                if (ARGS_ERROR == ac)
1.1       kristaps 1613:                        return(0);
1.53      kristaps 1614:                if (ARGS_EOLN == ac)
1.1       kristaps 1615:                        break;
                   1616:
1.53      kristaps 1617:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1618:
1.53      kristaps 1619:                if (MDOC_MAX == ntok) {
                   1620:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1621:                                return(0);
1.53      kristaps 1622:                        continue;
                   1623:                }
1.1       kristaps 1624:
1.53      kristaps 1625:                if ( ! rew_elem(m, tok))
1.1       kristaps 1626:                        return(0);
1.82      kristaps 1627:                return(mdoc_macro(m, ntok, line, la, pos, buf));
1.1       kristaps 1628:        }
                   1629:
1.32      kristaps 1630:        /* Close out (no delimiters). */
                   1631:
                   1632:        return(rew_elem(m, tok));
1.1       kristaps 1633: }
                   1634:
                   1635:
                   1636: /* ARGSUSED */
                   1637: static int
1.41      kristaps 1638: ctx_synopsis(MACRO_PROT_ARGS)
                   1639: {
1.60      kristaps 1640:        int              nl;
                   1641:
                   1642:        nl = MDOC_NEWLINE & m->flags;
1.41      kristaps 1643:
                   1644:        /* If we're not in the SYNOPSIS, go straight to in-line. */
                   1645:        if (SEC_SYNOPSIS != m->lastsec)
1.82      kristaps 1646:                return(in_line(m, tok, line, ppos, pos, buf));
1.41      kristaps 1647:
                   1648:        /* If we're a nested call, same place. */
1.60      kristaps 1649:        if ( ! nl)
1.82      kristaps 1650:                return(in_line(m, tok, line, ppos, pos, buf));
1.41      kristaps 1651:
                   1652:        /*
                   1653:         * XXX: this will open a block scope; however, if later we end
                   1654:         * up formatting the block scope, then child nodes will inherit
                   1655:         * the formatting.  Be careful.
                   1656:         */
1.88    ! schwarze 1657:        if (MDOC_Nm == tok)
        !          1658:                return(blk_full(m, tok, line, ppos, pos, buf));
        !          1659:        assert(MDOC_Vt == tok);
1.82      kristaps 1660:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
1.41      kristaps 1661: }
                   1662:
                   1663:
                   1664: /* ARGSUSED */
                   1665: static int
1.1       kristaps 1666: obsolete(MACRO_PROT_ARGS)
                   1667: {
                   1668:
1.68      kristaps 1669:        return(mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS));
1.1       kristaps 1670: }
                   1671:
                   1672:
1.24      kristaps 1673: /*
                   1674:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1675:  * They're unusual because they're basically free-form text until a
                   1676:  * macro is encountered.
                   1677:  */
1.1       kristaps 1678: static int
1.82      kristaps 1679: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1680: {
1.53      kristaps 1681:        int              la, pos;
1.75      kristaps 1682:        enum margserr    ac;
1.53      kristaps 1683:        enum mdoct       ntok;
                   1684:        char            *p;
1.1       kristaps 1685:
1.24      kristaps 1686:        for (pos = ppos; ; ) {
                   1687:                la = pos;
1.1       kristaps 1688:
1.75      kristaps 1689:                ac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1       kristaps 1690:
1.75      kristaps 1691:                if (ARGS_ERROR == ac)
1.24      kristaps 1692:                        return(0);
1.75      kristaps 1693:                if (ARGS_EOLN == ac)
1.24      kristaps 1694:                        break;
1.1       kristaps 1695:
1.75      kristaps 1696:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1697:
1.53      kristaps 1698:                if (MDOC_MAX == ntok) {
                   1699:                        if ( ! mdoc_word_alloc(m, line, la, p))
1.1       kristaps 1700:                                return(0);
1.53      kristaps 1701:                        continue;
                   1702:                }
1.1       kristaps 1703:
1.82      kristaps 1704:                if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1       kristaps 1705:                        return(0);
1.53      kristaps 1706:                return(append_delims(m, line, &pos, buf));
1.1       kristaps 1707:        }
                   1708:
                   1709:        return(1);
                   1710: }
1.24      kristaps 1711:
                   1712:
1.79      kristaps 1713: /* ARGSUSED */
1.75      kristaps 1714: static int
                   1715: phrase_ta(MACRO_PROT_ARGS)
                   1716: {
                   1717:        int               la;
                   1718:        enum mdoct        ntok;
                   1719:        enum margserr     ac;
                   1720:        char             *p;
                   1721:
                   1722:        /*
                   1723:         * FIXME: this is overly restrictive: if the `Ta' is unexpected,
                   1724:         * it should simply error out with ARGSLOST.
                   1725:         */
                   1726:
                   1727:        if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
                   1728:                return(0);
                   1729:        if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
                   1730:                return(0);
                   1731:
                   1732:        for (;;) {
                   1733:                la = *pos;
                   1734:                ac = mdoc_zargs(m, line, pos, buf, 0, &p);
                   1735:
                   1736:                if (ARGS_ERROR == ac)
                   1737:                        return(0);
                   1738:                if (ARGS_EOLN == ac)
                   1739:                        break;
                   1740:
                   1741:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
                   1742:
                   1743:                if (MDOC_MAX == ntok) {
                   1744:                        if ( ! mdoc_word_alloc(m, line, la, p))
                   1745:                                return(0);
                   1746:                        continue;
                   1747:                }
                   1748:
1.82      kristaps 1749:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.75      kristaps 1750:                        return(0);
                   1751:                return(append_delims(m, line, pos, buf));
                   1752:        }
                   1753:
                   1754:        return(1);
                   1755: }

CVSweb