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