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

Annotation of mandoc/mdoc_macro.c, Revision 1.92

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

CVSweb