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