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

Annotation of mandoc/roff.c, Revision 1.255

1.255   ! schwarze    1: /*     $Id: roff.c,v 1.254 2015/01/23 00:42:00 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,
                    182:        /* MAN_na; ignored in mdoc(7) */
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.103     kristaps  583:        { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
                    584:        { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  585:        { "nhychar", roff_line_ignore, NULL, NULL, 0, NULL },
                    586:        { "nm", roff_unsupp, NULL, NULL, 0, NULL },
                    587:        { "nn", roff_unsupp, NULL, NULL, 0, NULL },
                    588:        { "nop", roff_unsupp, NULL, NULL, 0, NULL },
1.104     kristaps  589:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.251     schwarze  590:        { "nrf", roff_unsupp, NULL, NULL, 0, NULL },
                    591:        { "nroff", roff_line_ignore, NULL, NULL, 0, NULL },
1.124     schwarze  592:        { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  593:        { "nx", roff_insec, NULL, NULL, 0, NULL },
                    594:        { "open", roff_insec, NULL, NULL, 0, NULL },
                    595:        { "opena", roff_insec, NULL, NULL, 0, NULL },
                    596:        { "os", roff_line_ignore, NULL, NULL, 0, NULL },
                    597:        { "output", roff_unsupp, NULL, NULL, 0, NULL },
                    598:        { "padj", roff_line_ignore, NULL, NULL, 0, NULL },
                    599:        { "papersize", roff_line_ignore, NULL, NULL, 0, NULL },
                    600:        { "pc", roff_line_ignore, NULL, NULL, 0, NULL },
                    601:        { "pev", roff_line_ignore, NULL, NULL, 0, NULL },
                    602:        { "pi", roff_insec, NULL, NULL, 0, NULL },
                    603:        { "PI", roff_unsupp, NULL, NULL, 0, NULL },
1.229     schwarze  604:        { "pl", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  605:        { "pm", roff_line_ignore, NULL, NULL, 0, NULL },
                    606:        { "pn", roff_line_ignore, NULL, NULL, 0, NULL },
                    607:        { "pnr", roff_line_ignore, NULL, NULL, 0, NULL },
                    608:        { "po", roff_line_ignore, NULL, NULL, 0, NULL },
1.124     schwarze  609:        { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.251     schwarze  610:        { "psbb", roff_unsupp, NULL, NULL, 0, NULL },
1.255   ! schwarze  611:        { "pshape", roff_unsupp, NULL, NULL, 0, NULL },
1.251     schwarze  612:        { "pso", roff_insec, NULL, NULL, 0, NULL },
                    613:        { "ptr", roff_line_ignore, NULL, NULL, 0, NULL },
                    614:        { "pvs", roff_line_ignore, NULL, NULL, 0, NULL },
                    615:        { "rchar", roff_unsupp, NULL, NULL, 0, NULL },
                    616:        { "rd", roff_line_ignore, NULL, NULL, 0, NULL },
                    617:        { "recursionlimit", roff_line_ignore, NULL, NULL, 0, NULL },
                    618:        { "return", roff_unsupp, NULL, NULL, 0, NULL },
                    619:        { "rfschar", roff_unsupp, NULL, NULL, 0, NULL },
                    620:        { "rhang", roff_line_ignore, NULL, NULL, 0, NULL },
                    621:        { "rj", roff_line_ignore, NULL, NULL, 0, NULL },
1.122     schwarze  622:        { "rm", roff_rm, NULL, NULL, 0, NULL },
1.251     schwarze  623:        { "rn", roff_unsupp, NULL, NULL, 0, NULL },
                    624:        { "rnn", roff_unsupp, NULL, NULL, 0, NULL },
1.203     schwarze  625:        { "rr", roff_rr, NULL, NULL, 0, NULL },
1.251     schwarze  626:        { "rs", roff_line_ignore, NULL, NULL, 0, NULL },
                    627:        { "rt", roff_line_ignore, NULL, NULL, 0, NULL },
                    628:        { "schar", roff_unsupp, NULL, NULL, 0, NULL },
                    629:        { "sentchar", roff_line_ignore, NULL, NULL, 0, NULL },
                    630:        { "shc", roff_line_ignore, NULL, NULL, 0, NULL },
                    631:        { "shift", roff_unsupp, NULL, NULL, 0, NULL },
                    632:        { "sizes", roff_line_ignore, NULL, NULL, 0, NULL },
1.105     kristaps  633:        { "so", roff_so, NULL, NULL, 0, NULL },
1.251     schwarze  634:        { "spacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
                    635:        { "special", roff_line_ignore, NULL, NULL, 0, NULL },
                    636:        { "spreadwarn", roff_line_ignore, NULL, NULL, 0, NULL },
                    637:        { "ss", roff_line_ignore, NULL, NULL, 0, NULL },
                    638:        { "sty", roff_line_ignore, NULL, NULL, 0, NULL },
                    639:        { "substring", roff_unsupp, NULL, NULL, 0, NULL },
                    640:        { "sv", roff_line_ignore, NULL, NULL, 0, NULL },
                    641:        { "sy", roff_insec, NULL, NULL, 0, NULL },
                    642:        { "T&", roff_T_, NULL, NULL, 0, NULL },
1.255   ! schwarze  643:        { "ta", roff_unsupp, NULL, NULL, 0, NULL },
1.251     schwarze  644:        { "tc", roff_unsupp, NULL, NULL, 0, NULL },
                    645:        { "TE", roff_TE, NULL, NULL, 0, NULL },
                    646:        { "TH", roff_TH, NULL, NULL, 0, NULL },
1.255   ! schwarze  647:        { "ti", roff_unsupp, NULL, NULL, 0, NULL },
1.251     schwarze  648:        { "tkf", roff_line_ignore, NULL, NULL, 0, NULL },
                    649:        { "tl", roff_unsupp, NULL, NULL, 0, NULL },
                    650:        { "tm", roff_line_ignore, NULL, NULL, 0, NULL },
                    651:        { "tm1", roff_line_ignore, NULL, NULL, 0, NULL },
                    652:        { "tmc", roff_line_ignore, NULL, NULL, 0, NULL },
1.164     kristaps  653:        { "tr", roff_tr, NULL, NULL, 0, NULL },
1.251     schwarze  654:        { "track", roff_line_ignore, NULL, NULL, 0, NULL },
                    655:        { "transchar", roff_line_ignore, NULL, NULL, 0, NULL },
                    656:        { "trf", roff_insec, NULL, NULL, 0, NULL },
                    657:        { "trimat", roff_line_ignore, NULL, NULL, 0, NULL },
                    658:        { "trin", roff_unsupp, NULL, NULL, 0, NULL },
                    659:        { "trnt", roff_unsupp, NULL, NULL, 0, NULL },
                    660:        { "troff", roff_line_ignore, NULL, NULL, 0, NULL },
1.109     kristaps  661:        { "TS", roff_TS, NULL, NULL, 0, NULL },
1.251     schwarze  662:        { "uf", roff_line_ignore, NULL, NULL, 0, NULL },
                    663:        { "ul", roff_line_ignore, NULL, NULL, 0, NULL },
                    664:        { "unformat", roff_unsupp, NULL, NULL, 0, NULL },
                    665:        { "unwatch", roff_line_ignore, NULL, NULL, 0, NULL },
                    666:        { "unwatchn", roff_line_ignore, NULL, NULL, 0, NULL },
                    667:        { "vpt", roff_line_ignore, NULL, NULL, 0, NULL },
                    668:        { "vs", roff_line_ignore, NULL, NULL, 0, NULL },
                    669:        { "warn", roff_line_ignore, NULL, NULL, 0, NULL },
                    670:        { "warnscale", roff_line_ignore, NULL, NULL, 0, NULL },
                    671:        { "watch", roff_line_ignore, NULL, NULL, 0, NULL },
                    672:        { "watchlength", roff_line_ignore, NULL, NULL, 0, NULL },
                    673:        { "watchn", roff_line_ignore, NULL, NULL, 0, NULL },
                    674:        { "wh", roff_unsupp, NULL, NULL, 0, NULL },
                    675:        { "while", roff_unsupp, NULL, NULL, 0, NULL },
                    676:        { "write", roff_insec, NULL, NULL, 0, NULL },
                    677:        { "writec", roff_insec, NULL, NULL, 0, NULL },
                    678:        { "writem", roff_insec, NULL, NULL, 0, NULL },
                    679:        { "xflag", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  680:        { ".", roff_cblock, NULL, NULL, 0, NULL },
1.106     kristaps  681:        { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67      kristaps  682: };
                    683:
1.200     schwarze  684: /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
1.175     schwarze  685: const  char *const __mdoc_reserved[] = {
                    686:        "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
                    687:        "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
                    688:        "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
                    689:        "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
1.200     schwarze  690:        "Dt", "Dv", "Dx", "D1",
                    691:        "Ec", "Ed", "Ef", "Ek", "El", "Em",
                    692:        "En", "Eo", "Er", "Es", "Ev", "Ex",
1.175     schwarze  693:        "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
1.200     schwarze  694:        "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
                    695:        "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
1.175     schwarze  696:        "Oc", "Oo", "Op", "Os", "Ot", "Ox",
1.200     schwarze  697:        "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
                    698:        "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
                    699:        "Sc", "Sh", "Sm", "So", "Sq",
1.175     schwarze  700:        "Ss", "St", "Sx", "Sy",
                    701:        "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
1.200     schwarze  702:        "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
1.175     schwarze  703:        "%P", "%Q", "%R", "%T", "%U", "%V",
                    704:        NULL
                    705: };
                    706:
1.200     schwarze  707: /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
1.175     schwarze  708: const  char *const __man_reserved[] = {
1.200     schwarze  709:        "AT", "B", "BI", "BR", "DT",
                    710:        "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
                    711:        "LP", "OP", "P", "PD", "PP",
                    712:        "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
                    713:        "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
1.175     schwarze  714:        NULL
                    715: };
                    716:
1.141     kristaps  717: /* Array of injected predefined strings. */
                    718: #define        PREDEFS_MAX      38
                    719: static const struct predef predefs[PREDEFS_MAX] = {
                    720: #include "predefs.in"
                    721: };
                    722:
1.155     kristaps  723: /* See roffhash_find() */
1.85      kristaps  724: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    725:
1.178     schwarze  726: static int      roffit_lines;  /* number of lines to delay */
                    727: static char    *roffit_macro;  /* nil-terminated macro line */
                    728:
1.207     schwarze  729:
1.85      kristaps  730: static void
1.155     kristaps  731: roffhash_init(void)
1.85      kristaps  732: {
                    733:        struct roffmac   *n;
                    734:        int               buc, i;
                    735:
1.106     kristaps  736:        for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85      kristaps  737:                assert(roffs[i].name[0] >= ASCII_LO);
                    738:                assert(roffs[i].name[0] <= ASCII_HI);
                    739:
                    740:                buc = ROFF_HASH(roffs[i].name);
                    741:
                    742:                if (NULL != (n = hash[buc])) {
                    743:                        for ( ; n->next; n = n->next)
                    744:                                /* Do nothing. */ ;
                    745:                        n->next = &roffs[i];
                    746:                } else
                    747:                        hash[buc] = &roffs[i];
                    748:        }
                    749: }
                    750:
1.67      kristaps  751: /*
                    752:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    753:  * the nil-terminated string name could be found.
                    754:  */
                    755: static enum rofft
1.155     kristaps  756: roffhash_find(const char *p, size_t s)
1.67      kristaps  757: {
1.85      kristaps  758:        int              buc;
                    759:        struct roffmac  *n;
1.67      kristaps  760:
1.85      kristaps  761:        /*
                    762:         * libroff has an extremely simple hashtable, for the time
                    763:         * being, which simply keys on the first character, which must
                    764:         * be printable, then walks a chain.  It works well enough until
                    765:         * optimised.
                    766:         */
                    767:
                    768:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    769:                return(ROFF_MAX);
                    770:
                    771:        buc = ROFF_HASH(p);
                    772:
                    773:        if (NULL == (n = hash[buc]))
                    774:                return(ROFF_MAX);
                    775:        for ( ; n; n = n->next)
1.106     kristaps  776:                if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85      kristaps  777:                        return((enum rofft)(n - roffs));
1.67      kristaps  778:
                    779:        return(ROFF_MAX);
                    780: }
                    781:
                    782: /*
                    783:  * Pop the current node off of the stack of roff instructions currently
                    784:  * pending.
                    785:  */
                    786: static void
                    787: roffnode_pop(struct roff *r)
                    788: {
                    789:        struct roffnode *p;
                    790:
1.75      kristaps  791:        assert(r->last);
1.207     schwarze  792:        p = r->last;
1.82      kristaps  793:
1.75      kristaps  794:        r->last = r->last->parent;
1.106     kristaps  795:        free(p->name);
                    796:        free(p->end);
1.67      kristaps  797:        free(p);
                    798: }
                    799:
                    800: /*
                    801:  * Push a roff node onto the instruction stack.  This must later be
                    802:  * removed with roffnode_pop().
                    803:  */
1.98      schwarze  804: static void
1.106     kristaps  805: roffnode_push(struct roff *r, enum rofft tok, const char *name,
                    806:                int line, int col)
1.67      kristaps  807: {
                    808:        struct roffnode *p;
                    809:
1.98      schwarze  810:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  811:        p->tok = tok;
1.106     kristaps  812:        if (name)
                    813:                p->name = mandoc_strdup(name);
1.67      kristaps  814:        p->parent = r->last;
                    815:        p->line = line;
                    816:        p->col = col;
1.198     schwarze  817:        p->rule = p->parent ? p->parent->rule : 0;
1.67      kristaps  818:
                    819:        r->last = p;
                    820: }
                    821:
                    822: static void
                    823: roff_free1(struct roff *r)
                    824: {
1.176     schwarze  825:        struct tbl_node *tbl;
1.125     kristaps  826:        struct eqn_node *e;
1.167     kristaps  827:        int              i;
1.67      kristaps  828:
1.176     schwarze  829:        while (NULL != (tbl = r->first_tbl)) {
                    830:                r->first_tbl = tbl->next;
                    831:                tbl_free(tbl);
1.109     kristaps  832:        }
1.113     kristaps  833:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    834:
1.125     kristaps  835:        while (NULL != (e = r->first_eqn)) {
                    836:                r->first_eqn = e->next;
                    837:                eqn_free(e);
                    838:        }
                    839:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    840:
1.67      kristaps  841:        while (r->last)
                    842:                roffnode_pop(r);
1.109     kristaps  843:
1.223     schwarze  844:        free (r->rstack);
                    845:        r->rstack = NULL;
                    846:        r->rstacksz = 0;
                    847:        r->rstackpos = -1;
                    848:
                    849:        roff_freereg(r->regtab);
                    850:        r->regtab = NULL;
                    851:
1.167     kristaps  852:        roff_freestr(r->strtab);
                    853:        roff_freestr(r->xmbtab);
                    854:        r->strtab = r->xmbtab = NULL;
                    855:
                    856:        if (r->xtab)
                    857:                for (i = 0; i < 128; i++)
                    858:                        free(r->xtab[i].p);
                    859:        free(r->xtab);
                    860:        r->xtab = NULL;
1.67      kristaps  861: }
                    862:
                    863: void
                    864: roff_reset(struct roff *r)
                    865: {
                    866:
                    867:        roff_free1(r);
1.227     schwarze  868:        r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.174     kristaps  869:        r->control = 0;
1.67      kristaps  870: }
                    871:
                    872: void
                    873: roff_free(struct roff *r)
                    874: {
                    875:
                    876:        roff_free1(r);
                    877:        free(r);
                    878: }
                    879:
                    880: struct roff *
1.237     schwarze  881: roff_alloc(struct mparse *parse, const struct mchars *mchars, int options)
1.67      kristaps  882: {
                    883:        struct roff     *r;
                    884:
1.98      schwarze  885:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  886:        r->parse = parse;
1.237     schwarze  887:        r->mchars = mchars;
1.199     schwarze  888:        r->options = options;
1.227     schwarze  889:        r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.82      kristaps  890:        r->rstackpos = -1;
1.207     schwarze  891:
1.155     kristaps  892:        roffhash_init();
1.141     kristaps  893:
1.67      kristaps  894:        return(r);
                    895: }
                    896:
1.94      kristaps  897: /*
1.206     schwarze  898:  * In the current line, expand escape sequences that tend to get
                    899:  * used in numerical expressions and conditional requests.
                    900:  * Also check the syntax of the remaining escape sequences.
1.154     kristaps  901:  */
1.172     schwarze  902: static enum rofferr
1.238     schwarze  903: roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1.94      kristaps  904: {
1.208     schwarze  905:        char             ubuf[24]; /* buffer to print the number */
1.205     schwarze  906:        const char      *start; /* start of the string to process */
1.209     schwarze  907:        char            *stesc; /* start of an escape sequence ('\\') */
1.108     schwarze  908:        const char      *stnam; /* start of the name, after "[(*" */
                    909:        const char      *cp;    /* end of the name, e.g. before ']' */
                    910:        const char      *res;   /* the string to be substituted */
1.238     schwarze  911:        char            *nbuf;  /* new buffer to copy buf->buf to */
1.181     schwarze  912:        size_t           maxl;  /* expected length of the escape name */
                    913:        size_t           naml;  /* actual length of the escape name */
1.237     schwarze  914:        enum mandoc_esc  esc;   /* type of the escape sequence */
                    915:        int              inaml; /* length returned from mandoc_escape() */
1.181     schwarze  916:        int              expand_count;  /* to avoid infinite loops */
1.206     schwarze  917:        int              npos;  /* position in numeric expression */
1.218     schwarze  918:        int              arg_complete; /* argument not interrupted by eol */
1.206     schwarze  919:        char             term;  /* character terminating the escape */
1.94      kristaps  920:
1.170     schwarze  921:        expand_count = 0;
1.238     schwarze  922:        start = buf->buf + pos;
1.205     schwarze  923:        stesc = strchr(start, '\0') - 1;
                    924:        while (stesc-- > start) {
1.170     schwarze  925:
1.205     schwarze  926:                /* Search backwards for the next backslash. */
                    927:
1.238     schwarze  928:                if (*stesc != '\\')
1.205     schwarze  929:                        continue;
                    930:
                    931:                /* If it is escaped, skip it. */
                    932:
                    933:                for (cp = stesc - 1; cp >= start; cp--)
1.238     schwarze  934:                        if (*cp != '\\')
1.205     schwarze  935:                                break;
                    936:
1.238     schwarze  937:                if ((stesc - cp) % 2 == 0) {
1.209     schwarze  938:                        stesc = (char *)cp;
1.205     schwarze  939:                        continue;
                    940:                }
1.108     schwarze  941:
1.206     schwarze  942:                /* Decide whether to expand or to check only. */
1.108     schwarze  943:
1.206     schwarze  944:                term = '\0';
1.205     schwarze  945:                cp = stesc + 1;
1.181     schwarze  946:                switch (*cp) {
1.207     schwarze  947:                case '*':
1.181     schwarze  948:                        res = NULL;
                    949:                        break;
1.207     schwarze  950:                case 'B':
1.206     schwarze  951:                        /* FALLTHROUGH */
1.207     schwarze  952:                case 'w':
1.206     schwarze  953:                        term = cp[1];
                    954:                        /* FALLTHROUGH */
1.207     schwarze  955:                case 'n':
1.181     schwarze  956:                        res = ubuf;
                    957:                        break;
                    958:                default:
1.237     schwarze  959:                        esc = mandoc_escape(&cp, &stnam, &inaml);
                    960:                        if (esc == ESCAPE_ERROR ||
                    961:                            (esc == ESCAPE_SPECIAL &&
                    962:                             mchars_spec2cp(r->mchars, stnam, inaml) < 0))
1.219     schwarze  963:                                mandoc_vmsg(MANDOCERR_ESC_BAD,
1.238     schwarze  964:                                    r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze  965:                                    "%.*s", (int)(cp - stesc), stesc);
1.205     schwarze  966:                        continue;
1.152     kristaps  967:                }
                    968:
1.205     schwarze  969:                if (EXPAND_LIMIT < ++expand_count) {
                    970:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1.238     schwarze  971:                            ln, (int)(stesc - buf->buf), NULL);
1.205     schwarze  972:                        return(ROFF_IGN);
                    973:                }
1.108     schwarze  974:
                    975:                /*
                    976:                 * The third character decides the length
1.181     schwarze  977:                 * of the name of the string or register.
1.108     schwarze  978:                 * Save a pointer to the name.
                    979:                 */
                    980:
1.238     schwarze  981:                if (term == '\0') {
1.206     schwarze  982:                        switch (*++cp) {
1.207     schwarze  983:                        case '\0':
1.206     schwarze  984:                                maxl = 0;
                    985:                                break;
1.207     schwarze  986:                        case '(':
1.206     schwarze  987:                                cp++;
                    988:                                maxl = 2;
                    989:                                break;
1.207     schwarze  990:                        case '[':
1.206     schwarze  991:                                cp++;
                    992:                                term = ']';
                    993:                                maxl = 0;
                    994:                                break;
                    995:                        default:
                    996:                                maxl = 1;
                    997:                                break;
                    998:                        }
                    999:                } else {
                   1000:                        cp += 2;
1.94      kristaps 1001:                        maxl = 0;
                   1002:                }
1.108     schwarze 1003:                stnam = cp;
1.94      kristaps 1004:
1.108     schwarze 1005:                /* Advance to the end of the name. */
1.94      kristaps 1006:
1.253     schwarze 1007:                naml = 0;
1.218     schwarze 1008:                arg_complete = 1;
1.253     schwarze 1009:                while (maxl == 0 || naml < maxl) {
1.238     schwarze 1010:                        if (*cp == '\0') {
1.219     schwarze 1011:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 1012:                                    ln, (int)(stesc - buf->buf), stesc);
1.218     schwarze 1013:                                arg_complete = 0;
1.206     schwarze 1014:                                break;
1.153     kristaps 1015:                        }
1.238     schwarze 1016:                        if (maxl == 0 && *cp == term) {
1.206     schwarze 1017:                                cp++;
1.253     schwarze 1018:                                break;
                   1019:                        }
                   1020:                        if (*cp++ != '\\' || stesc[1] != 'w') {
                   1021:                                naml++;
                   1022:                                continue;
                   1023:                        }
                   1024:                        switch (mandoc_escape(&cp, NULL, NULL)) {
                   1025:                        case ESCAPE_SPECIAL:
                   1026:                                /* FALLTHROUGH */
                   1027:                        case ESCAPE_UNICODE:
                   1028:                                /* FALLTHROUGH */
                   1029:                        case ESCAPE_NUMBERED:
                   1030:                                /* FALLTHROUGH */
                   1031:                        case ESCAPE_OVERSTRIKE:
                   1032:                                naml++;
                   1033:                                break;
                   1034:                        default:
1.94      kristaps 1035:                                break;
1.206     schwarze 1036:                        }
1.94      kristaps 1037:                }
                   1038:
1.108     schwarze 1039:                /*
                   1040:                 * Retrieve the replacement string; if it is
                   1041:                 * undefined, resume searching for escapes.
                   1042:                 */
                   1043:
1.206     schwarze 1044:                switch (stesc[1]) {
1.207     schwarze 1045:                case '*':
1.218     schwarze 1046:                        if (arg_complete)
                   1047:                                res = roff_getstrn(r, stnam, naml);
1.206     schwarze 1048:                        break;
1.207     schwarze 1049:                case 'B':
1.206     schwarze 1050:                        npos = 0;
1.218     schwarze 1051:                        ubuf[0] = arg_complete &&
1.234     kristaps 1052:                            roff_evalnum(r, ln, stnam, &npos, NULL, 0) &&
1.218     schwarze 1053:                            stnam + npos + 1 == cp ? '1' : '0';
1.206     schwarze 1054:                        ubuf[1] = '\0';
                   1055:                        break;
1.207     schwarze 1056:                case 'n':
1.218     schwarze 1057:                        if (arg_complete)
                   1058:                                (void)snprintf(ubuf, sizeof(ubuf), "%d",
                   1059:                                    roff_getregn(r, stnam, naml));
                   1060:                        else
                   1061:                                ubuf[0] = '\0';
1.206     schwarze 1062:                        break;
1.207     schwarze 1063:                case 'w':
1.218     schwarze 1064:                        /* use even incomplete args */
1.208     schwarze 1065:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206     schwarze 1066:                            24 * (int)naml);
                   1067:                        break;
                   1068:                }
1.94      kristaps 1069:
1.238     schwarze 1070:                if (res == NULL) {
1.219     schwarze 1071:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1072:                            r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze 1073:                            "%.*s", (int)naml, stnam);
1.142     kristaps 1074:                        res = "";
1.246     schwarze 1075:                } else if (buf->sz + strlen(res) > SHRT_MAX) {
                   1076:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                   1077:                            ln, (int)(stesc - buf->buf), NULL);
                   1078:                        return(ROFF_IGN);
1.94      kristaps 1079:                }
                   1080:
1.108     schwarze 1081:                /* Replace the escape sequence by the string. */
                   1082:
1.209     schwarze 1083:                *stesc = '\0';
1.238     schwarze 1084:                buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
                   1085:                    buf->buf, res, cp) + 1;
1.94      kristaps 1086:
1.205     schwarze 1087:                /* Prepare for the next replacement. */
1.94      kristaps 1088:
1.205     schwarze 1089:                start = nbuf + pos;
1.238     schwarze 1090:                stesc = nbuf + (stesc - buf->buf) + strlen(res);
                   1091:                free(buf->buf);
                   1092:                buf->buf = nbuf;
1.154     kristaps 1093:        }
1.172     schwarze 1094:        return(ROFF_CONT);
1.154     kristaps 1095: }
                   1096:
                   1097: /*
1.178     schwarze 1098:  * Process text streams:
                   1099:  * Convert all breakable hyphens into ASCII_HYPH.
                   1100:  * Decrement and spring input line trap.
1.154     kristaps 1101:  */
                   1102: static enum rofferr
