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

Annotation of mandoc/roff.c, Revision 1.256

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

CVSweb