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

Annotation of mandoc/roff.c, Revision 1.267

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

CVSweb