1.238     schwarze 1103: roff_parsetext(struct buf *buf, int pos, int *offs)
1.154     kristaps 1104: {
                   1105:        size_t           sz;
                   1106:        const char      *start;
1.178     schwarze 1107:        char            *p;
                   1108:        int              isz;
1.154     kristaps 1109:        enum mandoc_esc  esc;
                   1110:
1.238     schwarze 1111:        start = p = buf->buf + pos;
1.154     kristaps 1112:
1.238     schwarze 1113:        while (*p != '\0') {
1.154     kristaps 1114:                sz = strcspn(p, "-\\");
                   1115:                p += sz;
                   1116:
1.238     schwarze 1117:                if (*p == '\0')
1.159     kristaps 1118:                        break;
                   1119:
1.238     schwarze 1120:                if (*p == '\\') {
1.154     kristaps 1121:                        /* Skip over escapes. */
                   1122:                        p++;
1.189     schwarze 1123:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238     schwarze 1124:                        if (esc == ESCAPE_ERROR)
1.154     kristaps 1125:                                break;
1.155     kristaps 1126:                        continue;
1.159     kristaps 1127:                } else if (p == start) {
1.158     kristaps 1128:                        p++;
1.155     kristaps 1129:                        continue;
1.158     kristaps 1130:                }
1.155     kristaps 1131:
1.171     schwarze 1132:                if (isalpha((unsigned char)p[-1]) &&
                   1133:                    isalpha((unsigned char)p[1]))
1.155     kristaps 1134:                        *p = ASCII_HYPH;
                   1135:                p++;
1.94      kristaps 1136:        }
                   1137:
1.178     schwarze 1138:        /* Spring the input line trap. */
1.238     schwarze 1139:        if (roffit_lines == 1) {
                   1140:                isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
                   1141:                free(buf->buf);
                   1142:                buf->buf = p;
                   1143:                buf->sz = isz + 1;
1.178     schwarze 1144:                *offs = 0;
                   1145:                free(roffit_macro);
                   1146:                roffit_lines = 0;
                   1147:                return(ROFF_REPARSE);
1.238     schwarze 1148:        } else if (roffit_lines > 1)
1.178     schwarze 1149:                --roffit_lines;
1.154     kristaps 1150:        return(ROFF_CONT);
1.94      kristaps 1151: }
                   1152:
1.67      kristaps 1153: enum rofferr
1.238     schwarze 1154: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67      kristaps 1155: {
                   1156:        enum rofft       t;
1.109     kristaps 1157:        enum rofferr     e;
1.238     schwarze 1158:        int              pos;   /* parse point */
1.243     schwarze 1159:        int              spos;  /* saved parse point for messages */
1.238     schwarze 1160:        int              ppos;  /* original offset in buf->buf */
                   1161:        int              ctl;   /* macro line (boolean) */
                   1162:
                   1163:        ppos = pos = *offs;
1.79      kristaps 1164:
1.230     schwarze 1165:        /* Handle in-line equation delimiters. */
                   1166:
1.236     schwarze 1167:        if (r->tbl == NULL &&
                   1168:            r->last_eqn != NULL && r->last_eqn->delim &&
1.230     schwarze 1169:            (r->eqn == NULL || r->eqn_inline)) {
1.238     schwarze 1170:                e = roff_eqndelim(r, buf, pos);
1.230     schwarze 1171:                if (e == ROFF_REPARSE)
                   1172:                        return(e);
                   1173:                assert(e == ROFF_CONT);
                   1174:        }
                   1175:
                   1176:        /* Expand some escape sequences. */
1.94      kristaps 1177:
1.238     schwarze 1178:        e = roff_res(r, buf, ln, pos);
                   1179:        if (e == ROFF_IGN)
1.172     schwarze 1180:                return(e);
1.238     schwarze 1181:        assert(e == ROFF_CONT);
1.94      kristaps 1182:
1.238     schwarze 1183:        ctl = roff_getcontrol(r, buf->buf, &pos);
1.130     kristaps 1184:
1.94      kristaps 1185:        /*
1.79      kristaps 1186:         * First, if a scope is open and we're not a macro, pass the
1.252     schwarze 1187:         * text through the macro's filter.
                   1188:         * Equations process all content themselves.
                   1189:         * Tables process almost all content themselves, but we want
                   1190:         * to warn about macros before passing it there.
1.79      kristaps 1191:         */
1.74      kristaps 1192:
1.252     schwarze 1193:        if (r->last != NULL && ! ctl) {
1.78      kristaps 1194:                t = r->last->tok;
                   1195:                assert(roffs[t].text);
1.238     schwarze 1196:                e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
                   1197:                assert(e == ROFF_IGN || e == ROFF_CONT);
                   1198:                if (e != ROFF_CONT)
1.125     kristaps 1199:                        return(e);
1.182     schwarze 1200:        }
1.252     schwarze 1201:        if (r->eqn != NULL)
1.238     schwarze 1202:                return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
1.252     schwarze 1203:        if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
                   1204:                return(tbl_read(r->tbl, ln, buf->buf, pos));
                   1205:        if ( ! ctl)
1.238     schwarze 1206:                return(roff_parsetext(buf, pos, offs));
1.228     schwarze 1207:
                   1208:        /* Skip empty request lines. */
                   1209:
1.238     schwarze 1210:        if (buf->buf[pos] == '"') {
1.228     schwarze 1211:                mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
                   1212:                    ln, pos, NULL);
                   1213:                return(ROFF_IGN);
1.238     schwarze 1214:        } else if (buf->buf[pos] == '\0')
1.228     schwarze 1215:                return(ROFF_IGN);
1.67      kristaps 1216:
1.79      kristaps 1217:        /*
                   1218:         * If a scope is open, go to the child handler for that macro,
                   1219:         * as it may want to preprocess before doing anything with it.
1.125     kristaps 1220:         * Don't do so if an equation is open.
1.79      kristaps 1221:         */
1.78      kristaps 1222:
1.79      kristaps 1223:        if (r->last) {
                   1224:                t = r->last->tok;
                   1225:                assert(roffs[t].sub);
1.238     schwarze 1226:                return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
1.79      kristaps 1227:        }
1.78      kristaps 1228:
1.243     schwarze 1229:        /* No scope is open.  This is a new request or macro. */
                   1230:
                   1231:        spos = pos;
                   1232:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   1233:
                   1234:        /* Tables ignore most macros. */
                   1235:
                   1236:        if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
                   1237:                mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
                   1238:                    ln, pos, buf->buf + spos);
                   1239:                return(ROFF_IGN);
                   1240:        }
                   1241:
1.79      kristaps 1242:        /*
1.243     schwarze 1243:         * This is neither a roff request nor a user-defined macro.
                   1244:         * Let the standard macro set parsers handle it.
1.79      kristaps 1245:         */
1.67      kristaps 1246:
1.243     schwarze 1247:        if (t == ROFF_MAX)
1.79      kristaps 1248:                return(ROFF_CONT);
1.243     schwarze 1249:
                   1250:        /* Execute a roff request or a user defined macro. */
1.67      kristaps 1251:
1.75      kristaps 1252:        assert(roffs[t].proc);
1.238     schwarze 1253:        return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.74      kristaps 1254: }
                   1255:
1.117     kristaps 1256: void
1.74      kristaps 1257: roff_endparse(struct roff *r)
                   1258: {
                   1259:
1.110     kristaps 1260:        if (r->last)
1.221     schwarze 1261:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1262:                    r->last->line, r->last->col,
                   1263:                    roffs[r->last->tok].name);
1.117     kristaps 1264:
1.125     kristaps 1265:        if (r->eqn) {
1.221     schwarze 1266:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1267:                    r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151     kristaps 1268:                eqn_end(&r->eqn);
1.125     kristaps 1269:        }
                   1270:
1.117     kristaps 1271:        if (r->tbl) {
1.221     schwarze 1272:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1273:                    r->tbl->line, r->tbl->pos, "TS");
1.151     kristaps 1274:                tbl_end(&r->tbl);
1.117     kristaps 1275:        }
1.67      kristaps 1276: }
                   1277:
                   1278: /*
                   1279:  * Parse a roff node's type from the input buffer.  This must be in the
                   1280:  * form of ".foo xxx" in the usual way.
                   1281:  */
                   1282: static enum rofft
1.214     schwarze 1283: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67      kristaps 1284: {
1.214     schwarze 1285:        char            *cp;
1.106     kristaps 1286:        const char      *mac;
                   1287:        size_t           maclen;
1.67      kristaps 1288:        enum rofft       t;
                   1289:
1.214     schwarze 1290:        cp = buf + *pos;
                   1291:
                   1292:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67      kristaps 1293:                return(ROFF_MAX);
                   1294:
1.214     schwarze 1295:        mac = cp;
                   1296:        maclen = roff_getname(r, &cp, ln, ppos);
1.67      kristaps 1297:
1.106     kristaps 1298:        t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155     kristaps 1299:            ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67      kristaps 1300:
1.214     schwarze 1301:        if (ROFF_MAX != t)
                   1302:                *pos = cp - buf;
1.67      kristaps 1303:
                   1304:        return(t);
                   1305: }
                   1306:
                   1307: static enum rofferr
1.76      kristaps 1308: roff_cblock(ROFF_ARGS)
1.67      kristaps 1309: {
                   1310:
1.79      kristaps 1311:        /*
                   1312:         * A block-close `..' should only be invoked as a child of an
                   1313:         * ignore macro, otherwise raise a warning and just ignore it.
                   1314:         */
                   1315:
1.238     schwarze 1316:        if (r->last == NULL) {
1.221     schwarze 1317:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1318:                    ln, ppos, "..");
1.76      kristaps 1319:                return(ROFF_IGN);
                   1320:        }
1.67      kristaps 1321:
1.81      kristaps 1322:        switch (r->last->tok) {
1.207     schwarze 1323:        case ROFF_am:
1.220     schwarze 1324:                /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81      kristaps 1325:                /* FALLTHROUGH */
1.207     schwarze 1326:        case ROFF_ami:
1.81      kristaps 1327:                /* FALLTHROUGH */
1.207     schwarze 1328:        case ROFF_de:
1.108     schwarze 1329:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81      kristaps 1330:                /* FALLTHROUGH */
1.207     schwarze 1331:        case ROFF_dei:
1.81      kristaps 1332:                /* FALLTHROUGH */
1.207     schwarze 1333:        case ROFF_ig:
1.81      kristaps 1334:                break;
                   1335:        default:
1.221     schwarze 1336:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1337:                    ln, ppos, "..");
1.67      kristaps 1338:                return(ROFF_IGN);
1.76      kristaps 1339:        }
1.67      kristaps 1340:
1.238     schwarze 1341:        if (buf->buf[pos] != '\0')
1.217     schwarze 1342:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 1343:                    ".. %s", buf->buf + pos);
1.71      kristaps 1344:
                   1345:        roffnode_pop(r);
1.76      kristaps 1346:        roffnode_cleanscope(r);
                   1347:        return(ROFF_IGN);
1.71      kristaps 1348:
1.67      kristaps 1349: }
                   1350:
1.76      kristaps 1351: static void
                   1352: roffnode_cleanscope(struct roff *r)
1.67      kristaps 1353: {
                   1354:
1.76      kristaps 1355:        while (r->last) {
1.173     schwarze 1356:                if (--r->last->endspan != 0)
1.76      kristaps 1357:                        break;
                   1358:                roffnode_pop(r);
                   1359:        }
1.67      kristaps 1360: }
                   1361:
1.195     schwarze 1362: static void
                   1363: roff_ccond(struct roff *r, int ln, int ppos)
1.74      kristaps 1364: {
                   1365:
1.76      kristaps 1366:        if (NULL == r->last) {
1.221     schwarze 1367:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1368:                    ln, ppos, "\\}");
1.195     schwarze 1369:                return;
1.76      kristaps 1370:        }
                   1371:
1.82      kristaps 1372:        switch (r->last->tok) {
1.207     schwarze 1373:        case ROFF_el:
1.82      kristaps 1374:                /* FALLTHROUGH */
1.207     schwarze 1375:        case ROFF_ie:
1.82      kristaps 1376:                /* FALLTHROUGH */
1.207     schwarze 1377:        case ROFF_if:
1.82      kristaps 1378:                break;
                   1379:        default:
1.221     schwarze 1380:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1381:                    ln, ppos, "\\}");
1.195     schwarze 1382:                return;
1.75      kristaps 1383:        }
                   1384:
1.76      kristaps 1385:        if (r->last->endspan > -1) {
1.221     schwarze 1386:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1387:                    ln, ppos, "\\}");
1.195     schwarze 1388:                return;
1.76      kristaps 1389:        }
                   1390:
1.75      kristaps 1391:        roffnode_pop(r);
1.76      kristaps 1392:        roffnode_cleanscope(r);
1.195     schwarze 1393:        return;
1.76      kristaps 1394: }
                   1395:
                   1396: static enum rofferr
