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

Annotation of mandoc/mdoc_macro.c, Revision 1.83

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

CVSweb