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

Annotation of mandoc/roff.c, Revision 1.274

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

CVSweb