1.80      kristaps 1397: roff_block(ROFF_ARGS)
1.76      kristaps 1398: {
1.220     schwarze 1399:        const char      *name;
                   1400:        char            *iname, *cp;
1.213     schwarze 1401:        size_t           namesz;
1.106     kristaps 1402:
1.220     schwarze 1403:        /* Ignore groff compatibility mode for now. */
1.76      kristaps 1404:
1.238     schwarze 1405:        if (tok == ROFF_de1)
1.220     schwarze 1406:                tok = ROFF_de;
1.251     schwarze 1407:        else if (tok == ROFF_dei1)
                   1408:                tok = ROFF_dei;
1.238     schwarze 1409:        else if (tok == ROFF_am1)
1.220     schwarze 1410:                tok = ROFF_am;
1.251     schwarze 1411:        else if (tok == ROFF_ami1)
                   1412:                tok = ROFF_ami;
1.220     schwarze 1413:
                   1414:        /* Parse the macro name argument. */
                   1415:
1.238     schwarze 1416:        cp = buf->buf + pos;
                   1417:        if (tok == ROFF_ig) {
1.220     schwarze 1418:                iname = NULL;
                   1419:                namesz = 0;
                   1420:        } else {
                   1421:                iname = cp;
                   1422:                namesz = roff_getname(r, &cp, ln, ppos);
                   1423:                iname[namesz] = '\0';
                   1424:        }
1.107     kristaps 1425:
1.220     schwarze 1426:        /* Resolve the macro name argument if it is indirect. */
1.107     kristaps 1427:
1.238     schwarze 1428:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1429:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1430:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1431:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1432:                            "%.*s", (int)namesz, iname);
                   1433:                        namesz = 0;
                   1434:                } else
                   1435:                        namesz = strlen(name);
                   1436:        } else
                   1437:                name = iname;
1.107     kristaps 1438:
1.238     schwarze 1439:        if (namesz == 0 && tok != ROFF_ig) {
1.220     schwarze 1440:                mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
                   1441:                    ln, ppos, roffs[tok].name);
                   1442:                return(ROFF_IGN);
                   1443:        }
1.80      kristaps 1444:
1.106     kristaps 1445:        roffnode_push(r, tok, name, ln, ppos);
                   1446:
                   1447:        /*
                   1448:         * At the beginning of a `de' macro, clear the existing string
                   1449:         * with the same name, if there is one.  New content will be
1.193     schwarze 1450:         * appended from roff_block_text() in multiline mode.
1.106     kristaps 1451:         */
1.107     kristaps 1452:
1.238     schwarze 1453:        if (tok == ROFF_de || tok == ROFF_dei)
1.213     schwarze 1454:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76      kristaps 1455:
1.238     schwarze 1456:        if (*cp == '\0')
1.78      kristaps 1457:                return(ROFF_IGN);
                   1458:
1.220     schwarze 1459:        /* Get the custom end marker. */
1.107     kristaps 1460:
1.220     schwarze 1461:        iname = cp;
1.213     schwarze 1462:        namesz = roff_getname(r, &cp, ln, ppos);
1.220     schwarze 1463:
                   1464:        /* Resolve the end marker if it is indirect. */
                   1465:
1.238     schwarze 1466:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1467:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1468:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1469:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1470:                            "%.*s", (int)namesz, iname);
                   1471:                        namesz = 0;
                   1472:                } else
                   1473:                        namesz = strlen(name);
                   1474:        } else
                   1475:                name = iname;
                   1476:
1.213     schwarze 1477:        if (namesz)
                   1478:                r->last->end = mandoc_strndup(name, namesz);
1.78      kristaps 1479:
1.238     schwarze 1480:        if (*cp != '\0')
1.217     schwarze 1481:                mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
                   1482:                    ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74      kristaps 1483:
1.78      kristaps 1484:        return(ROFF_IGN);
                   1485: }
                   1486:
                   1487: static enum rofferr
1.80      kristaps 1488: roff_block_sub(ROFF_ARGS)
1.79      kristaps 1489: {
                   1490:        enum rofft      t;
                   1491:        int             i, j;
                   1492:
                   1493:        /*
                   1494:         * First check whether a custom macro exists at this level.  If
                   1495:         * it does, then check against it.  This is some of groff's
                   1496:         * stranger behaviours.  If we encountered a custom end-scope
                   1497:         * tag and that tag also happens to be a "real" macro, then we
                   1498:         * need to try interpreting it again as a real macro.  If it's
                   1499:         * not, then return ignore.  Else continue.
                   1500:         */
                   1501:
                   1502:        if (r->last->end) {
1.130     kristaps 1503:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238     schwarze 1504:                        if (buf->buf[i] != r->last->end[j])
1.79      kristaps 1505:                                break;
                   1506:
1.238     schwarze 1507:                if (r->last->end[j] == '\0' &&
                   1508:                    (buf->buf[i] == '\0' ||
                   1509:                     buf->buf[i] == ' ' ||
                   1510:                     buf->buf[i] == '\t')) {
1.79      kristaps 1511:                        roffnode_pop(r);
                   1512:                        roffnode_cleanscope(r);
                   1513:
1.238     schwarze 1514:                        while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130     kristaps 1515:                                i++;
                   1516:
                   1517:                        pos = i;
1.238     schwarze 1518:                        if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
                   1519:                            ROFF_MAX)
1.79      kristaps 1520:                                return(ROFF_RERUN);
                   1521:                        return(ROFF_IGN);
                   1522:                }
                   1523:        }
                   1524:
                   1525:        /*
                   1526:         * If we have no custom end-query or lookup failed, then try
                   1527:         * pulling it out of the hashtable.
                   1528:         */
                   1529:
1.238     schwarze 1530:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79      kristaps 1531:
1.238     schwarze 1532:        if (t != ROFF_cblock) {
                   1533:                if (tok != ROFF_ig)
                   1534:                        roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.79      kristaps 1535:                return(ROFF_IGN);
1.106     kristaps 1536:        }
1.79      kristaps 1537:
                   1538:        assert(roffs[t].proc);
1.238     schwarze 1539:        return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.79      kristaps 1540: }
                   1541:
                   1542: static enum rofferr
1.80      kristaps 1543: roff_block_text(ROFF_ARGS)
1.78      kristaps 1544: {
                   1545:
1.238     schwarze 1546:        if (tok != ROFF_ig)
                   1547:                roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106     kristaps 1548:
1.78      kristaps 1549:        return(ROFF_IGN);
                   1550: }
                   1551:
                   1552: static enum rofferr
1.82      kristaps 1553: roff_cond_sub(ROFF_ARGS)
                   1554: {
                   1555:        enum rofft       t;
1.139     kristaps 1556:        char            *ep;
1.198     schwarze 1557:        int              rr;
1.82      kristaps 1558:
                   1559:        rr = r->last->rule;
1.139     kristaps 1560:        roffnode_cleanscope(r);
1.238     schwarze 1561:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82      kristaps 1562:
1.139     kristaps 1563:        /*
1.177     schwarze 1564:         * Fully handle known macros when they are structurally
                   1565:         * required or when the conditional evaluated to true.
1.87      kristaps 1566:         */
                   1567:
1.238     schwarze 1568:        if ((t != ROFF_MAX) &&
                   1569:            (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1.177     schwarze 1570:                assert(roffs[t].proc);
1.238     schwarze 1571:                return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.177     schwarze 1572:        }
1.144     kristaps 1573:
1.196     schwarze 1574:        /*
                   1575:         * If `\}' occurs on a macro line without a preceding macro,
                   1576:         * drop the line completely.
                   1577:         */
                   1578:
1.238     schwarze 1579:        ep = buf->buf + pos;
                   1580:        if (ep[0] == '\\' && ep[1] == '}')
1.198     schwarze 1581:                rr = 0;
1.196     schwarze 1582:
1.177     schwarze 1583:        /* Always check for the closing delimiter `\}'. */
1.144     kristaps 1584:
1.238     schwarze 1585:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1586:                if (*(++ep) == '}') {
1.197     schwarze 1587:                        *ep = '&';
1.238     schwarze 1588:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1589:                }
1.247     schwarze 1590:                if (*ep != '\0')
                   1591:                        ++ep;
1.177     schwarze 1592:        }
1.198     schwarze 1593:        return(rr ? ROFF_CONT : ROFF_IGN);
1.82      kristaps 1594: }
                   1595:
                   1596: static enum rofferr
                   1597: roff_cond_text(ROFF_ARGS)
1.78      kristaps 1598: {
1.140     kristaps 1599:        char            *ep;
1.198     schwarze 1600:        int              rr;
1.82      kristaps 1601:
                   1602:        rr = r->last->rule;
1.140     kristaps 1603:        roffnode_cleanscope(r);
1.82      kristaps 1604:
1.238     schwarze 1605:        ep = buf->buf + pos;
                   1606:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1607:                if (*(++ep) == '}') {
1.197     schwarze 1608:                        *ep = '&';
1.238     schwarze 1609:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1610:                }
1.247     schwarze 1611:                if (*ep != '\0')
                   1612:                        ++ep;
1.78      kristaps 1613:        }
1.198     schwarze 1614:        return(rr ? ROFF_CONT : ROFF_IGN);
1.74      kristaps 1615: }
                   1616:
1.204     schwarze 1617: /*
                   1618:  * Parse a single signed integer number.  Stop at the first non-digit.
                   1619:  * If there is at least one digit, return success and advance the
                   1620:  * parse point, else return failure and let the parse point unchanged.
                   1621:  * Ignore overflows, treat them just like the C language.
                   1622:  */
1.184     schwarze 1623: static int
                   1624: roff_getnum(const char *v, int *pos, int *res)
                   1625: {
1.206     schwarze 1626:        int      myres, n, p;
                   1627:
                   1628:        if (NULL == res)
                   1629:                res = &myres;
1.184     schwarze 1630:
                   1631:        p = *pos;
                   1632:        n = v[p] == '-';
                   1633:        if (n)
                   1634:                p++;
                   1635:
                   1636:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204     schwarze 1637:                *res = 10 * *res + v[p] - '0';
1.184     schwarze 1638:        if (p == *pos + n)
                   1639:                return 0;
                   1640:
                   1641:        if (n)
                   1642:                *res = -*res;
                   1643:
1.254     schwarze 1644:        /* Each number may be followed by one optional scaling unit. */
                   1645:
                   1646:        switch (v[p]) {
                   1647:        case 'f':
                   1648:                *res *= 65536;
                   1649:                break;
                   1650:        case 'i':
                   1651:                *res *= 240;
                   1652:                break;
                   1653:        case 'c':
                   1654:                *res *= 240;
                   1655:                *res /= 2.54;
                   1656:                break;
                   1657:        case 'v':
                   1658:                /* FALLTROUGH */
                   1659:        case 'P':
                   1660:                *res *= 40;
                   1661:                break;
                   1662:        case 'm':
                   1663:                /* FALLTROUGH */
                   1664:        case 'n':
                   1665:                *res *= 24;
                   1666:                break;
                   1667:        case 'p':
                   1668:                *res *= 10;
                   1669:                *res /= 3;
                   1670:                break;
                   1671:        case 'u':
                   1672:                break;
                   1673:        case 'M':
                   1674:                *res *= 6;
                   1675:                *res /= 25;
                   1676:                break;
                   1677:        default:
                   1678:                p--;
                   1679:                break;
                   1680:        }
                   1681:
                   1682:        *pos = p + 1;
                   1683:        return(1);
1.184     schwarze 1684: }
                   1685:
1.198     schwarze 1686: /*
                   1687:  * Evaluate a string comparison condition.
                   1688:  * The first character is the delimiter.
                   1689:  * Succeed if the string up to its second occurrence
                   1690:  * matches the string up to its third occurence.
                   1691:  * Advance the cursor after the third occurrence
                   1692:  * or lacking that, to the end of the line.
                   1693:  */
                   1694: static int
                   1695: roff_evalstrcond(const char *v, int *pos)
                   1696: {
                   1697:        const char      *s1, *s2, *s3;
                   1698:        int              match;
                   1699:
                   1700:        match = 0;
                   1701:        s1 = v + *pos;          /* initial delimiter */
                   1702:        s2 = s1 + 1;            /* for scanning the first string */
                   1703:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   1704:
                   1705:        if (NULL == s3)         /* found no middle delimiter */
                   1706:                goto out;
                   1707:
                   1708:        while ('\0' != *++s3) {
                   1709:                if (*s2 != *s3) {  /* mismatch */
                   1710:                        s3 = strchr(s3, *s1);
                   1711:                        break;
                   1712:                }
                   1713:                if (*s3 == *s1) {  /* found the final delimiter */
                   1714:                        match = 1;
                   1715:                        break;
                   1716:                }
                   1717:                s2++;
                   1718:        }
                   1719:
                   1720: out:
                   1721:        if (NULL == s3)
                   1722:                s3 = strchr(s2, '\0');
1.242     schwarze 1723:        else if (*s3 != '\0')
1.198     schwarze 1724:                s3++;
                   1725:        *pos = s3 - v;
                   1726:        return(match);
                   1727: }
                   1728:
