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

Annotation of mandoc/roff.c, Revision 1.265

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

CVSweb