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

Annotation of mandoc/mdoc_macro.c, Revision 1.105

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

CVSweb