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

Annotation of mandoc/mdoc_macro.c, Revision 1.114

1.114   ! kristaps    1: /*     $Id: mdoc_macro.c,v 1.113 2011/12/03 23:59:14 schwarze Exp $ */
1.1       kristaps    2: /*
1.111     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.92      schwarze    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.106     kristaps   29: #include "mdoc.h"
1.68      kristaps   30: #include "mandoc.h"
1.1       kristaps   31: #include "libmdoc.h"
1.64      kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
1.84      schwarze   34: enum   rew {   /* see rew_dohalt() */
                     35:        REWIND_NONE,
                     36:        REWIND_THIS,
                     37:        REWIND_MORE,
1.98      kristaps   38:        REWIND_FORCE,
1.84      schwarze   39:        REWIND_LATER,
1.91      kristaps   40:        REWIND_ERROR
1.51      kristaps   41: };
1.1       kristaps   42:
1.53      kristaps   43: static int             blk_full(MACRO_PROT_ARGS);
                     44: static int             blk_exp_close(MACRO_PROT_ARGS);
                     45: static int             blk_part_exp(MACRO_PROT_ARGS);
                     46: static int             blk_part_imp(MACRO_PROT_ARGS);
                     47: static int             ctx_synopsis(MACRO_PROT_ARGS);
                     48: static int             in_line_eoln(MACRO_PROT_ARGS);
                     49: static int             in_line_argn(MACRO_PROT_ARGS);
                     50: static int             in_line(MACRO_PROT_ARGS);
                     51: static int             obsolete(MACRO_PROT_ARGS);
1.75      kristaps   52: static int             phrase_ta(MACRO_PROT_ARGS);
1.53      kristaps   53:
1.105     kristaps   54: static int             dword(struct mdoc *, int, int,
                     55:                                const char *, enum mdelim);
1.53      kristaps   56: static int             append_delims(struct mdoc *,
                     57:                                int, int *, char *);
1.58      kristaps   58: static enum mdoct      lookup(enum mdoct, const char *);
1.53      kristaps   59: static enum mdoct      lookup_raw(const char *);
1.85      schwarze   60: static int             make_pending(struct mdoc_node *, enum mdoct,
1.83      schwarze   61:                                struct mdoc *, int, int);
1.82      kristaps   62: static int             phrase(struct mdoc *, int, int, char *);
1.53      kristaps   63: static enum mdoct      rew_alt(enum mdoct);
                     64: static enum rew        rew_dohalt(enum mdoct, enum mdoc_type,
                     65:                                const struct mdoc_node *);
                     66: static int             rew_elem(struct mdoc *, enum mdoct);
                     67: static int             rew_last(struct mdoc *,
                     68:                                const struct mdoc_node *);
                     69: static int             rew_sub(enum mdoc_type, struct mdoc *,
                     70:                                enum mdoct, int, int);
1.1       kristaps   71:
                     72: const  struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.13      kristaps   73:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
