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

Annotation of mandoc/mdoc_macro.c, Revision 1.154

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

CVSweb