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

Annotation of mandoc/roff.c, Revision 1.287

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

CVSweb