1.204     schwarze 1729: /*
                   1730:  * Evaluate an optionally negated single character, numerical,
                   1731:  * or string condition.
                   1732:  */
1.198     schwarze 1733: static int
1.234     kristaps 1734: roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
1.88      kristaps 1735: {
1.241     schwarze 1736:        int      number, savepos, wanttrue;
1.88      kristaps 1737:
1.198     schwarze 1738:        if ('!' == v[*pos]) {
                   1739:                wanttrue = 0;
                   1740:                (*pos)++;
                   1741:        } else
                   1742:                wanttrue = 1;
                   1743:
1.88      kristaps 1744:        switch (v[*pos]) {
1.240     schwarze 1745:        case '\0':
                   1746:                return(0);
1.207     schwarze 1747:        case 'n':
1.198     schwarze 1748:                /* FALLTHROUGH */
1.207     schwarze 1749:        case 'o':
1.88      kristaps 1750:                (*pos)++;
1.198     schwarze 1751:                return(wanttrue);
1.207     schwarze 1752:        case 'c':
1.198     schwarze 1753:                /* FALLTHROUGH */
1.207     schwarze 1754:        case 'd':
1.198     schwarze 1755:                /* FALLTHROUGH */
1.207     schwarze 1756:        case 'e':
1.88      kristaps 1757:                /* FALLTHROUGH */
1.207     schwarze 1758:        case 'r':
1.88      kristaps 1759:                /* FALLTHROUGH */
1.207     schwarze 1760:        case 't':
1.239     schwarze 1761:                /* FALLTHROUGH */
                   1762:        case 'v':
1.88      kristaps 1763:                (*pos)++;
1.198     schwarze 1764:                return(!wanttrue);
1.88      kristaps 1765:        default:
                   1766:                break;
                   1767:        }
                   1768:
1.241     schwarze 1769:        savepos = *pos;
1.234     kristaps 1770:        if (roff_evalnum(r, ln, v, pos, &number, 0))
1.204     schwarze 1771:                return((number > 0) == wanttrue);
1.241     schwarze 1772:        else if (*pos == savepos)
                   1773:                return(roff_evalstrcond(v, pos) == wanttrue);
1.204     schwarze 1774:        else
1.241     schwarze 1775:                return (0);
1.88      kristaps 1776: }
                   1777:
1.74      kristaps 1778: static enum rofferr
1.103     kristaps 1779: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 1780: {
1.123     schwarze 1781:
1.89      kristaps 1782:        return(ROFF_IGN);
                   1783: }
                   1784:
1.104     kristaps 1785: static enum rofferr
1.251     schwarze 1786: roff_insec(ROFF_ARGS)
                   1787: {
                   1788:
                   1789:        mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
                   1790:            ln, ppos, roffs[tok].name);
                   1791:        return(ROFF_IGN);
                   1792: }
                   1793:
                   1794: static enum rofferr
                   1795: roff_unsupp(ROFF_ARGS)
                   1796: {
                   1797:
                   1798:        mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
                   1799:            ln, ppos, roffs[tok].name);
                   1800:        return(ROFF_IGN);
                   1801: }
                   1802:
                   1803: static enum rofferr
1.82      kristaps 1804: roff_cond(ROFF_ARGS)
1.74      kristaps 1805: {
1.173     schwarze 1806:
                   1807:        roffnode_push(r, tok, NULL, ln, ppos);
1.74      kristaps 1808:
1.207     schwarze 1809:        /*
1.134     kristaps 1810:         * An `.el' has no conditional body: it will consume the value
                   1811:         * of the current rstack entry set in prior `ie' calls or
1.207     schwarze 1812:         * defaults to DENY.
1.134     kristaps 1813:         *
                   1814:         * If we're not an `el', however, then evaluate the conditional.
                   1815:         */
1.133     kristaps 1816:
1.238     schwarze 1817:        r->last->rule = tok == ROFF_el ?
1.207     schwarze 1818:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238     schwarze 1819:            roff_evalcond(r, ln, buf->buf, &pos);
1.77      kristaps 1820:
1.134     kristaps 1821:        /*
                   1822:         * An if-else will put the NEGATION of the current evaluated
                   1823:         * conditional into the stack of rules.
                   1824:         */
                   1825:
1.238     schwarze 1826:        if (tok == ROFF_ie) {
1.223     schwarze 1827:                if (r->rstackpos + 1 == r->rstacksz) {
                   1828:                        r->rstacksz += 16;
                   1829:                        r->rstack = mandoc_reallocarray(r->rstack,
                   1830:                            r->rstacksz, sizeof(int));
1.134     kristaps 1831:                }
1.198     schwarze 1832:                r->rstack[++r->rstackpos] = !r->last->rule;
1.82      kristaps 1833:        }
1.88      kristaps 1834:
                   1835:        /* If the parent has false as its rule, then so do we. */
                   1836:
1.198     schwarze 1837:        if (r->last->parent && !r->last->parent->rule)
                   1838:                r->last->rule = 0;
1.88      kristaps 1839:
                   1840:        /*
1.173     schwarze 1841:         * Determine scope.
                   1842:         * If there is nothing on the line after the conditional,
                   1843:         * not even whitespace, use next-line scope.
1.88      kristaps 1844:         */
1.74      kristaps 1845:
1.238     schwarze 1846:        if (buf->buf[pos] == '\0') {
1.173     schwarze 1847:                r->last->endspan = 2;
                   1848:                goto out;
                   1849:        }
                   1850:
1.238     schwarze 1851:        while (buf->buf[pos] == ' ')
1.173     schwarze 1852:                pos++;
                   1853:
                   1854:        /* An opening brace requests multiline scope. */
1.75      kristaps 1855:
1.238     schwarze 1856:        if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75      kristaps 1857:                r->last->endspan = -1;
                   1858:                pos += 2;
1.173     schwarze 1859:                goto out;
1.207     schwarze 1860:        }
1.74      kristaps 1861:
1.77      kristaps 1862:        /*
1.173     schwarze 1863:         * Anything else following the conditional causes
                   1864:         * single-line scope.  Warn if the scope contains
                   1865:         * nothing but trailing whitespace.
1.77      kristaps 1866:         */
                   1867:
1.238     schwarze 1868:        if (buf->buf[pos] == '\0')
1.216     schwarze 1869:                mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
                   1870:                    ln, ppos, roffs[tok].name);
1.77      kristaps 1871:
1.173     schwarze 1872:        r->last->endspan = 1;
1.74      kristaps 1873:
1.173     schwarze 1874: out:
1.75      kristaps 1875:        *offs = pos;
                   1876:        return(ROFF_RERUN);
1.83      schwarze 1877: }
                   1878:
                   1879: static enum rofferr
1.92      schwarze 1880: roff_ds(ROFF_ARGS)
                   1881: {
1.212     schwarze 1882:        char            *string;
                   1883:        const char      *name;
                   1884:        size_t           namesz;
1.96      kristaps 1885:
1.251     schwarze 1886:        /* Ignore groff compatibility mode for now. */
                   1887:
                   1888:        if (tok == ROFF_ds1)
                   1889:                tok = ROFF_ds;
                   1890:        else if (tok == ROFF_as1)
                   1891:                tok = ROFF_as;
                   1892:
1.96      kristaps 1893:        /*
1.212     schwarze 1894:         * The first word is the name of the string.
                   1895:         * If it is empty or terminated by an escape sequence,
                   1896:         * abort the `ds' request without defining anything.
1.96      kristaps 1897:         */
1.92      schwarze 1898:
1.238     schwarze 1899:        name = string = buf->buf + pos;
                   1900:        if (*name == '\0')
1.92      schwarze 1901:                return(ROFF_IGN);
                   1902:
1.212     schwarze 1903:        namesz = roff_getname(r, &string, ln, pos);
1.238     schwarze 1904:        if (name[namesz] == '\\')
1.212     schwarze 1905:                return(ROFF_IGN);
                   1906:
                   1907:        /* Read past the initial double-quote, if any. */
1.238     schwarze 1908:        if (*string == '"')
1.92      schwarze 1909:                string++;
                   1910:
1.96      kristaps 1911:        /* The rest is the value. */
1.212     schwarze 1912:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   1913:            ROFF_as == tok);
1.92      schwarze 1914:        return(ROFF_IGN);
                   1915: }
                   1916:
1.204     schwarze 1917: /*
                   1918:  * Parse a single operator, one or two characters long.
                   1919:  * If the operator is recognized, return success and advance the
                   1920:  * parse point, else return failure and let the parse point unchanged.
                   1921:  */
                   1922: static int
                   1923: roff_getop(const char *v, int *pos, char *res)
                   1924: {
                   1925:
                   1926:        *res = v[*pos];
                   1927:
                   1928:        switch (*res) {
1.207     schwarze 1929:        case '+':
1.204     schwarze 1930:                /* FALLTHROUGH */
1.207     schwarze 1931:        case '-':
1.204     schwarze 1932:                /* FALLTHROUGH */
1.207     schwarze 1933:        case '*':
1.204     schwarze 1934:                /* FALLTHROUGH */
1.207     schwarze 1935:        case '/':
1.204     schwarze 1936:                /* FALLTHROUGH */
1.207     schwarze 1937:        case '%':
1.204     schwarze 1938:                /* FALLTHROUGH */
1.207     schwarze 1939:        case '&':
1.204     schwarze 1940:                /* FALLTHROUGH */
1.207     schwarze 1941:        case ':':
1.204     schwarze 1942:                break;
                   1943:        case '<':
                   1944:                switch (v[*pos + 1]) {
1.207     schwarze 1945:                case '=':
1.204     schwarze 1946:                        *res = 'l';
                   1947:                        (*pos)++;
                   1948:                        break;
1.207     schwarze 1949:                case '>':
1.204     schwarze 1950:                        *res = '!';
                   1951:                        (*pos)++;
                   1952:                        break;
1.207     schwarze 1953:                case '?':
1.204     schwarze 1954:                        *res = 'i';
                   1955:                        (*pos)++;
                   1956:                        break;
                   1957:                default:
                   1958:                        break;
                   1959:                }
                   1960:                break;
                   1961:        case '>':
                   1962:                switch (v[*pos + 1]) {
1.207     schwarze 1963:                case '=':
1.204     schwarze 1964:                        *res = 'g';
                   1965:                        (*pos)++;
                   1966:                        break;
1.207     schwarze 1967:                case '?':
1.204     schwarze 1968:                        *res = 'a';
                   1969:                        (*pos)++;
                   1970:                        break;
                   1971:                default:
                   1972:                        break;
                   1973:                }
                   1974:                break;
                   1975:        case '=':
                   1976:                if ('=' == v[*pos + 1])
                   1977:                        (*pos)++;
                   1978:                break;
                   1979:        default:
                   1980:                return(0);
                   1981:        }
                   1982:        (*pos)++;
                   1983:
                   1984:        return(*res);
                   1985: }
                   1986:
                   1987: /*
                   1988:  * Evaluate either a parenthesized numeric expression
                   1989:  * or a single signed integer number.
                   1990:  */
                   1991: static int
1.235     schwarze 1992: roff_evalpar(struct roff *r, int ln,
1.234     kristaps 1993:        const char *v, int *pos, int *res)
1.204     schwarze 1994: {
                   1995:
                   1996:        if ('(' != v[*pos])
                   1997:                return(roff_getnum(v, pos, res));
                   1998:
                   1999:        (*pos)++;
1.234     kristaps 2000:        if ( ! roff_evalnum(r, ln, v, pos, res, 1))
1.204     schwarze 2001:                return(0);
                   2002:
1.206     schwarze 2003:        /*
                   2004:         * Omission of the closing parenthesis
                   2005:         * is an error in validation mode,
                   2006:         * but ignored in evaluation mode.
                   2007:         */
                   2008:
1.204     schwarze 2009:        if (')' == v[*pos])
                   2010:                (*pos)++;
1.206     schwarze 2011:        else if (NULL == res)
                   2012:                return(0);
1.204     schwarze 2013:
                   2014:        return(1);
                   2015: }
                   2016:
                   2017: /*
                   2018:  * Evaluate a complete numeric expression.
                   2019:  * Proceed left to right, there is no concept of precedence.
                   2020:  */
                   2021: static int
