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

Annotation of mandoc/roff.c, Revision 1.253

1.253   ! schwarze    1: /*     $Id: roff.c,v 1.252 2015/01/21 02:16:52 schwarze Exp $ */
1.1       kristaps    2: /*
1.247     schwarze    3:  * Copyright (c) 2010, 2011, 2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.66      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.106     kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.66      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.106     kristaps   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.66      kristaps   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.66      kristaps   18: #include "config.h"
1.225     schwarze   19:
                     20: #include <sys/types.h>
1.30      kristaps   21:
1.67      kristaps   22: #include <assert.h>
1.85      kristaps   23: #include <ctype.h>
1.245     schwarze   24: #include <limits.h>
1.178     schwarze   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
1.67      kristaps   27: #include <string.h>
1.1       kristaps   28:
1.67      kristaps   29: #include "mandoc.h"
1.201     schwarze   30: #include "mandoc_aux.h"
1.226     schwarze   31: #include "libmandoc.h"
1.109     kristaps   32: #include "libroff.h"
1.33      kristaps   33:
1.141     kristaps   34: /* Maximum number of nested if-else conditionals. */
1.82      kristaps   35: #define        RSTACK_MAX      128
                     36:
1.170     schwarze   37: /* Maximum number of string expansions per line, to break infinite loops. */
                     38: #define        EXPAND_LIMIT    1000
                     39:
1.67      kristaps   40: enum   rofft {
1.251     schwarze   41:        ROFF_ab,
1.103     kristaps   42:        ROFF_ad,
1.251     schwarze   43:        ROFF_af,
                     44:        ROFF_aln,
                     45:        ROFF_als,
1.80      kristaps   46:        ROFF_am,
1.251     schwarze   47:        ROFF_am1,
1.80      kristaps   48:        ROFF_ami,
1.251     schwarze   49:        ROFF_ami1,
1.193     schwarze   50:        ROFF_as,
1.251     schwarze   51:        ROFF_as1,
                     52:        ROFF_asciify,
                     53:        ROFF_backtrace,
                     54:        ROFF_bd,
                     55:        ROFF_bleedat,
                     56:        ROFF_blm,
                     57:        ROFF_box,
                     58:        ROFF_boxa,
                     59:        ROFF_bp,
                     60:        ROFF_BP,
                     61:        /* MAN_br, MDOC_br */
                     62:        ROFF_break,
                     63:        ROFF_breakchar,
                     64:        ROFF_brnl,
                     65:        ROFF_brp,
                     66:        ROFF_brpnl,
                     67:        ROFF_c2,
1.174     kristaps   68:        ROFF_cc,
1.194     schwarze   69:        ROFF_ce,
1.251     schwarze   70:        ROFF_cf,
                     71:        ROFF_cflags,
                     72:        ROFF_ch,
                     73:        ROFF_char,
                     74:        ROFF_chop,
                     75:        ROFF_class,
                     76:        ROFF_close,
                     77:        ROFF_CL,
                     78:        ROFF_color,
                     79:        ROFF_composite,
                     80:        ROFF_continue,
                     81:        ROFF_cp,
                     82:        ROFF_cropat,
                     83:        ROFF_cs,
                     84:        ROFF_cu,
                     85:        ROFF_da,
                     86:        ROFF_dch,
                     87:        ROFF_Dd,
1.80      kristaps   88:        ROFF_de,
1.251     schwarze   89:        ROFF_de1,
                     90:        ROFF_defcolor,
1.80      kristaps   91:        ROFF_dei,
1.251     schwarze   92:        ROFF_dei1,
                     93:        ROFF_device,
                     94:        ROFF_devicem,
                     95:        ROFF_di,
                     96:        ROFF_do,
1.83      schwarze   97:        ROFF_ds,
1.251     schwarze   98:        ROFF_ds1,
                     99:        ROFF_dwh,
                    100:        ROFF_dt,
                    101:        ROFF_ec,
                    102:        ROFF_ecr,
                    103:        ROFF_ecs,
1.82      kristaps  104:        ROFF_el,
1.251     schwarze  105:        ROFF_em,
                    106:        ROFF_EN,
                    107:        ROFF_eo,
                    108:        ROFF_EP,
                    109:        ROFF_EQ,
                    110:        ROFF_errprint,
                    111:        ROFF_ev,
                    112:        ROFF_evc,
                    113:        ROFF_ex,
                    114:        ROFF_fallback,
1.185     schwarze  115:        ROFF_fam,
1.251     schwarze  116:        ROFF_fc,
                    117:        ROFF_fchar,
                    118:        ROFF_fcolor,
                    119:        ROFF_fdeferlig,
                    120:        ROFF_feature,
                    121:        /* MAN_fi; ignored in mdoc(7) */
                    122:        ROFF_fkern,
                    123:        ROFF_fl,
                    124:        ROFF_flig,
                    125:        ROFF_fp,
                    126:        ROFF_fps,
                    127:        ROFF_fschar,
                    128:        ROFF_fspacewidth,
                    129:        ROFF_fspecial,
                    130:        /* MAN_ft; ignored in mdoc(7) */
                    131:        ROFF_ftr,
                    132:        ROFF_fzoom,
                    133:        ROFF_gcolor,
                    134:        ROFF_hc,
                    135:        ROFF_hcode,
                    136:        ROFF_hidechar,
                    137:        ROFF_hla,
                    138:        ROFF_hlm,
                    139:        ROFF_hpf,
                    140:        ROFF_hpfa,
                    141:        ROFF_hpfcode,
1.186     schwarze  142:        ROFF_hw,
1.103     kristaps  143:        ROFF_hy,
1.251     schwarze  144:        ROFF_hylang,
                    145:        ROFF_hylen,
                    146:        ROFF_hym,
                    147:        ROFF_hypp,
                    148:        ROFF_hys,
1.82      kristaps  149:        ROFF_ie,
1.75      kristaps  150:        ROFF_if,
1.76      kristaps  151:        ROFF_ig,
1.251     schwarze  152:        /* MAN_in; ignored in mdoc(7) */
                    153:        ROFF_index,
1.123     schwarze  154:        ROFF_it,
1.251     schwarze  155:        ROFF_itc,
                    156:        ROFF_IX,
                    157:        ROFF_kern,
                    158:        ROFF_kernafter,
                    159:        ROFF_kernbefore,
                    160:        ROFF_kernpair,
                    161:        ROFF_lc,
                    162:        ROFF_lc_ctype,
                    163:        ROFF_lds,
                    164:        ROFF_length,
                    165:        ROFF_letadj,
                    166:        ROFF_lf,
                    167:        ROFF_lg,
                    168:        ROFF_lhang,
                    169:        ROFF_linetabs,
                    170:        /* MAN_ll, MDOC_ll */
                    171:        ROFF_lnr,
                    172:        ROFF_lnrf,
                    173:        ROFF_lpfx,
                    174:        ROFF_ls,
                    175:        ROFF_lsm,
                    176:        ROFF_lt,
                    177:        ROFF_mc,
                    178:        ROFF_mediasize,
                    179:        ROFF_minss,
                    180:        ROFF_mk,
                    181:        ROFF_mso,
                    182:        /* MAN_na; ignored in mdoc(7) */
1.103     kristaps  183:        ROFF_ne,
1.251     schwarze  184:        /* MAN_nf; ignored in mdoc(7) */
1.103     kristaps  185:        ROFF_nh,
1.251     schwarze  186:        ROFF_nhychar,
                    187:        ROFF_nm,
                    188:        ROFF_nn,
                    189:        ROFF_nop,
1.104     kristaps  190:        ROFF_nr,
1.251     schwarze  191:        ROFF_nrf,
                    192:        ROFF_nroff,
1.124     schwarze  193:        ROFF_ns,
1.251     schwarze  194:        ROFF_nx,
                    195:        ROFF_open,
                    196:        ROFF_opena,
                    197:        ROFF_os,
                    198:        ROFF_output,
                    199:        ROFF_padj,
                    200:        ROFF_papersize,
                    201:        ROFF_pc,
                    202:        ROFF_pev,
                    203:        ROFF_pi,
                    204:        ROFF_PI,
1.229     schwarze  205:        ROFF_pl,
1.251     schwarze  206:        ROFF_pm,
                    207:        ROFF_pn,
                    208:        ROFF_pnr,
                    209:        ROFF_po,
1.124     schwarze  210:        ROFF_ps,
1.251     schwarze  211:        ROFF_psbb,
                    212:        ROFF_pshape,
                    213:        ROFF_pso,
                    214:        ROFF_ptr,
                    215:        ROFF_pvs,
                    216:        ROFF_rchar,
                    217:        ROFF_rd,
                    218:        ROFF_recursionlimit,
                    219:        ROFF_return,
                    220:        ROFF_rfschar,
                    221:        ROFF_rhang,
                    222:        ROFF_rj,
1.83      schwarze  223:        ROFF_rm,
1.251     schwarze  224:        ROFF_rn,
                    225:        ROFF_rnn,
1.203     schwarze  226:        ROFF_rr,
1.251     schwarze  227:        ROFF_rs,
                    228:        ROFF_rt,
                    229:        ROFF_schar,
                    230:        ROFF_sentchar,
                    231:        ROFF_shc,
                    232:        ROFF_shift,
                    233:        ROFF_sizes,
1.105     kristaps  234:        ROFF_so,
1.251     schwarze  235:        /* MAN_sp, MDOC_sp */
                    236:        ROFF_spacewidth,
                    237:        ROFF_special,
                    238:        ROFF_spreadwarn,
                    239:        ROFF_ss,
                    240:        ROFF_sty,
                    241:        ROFF_substring,
                    242:        ROFF_sv,
                    243:        ROFF_sy,
                    244:        ROFF_T_,
1.124     schwarze  245:        ROFF_ta,
1.251     schwarze  246:        ROFF_tc,
                    247:        ROFF_TE,
                    248:        ROFF_TH,
                    249:        ROFF_ti,
                    250:        ROFF_tkf,
                    251:        ROFF_tl,
                    252:        ROFF_tm,
                    253:        ROFF_tm1,
                    254:        ROFF_tmc,
1.83      schwarze  255:        ROFF_tr,
1.251     schwarze  256:        ROFF_track,
                    257:        ROFF_transchar,
                    258:        ROFF_trf,
                    259:        ROFF_trimat,
                    260:        ROFF_trin,
                    261:        ROFF_trnt,
                    262:        ROFF_troff,
1.109     kristaps  263:        ROFF_TS,
1.251     schwarze  264:        ROFF_uf,
                    265:        ROFF_ul,
                    266:        ROFF_unformat,
                    267:        ROFF_unwatch,
                    268:        ROFF_unwatchn,
                    269:        ROFF_vpt,
                    270:        ROFF_vs,
                    271:        ROFF_warn,
                    272:        ROFF_warnscale,
                    273:        ROFF_watch,
                    274:        ROFF_watchlength,
                    275:        ROFF_watchn,
                    276:        ROFF_wh,
                    277:        ROFF_while,
                    278:        ROFF_write,
                    279:        ROFF_writec,
                    280:        ROFF_writem,
                    281:        ROFF_xflag,
1.76      kristaps  282:        ROFF_cblock,
1.106     kristaps  283:        ROFF_USERDEF,
1.67      kristaps  284:        ROFF_MAX
                    285: };
                    286:
1.147     kristaps  287: /*
1.167     kristaps  288:  * An incredibly-simple string buffer.
                    289:  */
1.94      kristaps  290: struct roffstr {
1.167     kristaps  291:        char            *p; /* nil-terminated buffer */
                    292:        size_t           sz; /* saved strlen(p) */
1.166     kristaps  293: };
                    294:
                    295: /*
1.167     kristaps  296:  * A key-value roffstr pair as part of a singly-linked list.
1.166     kristaps  297:  */
                    298: struct roffkv {
                    299:        struct roffstr   key;
                    300:        struct roffstr   val;
                    301:        struct roffkv   *next; /* next in list */
1.94      kristaps  302: };
                    303:
1.180     schwarze  304: /*
                    305:  * A single number register as part of a singly-linked list.
                    306:  */
                    307: struct roffreg {
                    308:        struct roffstr   key;
1.181     schwarze  309:        int              val;
1.180     schwarze  310:        struct roffreg  *next;
                    311: };
                    312:
1.67      kristaps  313: struct roff {
1.128     kristaps  314:        struct mparse   *parse; /* parse point */
1.237     schwarze  315:        const struct mchars *mchars; /* character table */
1.67      kristaps  316:        struct roffnode *last; /* leaf of stack */
1.223     schwarze  317:        int             *rstack; /* stack of inverted `ie' values */
1.180     schwarze  318:        struct roffreg  *regtab; /* number registers */
1.166     kristaps  319:        struct roffkv   *strtab; /* user-defined strings & macros */
1.167     kristaps  320:        struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */
                    321:        struct roffstr  *xtab; /* single-byte trans table (`tr') */
1.106     kristaps  322:        const char      *current_string; /* value of last called user macro */
1.118     kristaps  323:        struct tbl_node *first_tbl; /* first table parsed */
                    324:        struct tbl_node *last_tbl; /* last table parsed */
                    325:        struct tbl_node *tbl; /* current table being parsed */
1.125     kristaps  326:        struct eqn_node *last_eqn; /* last equation parsed */
                    327:        struct eqn_node *first_eqn; /* first equation parsed */
                    328:        struct eqn_node *eqn; /* current equation being parsed */
1.230     schwarze  329:        int              eqn_inline; /* current equation is inline */
1.223     schwarze  330:        int              options; /* parse options */
                    331:        int              rstacksz; /* current size limit of rstack */
                    332:        int              rstackpos; /* position in rstack */
1.227     schwarze  333:        int              format; /* current file in mdoc or man format */
1.223     schwarze  334:        char             control; /* control character */
1.79      kristaps  335: };
                    336:
1.67      kristaps  337: struct roffnode {
                    338:        enum rofft       tok; /* type of node */
                    339:        struct roffnode *parent; /* up one in stack */
                    340:        int              line; /* parse line */
                    341:        int              col; /* parse col */
1.106     kristaps  342:        char            *name; /* node name, e.g. macro name */
1.79      kristaps  343:        char            *end; /* end-rules: custom token */
                    344:        int              endspan; /* end-rules: next-line or infty */
1.198     schwarze  345:        int              rule; /* current evaluation rule */
1.67      kristaps  346: };
                    347:
                    348: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps  349:                         enum rofft tok, /* tok of macro */ \
1.238     schwarze  350:                         struct buf *buf, /* input buffer */ \
1.67      kristaps  351:                         int ln, /* parse line */ \
1.75      kristaps  352:                         int ppos, /* original pos in buffer */ \
                    353:                         int pos, /* current pos in buffer */ \
1.74      kristaps  354:                         int *offs /* reset offset of buffer data */
1.67      kristaps  355:
                    356: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    357:
                    358: struct roffmac {
                    359:        const char      *name; /* macro name */
1.79      kristaps  360:        roffproc         proc; /* process new macro */
                    361:        roffproc         text; /* process as child text of macro */
                    362:        roffproc         sub; /* process as child of macro */
                    363:        int              flags;
                    364: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  365:        struct roffmac  *next;
1.67      kristaps  366: };
                    367:
1.141     kristaps  368: struct predef {
                    369:        const char      *name; /* predefined input name */
                    370:        const char      *str; /* replacement symbol */
                    371: };
                    372:
                    373: #define        PREDEF(__name, __str) \
                    374:        { (__name), (__str) },
                    375:
1.155     kristaps  376: static enum rofft       roffhash_find(const char *, size_t);
                    377: static void             roffhash_init(void);
                    378: static void             roffnode_cleanscope(struct roff *);
                    379: static void             roffnode_pop(struct roff *);
                    380: static void             roffnode_push(struct roff *, enum rofft,
                    381:                                const char *, int, int);
1.80      kristaps  382: static enum rofferr     roff_block(ROFF_ARGS);
                    383: static enum rofferr     roff_block_text(ROFF_ARGS);
                    384: static enum rofferr     roff_block_sub(ROFF_ARGS);
1.251     schwarze  385: static enum rofferr     roff_brp(ROFF_ARGS);
1.80      kristaps  386: static enum rofferr     roff_cblock(ROFF_ARGS);
1.174     kristaps  387: static enum rofferr     roff_cc(ROFF_ARGS);
1.195     schwarze  388: static void             roff_ccond(struct roff *, int, int);
1.82      kristaps  389: static enum rofferr     roff_cond(ROFF_ARGS);
                    390: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    391: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  392: static enum rofferr     roff_ds(ROFF_ARGS);