1.1       kristaps   74:        { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
                     75:        { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
                     76:        { in_line_eoln, MDOC_PROLOGUE }, /* Os */
1.110     kristaps   77:        { blk_full, MDOC_PARSED }, /* Sh */
                     78:        { blk_full, MDOC_PARSED }, /* Ss */
1.21      kristaps   79:        { in_line_eoln, 0 }, /* Pp */
1.1       kristaps   80:        { blk_part_imp, MDOC_PARSED }, /* D1 */
                     81:        { blk_part_imp, MDOC_PARSED }, /* Dl */
                     82:        { blk_full, MDOC_EXPLICIT }, /* Bd */
                     83:        { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
                     84:        { blk_full, MDOC_EXPLICIT }, /* Bl */
                     85:        { blk_exp_close, MDOC_EXPLICIT }, /* El */
                     86:        { blk_full, MDOC_PARSED }, /* It */
                     87:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.10      kristaps   88:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
1.1       kristaps   89:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.23      kristaps   90:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
1.1       kristaps   91:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
                     92:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
                     93:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
                     94:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
                     95:        { in_line_eoln, 0 }, /* Ex */
                     96:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
                     97:        { in_line_eoln, 0 }, /* Fd */
                     98:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
                     99:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
1.11      kristaps  100:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.1       kristaps  101:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.18      kristaps  102:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.1       kristaps  103:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.19      kristaps  104:        { blk_full, 0 }, /* Nd */
1.88      schwarze  105:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1       kristaps  106:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
                    107:        { obsolete, 0 }, /* Ot */
                    108:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
                    109:        { in_line_eoln, 0 }, /* Rv */
                    110:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
                    111:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.41      kristaps  112:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
1.42      kristaps  113:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.1       kristaps  114:        { in_line_eoln, 0 }, /* %A */
                    115:        { in_line_eoln, 0 }, /* %B */
                    116:        { in_line_eoln, 0 }, /* %D */
                    117:        { in_line_eoln, 0 }, /* %I */
                    118:        { in_line_eoln, 0 }, /* %J */
                    119:        { in_line_eoln, 0 }, /* %N */
                    120:        { in_line_eoln, 0 }, /* %O */
                    121:        { in_line_eoln, 0 }, /* %P */
                    122:        { in_line_eoln, 0 }, /* %R */
                    123:        { in_line_eoln, 0 }, /* %T */
                    124:        { in_line_eoln, 0 }, /* %V */
                    125:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
                    126:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
                    127:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
                    128:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
                    129:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
                    130:        { blk_full, MDOC_EXPLICIT }, /* Bf */
                    131:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
                    132:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
                    133:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
                    134:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
                    135:        { in_line_eoln, 0 }, /* Db */
                    136:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
                    137:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
                    138:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
                    139:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
                    140:        { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
                    141:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
                    142:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
                    143:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11      kristaps  144:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.95      schwarze  145:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* No */
                    146:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Ns */
1.1       kristaps  147:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
                    148:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
                    149:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
1.52      kristaps  150:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
1.1       kristaps  151:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
                    152:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
                    153:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
                    154:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
                    155:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
                    156:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
                    157:        { blk_exp_close, MDOC_EXPLICIT }, /* Re */
                    158:        { blk_full, MDOC_EXPLICIT }, /* Rs */
                    159:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
                    160:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
                    161:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
                    162:        { in_line_eoln, 0 }, /* Sm */
                    163:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
                    164:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
                    165:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
                    166:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
                    167:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
                    168:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
                    169:        { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
                    170:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
                    171:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
                    172:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
                    173:        { blk_full, MDOC_EXPLICIT }, /* Bk */
                    174:        { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
                    175:        { in_line_eoln, 0 }, /* Bt */
                    176:        { in_line_eoln, 0 }, /* Hf */
                    177:        { obsolete, 0 }, /* Fr */
                    178:        { in_line_eoln, 0 }, /* Ud */
1.69      kristaps  179:        { in_line, 0 }, /* Lb */
1.21      kristaps  180:        { in_line_eoln, 0 }, /* Lp */
1.12      kristaps  181:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
                    182:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
1.1       kristaps  183:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
                    184:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
                    185:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
                    186:        { in_line_eoln, 0 }, /* %C */
                    187:        { obsolete, 0 }, /* Es */
                    188:        { obsolete, 0 }, /* En */
                    189:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
                    190:        { in_line_eoln, 0 }, /* %Q */
1.20      kristaps  191:        { in_line_eoln, 0 }, /* br */
                    192:        { in_line_eoln, 0 }, /* sp */
1.37      kristaps  193:        { in_line_eoln, 0 }, /* %U */
1.75      kristaps  194:        { phrase_ta, MDOC_CALLABLE | MDOC_PARSED }, /* Ta */
1.1       kristaps  195: };
                    196:
                    197: const  struct mdoc_macro * const mdoc_macros = __mdoc_macros;
                    198:
                    199:
                    200: /*
                    201:  * This is called at the end of parsing.  It must traverse up the tree,
                    202:  * closing out open [implicit] scopes.  Obviously, open explicit scopes
                    203:  * are errors.
                    204:  */
                    205: int
1.32      kristaps  206: mdoc_macroend(struct mdoc *m)
1.1       kristaps  207: {
                    208:        struct mdoc_node *n;
                    209:
                    210:        /* Scan for open explicit scopes. */
                    211:
1.32      kristaps  212:        n = MDOC_VALID & m->last->flags ?  m->last->parent : m->last;
1.1       kristaps  213:
1.94      schwarze  214:        for ( ; n; n = n->parent)
                    215:                if (MDOC_BLOCK == n->type &&
                    216:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
                    217:                        mdoc_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.1       kristaps  218:
1.32      kristaps  219:        /* Rewind to the first. */
                    220:
                    221:        return(rew_last(m, m->first));
1.1       kristaps  222: }
                    223:
1.32      kristaps  224:
                    225: /*
                    226:  * Look up a macro from within a subsequent context.
                    227:  */
1.53      kristaps  228: static enum mdoct
1.58      kristaps  229: lookup(enum mdoct from, const char *p)
1.28      kristaps  230: {
                    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;
1.108     schwarze  602:
                    603:        /*
                    604:         * Closing delimiters only suppress the preceding space
                    605:         * when they follow something, not when they start a new
                    606:         * block or element, and not when they follow `No'.
                    607:         *
                    608:         * XXX  Explicitly special-casing MDOC_No here feels
                    609:         *      like a layering violation.  Find a better way
                    610:         *      and solve this in the code related to `No'!
                    611:         */
                    612:
                    613:        else if (DELIM_CLOSE == d && m->last->prev &&
                    614:                        m->last->prev->tok != MDOC_No)
1.105     kristaps  615:                m->last->flags |= MDOC_DELIMC;
                    616:
                    617:        return(1);
                    618: }
1.1       kristaps  619:
                    620: static int
1.66      kristaps  621: append_delims(struct mdoc *m, int line, int *pos, char *buf)
1.1       kristaps  622: {
1.66      kristaps  623:        int              la;
1.55      kristaps  624:        enum margserr    ac;
1.1       kristaps  625:        char            *p;
                    626:
1.66      kristaps  627:        if ('\0' == buf[*pos])
1.1       kristaps  628:                return(1);
                    629:
                    630:        for (;;) {
1.66      kristaps  631:                la = *pos;
1.107     kristaps  632:                ac = mdoc_zargs(m, line, pos, buf, &p);
1.1       kristaps  633:
1.53      kristaps  634:                if (ARGS_ERROR == ac)
1.1       kristaps  635:                        return(0);
1.53      kristaps  636:                else if (ARGS_EOLN == ac)
1.1       kristaps  637:                        break;
1.66      kristaps  638:
1.105     kristaps  639:                dword(m, line, la, p, DELIM_MAX);
1.66      kristaps  640:
                    641:                /*
                    642:                 * If we encounter end-of-sentence symbols, then trigger
                    643:                 * the double-space.
                    644:                 *
1.109     kristaps  645:                 * XXX: it's easy to allow this to propagate outward to
1.66      kristaps  646:                 * the last symbol, such that `. )' will cause the
                    647:                 * correct double-spacing.  However, (1) groff isn't
                    648:                 * smart enough to do this and (2) it would require
                    649:                 * knowing which symbols break this behaviour, for
1.109     kristaps  650:                 * example, `.  ;' shouldn't propagate the double-space.
1.66      kristaps  651:                 */
1.93      schwarze  652:                if (mandoc_eos(p, strlen(p), 0))
1.66      kristaps  653:                        m->last->flags |= MDOC_EOS;
1.1       kristaps  654:        }
                    655:
                    656:        return(1);
                    657: }
                    658:
                    659:
                    660: /*
                    661:  * Close out block partial/full explicit.
                    662:  */
                    663: static int
                    664: blk_exp_close(MACRO_PROT_ARGS)
                    665: {
1.83      schwarze  666:        struct mdoc_node *body;         /* Our own body. */
                    667:        struct mdoc_node *later;        /* A sub-block starting later. */
                    668:        struct mdoc_node *n;            /* For searching backwards. */
                    669:
1.60      kristaps  670:        int              j, lastarg, maxargs, flushed, nl;
1.55      kristaps  671:        enum margserr    ac;
1.83      schwarze  672:        enum mdoct       atok, ntok;
1.1       kristaps  673:        char            *p;
                    674:
1.60      kristaps  675:        nl = MDOC_NEWLINE & m->flags;
                    676:
1.1       kristaps  677:        switch (tok) {
                    678:        case (MDOC_Ec):
                    679:                maxargs = 1;
                    680:                break;
                    681:        default:
                    682:                maxargs = 0;
                    683:                break;
                    684:        }
                    685:
1.83      schwarze  686:        /*
                    687:         * Search backwards for beginnings of blocks,
                    688:         * both of our own and of pending sub-blocks.
                    689:         */
                    690:        atok = rew_alt(tok);
                    691:        body = later = NULL;
                    692:        for (n = m->last; n; n = n->parent) {
                    693:                if (MDOC_VALID & n->flags)
                    694:                        continue;
                    695:
                    696:                /* Remember the start of our own body. */
                    697:                if (MDOC_BODY == n->type && atok == n->tok) {
1.87      kristaps  698:                        if (ENDBODY_NOT == n->end)
1.83      schwarze  699:                                body = n;
                    700:                        continue;
                    701:                }
                    702:
1.88      schwarze  703:                if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.83      schwarze  704:                        continue;
                    705:                if (atok == n->tok) {
                    706:                        assert(body);
                    707:
                    708:                        /*
                    709:                         * Found the start of our own block.
                    710:                         * When there is no pending sub block,
                    711:                         * just proceed to closing out.
                    712:                         */
                    713:                        if (NULL == later)
                    714:                                break;
                    715:
                    716:                        /*
                    717:                         * When there is a pending sub block,
                    718:                         * postpone closing out the current block
                    719:                         * until the rew_sub() closing out the sub-block.
                    720:                         */
1.98      kristaps  721:                        make_pending(later, tok, m, line, ppos);
1.83      schwarze  722:
                    723:                        /*
                    724:                         * Mark the place where the formatting - but not
                    725:                         * the scope - of the current block ends.
                    726:                         */
                    727:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                    728:                            atok, body, ENDBODY_SPACE))
                    729:                                return(0);
                    730:                        break;
                    731:                }
                    732:
                    733:                /*
                    734:                 * When finding an open sub block, remember the last
                    735:                 * open explicit block, or, in case there are only
                    736:                 * implicit ones, the first open implicit block.
                    737:                 */
                    738:                if (later &&
                    739:                    MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
                    740:                        continue;
1.97      kristaps  741:                if (MDOC_CALLABLE & mdoc_macros[n->tok].flags)
1.83      schwarze  742:                        later = n;
                    743:        }
                    744:
1.1       kristaps  745:        if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.68      kristaps  746:                /* FIXME: do this in validate */
1.22      kristaps  747:                if (buf[*pos])
1.103     kristaps  748:                        mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST);
1.22      kristaps  749:
1.32      kristaps  750:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.22      kristaps  751:                        return(0);
1.32      kristaps  752:                return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
1.1       kristaps  753:        }
                    754:
1.32      kristaps  755:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps  756:                return(0);
                    757:
1.83      schwarze  758:        if (NULL == later && maxargs > 0)
1.32      kristaps  759:                if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
1.1       kristaps  760:                        return(0);
                    761:
1.27      kristaps  762:        for (flushed = j = 0; ; j++) {
1.1       kristaps  763:                lastarg = *pos;
                    764:
                    765:                if (j == maxargs && ! flushed) {
1.32      kristaps  766:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  767:                                return(0);
                    768:                        flushed = 1;
                    769:                }
                    770:
1.53      kristaps  771:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  772:
1.53      kristaps  773:                if (ARGS_ERROR == ac)
1.1       kristaps  774:                        return(0);
1.53      kristaps  775:                if (ARGS_PUNCT == ac)
1.1       kristaps  776:                        break;
1.53      kristaps  777:                if (ARGS_EOLN == ac)
1.1       kristaps  778:                        break;
                    779:
1.54      kristaps  780:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                    781:
                    782:                if (MDOC_MAX == ntok) {
1.105     kristaps  783:                        if ( ! dword(m, line, lastarg, p, DELIM_MAX))
1.1       kristaps  784:                                return(0);
1.54      kristaps  785:                        continue;
                    786:                }
1.1       kristaps  787:
1.54      kristaps  788:                if ( ! flushed) {
                    789:                        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                    790:                                return(0);
                    791:                        flushed = 1;
                    792:                }
1.82      kristaps  793:                if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
1.1       kristaps  794:                        return(0);
1.54      kristaps  795:                break;
1.1       kristaps  796:        }
                    797:
1.32      kristaps  798:        if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps  799:                return(0);
                    800:
1.60      kristaps  801:        if ( ! nl)
1.1       kristaps  802:                return(1);
1.32      kristaps  803:        return(append_delims(m, line, pos, buf));
1.1       kristaps  804: }
                    805:
                    806:
                    807: static int
                    808: in_line(MACRO_PROT_ARGS)
                    809: {
1.70      kristaps  810:        int              la, scope, cnt, nc, nl;
1.56      kristaps  811:        enum margverr    av;
1.53      kristaps  812:        enum mdoct       ntok;
1.55      kristaps  813:        enum margserr    ac;
1.67      schwarze  814:        enum mdelim      d;
1.53      kristaps  815:        struct mdoc_arg *arg;
                    816:        char            *p;
1.1       kristaps  817:
1.60      kristaps  818:        nl = MDOC_NEWLINE & m->flags;
                    819:
1.4       kristaps  820:        /*
                    821:         * Whether we allow ignored elements (those without content,
                    822:         * usually because of reserved words) to squeak by.
                    823:         */
1.45      kristaps  824:
1.4       kristaps  825:        switch (tok) {
1.26      kristaps  826:        case (MDOC_An):
                    827:                /* FALLTHROUGH */
                    828:        case (MDOC_Ar):
1.4       kristaps  829:                /* FALLTHROUGH */
                    830:        case (MDOC_Fl):
                    831:                /* FALLTHROUGH */
1.96      kristaps  832:        case (MDOC_Mt):
1.12      kristaps  833:                /* FALLTHROUGH */
1.26      kristaps  834:        case (MDOC_Nm):
                    835:                /* FALLTHROUGH */
1.25      kristaps  836:        case (MDOC_Pa):
1.4       kristaps  837:                nc = 1;
                    838:                break;
                    839:        default:
                    840:                nc = 0;
                    841:                break;
                    842:        }
                    843:
1.27      kristaps  844:        for (arg = NULL;; ) {
1.1       kristaps  845:                la = *pos;
1.56      kristaps  846:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps  847:
1.56      kristaps  848:                if (ARGV_WORD == av) {
1.1       kristaps  849:                        *pos = la;
                    850:                        break;
                    851:                }
1.56      kristaps  852:                if (ARGV_EOLN == av)
1.1       kristaps  853:                        break;
1.56      kristaps  854:                if (ARGV_ARG == av)
1.1       kristaps  855:                        continue;
                    856:
                    857:                mdoc_argv_free(arg);
                    858:                return(0);
                    859:        }
                    860:
1.70      kristaps  861:        for (cnt = scope = 0;; ) {
1.1       kristaps  862:                la = *pos;
1.53      kristaps  863:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps  864:
1.53      kristaps  865:                if (ARGS_ERROR == ac)
1.1       kristaps  866:                        return(0);
1.53      kristaps  867:                if (ARGS_EOLN == ac)
1.1       kristaps  868:                        break;
1.53      kristaps  869:                if (ARGS_PUNCT == ac)
1.1       kristaps  870:                        break;
                    871:
1.53      kristaps  872:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps  873:
1.4       kristaps  874:                /*
                    875:                 * In this case, we've located a submacro and must
                    876:                 * execute it.  Close out scope, if open.  If no
                    877:                 * elements have been generated, either create one (nc)
                    878:                 * or raise a warning.
                    879:                 */
1.1       kristaps  880:
1.53      kristaps  881:                if (MDOC_MAX != ntok) {
1.70      kristaps  882:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  883:                                return(0);
1.4       kristaps  884:                        if (nc && 0 == cnt) {
1.32      kristaps  885:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  886:                                        return(0);
1.32      kristaps  887:                                if ( ! rew_last(m, m->last))
1.12      kristaps  888:                                        return(0);
1.7       kristaps  889:                        } else if ( ! nc && 0 == cnt) {
                    890:                                mdoc_argv_free(arg);
1.103     kristaps  891:                                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.7       kristaps  892:                        }
1.103     kristaps  893:
1.82      kristaps  894:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps  895:                                return(0);
1.60      kristaps  896:                        if ( ! nl)
1.1       kristaps  897:                                return(1);
1.32      kristaps  898:                        return(append_delims(m, line, pos, buf));
1.29      kristaps  899:                }
1.1       kristaps  900:
1.4       kristaps  901:                /*
                    902:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    903:                 * a word; rewind the scope, if a delimiter; then append
                    904:                 * the word.
                    905:                 */
1.1       kristaps  906:
1.105     kristaps  907:                d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.3       kristaps  908:
1.70      kristaps  909:                if (DELIM_NONE != d) {
                    910:                        /*
                    911:                         * If we encounter closing punctuation, no word
                    912:                         * has been omitted, no scope is open, and we're
                    913:                         * allowed to have an empty element, then start
                    914:                         * a new scope.  `Ar', `Fl', and `Li', only do
                    915:                         * this once per invocation.  There may be more
                    916:                         * of these (all of them?).
                    917:                         */
                    918:                        if (0 == cnt && (nc || MDOC_Li == tok) &&
                    919:                                        DELIM_CLOSE == d && ! scope) {
                    920:                                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
                    921:                                        return(0);
                    922:                                if (MDOC_Ar == tok || MDOC_Li == tok ||
                    923:                                                MDOC_Fl == tok)
                    924:                                        cnt++;
                    925:                                scope = 1;
                    926:                        }
                    927:                        /*
                    928:                         * Close out our scope, if one is open, before
                    929:                         * any punctuation.
                    930:                         */
                    931:                        if (scope && ! rew_elem(m, tok))
1.1       kristaps  932:                                return(0);
1.70      kristaps  933:                        scope = 0;
                    934:                } else if ( ! scope) {
1.32      kristaps  935:                        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps  936:                                return(0);
1.70      kristaps  937:                        scope = 1;
1.1       kristaps  938:                }
                    939:
1.67      schwarze  940:                if (DELIM_NONE == d)
1.3       kristaps  941:                        cnt++;
1.105     kristaps  942:
                    943:                if ( ! dword(m, line, la, p, d))
1.1       kristaps  944:                        return(0);
1.39      kristaps  945:
                    946:                /*
                    947:                 * `Fl' macros have their scope re-opened with each new
                    948:                 * word so that the `-' can be added to each one without
                    949:                 * having to parse out spaces.
                    950:                 */
1.70      kristaps  951:                if (scope && MDOC_Fl == tok) {
1.39      kristaps  952:                        if ( ! rew_elem(m, tok))
                    953:                                return(0);
1.70      kristaps  954:                        scope = 0;
1.39      kristaps  955:                }
1.1       kristaps  956:        }
                    957:
1.70      kristaps  958:        if (scope && ! rew_elem(m, tok))
1.1       kristaps  959:                return(0);
1.4       kristaps  960:
                    961:        /*
                    962:         * If no elements have been collected and we're allowed to have
                    963:         * empties (nc), open a scope and close it out.  Otherwise,
                    964:         * raise a warning.
                    965:         */
1.45      kristaps  966:
1.4       kristaps  967:        if (nc && 0 == cnt) {
1.32      kristaps  968:                if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3       kristaps  969:                        return(0);
1.32      kristaps  970:                if ( ! rew_last(m, m->last))
1.12      kristaps  971:                        return(0);
1.7       kristaps  972:        } else if ( ! nc && 0 == cnt) {
                    973:                mdoc_argv_free(arg);
1.103     kristaps  974:                mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
1.7       kristaps  975:        }
1.4       kristaps  976:
1.60      kristaps  977:        if ( ! nl)
1.1       kristaps  978:                return(1);
1.32      kristaps  979:        return(append_delims(m, line, pos, buf));
1.1       kristaps  980: }
                    981:
                    982:
                    983: static int
                    984: blk_full(MACRO_PROT_ARGS)
                    985: {
1.114   ! kristaps  986:        int               la, nl, nparsed;
1.1       kristaps  987:        struct mdoc_arg  *arg;
1.45      kristaps  988:        struct mdoc_node *head; /* save of head macro */
1.49      kristaps  989:        struct mdoc_node *body; /* save of body macro */
1.47      kristaps  990:        struct mdoc_node *n;
1.74      kristaps  991:        enum mdoc_type    mtt;
1.53      kristaps  992:        enum mdoct        ntok;
1.59      kristaps  993:        enum margserr     ac, lac;
1.56      kristaps  994:        enum margverr     av;
1.1       kristaps  995:        char             *p;
                    996:
1.114   ! kristaps  997:        /*
        !           998:         * Exception: `-diag' lists are not parsed, but lists in general
        !           999:         * are parsed.
        !          1000:         */
        !          1001:        nparsed = 0;
        !          1002:        if (MDOC_It == tok && NULL != m->last &&
        !          1003:                        MDOC_Bl == m->last->tok &&
        !          1004:                        LIST_diag == m->last->norm->Bl.type)
        !          1005:                nparsed = 1;
        !          1006:
1.63      kristaps 1007:        nl = MDOC_NEWLINE & m->flags;
                   1008:
1.45      kristaps 1009:        /* Close out prior implicit scope. */
1.19      kristaps 1010:
1.1       kristaps 1011:        if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32      kristaps 1012:                if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1       kristaps 1013:                        return(0);
1.32      kristaps 1014:                if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1015:                        return(0);
                   1016:        }
                   1017:
1.45      kristaps 1018:        /*
1.109     kristaps 1019:         * This routine accommodates implicitly- and explicitly-scoped
1.45      kristaps 1020:         * macro openings.  Implicit ones first close out prior scope
                   1021:         * (seen above).  Delay opening the head until necessary to
                   1022:         * allow leading punctuation to print.  Special consideration
                   1023:         * for `It -column', which has phrase-part syntax instead of
                   1024:         * regular child nodes.
                   1025:         */
                   1026:
1.1       kristaps 1027:        for (arg = NULL;; ) {
1.45      kristaps 1028:                la = *pos;
1.56      kristaps 1029:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1030:
1.56      kristaps 1031:                if (ARGV_WORD == av) {
1.45      kristaps 1032:                        *pos = la;
1.1       kristaps 1033:                        break;
                   1034:                }
                   1035:
1.56      kristaps 1036:                if (ARGV_EOLN == av)
1.1       kristaps 1037:                        break;
1.56      kristaps 1038:                if (ARGV_ARG == av)
1.1       kristaps 1039:                        continue;
                   1040:
                   1041:                mdoc_argv_free(arg);
                   1042:                return(0);
                   1043:        }
                   1044:
1.32      kristaps 1045:        if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1046:                return(0);
                   1047:
1.49      kristaps 1048:        head = body = NULL;
1.1       kristaps 1049:
1.45      kristaps 1050:        /*
                   1051:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1052:         * of block partial-explicit and full-implicit.  Stupid.
                   1053:         */
1.19      kristaps 1054:
1.45      kristaps 1055:        if (MDOC_Nd == tok) {
                   1056:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1057:                        return(0);
                   1058:                head = m->last;
1.32      kristaps 1059:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19      kristaps 1060:                        return(0);
1.32      kristaps 1061:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19      kristaps 1062:                        return(0);
1.49      kristaps 1063:                body = m->last;
1.19      kristaps 1064:        }
                   1065:
1.59      kristaps 1066:        ac = ARGS_ERROR;
                   1067:
1.62      kristaps 1068:        for ( ; ; ) {
1.45      kristaps 1069:                la = *pos;
1.75      kristaps 1070:                /* Initialise last-phrase-type with ARGS_PEND. */
                   1071:                lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.53      kristaps 1072:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.89      schwarze 1073:
                   1074:                if (ARGS_PUNCT == ac)
                   1075:                        break;
1.1       kristaps 1076:
1.53      kristaps 1077:                if (ARGS_ERROR == ac)
1.1       kristaps 1078:                        return(0);
1.75      kristaps 1079:
                   1080:                if (ARGS_EOLN == ac) {
                   1081:                        if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
                   1082:                                break;
                   1083:                        /*
                   1084:                         * This is necessary: if the last token on a
                   1085:                         * line is a `Ta' or tab, then we'll get
                   1086:                         * ARGS_EOLN, so we must be smart enough to
                   1087:                         * reopen our scope if the last parse was a
                   1088:                         * phrase or partial phrase.
                   1089:                         */
                   1090:                        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1091:                                return(0);
                   1092:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1093:                                return(0);
                   1094:                        body = m->last;
1.1       kristaps 1095:                        break;
1.75      kristaps 1096:                }
1.45      kristaps 1097:
1.73      kristaps 1098:                /*
                   1099:                 * Emit leading punctuation (i.e., punctuation before
                   1100:                 * the MDOC_HEAD) for non-phrase types.
                   1101:                 */
1.45      kristaps 1102:
1.59      kristaps 1103:                if (NULL == head &&
1.71      kristaps 1104:                                ARGS_PEND != ac &&
1.59      kristaps 1105:                                ARGS_PHRASE != ac &&
1.57      kristaps 1106:                                ARGS_PPHRASE != ac &&
1.53      kristaps 1107:                                ARGS_QWORD != ac &&
1.105     kristaps 1108:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1109:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.45      kristaps 1110:                                return(0);
                   1111:                        continue;
                   1112:                }
                   1113:
1.74      kristaps 1114:                /* Open a head if one hasn't been opened. */
1.45      kristaps 1115:
1.74      kristaps 1116:                if (NULL == head) {
1.45      kristaps 1117:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1118:                                return(0);
                   1119:                        head = m->last;
                   1120:                }
                   1121:
1.72      kristaps 1122:                if (ARGS_PHRASE == ac ||
                   1123:                                ARGS_PEND == ac ||
                   1124:                                ARGS_PPHRASE == ac) {
1.73      kristaps 1125:                        /*
1.74      kristaps 1126:                         * If we haven't opened a body yet, rewind the
                   1127:                         * head; if we have, rewind that instead.
                   1128:                         */
                   1129:
                   1130:                        mtt = body ? MDOC_BODY : MDOC_HEAD;
                   1131:                        if ( ! rew_sub(mtt, m, tok, line, ppos))
                   1132:                                return(0);
                   1133:
                   1134:                        /* Then allocate our body context. */
                   1135:
                   1136:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1137:                                return(0);
                   1138:                        body = m->last;
                   1139:
                   1140:                        /*
1.73      kristaps 1141:                         * Process phrases: set whether we're in a
                   1142:                         * partial-phrase (this effects line handling)
                   1143:                         * then call down into the phrase parser.
                   1144:                         */
1.74      kristaps 1145:
1.65      kristaps 1146:                        if (ARGS_PPHRASE == ac)
                   1147:                                m->flags |= MDOC_PPHRASE;
1.71      kristaps 1148:                        if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
                   1149:                                m->flags |= MDOC_PPHRASE;
                   1150:
1.82      kristaps 1151:                        if ( ! phrase(m, line, la, buf))
1.1       kristaps 1152:                                return(0);
1.71      kristaps 1153:
1.65      kristaps 1154:                        m->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1155:                        continue;
                   1156:                }
                   1157:
1.114   ! kristaps 1158:                ntok = nparsed || ARGS_QWORD == ac ?
        !          1159:                        MDOC_MAX : lookup(tok, p);
1.54      kristaps 1160:
                   1161:                if (MDOC_MAX == ntok) {
1.105     kristaps 1162:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1163:                                return(0);
1.53      kristaps 1164:                        continue;
1.45      kristaps 1165:                }
1.53      kristaps 1166:
1.82      kristaps 1167:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.45      kristaps 1168:                        return(0);
1.53      kristaps 1169:                break;
1.45      kristaps 1170:        }
1.1       kristaps 1171:
1.45      kristaps 1172:        if (NULL == head) {
                   1173:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1174:                        return(0);
1.45      kristaps 1175:                head = m->last;
1.1       kristaps 1176:        }
                   1177:
1.63      kristaps 1178:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1179:                return(0);
1.19      kristaps 1180:
1.49      kristaps 1181:        /* If we've already opened our body, exit now. */
1.45      kristaps 1182:
1.49      kristaps 1183:        if (NULL != body)
1.77      kristaps 1184:                goto out;
1.19      kristaps 1185:
1.47      kristaps 1186:        /*
1.49      kristaps 1187:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1188:         * explicit close-out, postpone switching the current block from
                   1189:         * head to body until the rew_sub() call closing out that
                   1190:         * sub-block.
1.47      kristaps 1191:         */
                   1192:        for (n = m->last; n && n != head; n = n->parent) {
1.49      kristaps 1193:                if (MDOC_BLOCK == n->type &&
                   1194:                                MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1195:                                ! (MDOC_VALID & n->flags)) {
1.47      kristaps 1196:                        n->pending = head;
                   1197:                        return(1);
                   1198:                }
                   1199:        }
                   1200:
1.45      kristaps 1201:        /* Close out scopes to remain in a consistent state. */
                   1202:
1.32      kristaps 1203:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1204:                return(0);
1.32      kristaps 1205:        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1206:                return(0);
                   1207:
1.77      kristaps 1208: out:
                   1209:        if ( ! (MDOC_FREECOL & m->flags))
                   1210:                return(1);
                   1211:
                   1212:        if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
                   1213:                return(0);
                   1214:        if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
                   1215:                return(0);
                   1216:
                   1217:        m->flags &= ~MDOC_FREECOL;
1.1       kristaps 1218:        return(1);
                   1219: }
                   1220:
                   1221:
                   1222: static int
                   1223: blk_part_imp(MACRO_PROT_ARGS)
                   1224: {
1.63      kristaps 1225:        int               la, nl;
1.53      kristaps 1226:        enum mdoct        ntok;
1.55      kristaps 1227:        enum margserr     ac;
1.1       kristaps 1228:        char             *p;
1.43      kristaps 1229:        struct mdoc_node *blk; /* saved block context */
                   1230:        struct mdoc_node *body; /* saved body context */
                   1231:        struct mdoc_node *n;
1.1       kristaps 1232:
1.63      kristaps 1233:        nl = MDOC_NEWLINE & m->flags;
                   1234:
1.43      kristaps 1235:        /*
                   1236:         * A macro that spans to the end of the line.  This is generally
                   1237:         * (but not necessarily) called as the first macro.  The block
                   1238:         * has a head as the immediate child, which is always empty,
                   1239:         * followed by zero or more opening punctuation nodes, then the
                   1240:         * body (which may be empty, depending on the macro), then zero
                   1241:         * or more closing punctuation nodes.
                   1242:         */
1.32      kristaps 1243:
                   1244:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
                   1245:                return(0);
1.43      kristaps 1246:
1.32      kristaps 1247:        blk = m->last;
1.43      kristaps 1248:
1.32      kristaps 1249:        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1250:                return(0);
1.32      kristaps 1251:        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1252:                return(0);
                   1253:
1.43      kristaps 1254:        /*
                   1255:         * Open the body scope "on-demand", that is, after we've
                   1256:         * processed all our the leading delimiters (open parenthesis,
                   1257:         * etc.).
                   1258:         */
1.1       kristaps 1259:
1.43      kristaps 1260:        for (body = NULL; ; ) {
1.32      kristaps 1261:                la = *pos;
1.53      kristaps 1262:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.43      kristaps 1263:
1.53      kristaps 1264:                if (ARGS_ERROR == ac)
1.1       kristaps 1265:                        return(0);
1.53      kristaps 1266:                if (ARGS_EOLN == ac)
1.43      kristaps 1267:                        break;
1.53      kristaps 1268:                if (ARGS_PUNCT == ac)
1.1       kristaps 1269:                        break;
                   1270:
1.53      kristaps 1271:                if (NULL == body && ARGS_QWORD != ac &&
1.105     kristaps 1272:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1273:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.1       kristaps 1274:                                return(0);
                   1275:                        continue;
                   1276:                }
                   1277:
1.43      kristaps 1278:                if (NULL == body) {
                   1279:                       if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1280:                               return(0);
                   1281:                        body = m->last;
                   1282:                }
                   1283:
1.54      kristaps 1284:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1285:
                   1286:                if (MDOC_MAX == ntok) {
1.105     kristaps 1287:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.43      kristaps 1288:                                return(0);
1.53      kristaps 1289:                        continue;
                   1290:                }
1.43      kristaps 1291:
1.82      kristaps 1292:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1293:                        return(0);
1.53      kristaps 1294:                break;
1.1       kristaps 1295:        }
                   1296:
1.43      kristaps 1297:        /* Clean-ups to leave in a consistent state. */
                   1298:
1.44      kristaps 1299:        if (NULL == body) {
                   1300:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
                   1301:                        return(0);
                   1302:                body = m->last;
1.64      kristaps 1303:        }
                   1304:
                   1305:        for (n = body->child; n && n->next; n = n->next)
                   1306:                /* Do nothing. */ ;
                   1307:
                   1308:        /*
                   1309:         * End of sentence spacing: if the last node is a text node and
                   1310:         * has a trailing period, then mark it as being end-of-sentence.
                   1311:         */
                   1312:
                   1313:        if (n && MDOC_TEXT == n->type && n->string)
1.93      schwarze 1314:                if (mandoc_eos(n->string, strlen(n->string), 1))
1.64      kristaps 1315:                        n->flags |= MDOC_EOS;
                   1316:
1.109     kristaps 1317:        /* Up-propagate the end-of-space flag. */
1.64      kristaps 1318:
                   1319:        if (n && (MDOC_EOS & n->flags)) {
                   1320:                body->flags |= MDOC_EOS;
                   1321:                body->parent->flags |= MDOC_EOS;
1.44      kristaps 1322:        }
1.43      kristaps 1323:
1.83      schwarze 1324:        /*
                   1325:         * If there is an open sub-block requiring explicit close-out,
                   1326:         * postpone closing out the current block
                   1327:         * until the rew_sub() call closing out the sub-block.
                   1328:         */
                   1329:        for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
                   1330:                if (MDOC_BLOCK == n->type &&
                   1331:                    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
                   1332:                    ! (MDOC_VALID & n->flags)) {
1.98      kristaps 1333:                        make_pending(n, tok, m, line, ppos);
1.83      schwarze 1334:                        if ( ! mdoc_endbody_alloc(m, line, ppos,
                   1335:                            tok, body, ENDBODY_NOSPACE))
                   1336:                                return(0);
                   1337:                        return(1);
                   1338:                }
                   1339:        }
                   1340:
1.32      kristaps 1341:        /*
                   1342:         * If we can't rewind to our body, then our scope has already
                   1343:         * been closed by another macro (like `Oc' closing `Op').  This
                   1344:         * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.47      kristaps 1345:         * crufty use of `Op' breakage.
1.1       kristaps 1346:         */
1.103     kristaps 1347:        if (n != body)
1.104     kristaps 1348:                mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
1.103     kristaps 1349:                                "%s broken", mdoc_macronames[tok]);
1.43      kristaps 1350:
1.83      schwarze 1351:        if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.32      kristaps 1352:                return(0);
1.1       kristaps 1353:
1.32      kristaps 1354:        /* Standard appending of delimiters. */
1.1       kristaps 1355:
1.63      kristaps 1356:        if (nl && ! append_delims(m, line, pos, buf))
1.1       kristaps 1357:                return(0);
                   1358:
1.32      kristaps 1359:        /* Rewind scope, if applicable. */
1.1       kristaps 1360:
1.83      schwarze 1361:        if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1       kristaps 1362:                return(0);
                   1363:
                   1364:        return(1);
                   1365: }
                   1366:
                   1367:
                   1368: static int
                   1369: blk_part_exp(MACRO_PROT_ARGS)
                   1370: {
1.60      kristaps 1371:        int               la, nl;
1.55      kristaps 1372:        enum margserr     ac;
1.43      kristaps 1373:        struct mdoc_node *head; /* keep track of head */
                   1374:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1375:        char             *p;
1.53      kristaps 1376:        enum mdoct        ntok;
1.1       kristaps 1377:
1.60      kristaps 1378:        nl = MDOC_NEWLINE & m->flags;
                   1379:
1.43      kristaps 1380:        /*
                   1381:         * The opening of an explicit macro having zero or more leading
                   1382:         * punctuation nodes; a head with optional single element (the
                   1383:         * case of `Eo'); and a body that may be empty.
                   1384:         */
1.32      kristaps 1385:
                   1386:        if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1       kristaps 1387:                return(0);
1.32      kristaps 1388:
1.43      kristaps 1389:        for (head = body = NULL; ; ) {
1.32      kristaps 1390:                la = *pos;
1.53      kristaps 1391:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1392:
1.53      kristaps 1393:                if (ARGS_ERROR == ac)
1.1       kristaps 1394:                        return(0);
1.53      kristaps 1395:                if (ARGS_PUNCT == ac)
1.1       kristaps 1396:                        break;
1.53      kristaps 1397:                if (ARGS_EOLN == ac)
1.1       kristaps 1398:                        break;
1.43      kristaps 1399:
                   1400:                /* Flush out leading punctuation. */
                   1401:
1.53      kristaps 1402:                if (NULL == head && ARGS_QWORD != ac &&
1.105     kristaps 1403:                                DELIM_OPEN == mdoc_isdelim(p)) {
1.43      kristaps 1404:                        assert(NULL == body);
1.105     kristaps 1405:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.43      kristaps 1406:                                return(0);
                   1407:                        continue;
                   1408:                }
                   1409:
                   1410:                if (NULL == head) {
                   1411:                        assert(NULL == body);
                   1412:                        if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1       kristaps 1413:                                return(0);
1.43      kristaps 1414:                        head = m->last;
1.1       kristaps 1415:                }
                   1416:
1.43      kristaps 1417:                /*
                   1418:                 * `Eo' gobbles any data into the head, but most other
                   1419:                 * macros just immediately close out and begin the body.
                   1420:                 */
                   1421:
                   1422:                if (NULL == body) {
                   1423:                        assert(head);
                   1424:                        /* No check whether it's a macro! */
                   1425:                        if (MDOC_Eo == tok)
1.105     kristaps 1426:                                if ( ! dword(m, line, la, p, DELIM_MAX))
1.43      kristaps 1427:                                        return(0);
                   1428:
1.32      kristaps 1429:                        if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1430:                                return(0);
1.32      kristaps 1431:                        if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1432:                                return(0);
1.43      kristaps 1433:                        body = m->last;
                   1434:
                   1435:                        if (MDOC_Eo == tok)
                   1436:                                continue;
1.1       kristaps 1437:                }
1.43      kristaps 1438:
                   1439:                assert(NULL != head && NULL != body);
                   1440:
1.54      kristaps 1441:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1442:
                   1443:                if (MDOC_MAX == ntok) {
1.105     kristaps 1444:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.43      kristaps 1445:                                return(0);
1.53      kristaps 1446:                        continue;
1.43      kristaps 1447:                }
                   1448:
1.82      kristaps 1449:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1450:                        return(0);
1.53      kristaps 1451:                break;
1.1       kristaps 1452:        }
                   1453:
1.43      kristaps 1454:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1455:
1.112     schwarze 1456:        if (NULL == head)
1.43      kristaps 1457:                if ( ! mdoc_head_alloc(m, line, ppos, tok))
                   1458:                        return(0);
                   1459:
                   1460:        if (NULL == body) {
1.32      kristaps 1461:                if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1       kristaps 1462:                        return(0);
1.32      kristaps 1463:                if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1       kristaps 1464:                        return(0);
                   1465:        }
                   1466:
1.32      kristaps 1467:        /* Standard appending of delimiters. */
                   1468:
1.60      kristaps 1469:        if ( ! nl)
1.1       kristaps 1470:                return(1);
1.32      kristaps 1471:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1472: }
                   1473:
                   1474:
1.61      kristaps 1475: /* ARGSUSED */
1.1       kristaps 1476: static int
                   1477: in_line_argn(MACRO_PROT_ARGS)
                   1478: {
1.60      kristaps 1479:        int              la, flushed, j, maxargs, nl;
1.55      kristaps 1480:        enum margserr    ac;
1.56      kristaps 1481:        enum margverr    av;
1.53      kristaps 1482:        struct mdoc_arg *arg;
                   1483:        char            *p;
                   1484:        enum mdoct       ntok;
1.1       kristaps 1485:
1.60      kristaps 1486:        nl = MDOC_NEWLINE & m->flags;
                   1487:
1.46      kristaps 1488:        /*
                   1489:         * A line macro that has a fixed number of arguments (maxargs).
                   1490:         * Only open the scope once the first non-leading-punctuation is
                   1491:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1492:         * keep it open until the maximum number of arguments are
                   1493:         * exhausted.
                   1494:         */
1.1       kristaps 1495:
                   1496:        switch (tok) {
                   1497:        case (MDOC_Ap):
                   1498:                /* FALLTHROUGH */
                   1499:        case (MDOC_No):
                   1500:                /* FALLTHROUGH */
                   1501:        case (MDOC_Ns):
                   1502:                /* FALLTHROUGH */
                   1503:        case (MDOC_Ux):
                   1504:                maxargs = 0;
                   1505:                break;
1.101     kristaps 1506:        case (MDOC_Bx):
                   1507:                /* FALLTHROUGH */
1.42      kristaps 1508:        case (MDOC_Xr):
                   1509:                maxargs = 2;
                   1510:                break;
1.1       kristaps 1511:        default:
                   1512:                maxargs = 1;
                   1513:                break;
                   1514:        }
                   1515:
1.46      kristaps 1516:        for (arg = NULL; ; ) {
1.32      kristaps 1517:                la = *pos;
1.56      kristaps 1518:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1519:
1.56      kristaps 1520:                if (ARGV_WORD == av) {
1.32      kristaps 1521:                        *pos = la;
1.1       kristaps 1522:                        break;
                   1523:                }
                   1524:
1.56      kristaps 1525:                if (ARGV_EOLN == av)
1.1       kristaps 1526:                        break;
1.56      kristaps 1527:                if (ARGV_ARG == av)
1.1       kristaps 1528:                        continue;
                   1529:
                   1530:                mdoc_argv_free(arg);
                   1531:                return(0);
                   1532:        }
                   1533:
1.46      kristaps 1534:        for (flushed = j = 0; ; ) {
1.32      kristaps 1535:                la = *pos;
1.53      kristaps 1536:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1537:
1.53      kristaps 1538:                if (ARGS_ERROR == ac)
1.1       kristaps 1539:                        return(0);
1.53      kristaps 1540:                if (ARGS_PUNCT == ac)
1.1       kristaps 1541:                        break;
1.53      kristaps 1542:                if (ARGS_EOLN == ac)
1.1       kristaps 1543:                        break;
                   1544:
1.46      kristaps 1545:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.105     kristaps 1546:                                ARGS_QWORD != ac && 0 == j &&
                   1547:                                DELIM_OPEN == mdoc_isdelim(p)) {
                   1548:                        if ( ! dword(m, line, la, p, DELIM_OPEN))
1.46      kristaps 1549:                                return(0);
                   1550:                        continue;
                   1551:                } else if (0 == j)
                   1552:                       if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1553:                               return(0);
                   1554:
                   1555:                if (j == maxargs && ! flushed) {
                   1556:                        if ( ! rew_elem(m, tok))
                   1557:                                return(0);
                   1558:                        flushed = 1;
                   1559:                }
                   1560:
1.54      kristaps 1561:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
                   1562:
                   1563:                if (MDOC_MAX != ntok) {
1.32      kristaps 1564:                        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1565:                                return(0);
                   1566:                        flushed = 1;
1.82      kristaps 1567:                        if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1       kristaps 1568:                                return(0);
1.46      kristaps 1569:                        j++;
1.1       kristaps 1570:                        break;
                   1571:                }
                   1572:
                   1573:                if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.53      kristaps 1574:                                ARGS_QWORD != ac &&
1.67      schwarze 1575:                                ! flushed &&
1.105     kristaps 1576:                                DELIM_NONE != mdoc_isdelim(p)) {
1.32      kristaps 1577:                        if ( ! rew_elem(m, tok))
1.1       kristaps 1578:                                return(0);
                   1579:                        flushed = 1;
                   1580:                }