1.235     schwarze 2022: roff_evalnum(struct roff *r, int ln, const char *v,
1.234     kristaps 2023:        int *pos, int *res, int skipwhite)
1.204     schwarze 2024: {
                   2025:        int              mypos, operand2;
                   2026:        char             operator;
                   2027:
                   2028:        if (NULL == pos) {
                   2029:                mypos = 0;
                   2030:                pos = &mypos;
                   2031:        }
                   2032:
                   2033:        if (skipwhite)
                   2034:                while (isspace((unsigned char)v[*pos]))
                   2035:                        (*pos)++;
                   2036:
1.234     kristaps 2037:        if ( ! roff_evalpar(r, ln, v, pos, res))
1.204     schwarze 2038:                return(0);
                   2039:
                   2040:        while (1) {
                   2041:                if (skipwhite)
                   2042:                        while (isspace((unsigned char)v[*pos]))
                   2043:                                (*pos)++;
                   2044:
                   2045:                if ( ! roff_getop(v, pos, &operator))
                   2046:                        break;
                   2047:
                   2048:                if (skipwhite)
                   2049:                        while (isspace((unsigned char)v[*pos]))
                   2050:                                (*pos)++;
                   2051:
1.234     kristaps 2052:                if ( ! roff_evalpar(r, ln, v, pos, &operand2))
1.204     schwarze 2053:                        return(0);
                   2054:
                   2055:                if (skipwhite)
                   2056:                        while (isspace((unsigned char)v[*pos]))
                   2057:                                (*pos)++;
1.206     schwarze 2058:
                   2059:                if (NULL == res)
                   2060:                        continue;
1.204     schwarze 2061:
                   2062:                switch (operator) {
1.207     schwarze 2063:                case '+':
1.204     schwarze 2064:                        *res += operand2;
                   2065:                        break;
1.207     schwarze 2066:                case '-':
1.204     schwarze 2067:                        *res -= operand2;
                   2068:                        break;
1.207     schwarze 2069:                case '*':
1.204     schwarze 2070:                        *res *= operand2;
                   2071:                        break;
1.207     schwarze 2072:                case '/':
1.244     schwarze 2073:                        if (operand2 == 0) {
1.235     schwarze 2074:                                mandoc_msg(MANDOCERR_DIVZERO,
1.234     kristaps 2075:                                        r->parse, ln, *pos, v);
                   2076:                                *res = 0;
                   2077:                                break;
                   2078:                        }
1.204     schwarze 2079:                        *res /= operand2;
                   2080:                        break;
1.207     schwarze 2081:                case '%':
1.244     schwarze 2082:                        if (operand2 == 0) {
                   2083:                                mandoc_msg(MANDOCERR_DIVZERO,
                   2084:                                        r->parse, ln, *pos, v);
                   2085:                                *res = 0;
                   2086:                                break;
                   2087:                        }
1.204     schwarze 2088:                        *res %= operand2;
                   2089:                        break;
1.207     schwarze 2090:                case '<':
1.204     schwarze 2091:                        *res = *res < operand2;
                   2092:                        break;
1.207     schwarze 2093:                case '>':
1.204     schwarze 2094:                        *res = *res > operand2;
                   2095:                        break;
1.207     schwarze 2096:                case 'l':
1.204     schwarze 2097:                        *res = *res <= operand2;
                   2098:                        break;
1.207     schwarze 2099:                case 'g':
1.204     schwarze 2100:                        *res = *res >= operand2;
                   2101:                        break;
1.207     schwarze 2102:                case '=':
1.204     schwarze 2103:                        *res = *res == operand2;
                   2104:                        break;
1.207     schwarze 2105:                case '!':
1.204     schwarze 2106:                        *res = *res != operand2;
                   2107:                        break;
1.207     schwarze 2108:                case '&':
1.204     schwarze 2109:                        *res = *res && operand2;
                   2110:                        break;
1.207     schwarze 2111:                case ':':
1.204     schwarze 2112:                        *res = *res || operand2;
                   2113:                        break;
1.207     schwarze 2114:                case 'i':
1.204     schwarze 2115:                        if (operand2 < *res)
                   2116:                                *res = operand2;
                   2117:                        break;
1.207     schwarze 2118:                case 'a':
1.204     schwarze 2119:                        if (operand2 > *res)
                   2120:                                *res = operand2;
                   2121:                        break;
                   2122:                default:
                   2123:                        abort();
                   2124:                }
                   2125:        }
                   2126:        return(1);
                   2127: }
                   2128:
1.180     schwarze 2129: void
1.187     schwarze 2130: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147     kristaps 2131: {
1.180     schwarze 2132:        struct roffreg  *reg;
                   2133:
                   2134:        /* Search for an existing register with the same name. */
                   2135:        reg = r->regtab;
                   2136:
                   2137:        while (reg && strcmp(name, reg->key.p))
                   2138:                reg = reg->next;
1.147     kristaps 2139:
1.180     schwarze 2140:        if (NULL == reg) {
                   2141:                /* Create a new register. */
                   2142:                reg = mandoc_malloc(sizeof(struct roffreg));
                   2143:                reg->key.p = mandoc_strdup(name);
                   2144:                reg->key.sz = strlen(name);
1.187     schwarze 2145:                reg->val = 0;
1.180     schwarze 2146:                reg->next = r->regtab;
                   2147:                r->regtab = reg;
                   2148:        }
                   2149:
1.187     schwarze 2150:        if ('+' == sign)
                   2151:                reg->val += val;
                   2152:        else if ('-' == sign)
                   2153:                reg->val -= val;
                   2154:        else
                   2155:                reg->val = val;
1.147     kristaps 2156: }
                   2157:
1.192     schwarze 2158: /*
                   2159:  * Handle some predefined read-only number registers.
                   2160:  * For now, return -1 if the requested register is not predefined;
                   2161:  * in case a predefined read-only register having the value -1
                   2162:  * were to turn up, another special value would have to be chosen.
                   2163:  */
                   2164: static int
                   2165: roff_getregro(const char *name)
                   2166: {
                   2167:
                   2168:        switch (*name) {
1.207     schwarze 2169:        case 'A':  /* ASCII approximation mode is always off. */
1.192     schwarze 2170:                return(0);
1.207     schwarze 2171:        case 'g':  /* Groff compatibility mode is always on. */
1.192     schwarze 2172:                return(1);
1.207     schwarze 2173:        case 'H':  /* Fixed horizontal resolution. */
1.192     schwarze 2174:                return (24);
1.207     schwarze 2175:        case 'j':  /* Always adjust left margin only. */
1.192     schwarze 2176:                return(0);
1.207     schwarze 2177:        case 'T':  /* Some output device is always defined. */
1.192     schwarze 2178:                return(1);
1.207     schwarze 2179:        case 'V':  /* Fixed vertical resolution. */
1.192     schwarze 2180:                return (40);
                   2181:        default:
                   2182:                return (-1);
                   2183:        }
                   2184: }
                   2185:
1.181     schwarze 2186: int
1.180     schwarze 2187: roff_getreg(const struct roff *r, const char *name)
1.147     kristaps 2188: {
1.180     schwarze 2189:        struct roffreg  *reg;
1.192     schwarze 2190:        int              val;
                   2191:
                   2192:        if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
                   2193:                val = roff_getregro(name + 1);
                   2194:                if (-1 != val)
                   2195:                        return (val);
                   2196:        }
1.180     schwarze 2197:
                   2198:        for (reg = r->regtab; reg; reg = reg->next)
                   2199:                if (0 == strcmp(name, reg->key.p))
1.181     schwarze 2200:                        return(reg->val);
                   2201:
                   2202:        return(0);
                   2203: }
                   2204:
                   2205: static int
                   2206: roff_getregn(const struct roff *r, const char *name, size_t len)
                   2207: {
                   2208:        struct roffreg  *reg;
1.192     schwarze 2209:        int              val;
                   2210:
                   2211:        if ('.' == name[0] && 2 == len) {
                   2212:                val = roff_getregro(name + 1);
                   2213:                if (-1 != val)
                   2214:                        return (val);
                   2215:        }
1.181     schwarze 2216:
                   2217:        for (reg = r->regtab; reg; reg = reg->next)
                   2218:                if (len == reg->key.sz &&
                   2219:                    0 == strncmp(name, reg->key.p, len))
                   2220:                        return(reg->val);
1.147     kristaps 2221:
1.180     schwarze 2222:        return(0);
1.147     kristaps 2223: }
                   2224:
1.180     schwarze 2225: static void
                   2226: roff_freereg(struct roffreg *reg)
1.147     kristaps 2227: {
1.180     schwarze 2228:        struct roffreg  *old_reg;
1.147     kristaps 2229:
1.180     schwarze 2230:        while (NULL != reg) {
                   2231:                free(reg->key.p);
                   2232:                old_reg = reg;
                   2233:                reg = reg->next;
                   2234:                free(old_reg);
                   2235:        }
1.147     kristaps 2236: }
1.92      schwarze 2237:
                   2238: static enum rofferr
1.89      kristaps 2239: roff_nr(ROFF_ARGS)
1.83      schwarze 2240: {
1.212     schwarze 2241:        char            *key, *val;
                   2242:        size_t           keysz;
1.138     kristaps 2243:        int              iv;
1.187     schwarze 2244:        char             sign;
1.89      kristaps 2245:
1.238     schwarze 2246:        key = val = buf->buf + pos;
                   2247:        if (*key == '\0')
1.212     schwarze 2248:                return(ROFF_IGN);
                   2249:
                   2250:        keysz = roff_getname(r, &val, ln, pos);
1.238     schwarze 2251:        if (key[keysz] == '\\')
1.212     schwarze 2252:                return(ROFF_IGN);
                   2253:        key[keysz] = '\0';
1.89      kristaps 2254:
1.187     schwarze 2255:        sign = *val;
1.238     schwarze 2256:        if (sign == '+' || sign == '-')
1.187     schwarze 2257:                val++;
                   2258:
1.234     kristaps 2259:        if (roff_evalnum(r, ln, val, NULL, &iv, 0))
1.204     schwarze 2260:                roff_setreg(r, key, iv, sign);
1.109     kristaps 2261:
1.203     schwarze 2262:        return(ROFF_IGN);
                   2263: }
                   2264:
                   2265: static enum rofferr
                   2266: roff_rr(ROFF_ARGS)
                   2267: {
                   2268:        struct roffreg  *reg, **prev;
1.212     schwarze 2269:        char            *name, *cp;
                   2270:        size_t           namesz;
1.203     schwarze 2271:
1.238     schwarze 2272:        name = cp = buf->buf + pos;
                   2273:        if (*name == '\0')
1.212     schwarze 2274:                return(ROFF_IGN);
                   2275:        namesz = roff_getname(r, &cp, ln, pos);
                   2276:        name[namesz] = '\0';
1.203     schwarze 2277:
                   2278:        prev = &r->regtab;
                   2279:        while (1) {
                   2280:                reg = *prev;
1.238     schwarze 2281:                if (reg == NULL || !strcmp(name, reg->key.p))
1.203     schwarze 2282:                        break;
                   2283:                prev = &reg->next;
                   2284:        }
1.238     schwarze 2285:        if (reg != NULL) {
1.203     schwarze 2286:                *prev = reg->next;
                   2287:                free(reg->key.p);
                   2288:                free(reg);
                   2289:        }
1.122     schwarze 2290:        return(ROFF_IGN);
                   2291: }
                   2292:
                   2293: static enum rofferr
                   2294: roff_rm(ROFF_ARGS)
                   2295: {
                   2296:        const char       *name;
                   2297:        char             *cp;
1.212     schwarze 2298:        size_t            namesz;
1.122     schwarze 2299:
1.238     schwarze 2300:        cp = buf->buf + pos;
                   2301:        while (*cp != '\0') {
1.212     schwarze 2302:                name = cp;
1.238     schwarze 2303:                namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212     schwarze 2304:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.238     schwarze 2305:                if (name[namesz] == '\\')
1.212     schwarze 2306:                        break;
1.122     schwarze 2307:        }
1.178     schwarze 2308:        return(ROFF_IGN);
                   2309: }
                   2310:
                   2311: static enum rofferr
                   2312: roff_it(ROFF_ARGS)
                   2313: {
                   2314:        char            *cp;
                   2315:        size_t           len;
                   2316:        int              iv;
                   2317:
                   2318:        /* Parse the number of lines. */
1.238     schwarze 2319:        cp = buf->buf + pos;
1.178     schwarze 2320:        len = strcspn(cp, " \t");
                   2321:        cp[len] = '\0';
                   2322:        if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1.222     schwarze 2323:                mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238     schwarze 2324:                    ln, ppos, buf->buf + 1);
1.178     schwarze 2325:                return(ROFF_IGN);
                   2326:        }
                   2327:        cp += len + 1;
                   2328:
                   2329:        /* Arm the input line trap. */
                   2330:        roffit_lines = iv;
                   2331:        roffit_macro = mandoc_strdup(cp);
