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

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

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