1.42      kristaps 1581:
1.105     kristaps 1582:                if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1583:                        return(0);
1.46      kristaps 1584:                j++;
1.1       kristaps 1585:        }
                   1586:
1.46      kristaps 1587:        if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
                   1588:               return(0);
                   1589:
                   1590:        /* Close out in a consistent state. */
1.32      kristaps 1591:
                   1592:        if ( ! flushed && ! rew_elem(m, tok))
1.1       kristaps 1593:                return(0);
1.60      kristaps 1594:        if ( ! nl)
1.1       kristaps 1595:                return(1);
1.32      kristaps 1596:        return(append_delims(m, line, pos, buf));
1.1       kristaps 1597: }
                   1598:
                   1599:
                   1600: static int
                   1601: in_line_eoln(MACRO_PROT_ARGS)
                   1602: {
1.56      kristaps 1603:        int              la;
1.55      kristaps 1604:        enum margserr    ac;
1.56      kristaps 1605:        enum margverr    av;
1.53      kristaps 1606:        struct mdoc_arg *arg;
                   1607:        char            *p;
                   1608:        enum mdoct       ntok;
1.1       kristaps 1609:
                   1610:        assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
                   1611:
1.90      schwarze 1612:        if (tok == MDOC_Pp)
                   1613:                rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos);
                   1614:
1.32      kristaps 1615:        /* Parse macro arguments. */
1.1       kristaps 1616:
1.32      kristaps 1617:        for (arg = NULL; ; ) {
1.1       kristaps 1618:                la = *pos;
1.56      kristaps 1619:                av = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1       kristaps 1620:
1.56      kristaps 1621:                if (ARGV_WORD == av) {
1.1       kristaps 1622:                        *pos = la;
                   1623:                        break;
                   1624:                }
1.56      kristaps 1625:                if (ARGV_EOLN == av)
1.1       kristaps 1626:                        break;
1.56      kristaps 1627:                if (ARGV_ARG == av)
1.1       kristaps 1628:                        continue;
                   1629:
                   1630:                mdoc_argv_free(arg);
                   1631:                return(0);
                   1632:        }
                   1633:
1.32      kristaps 1634:        /* Open element scope. */
                   1635:
                   1636:        if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1       kristaps 1637:                return(0);
                   1638:
1.32      kristaps 1639:        /* Parse argument terms. */
1.1       kristaps 1640:
                   1641:        for (;;) {
                   1642:                la = *pos;
1.53      kristaps 1643:                ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1       kristaps 1644:
1.53      kristaps 1645:                if (ARGS_ERROR == ac)
1.1       kristaps 1646:                        return(0);
1.53      kristaps 1647:                if (ARGS_EOLN == ac)
1.1       kristaps 1648:                        break;
                   1649:
1.53      kristaps 1650:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1       kristaps 1651:
1.53      kristaps 1652:                if (MDOC_MAX == ntok) {
1.105     kristaps 1653:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1654:                                return(0);
1.53      kristaps 1655:                        continue;
                   1656:                }
1.1       kristaps 1657:
1.53      kristaps 1658:                if ( ! rew_elem(m, tok))
1.1       kristaps 1659:                        return(0);
1.82      kristaps 1660:                return(mdoc_macro(m, ntok, line, la, pos, buf));
1.1       kristaps 1661:        }
                   1662:
1.32      kristaps 1663:        /* Close out (no delimiters). */
                   1664:
                   1665:        return(rew_elem(m, tok));
1.1       kristaps 1666: }
                   1667:
                   1668:
                   1669: /* ARGSUSED */
                   1670: static int
1.41      kristaps 1671: ctx_synopsis(MACRO_PROT_ARGS)
                   1672: {
1.60      kristaps 1673:        int              nl;
                   1674:
                   1675:        nl = MDOC_NEWLINE & m->flags;
1.41      kristaps 1676:
                   1677:        /* If we're not in the SYNOPSIS, go straight to in-line. */
1.90      schwarze 1678:        if ( ! (MDOC_SYNOPSIS & m->flags))
1.82      kristaps 1679:                return(in_line(m, tok, line, ppos, pos, buf));
1.41      kristaps 1680:
                   1681:        /* If we're a nested call, same place. */
1.60      kristaps 1682:        if ( ! nl)
1.82      kristaps 1683:                return(in_line(m, tok, line, ppos, pos, buf));
1.41      kristaps 1684:
                   1685:        /*
                   1686:         * XXX: this will open a block scope; however, if later we end
                   1687:         * up formatting the block scope, then child nodes will inherit
                   1688:         * the formatting.  Be careful.
                   1689:         */
1.88      schwarze 1690:        if (MDOC_Nm == tok)
                   1691:                return(blk_full(m, tok, line, ppos, pos, buf));
                   1692:        assert(MDOC_Vt == tok);
1.82      kristaps 1693:        return(blk_part_imp(m, tok, line, ppos, pos, buf));
1.41      kristaps 1694: }
                   1695:
                   1696:
                   1697: /* ARGSUSED */
                   1698: static int