1.109     kristaps 2332:        return(ROFF_IGN);
1.175     schwarze 2333: }
                   2334:
                   2335: static enum rofferr
                   2336: roff_Dd(ROFF_ARGS)
                   2337: {
                   2338:        const char *const       *cp;
                   2339:
1.227     schwarze 2340:        if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175     schwarze 2341:                for (cp = __mdoc_reserved; *cp; cp++)
                   2342:                        roff_setstr(r, *cp, NULL, 0);
                   2343:
1.227     schwarze 2344:        if (r->format == 0)
                   2345:                r->format = MPARSE_MDOC;
                   2346:
1.175     schwarze 2347:        return(ROFF_CONT);
                   2348: }
                   2349:
                   2350: static enum rofferr
                   2351: roff_TH(ROFF_ARGS)
                   2352: {
                   2353:        const char *const       *cp;
                   2354:
1.227     schwarze 2355:        if ((r->options & MPARSE_QUICK) == 0)
1.175     schwarze 2356:                for (cp = __man_reserved; *cp; cp++)
                   2357:                        roff_setstr(r, *cp, NULL, 0);
                   2358:
1.227     schwarze 2359:        if (r->format == 0)
                   2360:                r->format = MPARSE_MAN;
                   2361:
1.175     schwarze 2362:        return(ROFF_CONT);
1.109     kristaps 2363: }
                   2364:
                   2365: static enum rofferr
                   2366: roff_TE(ROFF_ARGS)
                   2367: {
                   2368:
                   2369:        if (NULL == r->tbl)
1.221     schwarze 2370:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2371:                    ln, ppos, "TE");
1.115     kristaps 2372:        else
1.151     kristaps 2373:                tbl_end(&r->tbl);
1.109     kristaps 2374:
1.112     kristaps 2375:        return(ROFF_IGN);
                   2376: }
                   2377:
                   2378: static enum rofferr
                   2379: roff_T_(ROFF_ARGS)
                   2380: {
                   2381:
                   2382:        if (NULL == r->tbl)
1.221     schwarze 2383:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2384:                    ln, ppos, "T&");
1.112     kristaps 2385:        else
1.116     kristaps 2386:                tbl_restart(ppos, ln, r->tbl);
1.112     kristaps 2387:
1.109     kristaps 2388:        return(ROFF_IGN);
                   2389: }
                   2390:
1.230     schwarze 2391: /*
                   2392:  * Handle in-line equation delimiters.
                   2393:  */
                   2394: static enum rofferr
1.238     schwarze 2395: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151     kristaps 2396: {
1.233     schwarze 2397:        char            *cp1, *cp2;
                   2398:        const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151     kristaps 2399:
1.230     schwarze 2400:        /*
                   2401:         * Outside equations, look for an opening delimiter.
                   2402:         * If we are inside an equation, we already know it is
                   2403:         * in-line, or this function wouldn't have been called;
                   2404:         * so look for a closing delimiter.
                   2405:         */
                   2406:
1.238     schwarze 2407:        cp1 = buf->buf + pos;
1.230     schwarze 2408:        cp2 = strchr(cp1, r->eqn == NULL ?
                   2409:            r->last_eqn->odelim : r->last_eqn->cdelim);
                   2410:        if (cp2 == NULL)
                   2411:                return(ROFF_CONT);
                   2412:
1.233     schwarze 2413:        *cp2++ = '\0';
                   2414:        bef_pr = bef_nl = aft_nl = aft_pr = "";
                   2415:
                   2416:        /* Handle preceding text, protecting whitespace. */
                   2417:
1.238     schwarze 2418:        if (*buf->buf != '\0') {
1.233     schwarze 2419:                if (r->eqn == NULL)
                   2420:                        bef_pr = "\\&";
                   2421:                bef_nl = "\n";
                   2422:        }
                   2423:
                   2424:        /*
                   2425:         * Prepare replacing the delimiter with an equation macro
                   2426:         * and drop leading white space from the equation.
                   2427:         */
1.230     schwarze 2428:
1.233     schwarze 2429:        if (r->eqn == NULL) {
                   2430:                while (*cp2 == ' ')
                   2431:                        cp2++;
                   2432:                mac = ".EQ";
                   2433:        } else
                   2434:                mac = ".EN";
                   2435:
                   2436:        /* Handle following text, protecting whitespace. */
                   2437:
                   2438:        if (*cp2 != '\0') {
                   2439:                aft_nl = "\n";
                   2440:                if (r->eqn != NULL)
                   2441:                        aft_pr = "\\&";
                   2442:        }
                   2443:
                   2444:        /* Do the actual replacement. */
                   2445:
1.238     schwarze 2446:        buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233     schwarze 2447:            bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238     schwarze 2448:        free(buf->buf);
                   2449:        buf->buf = cp1;
1.230     schwarze 2450:
                   2451:        /* Toggle the in-line state of the eqn subsystem. */
                   2452:
                   2453:        r->eqn_inline = r->eqn == NULL;
                   2454:        return(ROFF_REPARSE);
1.151     kristaps 2455: }
                   2456:
1.235     schwarze 2457: static enum rofferr
                   2458: roff_EQ(ROFF_ARGS)
1.125     kristaps 2459: {
1.151     kristaps 2460:        struct eqn_node *e;
1.125     kristaps 2461:
1.238     schwarze 2462:        assert(r->eqn == NULL);
1.235     schwarze 2463:        e = eqn_alloc(ppos, ln, r->parse);
1.125     kristaps 2464:
1.230     schwarze 2465:        if (r->last_eqn) {
1.125     kristaps 2466:                r->last_eqn->next = e;
1.230     schwarze 2467:                e->delim = r->last_eqn->delim;
                   2468:                e->odelim = r->last_eqn->odelim;
                   2469:                e->cdelim = r->last_eqn->cdelim;
                   2470:        } else
1.125     kristaps 2471:                r->first_eqn = r->last_eqn = e;
                   2472:
                   2473:        r->eqn = r->last_eqn = e;
1.151     kristaps 2474:
1.238     schwarze 2475:        if (buf->buf[pos] != '\0')
1.235     schwarze 2476:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 2477:                    ".EQ %s", buf->buf + pos);
1.151     kristaps 2478:
1.125     kristaps 2479:        return(ROFF_IGN);
                   2480: }
                   2481:
                   2482: static enum rofferr
                   2483: roff_EN(ROFF_ARGS)
                   2484: {
                   2485:
1.221     schwarze 2486:        mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125     kristaps 2487:        return(ROFF_IGN);
                   2488: }
                   2489:
                   2490: static enum rofferr
1.109     kristaps 2491: roff_TS(ROFF_ARGS)
                   2492: {
1.176     schwarze 2493:        struct tbl_node *tbl;
1.89      kristaps 2494:
1.115     kristaps 2495:        if (r->tbl) {
1.221     schwarze 2496:                mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
                   2497:                    ln, ppos, "TS breaks TS");
1.151     kristaps 2498:                tbl_end(&r->tbl);
1.115     kristaps 2499:        }
1.83      schwarze 2500:
1.176     schwarze 2501:        tbl = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 2502:
                   2503:        if (r->last_tbl)
1.176     schwarze 2504:                r->last_tbl->next = tbl;
1.113     kristaps 2505:        else
1.176     schwarze 2506:                r->first_tbl = r->last_tbl = tbl;
1.113     kristaps 2507:
1.176     schwarze 2508:        r->tbl = r->last_tbl = tbl;
1.83      schwarze 2509:        return(ROFF_IGN);
1.251     schwarze 2510: }
                   2511:
                   2512: static enum rofferr
                   2513: roff_brp(ROFF_ARGS)
                   2514: {
                   2515:
                   2516:        buf->buf[pos - 1] = '\0';
                   2517:        return(ROFF_CONT);
1.92      schwarze 2518: }
                   2519:
1.105     kristaps 2520: static enum rofferr
1.174     kristaps 2521: roff_cc(ROFF_ARGS)
                   2522: {
                   2523:        const char      *p;
                   2524:
1.238     schwarze 2525:        p = buf->buf + pos;
1.174     kristaps 2526:
1.238     schwarze 2527:        if (*p == '\0' || (r->control = *p++) == '.')
1.174     kristaps 2528:                r->control = 0;
                   2529:
1.238     schwarze 2530:        if (*p != '\0')
1.174     kristaps 2531:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   2532:
                   2533:        return(ROFF_IGN);
                   2534: }
                   2535:
                   2536: static enum rofferr
1.164     kristaps 2537: roff_tr(ROFF_ARGS)
                   2538: {
                   2539:        const char      *p, *first, *second;
                   2540:        size_t           fsz, ssz;
                   2541:        enum mandoc_esc  esc;
                   2542:
1.238     schwarze 2543:        p = buf->buf + pos;
1.164     kristaps 2544:
1.238     schwarze 2545:        if (*p == '\0') {
1.164     kristaps 2546:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   2547:                return(ROFF_IGN);
                   2548:        }
                   2549:
1.238     schwarze 2550:        while (*p != '\0') {
1.164     kristaps 2551:                fsz = ssz = 1;
                   2552:
                   2553:                first = p++;
1.238     schwarze 2554:                if (*first == '\\') {
1.164     kristaps 2555:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 2556:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 2557:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 2558:                                    ln, (int)(p - buf->buf), first);
1.164     kristaps 2559:                                return(ROFF_IGN);
                   2560:                        }
                   2561:                        fsz = (size_t)(p - first);
                   2562:                }
                   2563:
                   2564:                second = p++;
1.238     schwarze 2565:                if (*second == '\\') {
1.164     kristaps 2566:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 2567:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 2568:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 2569:                                    ln, (int)(p - buf->buf), second);
1.164     kristaps 2570:                                return(ROFF_IGN);
                   2571:                        }
                   2572:                        ssz = (size_t)(p - second);
1.238     schwarze 2573:                } else if (*second == '\0') {
1.207     schwarze 2574:                        mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1.238     schwarze 2575:                            ln, (int)(p - buf->buf), NULL);
1.164     kristaps 2576:                        second = " ";
1.165     kristaps 2577:                        p--;
1.164     kristaps 2578:                }
                   2579:
1.167     kristaps 2580:                if (fsz > 1) {
1.207     schwarze 2581:                        roff_setstrn(&r->xmbtab, first, fsz,
                   2582:                            second, ssz, 0);
1.167     kristaps 2583:                        continue;
                   2584:                }
                   2585:
1.238     schwarze 2586:                if (r->xtab == NULL)
1.207     schwarze 2587:                        r->xtab = mandoc_calloc(128,
                   2588:                            sizeof(struct roffstr));
1.167     kristaps 2589:
                   2590:                free(r->xtab[(int)*first].p);
                   2591:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   2592:                r->xtab[(int)*first].sz = ssz;
1.164     kristaps 2593:        }
                   2594:
                   2595:        return(ROFF_IGN);
                   2596: }
                   2597:
                   2598: static enum rofferr
1.105     kristaps 2599: roff_so(ROFF_ARGS)
                   2600: {
1.249     schwarze 2601:        char *name, *cp;
1.105     kristaps 2602:
1.238     schwarze 2603:        name = buf->buf + pos;
1.224     schwarze 2604:        mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105     kristaps 2605:
                   2606:        /*
                   2607:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   2608:         * opening anything that's not in our cwd or anything beneath
                   2609:         * it.  Thus, explicitly disallow traversing up the file-system
                   2610:         * or using absolute paths.
                   2611:         */
                   2612:
1.238     schwarze 2613:        if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210     schwarze 2614:                mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
                   2615:                    ".so %s", name);
1.249     schwarze 2616:                buf->sz = mandoc_asprintf(&cp,
                   2617:                    ".sp\nSee the file %s.\n.sp", name) + 1;
                   2618:                free(buf->buf);
                   2619:                buf->buf = cp;
                   2620:                *offs = 0;
                   2621:                return(ROFF_REPARSE);
1.105     kristaps 2622:        }
                   2623:
                   2624:        *offs = pos;
                   2625:        return(ROFF_SO);
                   2626: }
1.92      schwarze 2627:
1.106     kristaps 2628: static enum rofferr
                   2629: roff_userdef(ROFF_ARGS)
