1*0a6a1f1dSLionel Sambuc /* Id: roff.c,v 1.189 2013/12/30 18:44:06 schwarze Exp */
2d65f6f70SBen Gras /*
3*0a6a1f1dSLionel Sambuc * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
5d65f6f70SBen Gras *
6d65f6f70SBen Gras * Permission to use, copy, modify, and distribute this software for any
7d65f6f70SBen Gras * purpose with or without fee is hereby granted, provided that the above
8d65f6f70SBen Gras * copyright notice and this permission notice appear in all copies.
9d65f6f70SBen Gras *
10d65f6f70SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11d65f6f70SBen Gras * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d65f6f70SBen Gras * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13d65f6f70SBen Gras * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d65f6f70SBen Gras * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d65f6f70SBen Gras * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d65f6f70SBen Gras * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d65f6f70SBen Gras */
18d65f6f70SBen Gras #ifdef HAVE_CONFIG_H
19d65f6f70SBen Gras #include "config.h"
20d65f6f70SBen Gras #endif
21d65f6f70SBen Gras
22d65f6f70SBen Gras #include <assert.h>
23d65f6f70SBen Gras #include <ctype.h>
2484d9c625SLionel Sambuc #include <stdio.h>
25d65f6f70SBen Gras #include <stdlib.h>
26d65f6f70SBen Gras #include <string.h>
27d65f6f70SBen Gras
28d65f6f70SBen Gras #include "mandoc.h"
29d65f6f70SBen Gras #include "libroff.h"
30d65f6f70SBen Gras #include "libmandoc.h"
31d65f6f70SBen Gras
3292395e9cSLionel Sambuc /* Maximum number of nested if-else conditionals. */
33d65f6f70SBen Gras #define RSTACK_MAX 128
34d65f6f70SBen Gras
3592395e9cSLionel Sambuc /* Maximum number of string expansions per line, to break infinite loops. */
3692395e9cSLionel Sambuc #define EXPAND_LIMIT 1000
37d65f6f70SBen Gras
38d65f6f70SBen Gras enum rofft {
39d65f6f70SBen Gras ROFF_ad,
40d65f6f70SBen Gras ROFF_am,
41d65f6f70SBen Gras ROFF_ami,
42d65f6f70SBen Gras ROFF_am1,
43*0a6a1f1dSLionel Sambuc ROFF_cc,
44d65f6f70SBen Gras ROFF_de,
45d65f6f70SBen Gras ROFF_dei,
46d65f6f70SBen Gras ROFF_de1,
47d65f6f70SBen Gras ROFF_ds,
48d65f6f70SBen Gras ROFF_el,
49*0a6a1f1dSLionel Sambuc ROFF_fam,
50*0a6a1f1dSLionel Sambuc ROFF_hw,
51d65f6f70SBen Gras ROFF_hy,
52d65f6f70SBen Gras ROFF_ie,
53d65f6f70SBen Gras ROFF_if,
54d65f6f70SBen Gras ROFF_ig,
5592395e9cSLionel Sambuc ROFF_it,
56d65f6f70SBen Gras ROFF_ne,
57d65f6f70SBen Gras ROFF_nh,
58d65f6f70SBen Gras ROFF_nr,
5992395e9cSLionel Sambuc ROFF_ns,
6092395e9cSLionel Sambuc ROFF_ps,
61d65f6f70SBen Gras ROFF_rm,
62d65f6f70SBen Gras ROFF_so,
6392395e9cSLionel Sambuc ROFF_ta,
64d65f6f70SBen Gras ROFF_tr,
65*0a6a1f1dSLionel Sambuc ROFF_Dd,
66*0a6a1f1dSLionel Sambuc ROFF_TH,
67d65f6f70SBen Gras ROFF_TS,
68d65f6f70SBen Gras ROFF_TE,
69d65f6f70SBen Gras ROFF_T_,
7092395e9cSLionel Sambuc ROFF_EQ,
7192395e9cSLionel Sambuc ROFF_EN,
72d65f6f70SBen Gras ROFF_cblock,
7392395e9cSLionel Sambuc ROFF_ccond,
74d65f6f70SBen Gras ROFF_USERDEF,
75d65f6f70SBen Gras ROFF_MAX
76d65f6f70SBen Gras };
77d65f6f70SBen Gras
78d65f6f70SBen Gras enum roffrule {
7984d9c625SLionel Sambuc ROFFRULE_DENY,
8084d9c625SLionel Sambuc ROFFRULE_ALLOW
81d65f6f70SBen Gras };
82d65f6f70SBen Gras
8392395e9cSLionel Sambuc /*
8492395e9cSLionel Sambuc * An incredibly-simple string buffer.
8592395e9cSLionel Sambuc */
86d65f6f70SBen Gras struct roffstr {
8792395e9cSLionel Sambuc char *p; /* nil-terminated buffer */
8892395e9cSLionel Sambuc size_t sz; /* saved strlen(p) */
8992395e9cSLionel Sambuc };
9092395e9cSLionel Sambuc
9192395e9cSLionel Sambuc /*
9292395e9cSLionel Sambuc * A key-value roffstr pair as part of a singly-linked list.
9392395e9cSLionel Sambuc */
9492395e9cSLionel Sambuc struct roffkv {
9592395e9cSLionel Sambuc struct roffstr key;
9692395e9cSLionel Sambuc struct roffstr val;
9792395e9cSLionel Sambuc struct roffkv *next; /* next in list */
98d65f6f70SBen Gras };
99d65f6f70SBen Gras
100*0a6a1f1dSLionel Sambuc /*
101*0a6a1f1dSLionel Sambuc * A single number register as part of a singly-linked list.
102*0a6a1f1dSLionel Sambuc */
103*0a6a1f1dSLionel Sambuc struct roffreg {
104*0a6a1f1dSLionel Sambuc struct roffstr key;
105*0a6a1f1dSLionel Sambuc int val;
106*0a6a1f1dSLionel Sambuc struct roffreg *next;
107*0a6a1f1dSLionel Sambuc };
108*0a6a1f1dSLionel Sambuc
109d65f6f70SBen Gras struct roff {
110*0a6a1f1dSLionel Sambuc enum mparset parsetype; /* requested parse type */
11192395e9cSLionel Sambuc struct mparse *parse; /* parse point */
112d65f6f70SBen Gras struct roffnode *last; /* leaf of stack */
113d65f6f70SBen Gras enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
114*0a6a1f1dSLionel Sambuc char control; /* control character */
115d65f6f70SBen Gras int rstackpos; /* position in rstack */
116*0a6a1f1dSLionel Sambuc struct roffreg *regtab; /* number registers */
11792395e9cSLionel Sambuc struct roffkv *strtab; /* user-defined strings & macros */
11892395e9cSLionel Sambuc struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
11992395e9cSLionel Sambuc struct roffstr *xtab; /* single-byte trans table (`tr') */
120d65f6f70SBen Gras const char *current_string; /* value of last called user macro */
121d65f6f70SBen Gras struct tbl_node *first_tbl; /* first table parsed */
122d65f6f70SBen Gras struct tbl_node *last_tbl; /* last table parsed */
123d65f6f70SBen Gras struct tbl_node *tbl; /* current table being parsed */
12492395e9cSLionel Sambuc struct eqn_node *last_eqn; /* last equation parsed */
12592395e9cSLionel Sambuc struct eqn_node *first_eqn; /* first equation parsed */
12692395e9cSLionel Sambuc struct eqn_node *eqn; /* current equation being parsed */
127d65f6f70SBen Gras };
128d65f6f70SBen Gras
129d65f6f70SBen Gras struct roffnode {
130d65f6f70SBen Gras enum rofft tok; /* type of node */
131d65f6f70SBen Gras struct roffnode *parent; /* up one in stack */
132d65f6f70SBen Gras int line; /* parse line */
133d65f6f70SBen Gras int col; /* parse col */
134d65f6f70SBen Gras char *name; /* node name, e.g. macro name */
135d65f6f70SBen Gras char *end; /* end-rules: custom token */
136d65f6f70SBen Gras int endspan; /* end-rules: next-line or infty */
137d65f6f70SBen Gras enum roffrule rule; /* current evaluation rule */
138d65f6f70SBen Gras };
139d65f6f70SBen Gras
140d65f6f70SBen Gras #define ROFF_ARGS struct roff *r, /* parse ctx */ \
141d65f6f70SBen Gras enum rofft tok, /* tok of macro */ \
142d65f6f70SBen Gras char **bufp, /* input buffer */ \
143d65f6f70SBen Gras size_t *szp, /* size of input buffer */ \
144d65f6f70SBen Gras int ln, /* parse line */ \
145d65f6f70SBen Gras int ppos, /* original pos in buffer */ \
146d65f6f70SBen Gras int pos, /* current pos in buffer */ \
147d65f6f70SBen Gras int *offs /* reset offset of buffer data */
148d65f6f70SBen Gras
149d65f6f70SBen Gras typedef enum rofferr (*roffproc)(ROFF_ARGS);
150d65f6f70SBen Gras
151d65f6f70SBen Gras struct roffmac {
152d65f6f70SBen Gras const char *name; /* macro name */
153d65f6f70SBen Gras roffproc proc; /* process new macro */
154d65f6f70SBen Gras roffproc text; /* process as child text of macro */
155d65f6f70SBen Gras roffproc sub; /* process as child of macro */
156d65f6f70SBen Gras int flags;
157d65f6f70SBen Gras #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
158d65f6f70SBen Gras struct roffmac *next;
159d65f6f70SBen Gras };
160d65f6f70SBen Gras
16192395e9cSLionel Sambuc struct predef {
16292395e9cSLionel Sambuc const char *name; /* predefined input name */
16392395e9cSLionel Sambuc const char *str; /* replacement symbol */
16492395e9cSLionel Sambuc };
16592395e9cSLionel Sambuc
16692395e9cSLionel Sambuc #define PREDEF(__name, __str) \
16792395e9cSLionel Sambuc { (__name), (__str) },
16892395e9cSLionel Sambuc
16992395e9cSLionel Sambuc static enum rofft roffhash_find(const char *, size_t);
17092395e9cSLionel Sambuc static void roffhash_init(void);
17192395e9cSLionel Sambuc static void roffnode_cleanscope(struct roff *);
17292395e9cSLionel Sambuc static void roffnode_pop(struct roff *);
17392395e9cSLionel Sambuc static void roffnode_push(struct roff *, enum rofft,
17492395e9cSLionel Sambuc const char *, int, int);
175d65f6f70SBen Gras static enum rofferr roff_block(ROFF_ARGS);
176d65f6f70SBen Gras static enum rofferr roff_block_text(ROFF_ARGS);
177d65f6f70SBen Gras static enum rofferr roff_block_sub(ROFF_ARGS);
178d65f6f70SBen Gras static enum rofferr roff_cblock(ROFF_ARGS);
179*0a6a1f1dSLionel Sambuc static enum rofferr roff_cc(ROFF_ARGS);
180d65f6f70SBen Gras static enum rofferr roff_ccond(ROFF_ARGS);
181d65f6f70SBen Gras static enum rofferr roff_cond(ROFF_ARGS);
182d65f6f70SBen Gras static enum rofferr roff_cond_text(ROFF_ARGS);
183d65f6f70SBen Gras static enum rofferr roff_cond_sub(ROFF_ARGS);
184d65f6f70SBen Gras static enum rofferr roff_ds(ROFF_ARGS);
185d65f6f70SBen Gras static enum roffrule roff_evalcond(const char *, int *);
18692395e9cSLionel Sambuc static void roff_free1(struct roff *);
187*0a6a1f1dSLionel Sambuc static void roff_freereg(struct roffreg *);
18892395e9cSLionel Sambuc static void roff_freestr(struct roffkv *);
18992395e9cSLionel Sambuc static char *roff_getname(struct roff *, char **, int, int);
190*0a6a1f1dSLionel Sambuc static int roff_getnum(const char *, int *, int *);
191*0a6a1f1dSLionel Sambuc static int roff_getop(const char *, int *, char *);
192*0a6a1f1dSLionel Sambuc static int roff_getregn(const struct roff *,
193*0a6a1f1dSLionel Sambuc const char *, size_t);
194d65f6f70SBen Gras static const char *roff_getstrn(const struct roff *,
195d65f6f70SBen Gras const char *, size_t);
196*0a6a1f1dSLionel Sambuc static enum rofferr roff_it(ROFF_ARGS);
197d65f6f70SBen Gras static enum rofferr roff_line_ignore(ROFF_ARGS);
198d65f6f70SBen Gras static enum rofferr roff_nr(ROFF_ARGS);
19992395e9cSLionel Sambuc static void roff_openeqn(struct roff *, const char *,
20092395e9cSLionel Sambuc int, int, const char *);
20192395e9cSLionel Sambuc static enum rofft roff_parse(struct roff *, const char *, int *);
202*0a6a1f1dSLionel Sambuc static enum rofferr roff_parsetext(char **, size_t *, int, int *);
20392395e9cSLionel Sambuc static enum rofferr roff_res(struct roff *,
20492395e9cSLionel Sambuc char **, size_t *, int, int);
20592395e9cSLionel Sambuc static enum rofferr roff_rm(ROFF_ARGS);
206d65f6f70SBen Gras static void roff_setstr(struct roff *,
207d65f6f70SBen Gras const char *, const char *, int);
20892395e9cSLionel Sambuc static void roff_setstrn(struct roffkv **, const char *,
20992395e9cSLionel Sambuc size_t, const char *, size_t, int);
210d65f6f70SBen Gras static enum rofferr roff_so(ROFF_ARGS);
21192395e9cSLionel Sambuc static enum rofferr roff_tr(ROFF_ARGS);
212*0a6a1f1dSLionel Sambuc static enum rofferr roff_Dd(ROFF_ARGS);
213*0a6a1f1dSLionel Sambuc static enum rofferr roff_TH(ROFF_ARGS);
214d65f6f70SBen Gras static enum rofferr roff_TE(ROFF_ARGS);
215d65f6f70SBen Gras static enum rofferr roff_TS(ROFF_ARGS);
21692395e9cSLionel Sambuc static enum rofferr roff_EQ(ROFF_ARGS);
21792395e9cSLionel Sambuc static enum rofferr roff_EN(ROFF_ARGS);
218d65f6f70SBen Gras static enum rofferr roff_T_(ROFF_ARGS);
219d65f6f70SBen Gras static enum rofferr roff_userdef(ROFF_ARGS);
220d65f6f70SBen Gras
22192395e9cSLionel Sambuc /* See roffhash_find() */
222d65f6f70SBen Gras
223d65f6f70SBen Gras #define ASCII_HI 126
224d65f6f70SBen Gras #define ASCII_LO 33
225d65f6f70SBen Gras #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
226d65f6f70SBen Gras
227d65f6f70SBen Gras static struct roffmac *hash[HASHWIDTH];
228d65f6f70SBen Gras
229d65f6f70SBen Gras static struct roffmac roffs[ROFF_MAX] = {
230d65f6f70SBen Gras { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
231d65f6f70SBen Gras { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
232d65f6f70SBen Gras { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
233d65f6f70SBen Gras { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
234*0a6a1f1dSLionel Sambuc { "cc", roff_cc, NULL, NULL, 0, NULL },
235d65f6f70SBen Gras { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
236d65f6f70SBen Gras { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
237d65f6f70SBen Gras { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
238d65f6f70SBen Gras { "ds", roff_ds, NULL, NULL, 0, NULL },
239d65f6f70SBen Gras { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
240*0a6a1f1dSLionel Sambuc { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
241*0a6a1f1dSLionel Sambuc { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
242d65f6f70SBen Gras { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
243d65f6f70SBen Gras { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
244d65f6f70SBen Gras { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
245d65f6f70SBen Gras { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
246*0a6a1f1dSLionel Sambuc { "it", roff_it, NULL, NULL, 0, NULL },
247d65f6f70SBen Gras { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
248d65f6f70SBen Gras { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
249d65f6f70SBen Gras { "nr", roff_nr, NULL, NULL, 0, NULL },
25092395e9cSLionel Sambuc { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
25192395e9cSLionel Sambuc { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
25292395e9cSLionel Sambuc { "rm", roff_rm, NULL, NULL, 0, NULL },
253d65f6f70SBen Gras { "so", roff_so, NULL, NULL, 0, NULL },
25492395e9cSLionel Sambuc { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
25592395e9cSLionel Sambuc { "tr", roff_tr, NULL, NULL, 0, NULL },
256*0a6a1f1dSLionel Sambuc { "Dd", roff_Dd, NULL, NULL, 0, NULL },
257*0a6a1f1dSLionel Sambuc { "TH", roff_TH, NULL, NULL, 0, NULL },
258d65f6f70SBen Gras { "TS", roff_TS, NULL, NULL, 0, NULL },
259d65f6f70SBen Gras { "TE", roff_TE, NULL, NULL, 0, NULL },
260d65f6f70SBen Gras { "T&", roff_T_, NULL, NULL, 0, NULL },
26192395e9cSLionel Sambuc { "EQ", roff_EQ, NULL, NULL, 0, NULL },
26292395e9cSLionel Sambuc { "EN", roff_EN, NULL, NULL, 0, NULL },
263d65f6f70SBen Gras { ".", roff_cblock, NULL, NULL, 0, NULL },
264d65f6f70SBen Gras { "\\}", roff_ccond, NULL, NULL, 0, NULL },
265d65f6f70SBen Gras { NULL, roff_userdef, NULL, NULL, 0, NULL },
266d65f6f70SBen Gras };
267d65f6f70SBen Gras
268*0a6a1f1dSLionel Sambuc const char *const __mdoc_reserved[] = {
269*0a6a1f1dSLionel Sambuc "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
270*0a6a1f1dSLionel Sambuc "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
271*0a6a1f1dSLionel Sambuc "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
272*0a6a1f1dSLionel Sambuc "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
273*0a6a1f1dSLionel Sambuc "Ds", "Dt", "Dv", "Dx", "D1",
274*0a6a1f1dSLionel Sambuc "Ec", "Ed", "Ef", "Ek", "El", "Em", "em",
275*0a6a1f1dSLionel Sambuc "En", "Eo", "Eq", "Er", "Es", "Ev", "Ex",
276*0a6a1f1dSLionel Sambuc "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
277*0a6a1f1dSLionel Sambuc "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp", "LP",
278*0a6a1f1dSLionel Sambuc "Me", "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
279*0a6a1f1dSLionel Sambuc "Oc", "Oo", "Op", "Os", "Ot", "Ox",
280*0a6a1f1dSLionel Sambuc "Pa", "Pc", "Pf", "Po", "Pp", "PP", "pp", "Pq",
281*0a6a1f1dSLionel Sambuc "Qc", "Ql", "Qo", "Qq", "Or", "Rd", "Re", "Rs", "Rv",
282*0a6a1f1dSLionel Sambuc "Sc", "Sf", "Sh", "SH", "Sm", "So", "Sq",
283*0a6a1f1dSLionel Sambuc "Ss", "St", "Sx", "Sy",
284*0a6a1f1dSLionel Sambuc "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
285*0a6a1f1dSLionel Sambuc "%A", "%B", "%D", "%I", "%J", "%N", "%O",
286*0a6a1f1dSLionel Sambuc "%P", "%Q", "%R", "%T", "%U", "%V",
287*0a6a1f1dSLionel Sambuc NULL
288*0a6a1f1dSLionel Sambuc };
289*0a6a1f1dSLionel Sambuc
290*0a6a1f1dSLionel Sambuc const char *const __man_reserved[] = {
291*0a6a1f1dSLionel Sambuc "AT", "B", "BI", "BR", "BT", "DE", "DS", "DT",
292*0a6a1f1dSLionel Sambuc "EE", "EN", "EQ", "EX", "HF", "HP", "I", "IB", "IP", "IR",
293*0a6a1f1dSLionel Sambuc "LP", "ME", "MT", "OP", "P", "PD", "PP", "PT",
294*0a6a1f1dSLionel Sambuc "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS", "SY",
295*0a6a1f1dSLionel Sambuc "TE", "TH", "TP", "TQ", "TS", "T&", "UC", "UE", "UR", "YS",
296*0a6a1f1dSLionel Sambuc NULL
297*0a6a1f1dSLionel Sambuc };
298*0a6a1f1dSLionel Sambuc
29992395e9cSLionel Sambuc /* Array of injected predefined strings. */
30092395e9cSLionel Sambuc #define PREDEFS_MAX 38
30192395e9cSLionel Sambuc static const struct predef predefs[PREDEFS_MAX] = {
30292395e9cSLionel Sambuc #include "predefs.in"
30392395e9cSLionel Sambuc };
304d65f6f70SBen Gras
30592395e9cSLionel Sambuc /* See roffhash_find() */
306d65f6f70SBen Gras #define ROFF_HASH(p) (p[0] - ASCII_LO)
307d65f6f70SBen Gras
308*0a6a1f1dSLionel Sambuc static int roffit_lines; /* number of lines to delay */
309*0a6a1f1dSLionel Sambuc static char *roffit_macro; /* nil-terminated macro line */
310*0a6a1f1dSLionel Sambuc
311d65f6f70SBen Gras static void
roffhash_init(void)31292395e9cSLionel Sambuc roffhash_init(void)
313d65f6f70SBen Gras {
314d65f6f70SBen Gras struct roffmac *n;
315d65f6f70SBen Gras int buc, i;
316d65f6f70SBen Gras
317d65f6f70SBen Gras for (i = 0; i < (int)ROFF_USERDEF; i++) {
318d65f6f70SBen Gras assert(roffs[i].name[0] >= ASCII_LO);
319d65f6f70SBen Gras assert(roffs[i].name[0] <= ASCII_HI);
320d65f6f70SBen Gras
321d65f6f70SBen Gras buc = ROFF_HASH(roffs[i].name);
322d65f6f70SBen Gras
323d65f6f70SBen Gras if (NULL != (n = hash[buc])) {
324d65f6f70SBen Gras for ( ; n->next; n = n->next)
325d65f6f70SBen Gras /* Do nothing. */ ;
326d65f6f70SBen Gras n->next = &roffs[i];
327d65f6f70SBen Gras } else
328d65f6f70SBen Gras hash[buc] = &roffs[i];
329d65f6f70SBen Gras }
330d65f6f70SBen Gras }
331d65f6f70SBen Gras
332d65f6f70SBen Gras /*
333d65f6f70SBen Gras * Look up a roff token by its name. Returns ROFF_MAX if no macro by
334d65f6f70SBen Gras * the nil-terminated string name could be found.
335d65f6f70SBen Gras */
336d65f6f70SBen Gras static enum rofft
roffhash_find(const char * p,size_t s)33792395e9cSLionel Sambuc roffhash_find(const char *p, size_t s)
338d65f6f70SBen Gras {
339d65f6f70SBen Gras int buc;
340d65f6f70SBen Gras struct roffmac *n;
341d65f6f70SBen Gras
342d65f6f70SBen Gras /*
343d65f6f70SBen Gras * libroff has an extremely simple hashtable, for the time
344d65f6f70SBen Gras * being, which simply keys on the first character, which must
345d65f6f70SBen Gras * be printable, then walks a chain. It works well enough until
346d65f6f70SBen Gras * optimised.
347d65f6f70SBen Gras */
348d65f6f70SBen Gras
349d65f6f70SBen Gras if (p[0] < ASCII_LO || p[0] > ASCII_HI)
350d65f6f70SBen Gras return(ROFF_MAX);
351d65f6f70SBen Gras
352d65f6f70SBen Gras buc = ROFF_HASH(p);
353d65f6f70SBen Gras
354d65f6f70SBen Gras if (NULL == (n = hash[buc]))
355d65f6f70SBen Gras return(ROFF_MAX);
356d65f6f70SBen Gras for ( ; n; n = n->next)
357d65f6f70SBen Gras if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
358d65f6f70SBen Gras return((enum rofft)(n - roffs));
359d65f6f70SBen Gras
360d65f6f70SBen Gras return(ROFF_MAX);
361d65f6f70SBen Gras }
362d65f6f70SBen Gras
363d65f6f70SBen Gras
364d65f6f70SBen Gras /*
365d65f6f70SBen Gras * Pop the current node off of the stack of roff instructions currently
366d65f6f70SBen Gras * pending.
367d65f6f70SBen Gras */
368d65f6f70SBen Gras static void
roffnode_pop(struct roff * r)369d65f6f70SBen Gras roffnode_pop(struct roff *r)
370d65f6f70SBen Gras {
371d65f6f70SBen Gras struct roffnode *p;
372d65f6f70SBen Gras
373d65f6f70SBen Gras assert(r->last);
374d65f6f70SBen Gras p = r->last;
375d65f6f70SBen Gras
376d65f6f70SBen Gras r->last = r->last->parent;
377d65f6f70SBen Gras free(p->name);
378d65f6f70SBen Gras free(p->end);
379d65f6f70SBen Gras free(p);
380d65f6f70SBen Gras }
381d65f6f70SBen Gras
382d65f6f70SBen Gras
383d65f6f70SBen Gras /*
384d65f6f70SBen Gras * Push a roff node onto the instruction stack. This must later be
385d65f6f70SBen Gras * removed with roffnode_pop().
386d65f6f70SBen Gras */
387d65f6f70SBen Gras static void
roffnode_push(struct roff * r,enum rofft tok,const char * name,int line,int col)388d65f6f70SBen Gras roffnode_push(struct roff *r, enum rofft tok, const char *name,
389d65f6f70SBen Gras int line, int col)
390d65f6f70SBen Gras {
391d65f6f70SBen Gras struct roffnode *p;
392d65f6f70SBen Gras
393d65f6f70SBen Gras p = mandoc_calloc(1, sizeof(struct roffnode));
394d65f6f70SBen Gras p->tok = tok;
395d65f6f70SBen Gras if (name)
396d65f6f70SBen Gras p->name = mandoc_strdup(name);
397d65f6f70SBen Gras p->parent = r->last;
398d65f6f70SBen Gras p->line = line;
399d65f6f70SBen Gras p->col = col;
400d65f6f70SBen Gras p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
401d65f6f70SBen Gras
402d65f6f70SBen Gras r->last = p;
403d65f6f70SBen Gras }
404d65f6f70SBen Gras
405d65f6f70SBen Gras
406d65f6f70SBen Gras static void
roff_free1(struct roff * r)407d65f6f70SBen Gras roff_free1(struct roff *r)
408d65f6f70SBen Gras {
409*0a6a1f1dSLionel Sambuc struct tbl_node *tbl;
41092395e9cSLionel Sambuc struct eqn_node *e;
41192395e9cSLionel Sambuc int i;
412d65f6f70SBen Gras
413*0a6a1f1dSLionel Sambuc while (NULL != (tbl = r->first_tbl)) {
414*0a6a1f1dSLionel Sambuc r->first_tbl = tbl->next;
415*0a6a1f1dSLionel Sambuc tbl_free(tbl);
416d65f6f70SBen Gras }
417d65f6f70SBen Gras
418d65f6f70SBen Gras r->first_tbl = r->last_tbl = r->tbl = NULL;
419d65f6f70SBen Gras
42092395e9cSLionel Sambuc while (NULL != (e = r->first_eqn)) {
42192395e9cSLionel Sambuc r->first_eqn = e->next;
42292395e9cSLionel Sambuc eqn_free(e);
42392395e9cSLionel Sambuc }
42492395e9cSLionel Sambuc
42592395e9cSLionel Sambuc r->first_eqn = r->last_eqn = r->eqn = NULL;
42692395e9cSLionel Sambuc
427d65f6f70SBen Gras while (r->last)
428d65f6f70SBen Gras roffnode_pop(r);
429d65f6f70SBen Gras
43092395e9cSLionel Sambuc roff_freestr(r->strtab);
43192395e9cSLionel Sambuc roff_freestr(r->xmbtab);
432d65f6f70SBen Gras
43392395e9cSLionel Sambuc r->strtab = r->xmbtab = NULL;
43492395e9cSLionel Sambuc
435*0a6a1f1dSLionel Sambuc roff_freereg(r->regtab);
436*0a6a1f1dSLionel Sambuc
437*0a6a1f1dSLionel Sambuc r->regtab = NULL;
438*0a6a1f1dSLionel Sambuc
43992395e9cSLionel Sambuc if (r->xtab)
44092395e9cSLionel Sambuc for (i = 0; i < 128; i++)
44192395e9cSLionel Sambuc free(r->xtab[i].p);
44292395e9cSLionel Sambuc
44392395e9cSLionel Sambuc free(r->xtab);
44492395e9cSLionel Sambuc r->xtab = NULL;
44592395e9cSLionel Sambuc }
446d65f6f70SBen Gras
447d65f6f70SBen Gras void
roff_reset(struct roff * r)448d65f6f70SBen Gras roff_reset(struct roff *r)
449d65f6f70SBen Gras {
45092395e9cSLionel Sambuc int i;
451d65f6f70SBen Gras
452d65f6f70SBen Gras roff_free1(r);
45392395e9cSLionel Sambuc
454*0a6a1f1dSLionel Sambuc r->control = 0;
45592395e9cSLionel Sambuc
45692395e9cSLionel Sambuc for (i = 0; i < PREDEFS_MAX; i++)
45792395e9cSLionel Sambuc roff_setstr(r, predefs[i].name, predefs[i].str, 0);
458d65f6f70SBen Gras }
459d65f6f70SBen Gras
460d65f6f70SBen Gras
461d65f6f70SBen Gras void
roff_free(struct roff * r)462d65f6f70SBen Gras roff_free(struct roff *r)
463d65f6f70SBen Gras {
464d65f6f70SBen Gras
465d65f6f70SBen Gras roff_free1(r);
466d65f6f70SBen Gras free(r);
467d65f6f70SBen Gras }
468d65f6f70SBen Gras
469d65f6f70SBen Gras
470d65f6f70SBen Gras struct roff *
roff_alloc(enum mparset type,struct mparse * parse)471*0a6a1f1dSLionel Sambuc roff_alloc(enum mparset type, struct mparse *parse)
472d65f6f70SBen Gras {
473d65f6f70SBen Gras struct roff *r;
47492395e9cSLionel Sambuc int i;
475d65f6f70SBen Gras
476d65f6f70SBen Gras r = mandoc_calloc(1, sizeof(struct roff));
477*0a6a1f1dSLionel Sambuc r->parsetype = type;
47892395e9cSLionel Sambuc r->parse = parse;
479d65f6f70SBen Gras r->rstackpos = -1;
480d65f6f70SBen Gras
48192395e9cSLionel Sambuc roffhash_init();
48292395e9cSLionel Sambuc
48392395e9cSLionel Sambuc for (i = 0; i < PREDEFS_MAX; i++)
48492395e9cSLionel Sambuc roff_setstr(r, predefs[i].name, predefs[i].str, 0);
48592395e9cSLionel Sambuc
486d65f6f70SBen Gras return(r);
487d65f6f70SBen Gras }
488d65f6f70SBen Gras
489d65f6f70SBen Gras /*
490*0a6a1f1dSLionel Sambuc * In the current line, expand user-defined strings ("\*")
491*0a6a1f1dSLionel Sambuc * and references to number registers ("\n").
492*0a6a1f1dSLionel Sambuc * Also check the syntax of other escape sequences.
493d65f6f70SBen Gras */
49492395e9cSLionel Sambuc static enum rofferr
roff_res(struct roff * r,char ** bufp,size_t * szp,int ln,int pos)49592395e9cSLionel Sambuc roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
496d65f6f70SBen Gras {
497*0a6a1f1dSLionel Sambuc char ubuf[12]; /* buffer to print the number */
498d65f6f70SBen Gras const char *stesc; /* start of an escape sequence ('\\') */
499d65f6f70SBen Gras const char *stnam; /* start of the name, after "[(*" */
500d65f6f70SBen Gras const char *cp; /* end of the name, e.g. before ']' */
501d65f6f70SBen Gras const char *res; /* the string to be substituted */
502*0a6a1f1dSLionel Sambuc char *nbuf; /* new buffer to copy bufp to */
503*0a6a1f1dSLionel Sambuc size_t nsz; /* size of the new buffer */
504*0a6a1f1dSLionel Sambuc size_t maxl; /* expected length of the escape name */
505*0a6a1f1dSLionel Sambuc size_t naml; /* actual length of the escape name */
506*0a6a1f1dSLionel Sambuc int expand_count; /* to avoid infinite loops */
507d65f6f70SBen Gras
50892395e9cSLionel Sambuc expand_count = 0;
509d65f6f70SBen Gras
51092395e9cSLionel Sambuc again:
511d65f6f70SBen Gras cp = *bufp + pos;
512d65f6f70SBen Gras while (NULL != (cp = strchr(cp, '\\'))) {
513d65f6f70SBen Gras stesc = cp++;
514d65f6f70SBen Gras
515d65f6f70SBen Gras /*
516*0a6a1f1dSLionel Sambuc * The second character must be an asterisk or an n.
517d65f6f70SBen Gras * If it isn't, skip it anyway: It is escaped,
518d65f6f70SBen Gras * so it can't start another escape sequence.
519d65f6f70SBen Gras */
520d65f6f70SBen Gras
521d65f6f70SBen Gras if ('\0' == *cp)
52292395e9cSLionel Sambuc return(ROFF_CONT);
52392395e9cSLionel Sambuc
524*0a6a1f1dSLionel Sambuc switch (*cp) {
525*0a6a1f1dSLionel Sambuc case ('*'):
526*0a6a1f1dSLionel Sambuc res = NULL;
527*0a6a1f1dSLionel Sambuc break;
528*0a6a1f1dSLionel Sambuc case ('n'):
529*0a6a1f1dSLionel Sambuc res = ubuf;
530*0a6a1f1dSLionel Sambuc break;
531*0a6a1f1dSLionel Sambuc default:
532*0a6a1f1dSLionel Sambuc if (ESCAPE_ERROR != mandoc_escape(&cp, NULL, NULL))
533d65f6f70SBen Gras continue;
53492395e9cSLionel Sambuc mandoc_msg
53592395e9cSLionel Sambuc (MANDOCERR_BADESCAPE, r->parse,
53692395e9cSLionel Sambuc ln, (int)(stesc - *bufp), NULL);
53792395e9cSLionel Sambuc return(ROFF_CONT);
53892395e9cSLionel Sambuc }
53992395e9cSLionel Sambuc
54092395e9cSLionel Sambuc cp++;
541d65f6f70SBen Gras
542d65f6f70SBen Gras /*
543d65f6f70SBen Gras * The third character decides the length
544*0a6a1f1dSLionel Sambuc * of the name of the string or register.
545d65f6f70SBen Gras * Save a pointer to the name.
546d65f6f70SBen Gras */
547d65f6f70SBen Gras
548d65f6f70SBen Gras switch (*cp) {
549d65f6f70SBen Gras case ('\0'):
55092395e9cSLionel Sambuc return(ROFF_CONT);
551d65f6f70SBen Gras case ('('):
552d65f6f70SBen Gras cp++;
553d65f6f70SBen Gras maxl = 2;
554d65f6f70SBen Gras break;
555d65f6f70SBen Gras case ('['):
556d65f6f70SBen Gras cp++;
557d65f6f70SBen Gras maxl = 0;
558d65f6f70SBen Gras break;
559d65f6f70SBen Gras default:
560d65f6f70SBen Gras maxl = 1;
561d65f6f70SBen Gras break;
562d65f6f70SBen Gras }
563d65f6f70SBen Gras stnam = cp;
564d65f6f70SBen Gras
565d65f6f70SBen Gras /* Advance to the end of the name. */
566d65f6f70SBen Gras
567*0a6a1f1dSLionel Sambuc for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
56892395e9cSLionel Sambuc if ('\0' == *cp) {
56992395e9cSLionel Sambuc mandoc_msg
57092395e9cSLionel Sambuc (MANDOCERR_BADESCAPE,
57192395e9cSLionel Sambuc r->parse, ln,
57292395e9cSLionel Sambuc (int)(stesc - *bufp), NULL);
57392395e9cSLionel Sambuc return(ROFF_CONT);
57492395e9cSLionel Sambuc }
575d65f6f70SBen Gras if (0 == maxl && ']' == *cp)
576d65f6f70SBen Gras break;
577d65f6f70SBen Gras }
578d65f6f70SBen Gras
579d65f6f70SBen Gras /*
580d65f6f70SBen Gras * Retrieve the replacement string; if it is
581d65f6f70SBen Gras * undefined, resume searching for escapes.
582d65f6f70SBen Gras */
583d65f6f70SBen Gras
584*0a6a1f1dSLionel Sambuc if (NULL == res)
585*0a6a1f1dSLionel Sambuc res = roff_getstrn(r, stnam, naml);
586*0a6a1f1dSLionel Sambuc else
587*0a6a1f1dSLionel Sambuc snprintf(ubuf, sizeof(ubuf), "%d",
588*0a6a1f1dSLionel Sambuc roff_getregn(r, stnam, naml));
589d65f6f70SBen Gras
590d65f6f70SBen Gras if (NULL == res) {
59192395e9cSLionel Sambuc mandoc_msg
59292395e9cSLionel Sambuc (MANDOCERR_BADESCAPE, r->parse,
59392395e9cSLionel Sambuc ln, (int)(stesc - *bufp), NULL);
59492395e9cSLionel Sambuc res = "";
595d65f6f70SBen Gras }
596d65f6f70SBen Gras
597d65f6f70SBen Gras /* Replace the escape sequence by the string. */
598d65f6f70SBen Gras
59992395e9cSLionel Sambuc pos = stesc - *bufp;
60092395e9cSLionel Sambuc
601d65f6f70SBen Gras nsz = *szp + strlen(res) + 1;
602*0a6a1f1dSLionel Sambuc nbuf = mandoc_malloc(nsz);
603d65f6f70SBen Gras
604*0a6a1f1dSLionel Sambuc strlcpy(nbuf, *bufp, (size_t)(stesc - *bufp + 1));
605*0a6a1f1dSLionel Sambuc strlcat(nbuf, res, nsz);
606*0a6a1f1dSLionel Sambuc strlcat(nbuf, cp + (maxl ? 0 : 1), nsz);
607d65f6f70SBen Gras
608d65f6f70SBen Gras free(*bufp);
609d65f6f70SBen Gras
610*0a6a1f1dSLionel Sambuc *bufp = nbuf;
611d65f6f70SBen Gras *szp = nsz;
61292395e9cSLionel Sambuc
61392395e9cSLionel Sambuc if (EXPAND_LIMIT >= ++expand_count)
61492395e9cSLionel Sambuc goto again;
61592395e9cSLionel Sambuc
61692395e9cSLionel Sambuc /* Just leave the string unexpanded. */
61792395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
61892395e9cSLionel Sambuc return(ROFF_IGN);
61992395e9cSLionel Sambuc }
62092395e9cSLionel Sambuc return(ROFF_CONT);
621d65f6f70SBen Gras }
622d65f6f70SBen Gras
62392395e9cSLionel Sambuc /*
624*0a6a1f1dSLionel Sambuc * Process text streams:
625*0a6a1f1dSLionel Sambuc * Convert all breakable hyphens into ASCII_HYPH.
626*0a6a1f1dSLionel Sambuc * Decrement and spring input line trap.
62792395e9cSLionel Sambuc */
62892395e9cSLionel Sambuc static enum rofferr
roff_parsetext(char ** bufp,size_t * szp,int pos,int * offs)629*0a6a1f1dSLionel Sambuc roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
63092395e9cSLionel Sambuc {
63192395e9cSLionel Sambuc size_t sz;
63292395e9cSLionel Sambuc const char *start;
633*0a6a1f1dSLionel Sambuc char *p;
634*0a6a1f1dSLionel Sambuc int isz;
63592395e9cSLionel Sambuc enum mandoc_esc esc;
63692395e9cSLionel Sambuc
637*0a6a1f1dSLionel Sambuc start = p = *bufp + pos;
63892395e9cSLionel Sambuc
63992395e9cSLionel Sambuc while ('\0' != *p) {
64092395e9cSLionel Sambuc sz = strcspn(p, "-\\");
64192395e9cSLionel Sambuc p += sz;
64292395e9cSLionel Sambuc
64392395e9cSLionel Sambuc if ('\0' == *p)
64492395e9cSLionel Sambuc break;
64592395e9cSLionel Sambuc
64692395e9cSLionel Sambuc if ('\\' == *p) {
64792395e9cSLionel Sambuc /* Skip over escapes. */
64892395e9cSLionel Sambuc p++;
649*0a6a1f1dSLionel Sambuc esc = mandoc_escape((const char **)(void *)&p, NULL, NULL);
65092395e9cSLionel Sambuc if (ESCAPE_ERROR == esc)
65192395e9cSLionel Sambuc break;
65292395e9cSLionel Sambuc continue;
65392395e9cSLionel Sambuc } else if (p == start) {
65492395e9cSLionel Sambuc p++;
65592395e9cSLionel Sambuc continue;
656d65f6f70SBen Gras }
657d65f6f70SBen Gras
65892395e9cSLionel Sambuc if (isalpha((unsigned char)p[-1]) &&
65992395e9cSLionel Sambuc isalpha((unsigned char)p[1]))
66092395e9cSLionel Sambuc *p = ASCII_HYPH;
66192395e9cSLionel Sambuc p++;
66292395e9cSLionel Sambuc }
66392395e9cSLionel Sambuc
664*0a6a1f1dSLionel Sambuc /* Spring the input line trap. */
665*0a6a1f1dSLionel Sambuc if (1 == roffit_lines) {
666*0a6a1f1dSLionel Sambuc isz = asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
667*0a6a1f1dSLionel Sambuc if (-1 == isz) {
668*0a6a1f1dSLionel Sambuc perror(NULL);
669*0a6a1f1dSLionel Sambuc exit((int)MANDOCLEVEL_SYSERR);
670*0a6a1f1dSLionel Sambuc }
671*0a6a1f1dSLionel Sambuc free(*bufp);
672*0a6a1f1dSLionel Sambuc *bufp = p;
673*0a6a1f1dSLionel Sambuc *szp = isz + 1;
674*0a6a1f1dSLionel Sambuc *offs = 0;
675*0a6a1f1dSLionel Sambuc free(roffit_macro);
676*0a6a1f1dSLionel Sambuc roffit_lines = 0;
677*0a6a1f1dSLionel Sambuc return(ROFF_REPARSE);
678*0a6a1f1dSLionel Sambuc } else if (1 < roffit_lines)
679*0a6a1f1dSLionel Sambuc --roffit_lines;
68092395e9cSLionel Sambuc return(ROFF_CONT);
68192395e9cSLionel Sambuc }
682d65f6f70SBen Gras
683d65f6f70SBen Gras enum rofferr
roff_parseln(struct roff * r,int ln,char ** bufp,size_t * szp,int pos,int * offs)684d65f6f70SBen Gras roff_parseln(struct roff *r, int ln, char **bufp,
685d65f6f70SBen Gras size_t *szp, int pos, int *offs)
686d65f6f70SBen Gras {
687d65f6f70SBen Gras enum rofft t;
688d65f6f70SBen Gras enum rofferr e;
68992395e9cSLionel Sambuc int ppos, ctl;
690d65f6f70SBen Gras
691d65f6f70SBen Gras /*
692d65f6f70SBen Gras * Run the reserved-word filter only if we have some reserved
693d65f6f70SBen Gras * words to fill in.
694d65f6f70SBen Gras */
695d65f6f70SBen Gras
69692395e9cSLionel Sambuc e = roff_res(r, bufp, szp, ln, pos);
69792395e9cSLionel Sambuc if (ROFF_IGN == e)
69892395e9cSLionel Sambuc return(e);
69992395e9cSLionel Sambuc assert(ROFF_CONT == e);
70092395e9cSLionel Sambuc
70192395e9cSLionel Sambuc ppos = pos;
702*0a6a1f1dSLionel Sambuc ctl = roff_getcontrol(r, *bufp, &pos);
703d65f6f70SBen Gras
704d65f6f70SBen Gras /*
705d65f6f70SBen Gras * First, if a scope is open and we're not a macro, pass the
706d65f6f70SBen Gras * text through the macro's filter. If a scope isn't open and
707d65f6f70SBen Gras * we're not a macro, just let it through.
70892395e9cSLionel Sambuc * Finally, if there's an equation scope open, divert it into it
70992395e9cSLionel Sambuc * no matter our state.
710d65f6f70SBen Gras */
711d65f6f70SBen Gras
712*0a6a1f1dSLionel Sambuc if (r->last && ! ctl) {
713d65f6f70SBen Gras t = r->last->tok;
714d65f6f70SBen Gras assert(roffs[t].text);
715d65f6f70SBen Gras e = (*roffs[t].text)
716d65f6f70SBen Gras (r, t, bufp, szp, ln, pos, pos, offs);
717d65f6f70SBen Gras assert(ROFF_IGN == e || ROFF_CONT == e);
71892395e9cSLionel Sambuc if (ROFF_CONT != e)
719d65f6f70SBen Gras return(e);
72084d9c625SLionel Sambuc }
72192395e9cSLionel Sambuc if (r->eqn)
722*0a6a1f1dSLionel Sambuc return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
723*0a6a1f1dSLionel Sambuc if ( ! ctl) {
72492395e9cSLionel Sambuc if (r->tbl)
72592395e9cSLionel Sambuc return(tbl_read(r->tbl, ln, *bufp, pos));
726*0a6a1f1dSLionel Sambuc return(roff_parsetext(bufp, szp, pos, offs));
727*0a6a1f1dSLionel Sambuc }
728d65f6f70SBen Gras
729d65f6f70SBen Gras /*
730d65f6f70SBen Gras * If a scope is open, go to the child handler for that macro,
731d65f6f70SBen Gras * as it may want to preprocess before doing anything with it.
73292395e9cSLionel Sambuc * Don't do so if an equation is open.
733d65f6f70SBen Gras */
734d65f6f70SBen Gras
735d65f6f70SBen Gras if (r->last) {
736d65f6f70SBen Gras t = r->last->tok;
737d65f6f70SBen Gras assert(roffs[t].sub);
738d65f6f70SBen Gras return((*roffs[t].sub)
739d65f6f70SBen Gras (r, t, bufp, szp,
74092395e9cSLionel Sambuc ln, ppos, pos, offs));
741d65f6f70SBen Gras }
742d65f6f70SBen Gras
743d65f6f70SBen Gras /*
744d65f6f70SBen Gras * Lastly, as we've no scope open, try to look up and execute
745d65f6f70SBen Gras * the new macro. If no macro is found, simply return and let
746d65f6f70SBen Gras * the compilers handle it.
747d65f6f70SBen Gras */
748d65f6f70SBen Gras
749d65f6f70SBen Gras if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
750d65f6f70SBen Gras return(ROFF_CONT);
751d65f6f70SBen Gras
752d65f6f70SBen Gras assert(roffs[t].proc);
753d65f6f70SBen Gras return((*roffs[t].proc)
754d65f6f70SBen Gras (r, t, bufp, szp,
755d65f6f70SBen Gras ln, ppos, pos, offs));
756d65f6f70SBen Gras }
757d65f6f70SBen Gras
758d65f6f70SBen Gras
759d65f6f70SBen Gras void
roff_endparse(struct roff * r)760d65f6f70SBen Gras roff_endparse(struct roff *r)
761d65f6f70SBen Gras {
762d65f6f70SBen Gras
763d65f6f70SBen Gras if (r->last)
76492395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
765d65f6f70SBen Gras r->last->line, r->last->col, NULL);
766d65f6f70SBen Gras
76792395e9cSLionel Sambuc if (r->eqn) {
76892395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
76992395e9cSLionel Sambuc r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
77092395e9cSLionel Sambuc eqn_end(&r->eqn);
771d65f6f70SBen Gras }
772d65f6f70SBen Gras
77392395e9cSLionel Sambuc if (r->tbl) {
77492395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
77592395e9cSLionel Sambuc r->tbl->line, r->tbl->pos, NULL);
77692395e9cSLionel Sambuc tbl_end(&r->tbl);
77792395e9cSLionel Sambuc }
77892395e9cSLionel Sambuc }
779d65f6f70SBen Gras
780d65f6f70SBen Gras /*
781d65f6f70SBen Gras * Parse a roff node's type from the input buffer. This must be in the
782d65f6f70SBen Gras * form of ".foo xxx" in the usual way.
783d65f6f70SBen Gras */
784d65f6f70SBen Gras static enum rofft
roff_parse(struct roff * r,const char * buf,int * pos)785d65f6f70SBen Gras roff_parse(struct roff *r, const char *buf, int *pos)
786d65f6f70SBen Gras {
787d65f6f70SBen Gras const char *mac;
788d65f6f70SBen Gras size_t maclen;
789d65f6f70SBen Gras enum rofft t;
790d65f6f70SBen Gras
79192395e9cSLionel Sambuc if ('\0' == buf[*pos] || '"' == buf[*pos] ||
79292395e9cSLionel Sambuc '\t' == buf[*pos] || ' ' == buf[*pos])
793d65f6f70SBen Gras return(ROFF_MAX);
794d65f6f70SBen Gras
79592395e9cSLionel Sambuc /*
79692395e9cSLionel Sambuc * We stop the macro parse at an escape, tab, space, or nil.
79792395e9cSLionel Sambuc * However, `\}' is also a valid macro, so make sure we don't
79892395e9cSLionel Sambuc * clobber it by seeing the `\' as the end of token.
79992395e9cSLionel Sambuc */
80092395e9cSLionel Sambuc
801d65f6f70SBen Gras mac = buf + *pos;
80292395e9cSLionel Sambuc maclen = strcspn(mac + 1, " \\\t\0") + 1;
803d65f6f70SBen Gras
804d65f6f70SBen Gras t = (r->current_string = roff_getstrn(r, mac, maclen))
80592395e9cSLionel Sambuc ? ROFF_USERDEF : roffhash_find(mac, maclen);
806d65f6f70SBen Gras
80792395e9cSLionel Sambuc *pos += (int)maclen;
80892395e9cSLionel Sambuc
809d65f6f70SBen Gras while (buf[*pos] && ' ' == buf[*pos])
810d65f6f70SBen Gras (*pos)++;
811d65f6f70SBen Gras
812d65f6f70SBen Gras return(t);
813d65f6f70SBen Gras }
814d65f6f70SBen Gras
815d65f6f70SBen Gras /* ARGSUSED */
816d65f6f70SBen Gras static enum rofferr
roff_cblock(ROFF_ARGS)817d65f6f70SBen Gras roff_cblock(ROFF_ARGS)
818d65f6f70SBen Gras {
819d65f6f70SBen Gras
820d65f6f70SBen Gras /*
821d65f6f70SBen Gras * A block-close `..' should only be invoked as a child of an
822d65f6f70SBen Gras * ignore macro, otherwise raise a warning and just ignore it.
823d65f6f70SBen Gras */
824d65f6f70SBen Gras
825d65f6f70SBen Gras if (NULL == r->last) {
82692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
827d65f6f70SBen Gras return(ROFF_IGN);
828d65f6f70SBen Gras }
829d65f6f70SBen Gras
830d65f6f70SBen Gras switch (r->last->tok) {
831d65f6f70SBen Gras case (ROFF_am):
832d65f6f70SBen Gras /* FALLTHROUGH */
833d65f6f70SBen Gras case (ROFF_ami):
834d65f6f70SBen Gras /* FALLTHROUGH */
835d65f6f70SBen Gras case (ROFF_am1):
836d65f6f70SBen Gras /* FALLTHROUGH */
837d65f6f70SBen Gras case (ROFF_de):
838d65f6f70SBen Gras /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
839d65f6f70SBen Gras /* FALLTHROUGH */
840d65f6f70SBen Gras case (ROFF_dei):
841d65f6f70SBen Gras /* FALLTHROUGH */
842d65f6f70SBen Gras case (ROFF_ig):
843d65f6f70SBen Gras break;
844d65f6f70SBen Gras default:
84592395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
846d65f6f70SBen Gras return(ROFF_IGN);
847d65f6f70SBen Gras }
848d65f6f70SBen Gras
849d65f6f70SBen Gras if ((*bufp)[pos])
85092395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
851d65f6f70SBen Gras
852d65f6f70SBen Gras roffnode_pop(r);
853d65f6f70SBen Gras roffnode_cleanscope(r);
854d65f6f70SBen Gras return(ROFF_IGN);
855d65f6f70SBen Gras
856d65f6f70SBen Gras }
857d65f6f70SBen Gras
858d65f6f70SBen Gras
859d65f6f70SBen Gras static void
roffnode_cleanscope(struct roff * r)860d65f6f70SBen Gras roffnode_cleanscope(struct roff *r)
861d65f6f70SBen Gras {
862d65f6f70SBen Gras
863d65f6f70SBen Gras while (r->last) {
864*0a6a1f1dSLionel Sambuc if (--r->last->endspan != 0)
865d65f6f70SBen Gras break;
866d65f6f70SBen Gras roffnode_pop(r);
867d65f6f70SBen Gras }
868d65f6f70SBen Gras }
869d65f6f70SBen Gras
870d65f6f70SBen Gras
871d65f6f70SBen Gras /* ARGSUSED */
872d65f6f70SBen Gras static enum rofferr
roff_ccond(ROFF_ARGS)873d65f6f70SBen Gras roff_ccond(ROFF_ARGS)
874d65f6f70SBen Gras {
875d65f6f70SBen Gras
876d65f6f70SBen Gras if (NULL == r->last) {
87792395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
878d65f6f70SBen Gras return(ROFF_IGN);
879d65f6f70SBen Gras }
880d65f6f70SBen Gras
881d65f6f70SBen Gras switch (r->last->tok) {
882d65f6f70SBen Gras case (ROFF_el):
883d65f6f70SBen Gras /* FALLTHROUGH */
884d65f6f70SBen Gras case (ROFF_ie):
885d65f6f70SBen Gras /* FALLTHROUGH */
886d65f6f70SBen Gras case (ROFF_if):
887d65f6f70SBen Gras break;
888d65f6f70SBen Gras default:
88992395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
890d65f6f70SBen Gras return(ROFF_IGN);
891d65f6f70SBen Gras }
892d65f6f70SBen Gras
893d65f6f70SBen Gras if (r->last->endspan > -1) {
89492395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
895d65f6f70SBen Gras return(ROFF_IGN);
896d65f6f70SBen Gras }
897d65f6f70SBen Gras
898d65f6f70SBen Gras if ((*bufp)[pos])
89992395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
900d65f6f70SBen Gras
901d65f6f70SBen Gras roffnode_pop(r);
902d65f6f70SBen Gras roffnode_cleanscope(r);
903d65f6f70SBen Gras return(ROFF_IGN);
904d65f6f70SBen Gras }
905d65f6f70SBen Gras
906d65f6f70SBen Gras
907d65f6f70SBen Gras /* ARGSUSED */
908d65f6f70SBen Gras static enum rofferr
roff_block(ROFF_ARGS)909d65f6f70SBen Gras roff_block(ROFF_ARGS)
910d65f6f70SBen Gras {
911d65f6f70SBen Gras int sv;
912d65f6f70SBen Gras size_t sz;
913d65f6f70SBen Gras char *name;
914d65f6f70SBen Gras
915d65f6f70SBen Gras name = NULL;
916d65f6f70SBen Gras
917d65f6f70SBen Gras if (ROFF_ig != tok) {
918d65f6f70SBen Gras if ('\0' == (*bufp)[pos]) {
91992395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
920d65f6f70SBen Gras return(ROFF_IGN);
921d65f6f70SBen Gras }
922d65f6f70SBen Gras
923d65f6f70SBen Gras /*
924d65f6f70SBen Gras * Re-write `de1', since we don't really care about
925d65f6f70SBen Gras * groff's strange compatibility mode, into `de'.
926d65f6f70SBen Gras */
927d65f6f70SBen Gras
928d65f6f70SBen Gras if (ROFF_de1 == tok)
929d65f6f70SBen Gras tok = ROFF_de;
930d65f6f70SBen Gras if (ROFF_de == tok)
931d65f6f70SBen Gras name = *bufp + pos;
932d65f6f70SBen Gras else
93392395e9cSLionel Sambuc mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
934d65f6f70SBen Gras roffs[tok].name);
935d65f6f70SBen Gras
93692395e9cSLionel Sambuc while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
937d65f6f70SBen Gras pos++;
938d65f6f70SBen Gras
93992395e9cSLionel Sambuc while (isspace((unsigned char)(*bufp)[pos]))
940d65f6f70SBen Gras (*bufp)[pos++] = '\0';
941d65f6f70SBen Gras }
942d65f6f70SBen Gras
943d65f6f70SBen Gras roffnode_push(r, tok, name, ln, ppos);
944d65f6f70SBen Gras
945d65f6f70SBen Gras /*
946d65f6f70SBen Gras * At the beginning of a `de' macro, clear the existing string
947d65f6f70SBen Gras * with the same name, if there is one. New content will be
948d65f6f70SBen Gras * added from roff_block_text() in multiline mode.
949d65f6f70SBen Gras */
950d65f6f70SBen Gras
951d65f6f70SBen Gras if (ROFF_de == tok)
952d65f6f70SBen Gras roff_setstr(r, name, "", 0);
953d65f6f70SBen Gras
954d65f6f70SBen Gras if ('\0' == (*bufp)[pos])
955d65f6f70SBen Gras return(ROFF_IGN);
956d65f6f70SBen Gras
957d65f6f70SBen Gras /* If present, process the custom end-of-line marker. */
958d65f6f70SBen Gras
959d65f6f70SBen Gras sv = pos;
96092395e9cSLionel Sambuc while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
961d65f6f70SBen Gras pos++;
962d65f6f70SBen Gras
963d65f6f70SBen Gras /*
964d65f6f70SBen Gras * Note: groff does NOT like escape characters in the input.
965d65f6f70SBen Gras * Instead of detecting this, we're just going to let it fly and
966d65f6f70SBen Gras * to hell with it.
967d65f6f70SBen Gras */
968d65f6f70SBen Gras
969d65f6f70SBen Gras assert(pos > sv);
970d65f6f70SBen Gras sz = (size_t)(pos - sv);
971d65f6f70SBen Gras
972d65f6f70SBen Gras if (1 == sz && '.' == (*bufp)[sv])
973d65f6f70SBen Gras return(ROFF_IGN);
974d65f6f70SBen Gras
975d65f6f70SBen Gras r->last->end = mandoc_malloc(sz + 1);
976d65f6f70SBen Gras
977d65f6f70SBen Gras memcpy(r->last->end, *bufp + sv, sz);
978d65f6f70SBen Gras r->last->end[(int)sz] = '\0';
979d65f6f70SBen Gras
980d65f6f70SBen Gras if ((*bufp)[pos])
98192395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
982d65f6f70SBen Gras
983d65f6f70SBen Gras return(ROFF_IGN);
984d65f6f70SBen Gras }
985d65f6f70SBen Gras
986d65f6f70SBen Gras
987d65f6f70SBen Gras /* ARGSUSED */
988d65f6f70SBen Gras static enum rofferr
roff_block_sub(ROFF_ARGS)989d65f6f70SBen Gras roff_block_sub(ROFF_ARGS)
990d65f6f70SBen Gras {
991d65f6f70SBen Gras enum rofft t;
992d65f6f70SBen Gras int i, j;
993d65f6f70SBen Gras
994d65f6f70SBen Gras /*
995d65f6f70SBen Gras * First check whether a custom macro exists at this level. If
996d65f6f70SBen Gras * it does, then check against it. This is some of groff's
997d65f6f70SBen Gras * stranger behaviours. If we encountered a custom end-scope
998d65f6f70SBen Gras * tag and that tag also happens to be a "real" macro, then we
999d65f6f70SBen Gras * need to try interpreting it again as a real macro. If it's
1000d65f6f70SBen Gras * not, then return ignore. Else continue.
1001d65f6f70SBen Gras */
1002d65f6f70SBen Gras
1003d65f6f70SBen Gras if (r->last->end) {
100492395e9cSLionel Sambuc for (i = pos, j = 0; r->last->end[j]; j++, i++)
1005d65f6f70SBen Gras if ((*bufp)[i] != r->last->end[j])
1006d65f6f70SBen Gras break;
1007d65f6f70SBen Gras
1008d65f6f70SBen Gras if ('\0' == r->last->end[j] &&
1009d65f6f70SBen Gras ('\0' == (*bufp)[i] ||
1010d65f6f70SBen Gras ' ' == (*bufp)[i] ||
1011d65f6f70SBen Gras '\t' == (*bufp)[i])) {
1012d65f6f70SBen Gras roffnode_pop(r);
1013d65f6f70SBen Gras roffnode_cleanscope(r);
1014d65f6f70SBen Gras
101592395e9cSLionel Sambuc while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
101692395e9cSLionel Sambuc i++;
101792395e9cSLionel Sambuc
101892395e9cSLionel Sambuc pos = i;
1019d65f6f70SBen Gras if (ROFF_MAX != roff_parse(r, *bufp, &pos))
1020d65f6f70SBen Gras return(ROFF_RERUN);
1021d65f6f70SBen Gras return(ROFF_IGN);
1022d65f6f70SBen Gras }
1023d65f6f70SBen Gras }
1024d65f6f70SBen Gras
1025d65f6f70SBen Gras /*
1026d65f6f70SBen Gras * If we have no custom end-query or lookup failed, then try
1027d65f6f70SBen Gras * pulling it out of the hashtable.
1028d65f6f70SBen Gras */
1029d65f6f70SBen Gras
1030d65f6f70SBen Gras t = roff_parse(r, *bufp, &pos);
1031d65f6f70SBen Gras
1032d65f6f70SBen Gras /*
1033d65f6f70SBen Gras * Macros other than block-end are only significant
1034d65f6f70SBen Gras * in `de' blocks; elsewhere, simply throw them away.
1035d65f6f70SBen Gras */
1036d65f6f70SBen Gras if (ROFF_cblock != t) {
1037d65f6f70SBen Gras if (ROFF_de == tok)
1038d65f6f70SBen Gras roff_setstr(r, r->last->name, *bufp + ppos, 1);
1039d65f6f70SBen Gras return(ROFF_IGN);
1040d65f6f70SBen Gras }
1041d65f6f70SBen Gras
1042d65f6f70SBen Gras assert(roffs[t].proc);
1043d65f6f70SBen Gras return((*roffs[t].proc)(r, t, bufp, szp,
1044d65f6f70SBen Gras ln, ppos, pos, offs));
1045d65f6f70SBen Gras }
1046d65f6f70SBen Gras
1047d65f6f70SBen Gras
1048d65f6f70SBen Gras /* ARGSUSED */
1049d65f6f70SBen Gras static enum rofferr
roff_block_text(ROFF_ARGS)1050d65f6f70SBen Gras roff_block_text(ROFF_ARGS)
1051d65f6f70SBen Gras {
1052d65f6f70SBen Gras
1053d65f6f70SBen Gras if (ROFF_de == tok)
1054d65f6f70SBen Gras roff_setstr(r, r->last->name, *bufp + pos, 1);
1055d65f6f70SBen Gras
1056d65f6f70SBen Gras return(ROFF_IGN);
1057d65f6f70SBen Gras }
1058d65f6f70SBen Gras
1059d65f6f70SBen Gras
1060d65f6f70SBen Gras /* ARGSUSED */
1061d65f6f70SBen Gras static enum rofferr
roff_cond_sub(ROFF_ARGS)1062d65f6f70SBen Gras roff_cond_sub(ROFF_ARGS)
1063d65f6f70SBen Gras {
1064d65f6f70SBen Gras enum rofft t;
1065d65f6f70SBen Gras enum roffrule rr;
106692395e9cSLionel Sambuc char *ep;
1067d65f6f70SBen Gras
1068d65f6f70SBen Gras rr = r->last->rule;
1069d65f6f70SBen Gras roffnode_cleanscope(r);
1070*0a6a1f1dSLionel Sambuc t = roff_parse(r, *bufp, &pos);
1071d65f6f70SBen Gras
107292395e9cSLionel Sambuc /*
1073*0a6a1f1dSLionel Sambuc * Fully handle known macros when they are structurally
1074*0a6a1f1dSLionel Sambuc * required or when the conditional evaluated to true.
107592395e9cSLionel Sambuc */
107692395e9cSLionel Sambuc
1077*0a6a1f1dSLionel Sambuc if ((ROFF_MAX != t) &&
1078*0a6a1f1dSLionel Sambuc (ROFF_ccond == t || ROFFRULE_ALLOW == rr ||
1079*0a6a1f1dSLionel Sambuc ROFFMAC_STRUCT & roffs[t].flags)) {
1080*0a6a1f1dSLionel Sambuc assert(roffs[t].proc);
1081*0a6a1f1dSLionel Sambuc return((*roffs[t].proc)(r, t, bufp, szp,
1082*0a6a1f1dSLionel Sambuc ln, ppos, pos, offs));
1083*0a6a1f1dSLionel Sambuc }
1084*0a6a1f1dSLionel Sambuc
1085*0a6a1f1dSLionel Sambuc /* Always check for the closing delimiter `\}'. */
1086*0a6a1f1dSLionel Sambuc
108792395e9cSLionel Sambuc ep = &(*bufp)[pos];
1088*0a6a1f1dSLionel Sambuc while (NULL != (ep = strchr(ep, '\\'))) {
1089*0a6a1f1dSLionel Sambuc if ('}' != *(++ep))
109092395e9cSLionel Sambuc continue;
109192395e9cSLionel Sambuc
109292395e9cSLionel Sambuc /*
109392395e9cSLionel Sambuc * If we're at the end of line, then just chop
109492395e9cSLionel Sambuc * off the \} and resize the buffer.
1095*0a6a1f1dSLionel Sambuc * If we aren't, then convert it to spaces.
109692395e9cSLionel Sambuc */
109792395e9cSLionel Sambuc
109892395e9cSLionel Sambuc if ('\0' == *(ep + 1)) {
109992395e9cSLionel Sambuc *--ep = '\0';
110092395e9cSLionel Sambuc *szp -= 2;
110192395e9cSLionel Sambuc } else
110292395e9cSLionel Sambuc *(ep - 1) = *ep = ' ';
110392395e9cSLionel Sambuc
110492395e9cSLionel Sambuc roff_ccond(r, ROFF_ccond, bufp, szp,
110592395e9cSLionel Sambuc ln, pos, pos + 2, offs);
110692395e9cSLionel Sambuc break;
110792395e9cSLionel Sambuc }
1108d65f6f70SBen Gras return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1109d65f6f70SBen Gras }
1110d65f6f70SBen Gras
1111d65f6f70SBen Gras /* ARGSUSED */
1112d65f6f70SBen Gras static enum rofferr
roff_cond_text(ROFF_ARGS)1113d65f6f70SBen Gras roff_cond_text(ROFF_ARGS)
1114d65f6f70SBen Gras {
111592395e9cSLionel Sambuc char *ep;
1116d65f6f70SBen Gras enum roffrule rr;
1117d65f6f70SBen Gras
1118d65f6f70SBen Gras rr = r->last->rule;
1119d65f6f70SBen Gras roffnode_cleanscope(r);
112092395e9cSLionel Sambuc
112192395e9cSLionel Sambuc ep = &(*bufp)[pos];
112292395e9cSLionel Sambuc for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
112392395e9cSLionel Sambuc ep++;
112492395e9cSLionel Sambuc if ('}' != *ep)
112592395e9cSLionel Sambuc continue;
112692395e9cSLionel Sambuc *ep = '&';
112792395e9cSLionel Sambuc roff_ccond(r, ROFF_ccond, bufp, szp,
112892395e9cSLionel Sambuc ln, pos, pos + 2, offs);
112992395e9cSLionel Sambuc }
1130d65f6f70SBen Gras return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1131d65f6f70SBen Gras }
1132d65f6f70SBen Gras
113384d9c625SLionel Sambuc static int
roff_getnum(const char * v,int * pos,int * res)113484d9c625SLionel Sambuc roff_getnum(const char *v, int *pos, int *res)
113584d9c625SLionel Sambuc {
113684d9c625SLionel Sambuc int p, n;
113784d9c625SLionel Sambuc
113884d9c625SLionel Sambuc p = *pos;
1139*0a6a1f1dSLionel Sambuc n = v[p] == '-';
1140*0a6a1f1dSLionel Sambuc if (n)
1141*0a6a1f1dSLionel Sambuc p++;
1142*0a6a1f1dSLionel Sambuc
114384d9c625SLionel Sambuc for (*res = 0; isdigit((unsigned char)v[p]); p++)
114484d9c625SLionel Sambuc *res += 10 * *res + v[p] - '0';
1145*0a6a1f1dSLionel Sambuc if (p == *pos + n)
114684d9c625SLionel Sambuc return 0;
114784d9c625SLionel Sambuc
114884d9c625SLionel Sambuc if (n)
114984d9c625SLionel Sambuc *res = -*res;
115084d9c625SLionel Sambuc
115184d9c625SLionel Sambuc *pos = p;
115284d9c625SLionel Sambuc return 1;
115384d9c625SLionel Sambuc }
115484d9c625SLionel Sambuc
115584d9c625SLionel Sambuc static int
roff_getop(const char * v,int * pos,char * res)1156*0a6a1f1dSLionel Sambuc roff_getop(const char *v, int *pos, char *res)
115784d9c625SLionel Sambuc {
1158*0a6a1f1dSLionel Sambuc int e;
1159*0a6a1f1dSLionel Sambuc
1160*0a6a1f1dSLionel Sambuc *res = v[*pos];
1161*0a6a1f1dSLionel Sambuc e = v[*pos + 1] == '=';
1162*0a6a1f1dSLionel Sambuc
1163*0a6a1f1dSLionel Sambuc switch (*res) {
116484d9c625SLionel Sambuc case '=':
1165*0a6a1f1dSLionel Sambuc break;
116684d9c625SLionel Sambuc case '>':
1167*0a6a1f1dSLionel Sambuc if (e)
1168*0a6a1f1dSLionel Sambuc *res = 'g';
1169*0a6a1f1dSLionel Sambuc break;
117084d9c625SLionel Sambuc case '<':
1171*0a6a1f1dSLionel Sambuc if (e)
1172*0a6a1f1dSLionel Sambuc *res = 'l';
1173*0a6a1f1dSLionel Sambuc break;
117484d9c625SLionel Sambuc default:
1175*0a6a1f1dSLionel Sambuc return(0);
117684d9c625SLionel Sambuc }
1177*0a6a1f1dSLionel Sambuc
1178*0a6a1f1dSLionel Sambuc *pos += 1 + e;
1179*0a6a1f1dSLionel Sambuc
1180*0a6a1f1dSLionel Sambuc return(*res);
118184d9c625SLionel Sambuc }
118284d9c625SLionel Sambuc
1183d65f6f70SBen Gras static enum roffrule
roff_evalcond(const char * v,int * pos)1184d65f6f70SBen Gras roff_evalcond(const char *v, int *pos)
1185d65f6f70SBen Gras {
1186*0a6a1f1dSLionel Sambuc int not, lh, rh;
1187*0a6a1f1dSLionel Sambuc char op;
1188*0a6a1f1dSLionel Sambuc
1189d65f6f70SBen Gras switch (v[*pos]) {
1190d65f6f70SBen Gras case ('n'):
1191d65f6f70SBen Gras (*pos)++;
1192d65f6f70SBen Gras return(ROFFRULE_ALLOW);
1193d65f6f70SBen Gras case ('e'):
1194d65f6f70SBen Gras /* FALLTHROUGH */
1195d65f6f70SBen Gras case ('o'):
1196d65f6f70SBen Gras /* FALLTHROUGH */
1197d65f6f70SBen Gras case ('t'):
1198d65f6f70SBen Gras (*pos)++;
1199d65f6f70SBen Gras return(ROFFRULE_DENY);
120084d9c625SLionel Sambuc case ('!'):
120184d9c625SLionel Sambuc (*pos)++;
1202*0a6a1f1dSLionel Sambuc not = 1;
120384d9c625SLionel Sambuc break;
1204d65f6f70SBen Gras default:
1205*0a6a1f1dSLionel Sambuc not = 0;
1206d65f6f70SBen Gras break;
1207d65f6f70SBen Gras }
1208*0a6a1f1dSLionel Sambuc
120984d9c625SLionel Sambuc if (!roff_getnum(v, pos, &lh))
121084d9c625SLionel Sambuc return ROFFRULE_DENY;
1211*0a6a1f1dSLionel Sambuc if (!roff_getop(v, pos, &op)) {
1212*0a6a1f1dSLionel Sambuc if (lh < 0)
1213*0a6a1f1dSLionel Sambuc lh = 0;
121484d9c625SLionel Sambuc goto out;
1215*0a6a1f1dSLionel Sambuc }
121684d9c625SLionel Sambuc if (!roff_getnum(v, pos, &rh))
121784d9c625SLionel Sambuc return ROFFRULE_DENY;
121884d9c625SLionel Sambuc switch (op) {
1219*0a6a1f1dSLionel Sambuc case 'g':
122084d9c625SLionel Sambuc lh = lh >= rh;
122184d9c625SLionel Sambuc break;
1222*0a6a1f1dSLionel Sambuc case 'l':
122384d9c625SLionel Sambuc lh = lh <= rh;
122484d9c625SLionel Sambuc break;
122584d9c625SLionel Sambuc case '=':
122684d9c625SLionel Sambuc lh = lh == rh;
122784d9c625SLionel Sambuc break;
1228*0a6a1f1dSLionel Sambuc case '>':
122984d9c625SLionel Sambuc lh = lh > rh;
123084d9c625SLionel Sambuc break;
1231*0a6a1f1dSLionel Sambuc case '<':
123284d9c625SLionel Sambuc lh = lh < rh;
123384d9c625SLionel Sambuc break;
123484d9c625SLionel Sambuc default:
123584d9c625SLionel Sambuc return ROFFRULE_DENY;
123684d9c625SLionel Sambuc }
123784d9c625SLionel Sambuc out:
123884d9c625SLionel Sambuc if (not)
123984d9c625SLionel Sambuc lh = !lh;
124084d9c625SLionel Sambuc return lh ? ROFFRULE_ALLOW : ROFFRULE_DENY;
1241d65f6f70SBen Gras }
1242d65f6f70SBen Gras
1243d65f6f70SBen Gras /* ARGSUSED */
1244d65f6f70SBen Gras static enum rofferr
roff_line_ignore(ROFF_ARGS)1245d65f6f70SBen Gras roff_line_ignore(ROFF_ARGS)
1246d65f6f70SBen Gras {
1247d65f6f70SBen Gras
1248d65f6f70SBen Gras return(ROFF_IGN);
1249d65f6f70SBen Gras }
1250d65f6f70SBen Gras
1251d65f6f70SBen Gras /* ARGSUSED */
1252d65f6f70SBen Gras static enum rofferr
roff_cond(ROFF_ARGS)1253d65f6f70SBen Gras roff_cond(ROFF_ARGS)
1254d65f6f70SBen Gras {
1255*0a6a1f1dSLionel Sambuc
1256*0a6a1f1dSLionel Sambuc roffnode_push(r, tok, NULL, ln, ppos);
1257d65f6f70SBen Gras
1258d65f6f70SBen Gras /*
125992395e9cSLionel Sambuc * An `.el' has no conditional body: it will consume the value
126092395e9cSLionel Sambuc * of the current rstack entry set in prior `ie' calls or
126192395e9cSLionel Sambuc * defaults to DENY.
126292395e9cSLionel Sambuc *
126392395e9cSLionel Sambuc * If we're not an `el', however, then evaluate the conditional.
1264d65f6f70SBen Gras */
126592395e9cSLionel Sambuc
1266*0a6a1f1dSLionel Sambuc r->last->rule = ROFF_el == tok ?
126792395e9cSLionel Sambuc (r->rstackpos < 0 ?
126892395e9cSLionel Sambuc ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
126992395e9cSLionel Sambuc roff_evalcond(*bufp, &pos);
1270d65f6f70SBen Gras
1271d65f6f70SBen Gras /*
127292395e9cSLionel Sambuc * An if-else will put the NEGATION of the current evaluated
127392395e9cSLionel Sambuc * conditional into the stack of rules.
1274d65f6f70SBen Gras */
127592395e9cSLionel Sambuc
127692395e9cSLionel Sambuc if (ROFF_ie == tok) {
127792395e9cSLionel Sambuc if (r->rstackpos == RSTACK_MAX - 1) {
127892395e9cSLionel Sambuc mandoc_msg(MANDOCERR_MEM,
127992395e9cSLionel Sambuc r->parse, ln, ppos, NULL);
128092395e9cSLionel Sambuc return(ROFF_ERR);
128192395e9cSLionel Sambuc }
128292395e9cSLionel Sambuc r->rstack[++r->rstackpos] =
128392395e9cSLionel Sambuc ROFFRULE_DENY == r->last->rule ?
128492395e9cSLionel Sambuc ROFFRULE_ALLOW : ROFFRULE_DENY;
1285d65f6f70SBen Gras }
1286d65f6f70SBen Gras
1287d65f6f70SBen Gras /* If the parent has false as its rule, then so do we. */
1288d65f6f70SBen Gras
1289d65f6f70SBen Gras if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1290d65f6f70SBen Gras r->last->rule = ROFFRULE_DENY;
1291d65f6f70SBen Gras
1292d65f6f70SBen Gras /*
1293*0a6a1f1dSLionel Sambuc * Determine scope.
1294*0a6a1f1dSLionel Sambuc * If there is nothing on the line after the conditional,
1295*0a6a1f1dSLionel Sambuc * not even whitespace, use next-line scope.
1296d65f6f70SBen Gras */
1297d65f6f70SBen Gras
1298*0a6a1f1dSLionel Sambuc if ('\0' == (*bufp)[pos]) {
1299*0a6a1f1dSLionel Sambuc r->last->endspan = 2;
1300*0a6a1f1dSLionel Sambuc goto out;
1301*0a6a1f1dSLionel Sambuc }
1302*0a6a1f1dSLionel Sambuc
1303*0a6a1f1dSLionel Sambuc while (' ' == (*bufp)[pos])
1304*0a6a1f1dSLionel Sambuc pos++;
1305*0a6a1f1dSLionel Sambuc
1306*0a6a1f1dSLionel Sambuc /* An opening brace requests multiline scope. */
1307d65f6f70SBen Gras
1308d65f6f70SBen Gras if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1309d65f6f70SBen Gras r->last->endspan = -1;
1310d65f6f70SBen Gras pos += 2;
1311*0a6a1f1dSLionel Sambuc goto out;
1312d65f6f70SBen Gras }
1313d65f6f70SBen Gras
1314d65f6f70SBen Gras /*
1315*0a6a1f1dSLionel Sambuc * Anything else following the conditional causes
1316*0a6a1f1dSLionel Sambuc * single-line scope. Warn if the scope contains
1317*0a6a1f1dSLionel Sambuc * nothing but trailing whitespace.
1318d65f6f70SBen Gras */
1319d65f6f70SBen Gras
1320d65f6f70SBen Gras if ('\0' == (*bufp)[pos])
1321*0a6a1f1dSLionel Sambuc mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1322d65f6f70SBen Gras
1323*0a6a1f1dSLionel Sambuc r->last->endspan = 1;
1324d65f6f70SBen Gras
1325*0a6a1f1dSLionel Sambuc out:
1326d65f6f70SBen Gras *offs = pos;
1327d65f6f70SBen Gras return(ROFF_RERUN);
1328d65f6f70SBen Gras }
1329d65f6f70SBen Gras
1330d65f6f70SBen Gras
1331d65f6f70SBen Gras /* ARGSUSED */
1332d65f6f70SBen Gras static enum rofferr
roff_ds(ROFF_ARGS)1333d65f6f70SBen Gras roff_ds(ROFF_ARGS)
1334d65f6f70SBen Gras {
1335d65f6f70SBen Gras char *name, *string;
1336d65f6f70SBen Gras
1337d65f6f70SBen Gras /*
1338d65f6f70SBen Gras * A symbol is named by the first word following the macro
1339d65f6f70SBen Gras * invocation up to a space. Its value is anything after the
1340d65f6f70SBen Gras * name's trailing whitespace and optional double-quote. Thus,
1341d65f6f70SBen Gras *
1342d65f6f70SBen Gras * [.ds foo "bar " ]
1343d65f6f70SBen Gras *
1344d65f6f70SBen Gras * will have `bar " ' as its value.
1345d65f6f70SBen Gras */
1346d65f6f70SBen Gras
134792395e9cSLionel Sambuc string = *bufp + pos;
134892395e9cSLionel Sambuc name = roff_getname(r, &string, ln, pos);
1349d65f6f70SBen Gras if ('\0' == *name)
1350d65f6f70SBen Gras return(ROFF_IGN);
1351d65f6f70SBen Gras
135292395e9cSLionel Sambuc /* Read past initial double-quote. */
135392395e9cSLionel Sambuc if ('"' == *string)
1354d65f6f70SBen Gras string++;
1355d65f6f70SBen Gras
1356d65f6f70SBen Gras /* The rest is the value. */
1357d65f6f70SBen Gras roff_setstr(r, name, string, 0);
1358d65f6f70SBen Gras return(ROFF_IGN);
1359d65f6f70SBen Gras }
1360d65f6f70SBen Gras
136192395e9cSLionel Sambuc void
roff_setreg(struct roff * r,const char * name,int val,char sign)1362*0a6a1f1dSLionel Sambuc roff_setreg(struct roff *r, const char *name, int val, char sign)
136392395e9cSLionel Sambuc {
1364*0a6a1f1dSLionel Sambuc struct roffreg *reg;
136592395e9cSLionel Sambuc
1366*0a6a1f1dSLionel Sambuc /* Search for an existing register with the same name. */
1367*0a6a1f1dSLionel Sambuc reg = r->regtab;
1368*0a6a1f1dSLionel Sambuc
1369*0a6a1f1dSLionel Sambuc while (reg && strcmp(name, reg->key.p))
1370*0a6a1f1dSLionel Sambuc reg = reg->next;
1371*0a6a1f1dSLionel Sambuc
1372*0a6a1f1dSLionel Sambuc if (NULL == reg) {
1373*0a6a1f1dSLionel Sambuc /* Create a new register. */
1374*0a6a1f1dSLionel Sambuc reg = mandoc_malloc(sizeof(struct roffreg));
1375*0a6a1f1dSLionel Sambuc reg->key.p = mandoc_strdup(name);
1376*0a6a1f1dSLionel Sambuc reg->key.sz = strlen(name);
1377*0a6a1f1dSLionel Sambuc reg->val = 0;
1378*0a6a1f1dSLionel Sambuc reg->next = r->regtab;
1379*0a6a1f1dSLionel Sambuc r->regtab = reg;
138092395e9cSLionel Sambuc }
1381d65f6f70SBen Gras
1382*0a6a1f1dSLionel Sambuc if ('+' == sign)
1383*0a6a1f1dSLionel Sambuc reg->val += val;
1384*0a6a1f1dSLionel Sambuc else if ('-' == sign)
1385*0a6a1f1dSLionel Sambuc reg->val -= val;
1386*0a6a1f1dSLionel Sambuc else
1387*0a6a1f1dSLionel Sambuc reg->val = val;
138884d9c625SLionel Sambuc }
138984d9c625SLionel Sambuc
1390*0a6a1f1dSLionel Sambuc int
roff_getreg(const struct roff * r,const char * name)1391*0a6a1f1dSLionel Sambuc roff_getreg(const struct roff *r, const char *name)
139284d9c625SLionel Sambuc {
1393*0a6a1f1dSLionel Sambuc struct roffreg *reg;
139484d9c625SLionel Sambuc
1395*0a6a1f1dSLionel Sambuc for (reg = r->regtab; reg; reg = reg->next)
1396*0a6a1f1dSLionel Sambuc if (0 == strcmp(name, reg->key.p))
1397*0a6a1f1dSLionel Sambuc return(reg->val);
1398*0a6a1f1dSLionel Sambuc
1399*0a6a1f1dSLionel Sambuc return(0);
140084d9c625SLionel Sambuc }
140184d9c625SLionel Sambuc
1402*0a6a1f1dSLionel Sambuc static int
roff_getregn(const struct roff * r,const char * name,size_t len)1403*0a6a1f1dSLionel Sambuc roff_getregn(const struct roff *r, const char *name, size_t len)
140484d9c625SLionel Sambuc {
1405*0a6a1f1dSLionel Sambuc struct roffreg *reg;
140684d9c625SLionel Sambuc
1407*0a6a1f1dSLionel Sambuc for (reg = r->regtab; reg; reg = reg->next)
1408*0a6a1f1dSLionel Sambuc if (len == reg->key.sz &&
1409*0a6a1f1dSLionel Sambuc 0 == strncmp(name, reg->key.p, len))
1410*0a6a1f1dSLionel Sambuc return(reg->val);
1411*0a6a1f1dSLionel Sambuc
1412*0a6a1f1dSLionel Sambuc return(0);
1413*0a6a1f1dSLionel Sambuc }
1414*0a6a1f1dSLionel Sambuc
1415*0a6a1f1dSLionel Sambuc static void
roff_freereg(struct roffreg * reg)1416*0a6a1f1dSLionel Sambuc roff_freereg(struct roffreg *reg)
1417*0a6a1f1dSLionel Sambuc {
1418*0a6a1f1dSLionel Sambuc struct roffreg *old_reg;
1419*0a6a1f1dSLionel Sambuc
1420*0a6a1f1dSLionel Sambuc while (NULL != reg) {
1421*0a6a1f1dSLionel Sambuc free(reg->key.p);
1422*0a6a1f1dSLionel Sambuc old_reg = reg;
1423*0a6a1f1dSLionel Sambuc reg = reg->next;
1424*0a6a1f1dSLionel Sambuc free(old_reg);
1425*0a6a1f1dSLionel Sambuc }
142684d9c625SLionel Sambuc }
142784d9c625SLionel Sambuc
1428d65f6f70SBen Gras /* ARGSUSED */
1429d65f6f70SBen Gras static enum rofferr
roff_nr(ROFF_ARGS)1430d65f6f70SBen Gras roff_nr(ROFF_ARGS)
1431d65f6f70SBen Gras {
143292395e9cSLionel Sambuc const char *key;
143392395e9cSLionel Sambuc char *val;
1434*0a6a1f1dSLionel Sambuc size_t sz;
1435*0a6a1f1dSLionel Sambuc int iv;
1436*0a6a1f1dSLionel Sambuc char sign;
1437d65f6f70SBen Gras
143892395e9cSLionel Sambuc val = *bufp + pos;
143992395e9cSLionel Sambuc key = roff_getname(r, &val, ln, pos);
1440d65f6f70SBen Gras
1441*0a6a1f1dSLionel Sambuc sign = *val;
1442*0a6a1f1dSLionel Sambuc if ('+' == sign || '-' == sign)
1443*0a6a1f1dSLionel Sambuc val++;
144484d9c625SLionel Sambuc
1445*0a6a1f1dSLionel Sambuc sz = strspn(val, "0123456789");
1446*0a6a1f1dSLionel Sambuc iv = sz ? mandoc_strntoi(val, sz, 10) : 0;
144784d9c625SLionel Sambuc
1448*0a6a1f1dSLionel Sambuc roff_setreg(r, key, iv, sign);
1449d65f6f70SBen Gras
1450d65f6f70SBen Gras return(ROFF_IGN);
1451d65f6f70SBen Gras }
1452d65f6f70SBen Gras
1453d65f6f70SBen Gras /* ARGSUSED */
1454d65f6f70SBen Gras static enum rofferr
roff_rm(ROFF_ARGS)145592395e9cSLionel Sambuc roff_rm(ROFF_ARGS)
145692395e9cSLionel Sambuc {
145792395e9cSLionel Sambuc const char *name;
145892395e9cSLionel Sambuc char *cp;
145992395e9cSLionel Sambuc
146092395e9cSLionel Sambuc cp = *bufp + pos;
146192395e9cSLionel Sambuc while ('\0' != *cp) {
146292395e9cSLionel Sambuc name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
146392395e9cSLionel Sambuc if ('\0' != *name)
146492395e9cSLionel Sambuc roff_setstr(r, name, NULL, 0);
146592395e9cSLionel Sambuc }
146692395e9cSLionel Sambuc return(ROFF_IGN);
146792395e9cSLionel Sambuc }
146892395e9cSLionel Sambuc
146992395e9cSLionel Sambuc /* ARGSUSED */
147092395e9cSLionel Sambuc static enum rofferr
roff_it(ROFF_ARGS)1471*0a6a1f1dSLionel Sambuc roff_it(ROFF_ARGS)
1472*0a6a1f1dSLionel Sambuc {
1473*0a6a1f1dSLionel Sambuc char *cp;
1474*0a6a1f1dSLionel Sambuc size_t len;
1475*0a6a1f1dSLionel Sambuc int iv;
1476*0a6a1f1dSLionel Sambuc
1477*0a6a1f1dSLionel Sambuc /* Parse the number of lines. */
1478*0a6a1f1dSLionel Sambuc cp = *bufp + pos;
1479*0a6a1f1dSLionel Sambuc len = strcspn(cp, " \t");
1480*0a6a1f1dSLionel Sambuc cp[len] = '\0';
1481*0a6a1f1dSLionel Sambuc if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1482*0a6a1f1dSLionel Sambuc mandoc_msg(MANDOCERR_NUMERIC, r->parse,
1483*0a6a1f1dSLionel Sambuc ln, ppos, *bufp + 1);
1484*0a6a1f1dSLionel Sambuc return(ROFF_IGN);
1485*0a6a1f1dSLionel Sambuc }
1486*0a6a1f1dSLionel Sambuc cp += len + 1;
1487*0a6a1f1dSLionel Sambuc
1488*0a6a1f1dSLionel Sambuc /* Arm the input line trap. */
1489*0a6a1f1dSLionel Sambuc roffit_lines = iv;
1490*0a6a1f1dSLionel Sambuc roffit_macro = mandoc_strdup(cp);
1491*0a6a1f1dSLionel Sambuc return(ROFF_IGN);
1492*0a6a1f1dSLionel Sambuc }
1493*0a6a1f1dSLionel Sambuc
1494*0a6a1f1dSLionel Sambuc /* ARGSUSED */
1495*0a6a1f1dSLionel Sambuc static enum rofferr
roff_Dd(ROFF_ARGS)1496*0a6a1f1dSLionel Sambuc roff_Dd(ROFF_ARGS)
1497*0a6a1f1dSLionel Sambuc {
1498*0a6a1f1dSLionel Sambuc const char *const *cp;
1499*0a6a1f1dSLionel Sambuc
1500*0a6a1f1dSLionel Sambuc if (MPARSE_MDOC != r->parsetype)
1501*0a6a1f1dSLionel Sambuc for (cp = __mdoc_reserved; *cp; cp++)
1502*0a6a1f1dSLionel Sambuc roff_setstr(r, *cp, NULL, 0);
1503*0a6a1f1dSLionel Sambuc
1504*0a6a1f1dSLionel Sambuc return(ROFF_CONT);
1505*0a6a1f1dSLionel Sambuc }
1506*0a6a1f1dSLionel Sambuc
1507*0a6a1f1dSLionel Sambuc /* ARGSUSED */
1508*0a6a1f1dSLionel Sambuc static enum rofferr
roff_TH(ROFF_ARGS)1509*0a6a1f1dSLionel Sambuc roff_TH(ROFF_ARGS)
1510*0a6a1f1dSLionel Sambuc {
1511*0a6a1f1dSLionel Sambuc const char *const *cp;
1512*0a6a1f1dSLionel Sambuc
1513*0a6a1f1dSLionel Sambuc if (MPARSE_MDOC != r->parsetype)
1514*0a6a1f1dSLionel Sambuc for (cp = __man_reserved; *cp; cp++)
1515*0a6a1f1dSLionel Sambuc roff_setstr(r, *cp, NULL, 0);
1516*0a6a1f1dSLionel Sambuc
1517*0a6a1f1dSLionel Sambuc return(ROFF_CONT);
1518*0a6a1f1dSLionel Sambuc }
1519*0a6a1f1dSLionel Sambuc
1520*0a6a1f1dSLionel Sambuc /* ARGSUSED */
1521*0a6a1f1dSLionel Sambuc static enum rofferr
roff_TE(ROFF_ARGS)1522d65f6f70SBen Gras roff_TE(ROFF_ARGS)
1523d65f6f70SBen Gras {
1524d65f6f70SBen Gras
1525d65f6f70SBen Gras if (NULL == r->tbl)
152692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1527d65f6f70SBen Gras else
152892395e9cSLionel Sambuc tbl_end(&r->tbl);
1529d65f6f70SBen Gras
1530d65f6f70SBen Gras return(ROFF_IGN);
1531d65f6f70SBen Gras }
1532d65f6f70SBen Gras
1533d65f6f70SBen Gras /* ARGSUSED */
1534d65f6f70SBen Gras static enum rofferr
roff_T_(ROFF_ARGS)1535d65f6f70SBen Gras roff_T_(ROFF_ARGS)
1536d65f6f70SBen Gras {
1537d65f6f70SBen Gras
1538d65f6f70SBen Gras if (NULL == r->tbl)
153992395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1540d65f6f70SBen Gras else
1541d65f6f70SBen Gras tbl_restart(ppos, ln, r->tbl);
1542d65f6f70SBen Gras
1543d65f6f70SBen Gras return(ROFF_IGN);
1544d65f6f70SBen Gras }
1545d65f6f70SBen Gras
154692395e9cSLionel Sambuc #if 0
154792395e9cSLionel Sambuc static int
154892395e9cSLionel Sambuc roff_closeeqn(struct roff *r)
154992395e9cSLionel Sambuc {
155092395e9cSLionel Sambuc
155192395e9cSLionel Sambuc return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
155292395e9cSLionel Sambuc }
155392395e9cSLionel Sambuc #endif
155492395e9cSLionel Sambuc
155592395e9cSLionel Sambuc static void
roff_openeqn(struct roff * r,const char * name,int line,int offs,const char * buf)155692395e9cSLionel Sambuc roff_openeqn(struct roff *r, const char *name, int line,
155792395e9cSLionel Sambuc int offs, const char *buf)
155892395e9cSLionel Sambuc {
155992395e9cSLionel Sambuc struct eqn_node *e;
156092395e9cSLionel Sambuc int poff;
156192395e9cSLionel Sambuc
156292395e9cSLionel Sambuc assert(NULL == r->eqn);
156392395e9cSLionel Sambuc e = eqn_alloc(name, offs, line, r->parse);
156492395e9cSLionel Sambuc
156592395e9cSLionel Sambuc if (r->last_eqn)
156692395e9cSLionel Sambuc r->last_eqn->next = e;
156792395e9cSLionel Sambuc else
156892395e9cSLionel Sambuc r->first_eqn = r->last_eqn = e;
156992395e9cSLionel Sambuc
157092395e9cSLionel Sambuc r->eqn = r->last_eqn = e;
157192395e9cSLionel Sambuc
157292395e9cSLionel Sambuc if (buf) {
157392395e9cSLionel Sambuc poff = 0;
157492395e9cSLionel Sambuc eqn_read(&r->eqn, line, buf, offs, &poff);
157592395e9cSLionel Sambuc }
157692395e9cSLionel Sambuc }
157792395e9cSLionel Sambuc
157892395e9cSLionel Sambuc /* ARGSUSED */
157992395e9cSLionel Sambuc static enum rofferr
roff_EQ(ROFF_ARGS)158092395e9cSLionel Sambuc roff_EQ(ROFF_ARGS)
158192395e9cSLionel Sambuc {
158292395e9cSLionel Sambuc
158392395e9cSLionel Sambuc roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
158492395e9cSLionel Sambuc return(ROFF_IGN);
158592395e9cSLionel Sambuc }
158692395e9cSLionel Sambuc
158792395e9cSLionel Sambuc /* ARGSUSED */
158892395e9cSLionel Sambuc static enum rofferr
roff_EN(ROFF_ARGS)158992395e9cSLionel Sambuc roff_EN(ROFF_ARGS)
159092395e9cSLionel Sambuc {
159192395e9cSLionel Sambuc
159292395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
159392395e9cSLionel Sambuc return(ROFF_IGN);
159492395e9cSLionel Sambuc }
159592395e9cSLionel Sambuc
1596d65f6f70SBen Gras /* ARGSUSED */
1597d65f6f70SBen Gras static enum rofferr
roff_TS(ROFF_ARGS)1598d65f6f70SBen Gras roff_TS(ROFF_ARGS)
1599d65f6f70SBen Gras {
1600*0a6a1f1dSLionel Sambuc struct tbl_node *tbl;
1601d65f6f70SBen Gras
1602d65f6f70SBen Gras if (r->tbl) {
160392395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
160492395e9cSLionel Sambuc tbl_end(&r->tbl);
1605d65f6f70SBen Gras }
1606d65f6f70SBen Gras
1607*0a6a1f1dSLionel Sambuc tbl = tbl_alloc(ppos, ln, r->parse);
1608d65f6f70SBen Gras
1609d65f6f70SBen Gras if (r->last_tbl)
1610*0a6a1f1dSLionel Sambuc r->last_tbl->next = tbl;
1611d65f6f70SBen Gras else
1612*0a6a1f1dSLionel Sambuc r->first_tbl = r->last_tbl = tbl;
1613d65f6f70SBen Gras
1614*0a6a1f1dSLionel Sambuc r->tbl = r->last_tbl = tbl;
1615*0a6a1f1dSLionel Sambuc return(ROFF_IGN);
1616*0a6a1f1dSLionel Sambuc }
1617*0a6a1f1dSLionel Sambuc
1618*0a6a1f1dSLionel Sambuc /* ARGSUSED */
1619*0a6a1f1dSLionel Sambuc static enum rofferr
roff_cc(ROFF_ARGS)1620*0a6a1f1dSLionel Sambuc roff_cc(ROFF_ARGS)
1621*0a6a1f1dSLionel Sambuc {
1622*0a6a1f1dSLionel Sambuc const char *p;
1623*0a6a1f1dSLionel Sambuc
1624*0a6a1f1dSLionel Sambuc p = *bufp + pos;
1625*0a6a1f1dSLionel Sambuc
1626*0a6a1f1dSLionel Sambuc if ('\0' == *p || '.' == (r->control = *p++))
1627*0a6a1f1dSLionel Sambuc r->control = 0;
1628*0a6a1f1dSLionel Sambuc
1629*0a6a1f1dSLionel Sambuc if ('\0' != *p)
1630*0a6a1f1dSLionel Sambuc mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1631*0a6a1f1dSLionel Sambuc
1632d65f6f70SBen Gras return(ROFF_IGN);
1633d65f6f70SBen Gras }
1634d65f6f70SBen Gras
1635d65f6f70SBen Gras /* ARGSUSED */
1636d65f6f70SBen Gras static enum rofferr
roff_tr(ROFF_ARGS)163792395e9cSLionel Sambuc roff_tr(ROFF_ARGS)
163892395e9cSLionel Sambuc {
163992395e9cSLionel Sambuc const char *p, *first, *second;
164092395e9cSLionel Sambuc size_t fsz, ssz;
164192395e9cSLionel Sambuc enum mandoc_esc esc;
164292395e9cSLionel Sambuc
164392395e9cSLionel Sambuc p = *bufp + pos;
164492395e9cSLionel Sambuc
164592395e9cSLionel Sambuc if ('\0' == *p) {
164692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
164792395e9cSLionel Sambuc return(ROFF_IGN);
164892395e9cSLionel Sambuc }
164992395e9cSLionel Sambuc
165092395e9cSLionel Sambuc while ('\0' != *p) {
165192395e9cSLionel Sambuc fsz = ssz = 1;
165292395e9cSLionel Sambuc
165392395e9cSLionel Sambuc first = p++;
165492395e9cSLionel Sambuc if ('\\' == *first) {
165592395e9cSLionel Sambuc esc = mandoc_escape(&p, NULL, NULL);
165692395e9cSLionel Sambuc if (ESCAPE_ERROR == esc) {
165792395e9cSLionel Sambuc mandoc_msg
165892395e9cSLionel Sambuc (MANDOCERR_BADESCAPE, r->parse,
165992395e9cSLionel Sambuc ln, (int)(p - *bufp), NULL);
166092395e9cSLionel Sambuc return(ROFF_IGN);
166192395e9cSLionel Sambuc }
166292395e9cSLionel Sambuc fsz = (size_t)(p - first);
166392395e9cSLionel Sambuc }
166492395e9cSLionel Sambuc
166592395e9cSLionel Sambuc second = p++;
166692395e9cSLionel Sambuc if ('\\' == *second) {
166792395e9cSLionel Sambuc esc = mandoc_escape(&p, NULL, NULL);
166892395e9cSLionel Sambuc if (ESCAPE_ERROR == esc) {
166992395e9cSLionel Sambuc mandoc_msg
167092395e9cSLionel Sambuc (MANDOCERR_BADESCAPE, r->parse,
167192395e9cSLionel Sambuc ln, (int)(p - *bufp), NULL);
167292395e9cSLionel Sambuc return(ROFF_IGN);
167392395e9cSLionel Sambuc }
167492395e9cSLionel Sambuc ssz = (size_t)(p - second);
167592395e9cSLionel Sambuc } else if ('\0' == *second) {
167692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
167792395e9cSLionel Sambuc ln, (int)(p - *bufp), NULL);
167892395e9cSLionel Sambuc second = " ";
167992395e9cSLionel Sambuc p--;
168092395e9cSLionel Sambuc }
168192395e9cSLionel Sambuc
168292395e9cSLionel Sambuc if (fsz > 1) {
168392395e9cSLionel Sambuc roff_setstrn(&r->xmbtab, first,
168492395e9cSLionel Sambuc fsz, second, ssz, 0);
168592395e9cSLionel Sambuc continue;
168692395e9cSLionel Sambuc }
168792395e9cSLionel Sambuc
168892395e9cSLionel Sambuc if (NULL == r->xtab)
168992395e9cSLionel Sambuc r->xtab = mandoc_calloc
169092395e9cSLionel Sambuc (128, sizeof(struct roffstr));
169192395e9cSLionel Sambuc
169292395e9cSLionel Sambuc free(r->xtab[(int)*first].p);
169392395e9cSLionel Sambuc r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
169492395e9cSLionel Sambuc r->xtab[(int)*first].sz = ssz;
169592395e9cSLionel Sambuc }
169692395e9cSLionel Sambuc
169792395e9cSLionel Sambuc return(ROFF_IGN);
169892395e9cSLionel Sambuc }
169992395e9cSLionel Sambuc
170092395e9cSLionel Sambuc /* ARGSUSED */
170192395e9cSLionel Sambuc static enum rofferr
roff_so(ROFF_ARGS)1702d65f6f70SBen Gras roff_so(ROFF_ARGS)
1703d65f6f70SBen Gras {
1704d65f6f70SBen Gras char *name;
1705d65f6f70SBen Gras
170692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1707d65f6f70SBen Gras
1708d65f6f70SBen Gras /*
1709d65f6f70SBen Gras * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1710d65f6f70SBen Gras * opening anything that's not in our cwd or anything beneath
1711d65f6f70SBen Gras * it. Thus, explicitly disallow traversing up the file-system
1712d65f6f70SBen Gras * or using absolute paths.
1713d65f6f70SBen Gras */
1714d65f6f70SBen Gras
1715d65f6f70SBen Gras name = *bufp + pos;
1716d65f6f70SBen Gras if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
171792395e9cSLionel Sambuc mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1718d65f6f70SBen Gras return(ROFF_ERR);
1719d65f6f70SBen Gras }
1720d65f6f70SBen Gras
1721d65f6f70SBen Gras *offs = pos;
1722d65f6f70SBen Gras return(ROFF_SO);
1723d65f6f70SBen Gras }
1724d65f6f70SBen Gras
1725d65f6f70SBen Gras /* ARGSUSED */
1726d65f6f70SBen Gras static enum rofferr
roff_userdef(ROFF_ARGS)1727d65f6f70SBen Gras roff_userdef(ROFF_ARGS)
1728d65f6f70SBen Gras {
1729d65f6f70SBen Gras const char *arg[9];
1730d65f6f70SBen Gras char *cp, *n1, *n2;
1731d65f6f70SBen Gras int i;
1732d65f6f70SBen Gras
1733d65f6f70SBen Gras /*
1734d65f6f70SBen Gras * Collect pointers to macro argument strings
1735*0a6a1f1dSLionel Sambuc * and NUL-terminate them.
1736d65f6f70SBen Gras */
1737d65f6f70SBen Gras cp = *bufp + pos;
1738d65f6f70SBen Gras for (i = 0; i < 9; i++)
1739d65f6f70SBen Gras arg[i] = '\0' == *cp ? "" :
174092395e9cSLionel Sambuc mandoc_getarg(r->parse, &cp, ln, &pos);
1741d65f6f70SBen Gras
1742d65f6f70SBen Gras /*
1743d65f6f70SBen Gras * Expand macro arguments.
1744d65f6f70SBen Gras */
1745d65f6f70SBen Gras *szp = 0;
1746d65f6f70SBen Gras n1 = cp = mandoc_strdup(r->current_string);
1747d65f6f70SBen Gras while (NULL != (cp = strstr(cp, "\\$"))) {
1748d65f6f70SBen Gras i = cp[2] - '1';
1749d65f6f70SBen Gras if (0 > i || 8 < i) {
1750d65f6f70SBen Gras /* Not an argument invocation. */
1751d65f6f70SBen Gras cp += 2;
1752d65f6f70SBen Gras continue;
1753d65f6f70SBen Gras }
1754d65f6f70SBen Gras
1755d65f6f70SBen Gras *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1756d65f6f70SBen Gras n2 = mandoc_malloc(*szp);
1757d65f6f70SBen Gras
1758d65f6f70SBen Gras strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1759d65f6f70SBen Gras strlcat(n2, arg[i], *szp);
1760d65f6f70SBen Gras strlcat(n2, cp + 3, *szp);
1761d65f6f70SBen Gras
1762d65f6f70SBen Gras cp = n2 + (cp - n1);
1763d65f6f70SBen Gras free(n1);
1764d65f6f70SBen Gras n1 = n2;
1765d65f6f70SBen Gras }
1766d65f6f70SBen Gras
1767d65f6f70SBen Gras /*
1768d65f6f70SBen Gras * Replace the macro invocation
1769d65f6f70SBen Gras * by the expanded macro.
1770d65f6f70SBen Gras */
1771d65f6f70SBen Gras free(*bufp);
1772d65f6f70SBen Gras *bufp = n1;
1773d65f6f70SBen Gras if (0 == *szp)
1774d65f6f70SBen Gras *szp = strlen(*bufp) + 1;
1775d65f6f70SBen Gras
1776d65f6f70SBen Gras return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1777d65f6f70SBen Gras ROFF_REPARSE : ROFF_APPEND);
1778d65f6f70SBen Gras }
1779d65f6f70SBen Gras
178092395e9cSLionel Sambuc static char *
roff_getname(struct roff * r,char ** cpp,int ln,int pos)178192395e9cSLionel Sambuc roff_getname(struct roff *r, char **cpp, int ln, int pos)
178292395e9cSLionel Sambuc {
178392395e9cSLionel Sambuc char *name, *cp;
178492395e9cSLionel Sambuc
178592395e9cSLionel Sambuc name = *cpp;
178692395e9cSLionel Sambuc if ('\0' == *name)
178792395e9cSLionel Sambuc return(name);
178892395e9cSLionel Sambuc
178992395e9cSLionel Sambuc /* Read until end of name. */
179092395e9cSLionel Sambuc for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
179192395e9cSLionel Sambuc if ('\\' != *cp)
179292395e9cSLionel Sambuc continue;
179392395e9cSLionel Sambuc cp++;
179492395e9cSLionel Sambuc if ('\\' == *cp)
179592395e9cSLionel Sambuc continue;
179692395e9cSLionel Sambuc mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
179792395e9cSLionel Sambuc *cp = '\0';
179892395e9cSLionel Sambuc name = cp;
179992395e9cSLionel Sambuc }
180092395e9cSLionel Sambuc
180192395e9cSLionel Sambuc /* Nil-terminate name. */
180292395e9cSLionel Sambuc if ('\0' != *cp)
180392395e9cSLionel Sambuc *(cp++) = '\0';
180492395e9cSLionel Sambuc
180592395e9cSLionel Sambuc /* Read past spaces. */
180692395e9cSLionel Sambuc while (' ' == *cp)
180792395e9cSLionel Sambuc cp++;
180892395e9cSLionel Sambuc
180992395e9cSLionel Sambuc *cpp = cp;
181092395e9cSLionel Sambuc return(name);
181192395e9cSLionel Sambuc }
181292395e9cSLionel Sambuc
1813d65f6f70SBen Gras /*
1814d65f6f70SBen Gras * Store *string into the user-defined string called *name.
1815d65f6f70SBen Gras * In multiline mode, append to an existing entry and append '\n';
1816d65f6f70SBen Gras * else replace the existing entry, if there is one.
1817d65f6f70SBen Gras * To clear an existing entry, call with (*r, *name, NULL, 0).
1818d65f6f70SBen Gras */
1819d65f6f70SBen Gras static void
roff_setstr(struct roff * r,const char * name,const char * string,int multiline)1820d65f6f70SBen Gras roff_setstr(struct roff *r, const char *name, const char *string,
1821d65f6f70SBen Gras int multiline)
1822d65f6f70SBen Gras {
182392395e9cSLionel Sambuc
182492395e9cSLionel Sambuc roff_setstrn(&r->strtab, name, strlen(name), string,
182592395e9cSLionel Sambuc string ? strlen(string) : 0, multiline);
182692395e9cSLionel Sambuc }
182792395e9cSLionel Sambuc
182892395e9cSLionel Sambuc static void
roff_setstrn(struct roffkv ** r,const char * name,size_t namesz,const char * string,size_t stringsz,int multiline)182992395e9cSLionel Sambuc roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
183092395e9cSLionel Sambuc const char *string, size_t stringsz, int multiline)
183192395e9cSLionel Sambuc {
183292395e9cSLionel Sambuc struct roffkv *n;
1833d65f6f70SBen Gras char *c;
183492395e9cSLionel Sambuc int i;
1835d65f6f70SBen Gras size_t oldch, newch;
1836d65f6f70SBen Gras
1837d65f6f70SBen Gras /* Search for an existing string with the same name. */
183892395e9cSLionel Sambuc n = *r;
183992395e9cSLionel Sambuc
184092395e9cSLionel Sambuc while (n && strcmp(name, n->key.p))
1841d65f6f70SBen Gras n = n->next;
1842d65f6f70SBen Gras
1843d65f6f70SBen Gras if (NULL == n) {
1844d65f6f70SBen Gras /* Create a new string table entry. */
184592395e9cSLionel Sambuc n = mandoc_malloc(sizeof(struct roffkv));
184692395e9cSLionel Sambuc n->key.p = mandoc_strndup(name, namesz);
184792395e9cSLionel Sambuc n->key.sz = namesz;
184892395e9cSLionel Sambuc n->val.p = NULL;
184992395e9cSLionel Sambuc n->val.sz = 0;
185092395e9cSLionel Sambuc n->next = *r;
185192395e9cSLionel Sambuc *r = n;
1852d65f6f70SBen Gras } else if (0 == multiline) {
1853d65f6f70SBen Gras /* In multiline mode, append; else replace. */
185492395e9cSLionel Sambuc free(n->val.p);
185592395e9cSLionel Sambuc n->val.p = NULL;
185692395e9cSLionel Sambuc n->val.sz = 0;
1857d65f6f70SBen Gras }
1858d65f6f70SBen Gras
1859d65f6f70SBen Gras if (NULL == string)
1860d65f6f70SBen Gras return;
1861d65f6f70SBen Gras
1862d65f6f70SBen Gras /*
1863d65f6f70SBen Gras * One additional byte for the '\n' in multiline mode,
1864d65f6f70SBen Gras * and one for the terminating '\0'.
1865d65f6f70SBen Gras */
186692395e9cSLionel Sambuc newch = stringsz + (multiline ? 2u : 1u);
186792395e9cSLionel Sambuc
186892395e9cSLionel Sambuc if (NULL == n->val.p) {
186992395e9cSLionel Sambuc n->val.p = mandoc_malloc(newch);
187092395e9cSLionel Sambuc *n->val.p = '\0';
1871d65f6f70SBen Gras oldch = 0;
1872d65f6f70SBen Gras } else {
187392395e9cSLionel Sambuc oldch = n->val.sz;
187492395e9cSLionel Sambuc n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1875d65f6f70SBen Gras }
1876d65f6f70SBen Gras
1877d65f6f70SBen Gras /* Skip existing content in the destination buffer. */
187892395e9cSLionel Sambuc c = n->val.p + (int)oldch;
1879d65f6f70SBen Gras
1880d65f6f70SBen Gras /* Append new content to the destination buffer. */
188192395e9cSLionel Sambuc i = 0;
188292395e9cSLionel Sambuc while (i < (int)stringsz) {
1883d65f6f70SBen Gras /*
1884d65f6f70SBen Gras * Rudimentary roff copy mode:
1885d65f6f70SBen Gras * Handle escaped backslashes.
1886d65f6f70SBen Gras */
188792395e9cSLionel Sambuc if ('\\' == string[i] && '\\' == string[i + 1])
188892395e9cSLionel Sambuc i++;
188992395e9cSLionel Sambuc *c++ = string[i++];
1890d65f6f70SBen Gras }
1891d65f6f70SBen Gras
1892d65f6f70SBen Gras /* Append terminating bytes. */
1893d65f6f70SBen Gras if (multiline)
1894d65f6f70SBen Gras *c++ = '\n';
1895d65f6f70SBen Gras
189692395e9cSLionel Sambuc *c = '\0';
189792395e9cSLionel Sambuc n->val.sz = (int)(c - n->val.p);
189892395e9cSLionel Sambuc }
1899d65f6f70SBen Gras
1900d65f6f70SBen Gras static const char *
roff_getstrn(const struct roff * r,const char * name,size_t len)1901d65f6f70SBen Gras roff_getstrn(const struct roff *r, const char *name, size_t len)
1902d65f6f70SBen Gras {
190392395e9cSLionel Sambuc const struct roffkv *n;
1904d65f6f70SBen Gras
190592395e9cSLionel Sambuc for (n = r->strtab; n; n = n->next)
190692395e9cSLionel Sambuc if (0 == strncmp(name, n->key.p, len) &&
190792395e9cSLionel Sambuc '\0' == n->key.p[(int)len])
190892395e9cSLionel Sambuc return(n->val.p);
1909d65f6f70SBen Gras
191092395e9cSLionel Sambuc return(NULL);
1911d65f6f70SBen Gras }
1912d65f6f70SBen Gras
1913d65f6f70SBen Gras static void
roff_freestr(struct roffkv * r)191492395e9cSLionel Sambuc roff_freestr(struct roffkv *r)
1915d65f6f70SBen Gras {
191692395e9cSLionel Sambuc struct roffkv *n, *nn;
1917d65f6f70SBen Gras
191892395e9cSLionel Sambuc for (n = r; n; n = nn) {
191992395e9cSLionel Sambuc free(n->key.p);
192092395e9cSLionel Sambuc free(n->val.p);
1921d65f6f70SBen Gras nn = n->next;
1922d65f6f70SBen Gras free(n);
1923d65f6f70SBen Gras }
1924d65f6f70SBen Gras }
1925d65f6f70SBen Gras
1926d65f6f70SBen Gras const struct tbl_span *
roff_span(const struct roff * r)1927d65f6f70SBen Gras roff_span(const struct roff *r)
1928d65f6f70SBen Gras {
1929d65f6f70SBen Gras
1930d65f6f70SBen Gras return(r->tbl ? tbl_span(r->tbl) : NULL);
1931d65f6f70SBen Gras }
193292395e9cSLionel Sambuc
193392395e9cSLionel Sambuc const struct eqn *
roff_eqn(const struct roff * r)193492395e9cSLionel Sambuc roff_eqn(const struct roff *r)
193592395e9cSLionel Sambuc {
193692395e9cSLionel Sambuc
193792395e9cSLionel Sambuc return(r->last_eqn ? &r->last_eqn->eqn : NULL);
193892395e9cSLionel Sambuc }
193992395e9cSLionel Sambuc
194092395e9cSLionel Sambuc /*
194192395e9cSLionel Sambuc * Duplicate an input string, making the appropriate character
194292395e9cSLionel Sambuc * conversations (as stipulated by `tr') along the way.
194392395e9cSLionel Sambuc * Returns a heap-allocated string with all the replacements made.
194492395e9cSLionel Sambuc */
194592395e9cSLionel Sambuc char *
roff_strdup(const struct roff * r,const char * p)194692395e9cSLionel Sambuc roff_strdup(const struct roff *r, const char *p)
194792395e9cSLionel Sambuc {
194892395e9cSLionel Sambuc const struct roffkv *cp;
194992395e9cSLionel Sambuc char *res;
195092395e9cSLionel Sambuc const char *pp;
195192395e9cSLionel Sambuc size_t ssz, sz;
195292395e9cSLionel Sambuc enum mandoc_esc esc;
195392395e9cSLionel Sambuc
195492395e9cSLionel Sambuc if (NULL == r->xmbtab && NULL == r->xtab)
195592395e9cSLionel Sambuc return(mandoc_strdup(p));
195692395e9cSLionel Sambuc else if ('\0' == *p)
195792395e9cSLionel Sambuc return(mandoc_strdup(""));
195892395e9cSLionel Sambuc
195992395e9cSLionel Sambuc /*
196092395e9cSLionel Sambuc * Step through each character looking for term matches
196192395e9cSLionel Sambuc * (remember that a `tr' can be invoked with an escape, which is
196292395e9cSLionel Sambuc * a glyph but the escape is multi-character).
196392395e9cSLionel Sambuc * We only do this if the character hash has been initialised
196492395e9cSLionel Sambuc * and the string is >0 length.
196592395e9cSLionel Sambuc */
196692395e9cSLionel Sambuc
196792395e9cSLionel Sambuc res = NULL;
196892395e9cSLionel Sambuc ssz = 0;
196992395e9cSLionel Sambuc
197092395e9cSLionel Sambuc while ('\0' != *p) {
197192395e9cSLionel Sambuc if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
197292395e9cSLionel Sambuc sz = r->xtab[(int)*p].sz;
197392395e9cSLionel Sambuc res = mandoc_realloc(res, ssz + sz + 1);
197492395e9cSLionel Sambuc memcpy(res + ssz, r->xtab[(int)*p].p, sz);
197592395e9cSLionel Sambuc ssz += sz;
197692395e9cSLionel Sambuc p++;
197792395e9cSLionel Sambuc continue;
197892395e9cSLionel Sambuc } else if ('\\' != *p) {
197992395e9cSLionel Sambuc res = mandoc_realloc(res, ssz + 2);
198092395e9cSLionel Sambuc res[ssz++] = *p++;
198192395e9cSLionel Sambuc continue;
198292395e9cSLionel Sambuc }
198392395e9cSLionel Sambuc
198492395e9cSLionel Sambuc /* Search for term matches. */
198592395e9cSLionel Sambuc for (cp = r->xmbtab; cp; cp = cp->next)
198692395e9cSLionel Sambuc if (0 == strncmp(p, cp->key.p, cp->key.sz))
198792395e9cSLionel Sambuc break;
198892395e9cSLionel Sambuc
198992395e9cSLionel Sambuc if (NULL != cp) {
199092395e9cSLionel Sambuc /*
199192395e9cSLionel Sambuc * A match has been found.
199292395e9cSLionel Sambuc * Append the match to the array and move
199392395e9cSLionel Sambuc * forward by its keysize.
199492395e9cSLionel Sambuc */
199592395e9cSLionel Sambuc res = mandoc_realloc
199692395e9cSLionel Sambuc (res, ssz + cp->val.sz + 1);
199792395e9cSLionel Sambuc memcpy(res + ssz, cp->val.p, cp->val.sz);
199892395e9cSLionel Sambuc ssz += cp->val.sz;
199992395e9cSLionel Sambuc p += (int)cp->key.sz;
200092395e9cSLionel Sambuc continue;
200192395e9cSLionel Sambuc }
200292395e9cSLionel Sambuc
200392395e9cSLionel Sambuc /*
200492395e9cSLionel Sambuc * Handle escapes carefully: we need to copy
200592395e9cSLionel Sambuc * over just the escape itself, or else we might
200692395e9cSLionel Sambuc * do replacements within the escape itself.
200792395e9cSLionel Sambuc * Make sure to pass along the bogus string.
200892395e9cSLionel Sambuc */
200992395e9cSLionel Sambuc pp = p++;
201092395e9cSLionel Sambuc esc = mandoc_escape(&p, NULL, NULL);
201192395e9cSLionel Sambuc if (ESCAPE_ERROR == esc) {
201292395e9cSLionel Sambuc sz = strlen(pp);
201392395e9cSLionel Sambuc res = mandoc_realloc(res, ssz + sz + 1);
201492395e9cSLionel Sambuc memcpy(res + ssz, pp, sz);
201592395e9cSLionel Sambuc break;
201692395e9cSLionel Sambuc }
201792395e9cSLionel Sambuc /*
201892395e9cSLionel Sambuc * We bail out on bad escapes.
201992395e9cSLionel Sambuc * No need to warn: we already did so when
202092395e9cSLionel Sambuc * roff_res() was called.
202192395e9cSLionel Sambuc */
202292395e9cSLionel Sambuc sz = (int)(p - pp);
202392395e9cSLionel Sambuc res = mandoc_realloc(res, ssz + sz + 1);
202492395e9cSLionel Sambuc memcpy(res + ssz, pp, sz);
202592395e9cSLionel Sambuc ssz += sz;
202692395e9cSLionel Sambuc }
202792395e9cSLionel Sambuc
202892395e9cSLionel Sambuc res[(int)ssz] = '\0';
202992395e9cSLionel Sambuc return(res);
203092395e9cSLionel Sambuc }
2031*0a6a1f1dSLionel Sambuc
2032*0a6a1f1dSLionel Sambuc /*
2033*0a6a1f1dSLionel Sambuc * Find out whether a line is a macro line or not.
2034*0a6a1f1dSLionel Sambuc * If it is, adjust the current position and return one; if it isn't,
2035*0a6a1f1dSLionel Sambuc * return zero and don't change the current position.
2036*0a6a1f1dSLionel Sambuc * If the control character has been set with `.cc', then let that grain
2037*0a6a1f1dSLionel Sambuc * precedence.
2038*0a6a1f1dSLionel Sambuc * This is slighly contrary to groff, where using the non-breaking
2039*0a6a1f1dSLionel Sambuc * control character when `cc' has been invoked will cause the
2040*0a6a1f1dSLionel Sambuc * non-breaking macro contents to be printed verbatim.
2041*0a6a1f1dSLionel Sambuc */
2042*0a6a1f1dSLionel Sambuc int
roff_getcontrol(const struct roff * r,const char * cp,int * ppos)2043*0a6a1f1dSLionel Sambuc roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
2044*0a6a1f1dSLionel Sambuc {
2045*0a6a1f1dSLionel Sambuc int pos;
2046*0a6a1f1dSLionel Sambuc
2047*0a6a1f1dSLionel Sambuc pos = *ppos;
2048*0a6a1f1dSLionel Sambuc
2049*0a6a1f1dSLionel Sambuc if (0 != r->control && cp[pos] == r->control)
2050*0a6a1f1dSLionel Sambuc pos++;
2051*0a6a1f1dSLionel Sambuc else if (0 != r->control)
2052*0a6a1f1dSLionel Sambuc return(0);
2053*0a6a1f1dSLionel Sambuc else if ('\\' == cp[pos] && '.' == cp[pos + 1])
2054*0a6a1f1dSLionel Sambuc pos += 2;
2055*0a6a1f1dSLionel Sambuc else if ('.' == cp[pos] || '\'' == cp[pos])
2056*0a6a1f1dSLionel Sambuc pos++;
2057*0a6a1f1dSLionel Sambuc else
2058*0a6a1f1dSLionel Sambuc return(0);
2059*0a6a1f1dSLionel Sambuc
2060*0a6a1f1dSLionel Sambuc while (' ' == cp[pos] || '\t' == cp[pos])
2061*0a6a1f1dSLionel Sambuc pos++;
2062*0a6a1f1dSLionel Sambuc
2063*0a6a1f1dSLionel Sambuc *ppos = pos;
2064*0a6a1f1dSLionel Sambuc return(1);
2065*0a6a1f1dSLionel Sambuc }
2066