1.1       kristaps 1699: obsolete(MACRO_PROT_ARGS)
                   1700: {
                   1701:
1.103     kristaps 1702:        mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS);
                   1703:        return(1);
1.1       kristaps 1704: }
                   1705:
                   1706:
1.24      kristaps 1707: /*
                   1708:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1709:  * They're unusual because they're basically free-form text until a
                   1710:  * macro is encountered.
                   1711:  */
1.1       kristaps 1712: static int
1.82      kristaps 1713: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1       kristaps 1714: {
1.53      kristaps 1715:        int              la, pos;
1.75      kristaps 1716:        enum margserr    ac;
1.53      kristaps 1717:        enum mdoct       ntok;
                   1718:        char            *p;
1.1       kristaps 1719:
1.24      kristaps 1720:        for (pos = ppos; ; ) {
                   1721:                la = pos;
1.1       kristaps 1722:
1.107     kristaps 1723:                ac = mdoc_zargs(m, line, &pos, buf, &p);
1.1       kristaps 1724:
1.75      kristaps 1725:                if (ARGS_ERROR == ac)
1.24      kristaps 1726:                        return(0);
1.75      kristaps 1727:                if (ARGS_EOLN == ac)
1.24      kristaps 1728:                        break;
1.1       kristaps 1729:
1.75      kristaps 1730:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1       kristaps 1731:
1.53      kristaps 1732:                if (MDOC_MAX == ntok) {
1.105     kristaps 1733:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.1       kristaps 1734:                                return(0);
1.53      kristaps 1735:                        continue;
                   1736:                }
1.1       kristaps 1737:
1.82      kristaps 1738:                if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1       kristaps 1739:                        return(0);
1.53      kristaps 1740:                return(append_delims(m, line, &pos, buf));
1.1       kristaps 1741:        }
                   1742:
                   1743:        return(1);
                   1744: }
