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

Annotation of mandoc/mdoc_macro.c, Revision 1.64

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

CVSweb