1.238     schwarze  393: static enum rofferr     roff_eqndelim(struct roff *, struct buf *, int);
1.234     kristaps  394: static int              roff_evalcond(struct roff *r, int,
                    395:                                const char *, int *);
                    396: static int              roff_evalnum(struct roff *, int,
                    397:                                const char *, int *, int *, int);
                    398: static int              roff_evalpar(struct roff *, int,
                    399:                                const char *, int *, int *);
1.198     schwarze  400: static int              roff_evalstrcond(const char *, int *);
1.155     kristaps  401: static void             roff_free1(struct roff *);
1.180     schwarze  402: static void             roff_freereg(struct roffreg *);
1.167     kristaps  403: static void             roff_freestr(struct roffkv *);
1.212     schwarze  404: static size_t           roff_getname(struct roff *, char **, int, int);
1.184     schwarze  405: static int              roff_getnum(const char *, int *, int *);
                    406: static int              roff_getop(const char *, int *, char *);
1.181     schwarze  407: static int              roff_getregn(const struct roff *,
                    408:                                const char *, size_t);
1.192     schwarze  409: static int              roff_getregro(const char *name);
1.207     schwarze  410: static const char      *roff_getstrn(const struct roff *,
1.94      kristaps  411:                                const char *, size_t);
1.251     schwarze  412: static enum rofferr     roff_insec(ROFF_ARGS);
1.178     schwarze  413: static enum rofferr     roff_it(ROFF_ARGS);
1.103     kristaps  414: static enum rofferr     roff_line_ignore(ROFF_ARGS);
1.89      kristaps  415: static enum rofferr     roff_nr(ROFF_ARGS);
1.214     schwarze  416: static enum rofft       roff_parse(struct roff *, char *, int *,
                    417:                                int, int);
1.238     schwarze  418: static enum rofferr     roff_parsetext(struct buf *, int, int *);
                    419: static enum rofferr     roff_res(struct roff *, struct buf *, int, int);
1.122     schwarze  420: static enum rofferr     roff_rm(ROFF_ARGS);
1.203     schwarze  421: static enum rofferr     roff_rr(ROFF_ARGS);
1.94      kristaps  422: static void             roff_setstr(struct roff *,
1.106     kristaps  423:                                const char *, const char *, int);
1.207     schwarze  424: static void             roff_setstrn(struct roffkv **, const char *,
1.164     kristaps  425:                                size_t, const char *, size_t, int);
1.105     kristaps  426: static enum rofferr     roff_so(ROFF_ARGS);
1.164     kristaps  427: static enum rofferr     roff_tr(ROFF_ARGS);
1.175     schwarze  428: static enum rofferr     roff_Dd(ROFF_ARGS);
                    429: static enum rofferr     roff_TH(ROFF_ARGS);
1.109     kristaps  430: static enum rofferr     roff_TE(ROFF_ARGS);
                    431: static enum rofferr     roff_TS(ROFF_ARGS);
1.125     kristaps  432: static enum rofferr     roff_EQ(ROFF_ARGS);
                    433: static enum rofferr     roff_EN(ROFF_ARGS);