1.99      kristaps 2630: {
1.106     kristaps 2631:        const char       *arg[9];
                   2632:        char             *cp, *n1, *n2;
1.119     schwarze 2633:        int               i;
1.106     kristaps 2634:
                   2635:        /*
                   2636:         * Collect pointers to macro argument strings
1.188     schwarze 2637:         * and NUL-terminate them.
1.106     kristaps 2638:         */
1.238     schwarze 2639:        cp = buf->buf + pos;
1.119     schwarze 2640:        for (i = 0; i < 9; i++)
1.238     schwarze 2641:                arg[i] = *cp == '\0' ? "" :
1.136     kristaps 2642:                    mandoc_getarg(r->parse, &cp, ln, &pos);
1.99      kristaps 2643:
1.106     kristaps 2644:        /*
                   2645:         * Expand macro arguments.
1.99      kristaps 2646:         */
1.238     schwarze 2647:        buf->sz = 0;
1.106     kristaps 2648:        n1 = cp = mandoc_strdup(r->current_string);
1.238     schwarze 2649:        while ((cp = strstr(cp, "\\$")) != NULL) {
1.106     kristaps 2650:                i = cp[2] - '1';
                   2651:                if (0 > i || 8 < i) {
                   2652:                        /* Not an argument invocation. */
                   2653:                        cp += 2;
                   2654:                        continue;
                   2655:                }
1.209     schwarze 2656:                *cp = '\0';
1.238     schwarze 2657:                buf->sz = mandoc_asprintf(&n2, "%s%s%s",
1.209     schwarze 2658:                    n1, arg[i], cp + 3) + 1;
1.106     kristaps 2659:                cp = n2 + (cp - n1);
                   2660:                free(n1);
                   2661:                n1 = n2;
1.99      kristaps 2662:        }
                   2663:
1.106     kristaps 2664:        /*
                   2665:         * Replace the macro invocation
                   2666:         * by the expanded macro.
                   2667:         */
1.238     schwarze 2668:        free(buf->buf);
                   2669:        buf->buf = n1;
                   2670:        if (buf->sz == 0)
                   2671:                buf->sz = strlen(buf->buf) + 1;
1.248     schwarze 2672:        *offs = 0;
1.106     kristaps 2673:
1.238     schwarze 2674:        return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
1.106     kristaps 2675:           ROFF_REPARSE : ROFF_APPEND);
1.99      kristaps 2676: }
1.121     schwarze 2677:
1.212     schwarze 2678: static size_t
1.121     schwarze 2679: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   2680: {
                   2681:        char     *name, *cp;
1.212     schwarze 2682:        size_t    namesz;
1.121     schwarze 2683:
                   2684:        name = *cpp;
                   2685:        if ('\0' == *name)
1.212     schwarze 2686:                return(0);
1.121     schwarze 2687:
1.212     schwarze 2688:        /* Read until end of name and terminate it with NUL. */
                   2689:        for (cp = name; 1; cp++) {
                   2690:                if ('\0' == *cp || ' ' == *cp) {
                   2691:                        namesz = cp - name;
                   2692:                        break;
                   2693:                }
1.121     schwarze 2694:                if ('\\' != *cp)
                   2695:                        continue;
1.215     schwarze 2696:                namesz = cp - name;
                   2697:                if ('{' == cp[1] || '}' == cp[1])
                   2698:                        break;
1.121     schwarze 2699:                cp++;
                   2700:                if ('\\' == *cp)
                   2701:                        continue;
1.224     schwarze 2702:                mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
                   2703:                    "%.*s", (int)(cp - name + 1), name);
1.212     schwarze 2704:                mandoc_escape((const char **)&cp, NULL, NULL);
                   2705:                break;
1.121     schwarze 2706:        }
                   2707:
                   2708:        /* Read past spaces. */
                   2709:        while (' ' == *cp)
                   2710:                cp++;
                   2711:
                   2712:        *cpp = cp;
1.212     schwarze 2713:        return(namesz);
1.121     schwarze 2714: }
                   2715:
1.106     kristaps 2716: /*
                   2717:  * Store *string into the user-defined string called *name.
                   2718:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193     schwarze 2719:  * append == 0: replace mode
                   2720:  * append == 1: single-line append mode
                   2721:  * append == 2: multiline append mode, append '\n' after each call
1.106     kristaps 2722:  */
1.94      kristaps 2723: static void
1.106     kristaps 2724: roff_setstr(struct roff *r, const char *name, const char *string,
1.193     schwarze 2725:        int append)
1.92      schwarze 2726: {
1.164     kristaps 2727:
                   2728:        roff_setstrn(&r->strtab, name, strlen(name), string,
1.207     schwarze 2729:            string ? strlen(string) : 0, append);
1.164     kristaps 2730: }
                   2731:
                   2732: static void
1.166     kristaps 2733: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193     schwarze 2734:                const char *string, size_t stringsz, int append)
1.164     kristaps 2735: {
1.166     kristaps 2736:        struct roffkv   *n;
1.164     kristaps 2737:        char            *c;
                   2738:        int              i;
                   2739:        size_t           oldch, newch;
1.92      schwarze 2740:
1.106     kristaps 2741:        /* Search for an existing string with the same name. */
1.164     kristaps 2742:        n = *r;
                   2743:
1.211     schwarze 2744:        while (n && (namesz != n->key.sz ||
                   2745:                        strncmp(n->key.p, name, namesz)))
1.92      schwarze 2746:                n = n->next;
1.94      kristaps 2747:
                   2748:        if (NULL == n) {
1.106     kristaps 2749:                /* Create a new string table entry. */
1.166     kristaps 2750:                n = mandoc_malloc(sizeof(struct roffkv));
                   2751:                n->key.p = mandoc_strndup(name, namesz);
                   2752:                n->key.sz = namesz;
                   2753:                n->val.p = NULL;
                   2754:                n->val.sz = 0;
1.164     kristaps 2755:                n->next = *r;
                   2756:                *r = n;
1.193     schwarze 2757:        } else if (0 == append) {
1.166     kristaps 2758:                free(n->val.p);
                   2759:                n->val.p = NULL;
                   2760:                n->val.sz = 0;
1.106     kristaps 2761:        }
                   2762:
                   2763:        if (NULL == string)
                   2764:                return;
                   2765:
                   2766:        /*
                   2767:         * One additional byte for the '\n' in multiline mode,
                   2768:         * and one for the terminating '\0'.
                   2769:         */
1.193     schwarze 2770:        newch = stringsz + (1 < append ? 2u : 1u);
1.164     kristaps 2771:
1.166     kristaps 2772:        if (NULL == n->val.p) {
                   2773:                n->val.p = mandoc_malloc(newch);
                   2774:                *n->val.p = '\0';
1.106     kristaps 2775:                oldch = 0;
                   2776:        } else {
1.166     kristaps 2777:                oldch = n->val.sz;
                   2778:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 2779:        }
                   2780:
                   2781:        /* Skip existing content in the destination buffer. */
1.166     kristaps 2782:        c = n->val.p + (int)oldch;
1.106     kristaps 2783:
                   2784:        /* Append new content to the destination buffer. */
1.164     kristaps 2785:        i = 0;
                   2786:        while (i < (int)stringsz) {
1.106     kristaps 2787:                /*
                   2788:                 * Rudimentary roff copy mode:
                   2789:                 * Handle escaped backslashes.
                   2790:                 */
1.164     kristaps 2791:                if ('\\' == string[i] && '\\' == string[i + 1])
                   2792:                        i++;
                   2793:                *c++ = string[i++];
1.106     kristaps 2794:        }
1.94      kristaps 2795:
1.106     kristaps 2796:        /* Append terminating bytes. */
1.193     schwarze 2797:        if (1 < append)
1.106     kristaps 2798:                *c++ = '\n';
1.163     kristaps 2799:
1.106     kristaps 2800:        *c = '\0';
1.166     kristaps 2801:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 2802: }
                   2803:
1.94      kristaps 2804: static const char *
                   2805: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 2806: {
1.166     kristaps 2807:        const struct roffkv *n;
1.191     schwarze 2808:        int i;
1.92      schwarze 2809:
1.164     kristaps 2810:        for (n = r->strtab; n; n = n->next)
1.207     schwarze 2811:                if (0 == strncmp(name, n->key.p, len) &&
                   2812:                    '\0' == n->key.p[(int)len])
1.166     kristaps 2813:                        return(n->val.p);
1.191     schwarze 2814:
                   2815:        for (i = 0; i < PREDEFS_MAX; i++)
                   2816:                if (0 == strncmp(name, predefs[i].name, len) &&
                   2817:                                '\0' == predefs[i].name[(int)len])
                   2818:                        return(predefs[i].str);
1.94      kristaps 2819:
1.157     kristaps 2820:        return(NULL);
1.92      schwarze 2821: }
                   2822:
1.94      kristaps 2823: static void
1.167     kristaps 2824: roff_freestr(struct roffkv *r)
1.92      schwarze 2825: {
1.166     kristaps 2826:        struct roffkv    *n, *nn;
1.92      schwarze 2827:
1.167     kristaps 2828:        for (n = r; n; n = nn) {
1.166     kristaps 2829:                free(n->key.p);
                   2830:                free(n->val.p);
1.92      schwarze 2831:                nn = n->next;
                   2832:                free(n);
                   2833:        }
1.114     kristaps 2834: }
                   2835:
                   2836: const struct tbl_span *
                   2837: roff_span(const struct roff *r)
                   2838: {
1.207     schwarze 2839:
1.114     kristaps 2840:        return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125     kristaps 2841: }
                   2842:
                   2843: const struct eqn *
                   2844: roff_eqn(const struct roff *r)
                   2845: {
1.207     schwarze 2846:
1.125     kristaps 2847:        return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164     kristaps 2848: }
                   2849:
                   2850: /*
                   2851:  * Duplicate an input string, making the appropriate character
                   2852:  * conversations (as stipulated by `tr') along the way.
                   2853:  * Returns a heap-allocated string with all the replacements made.
                   2854:  */
                   2855: char *
                   2856: roff_strdup(const struct roff *r, const char *p)
                   2857: {
1.166     kristaps 2858:        const struct roffkv *cp;
1.164     kristaps 2859:        char            *res;
                   2860:        const char      *pp;
                   2861:        size_t           ssz, sz;
                   2862:        enum mandoc_esc  esc;
                   2863:
1.167     kristaps 2864:        if (NULL == r->xmbtab && NULL == r->xtab)
1.164     kristaps 2865:                return(mandoc_strdup(p));
                   2866:        else if ('\0' == *p)
                   2867:                return(mandoc_strdup(""));
                   2868:
                   2869:        /*
                   2870:         * Step through each character looking for term matches
                   2871:         * (remember that a `tr' can be invoked with an escape, which is
                   2872:         * a glyph but the escape is multi-character).
                   2873:         * We only do this if the character hash has been initialised
                   2874:         * and the string is >0 length.
                   2875:         */
                   2876:
                   2877:        res = NULL;
                   2878:        ssz = 0;
                   2879:
                   2880:        while ('\0' != *p) {
1.167     kristaps 2881:                if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
                   2882:                        sz = r->xtab[(int)*p].sz;
                   2883:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2884:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   2885:                        ssz += sz;
                   2886:                        p++;
                   2887:                        continue;
                   2888:                } else if ('\\' != *p) {
                   2889:                        res = mandoc_realloc(res, ssz + 2);
                   2890:                        res[ssz++] = *p++;
                   2891:                        continue;
                   2892:                }
                   2893:
1.164     kristaps 2894:                /* Search for term matches. */
1.167     kristaps 2895:                for (cp = r->xmbtab; cp; cp = cp->next)
1.166     kristaps 2896:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 2897:                                break;
                   2898:
                   2899:                if (NULL != cp) {
                   2900:                        /*
                   2901:                         * A match has been found.
                   2902:                         * Append the match to the array and move
                   2903:                         * forward by its keysize.
                   2904:                         */
1.207     schwarze 2905:                        res = mandoc_realloc(res,
                   2906:                            ssz + cp->val.sz + 1);
1.166     kristaps 2907:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   2908:                        ssz += cp->val.sz;
                   2909:                        p += (int)cp->key.sz;
1.164     kristaps 2910:                        continue;
                   2911:                }
                   2912:
1.167     kristaps 2913:                /*
                   2914:                 * Handle escapes carefully: we need to copy
                   2915:                 * over just the escape itself, or else we might
                   2916:                 * do replacements within the escape itself.
                   2917:                 * Make sure to pass along the bogus string.
                   2918:                 */
                   2919:                pp = p++;
                   2920:                esc = mandoc_escape(&p, NULL, NULL);
                   2921:                if (ESCAPE_ERROR == esc) {
                   2922:                        sz = strlen(pp);
1.164     kristaps 2923:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2924:                        memcpy(res + ssz, pp, sz);
1.167     kristaps 2925:                        break;
1.164     kristaps 2926:                }
1.207     schwarze 2927:                /*
                   2928:                 * We bail out on bad escapes.
1.167     kristaps 2929:                 * No need to warn: we already did so when
                   2930:                 * roff_res() was called.
                   2931:                 */
                   2932:                sz = (int)(p - pp);
                   2933:                res = mandoc_realloc(res, ssz + sz + 1);
                   2934:                memcpy(res + ssz, pp, sz);
                   2935:                ssz += sz;
1.164     kristaps 2936:        }
                   2937:
                   2938:        res[(int)ssz] = '\0';
                   2939:        return(res);
1.227     schwarze 2940: }
                   2941:
                   2942: int
                   2943: roff_getformat(const struct roff *r)
                   2944: {
                   2945:
                   2946:        return(r->format);
1.174     kristaps 2947: }
                   2948:
                   2949: /*
1.207     schwarze 2950:  * Find out whether a line is a macro line or not.
1.174     kristaps 2951:  * If it is, adjust the current position and return one; if it isn't,
                   2952:  * return zero and don't change the current position.
                   2953:  * If the control character has been set with `.cc', then let that grain
                   2954:  * precedence.
                   2955:  * This is slighly contrary to groff, where using the non-breaking
                   2956:  * control character when `cc' has been invoked will cause the
                   2957:  * non-breaking macro contents to be printed verbatim.
                   2958:  */
                   2959: int
                   2960: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   2961: {
                   2962:        int             pos;
                   2963:
                   2964:        pos = *ppos;
                   2965:
                   2966:        if (0 != r->control && cp[pos] == r->control)
                   2967:                pos++;
                   2968:        else if (0 != r->control)
                   2969:                return(0);
                   2970:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   2971:                pos += 2;
                   2972:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   2973:                pos++;
                   2974:        else
                   2975:                return(0);
                   2976:
                   2977:        while (' ' == cp[pos] || '\t' == cp[pos])
                   2978:                pos++;
                   2979:
                   2980:        *ppos = pos;
                   2981:        return(1);
1.74      kristaps 2982: }

CVSweb