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

Annotation of mandoc/mdoc_macro.c, Revision 1.71

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

CVSweb