1.112     kristaps  434: static enum rofferr     roff_T_(ROFF_ARGS);
1.251     schwarze  435: static enum rofferr     roff_unsupp(ROFF_ARGS);
1.106     kristaps  436: static enum rofferr     roff_userdef(ROFF_ARGS);
1.67      kristaps  437:
1.155     kristaps  438: /* See roffhash_find() */
1.85      kristaps  439:
                    440: #define        ASCII_HI         126
                    441: #define        ASCII_LO         33
                    442: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    443:
                    444: static struct roffmac  *hash[HASHWIDTH];
                    445:
                    446: static struct roffmac   roffs[ROFF_MAX] = {
1.251     schwarze  447:        { "ab", roff_unsupp, NULL, NULL, 0, NULL },
1.103     kristaps  448:        { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  449:        { "af", roff_line_ignore, NULL, NULL, 0, NULL },
                    450:        { "aln", roff_unsupp, NULL, NULL, 0, NULL },
                    451:        { "als", roff_unsupp, NULL, NULL, 0, NULL },
1.85      kristaps  452:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251     schwarze  453:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.85      kristaps  454:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251     schwarze  455:        { "ami1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.193     schwarze  456:        { "as", roff_ds, NULL, NULL, 0, NULL },
1.251     schwarze  457:        { "as1", roff_ds, NULL, NULL, 0, NULL },
                    458:        { "asciify", roff_unsupp, NULL, NULL, 0, NULL },
                    459:        { "backtrace", roff_line_ignore, NULL, NULL, 0, NULL },
                    460:        { "bd", roff_line_ignore, NULL, NULL, 0, NULL },
                    461:        { "bleedat", roff_line_ignore, NULL, NULL, 0, NULL },
                    462:        { "blm", roff_unsupp, NULL, NULL, 0, NULL },
                    463:        { "box", roff_unsupp, NULL, NULL, 0, NULL },
                    464:        { "boxa", roff_unsupp, NULL, NULL, 0, NULL },
                    465:        { "bp", roff_line_ignore, NULL, NULL, 0, NULL },
                    466:        { "BP", roff_unsupp, NULL, NULL, 0, NULL },
                    467:        { "break", roff_unsupp, NULL, NULL, 0, NULL },
                    468:        { "breakchar", roff_line_ignore, NULL, NULL, 0, NULL },
                    469:        { "brnl", roff_line_ignore, NULL, NULL, 0, NULL },
                    470:        { "brp", roff_brp, NULL, NULL, 0, NULL },
                    471:        { "brpnl", roff_line_ignore, NULL, NULL, 0, NULL },
                    472:        { "c2", roff_unsupp, NULL, NULL, 0, NULL },
1.174     kristaps  473:        { "cc", roff_cc, NULL, NULL, 0, NULL },
1.194     schwarze  474:        { "ce", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  475:        { "cf", roff_insec, NULL, NULL, 0, NULL },
                    476:        { "cflags", roff_line_ignore, NULL, NULL, 0, NULL },
                    477:        { "ch", roff_line_ignore, NULL, NULL, 0, NULL },
                    478:        { "char", roff_unsupp, NULL, NULL, 0, NULL },
                    479:        { "chop", roff_unsupp, NULL, NULL, 0, NULL },
                    480:        { "class", roff_line_ignore, NULL, NULL, 0, NULL },
                    481:        { "close", roff_insec, NULL, NULL, 0, NULL },
                    482:        { "CL", roff_unsupp, NULL, NULL, 0, NULL },
                    483:        { "color", roff_line_ignore, NULL, NULL, 0, NULL },
                    484:        { "composite", roff_unsupp, NULL, NULL, 0, NULL },
                    485:        { "continue", roff_unsupp, NULL, NULL, 0, NULL },
                    486:        { "cp", roff_line_ignore, NULL, NULL, 0, NULL },
                    487:        { "cropat", roff_line_ignore, NULL, NULL, 0, NULL },
                    488:        { "cs", roff_line_ignore, NULL, NULL, 0, NULL },
                    489:        { "cu", roff_line_ignore, NULL, NULL, 0, NULL },
                    490:        { "da", roff_unsupp, NULL, NULL, 0, NULL },
                    491:        { "dch", roff_unsupp, NULL, NULL, 0, NULL },
                    492:        { "Dd", roff_Dd, NULL, NULL, 0, NULL },
1.85      kristaps  493:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251     schwarze  494:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    495:        { "defcolor", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  496:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251     schwarze  497:        { "dei1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    498:        { "device", roff_unsupp, NULL, NULL, 0, NULL },
                    499:        { "devicem", roff_unsupp, NULL, NULL, 0, NULL },
                    500:        { "di", roff_unsupp, NULL, NULL, 0, NULL },
                    501:        { "do", roff_unsupp, NULL, NULL, 0, NULL },
1.92      schwarze  502:        { "ds", roff_ds, NULL, NULL, 0, NULL },
1.251     schwarze  503:        { "ds1", roff_ds, NULL, NULL, 0, NULL },
                    504:        { "dwh", roff_unsupp, NULL, NULL, 0, NULL },
                    505:        { "dt", roff_unsupp, NULL, NULL, 0, NULL },
                    506:        { "ec", roff_unsupp, NULL, NULL, 0, NULL },
                    507:        { "ecr", roff_unsupp, NULL, NULL, 0, NULL },
                    508:        { "ecs", roff_unsupp, NULL, NULL, 0, NULL },
1.85      kristaps  509:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.251     schwarze  510:        { "em", roff_unsupp, NULL, NULL, 0, NULL },
                    511:        { "EN", roff_EN, NULL, NULL, 0, NULL },
                    512:        { "eo", roff_unsupp, NULL, NULL, 0, NULL },
                    513:        { "EP", roff_unsupp, NULL, NULL, 0, NULL },
                    514:        { "EQ", roff_EQ, NULL, NULL, 0, NULL },
                    515:        { "errprint", roff_line_ignore, NULL, NULL, 0, NULL },
                    516:        { "ev", roff_unsupp, NULL, NULL, 0, NULL },
                    517:        { "evc", roff_unsupp, NULL, NULL, 0, NULL },
                    518:        { "ex", roff_unsupp, NULL, NULL, 0, NULL },
                    519:        { "fallback", roff_line_ignore, NULL, NULL, 0, NULL },
1.185     schwarze  520:        { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  521:        { "fc", roff_unsupp, NULL, NULL, 0, NULL },
                    522:        { "fchar", roff_unsupp, NULL, NULL, 0, NULL },
                    523:        { "fcolor", roff_line_ignore, NULL, NULL, 0, NULL },
                    524:        { "fdeferlig", roff_line_ignore, NULL, NULL, 0, NULL },
                    525:        { "feature", roff_line_ignore, NULL, NULL, 0, NULL },
                    526:        { "fkern", roff_line_ignore, NULL, NULL, 0, NULL },
                    527:        { "fl", roff_line_ignore, NULL, NULL, 0, NULL },
                    528:        { "flig", roff_line_ignore, NULL, NULL, 0, NULL },
                    529:        { "fp", roff_line_ignore, NULL, NULL, 0, NULL },
                    530:        { "fps", roff_line_ignore, NULL, NULL, 0, NULL },
                    531:        { "fschar", roff_unsupp, NULL, NULL, 0, NULL },
                    532:        { "fspacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
                    533:        { "fspecial", roff_line_ignore, NULL, NULL, 0, NULL },
                    534:        { "ftr", roff_line_ignore, NULL, NULL, 0, NULL },
                    535:        { "fzoom", roff_line_ignore, NULL, NULL, 0, NULL },
                    536:        { "gcolor", roff_line_ignore, NULL, NULL, 0, NULL },
                    537:        { "hc", roff_line_ignore, NULL, NULL, 0, NULL },
                    538:        { "hcode", roff_line_ignore, NULL, NULL, 0, NULL },
                    539:        { "hidechar", roff_line_ignore, NULL, NULL, 0, NULL },
                    540:        { "hla", roff_line_ignore, NULL, NULL, 0, NULL },
                    541:        { "hlm", roff_line_ignore, NULL, NULL, 0, NULL },
                    542:        { "hpf", roff_line_ignore, NULL, NULL, 0, NULL },
                    543:        { "hpfa", roff_line_ignore, NULL, NULL, 0, NULL },
                    544:        { "hpfcode", roff_line_ignore, NULL, NULL, 0, NULL },
1.186     schwarze  545:        { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
1.103     kristaps  546:        { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  547:        { "hylang", roff_line_ignore, NULL, NULL, 0, NULL },
                    548:        { "hylen", roff_line_ignore, NULL, NULL, 0, NULL },
                    549:        { "hym", roff_line_ignore, NULL, NULL, 0, NULL },
                    550:        { "hypp", roff_line_ignore, NULL, NULL, 0, NULL },
                    551:        { "hys", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  552:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    553:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    554:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251     schwarze  555:        { "index", roff_unsupp, NULL, NULL, 0, NULL },
1.178     schwarze  556:        { "it", roff_it, NULL, NULL, 0, NULL },
1.251     schwarze  557:        { "itc", roff_unsupp, NULL, NULL, 0, NULL },
                    558:        { "IX", roff_line_ignore, NULL, NULL, 0, NULL },
                    559:        { "kern", roff_line_ignore, NULL, NULL, 0, NULL },
                    560:        { "kernafter", roff_line_ignore, NULL, NULL, 0, NULL },
                    561:        { "kernbefore", roff_line_ignore, NULL, NULL, 0, NULL },
                    562:        { "kernpair", roff_line_ignore, NULL, NULL, 0, NULL },
                    563:        { "lc", roff_unsupp, NULL, NULL, 0, NULL },
                    564:        { "lc_ctype", roff_unsupp, NULL, NULL, 0, NULL },
                    565:        { "lds", roff_unsupp, NULL, NULL, 0, NULL },
                    566:        { "length", roff_unsupp, NULL, NULL, 0, NULL },
                    567:        { "letadj", roff_line_ignore, NULL, NULL, 0, NULL },
                    568:        { "lf", roff_insec, NULL, NULL, 0, NULL },
                    569:        { "lg", roff_line_ignore, NULL, NULL, 0, NULL },
                    570:        { "lhang", roff_line_ignore, NULL, NULL, 0, NULL },
                    571:        { "linetabs", roff_unsupp, NULL, NULL, 0, NULL },
                    572:        { "lnr", roff_unsupp, NULL, NULL, 0, NULL },
                    573:        { "lnrf", roff_unsupp, NULL, NULL, 0, NULL },
                    574:        { "lpfx", roff_unsupp, NULL, NULL, 0, NULL },
                    575:        { "ls", roff_line_ignore, NULL, NULL, 0, NULL },
                    576:        { "lsm", roff_unsupp, NULL, NULL, 0, NULL },
                    577:        { "lt", roff_line_ignore, NULL, NULL, 0, NULL },
                    578:        { "mc", roff_line_ignore, NULL, NULL, 0, NULL },
                    579:        { "mediasize", roff_line_ignore, NULL, NULL, 0, NULL },
                    580:        { "minss", roff_line_ignore, NULL, NULL, 0, NULL },
                    581:        { "mk", roff_line_ignore, NULL, NULL, 0, NULL },
                    582:        { "mso", roff_insec, NULL, NULL, 0, NULL },
1.103     kristaps  583:        { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
                    584:        { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  585:        { "nhychar", roff_line_ignore, NULL, NULL, 0, NULL },
                    586:        { "nm", roff_unsupp, NULL, NULL, 0, NULL },
                    587:        { "nn", roff_unsupp, NULL, NULL, 0, NULL },
                    588:        { "nop", roff_unsupp, NULL, NULL, 0, NULL },
1.104     kristaps  589:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.251     schwarze  590:        { "nrf", roff_unsupp, NULL, NULL, 0, NULL },
                    591:        { "nroff", roff_line_ignore, NULL, NULL, 0, NULL },
1.124     schwarze  592:        { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  593:        { "nx", roff_insec, NULL, NULL, 0, NULL },
                    594:        { "open", roff_insec, NULL, NULL, 0, NULL },
                    595:        { "opena", roff_insec, NULL, NULL, 0, NULL },
                    596:        { "os", roff_line_ignore, NULL, NULL, 0, NULL },
                    597:        { "output", roff_unsupp, NULL, NULL, 0, NULL },
                    598:        { "padj", roff_line_ignore, NULL, NULL, 0, NULL },
                    599:        { "papersize", roff_line_ignore, NULL, NULL, 0, NULL },
                    600:        { "pc", roff_line_ignore, NULL, NULL, 0, NULL },
                    601:        { "pev", roff_line_ignore, NULL, NULL, 0, NULL },
                    602:        { "pi", roff_insec, NULL, NULL, 0, NULL },
                    603:        { "PI", roff_unsupp, NULL, NULL, 0, NULL },
1.229     schwarze  604:        { "pl", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  605:        { "pm", roff_line_ignore, NULL, NULL, 0, NULL },
                    606:        { "pn", roff_line_ignore, NULL, NULL, 0, NULL },
                    607:        { "pnr", roff_line_ignore, NULL, NULL, 0, NULL },
                    608:        { "po", roff_line_ignore, NULL, NULL, 0, NULL },
1.124     schwarze  609:        { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  610:        { "psbb", roff_unsupp, NULL, NULL, 0, NULL },
                    611:        { "pshape", roff_line_ignore, NULL, NULL, 0, NULL },
                    612:        { "pso", roff_insec, NULL, NULL, 0, NULL },
                    613:        { "ptr", roff_line_ignore, NULL, NULL, 0, NULL },
                    614:        { "pvs", roff_line_ignore, NULL, NULL, 0, NULL },
                    615:        { "rchar", roff_unsupp, NULL, NULL, 0, NULL },
                    616:        { "rd", roff_line_ignore, NULL, NULL, 0, NULL },
                    617:        { "recursionlimit", roff_line_ignore, NULL, NULL, 0, NULL },
                    618:        { "return", roff_unsupp, NULL, NULL, 0, NULL },
                    619:        { "rfschar", roff_unsupp, NULL, NULL, 0, NULL },
                    620:        { "rhang", roff_line_ignore, NULL, NULL, 0, NULL },
                    621:        { "rj", roff_line_ignore, NULL, NULL, 0, NULL },
1.122     schwarze  622:        { "rm", roff_rm, NULL, NULL, 0, NULL },
1.251     schwarze  623:        { "rn", roff_unsupp, NULL, NULL, 0, NULL },
                    624:        { "rnn", roff_unsupp, NULL, NULL, 0, NULL },
1.203     schwarze  625:        { "rr", roff_rr, NULL, NULL, 0, NULL },
1.251     schwarze  626:        { "rs", roff_line_ignore, NULL, NULL, 0, NULL },
                    627:        { "rt", roff_line_ignore, NULL, NULL, 0, NULL },
                    628:        { "schar", roff_unsupp, NULL, NULL, 0, NULL },
                    629:        { "sentchar", roff_line_ignore, NULL, NULL, 0, NULL },
                    630:        { "shc", roff_line_ignore, NULL, NULL, 0, NULL },
                    631:        { "shift", roff_unsupp, NULL, NULL, 0, NULL },
                    632:        { "sizes", roff_line_ignore, NULL, NULL, 0, NULL },
1.105     kristaps  633:        { "so", roff_so, NULL, NULL, 0, NULL },
1.251     schwarze  634:        { "spacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
                    635:        { "special", roff_line_ignore, NULL, NULL, 0, NULL },
                    636:        { "spreadwarn", roff_line_ignore, NULL, NULL, 0, NULL },
                    637:        { "ss", roff_line_ignore, NULL, NULL, 0, NULL },
                    638:        { "sty", roff_line_ignore, NULL, NULL, 0, NULL },
                    639:        { "substring", roff_unsupp, NULL, NULL, 0, NULL },
                    640:        { "sv", roff_line_ignore, NULL, NULL, 0, NULL },
                    641:        { "sy", roff_insec, NULL, NULL, 0, NULL },
                    642:        { "T&", roff_T_, NULL, NULL, 0, NULL },
1.124     schwarze  643:        { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  644:        { "tc", roff_unsupp, NULL, NULL, 0, NULL },
                    645:        { "TE", roff_TE, NULL, NULL, 0, NULL },
                    646:        { "TH", roff_TH, NULL, NULL, 0, NULL },
                    647:        { "ti", roff_line_ignore, NULL, NULL, 0, NULL },
                    648:        { "tkf", roff_line_ignore, NULL, NULL, 0, NULL },
                    649:        { "tl", roff_unsupp, NULL, NULL, 0, NULL },
                    650:        { "tm", roff_line_ignore, NULL, NULL, 0, NULL },
                    651:        { "tm1", roff_line_ignore, NULL, NULL, 0, NULL },
                    652:        { "tmc", roff_line_ignore, NULL, NULL, 0, NULL },
1.164     kristaps  653:        { "tr", roff_tr, NULL, NULL, 0, NULL },
1.251     schwarze  654:        { "track", roff_line_ignore, NULL, NULL, 0, NULL },
                    655:        { "transchar", roff_line_ignore, NULL, NULL, 0, NULL },
                    656:        { "trf", roff_insec, NULL, NULL, 0, NULL },
                    657:        { "trimat", roff_line_ignore, NULL, NULL, 0, NULL },
                    658:        { "trin", roff_unsupp, NULL, NULL, 0, NULL },
                    659:        { "trnt", roff_unsupp, NULL, NULL, 0, NULL },
                    660:        { "troff", roff_line_ignore, NULL, NULL, 0, NULL },
1.109     kristaps  661:        { "TS", roff_TS, NULL, NULL, 0, NULL },
1.251     schwarze  662:        { "uf", roff_line_ignore, NULL, NULL, 0, NULL },
                    663:        { "ul", roff_line_ignore, NULL, NULL, 0, NULL },
                    664:        { "unformat", roff_unsupp, NULL, NULL, 0, NULL },
                    665:        { "unwatch", roff_line_ignore, NULL, NULL, 0, NULL },
                    666:        { "unwatchn", roff_line_ignore, NULL, NULL, 0, NULL },
                    667:        { "vpt", roff_line_ignore, NULL, NULL, 0, NULL },
                    668:        { "vs", roff_line_ignore, NULL, NULL, 0, NULL },
                    669:        { "warn", roff_line_ignore, NULL, NULL, 0, NULL },
                    670:        { "warnscale", roff_line_ignore, NULL, NULL, 0, NULL },
                    671:        { "watch", roff_line_ignore, NULL, NULL, 0, NULL },
                    672:        { "watchlength", roff_line_ignore, NULL, NULL, 0, NULL },
                    673:        { "watchn", roff_line_ignore, NULL, NULL, 0, NULL },
                    674:        { "wh", roff_unsupp, NULL, NULL, 0, NULL },
                    675:        { "while", roff_unsupp, NULL, NULL, 0, NULL },
                    676:        { "write", roff_insec, NULL, NULL, 0, NULL },
                    677:        { "writec", roff_insec, NULL, NULL, 0, NULL },
                    678:        { "writem", roff_insec, NULL, NULL, 0, NULL },
                    679:        { "xflag", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  680:        { ".", roff_cblock, NULL, NULL, 0, NULL },
1.106     kristaps  681:        { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67      kristaps  682: };
                    683:
1.200     schwarze  684: /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
1.175     schwarze  685: const  char *const __mdoc_reserved[] = {
                    686:        "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
                    687:        "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
                    688:        "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
                    689:        "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
1.200     schwarze  690:        "Dt", "Dv", "Dx", "D1",
                    691:        "Ec", "Ed", "Ef", "Ek", "El", "Em",
                    692:        "En", "Eo", "Er", "Es", "Ev", "Ex",
1.175     schwarze  693:        "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
1.200     schwarze  694:        "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
                    695:        "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
1.175     schwarze  696:        "Oc", "Oo", "Op", "Os", "Ot", "Ox",
1.200     schwarze  697:        "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
                    698:        "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
                    699:        "Sc", "Sh", "Sm", "So", "Sq",
1.175     schwarze  700:        "Ss", "St", "Sx", "Sy",
                    701:        "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
1.200     schwarze  702:        "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
1.175     schwarze  703:        "%P", "%Q", "%R", "%T", "%U", "%V",
                    704:        NULL
                    705: };
                    706:
1.200     schwarze  707: /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
1.175     schwarze  708: const  char *const __man_reserved[] = {
1.200     schwarze  709:        "AT", "B", "BI", "BR", "DT",
                    710:        "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
                    711:        "LP", "OP", "P", "PD", "PP",
                    712:        "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
                    713:        "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
1.175     schwarze  714:        NULL
                    715: };
                    716:
1.141     kristaps  717: /* Array of injected predefined strings. */
                    718: #define        PREDEFS_MAX      38
                    719: static const struct predef predefs[PREDEFS_MAX] = {
                    720: #include "predefs.in"
                    721: };
                    722:
1.155     kristaps  723: /* See roffhash_find() */
1.85      kristaps  724: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    725:
1.178     schwarze  726: static int      roffit_lines;  /* number of lines to delay */
                    727: static char    *roffit_macro;  /* nil-terminated macro line */
                    728:
1.207     schwarze  729:
1.85      kristaps  730: static void
1.155     kristaps  731: roffhash_init(void)
1.85      kristaps  732: {
                    733:        struct roffmac   *n;
                    734:        int               buc, i;
                    735:
1.106     kristaps  736:        for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85      kristaps  737:                assert(roffs[i].name[0] >= ASCII_LO);
                    738:                assert(roffs[i].name[0] <= ASCII_HI);
                    739:
                    740:                buc = ROFF_HASH(roffs[i].name);
                    741:
                    742:                if (NULL != (n = hash[buc])) {
                    743:                        for ( ; n->next; n = n->next)
                    744:                                /* Do nothing. */ ;
                    745:                        n->next = &roffs[i];
                    746:                } else
                    747:                        hash[buc] = &roffs[i];
                    748:        }
                    749: }
                    750:
1.67      kristaps  751: /*
                    752:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    753:  * the nil-terminated string name could be found.
                    754:  */
                    755: static enum rofft
1.155     kristaps  756: roffhash_find(const char *p, size_t s)
1.67      kristaps  757: {
1.85      kristaps  758:        int              buc;
                    759:        struct roffmac  *n;
1.67      kristaps  760:
1.85      kristaps  761:        /*
                    762:         * libroff has an extremely simple hashtable, for the time
                    763:         * being, which simply keys on the first character, which must
                    764:         * be printable, then walks a chain.  It works well enough until
                    765:         * optimised.
                    766:         */
                    767:
                    768:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    769:                return(ROFF_MAX);
                    770:
                    771:        buc = ROFF_HASH(p);
                    772:
                    773:        if (NULL == (n = hash[buc]))
                    774:                return(ROFF_MAX);
                    775:        for ( ; n; n = n->next)
1.106     kristaps  776:                if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85      kristaps  777:                        return((enum rofft)(n - roffs));
1.67      kristaps  778:
                    779:        return(ROFF_MAX);
                    780: }
                    781:
                    782: /*
                    783:  * Pop the current node off of the stack of roff instructions currently
                    784:  * pending.
                    785:  */
                    786: static void
                    787: roffnode_pop(struct roff *r)
                    788: {
                    789:        struct roffnode *p;
                    790:
1.75      kristaps  791:        assert(r->last);
1.207     schwarze  792:        p = r->last;
1.82      kristaps  793:
1.75      kristaps  794:        r->last = r->last->parent;
1.106     kristaps  795:        free(p->name);
                    796:        free(p->end);
1.67      kristaps  797:        free(p);
                    798: }
                    799:
                    800: /*
                    801:  * Push a roff node onto the instruction stack.  This must later be
                    802:  * removed with roffnode_pop().
                    803:  */
1.98      schwarze  804: static void
1.106     kristaps  805: roffnode_push(struct roff *r, enum rofft tok, const char *name,
                    806:                int line, int col)
1.67      kristaps  807: {
                    808:        struct roffnode *p;
                    809:
1.98      schwarze  810:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  811:        p->tok = tok;
1.106     kristaps  812:        if (name)
                    813:                p->name = mandoc_strdup(name);
1.67      kristaps  814:        p->parent = r->last;
                    815:        p->line = line;
                    816:        p->col = col;
1.198     schwarze  817:        p->rule = p->parent ? p->parent->rule : 0;
1.67      kristaps  818:
                    819:        r->last = p;
                    820: }
                    821:
                    822: static void
                    823: roff_free1(struct roff *r)
                    824: {
1.176     schwarze  825:        struct tbl_node *tbl;
1.125     kristaps  826:        struct eqn_node *e;
1.167     kristaps  827:        int              i;
1.67      kristaps  828:
1.176     schwarze  829:        while (NULL != (tbl = r->first_tbl)) {
                    830:                r->first_tbl = tbl->next;
                    831:                tbl_free(tbl);
1.109     kristaps  832:        }
1.113     kristaps  833:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    834:
1.125     kristaps  835:        while (NULL != (e = r->first_eqn)) {
                    836:                r->first_eqn = e->next;
                    837:                eqn_free(e);
                    838:        }
                    839:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    840:
1.67      kristaps  841:        while (r->last)
                    842:                roffnode_pop(r);
1.109     kristaps  843:
1.223     schwarze  844:        free (r->rstack);
                    845:        r->rstack = NULL;
                    846:        r->rstacksz = 0;
                    847:        r->rstackpos = -1;
                    848:
                    849:        roff_freereg(r->regtab);
                    850:        r->regtab = NULL;
                    851:
1.167     kristaps  852:        roff_freestr(r->strtab);
                    853:        roff_freestr(r->xmbtab);
                    854:        r->strtab = r->xmbtab = NULL;
                    855:
                    856:        if (r->xtab)
                    857:                for (i = 0; i < 128; i++)
                    858:                        free(r->xtab[i].p);
                    859:        free(r->xtab);
                    860:        r->xtab = NULL;
1.67      kristaps  861: }
                    862:
                    863: void
                    864: roff_reset(struct roff *r)
                    865: {
                    866:
                    867:        roff_free1(r);
1.227     schwarze  868:        r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.174     kristaps  869:        r->control = 0;
1.67      kristaps  870: }
                    871:
                    872: void
                    873: roff_free(struct roff *r)
                    874: {
                    875:
                    876:        roff_free1(r);
                    877:        free(r);
                    878: }
                    879:
                    880: struct roff *
1.237     schwarze  881: roff_alloc(struct mparse *parse, const struct mchars *mchars, int options)
1.67      kristaps  882: {
                    883:        struct roff     *r;
                    884:
1.98      schwarze  885:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  886:        r->parse = parse;
1.237     schwarze  887:        r->mchars = mchars;
1.199     schwarze  888:        r->options = options;
1.227     schwarze  889:        r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.82      kristaps  890:        r->rstackpos = -1;
1.207     schwarze  891:
1.155     kristaps  892:        roffhash_init();
1.141     kristaps  893:
1.67      kristaps  894:        return(r);
                    895: }
                    896:
1.94      kristaps  897: /*
1.206     schwarze  898:  * In the current line, expand escape sequences that tend to get
                    899:  * used in numerical expressions and conditional requests.
                    900:  * Also check the syntax of the remaining escape sequences.
1.154     kristaps  901:  */
1.172     schwarze  902: static enum rofferr
1.238     schwarze  903: roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1.94      kristaps  904: {
1.208     schwarze  905:        char             ubuf[24]; /* buffer to print the number */
1.205     schwarze  906:        const char      *start; /* start of the string to process */
1.209     schwarze  907:        char            *stesc; /* start of an escape sequence ('\\') */
1.108     schwarze  908:        const char      *stnam; /* start of the name, after "[(*" */
                    909:        const char      *cp;    /* end of the name, e.g. before ']' */
                    910:        const char      *res;   /* the string to be substituted */
1.238     schwarze  911:        char            *nbuf;  /* new buffer to copy buf->buf to */
1.181     schwarze  912:        size_t           maxl;  /* expected length of the escape name */
                    913:        size_t           naml;  /* actual length of the escape name */
1.237     schwarze  914:        enum mandoc_esc  esc;   /* type of the escape sequence */
                    915:        int              inaml; /* length returned from mandoc_escape() */
1.181     schwarze  916:        int              expand_count;  /* to avoid infinite loops */
1.206     schwarze  917:        int              npos;  /* position in numeric expression */
1.218     schwarze  918:        int              arg_complete; /* argument not interrupted by eol */
1.206     schwarze  919:        char             term;  /* character terminating the escape */
1.94      kristaps  920:
1.170     schwarze  921:        expand_count = 0;
1.238     schwarze  922:        start = buf->buf + pos;
1.205     schwarze  923:        stesc = strchr(start, '\0') - 1;
                    924:        while (stesc-- > start) {
1.170     schwarze  925:
1.205     schwarze  926:                /* Search backwards for the next backslash. */
                    927:
1.238     schwarze  928:                if (*stesc != '\\')
1.205     schwarze  929:                        continue;
                    930:
                    931:                /* If it is escaped, skip it. */
                    932:
                    933:                for (cp = stesc - 1; cp >= start; cp--)
1.238     schwarze  934:                        if (*cp != '\\')
1.205     schwarze  935:                                break;
                    936:
1.238     schwarze  937:                if ((stesc - cp) % 2 == 0) {
1.209     schwarze  938:                        stesc = (char *)cp;
1.205     schwarze  939:                        continue;
                    940:                }
1.108     schwarze  941:
1.206     schwarze  942:                /* Decide whether to expand or to check only. */
1.108     schwarze  943:
1.206     schwarze  944:                term = '\0';
1.205     schwarze  945:                cp = stesc + 1;
1.181     schwarze  946:                switch (*cp) {
1.207     schwarze  947:                case '*':
1.181     schwarze  948:                        res = NULL;
                    949:                        break;
1.207     schwarze  950:                case 'B':
1.206     schwarze  951:                        /* FALLTHROUGH */
1.207     schwarze  952:                case 'w':
1.206     schwarze  953:                        term = cp[1];
                    954:                        /* FALLTHROUGH */
1.207     schwarze  955:                case 'n':
1.181     schwarze  956:                        res = ubuf;
                    957:                        break;
                    958:                default:
1.237     schwarze  959:                        esc = mandoc_escape(&cp, &stnam, &inaml);
                    960:                        if (esc == ESCAPE_ERROR ||
                    961:                            (esc == ESCAPE_SPECIAL &&
                    962:                             mchars_spec2cp(r->mchars, stnam, inaml) < 0))
1.219     schwarze  963:                                mandoc_vmsg(MANDOCERR_ESC_BAD,
1.238     schwarze  964:                                    r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze  965:                                    "%.*s", (int)(cp - stesc), stesc);
1.205     schwarze  966:                        continue;
1.152     kristaps  967:                }
                    968:
1.205     schwarze  969:                if (EXPAND_LIMIT < ++expand_count) {
                    970:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1.238     schwarze  971:                            ln, (int)(stesc - buf->buf), NULL);
1.205     schwarze  972:                        return(ROFF_IGN);
                    973:                }
1.108     schwarze  974:
                    975:                /*
                    976:                 * The third character decides the length
1.181     schwarze  977:                 * of the name of the string or register.
1.108     schwarze  978:                 * Save a pointer to the name.
                    979:                 */
                    980:
1.238     schwarze  981:                if (term == '\0') {
1.206     schwarze  982:                        switch (*++cp) {
1.207     schwarze  983:                        case '\0':
1.206     schwarze  984:                                maxl = 0;
                    985:                                break;
1.207     schwarze  986:                        case '(':
1.206     schwarze  987:                                cp++;
                    988:                                maxl = 2;
                    989:                                break;
1.207     schwarze  990:                        case '[':
1.206     schwarze  991:                                cp++;
                    992:                                term = ']';
                    993:                                maxl = 0;
                    994:                                break;
                    995:                        default:
                    996:                                maxl = 1;
                    997:                                break;
                    998:                        }
                    999:                } else {
                   1000:                        cp += 2;
1.94      kristaps 1001:                        maxl = 0;
                   1002:                }
1.108     schwarze 1003:                stnam = cp;
1.94      kristaps 1004:
1.108     schwarze 1005:                /* Advance to the end of the name. */
1.94      kristaps 1006:
1.253   ! schwarze 1007:                naml = 0;
1.218     schwarze 1008:                arg_complete = 1;
1.253   ! schwarze 1009:                while (maxl == 0 || naml < maxl) {
1.238     schwarze 1010:                        if (*cp == '\0') {
1.219     schwarze 1011:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 1012:                                    ln, (int)(stesc - buf->buf), stesc);
1.218     schwarze 1013:                                arg_complete = 0;
1.206     schwarze 1014:                                break;
1.153     kristaps 1015:                        }
1.238     schwarze 1016:                        if (maxl == 0 && *cp == term) {
1.206     schwarze 1017:                                cp++;
1.253   ! schwarze 1018:                                break;
        !          1019:                        }
        !          1020:                        if (*cp++ != '\\' || stesc[1] != 'w') {
        !          1021:                                naml++;
        !          1022:                                continue;
        !          1023:                        }
        !          1024:                        switch (mandoc_escape(&cp, NULL, NULL)) {
        !          1025:                        case ESCAPE_SPECIAL:
        !          1026:                                /* FALLTHROUGH */
        !          1027:                        case ESCAPE_UNICODE:
        !          1028:                                /* FALLTHROUGH */
        !          1029:                        case ESCAPE_NUMBERED:
        !          1030:                                /* FALLTHROUGH */
        !          1031:                        case ESCAPE_OVERSTRIKE:
        !          1032:                                naml++;
        !          1033:                                break;
        !          1034:                        default:
1.94      kristaps 1035:                                break;
1.206     schwarze 1036:                        }
1.94      kristaps 1037:                }
                   1038:
1.108     schwarze 1039:                /*
                   1040:                 * Retrieve the replacement string; if it is
                   1041:                 * undefined, resume searching for escapes.
                   1042:                 */
                   1043:
1.206     schwarze 1044:                switch (stesc[1]) {
1.207     schwarze 1045:                case '*':
1.218     schwarze 1046:                        if (arg_complete)
                   1047:                                res = roff_getstrn(r, stnam, naml);
1.206     schwarze 1048:                        break;
1.207     schwarze 1049:                case 'B':
1.206     schwarze 1050:                        npos = 0;
1.218     schwarze 1051:                        ubuf[0] = arg_complete &&
1.234     kristaps 1052:                            roff_evalnum(r, ln, stnam, &npos, NULL, 0) &&
1.218     schwarze 1053:                            stnam + npos + 1 == cp ? '1' : '0';
1.206     schwarze 1054:                        ubuf[1] = '\0';
                   1055:                        break;
1.207     schwarze 1056:                case 'n':
1.218     schwarze 1057:                        if (arg_complete)
                   1058:                                (void)snprintf(ubuf, sizeof(ubuf), "%d",
                   1059:                                    roff_getregn(r, stnam, naml));
                   1060:                        else
                   1061:                                ubuf[0] = '\0';
1.206     schwarze 1062:                        break;
1.207     schwarze 1063:                case 'w':
1.218     schwarze 1064:                        /* use even incomplete args */
1.208     schwarze 1065:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206     schwarze 1066:                            24 * (int)naml);
                   1067:                        break;
                   1068:                }
1.94      kristaps 1069:
1.238     schwarze 1070:                if (res == NULL) {
1.219     schwarze 1071:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1072:                            r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze 1073:                            "%.*s", (int)naml, stnam);
1.142     kristaps 1074:                        res = "";
1.246     schwarze 1075:                } else if (buf->sz + strlen(res) > SHRT_MAX) {
                   1076:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                   1077:                            ln, (int)(stesc - buf->buf), NULL);
                   1078:                        return(ROFF_IGN);
1.94      kristaps 1079:                }
                   1080:
1.108     schwarze 1081:                /* Replace the escape sequence by the string. */
                   1082:
1.209     schwarze 1083:                *stesc = '\0';
1.238     schwarze 1084:                buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
                   1085:                    buf->buf, res, cp) + 1;
1.94      kristaps 1086:
1.205     schwarze 1087:                /* Prepare for the next replacement. */
1.94      kristaps 1088:
1.205     schwarze 1089:                start = nbuf + pos;
1.238     schwarze 1090:                stesc = nbuf + (stesc - buf->buf) + strlen(res);
                   1091:                free(buf->buf);
                   1092:                buf->buf = nbuf;
1.154     kristaps 1093:        }
1.172     schwarze 1094:        return(ROFF_CONT);
1.154     kristaps 1095: }
                   1096:
                   1097: /*
1.178     schwarze 1098:  * Process text streams:
                   1099:  * Convert all breakable hyphens into ASCII_HYPH.
                   1100:  * Decrement and spring input line trap.
1.154     kristaps 1101:  */
                   1102: static enum rofferr
1.238     schwarze 1103: roff_parsetext(struct buf *buf, int pos, int *offs)
1.154     kristaps 1104: {
                   1105:        size_t           sz;
                   1106:        const char      *start;
1.178     schwarze 1107:        char            *p;
                   1108:        int              isz;
1.154     kristaps 1109:        enum mandoc_esc  esc;
                   1110:
1.238     schwarze 1111:        start = p = buf->buf + pos;
1.154     kristaps 1112:
1.238     schwarze 1113:        while (*p != '\0') {
1.154     kristaps 1114:                sz = strcspn(p, "-\\");
                   1115:                p += sz;
                   1116:
1.238     schwarze 1117:                if (*p == '\0')
1.159     kristaps 1118:                        break;
                   1119:
1.238     schwarze 1120:                if (*p == '\\') {
1.154     kristaps 1121:                        /* Skip over escapes. */
                   1122:                        p++;
1.189     schwarze 1123:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238     schwarze 1124:                        if (esc == ESCAPE_ERROR)
1.154     kristaps 1125:                                break;
1.155     kristaps 1126:                        continue;
1.159     kristaps 1127:                } else if (p == start) {
1.158     kristaps 1128:                        p++;
1.155     kristaps 1129:                        continue;
1.158     kristaps 1130:                }
1.155     kristaps 1131:
1.171     schwarze 1132:                if (isalpha((unsigned char)p[-1]) &&
                   1133:                    isalpha((unsigned char)p[1]))
1.155     kristaps 1134:                        *p = ASCII_HYPH;
                   1135:                p++;
1.94      kristaps 1136:        }
                   1137:
1.178     schwarze 1138:        /* Spring the input line trap. */
1.238     schwarze 1139:        if (roffit_lines == 1) {
                   1140:                isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
                   1141:                free(buf->buf);
                   1142:                buf->buf = p;
                   1143:                buf->sz = isz + 1;
1.178     schwarze 1144:                *offs = 0;
                   1145:                free(roffit_macro);
                   1146:                roffit_lines = 0;
                   1147:                return(ROFF_REPARSE);
1.238     schwarze 1148:        } else if (roffit_lines > 1)
1.178     schwarze 1149:                --roffit_lines;
1.154     kristaps 1150:        return(ROFF_CONT);
1.94      kristaps 1151: }
                   1152:
1.67      kristaps 1153: enum rofferr
1.238     schwarze 1154: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67      kristaps 1155: {
                   1156:        enum rofft       t;
1.109     kristaps 1157:        enum rofferr     e;
1.238     schwarze 1158:        int              pos;   /* parse point */
1.243     schwarze 1159:        int              spos;  /* saved parse point for messages */
1.238     schwarze 1160:        int              ppos;  /* original offset in buf->buf */
                   1161:        int              ctl;   /* macro line (boolean) */
                   1162:
                   1163:        ppos = pos = *offs;
1.79      kristaps 1164:
1.230     schwarze 1165:        /* Handle in-line equation delimiters. */
                   1166:
1.236     schwarze 1167:        if (r->tbl == NULL &&
                   1168:            r->last_eqn != NULL && r->last_eqn->delim &&
1.230     schwarze 1169:            (r->eqn == NULL || r->eqn_inline)) {
1.238     schwarze 1170:                e = roff_eqndelim(r, buf, pos);
1.230     schwarze 1171:                if (e == ROFF_REPARSE)
                   1172:                        return(e);
                   1173:                assert(e == ROFF_CONT);
                   1174:        }
                   1175:
                   1176:        /* Expand some escape sequences. */
1.94      kristaps 1177:
1.238     schwarze 1178:        e = roff_res(r, buf, ln, pos);
                   1179:        if (e == ROFF_IGN)
1.172     schwarze 1180:                return(e);
1.238     schwarze 1181:        assert(e == ROFF_CONT);
1.94      kristaps 1182:
1.238     schwarze 1183:        ctl = roff_getcontrol(r, buf->buf, &pos);
1.130     kristaps 1184:
1.94      kristaps 1185:        /*
1.79      kristaps 1186:         * First, if a scope is open and we're not a macro, pass the
1.252     schwarze 1187:         * text through the macro's filter.
                   1188:         * Equations process all content themselves.
                   1189:         * Tables process almost all content themselves, but we want
                   1190:         * to warn about macros before passing it there.
1.79      kristaps 1191:         */
1.74      kristaps 1192:
1.252     schwarze 1193:        if (r->last != NULL && ! ctl) {
1.78      kristaps 1194:                t = r->last->tok;
                   1195:                assert(roffs[t].text);
1.238     schwarze 1196:                e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
                   1197:                assert(e == ROFF_IGN || e == ROFF_CONT);
                   1198:                if (e != ROFF_CONT)
1.125     kristaps 1199:                        return(e);
1.182     schwarze 1200:        }
1.252     schwarze 1201:        if (r->eqn != NULL)
1.238     schwarze 1202:                return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
1.252     schwarze 1203:        if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
                   1204:                return(tbl_read(r->tbl, ln, buf->buf, pos));
                   1205:        if ( ! ctl)
1.238     schwarze 1206:                return(roff_parsetext(buf, pos, offs));
1.228     schwarze 1207:
                   1208:        /* Skip empty request lines. */
                   1209:
1.238     schwarze 1210:        if (buf->buf[pos] == '"') {
1.228     schwarze 1211:                mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
                   1212:                    ln, pos, NULL);
                   1213:                return(ROFF_IGN);
1.238     schwarze 1214:        } else if (buf->buf[pos] == '\0')
1.228     schwarze 1215:                return(ROFF_IGN);
1.67      kristaps 1216:
1.79      kristaps 1217:        /*
                   1218:         * If a scope is open, go to the child handler for that macro,
                   1219:         * as it may want to preprocess before doing anything with it.
1.125     kristaps 1220:         * Don't do so if an equation is open.
1.79      kristaps 1221:         */
1.78      kristaps 1222:
1.79      kristaps 1223:        if (r->last) {
                   1224:                t = r->last->tok;
                   1225:                assert(roffs[t].sub);
1.238     schwarze 1226:                return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
1.79      kristaps 1227:        }
1.78      kristaps 1228:
1.243     schwarze 1229:        /* No scope is open.  This is a new request or macro. */
                   1230:
                   1231:        spos = pos;
                   1232:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   1233:
                   1234:        /* Tables ignore most macros. */
                   1235:
                   1236:        if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
                   1237:                mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
                   1238:                    ln, pos, buf->buf + spos);
                   1239:                return(ROFF_IGN);
                   1240:        }
                   1241:
1.79      kristaps 1242:        /*
1.243     schwarze 1243:         * This is neither a roff request nor a user-defined macro.
                   1244:         * Let the standard macro set parsers handle it.
1.79      kristaps 1245:         */
1.67      kristaps 1246:
1.243     schwarze 1247:        if (t == ROFF_MAX)
1.79      kristaps 1248:                return(ROFF_CONT);
1.243     schwarze 1249:
                   1250:        /* Execute a roff request or a user defined macro. */
1.67      kristaps 1251:
1.75      kristaps 1252:        assert(roffs[t].proc);
1.238     schwarze 1253:        return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.74      kristaps 1254: }
                   1255:
1.117     kristaps 1256: void
1.74      kristaps 1257: roff_endparse(struct roff *r)
                   1258: {
                   1259:
1.110     kristaps 1260:        if (r->last)
1.221     schwarze 1261:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1262:                    r->last->line, r->last->col,
                   1263:                    roffs[r->last->tok].name);
1.117     kristaps 1264:
1.125     kristaps 1265:        if (r->eqn) {
1.221     schwarze 1266:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1267:                    r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151     kristaps 1268:                eqn_end(&r->eqn);
1.125     kristaps 1269:        }
                   1270:
1.117     kristaps 1271:        if (r->tbl) {
1.221     schwarze 1272:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1273:                    r->tbl->line, r->tbl->pos, "TS");
1.151     kristaps 1274:                tbl_end(&r->tbl);
1.117     kristaps 1275:        }
1.67      kristaps 1276: }
                   1277:
                   1278: /*
                   1279:  * Parse a roff node's type from the input buffer.  This must be in the
                   1280:  * form of ".foo xxx" in the usual way.
                   1281:  */
                   1282: static enum rofft
1.214     schwarze 1283: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67      kristaps 1284: {
1.214     schwarze 1285:        char            *cp;
1.106     kristaps 1286:        const char      *mac;
                   1287:        size_t           maclen;
1.67      kristaps 1288:        enum rofft       t;
                   1289:
1.214     schwarze 1290:        cp = buf + *pos;
                   1291:
                   1292:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67      kristaps 1293:                return(ROFF_MAX);
                   1294:
1.214     schwarze 1295:        mac = cp;
                   1296:        maclen = roff_getname(r, &cp, ln, ppos);
1.67      kristaps 1297:
1.106     kristaps 1298:        t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155     kristaps 1299:            ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67      kristaps 1300:
1.214     schwarze 1301:        if (ROFF_MAX != t)
                   1302:                *pos = cp - buf;
1.67      kristaps 1303:
                   1304:        return(t);
                   1305: }
                   1306:
                   1307: static enum rofferr
1.76      kristaps 1308: roff_cblock(ROFF_ARGS)
1.67      kristaps 1309: {
                   1310:
1.79      kristaps 1311:        /*
                   1312:         * A block-close `..' should only be invoked as a child of an
                   1313:         * ignore macro, otherwise raise a warning and just ignore it.
                   1314:         */
                   1315:
1.238     schwarze 1316:        if (r->last == NULL) {
1.221     schwarze 1317:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1318:                    ln, ppos, "..");
1.76      kristaps 1319:                return(ROFF_IGN);
                   1320:        }
1.67      kristaps 1321:
1.81      kristaps 1322:        switch (r->last->tok) {
1.207     schwarze 1323:        case ROFF_am:
1.220     schwarze 1324:                /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81      kristaps 1325:                /* FALLTHROUGH */
1.207     schwarze 1326:        case ROFF_ami:
1.81      kristaps 1327:                /* FALLTHROUGH */
1.207     schwarze 1328:        case ROFF_de:
1.108     schwarze 1329:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81      kristaps 1330:                /* FALLTHROUGH */
1.207     schwarze 1331:        case ROFF_dei:
1.81      kristaps 1332:                /* FALLTHROUGH */
1.207     schwarze 1333:        case ROFF_ig:
1.81      kristaps 1334:                break;
                   1335:        default:
1.221     schwarze 1336:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1337:                    ln, ppos, "..");
1.67      kristaps 1338:                return(ROFF_IGN);
1.76      kristaps 1339:        }
1.67      kristaps 1340:
1.238     schwarze 1341:        if (buf->buf[pos] != '\0')
1.217     schwarze 1342:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 1343:                    ".. %s", buf->buf + pos);
1.71      kristaps 1344:
                   1345:        roffnode_pop(r);
1.76      kristaps 1346:        roffnode_cleanscope(r);
                   1347:        return(ROFF_IGN);
1.71      kristaps 1348:
1.67      kristaps 1349: }
                   1350:
1.76      kristaps 1351: static void
                   1352: roffnode_cleanscope(struct roff *r)
1.67      kristaps 1353: {
                   1354:
1.76      kristaps 1355:        while (r->last) {
1.173     schwarze 1356:                if (--r->last->endspan != 0)
1.76      kristaps 1357:                        break;
                   1358:                roffnode_pop(r);
                   1359:        }
1.67      kristaps 1360: }
                   1361:
1.195     schwarze 1362: static void
                   1363: roff_ccond(struct roff *r, int ln, int ppos)
1.74      kristaps 1364: {
                   1365:
1.76      kristaps 1366:        if (NULL == r->last) {
1.221     schwarze 1367:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1368:                    ln, ppos, "\\}");
1.195     schwarze 1369:                return;
1.76      kristaps 1370:        }
                   1371:
1.82      kristaps 1372:        switch (r->last->tok) {
1.207     schwarze 1373:        case ROFF_el:
1.82      kristaps 1374:                /* FALLTHROUGH */
1.207     schwarze 1375:        case ROFF_ie:
1.82      kristaps 1376:                /* FALLTHROUGH */
1.207     schwarze 1377:        case ROFF_if:
1.82      kristaps 1378:                break;
                   1379:        default:
1.221     schwarze 1380:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1381:                    ln, ppos, "\\}");
1.195     schwarze 1382:                return;
1.75      kristaps 1383:        }
                   1384:
1.76      kristaps 1385:        if (r->last->endspan > -1) {
1.221     schwarze 1386:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1387:                    ln, ppos, "\\}");
1.195     schwarze 1388:                return;
1.76      kristaps 1389:        }
                   1390:
1.75      kristaps 1391:        roffnode_pop(r);
1.76      kristaps 1392:        roffnode_cleanscope(r);
1.195     schwarze 1393:        return;
1.76      kristaps 1394: }
                   1395:
                   1396: static enum rofferr
1.80      kristaps 1397: roff_block(ROFF_ARGS)
1.76      kristaps 1398: {
1.220     schwarze 1399:        const char      *name;
                   1400:        char            *iname, *cp;
1.213     schwarze 1401:        size_t           namesz;
1.106     kristaps 1402:
1.220     schwarze 1403:        /* Ignore groff compatibility mode for now. */
1.76      kristaps 1404:
1.238     schwarze 1405:        if (tok == ROFF_de1)
1.220     schwarze 1406:                tok = ROFF_de;
1.251     schwarze 1407:        else if (tok == ROFF_dei1)
                   1408:                tok = ROFF_dei;
1.238     schwarze 1409:        else if (tok == ROFF_am1)
1.220     schwarze 1410:                tok = ROFF_am;
1.251     schwarze 1411:        else if (tok == ROFF_ami1)
                   1412:                tok = ROFF_ami;
1.220     schwarze 1413:
                   1414:        /* Parse the macro name argument. */
                   1415:
1.238     schwarze 1416:        cp = buf->buf + pos;
                   1417:        if (tok == ROFF_ig) {
1.220     schwarze 1418:                iname = NULL;
                   1419:                namesz = 0;
                   1420:        } else {
                   1421:                iname = cp;
                   1422:                namesz = roff_getname(r, &cp, ln, ppos);
                   1423:                iname[namesz] = '\0';
                   1424:        }
1.107     kristaps 1425:
1.220     schwarze 1426:        /* Resolve the macro name argument if it is indirect. */
1.107     kristaps 1427:
1.238     schwarze 1428:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1429:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1430:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1431:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1432:                            "%.*s", (int)namesz, iname);
                   1433:                        namesz = 0;
                   1434:                } else
                   1435:                        namesz = strlen(name);
                   1436:        } else
                   1437:                name = iname;