1.24      kristaps 1745:
                   1746:
1.79      kristaps 1747: /* ARGSUSED */
1.75      kristaps 1748: static int
                   1749: phrase_ta(MACRO_PROT_ARGS)
                   1750: {
                   1751:        int               la;
                   1752:        enum mdoct        ntok;
                   1753:        enum margserr     ac;
                   1754:        char             *p;
                   1755:
                   1756:        /*
                   1757:         * FIXME: this is overly restrictive: if the `Ta' is unexpected,
                   1758:         * it should simply error out with ARGSLOST.
                   1759:         */
                   1760:
                   1761:        if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
                   1762:                return(0);
                   1763:        if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
                   1764:                return(0);
                   1765:
                   1766:        for (;;) {
                   1767:                la = *pos;
1.107     kristaps 1768:                ac = mdoc_zargs(m, line, pos, buf, &p);
1.75      kristaps 1769:
                   1770:                if (ARGS_ERROR == ac)
                   1771:                        return(0);
                   1772:                if (ARGS_EOLN == ac)
                   1773:                        break;
                   1774:
                   1775:                ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
                   1776:
                   1777:                if (MDOC_MAX == ntok) {
1.105     kristaps 1778:                        if ( ! dword(m, line, la, p, DELIM_MAX))
1.75      kristaps 1779:                                return(0);
                   1780:                        continue;
                   1781:                }
                   1782:
1.82      kristaps 1783:                if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.75      kristaps 1784:                        return(0);
                   1785:                return(append_delims(m, line, pos, buf));
                   1786:        }
                   1787:
                   1788:        return(1);
                   1789: }

CVSweb