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

Annotation of mandoc/mdoc_macro.c, Revision 1.84

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

CVSweb