1.107     kristaps 1438:
1.238     schwarze 1439:        if (namesz == 0 && tok != ROFF_ig) {
1.220     schwarze 1440:                mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
                   1441:                    ln, ppos, roffs[tok].name);
                   1442:                return(ROFF_IGN);
                   1443:        }
1.80      kristaps 1444:
1.106     kristaps 1445:        roffnode_push(r, tok, name, ln, ppos);
                   1446:
                   1447:        /*
                   1448:         * At the beginning of a `de' macro, clear the existing string
                   1449:         * with the same name, if there is one.  New content will be
1.193     schwarze 1450:         * appended from roff_block_text() in multiline mode.
1.106     kristaps 1451:         */
1.107     kristaps 1452:
1.238     schwarze 1453:        if (tok == ROFF_de || tok == ROFF_dei)
1.213     schwarze 1454:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76      kristaps 1455:
1.238     schwarze 1456:        if (*cp == '\0')
1.78      kristaps 1457:                return(ROFF_IGN);
                   1458:
1.220     schwarze 1459:        /* Get the custom end marker. */
1.107     kristaps 1460:
1.220     schwarze 1461:        iname = cp;
1.213     schwarze 1462:        namesz = roff_getname(r, &cp, ln, ppos);
1.220     schwarze 1463:
                   1464:        /* Resolve the end marker if it is indirect. */
                   1465:
1.238     schwarze 1466:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1467:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1468:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1469:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1470:                            "%.*s", (int)namesz, iname);
                   1471:                        namesz = 0;
                   1472:                } else
                   1473:                        namesz = strlen(name);
                   1474:        } else
                   1475:                name = iname;
                   1476:
1.213     schwarze 1477:        if (namesz)
                   1478:                r->last->end = mandoc_strndup(name, namesz);
1.78      kristaps 1479:
1.238     schwarze 1480:        if (*cp != '\0')
1.217     schwarze 1481:                mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
                   1482:                    ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74      kristaps 1483:
1.78      kristaps 1484:        return(ROFF_IGN);
                   1485: }
                   1486:
                   1487: static enum rofferr
1.80      kristaps 1488: roff_block_sub(ROFF_ARGS)
1.79      kristaps 1489: {
                   1490:        enum rofft      t;
                   1491:        int             i, j;
                   1492:
                   1493:        /*
                   1494:         * First check whether a custom macro exists at this level.  If
                   1495:         * it does, then check against it.  This is some of groff's
                   1496:         * stranger behaviours.  If we encountered a custom end-scope
                   1497:         * tag and that tag also happens to be a "real" macro, then we
                   1498:         * need to try interpreting it again as a real macro.  If it's
                   1499:         * not, then return ignore.  Else continue.
                   1500:         */
                   1501:
                   1502:        if (r->last->end) {
1.130     kristaps 1503:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238     schwarze 1504:                        if (buf->buf[i] != r->last->end[j])
1.79      kristaps 1505:                                break;
                   1506:
1.238     schwarze 1507:                if (r->last->end[j] == '\0' &&
                   1508:                    (buf->buf[i] == '\0' ||
                   1509:                     buf->buf[i] == ' ' ||
                   1510:                     buf->buf[i] == '\t')) {
1.79      kristaps 1511:                        roffnode_pop(r);
                   1512:                        roffnode_cleanscope(r);
                   1513:
1.238     schwarze 1514:                        while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130     kristaps 1515:                                i++;
                   1516:
                   1517:                        pos = i;
1.238     schwarze 1518:                        if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
                   1519:                            ROFF_MAX)
1.79      kristaps 1520:                                return(ROFF_RERUN);
                   1521:                        return(ROFF_IGN);
                   1522:                }
                   1523:        }
                   1524:
                   1525:        /*
                   1526:         * If we have no custom end-query or lookup failed, then try
                   1527:         * pulling it out of the hashtable.
                   1528:         */
                   1529:
1.238     schwarze 1530:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79      kristaps 1531:
1.238     schwarze 1532:        if (t != ROFF_cblock) {
                   1533:                if (tok != ROFF_ig)
                   1534:                        roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.79      kristaps 1535:                return(ROFF_IGN);
1.106     kristaps 1536:        }
1.79      kristaps 1537:
                   1538:        assert(roffs[t].proc);
1.238     schwarze 1539:        return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.79      kristaps 1540: }
                   1541:
                   1542: static enum rofferr
1.80      kristaps 1543: roff_block_text(ROFF_ARGS)
1.78      kristaps 1544: {
                   1545:
1.238     schwarze 1546:        if (tok != ROFF_ig)
                   1547:                roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106     kristaps 1548:
1.78      kristaps 1549:        return(ROFF_IGN);
                   1550: }
                   1551:
                   1552: static enum rofferr
1.82      kristaps 1553: roff_cond_sub(ROFF_ARGS)
                   1554: {
                   1555:        enum rofft       t;
1.139     kristaps 1556:        char            *ep;
1.198     schwarze 1557:        int              rr;
1.82      kristaps 1558:
                   1559:        rr = r->last->rule;
1.139     kristaps 1560:        roffnode_cleanscope(r);
1.238     schwarze 1561:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82      kristaps 1562:
1.139     kristaps 1563:        /*
1.177     schwarze 1564:         * Fully handle known macros when they are structurally
                   1565:         * required or when the conditional evaluated to true.
1.87      kristaps 1566:         */
                   1567:
1.238     schwarze 1568:        if ((t != ROFF_MAX) &&
                   1569:            (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1.177     schwarze 1570:                assert(roffs[t].proc);
1.238     schwarze 1571:                return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.177     schwarze 1572:        }
1.144     kristaps 1573:
1.196     schwarze 1574:        /*
                   1575:         * If `\}' occurs on a macro line without a preceding macro,
                   1576:         * drop the line completely.
                   1577:         */
                   1578:
1.238     schwarze 1579:        ep = buf->buf + pos;
                   1580:        if (ep[0] == '\\' && ep[1] == '}')
1.198     schwarze 1581:                rr = 0;
1.196     schwarze 1582:
1.177     schwarze 1583:        /* Always check for the closing delimiter `\}'. */
1.144     kristaps 1584:
1.238     schwarze 1585:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1586:                if (*(++ep) == '}') {
1.197     schwarze 1587:                        *ep = '&';
1.238     schwarze 1588:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1589:                }
1.247     schwarze 1590:                if (*ep != '\0')
                   1591:                        ++ep;
1.177     schwarze 1592:        }
1.198     schwarze 1593:        return(rr ? ROFF_CONT : ROFF_IGN);
1.82      kristaps 1594: }
                   1595:
                   1596: static enum rofferr
                   1597: roff_cond_text(ROFF_ARGS)
1.78      kristaps 1598: {
1.140     kristaps 1599:        char            *ep;
1.198     schwarze 1600:        int              rr;
1.82      kristaps 1601:
                   1602:        rr = r->last->rule;
1.140     kristaps 1603:        roffnode_cleanscope(r);
1.82      kristaps 1604:
1.238     schwarze 1605:        ep = buf->buf + pos;
                   1606:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1607:                if (*(++ep) == '}') {
1.197     schwarze 1608:                        *ep = '&';
1.238     schwarze 1609:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1610:                }
1.247     schwarze 1611:                if (*ep != '\0')
                   1612:                        ++ep;
1.78      kristaps 1613:        }
1.198     schwarze 1614:        return(rr ? ROFF_CONT : ROFF_IGN);
1.74      kristaps 1615: }
                   1616:
1.204     schwarze 1617: /*
                   1618:  * Parse a single signed integer number.  Stop at the first non-digit.
                   1619:  * If there is at least one digit, return success and advance the
                   1620:  * parse point, else return failure and let the parse point unchanged.
                   1621:  * Ignore overflows, treat them just like the C language.
                   1622:  */
1.184     schwarze 1623: static int
                   1624: roff_getnum(const char *v, int *pos, int *res)
                   1625: {
1.206     schwarze 1626:        int      myres, n, p;
                   1627:
                   1628:        if (NULL == res)
                   1629:                res = &myres;
1.184     schwarze 1630:
                   1631:        p = *pos;
                   1632:        n = v[p] == '-';
                   1633:        if (n)
                   1634:                p++;
                   1635:
                   1636:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204     schwarze 1637:                *res = 10 * *res + v[p] - '0';
1.184     schwarze 1638:        if (p == *pos + n)
                   1639:                return 0;
                   1640:
                   1641:        if (n)
                   1642:                *res = -*res;
                   1643:
                   1644:        *pos = p;
                   1645:        return 1;
                   1646: }
                   1647:
1.198     schwarze 1648: /*
                   1649:  * Evaluate a string comparison condition.
                   1650:  * The first character is the delimiter.
                   1651:  * Succeed if the string up to its second occurrence
                   1652:  * matches the string up to its third occurence.
                   1653:  * Advance the cursor after the third occurrence
                   1654:  * or lacking that, to the end of the line.
                   1655:  */
                   1656: static int
                   1657: roff_evalstrcond(const char *v, int *pos)
                   1658: {
                   1659:        const char      *s1, *s2, *s3;
                   1660:        int              match;
                   1661:
                   1662:        match = 0;
                   1663:        s1 = v + *pos;          /* initial delimiter */
                   1664:        s2 = s1 + 1;            /* for scanning the first string */
                   1665:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   1666:
                   1667:        if (NULL == s3)         /* found no middle delimiter */
                   1668:                goto out;
                   1669:
                   1670:        while ('\0' != *++s3) {
                   1671:                if (*s2 != *s3) {  /* mismatch */
                   1672:                        s3 = strchr(s3, *s1);
                   1673:                        break;
                   1674:                }
                   1675:                if (*s3 == *s1) {  /* found the final delimiter */
                   1676:                        match = 1;
                   1677:                        break;
                   1678:                }
                   1679:                s2++;
                   1680:        }
                   1681:
                   1682: out:
                   1683:        if (NULL == s3)
                   1684:                s3 = strchr(s2, '\0');
1.242     schwarze 1685:        else if (*s3 != '\0')
1.198     schwarze 1686:                s3++;
                   1687:        *pos = s3 - v;
                   1688:        return(match);
                   1689: }
                   1690:
1.204     schwarze 1691: /*
                   1692:  * Evaluate an optionally negated single character, numerical,
                   1693:  * or string condition.
                   1694:  */
1.198     schwarze 1695: static int
1.234     kristaps 1696: roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
1.88      kristaps 1697: {
1.241     schwarze 1698:        int      number, savepos, wanttrue;
1.88      kristaps 1699:
1.198     schwarze 1700:        if ('!' == v[*pos]) {
                   1701:                wanttrue = 0;
                   1702:                (*pos)++;
                   1703:        } else
                   1704:                wanttrue = 1;
                   1705:
1.88      kristaps 1706:        switch (v[*pos]) {
1.240     schwarze 1707:        case '\0':
                   1708:                return(0);
1.207     schwarze 1709:        case 'n':
1.198     schwarze 1710:                /* FALLTHROUGH */
1.207     schwarze 1711:        case 'o':
1.88      kristaps 1712:                (*pos)++;
1.198     schwarze 1713:                return(wanttrue);
1.207     schwarze 1714:        case 'c':
1.198     schwarze 1715:                /* FALLTHROUGH */
1.207     schwarze 1716:        case 'd':
1.198     schwarze 1717:                /* FALLTHROUGH */
1.207     schwarze 1718:        case 'e':
1.88      kristaps 1719:                /* FALLTHROUGH */
1.207     schwarze 1720:        case 'r':
1.88      kristaps 1721:                /* FALLTHROUGH */
1.207     schwarze 1722:        case 't':
1.239     schwarze 1723:                /* FALLTHROUGH */
                   1724:        case 'v':
1.88      kristaps 1725:                (*pos)++;
1.198     schwarze 1726:                return(!wanttrue);
1.88      kristaps 1727:        default:
                   1728:                break;
                   1729:        }
                   1730:
1.241     schwarze 1731:        savepos = *pos;
1.234     kristaps 1732:        if (roff_evalnum(r, ln, v, pos, &number, 0))
1.204     schwarze 1733:                return((number > 0) == wanttrue);
1.241     schwarze 1734:        else if (*pos == savepos)
                   1735:                return(roff_evalstrcond(v, pos) == wanttrue);
1.204     schwarze 1736:        else
1.241     schwarze 1737:                return (0);
1.88      kristaps 1738: }
                   1739:
1.74      kristaps 1740: static enum rofferr
1.103     kristaps 1741: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 1742: {
1.123     schwarze 1743:
1.89      kristaps 1744:        return(ROFF_IGN);
                   1745: }
                   1746:
1.104     kristaps 1747: static enum rofferr
1.251     schwarze 1748: roff_insec(ROFF_ARGS)
                   1749: {
                   1750:
                   1751:        mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
                   1752:            ln, ppos, roffs[tok].name);
                   1753:        return(ROFF_IGN);
                   1754: }
                   1755:
                   1756: static enum rofferr
                   1757: roff_unsupp(ROFF_ARGS)
                   1758: {
                   1759:
                   1760:        mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
                   1761:            ln, ppos, roffs[tok].name);
                   1762:        return(ROFF_IGN);
                   1763: }
                   1764:
                   1765: static enum rofferr
1.82      kristaps 1766: roff_cond(ROFF_ARGS)
1.74      kristaps 1767: {
1.173     schwarze 1768:
                   1769:        roffnode_push(r, tok, NULL, ln, ppos);
1.74      kristaps 1770:
1.207     schwarze 1771:        /*
1.134     kristaps 1772:         * An `.el' has no conditional body: it will consume the value
                   1773:         * of the current rstack entry set in prior `ie' calls or
1.207     schwarze 1774:         * defaults to DENY.
1.134     kristaps 1775:         *
                   1776:         * If we're not an `el', however, then evaluate the conditional.
                   1777:         */
1.133     kristaps 1778:
1.238     schwarze 1779:        r->last->rule = tok == ROFF_el ?
1.207     schwarze 1780:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238     schwarze 1781:            roff_evalcond(r, ln, buf->buf, &pos);
1.77      kristaps 1782:
1.134     kristaps 1783:        /*
                   1784:         * An if-else will put the NEGATION of the current evaluated
                   1785:         * conditional into the stack of rules.
                   1786:         */
                   1787:
1.238     schwarze 1788:        if (tok == ROFF_ie) {
1.223     schwarze 1789:                if (r->rstackpos + 1 == r->rstacksz) {
                   1790:                        r->rstacksz += 16;
                   1791:                        r->rstack = mandoc_reallocarray(r->rstack,
                   1792:                            r->rstacksz, sizeof(int));
1.134     kristaps 1793:                }
1.198     schwarze 1794:                r->rstack[++r->rstackpos] = !r->last->rule;
1.82      kristaps 1795:        }
1.88      kristaps 1796:
                   1797:        /* If the parent has false as its rule, then so do we. */
                   1798:
1.198     schwarze 1799:        if (r->last->parent && !r->last->parent->rule)
                   1800:                r->last->rule = 0;
1.88      kristaps 1801:
                   1802:        /*
1.173     schwarze 1803:         * Determine scope.
                   1804:         * If there is nothing on the line after the conditional,
                   1805:         * not even whitespace, use next-line scope.
1.88      kristaps 1806:         */
1.74      kristaps 1807:
1.238     schwarze 1808:        if (buf->buf[pos] == '\0') {
1.173     schwarze 1809:                r->last->endspan = 2;
                   1810:                goto out;
                   1811:        }
                   1812:
1.238     schwarze 1813:        while (buf->buf[pos] == ' ')
1.173     schwarze 1814:                pos++;
                   1815:
                   1816:        /* An opening brace requests multiline scope. */
1.75      kristaps 1817:
1.238     schwarze 1818:        if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75      kristaps 1819:                r->last->endspan = -1;
                   1820:                pos += 2;
1.173     schwarze 1821:                goto out;
1.207     schwarze 1822:        }
1.74      kristaps 1823:
1.77      kristaps 1824:        /*
1.173     schwarze 1825:         * Anything else following the conditional causes
                   1826:         * single-line scope.  Warn if the scope contains
                   1827:         * nothing but trailing whitespace.
1.77      kristaps 1828:         */
                   1829:
1.238     schwarze 1830:        if (buf->buf[pos] == '\0')
1.216     schwarze 1831:                mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
                   1832:                    ln, ppos, roffs[tok].name);
1.77      kristaps 1833:
1.173     schwarze 1834:        r->last->endspan = 1;
1.74      kristaps 1835:
1.173     schwarze 1836: out:
1.75      kristaps 1837:        *offs = pos;
                   1838:        return(ROFF_RERUN);
1.83      schwarze 1839: }
                   1840:
                   1841: static enum rofferr
1.92      schwarze 1842: roff_ds(ROFF_ARGS)
                   1843: {
1.212     schwarze 1844:        char            *string;
                   1845:        const char      *name;
                   1846:        size_t           namesz;
1.96      kristaps 1847:
1.251     schwarze 1848:        /* Ignore groff compatibility mode for now. */
                   1849:
                   1850:        if (tok == ROFF_ds1)
                   1851:                tok = ROFF_ds;
                   1852:        else if (tok == ROFF_as1)
                   1853:                tok = ROFF_as;
                   1854:
1.96      kristaps 1855:        /*
1.212     schwarze 1856:         * The first word is the name of the string.
                   1857:         * If it is empty or terminated by an escape sequence,
                   1858:         * abort the `ds' request without defining anything.
1.96      kristaps 1859:         */
1.92      schwarze 1860:
1.238     schwarze 1861:        name = string = buf->buf + pos;
                   1862:        if (*name == '\0')
1.92      schwarze 1863:                return(ROFF_IGN);
                   1864:
1.212     schwarze 1865:        namesz = roff_getname(r, &string, ln, pos);
1.238     schwarze 1866:        if (name[namesz] == '\\')
1.212     schwarze 1867:                return(ROFF_IGN);
                   1868:
                   1869:        /* Read past the initial double-quote, if any. */
1.238     schwarze 1870:        if (*string == '"')
1.92      schwarze 1871:                string++;
                   1872:
1.96      kristaps 1873:        /* The rest is the value. */
1.212     schwarze 1874:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   1875:            ROFF_as == tok);
1.92      schwarze 1876:        return(ROFF_IGN);
                   1877: }
                   1878:
1.204     schwarze 1879: /*
                   1880:  * Parse a single operator, one or two characters long.
                   1881:  * If the operator is recognized, return success and advance the
                   1882:  * parse point, else return failure and let the parse point unchanged.
                   1883:  */
                   1884: static int
                   1885: roff_getop(const char *v, int *pos, char *res)
                   1886: {
                   1887:
                   1888:        *res = v[*pos];
                   1889:
                   1890:        switch (*res) {
1.207     schwarze 1891:        case '+':
1.204     schwarze 1892:                /* FALLTHROUGH */
1.207     schwarze 1893:        case '-':
1.204     schwarze 1894:                /* FALLTHROUGH */
1.207     schwarze 1895:        case '*':
1.204     schwarze 1896:                /* FALLTHROUGH */
1.207     schwarze 1897:        case '/':
1.204     schwarze 1898:                /* FALLTHROUGH */
1.207     schwarze 1899:        case '%':
1.204     schwarze 1900:                /* FALLTHROUGH */
1.207     schwarze 1901:        case '&':
1.204     schwarze 1902:                /* FALLTHROUGH */
1.207     schwarze 1903:        case ':':
1.204     schwarze 1904:                break;
                   1905:        case '<':
                   1906:                switch (v[*pos + 1]) {
1.207     schwarze 1907:                case '=':
1.204     schwarze 1908:                        *res = 'l';
                   1909:                        (*pos)++;
                   1910:                        break;
1.207     schwarze 1911:                case '>':
1.204     schwarze 1912:                        *res = '!';
                   1913:                        (*pos)++;
                   1914:                        break;
1.207     schwarze 1915:                case '?':
1.204     schwarze 1916:                        *res = 'i';
                   1917:                        (*pos)++;
                   1918:                        break;
                   1919:                default:
                   1920:                        break;
                   1921:                }
                   1922:                break;
                   1923:        case '>':
                   1924:                switch (v[*pos + 1]) {
1.207     schwarze 1925:                case '=':
1.204     schwarze 1926:                        *res = 'g';
                   1927:                        (*pos)++;
                   1928:                        break;
1.207     schwarze 1929:                case '?':
1.204     schwarze 1930:                        *res = 'a';
                   1931:                        (*pos)++;
                   1932:                        break;
                   1933:                default:
                   1934:                        break;
                   1935:                }
                   1936:                break;
                   1937:        case '=':
                   1938:                if ('=' == v[*pos + 1])
                   1939:                        (*pos)++;
                   1940:                break;
                   1941:        default:
                   1942:                return(0);
                   1943:        }
                   1944:        (*pos)++;
                   1945:
                   1946:        return(*res);
                   1947: }
                   1948:
                   1949: /*
                   1950:  * Evaluate either a parenthesized numeric expression
                   1951:  * or a single signed integer number.
                   1952:  */
                   1953: static int
1.235     schwarze 1954: roff_evalpar(struct roff *r, int ln,
1.234     kristaps 1955:        const char *v, int *pos, int *res)
1.204     schwarze 1956: {
                   1957:
                   1958:        if ('(' != v[*pos])
                   1959:                return(roff_getnum(v, pos, res));
                   1960:
                   1961:        (*pos)++;
1.234     kristaps 1962:        if ( ! roff_evalnum(r, ln, v, pos, res, 1))
1.204     schwarze 1963:                return(0);
                   1964:
1.206     schwarze 1965:        /*
                   1966:         * Omission of the closing parenthesis
                   1967:         * is an error in validation mode,
                   1968:         * but ignored in evaluation mode.
                   1969:         */
                   1970:
1.204     schwarze 1971:        if (')' == v[*pos])
                   1972:                (*pos)++;
1.206     schwarze 1973:        else if (NULL == res)
                   1974:                return(0);
1.204     schwarze 1975:
                   1976:        return(1);
                   1977: }
                   1978:
                   1979: /*
                   1980:  * Evaluate a complete numeric expression.
                   1981:  * Proceed left to right, there is no concept of precedence.
                   1982:  */
                   1983: static int
1.235     schwarze 1984: roff_evalnum(struct roff *r, int ln, const char *v,
1.234     kristaps 1985:        int *pos, int *res, int skipwhite)
1.204     schwarze 1986: {
                   1987:        int              mypos, operand2;
                   1988:        char             operator;
                   1989:
                   1990:        if (NULL == pos) {
                   1991:                mypos = 0;
                   1992:                pos = &mypos;
                   1993:        }
                   1994:
                   1995:        if (skipwhite)
                   1996:                while (isspace((unsigned char)v[*pos]))
                   1997:                        (*pos)++;
                   1998:
1.234     kristaps 1999:        if ( ! roff_evalpar(r, ln, v, pos, res))
1.204     schwarze 2000:                return(0);
                   2001:
                   2002:        while (1) {
                   2003:                if (skipwhite)
                   2004:                        while (isspace((unsigned char)v[*pos]))
                   2005:                                (*pos)++;
                   2006:
                   2007:                if ( ! roff_getop(v, pos, &operator))
                   2008:                        break;
                   2009:
                   2010:                if (skipwhite)
                   2011:                        while (isspace((unsigned char)v[*pos]))
                   2012:                                (*pos)++;
                   2013:
1.234     kristaps 2014:                if ( ! roff_evalpar(r, ln, v, pos, &operand2))
1.204     schwarze 2015:                        return(0);
                   2016:
                   2017:                if (skipwhite)
                   2018:                        while (isspace((unsigned char)v[*pos]))
                   2019:                                (*pos)++;
1.206     schwarze 2020:
                   2021:                if (NULL == res)
                   2022:                        continue;
1.204     schwarze 2023:
                   2024:                switch (operator) {
1.207     schwarze 2025:                case '+':
1.204     schwarze 2026:                        *res += operand2;
                   2027:                        break;
1.207     schwarze 2028:                case '-':
1.204     schwarze 2029:                        *res -= operand2;
                   2030:                        break;
1.207     schwarze 2031:                case '*':
1.204     schwarze 2032:                        *res *= operand2;
                   2033:                        break;
1.207     schwarze 2034:                case '/':
1.244     schwarze 2035:                        if (operand2 == 0) {
1.235     schwarze 2036:                                mandoc_msg(MANDOCERR_DIVZERO,
1.234     kristaps 2037:                                        r->parse, ln, *pos, v);
                   2038:                                *res = 0;
                   2039:                                break;
                   2040:                        }
1.204     schwarze 2041:                        *res /= operand2;
                   2042:                        break;
1.207     schwarze 2043:                case '%':
1.244     schwarze 2044:                        if (operand2 == 0) {
                   2045:                                mandoc_msg(MANDOCERR_DIVZERO,
                   2046:                                        r->parse, ln, *pos, v);
                   2047:                                *res = 0;
                   2048:                                break;
                   2049:                        }
1.204     schwarze 2050:                        *res %= operand2;
                   2051:                        break;
1.207     schwarze 2052:                case '<':
1.204     schwarze 2053:                        *res = *res < operand2;
                   2054:                        break;
1.207     schwarze 2055:                case '>':
1.204     schwarze 2056:                        *res = *res > operand2;
                   2057:                        break;
1.207     schwarze 2058:                case 'l':
1.204     schwarze 2059:                        *res = *res <= operand2;
                   2060:                        break;
1.207     schwarze 2061:                case 'g':
1.204     schwarze 2062:                        *res = *res >= operand2;
                   2063:                        break;
1.207     schwarze 2064:                case '=':
1.204     schwarze 2065:                        *res = *res == operand2;
                   2066:                        break;
1.207     schwarze 2067:                case '!':
1.204     schwarze 2068:                        *res = *res != operand2;
                   2069:                        break;
1.207     schwarze 2070:                case '&':
1.204     schwarze 2071:                        *res = *res && operand2;
                   2072:                        break;
1.207     schwarze 2073:                case ':':
1.204     schwarze 2074:                        *res = *res || operand2;
                   2075:                        break;
1.207     schwarze 2076:                case 'i':
1.204     schwarze 2077:                        if (operand2 < *res)
                   2078:                                *res = operand2;
                   2079:                        break;
1.207     schwarze 2080:                case 'a':
1.204     schwarze 2081:                        if (operand2 > *res)
                   2082:                                *res = operand2;
                   2083:                        break;
                   2084:                default:
                   2085:                        abort();
                   2086:                }
                   2087:        }
                   2088:        return(1);
                   2089: }
                   2090:
1.180     schwarze 2091: void
1.187     schwarze 2092: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147     kristaps 2093: {
1.180     schwarze 2094:        struct roffreg  *reg;
                   2095:
                   2096:        /* Search for an existing register with the same name. */
                   2097:        reg = r->regtab;
                   2098:
                   2099:        while (reg && strcmp(name, reg->key.p))
                   2100:                reg = reg->next;
1.147     kristaps 2101:
1.180     schwarze 2102:        if (NULL == reg) {
                   2103:                /* Create a new register. */
                   2104:                reg = mandoc_malloc(sizeof(struct roffreg));
                   2105:                reg->key.p = mandoc_strdup(name);
                   2106:                reg->key.sz = strlen(name);
1.187     schwarze 2107:                reg->val = 0;
1.180     schwarze 2108:                reg->next = r->regtab;
                   2109:                r->regtab = reg;
                   2110:        }
                   2111:
1.187     schwarze 2112:        if ('+' == sign)
                   2113:                reg->val += val;
                   2114:        else if ('-' == sign)
                   2115:                reg->val -= val;
                   2116:        else
                   2117:                reg->val = val;
1.147     kristaps 2118: }
                   2119:
1.192     schwarze 2120: /*
                   2121:  * Handle some predefined read-only number registers.
                   2122:  * For now, return -1 if the requested register is not predefined;
                   2123:  * in case a predefined read-only register having the value -1
                   2124:  * were to turn up, another special value would have to be chosen.
                   2125:  */
                   2126: static int
                   2127: roff_getregro(const char *name)
                   2128: {
                   2129:
                   2130:        switch (*name) {
1.207     schwarze 2131:        case 'A':  /* ASCII approximation mode is always off. */
1.192     schwarze 2132:                return(0);
1.207     schwarze 2133:        case 'g':  /* Groff compatibility mode is always on. */
1.192     schwarze 2134:                return(1);
1.207     schwarze 2135:        case 'H':  /* Fixed horizontal resolution. */
1.192     schwarze 2136:                return (24);
1.207     schwarze 2137:        case 'j':  /* Always adjust left margin only. */
1.192     schwarze 2138:                return(0);
1.207     schwarze 2139:        case 'T':  /* Some output device is always defined. */
1.192     schwarze 2140:                return(1);
1.207     schwarze 2141:        case 'V':  /* Fixed vertical resolution. */
1.192     schwarze 2142:                return (40);
                   2143:        default:
                   2144:                return (-1);
                   2145:        }
                   2146: }
                   2147:
1.181     schwarze 2148: int
1.180     schwarze 2149: roff_getreg(const struct roff *r, const char *name)
1.147     kristaps 2150: {
1.180     schwarze 2151:        struct roffreg  *reg;
1.192     schwarze 2152:        int              val;
                   2153:
                   2154:        if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
                   2155:                val = roff_getregro(name + 1);
                   2156:                if (-1 != val)
                   2157:                        return (val);
                   2158:        }
1.180     schwarze 2159:
                   2160:        for (reg = r->regtab; reg; reg = reg->next)
                   2161:                if (0 == strcmp(name, reg->key.p))
1.181     schwarze 2162:                        return(reg->val);
                   2163:
                   2164:        return(0);
                   2165: }
                   2166:
                   2167: static int
                   2168: roff_getregn(const struct roff *r, const char *name, size_t len)
                   2169: {
                   2170:        struct roffreg  *reg;
1.192     schwarze 2171:        int              val;
                   2172:
                   2173:        if ('.' == name[0] && 2 == len) {
                   2174:                val = roff_getregro(name + 1);
                   2175:                if (-1 != val)
                   2176:                        return (val);
                   2177:        }
1.181     schwarze 2178:
                   2179:        for (reg = r->regtab; reg; reg = reg->next)
                   2180:                if (len == reg->key.sz &&
                   2181:                    0 == strncmp(name, reg->key.p, len))
                   2182:                        return(reg->val);
1.147     kristaps 2183:
1.180     schwarze 2184:        return(0);
1.147     kristaps 2185: }
                   2186:
1.180     schwarze 2187: static void
                   2188: roff_freereg(struct roffreg *reg)
1.147     kristaps 2189: {
1.180     schwarze 2190:        struct roffreg  *old_reg;
1.147     kristaps 2191:
1.180     schwarze 2192:        while (NULL != reg) {
                   2193:                free(reg->key.p);
                   2194:                old_reg = reg;
                   2195:                reg = reg->next;
                   2196:                free(old_reg);
                   2197:        }
1.147     kristaps 2198: }
1.92      schwarze 2199:
                   2200: static enum rofferr
1.89      kristaps 2201: roff_nr(ROFF_ARGS)
1.83      schwarze 2202: {
1.212     schwarze 2203:        char            *key, *val;
                   2204:        size_t           keysz;
1.138     kristaps 2205:        int              iv;
1.187     schwarze 2206:        char             sign;
1.89      kristaps 2207:
1.238     schwarze 2208:        key = val = buf->buf + pos;
                   2209:        if (*key == '\0')
1.212     schwarze 2210:                return(ROFF_IGN);
                   2211:
                   2212:        keysz = roff_getname(r, &val, ln, pos);
1.238     schwarze 2213:        if (key[keysz] == '\\')
1.212     schwarze 2214:                return(ROFF_IGN);
                   2215:        key[keysz] = '\0';
1.89      kristaps 2216:
1.187     schwarze 2217:        sign = *val;
1.238     schwarze 2218:        if (sign == '+' || sign == '-')
1.187     schwarze 2219:                val++;
                   2220:
1.234     kristaps 2221:        if (roff_evalnum(r, ln, val, NULL, &iv, 0))
1.204     schwarze 2222:                roff_setreg(r, key, iv, sign);
1.109     kristaps 2223:
1.203     schwarze 2224:        return(ROFF_IGN);
                   2225: }
                   2226:
                   2227: static enum rofferr
                   2228: roff_rr(ROFF_ARGS)
                   2229: {
                   2230:        struct roffreg  *reg, **prev;
1.212     schwarze 2231:        char            *name, *cp;
                   2232:        size_t           namesz;
1.203     schwarze 2233:
1.238     schwarze 2234:        name = cp = buf->buf + pos;
                   2235:        if (*name == '\0')
1.212     schwarze 2236:                return(ROFF_IGN);
                   2237:        namesz = roff_getname(r, &cp, ln, pos);
                   2238:        name[namesz] = '\0';
1.203     schwarze 2239:
                   2240:        prev = &r->regtab;
                   2241:        while (1) {
                   2242:                reg = *prev;
1.238     schwarze 2243:                if (reg == NULL || !strcmp(name, reg->key.p))
1.203     schwarze 2244:                        break;
                   2245:                prev = &reg->next;
                   2246:        }
1.238     schwarze 2247:        if (reg != NULL) {
1.203     schwarze 2248:                *prev = reg->next;
                   2249:                free(reg->key.p);
                   2250:                free(reg);
                   2251:        }
1.122     schwarze 2252:        return(ROFF_IGN);
                   2253: }
                   2254:
                   2255: static enum rofferr
                   2256: roff_rm(ROFF_ARGS)
                   2257: {
                   2258:        const char       *name;
                   2259:        char             *cp;
1.212     schwarze 2260:        size_t            namesz;
1.122     schwarze 2261:
1.238     schwarze 2262:        cp = buf->buf + pos;
                   2263:        while (*cp != '\0') {
1.212     schwarze 2264:                name = cp;
1.238     schwarze 2265:                namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212     schwarze 2266:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.238     schwarze 2267:                if (name[namesz] == '\\')
1.212     schwarze 2268:                        break;
1.122     schwarze 2269:        }
1.178     schwarze 2270:        return(ROFF_IGN);
                   2271: }
                   2272:
                   2273: static enum rofferr
                   2274: roff_it(ROFF_ARGS)
                   2275: {
                   2276:        char            *cp;
                   2277:        size_t           len;
                   2278:        int              iv;
                   2279:
                   2280:        /* Parse the number of lines. */
1.238     schwarze 2281:        cp = buf->buf + pos;
1.178     schwarze 2282:        len = strcspn(cp, " \t");
                   2283:        cp[len] = '\0';
                   2284:        if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1.222     schwarze 2285:                mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238     schwarze 2286:                    ln, ppos, buf->buf + 1);
1.178     schwarze 2287:                return(ROFF_IGN);
                   2288:        }
                   2289:        cp += len + 1;
                   2290:
                   2291:        /* Arm the input line trap. */
                   2292:        roffit_lines = iv;
                   2293:        roffit_macro = mandoc_strdup(cp);
1.109     kristaps 2294:        return(ROFF_IGN);
1.175     schwarze 2295: }
                   2296:
                   2297: static enum rofferr
                   2298: roff_Dd(ROFF_ARGS)
                   2299: {
                   2300:        const char *const       *cp;
                   2301:
1.227     schwarze 2302:        if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175     schwarze 2303:                for (cp = __mdoc_reserved; *cp; cp++)
                   2304:                        roff_setstr(r, *cp, NULL, 0);
                   2305:
1.227     schwarze 2306:        if (r->format == 0)
                   2307:                r->format = MPARSE_MDOC;
                   2308:
1.175     schwarze 2309:        return(ROFF_CONT);
                   2310: }
                   2311:
                   2312: static enum rofferr
                   2313: roff_TH(ROFF_ARGS)
                   2314: {
                   2315:        const char *const       *cp;
                   2316:
1.227     schwarze 2317:        if ((r->options & MPARSE_QUICK) == 0)
1.175     schwarze 2318:                for (cp = __man_reserved; *cp; cp++)
                   2319:                        roff_setstr(r, *cp, NULL, 0);
                   2320:
1.227     schwarze 2321:        if (r->format == 0)
                   2322:                r->format = MPARSE_MAN;
                   2323:
1.175     schwarze 2324:        return(ROFF_CONT);
1.109     kristaps 2325: }
                   2326:
                   2327: static enum rofferr
                   2328: roff_TE(ROFF_ARGS)
                   2329: {
                   2330:
                   2331:        if (NULL == r->tbl)
1.221     schwarze 2332:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2333:                    ln, ppos, "TE");
1.115     kristaps 2334:        else
1.151     kristaps 2335:                tbl_end(&r->tbl);
1.109     kristaps 2336:
1.112     kristaps 2337:        return(ROFF_IGN);
                   2338: }
                   2339:
                   2340: static enum rofferr
                   2341: roff_T_(ROFF_ARGS)
                   2342: {
                   2343:
                   2344:        if (NULL == r->tbl)
1.221     schwarze 2345:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2346:                    ln, ppos, "T&");
1.112     kristaps 2347:        else
1.116     kristaps 2348:                tbl_restart(ppos, ln, r->tbl);
1.112     kristaps 2349:
1.109     kristaps 2350:        return(ROFF_IGN);
                   2351: }
                   2352:
1.230     schwarze 2353: /*
                   2354:  * Handle in-line equation delimiters.
                   2355:  */
                   2356: static enum rofferr
1.238     schwarze 2357: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151     kristaps 2358: {
1.233     schwarze 2359:        char            *cp1, *cp2;
                   2360:        const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151     kristaps 2361:
1.230     schwarze 2362:        /*
                   2363:         * Outside equations, look for an opening delimiter.
                   2364:         * If we are inside an equation, we already know it is
                   2365:         * in-line, or this function wouldn't have been called;
                   2366:         * so look for a closing delimiter.
                   2367:         */
                   2368:
1.238     schwarze 2369:        cp1 = buf->buf + pos;
1.230     schwarze 2370:        cp2 = strchr(cp1, r->eqn == NULL ?
                   2371:            r->last_eqn->odelim : r->last_eqn->cdelim);
                   2372:        if (cp2 == NULL)
                   2373:                return(ROFF_CONT);
                   2374:
1.233     schwarze 2375:        *cp2++ = '\0';
                   2376:        bef_pr = bef_nl = aft_nl = aft_pr = "";
                   2377:
                   2378:        /* Handle preceding text, protecting whitespace. */
                   2379:
1.238     schwarze 2380:        if (*buf->buf != '\0') {
1.233     schwarze 2381:                if (r->eqn == NULL)
                   2382:                        bef_pr = "\\&";
                   2383:                bef_nl = "\n";
                   2384:        }
                   2385:
                   2386:        /*
                   2387:         * Prepare replacing the delimiter with an equation macro
                   2388:         * and drop leading white space from the equation.
                   2389:         */
1.230     schwarze 2390:
1.233     schwarze 2391:        if (r->eqn == NULL) {
                   2392:                while (*cp2 == ' ')
                   2393:                        cp2++;
                   2394:                mac = ".EQ";
                   2395:        } else
                   2396:                mac = ".EN";
                   2397:
                   2398:        /* Handle following text, protecting whitespace. */
                   2399:
                   2400:        if (*cp2 != '\0') {
                   2401:                aft_nl = "\n";
                   2402:                if (r->eqn != NULL)
                   2403:                        aft_pr = "\\&";
                   2404:        }
                   2405:
                   2406:        /* Do the actual replacement. */
                   2407:
1.238     schwarze 2408:        buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233     schwarze 2409:            bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238     schwarze 2410:        free(buf->buf);
                   2411:        buf->buf = cp1;
1.230     schwarze 2412:
                   2413:        /* Toggle the in-line state of the eqn subsystem. */
                   2414:
                   2415:        r->eqn_inline = r->eqn == NULL;
                   2416:        return(ROFF_REPARSE);
1.151     kristaps 2417: }
                   2418:
1.235     schwarze 2419: static enum rofferr
                   2420: roff_EQ(ROFF_ARGS)
1.125     kristaps 2421: {
1.151     kristaps 2422:        struct eqn_node *e;
1.125     kristaps 2423:
1.238     schwarze 2424:        assert(r->eqn == NULL);
1.235     schwarze 2425:        e = eqn_alloc(ppos, ln, r->parse);
1.125     kristaps 2426:
1.230     schwarze 2427:        if (r->last_eqn) {
1.125     kristaps 2428:                r->last_eqn->next = e;
1.230     schwarze 2429:                e->delim = r->last_eqn->delim;
                   2430:                e->odelim = r->last_eqn->odelim;
                   2431:                e->cdelim = r->last_eqn->cdelim;
                   2432:        } else
1.125     kristaps 2433:                r->first_eqn = r->last_eqn = e;
                   2434:
                   2435:        r->eqn = r->last_eqn = e;
1.151     kristaps 2436:
1.238     schwarze 2437:        if (buf->buf[pos] != '\0')
1.235     schwarze 2438:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 2439:                    ".EQ %s", buf->buf + pos);
1.151     kristaps 2440:
1.125     kristaps 2441:        return(ROFF_IGN);
                   2442: }
                   2443:
                   2444: static enum rofferr
                   2445: roff_EN(ROFF_ARGS)
                   2446: {
                   2447:
1.221     schwarze 2448:        mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125     kristaps 2449:        return(ROFF_IGN);
                   2450: }
                   2451:
                   2452: static enum rofferr
1.109     kristaps 2453: roff_TS(ROFF_ARGS)
                   2454: {
1.176     schwarze 2455:        struct tbl_node *tbl;
1.89      kristaps 2456:
1.115     kristaps 2457:        if (r->tbl) {
1.221     schwarze 2458:                mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
                   2459:                    ln, ppos, "TS breaks TS");
1.151     kristaps 2460:                tbl_end(&r->tbl);
1.115     kristaps 2461:        }
1.83      schwarze 2462:
1.176     schwarze 2463:        tbl = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 2464:
                   2465:        if (r->last_tbl)
1.176     schwarze 2466:                r->last_tbl->next = tbl;
1.113     kristaps 2467:        else
1.176     schwarze 2468:                r->first_tbl = r->last_tbl = tbl;
1.113     kristaps 2469:
1.176     schwarze 2470:        r->tbl = r->last_tbl = tbl;
1.83      schwarze 2471:        return(ROFF_IGN);
1.251     schwarze 2472: }
                   2473:
                   2474: static enum rofferr
                   2475: roff_brp(ROFF_ARGS)
                   2476: {
                   2477:
                   2478:        buf->buf[pos - 1] = '\0';
                   2479:        return(ROFF_CONT);
1.92      schwarze 2480: }
                   2481:
1.105     kristaps 2482: static enum rofferr
1.174     kristaps 2483: roff_cc(ROFF_ARGS)
                   2484: {
                   2485:        const char      *p;
                   2486:
1.238     schwarze 2487:        p = buf->buf + pos;
1.174     kristaps 2488:
1.238     schwarze 2489:        if (*p == '\0' || (r->control = *p++) == '.')
1.174     kristaps 2490:                r->control = 0;
                   2491:
1.238     schwarze 2492:        if (*p != '\0')
1.174     kristaps 2493:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   2494:
                   2495:        return(ROFF_IGN);
                   2496: }
                   2497:
                   2498: static enum rofferr
1.164     kristaps 2499: roff_tr(ROFF_ARGS)
                   2500: {
                   2501:        const char      *p, *first, *second;
                   2502:        size_t           fsz, ssz;
                   2503:        enum mandoc_esc  esc;
                   2504:
1.238     schwarze 2505:        p = buf->buf + pos;
1.164     kristaps 2506:
1.238     schwarze 2507:        if (*p == '\0') {
1.164     kristaps 2508:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   2509:                return(ROFF_IGN);
                   2510:        }
                   2511:
1.238     schwarze 2512:        while (*p != '\0') {
1.164     kristaps 2513:                fsz = ssz = 1;
                   2514:
                   2515:                first = p++;
1.238     schwarze 2516:                if (*first == '\\') {
1.164     kristaps 2517:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 2518:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 2519:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 2520:                                    ln, (int)(p - buf->buf), first);
1.164     kristaps 2521:                                return(ROFF_IGN);
                   2522:                        }
                   2523:                        fsz = (size_t)(p - first);
                   2524:                }
                   2525:
                   2526:                second = p++;
1.238     schwarze 2527:                if (*second == '\\') {
1.164     kristaps 2528:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 2529:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 2530:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 2531:                                    ln, (int)(p - buf->buf), second);
1.164     kristaps 2532:                                return(ROFF_IGN);
                   2533:                        }
                   2534:                        ssz = (size_t)(p - second);
1.238     schwarze 2535:                } else if (*second == '\0') {
1.207     schwarze 2536:                        mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1.238     schwarze 2537:                            ln, (int)(p - buf->buf), NULL);
1.164     kristaps 2538:                        second = " ";
1.165     kristaps 2539:                        p--;
1.164     kristaps 2540:                }
                   2541:
1.167     kristaps 2542:                if (fsz > 1) {
1.207     schwarze 2543:                        roff_setstrn(&r->xmbtab, first, fsz,
                   2544:                            second, ssz, 0);
1.167     kristaps 2545:                        continue;
                   2546:                }
                   2547:
1.238     schwarze 2548:                if (r->xtab == NULL)
1.207     schwarze 2549:                        r->xtab = mandoc_calloc(128,
                   2550:                            sizeof(struct roffstr));
1.167     kristaps 2551:
                   2552:                free(r->xtab[(int)*first].p);
                   2553:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   2554:                r->xtab[(int)*first].sz = ssz;
1.164     kristaps 2555:        }
                   2556:
                   2557:        return(ROFF_IGN);
                   2558: }
                   2559:
                   2560: static enum rofferr
1.105     kristaps 2561: roff_so(ROFF_ARGS)
                   2562: {
1.249     schwarze 2563:        char *name, *cp;
1.105     kristaps 2564:
1.238     schwarze 2565:        name = buf->buf + pos;
1.224     schwarze 2566:        mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105     kristaps 2567:
                   2568:        /*
                   2569:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   2570:         * opening anything that's not in our cwd or anything beneath
                   2571:         * it.  Thus, explicitly disallow traversing up the file-system
                   2572:         * or using absolute paths.
                   2573:         */
                   2574:
1.238     schwarze 2575:        if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210     schwarze 2576:                mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
                   2577:                    ".so %s", name);
1.249     schwarze 2578:                buf->sz = mandoc_asprintf(&cp,
                   2579:                    ".sp\nSee the file %s.\n.sp", name) + 1;
                   2580:                free(buf->buf);
                   2581:                buf->buf = cp;
                   2582:                *offs = 0;
                   2583:                return(ROFF_REPARSE);
1.105     kristaps 2584:        }
                   2585:
                   2586:        *offs = pos;
                   2587:        return(ROFF_SO);
                   2588: }
1.92      schwarze 2589:
1.106     kristaps 2590: static enum rofferr
                   2591: roff_userdef(ROFF_ARGS)
1.99      kristaps 2592: {
1.106     kristaps 2593:        const char       *arg[9];
                   2594:        char             *cp, *n1, *n2;
1.119     schwarze 2595:        int               i;
1.106     kristaps 2596:
                   2597:        /*
                   2598:         * Collect pointers to macro argument strings
1.188     schwarze 2599:         * and NUL-terminate them.
1.106     kristaps 2600:         */
1.238     schwarze 2601:        cp = buf->buf + pos;
1.119     schwarze 2602:        for (i = 0; i < 9; i++)
1.238     schwarze 2603:                arg[i] = *cp == '\0' ? "" :
1.136     kristaps 2604:                    mandoc_getarg(r->parse, &cp, ln, &pos);
1.99      kristaps 2605:
1.106     kristaps 2606:        /*
                   2607:         * Expand macro arguments.
1.99      kristaps 2608:         */
1.238     schwarze 2609:        buf->sz = 0;
1.106     kristaps 2610:        n1 = cp = mandoc_strdup(r->current_string);
1.238     schwarze 2611:        while ((cp = strstr(cp, "\\$")) != NULL) {
1.106     kristaps 2612:                i = cp[2] - '1';
                   2613:                if (0 > i || 8 < i) {
                   2614:                        /* Not an argument invocation. */
                   2615:                        cp += 2;
                   2616:                        continue;
                   2617:                }
1.209     schwarze 2618:                *cp = '\0';
1.238     schwarze 2619:                buf->sz = mandoc_asprintf(&n2, "%s%s%s",
1.209     schwarze 2620:                    n1, arg[i], cp + 3) + 1;
1.106     kristaps 2621:                cp = n2 + (cp - n1);
                   2622:                free(n1);
                   2623:                n1 = n2;
1.99      kristaps 2624:        }
                   2625:
1.106     kristaps 2626:        /*
                   2627:         * Replace the macro invocation
                   2628:         * by the expanded macro.
                   2629:         */
1.238     schwarze 2630:        free(buf->buf);
                   2631:        buf->buf = n1;
                   2632:        if (buf->sz == 0)
                   2633:                buf->sz = strlen(buf->buf) + 1;
1.248     schwarze 2634:        *offs = 0;
1.106     kristaps 2635:
1.238     schwarze 2636:        return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
1.106     kristaps 2637:           ROFF_REPARSE : ROFF_APPEND);
1.99      kristaps 2638: }
1.121     schwarze 2639:
1.212     schwarze 2640: static size_t
1.121     schwarze 2641: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   2642: {
                   2643:        char     *name, *cp;
1.212     schwarze 2644:        size_t    namesz;
1.121     schwarze 2645:
                   2646:        name = *cpp;
                   2647:        if ('\0' == *name)
1.212     schwarze 2648:                return(0);
1.121     schwarze 2649:
1.212     schwarze 2650:        /* Read until end of name and terminate it with NUL. */
                   2651:        for (cp = name; 1; cp++) {
                   2652:                if ('\0' == *cp || ' ' == *cp) {
                   2653:                        namesz = cp - name;
                   2654:                        break;
                   2655:                }
1.121     schwarze 2656:                if ('\\' != *cp)
                   2657:                        continue;
1.215     schwarze 2658:                namesz = cp - name;
                   2659:                if ('{' == cp[1] || '}' == cp[1])
                   2660:                        break;
1.121     schwarze 2661:                cp++;
                   2662:                if ('\\' == *cp)
                   2663:                        continue;
1.224     schwarze 2664:                mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
                   2665:                    "%.*s", (int)(cp - name + 1), name);
1.212     schwarze 2666:                mandoc_escape((const char **)&cp, NULL, NULL);
                   2667:                break;
1.121     schwarze 2668:        }
                   2669:
                   2670:        /* Read past spaces. */
                   2671:        while (' ' == *cp)
                   2672:                cp++;
                   2673:
                   2674:        *cpp = cp;
1.212     schwarze 2675:        return(namesz);
1.121     schwarze 2676: }
                   2677:
1.106     kristaps 2678: /*
                   2679:  * Store *string into the user-defined string called *name.
                   2680:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193     schwarze 2681:  * append == 0: replace mode
                   2682:  * append == 1: single-line append mode
                   2683:  * append == 2: multiline append mode, append '\n' after each call
1.106     kristaps 2684:  */
1.94      kristaps 2685: static void
1.106     kristaps 2686: roff_setstr(struct roff *r, const char *name, const char *string,
1.193     schwarze 2687:        int append)
1.92      schwarze 2688: {
1.164     kristaps 2689:
                   2690:        roff_setstrn(&r->strtab, name, strlen(name), string,
1.207     schwarze 2691:            string ? strlen(string) : 0, append);
1.164     kristaps 2692: }
                   2693:
                   2694: static void
1.166     kristaps 2695: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193     schwarze 2696:                const char *string, size_t stringsz, int append)
1.164     kristaps 2697: {
1.166     kristaps 2698:        struct roffkv   *n;
1.164     kristaps 2699:        char            *c;
                   2700:        int              i;
                   2701:        size_t           oldch, newch;
1.92      schwarze 2702:
1.106     kristaps 2703:        /* Search for an existing string with the same name. */
1.164     kristaps 2704:        n = *r;
                   2705:
1.211     schwarze 2706:        while (n && (namesz != n->key.sz ||
                   2707:                        strncmp(n->key.p, name, namesz)))
1.92      schwarze 2708:                n = n->next;
1.94      kristaps 2709:
                   2710:        if (NULL == n) {
1.106     kristaps 2711:                /* Create a new string table entry. */
1.166     kristaps 2712:                n = mandoc_malloc(sizeof(struct roffkv));
                   2713:                n->key.p = mandoc_strndup(name, namesz);
                   2714:                n->key.sz = namesz;
                   2715:                n->val.p = NULL;
                   2716:                n->val.sz = 0;
1.164     kristaps 2717:                n->next = *r;
                   2718:                *r = n;
1.193     schwarze 2719:        } else if (0 == append) {
1.166     kristaps 2720:                free(n->val.p);
                   2721:                n->val.p = NULL;
                   2722:                n->val.sz = 0;
1.106     kristaps 2723:        }
                   2724:
                   2725:        if (NULL == string)
                   2726:                return;
                   2727:
                   2728:        /*
                   2729:         * One additional byte for the '\n' in multiline mode,
                   2730:         * and one for the terminating '\0'.
                   2731:         */
1.193     schwarze 2732:        newch = stringsz + (1 < append ? 2u : 1u);
1.164     kristaps 2733:
1.166     kristaps 2734:        if (NULL == n->val.p) {
                   2735:                n->val.p = mandoc_malloc(newch);
                   2736:                *n->val.p = '\0';
1.106     kristaps 2737:                oldch = 0;
                   2738:        } else {
1.166     kristaps 2739:                oldch = n->val.sz;
                   2740:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 2741:        }
                   2742:
                   2743:        /* Skip existing content in the destination buffer. */
1.166     kristaps 2744:        c = n->val.p + (int)oldch;
1.106     kristaps 2745:
                   2746:        /* Append new content to the destination buffer. */
1.164     kristaps 2747:        i = 0;
                   2748:        while (i < (int)stringsz) {
1.106     kristaps 2749:                /*
                   2750:                 * Rudimentary roff copy mode:
                   2751:                 * Handle escaped backslashes.
                   2752:                 */
1.164     kristaps 2753:                if ('\\' == string[i] && '\\' == string[i + 1])
                   2754:                        i++;
                   2755:                *c++ = string[i++];
1.106     kristaps 2756:        }
1.94      kristaps 2757:
1.106     kristaps 2758:        /* Append terminating bytes. */
1.193     schwarze 2759:        if (1 < append)
1.106     kristaps 2760:                *c++ = '\n';
1.163     kristaps 2761:
1.106     kristaps 2762:        *c = '\0';
1.166     kristaps 2763:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 2764: }
                   2765:
1.94      kristaps 2766: static const char *
                   2767: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 2768: {
1.166     kristaps 2769:        const struct roffkv *n;
1.191     schwarze 2770:        int i;
1.92      schwarze 2771:
1.164     kristaps 2772:        for (n = r->strtab; n; n = n->next)
1.207     schwarze 2773:                if (0 == strncmp(name, n->key.p, len) &&
                   2774:                    '\0' == n->key.p[(int)len])
1.166     kristaps 2775:                        return(n->val.p);
1.191     schwarze 2776:
                   2777:        for (i = 0; i < PREDEFS_MAX; i++)
                   2778:                if (0 == strncmp(name, predefs[i].name, len) &&
                   2779:                                '\0' == predefs[i].name[(int)len])
                   2780:                        return(predefs[i].str);
1.94      kristaps 2781:
1.157     kristaps 2782:        return(NULL);
1.92      schwarze 2783: }
                   2784:
1.94      kristaps 2785: static void
1.167     kristaps 2786: roff_freestr(struct roffkv *r)
1.92      schwarze 2787: {
1.166     kristaps 2788:        struct roffkv    *n, *nn;
1.92      schwarze 2789:
1.167     kristaps 2790:        for (n = r; n; n = nn) {
1.166     kristaps 2791:                free(n->key.p);
                   2792:                free(n->val.p);
1.92      schwarze 2793:                nn = n->next;
                   2794:                free(n);
                   2795:        }
1.114     kristaps 2796: }
                   2797:
                   2798: const struct tbl_span *
                   2799: roff_span(const struct roff *r)
                   2800: {
1.207     schwarze 2801:
1.114     kristaps 2802:        return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125     kristaps 2803: }
                   2804:
                   2805: const struct eqn *
                   2806: roff_eqn(const struct roff *r)
                   2807: {
1.207     schwarze 2808:
1.125     kristaps 2809:        return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164     kristaps 2810: }
                   2811:
                   2812: /*
                   2813:  * Duplicate an input string, making the appropriate character
                   2814:  * conversations (as stipulated by `tr') along the way.
                   2815:  * Returns a heap-allocated string with all the replacements made.
                   2816:  */
                   2817: char *
                   2818: roff_strdup(const struct roff *r, const char *p)
                   2819: {
1.166     kristaps 2820:        const struct roffkv *cp;
1.164     kristaps 2821:        char            *res;
                   2822:        const char      *pp;
                   2823:        size_t           ssz, sz;
                   2824:        enum mandoc_esc  esc;
                   2825:
1.167     kristaps 2826:        if (NULL == r->xmbtab && NULL == r->xtab)
1.164     kristaps 2827:                return(mandoc_strdup(p));
                   2828:        else if ('\0' == *p)
                   2829:                return(mandoc_strdup(""));
                   2830:
                   2831:        /*
                   2832:         * Step through each character looking for term matches
                   2833:         * (remember that a `tr' can be invoked with an escape, which is
                   2834:         * a glyph but the escape is multi-character).
                   2835:         * We only do this if the character hash has been initialised
                   2836:         * and the string is >0 length.
                   2837:         */
                   2838:
                   2839:        res = NULL;
                   2840:        ssz = 0;
                   2841:
                   2842:        while ('\0' != *p) {
1.167     kristaps 2843:                if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
                   2844:                        sz = r->xtab[(int)*p].sz;
                   2845:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2846:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   2847:                        ssz += sz;
                   2848:                        p++;
                   2849:                        continue;
                   2850:                } else if ('\\' != *p) {
                   2851:                        res = mandoc_realloc(res, ssz + 2);
                   2852:                        res[ssz++] = *p++;
                   2853:                        continue;
                   2854:                }
                   2855:
1.164     kristaps 2856:                /* Search for term matches. */
1.167     kristaps 2857:                for (cp = r->xmbtab; cp; cp = cp->next)
1.166     kristaps 2858:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 2859:                                break;
                   2860:
                   2861:                if (NULL != cp) {
                   2862:                        /*
                   2863:                         * A match has been found.
                   2864:                         * Append the match to the array and move
                   2865:                         * forward by its keysize.
                   2866:                         */
1.207     schwarze 2867:                        res = mandoc_realloc(res,
                   2868:                            ssz + cp->val.sz + 1);
1.166     kristaps 2869:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   2870:                        ssz += cp->val.sz;
                   2871:                        p += (int)cp->key.sz;
1.164     kristaps 2872:                        continue;
                   2873:                }
                   2874:
1.167     kristaps 2875:                /*
                   2876:                 * Handle escapes carefully: we need to copy
                   2877:                 * over just the escape itself, or else we might
                   2878:                 * do replacements within the escape itself.
                   2879:                 * Make sure to pass along the bogus string.
                   2880:                 */
                   2881:                pp = p++;
                   2882:                esc = mandoc_escape(&p, NULL, NULL);
                   2883:                if (ESCAPE_ERROR == esc) {
                   2884:                        sz = strlen(pp);
1.164     kristaps 2885:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2886:                        memcpy(res + ssz, pp, sz);
1.167     kristaps 2887:                        break;
1.164     kristaps 2888:                }
1.207     schwarze 2889:                /*
                   2890:                 * We bail out on bad escapes.
1.167     kristaps 2891:                 * No need to warn: we already did so when
                   2892:                 * roff_res() was called.
                   2893:                 */
                   2894:                sz = (int)(p - pp);
                   2895:                res = mandoc_realloc(res, ssz + sz + 1);
                   2896:                memcpy(res + ssz, pp, sz);
                   2897:                ssz += sz;
1.164     kristaps 2898:        }
                   2899:
                   2900:        res[(int)ssz] = '\0';
                   2901:        return(res);
1.227     schwarze 2902: }
                   2903:
                   2904: int
                   2905: roff_getformat(const struct roff *r)
                   2906: {
                   2907:
                   2908:        return(r->format);
1.174     kristaps 2909: }
                   2910:
                   2911: /*
1.207     schwarze 2912:  * Find out whether a line is a macro line or not.
1.174     kristaps 2913:  * If it is, adjust the current position and return one; if it isn't,
                   2914:  * return zero and don't change the current position.
                   2915:  * If the control character has been set with `.cc', then let that grain
                   2916:  * precedence.
                   2917:  * This is slighly contrary to groff, where using the non-breaking
                   2918:  * control character when `cc' has been invoked will cause the
                   2919:  * non-breaking macro contents to be printed verbatim.
                   2920:  */
                   2921: int
                   2922: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   2923: {
                   2924:        int             pos;
                   2925:
                   2926:        pos = *ppos;
                   2927:
                   2928:        if (0 != r->control && cp[pos] == r->control)
                   2929:                pos++;
                   2930:        else if (0 != r->control)
                   2931:                return(0);
                   2932:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   2933:                pos += 2;
                   2934:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   2935:                pos++;
                   2936:        else
                   2937:                return(0);
                   2938:
                   2939:        while (' ' == cp[pos] || '\t' == cp[pos])
                   2940:                pos++;
                   2941:
                   2942:        *ppos = pos;
                   2943:        return(1);
1.74      kristaps 2944: }

CVSweb