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

Annotation of mandoc/mdoc_macro.c, Revision 1.136

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

CVSweb