]> git.cameronkatri.com Git - mandoc.git/blob - roff.c
When a subsection header contains no letters but only special
[mandoc.git] / roff.c
1 /* $Id: roff.c,v 1.341 2018/08/25 16:53:39 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 /* Always check for the closing delimiter `\}'. */
2111
2112 while ((ep = strchr(ep, '\\')) != NULL) {
2113 switch (ep[1]) {
2114 case '}':
2115 memmove(ep, ep + 2, strlen(ep + 2) + 1);
2116 if (roff_ccond(r, ln, ep - buf->buf))
2117 irc |= endloop;
2118 break;
2119 case '\0':
2120 ++ep;
2121 break;
2122 default:
2123 ep += 2;
2124 break;
2125 }
2126 }
2127
2128 /*
2129 * Fully handle known macros when they are structurally
2130 * required or when the conditional evaluated to true.
2131 */
2132
2133 t = roff_parse(r, buf->buf, &pos, ln, ppos);
2134 irc |= t != TOKEN_NONE && (rr || roffs[t].flags & ROFFMAC_STRUCT) ?
2135 (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs) :
2136 rr ? ROFF_CONT : ROFF_IGN;
2137 return irc;
2138 }
2139
2140 static int
2141 roff_cond_text(ROFF_ARGS)
2142 {
2143 char *ep;
2144 int endloop, irc, rr;
2145
2146 irc = ROFF_IGN;
2147 rr = r->last->rule;
2148 endloop = tok != ROFF_while ? ROFF_IGN :
2149 rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
2150 if (roffnode_cleanscope(r))
2151 irc |= endloop;
2152
2153 ep = buf->buf + pos;
2154 while ((ep = strchr(ep, '\\')) != NULL) {
2155 if (*(++ep) == '}') {
2156 *ep = '&';
2157 if (roff_ccond(r, ln, ep - buf->buf - 1))
2158 irc |= endloop;
2159 }
2160 if (*ep != '\0')
2161 ++ep;
2162 }
2163 if (rr)
2164 irc |= ROFF_CONT;
2165 return irc;
2166 }
2167
2168 /* --- handling of numeric and conditional expressions -------------------- */
2169
2170 /*
2171 * Parse a single signed integer number. Stop at the first non-digit.
2172 * If there is at least one digit, return success and advance the
2173 * parse point, else return failure and let the parse point unchanged.
2174 * Ignore overflows, treat them just like the C language.
2175 */
2176 static int
2177 roff_getnum(const char *v, int *pos, int *res, int flags)
2178 {
2179 int myres, scaled, n, p;
2180
2181 if (NULL == res)
2182 res = &myres;
2183
2184 p = *pos;
2185 n = v[p] == '-';
2186 if (n || v[p] == '+')
2187 p++;
2188
2189 if (flags & ROFFNUM_WHITE)
2190 while (isspace((unsigned char)v[p]))
2191 p++;
2192
2193 for (*res = 0; isdigit((unsigned char)v[p]); p++)
2194 *res = 10 * *res + v[p] - '0';
2195 if (p == *pos + n)
2196 return 0;
2197
2198 if (n)
2199 *res = -*res;
2200
2201 /* Each number may be followed by one optional scaling unit. */
2202
2203 switch (v[p]) {
2204 case 'f':
2205 scaled = *res * 65536;
2206 break;
2207 case 'i':
2208 scaled = *res * 240;
2209 break;
2210 case 'c':
2211 scaled = *res * 240 / 2.54;
2212 break;
2213 case 'v':
2214 case 'P':
2215 scaled = *res * 40;
2216 break;
2217 case 'm':
2218 case 'n':
2219 scaled = *res * 24;
2220 break;
2221 case 'p':
2222 scaled = *res * 10 / 3;
2223 break;
2224 case 'u':
2225 scaled = *res;
2226 break;
2227 case 'M':
2228 scaled = *res * 6 / 25;
2229 break;
2230 default:
2231 scaled = *res;
2232 p--;
2233 break;
2234 }
2235 if (flags & ROFFNUM_SCALE)
2236 *res = scaled;
2237
2238 *pos = p + 1;
2239 return 1;
2240 }
2241
2242 /*
2243 * Evaluate a string comparison condition.
2244 * The first character is the delimiter.
2245 * Succeed if the string up to its second occurrence
2246 * matches the string up to its third occurence.
2247 * Advance the cursor after the third occurrence
2248 * or lacking that, to the end of the line.
2249 */
2250 static int
2251 roff_evalstrcond(const char *v, int *pos)
2252 {
2253 const char *s1, *s2, *s3;
2254 int match;
2255
2256 match = 0;
2257 s1 = v + *pos; /* initial delimiter */
2258 s2 = s1 + 1; /* for scanning the first string */
2259 s3 = strchr(s2, *s1); /* for scanning the second string */
2260
2261 if (NULL == s3) /* found no middle delimiter */
2262 goto out;
2263
2264 while ('\0' != *++s3) {
2265 if (*s2 != *s3) { /* mismatch */
2266 s3 = strchr(s3, *s1);
2267 break;
2268 }
2269 if (*s3 == *s1) { /* found the final delimiter */
2270 match = 1;
2271 break;
2272 }
2273 s2++;
2274 }
2275
2276 out:
2277 if (NULL == s3)
2278 s3 = strchr(s2, '\0');
2279 else if (*s3 != '\0')
2280 s3++;
2281 *pos = s3 - v;
2282 return match;
2283 }
2284
2285 /*
2286 * Evaluate an optionally negated single character, numerical,
2287 * or string condition.
2288 */
2289 static int
2290 roff_evalcond(struct roff *r, int ln, char *v, int *pos)
2291 {
2292 const char *start, *end;
2293 char *cp, *name;
2294 size_t sz;
2295 int deftype, len, number, savepos, istrue, wanttrue;
2296
2297 if ('!' == v[*pos]) {
2298 wanttrue = 0;
2299 (*pos)++;
2300 } else
2301 wanttrue = 1;
2302
2303 switch (v[*pos]) {
2304 case '\0':
2305 return 0;
2306 case 'n':
2307 case 'o':
2308 (*pos)++;
2309 return wanttrue;
2310 case 'e':
2311 case 't':
2312 case 'v':
2313 (*pos)++;
2314 return !wanttrue;
2315 case 'c':
2316 do {
2317 (*pos)++;
2318 } while (v[*pos] == ' ');
2319
2320 /*
2321 * Quirk for groff compatibility:
2322 * The horizontal tab is neither available nor unavailable.
2323 */
2324
2325 if (v[*pos] == '\t') {
2326 (*pos)++;
2327 return 0;
2328 }
2329
2330 /* Printable ASCII characters are available. */
2331
2332 if (v[*pos] != '\\') {
2333 (*pos)++;
2334 return wanttrue;
2335 }
2336
2337 end = v + ++*pos;
2338 switch (mandoc_escape(&end, &start, &len)) {
2339 case ESCAPE_SPECIAL:
2340 istrue = mchars_spec2cp(start, len) != -1;
2341 break;
2342 case ESCAPE_UNICODE:
2343 istrue = 1;
2344 break;
2345 case ESCAPE_NUMBERED:
2346 istrue = mchars_num2char(start, len) != -1;
2347 break;
2348 default:
2349 istrue = !wanttrue;
2350 break;
2351 }
2352 *pos = end - v;
2353 return istrue == wanttrue;
2354 case 'd':
2355 case 'r':
2356 cp = v + *pos + 1;
2357 while (*cp == ' ')
2358 cp++;
2359 name = cp;
2360 sz = roff_getname(r, &cp, ln, cp - v);
2361 if (sz == 0)
2362 istrue = 0;
2363 else if (v[*pos] == 'r')
2364 istrue = roff_hasregn(r, name, sz);
2365 else {
2366 deftype = ROFFDEF_ANY;
2367 roff_getstrn(r, name, sz, &deftype);
2368 istrue = !!deftype;
2369 }
2370 *pos = cp - v;
2371 return istrue == wanttrue;
2372 default:
2373 break;
2374 }
2375
2376 savepos = *pos;
2377 if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
2378 return (number > 0) == wanttrue;
2379 else if (*pos == savepos)
2380 return roff_evalstrcond(v, pos) == wanttrue;
2381 else
2382 return 0;
2383 }
2384
2385 static int
2386 roff_line_ignore(ROFF_ARGS)
2387 {
2388
2389 return ROFF_IGN;
2390 }
2391
2392 static int
2393 roff_insec(ROFF_ARGS)
2394 {
2395
2396 mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
2397 ln, ppos, roff_name[tok]);
2398 return ROFF_IGN;
2399 }
2400
2401 static int
2402 roff_unsupp(ROFF_ARGS)
2403 {
2404
2405 mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
2406 ln, ppos, roff_name[tok]);
2407 return ROFF_IGN;
2408 }
2409
2410 static int
2411 roff_cond(ROFF_ARGS)
2412 {
2413 int irc;
2414
2415 roffnode_push(r, tok, NULL, ln, ppos);
2416
2417 /*
2418 * An `.el' has no conditional body: it will consume the value
2419 * of the current rstack entry set in prior `ie' calls or
2420 * defaults to DENY.
2421 *
2422 * If we're not an `el', however, then evaluate the conditional.
2423 */
2424
2425 r->last->rule = tok == ROFF_el ?
2426 (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
2427 roff_evalcond(r, ln, buf->buf, &pos);
2428
2429 /*
2430 * An if-else will put the NEGATION of the current evaluated
2431 * conditional into the stack of rules.
2432 */
2433
2434 if (tok == ROFF_ie) {
2435 if (r->rstackpos + 1 == r->rstacksz) {
2436 r->rstacksz += 16;
2437 r->rstack = mandoc_reallocarray(r->rstack,
2438 r->rstacksz, sizeof(int));
2439 }
2440 r->rstack[++r->rstackpos] = !r->last->rule;
2441 }
2442
2443 /* If the parent has false as its rule, then so do we. */
2444
2445 if (r->last->parent && !r->last->parent->rule)
2446 r->last->rule = 0;
2447
2448 /*
2449 * Determine scope.
2450 * If there is nothing on the line after the conditional,
2451 * not even whitespace, use next-line scope.
2452 * Except that .while does not support next-line scope.
2453 */
2454
2455 if (buf->buf[pos] == '\0' && tok != ROFF_while) {
2456 r->last->endspan = 2;
2457 goto out;
2458 }
2459
2460 while (buf->buf[pos] == ' ')
2461 pos++;
2462
2463 /* An opening brace requests multiline scope. */
2464
2465 if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
2466 r->last->endspan = -1;
2467 pos += 2;
2468 while (buf->buf[pos] == ' ')
2469 pos++;
2470 goto out;
2471 }
2472
2473 /*
2474 * Anything else following the conditional causes
2475 * single-line scope. Warn if the scope contains
2476 * nothing but trailing whitespace.
2477 */
2478
2479 if (buf->buf[pos] == '\0')
2480 mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
2481 ln, ppos, roff_name[tok]);
2482
2483 r->last->endspan = 1;
2484
2485 out:
2486 *offs = pos;
2487 irc = ROFF_RERUN;
2488 if (tok == ROFF_while)
2489 irc |= ROFF_WHILE;
2490 return irc;
2491 }
2492
2493 static int
2494 roff_ds(ROFF_ARGS)
2495 {
2496 char *string;
2497 const char *name;
2498 size_t namesz;
2499
2500 /* Ignore groff compatibility mode for now. */
2501
2502 if (tok == ROFF_ds1)
2503 tok = ROFF_ds;
2504 else if (tok == ROFF_as1)
2505 tok = ROFF_as;
2506
2507 /*
2508 * The first word is the name of the string.
2509 * If it is empty or terminated by an escape sequence,
2510 * abort the `ds' request without defining anything.
2511 */
2512
2513 name = string = buf->buf + pos;
2514 if (*name == '\0')
2515 return ROFF_IGN;
2516
2517 namesz = roff_getname(r, &string, ln, pos);
2518 if (name[namesz] == '\\')
2519 return ROFF_IGN;
2520
2521 /* Read past the initial double-quote, if any. */
2522 if (*string == '"')
2523 string++;
2524
2525 /* The rest is the value. */
2526 roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2527 ROFF_as == tok);
2528 roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
2529 return ROFF_IGN;
2530 }
2531
2532 /*
2533 * Parse a single operator, one or two characters long.
2534 * If the operator is recognized, return success and advance the
2535 * parse point, else return failure and let the parse point unchanged.
2536 */
2537 static int
2538 roff_getop(const char *v, int *pos, char *res)
2539 {
2540
2541 *res = v[*pos];
2542
2543 switch (*res) {
2544 case '+':
2545 case '-':
2546 case '*':
2547 case '/':
2548 case '%':
2549 case '&':
2550 case ':':
2551 break;
2552 case '<':
2553 switch (v[*pos + 1]) {
2554 case '=':
2555 *res = 'l';
2556 (*pos)++;
2557 break;
2558 case '>':
2559 *res = '!';
2560 (*pos)++;
2561 break;
2562 case '?':
2563 *res = 'i';
2564 (*pos)++;
2565 break;
2566 default:
2567 break;
2568 }
2569 break;
2570 case '>':
2571 switch (v[*pos + 1]) {
2572 case '=':
2573 *res = 'g';
2574 (*pos)++;
2575 break;
2576 case '?':
2577 *res = 'a';
2578 (*pos)++;
2579 break;
2580 default:
2581 break;
2582 }
2583 break;
2584 case '=':
2585 if ('=' == v[*pos + 1])
2586 (*pos)++;
2587 break;
2588 default:
2589 return 0;
2590 }
2591 (*pos)++;
2592
2593 return *res;
2594 }
2595
2596 /*
2597 * Evaluate either a parenthesized numeric expression
2598 * or a single signed integer number.
2599 */
2600 static int
2601 roff_evalpar(struct roff *r, int ln,
2602 const char *v, int *pos, int *res, int flags)
2603 {
2604
2605 if ('(' != v[*pos])
2606 return roff_getnum(v, pos, res, flags);
2607
2608 (*pos)++;
2609 if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
2610 return 0;
2611
2612 /*
2613 * Omission of the closing parenthesis
2614 * is an error in validation mode,
2615 * but ignored in evaluation mode.
2616 */
2617
2618 if (')' == v[*pos])
2619 (*pos)++;
2620 else if (NULL == res)
2621 return 0;
2622
2623 return 1;
2624 }
2625
2626 /*
2627 * Evaluate a complete numeric expression.
2628 * Proceed left to right, there is no concept of precedence.
2629 */
2630 static int
2631 roff_evalnum(struct roff *r, int ln, const char *v,
2632 int *pos, int *res, int flags)
2633 {
2634 int mypos, operand2;
2635 char operator;
2636
2637 if (NULL == pos) {
2638 mypos = 0;
2639 pos = &mypos;
2640 }
2641
2642 if (flags & ROFFNUM_WHITE)
2643 while (isspace((unsigned char)v[*pos]))
2644 (*pos)++;
2645
2646 if ( ! roff_evalpar(r, ln, v, pos, res, flags))
2647 return 0;
2648
2649 while (1) {
2650 if (flags & ROFFNUM_WHITE)
2651 while (isspace((unsigned char)v[*pos]))
2652 (*pos)++;
2653
2654 if ( ! roff_getop(v, pos, &operator))
2655 break;
2656
2657 if (flags & ROFFNUM_WHITE)
2658 while (isspace((unsigned char)v[*pos]))
2659 (*pos)++;
2660
2661 if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
2662 return 0;
2663
2664 if (flags & ROFFNUM_WHITE)
2665 while (isspace((unsigned char)v[*pos]))
2666 (*pos)++;
2667
2668 if (NULL == res)
2669 continue;
2670
2671 switch (operator) {
2672 case '+':
2673 *res += operand2;
2674 break;
2675 case '-':
2676 *res -= operand2;
2677 break;
2678 case '*':
2679 *res *= operand2;
2680 break;
2681 case '/':
2682 if (operand2 == 0) {
2683 mandoc_msg(MANDOCERR_DIVZERO,
2684 r->parse, ln, *pos, v);
2685 *res = 0;
2686 break;
2687 }
2688 *res /= operand2;
2689 break;
2690 case '%':
2691 if (operand2 == 0) {
2692 mandoc_msg(MANDOCERR_DIVZERO,
2693 r->parse, ln, *pos, v);
2694 *res = 0;
2695 break;
2696 }
2697 *res %= operand2;
2698 break;
2699 case '<':
2700 *res = *res < operand2;
2701 break;
2702 case '>':
2703 *res = *res > operand2;
2704 break;
2705 case 'l':
2706 *res = *res <= operand2;
2707 break;
2708 case 'g':
2709 *res = *res >= operand2;
2710 break;
2711 case '=':
2712 *res = *res == operand2;
2713 break;
2714 case '!':
2715 *res = *res != operand2;
2716 break;
2717 case '&':
2718 *res = *res && operand2;
2719 break;
2720 case ':':
2721 *res = *res || operand2;
2722 break;
2723 case 'i':
2724 if (operand2 < *res)
2725 *res = operand2;
2726 break;
2727 case 'a':
2728 if (operand2 > *res)
2729 *res = operand2;
2730 break;
2731 default:
2732 abort();
2733 }
2734 }
2735 return 1;
2736 }
2737
2738 /* --- register management ------------------------------------------------ */
2739
2740 void
2741 roff_setreg(struct roff *r, const char *name, int val, char sign)
2742 {
2743 roff_setregn(r, name, strlen(name), val, sign, INT_MIN);
2744 }
2745
2746 static void
2747 roff_setregn(struct roff *r, const char *name, size_t len,
2748 int val, char sign, int step)
2749 {
2750 struct roffreg *reg;
2751
2752 /* Search for an existing register with the same name. */
2753 reg = r->regtab;
2754
2755 while (reg != NULL && (reg->key.sz != len ||
2756 strncmp(reg->key.p, name, len) != 0))
2757 reg = reg->next;
2758
2759 if (NULL == reg) {
2760 /* Create a new register. */
2761 reg = mandoc_malloc(sizeof(struct roffreg));
2762 reg->key.p = mandoc_strndup(name, len);
2763 reg->key.sz = len;
2764 reg->val = 0;
2765 reg->step = 0;
2766 reg->next = r->regtab;
2767 r->regtab = reg;
2768 }
2769
2770 if ('+' == sign)
2771 reg->val += val;
2772 else if ('-' == sign)
2773 reg->val -= val;
2774 else
2775 reg->val = val;
2776 if (step != INT_MIN)
2777 reg->step = step;
2778 }
2779
2780 /*
2781 * Handle some predefined read-only number registers.
2782 * For now, return -1 if the requested register is not predefined;
2783 * in case a predefined read-only register having the value -1
2784 * were to turn up, another special value would have to be chosen.
2785 */
2786 static int
2787 roff_getregro(const struct roff *r, const char *name)
2788 {
2789
2790 switch (*name) {
2791 case '$': /* Number of arguments of the last macro evaluated. */
2792 return r->mstackpos < 0 ? 0 : r->mstack[r->mstackpos].argc;
2793 case 'A': /* ASCII approximation mode is always off. */
2794 return 0;
2795 case 'g': /* Groff compatibility mode is always on. */
2796 return 1;
2797 case 'H': /* Fixed horizontal resolution. */
2798 return 24;
2799 case 'j': /* Always adjust left margin only. */
2800 return 0;
2801 case 'T': /* Some output device is always defined. */
2802 return 1;
2803 case 'V': /* Fixed vertical resolution. */
2804 return 40;
2805 default:
2806 return -1;
2807 }
2808 }
2809
2810 int
2811 roff_getreg(struct roff *r, const char *name)
2812 {
2813 return roff_getregn(r, name, strlen(name), '\0');
2814 }
2815
2816 static int
2817 roff_getregn(struct roff *r, const char *name, size_t len, char sign)
2818 {
2819 struct roffreg *reg;
2820 int val;
2821
2822 if ('.' == name[0] && 2 == len) {
2823 val = roff_getregro(r, name + 1);
2824 if (-1 != val)
2825 return val;
2826 }
2827
2828 for (reg = r->regtab; reg; reg = reg->next) {
2829 if (len == reg->key.sz &&
2830 0 == strncmp(name, reg->key.p, len)) {
2831 switch (sign) {
2832 case '+':
2833 reg->val += reg->step;
2834 break;
2835 case '-':
2836 reg->val -= reg->step;
2837 break;
2838 default:
2839 break;
2840 }
2841 return reg->val;
2842 }
2843 }
2844
2845 roff_setregn(r, name, len, 0, '\0', INT_MIN);
2846 return 0;
2847 }
2848
2849 static int
2850 roff_hasregn(const struct roff *r, const char *name, size_t len)
2851 {
2852 struct roffreg *reg;
2853 int val;
2854
2855 if ('.' == name[0] && 2 == len) {
2856 val = roff_getregro(r, name + 1);
2857 if (-1 != val)
2858 return 1;
2859 }
2860
2861 for (reg = r->regtab; reg; reg = reg->next)
2862 if (len == reg->key.sz &&
2863 0 == strncmp(name, reg->key.p, len))
2864 return 1;
2865
2866 return 0;
2867 }
2868
2869 static void
2870 roff_freereg(struct roffreg *reg)
2871 {
2872 struct roffreg *old_reg;
2873
2874 while (NULL != reg) {
2875 free(reg->key.p);
2876 old_reg = reg;
2877 reg = reg->next;
2878 free(old_reg);
2879 }
2880 }
2881
2882 static int
2883 roff_nr(ROFF_ARGS)
2884 {
2885 char *key, *val, *step;
2886 size_t keysz;
2887 int iv, is, len;
2888 char sign;
2889
2890 key = val = buf->buf + pos;
2891 if (*key == '\0')
2892 return ROFF_IGN;
2893
2894 keysz = roff_getname(r, &val, ln, pos);
2895 if (key[keysz] == '\\')
2896 return ROFF_IGN;
2897
2898 sign = *val;
2899 if (sign == '+' || sign == '-')
2900 val++;
2901
2902 len = 0;
2903 if (roff_evalnum(r, ln, val, &len, &iv, ROFFNUM_SCALE) == 0)
2904 return ROFF_IGN;
2905
2906 step = val + len;
2907 while (isspace((unsigned char)*step))
2908 step++;
2909 if (roff_evalnum(r, ln, step, NULL, &is, 0) == 0)
2910 is = INT_MIN;
2911
2912 roff_setregn(r, key, keysz, iv, sign, is);
2913 return ROFF_IGN;
2914 }
2915
2916 static int
2917 roff_rr(ROFF_ARGS)
2918 {
2919 struct roffreg *reg, **prev;
2920 char *name, *cp;
2921 size_t namesz;
2922
2923 name = cp = buf->buf + pos;
2924 if (*name == '\0')
2925 return ROFF_IGN;
2926 namesz = roff_getname(r, &cp, ln, pos);
2927 name[namesz] = '\0';
2928
2929 prev = &r->regtab;
2930 while (1) {
2931 reg = *prev;
2932 if (reg == NULL || !strcmp(name, reg->key.p))
2933 break;
2934 prev = &reg->next;
2935 }
2936 if (reg != NULL) {
2937 *prev = reg->next;
2938 free(reg->key.p);
2939 free(reg);
2940 }
2941 return ROFF_IGN;
2942 }
2943
2944 /* --- handler functions for roff requests -------------------------------- */
2945
2946 static int
2947 roff_rm(ROFF_ARGS)
2948 {
2949 const char *name;
2950 char *cp;
2951 size_t namesz;
2952
2953 cp = buf->buf + pos;
2954 while (*cp != '\0') {
2955 name = cp;
2956 namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
2957 roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
2958 roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
2959 if (name[namesz] == '\\')
2960 break;
2961 }
2962 return ROFF_IGN;
2963 }
2964
2965 static int
2966 roff_it(ROFF_ARGS)
2967 {
2968 int iv;
2969
2970 /* Parse the number of lines. */
2971
2972 if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
2973 mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
2974 ln, ppos, buf->buf + 1);
2975 return ROFF_IGN;
2976 }
2977
2978 while (isspace((unsigned char)buf->buf[pos]))
2979 pos++;
2980
2981 /*
2982 * Arm the input line trap.
2983 * Special-casing "an-trap" is an ugly workaround to cope
2984 * with DocBook stupidly fiddling with man(7) internals.
2985 */
2986
2987 roffit_lines = iv;
2988 roffit_macro = mandoc_strdup(iv != 1 ||
2989 strcmp(buf->buf + pos, "an-trap") ?
2990 buf->buf + pos : "br");
2991 return ROFF_IGN;
2992 }
2993
2994 static int
2995 roff_Dd(ROFF_ARGS)
2996 {
2997 int mask;
2998 enum roff_tok t, te;
2999
3000 switch (tok) {
3001 case ROFF_Dd:
3002 tok = MDOC_Dd;
3003 te = MDOC_MAX;
3004 if (r->format == 0)
3005 r->format = MPARSE_MDOC;
3006 mask = MPARSE_MDOC | MPARSE_QUICK;
3007 break;
3008 case ROFF_TH:
3009 tok = MAN_TH;
3010 te = MAN_MAX;
3011 if (r->format == 0)
3012 r->format = MPARSE_MAN;
3013 mask = MPARSE_QUICK;
3014 break;
3015 default:
3016 abort();
3017 }
3018 if ((r->options & mask) == 0)
3019 for (t = tok; t < te; t++)
3020 roff_setstr(r, roff_name[t], NULL, 0);
3021 return ROFF_CONT;
3022 }
3023
3024 static int
3025 roff_TE(ROFF_ARGS)
3026 {
3027 if (r->tbl == NULL) {
3028 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
3029 ln, ppos, "TE");
3030 return ROFF_IGN;
3031 }
3032 if (tbl_end(r->tbl) == 0) {
3033 r->tbl = NULL;
3034 free(buf->buf);
3035 buf->buf = mandoc_strdup(".sp");
3036 buf->sz = 4;
3037 *offs = 0;
3038 return ROFF_REPARSE;
3039 }
3040 r->tbl = NULL;
3041 return ROFF_IGN;
3042 }
3043
3044 static int
3045 roff_T_(ROFF_ARGS)
3046 {
3047
3048 if (NULL == r->tbl)
3049 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
3050 ln, ppos, "T&");
3051 else
3052 tbl_restart(ln, ppos, r->tbl);
3053
3054 return ROFF_IGN;
3055 }
3056
3057 /*
3058 * Handle in-line equation delimiters.
3059 */
3060 static int
3061 roff_eqndelim(struct roff *r, struct buf *buf, int pos)
3062 {
3063 char *cp1, *cp2;
3064 const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
3065
3066 /*
3067 * Outside equations, look for an opening delimiter.
3068 * If we are inside an equation, we already know it is
3069 * in-line, or this function wouldn't have been called;
3070 * so look for a closing delimiter.
3071 */
3072
3073 cp1 = buf->buf + pos;
3074 cp2 = strchr(cp1, r->eqn == NULL ?
3075 r->last_eqn->odelim : r->last_eqn->cdelim);
3076 if (cp2 == NULL)
3077 return ROFF_CONT;
3078
3079 *cp2++ = '\0';
3080 bef_pr = bef_nl = aft_nl = aft_pr = "";
3081
3082 /* Handle preceding text, protecting whitespace. */
3083
3084 if (*buf->buf != '\0') {
3085 if (r->eqn == NULL)
3086 bef_pr = "\\&";
3087 bef_nl = "\n";
3088 }
3089
3090 /*
3091 * Prepare replacing the delimiter with an equation macro
3092 * and drop leading white space from the equation.
3093 */
3094
3095 if (r->eqn == NULL) {
3096 while (*cp2 == ' ')
3097 cp2++;
3098 mac = ".EQ";
3099 } else
3100 mac = ".EN";
3101
3102 /* Handle following text, protecting whitespace. */
3103
3104 if (*cp2 != '\0') {
3105 aft_nl = "\n";
3106 if (r->eqn != NULL)
3107 aft_pr = "\\&";
3108 }
3109
3110 /* Do the actual replacement. */
3111
3112 buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
3113 bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
3114 free(buf->buf);
3115 buf->buf = cp1;
3116
3117 /* Toggle the in-line state of the eqn subsystem. */
3118
3119 r->eqn_inline = r->eqn == NULL;
3120 return ROFF_REPARSE;
3121 }
3122
3123 static int
3124 roff_EQ(ROFF_ARGS)
3125 {
3126 struct roff_node *n;
3127
3128 if (r->man->macroset == MACROSET_MAN)
3129 man_breakscope(r->man, ROFF_EQ);
3130 n = roff_node_alloc(r->man, ln, ppos, ROFFT_EQN, TOKEN_NONE);
3131 if (ln > r->man->last->line)
3132 n->flags |= NODE_LINE;
3133 n->eqn = mandoc_calloc(1, sizeof(*n->eqn));
3134 n->eqn->expectargs = UINT_MAX;
3135 roff_node_append(r->man, n);
3136 r->man->next = ROFF_NEXT_SIBLING;
3137
3138 assert(r->eqn == NULL);
3139 if (r->last_eqn == NULL)
3140 r->last_eqn = eqn_alloc(r->parse);
3141 else
3142 eqn_reset(r->last_eqn);
3143 r->eqn = r->last_eqn;
3144 r->eqn->node = n;
3145
3146 if (buf->buf[pos] != '\0')
3147 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
3148 ".EQ %s", buf->buf + pos);
3149
3150 return ROFF_IGN;
3151 }
3152
3153 static int
3154 roff_EN(ROFF_ARGS)
3155 {
3156 if (r->eqn != NULL) {
3157 eqn_parse(r->eqn);
3158 r->eqn = NULL;
3159 } else
3160 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
3161 if (buf->buf[pos] != '\0')
3162 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
3163 "EN %s", buf->buf + pos);
3164 return ROFF_IGN;
3165 }
3166
3167 static int
3168 roff_TS(ROFF_ARGS)
3169 {
3170 if (r->tbl != NULL) {
3171 mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
3172 ln, ppos, "TS breaks TS");
3173 tbl_end(r->tbl);
3174 }
3175 r->tbl = tbl_alloc(ppos, ln, r->parse);
3176 if (r->last_tbl)
3177 r->last_tbl->next = r->tbl;
3178 else
3179 r->first_tbl = r->tbl;
3180 r->last_tbl = r->tbl;
3181 return ROFF_IGN;
3182 }
3183
3184 static int
3185 roff_onearg(ROFF_ARGS)
3186 {
3187 struct roff_node *n;
3188 char *cp;
3189 int npos;
3190
3191 if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
3192 (tok == ROFF_ce || tok == ROFF_rj || tok == ROFF_sp ||
3193 tok == ROFF_ti))
3194 man_breakscope(r->man, tok);
3195
3196 if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
3197 r->man->last = roffce_node;
3198 r->man->next = ROFF_NEXT_SIBLING;
3199 }
3200
3201 roff_elem_alloc(r->man, ln, ppos, tok);
3202 n = r->man->last;
3203
3204 cp = buf->buf + pos;
3205 if (*cp != '\0') {
3206 while (*cp != '\0' && *cp != ' ')
3207 cp++;
3208 while (*cp == ' ')
3209 *cp++ = '\0';
3210 if (*cp != '\0')
3211 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
3212 r->parse, ln, cp - buf->buf,
3213 "%s ... %s", roff_name[tok], cp);
3214 roff_word_alloc(r->man, ln, pos, buf->buf + pos);
3215 }
3216
3217 if (tok == ROFF_ce || tok == ROFF_rj) {
3218 if (r->man->last->type == ROFFT_ELEM) {
3219 roff_word_alloc(r->man, ln, pos, "1");
3220 r->man->last->flags |= NODE_NOSRC;
3221 }
3222 npos = 0;
3223 if (roff_evalnum(r, ln, r->man->last->string, &npos,
3224 &roffce_lines, 0) == 0) {
3225 mandoc_vmsg(MANDOCERR_CE_NONUM,
3226 r->parse, ln, pos, "ce %s", buf->buf + pos);
3227 roffce_lines = 1;
3228 }
3229 if (roffce_lines < 1) {
3230 r->man->last = r->man->last->parent;
3231 roffce_node = NULL;
3232 roffce_lines = 0;
3233 } else
3234 roffce_node = r->man->last->parent;
3235 } else {
3236 n->flags |= NODE_VALID | NODE_ENDED;
3237 r->man->last = n;
3238 }
3239 n->flags |= NODE_LINE;
3240 r->man->next = ROFF_NEXT_SIBLING;
3241 return ROFF_IGN;
3242 }
3243
3244 static int
3245 roff_manyarg(ROFF_ARGS)
3246 {
3247 struct roff_node *n;
3248 char *sp, *ep;
3249
3250 roff_elem_alloc(r->man, ln, ppos, tok);
3251 n = r->man->last;
3252
3253 for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
3254 while (*ep != '\0' && *ep != ' ')
3255 ep++;
3256 while (*ep == ' ')
3257 *ep++ = '\0';
3258 roff_word_alloc(r->man, ln, sp - buf->buf, sp);
3259 }
3260
3261 n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3262 r->man->last = n;
3263 r->man->next = ROFF_NEXT_SIBLING;
3264 return ROFF_IGN;
3265 }
3266
3267 static int
3268 roff_als(ROFF_ARGS)
3269 {
3270 char *oldn, *newn, *end, *value;
3271 size_t oldsz, newsz, valsz;
3272
3273 newn = oldn = buf->buf + pos;
3274 if (*newn == '\0')
3275 return ROFF_IGN;
3276
3277 newsz = roff_getname(r, &oldn, ln, pos);
3278 if (newn[newsz] == '\\' || *oldn == '\0')
3279 return ROFF_IGN;
3280
3281 end = oldn;
3282 oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
3283 if (oldsz == 0)
3284 return ROFF_IGN;
3285
3286 valsz = mandoc_asprintf(&value, ".%.*s \\$@\\\"\n",
3287 (int)oldsz, oldn);
3288 roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
3289 roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3290 free(value);
3291 return ROFF_IGN;
3292 }
3293
3294 static int
3295 roff_br(ROFF_ARGS)
3296 {
3297 if (r->man->flags & (MAN_BLINE | MAN_ELINE))
3298 man_breakscope(r->man, ROFF_br);
3299 roff_elem_alloc(r->man, ln, ppos, ROFF_br);
3300 if (buf->buf[pos] != '\0')
3301 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
3302 "%s %s", roff_name[tok], buf->buf + pos);
3303 r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3304 r->man->next = ROFF_NEXT_SIBLING;
3305 return ROFF_IGN;
3306 }
3307
3308 static int
3309 roff_cc(ROFF_ARGS)
3310 {
3311 const char *p;
3312
3313 p = buf->buf + pos;
3314
3315 if (*p == '\0' || (r->control = *p++) == '.')
3316 r->control = '\0';
3317
3318 if (*p != '\0')
3319 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
3320 ln, p - buf->buf, "cc ... %s", p);
3321
3322 return ROFF_IGN;
3323 }
3324
3325 static int
3326 roff_char(ROFF_ARGS)
3327 {
3328 const char *p, *kp, *vp;
3329 size_t ksz, vsz;
3330 int font;
3331
3332 /* Parse the character to be replaced. */
3333
3334 kp = buf->buf + pos;
3335 p = kp + 1;
3336 if (*kp == '\0' || (*kp == '\\' &&
3337 mandoc_escape(&p, NULL, NULL) != ESCAPE_SPECIAL) ||
3338 (*p != ' ' && *p != '\0')) {
3339 mandoc_vmsg(MANDOCERR_CHAR_ARG, r->parse,
3340 ln, pos, "char %s", kp);
3341 return ROFF_IGN;
3342 }
3343 ksz = p - kp;
3344 while (*p == ' ')
3345 p++;
3346
3347 /*
3348 * If the replacement string contains a font escape sequence,
3349 * we have to restore the font at the end.
3350 */
3351
3352 vp = p;
3353 vsz = strlen(p);
3354 font = 0;
3355 while (*p != '\0') {
3356 if (*p++ != '\\')
3357 continue;
3358 switch (mandoc_escape(&p, NULL, NULL)) {
3359 case ESCAPE_FONT:
3360 case ESCAPE_FONTROMAN:
3361 case ESCAPE_FONTITALIC:
3362 case ESCAPE_FONTBOLD:
3363 case ESCAPE_FONTBI:
3364 case ESCAPE_FONTPREV:
3365 font++;
3366 break;
3367 default:
3368 break;
3369 }
3370 }
3371 if (font > 1)
3372 mandoc_msg(MANDOCERR_CHAR_FONT, r->parse,
3373 ln, vp - buf->buf, vp);
3374
3375 /*
3376 * Approximate the effect of .char using the .tr tables.
3377 * XXX In groff, .char and .tr interact differently.
3378 */
3379
3380 if (ksz == 1) {
3381 if (r->xtab == NULL)
3382 r->xtab = mandoc_calloc(128, sizeof(*r->xtab));
3383 assert((unsigned int)*kp < 128);
3384 free(r->xtab[(int)*kp].p);
3385 r->xtab[(int)*kp].sz = mandoc_asprintf(&r->xtab[(int)*kp].p,
3386 "%s%s", vp, font ? "\fP" : "");
3387 } else {
3388 roff_setstrn(&r->xmbtab, kp, ksz, vp, vsz, 0);
3389 if (font)
3390 roff_setstrn(&r->xmbtab, kp, ksz, "\\fP", 3, 1);
3391 }
3392 return ROFF_IGN;
3393 }
3394
3395 static int
3396 roff_ec(ROFF_ARGS)
3397 {
3398 const char *p;
3399
3400 p = buf->buf + pos;
3401 if (*p == '\0')
3402 r->escape = '\\';
3403 else {
3404 r->escape = *p;
3405 if (*++p != '\0')
3406 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
3407 ln, p - buf->buf, "ec ... %s", p);
3408 }
3409 return ROFF_IGN;
3410 }
3411
3412 static int
3413 roff_eo(ROFF_ARGS)
3414 {
3415 r->escape = '\0';
3416 if (buf->buf[pos] != '\0')
3417 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse,
3418 ln, pos, "eo %s", buf->buf + pos);
3419 return ROFF_IGN;
3420 }
3421
3422 static int
3423 roff_nop(ROFF_ARGS)
3424 {
3425 while (buf->buf[pos] == ' ')
3426 pos++;
3427 *offs = pos;
3428 return ROFF_RERUN;
3429 }
3430
3431 static int
3432 roff_tr(ROFF_ARGS)
3433 {
3434 const char *p, *first, *second;
3435 size_t fsz, ssz;
3436 enum mandoc_esc esc;
3437
3438 p = buf->buf + pos;
3439
3440 if (*p == '\0') {
3441 mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
3442 return ROFF_IGN;
3443 }
3444
3445 while (*p != '\0') {
3446 fsz = ssz = 1;
3447
3448 first = p++;
3449 if (*first == '\\') {
3450 esc = mandoc_escape(&p, NULL, NULL);
3451 if (esc == ESCAPE_ERROR) {
3452 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
3453 ln, (int)(p - buf->buf), first);
3454 return ROFF_IGN;
3455 }
3456 fsz = (size_t)(p - first);
3457 }
3458
3459 second = p++;
3460 if (*second == '\\') {
3461 esc = mandoc_escape(&p, NULL, NULL);
3462 if (esc == ESCAPE_ERROR) {
3463 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
3464 ln, (int)(p - buf->buf), second);
3465 return ROFF_IGN;
3466 }
3467 ssz = (size_t)(p - second);
3468 } else if (*second == '\0') {
3469 mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
3470 ln, first - buf->buf, "tr %s", first);
3471 second = " ";
3472 p--;
3473 }
3474
3475 if (fsz > 1) {
3476 roff_setstrn(&r->xmbtab, first, fsz,
3477 second, ssz, 0);
3478 continue;
3479 }
3480
3481 if (r->xtab == NULL)
3482 r->xtab = mandoc_calloc(128,
3483 sizeof(struct roffstr));
3484
3485 free(r->xtab[(int)*first].p);
3486 r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
3487 r->xtab[(int)*first].sz = ssz;
3488 }
3489
3490 return ROFF_IGN;
3491 }
3492
3493 /*
3494 * Implementation of the .return request.
3495 * There is no need to call roff_userret() from here.
3496 * The read module will call that after rewinding the reader stack
3497 * to the place from where the current macro was called.
3498 */
3499 static int
3500 roff_return(ROFF_ARGS)
3501 {
3502 if (r->mstackpos >= 0)
3503 return ROFF_IGN | ROFF_USERRET;
3504
3505 mandoc_msg(MANDOCERR_REQ_NOMAC, r->parse, ln, ppos, "return");
3506 return ROFF_IGN;
3507 }
3508
3509 static int
3510 roff_rn(ROFF_ARGS)
3511 {
3512 const char *value;
3513 char *oldn, *newn, *end;
3514 size_t oldsz, newsz;
3515 int deftype;
3516
3517 oldn = newn = buf->buf + pos;
3518 if (*oldn == '\0')
3519 return ROFF_IGN;
3520
3521 oldsz = roff_getname(r, &newn, ln, pos);
3522 if (oldn[oldsz] == '\\' || *newn == '\0')
3523 return ROFF_IGN;
3524
3525 end = newn;
3526 newsz = roff_getname(r, &end, ln, newn - buf->buf);
3527 if (newsz == 0)
3528 return ROFF_IGN;
3529
3530 deftype = ROFFDEF_ANY;
3531 value = roff_getstrn(r, oldn, oldsz, &deftype);
3532 switch (deftype) {
3533 case ROFFDEF_USER:
3534 roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3535 roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
3536 roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3537 break;
3538 case ROFFDEF_PRE:
3539 roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3540 roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3541 break;
3542 case ROFFDEF_REN:
3543 roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
3544 roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
3545 roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3546 break;
3547 case ROFFDEF_STD:
3548 roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
3549 roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3550 break;
3551 default:
3552 roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3553 roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3554 break;
3555 }
3556 return ROFF_IGN;
3557 }
3558
3559 static int
3560 roff_shift(ROFF_ARGS)
3561 {
3562 struct mctx *ctx;
3563 int levels, i;
3564
3565 levels = 1;
3566 if (buf->buf[pos] != '\0' &&
3567 roff_evalnum(r, ln, buf->buf, &pos, &levels, 0) == 0) {
3568 mandoc_vmsg(MANDOCERR_CE_NONUM, r->parse,
3569 ln, pos, "shift %s", buf->buf + pos);
3570 levels = 1;
3571 }
3572 if (r->mstackpos < 0) {
3573 mandoc_msg(MANDOCERR_REQ_NOMAC, r->parse, ln, ppos, "shift");
3574 return ROFF_IGN;
3575 }
3576 ctx = r->mstack + r->mstackpos;
3577 if (levels > ctx->argc) {
3578 mandoc_vmsg(MANDOCERR_SHIFT, r->parse,
3579 ln, pos, "%d, but max is %d", levels, ctx->argc);
3580 levels = ctx->argc;
3581 }
3582 if (levels == 0)
3583 return ROFF_IGN;
3584 for (i = 0; i < levels; i++)
3585 free(ctx->argv[i]);
3586 ctx->argc -= levels;
3587 for (i = 0; i < ctx->argc; i++)
3588 ctx->argv[i] = ctx->argv[i + levels];
3589 return ROFF_IGN;
3590 }
3591
3592 static int
3593 roff_so(ROFF_ARGS)
3594 {
3595 char *name, *cp;
3596
3597 name = buf->buf + pos;
3598 mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
3599
3600 /*
3601 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3602 * opening anything that's not in our cwd or anything beneath
3603 * it. Thus, explicitly disallow traversing up the file-system
3604 * or using absolute paths.
3605 */
3606
3607 if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
3608 mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
3609 ".so %s", name);
3610 buf->sz = mandoc_asprintf(&cp,
3611 ".sp\nSee the file %s.\n.sp", name) + 1;
3612 free(buf->buf);
3613 buf->buf = cp;
3614 *offs = 0;
3615 return ROFF_REPARSE;
3616 }
3617
3618 *offs = pos;
3619 return ROFF_SO;
3620 }
3621
3622 /* --- user defined strings and macros ------------------------------------ */
3623
3624 static int
3625 roff_userdef(ROFF_ARGS)
3626 {
3627 struct mctx *ctx;
3628 char *arg, *ap, *dst, *src;
3629 size_t sz;
3630
3631 /* Initialize a new macro stack context. */
3632
3633 if (++r->mstackpos == r->mstacksz) {
3634 r->mstack = mandoc_recallocarray(r->mstack,
3635 r->mstacksz, r->mstacksz + 8, sizeof(*r->mstack));
3636 r->mstacksz += 8;
3637 }
3638 ctx = r->mstack + r->mstackpos;
3639 ctx->argsz = 0;
3640 ctx->argc = 0;
3641 ctx->argv = NULL;
3642
3643 /*
3644 * Collect pointers to macro argument strings,
3645 * NUL-terminating them and escaping quotes.
3646 */
3647
3648 src = buf->buf + pos;
3649 while (*src != '\0') {
3650 if (ctx->argc == ctx->argsz) {
3651 ctx->argsz += 8;
3652 ctx->argv = mandoc_reallocarray(ctx->argv,
3653 ctx->argsz, sizeof(*ctx->argv));
3654 }
3655 arg = mandoc_getarg(r->parse, &src, ln, &pos);
3656 sz = 1; /* For the terminating NUL. */
3657 for (ap = arg; *ap != '\0'; ap++)
3658 sz += *ap == '"' ? 4 : 1;
3659 ctx->argv[ctx->argc++] = dst = mandoc_malloc(sz);
3660 for (ap = arg; *ap != '\0'; ap++) {
3661 if (*ap == '"') {
3662 memcpy(dst, "\\(dq", 4);
3663 dst += 4;
3664 } else
3665 *dst++ = *ap;
3666 }
3667 *dst = '\0';
3668 }
3669
3670 /* Replace the macro invocation by the macro definition. */
3671
3672 free(buf->buf);
3673 buf->buf = mandoc_strdup(r->current_string);
3674 buf->sz = strlen(buf->buf) + 1;
3675 *offs = 0;
3676
3677 return buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
3678 ROFF_REPARSE | ROFF_USERCALL : ROFF_IGN | ROFF_APPEND;
3679 }
3680
3681 /*
3682 * Calling a high-level macro that was renamed with .rn.
3683 * r->current_string has already been set up by roff_parse().
3684 */
3685 static int
3686 roff_renamed(ROFF_ARGS)
3687 {
3688 char *nbuf;
3689
3690 buf->sz = mandoc_asprintf(&nbuf, ".%s%s%s", r->current_string,
3691 buf->buf[pos] == '\0' ? "" : " ", buf->buf + pos) + 1;
3692 free(buf->buf);
3693 buf->buf = nbuf;
3694 *offs = 0;
3695 return ROFF_CONT;
3696 }
3697
3698 static size_t
3699 roff_getname(struct roff *r, char **cpp, int ln, int pos)
3700 {
3701 char *name, *cp;
3702 size_t namesz;
3703
3704 name = *cpp;
3705 if ('\0' == *name)
3706 return 0;
3707
3708 /* Read until end of name and terminate it with NUL. */
3709 for (cp = name; 1; cp++) {
3710 if ('\0' == *cp || ' ' == *cp) {
3711 namesz = cp - name;
3712 break;
3713 }
3714 if ('\\' != *cp)
3715 continue;
3716 namesz = cp - name;
3717 if ('{' == cp[1] || '}' == cp[1])
3718 break;
3719 cp++;
3720 if ('\\' == *cp)
3721 continue;
3722 mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
3723 "%.*s", (int)(cp - name + 1), name);
3724 mandoc_escape((const char **)&cp, NULL, NULL);
3725 break;
3726 }
3727
3728 /* Read past spaces. */
3729 while (' ' == *cp)
3730 cp++;
3731
3732 *cpp = cp;
3733 return namesz;
3734 }
3735
3736 /*
3737 * Store *string into the user-defined string called *name.
3738 * To clear an existing entry, call with (*r, *name, NULL, 0).
3739 * append == 0: replace mode
3740 * append == 1: single-line append mode
3741 * append == 2: multiline append mode, append '\n' after each call
3742 */
3743 static void
3744 roff_setstr(struct roff *r, const char *name, const char *string,
3745 int append)
3746 {
3747 size_t namesz;
3748
3749 namesz = strlen(name);
3750 roff_setstrn(&r->strtab, name, namesz, string,
3751 string ? strlen(string) : 0, append);
3752 roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
3753 }
3754
3755 static void
3756 roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
3757 const char *string, size_t stringsz, int append)
3758 {
3759 struct roffkv *n;
3760 char *c;
3761 int i;
3762 size_t oldch, newch;
3763
3764 /* Search for an existing string with the same name. */
3765 n = *r;
3766
3767 while (n && (namesz != n->key.sz ||
3768 strncmp(n->key.p, name, namesz)))
3769 n = n->next;
3770
3771 if (NULL == n) {
3772 /* Create a new string table entry. */
3773 n = mandoc_malloc(sizeof(struct roffkv));
3774 n->key.p = mandoc_strndup(name, namesz);
3775 n->key.sz = namesz;
3776 n->val.p = NULL;
3777 n->val.sz = 0;
3778 n->next = *r;
3779 *r = n;
3780 } else if (0 == append) {
3781 free(n->val.p);
3782 n->val.p = NULL;
3783 n->val.sz = 0;
3784 }
3785
3786 if (NULL == string)
3787 return;
3788
3789 /*
3790 * One additional byte for the '\n' in multiline mode,
3791 * and one for the terminating '\0'.
3792 */
3793 newch = stringsz + (1 < append ? 2u : 1u);
3794
3795 if (NULL == n->val.p) {
3796 n->val.p = mandoc_malloc(newch);
3797 *n->val.p = '\0';
3798 oldch = 0;
3799 } else {
3800 oldch = n->val.sz;
3801 n->val.p = mandoc_realloc(n->val.p, oldch + newch);
3802 }
3803
3804 /* Skip existing content in the destination buffer. */
3805 c = n->val.p + (int)oldch;
3806
3807 /* Append new content to the destination buffer. */
3808 i = 0;
3809 while (i < (int)stringsz) {
3810 /*
3811 * Rudimentary roff copy mode:
3812 * Handle escaped backslashes.
3813 */
3814 if ('\\' == string[i] && '\\' == string[i + 1])
3815 i++;
3816 *c++ = string[i++];
3817 }
3818
3819 /* Append terminating bytes. */
3820 if (1 < append)
3821 *c++ = '\n';
3822
3823 *c = '\0';
3824 n->val.sz = (int)(c - n->val.p);
3825 }
3826
3827 static const char *
3828 roff_getstrn(struct roff *r, const char *name, size_t len,
3829 int *deftype)
3830 {
3831 const struct roffkv *n;
3832 int found, i;
3833 enum roff_tok tok;
3834
3835 found = 0;
3836 for (n = r->strtab; n != NULL; n = n->next) {
3837 if (strncmp(name, n->key.p, len) != 0 ||
3838 n->key.p[len] != '\0' || n->val.p == NULL)
3839 continue;
3840 if (*deftype & ROFFDEF_USER) {
3841 *deftype = ROFFDEF_USER;
3842 return n->val.p;
3843 } else {
3844 found = 1;
3845 break;
3846 }
3847 }
3848 for (n = r->rentab; n != NULL; n = n->next) {
3849 if (strncmp(name, n->key.p, len) != 0 ||
3850 n->key.p[len] != '\0' || n->val.p == NULL)
3851 continue;
3852 if (*deftype & ROFFDEF_REN) {
3853 *deftype = ROFFDEF_REN;
3854 return n->val.p;
3855 } else {
3856 found = 1;
3857 break;
3858 }
3859 }
3860 for (i = 0; i < PREDEFS_MAX; i++) {
3861 if (strncmp(name, predefs[i].name, len) != 0 ||
3862 predefs[i].name[len] != '\0')
3863 continue;
3864 if (*deftype & ROFFDEF_PRE) {
3865 *deftype = ROFFDEF_PRE;
3866 return predefs[i].str;
3867 } else {
3868 found = 1;
3869 break;
3870 }
3871 }
3872 if (r->man->macroset != MACROSET_MAN) {
3873 for (tok = MDOC_Dd; tok < MDOC_MAX; tok++) {
3874 if (strncmp(name, roff_name[tok], len) != 0 ||
3875 roff_name[tok][len] != '\0')
3876 continue;
3877 if (*deftype & ROFFDEF_STD) {
3878 *deftype = ROFFDEF_STD;
3879 return NULL;
3880 } else {
3881 found = 1;
3882 break;
3883 }
3884 }
3885 }
3886 if (r->man->macroset != MACROSET_MDOC) {
3887 for (tok = MAN_TH; tok < MAN_MAX; tok++) {
3888 if (strncmp(name, roff_name[tok], len) != 0 ||
3889 roff_name[tok][len] != '\0')
3890 continue;
3891 if (*deftype & ROFFDEF_STD) {
3892 *deftype = ROFFDEF_STD;
3893 return NULL;
3894 } else {
3895 found = 1;
3896 break;
3897 }
3898 }
3899 }
3900
3901 if (found == 0 && *deftype != ROFFDEF_ANY) {
3902 if (*deftype & ROFFDEF_REN) {
3903 /*
3904 * This might still be a request,
3905 * so do not treat it as undefined yet.
3906 */
3907 *deftype = ROFFDEF_UNDEF;
3908 return NULL;
3909 }
3910
3911 /* Using an undefined string defines it to be empty. */
3912
3913 roff_setstrn(&r->strtab, name, len, "", 0, 0);
3914 roff_setstrn(&r->rentab, name, len, NULL, 0, 0);
3915 }
3916
3917 *deftype = 0;
3918 return NULL;
3919 }
3920
3921 static void
3922 roff_freestr(struct roffkv *r)
3923 {
3924 struct roffkv *n, *nn;
3925
3926 for (n = r; n; n = nn) {
3927 free(n->key.p);
3928 free(n->val.p);
3929 nn = n->next;
3930 free(n);
3931 }
3932 }
3933
3934 /* --- accessors and utility functions ------------------------------------ */
3935
3936 /*
3937 * Duplicate an input string, making the appropriate character
3938 * conversations (as stipulated by `tr') along the way.
3939 * Returns a heap-allocated string with all the replacements made.
3940 */
3941 char *
3942 roff_strdup(const struct roff *r, const char *p)
3943 {
3944 const struct roffkv *cp;
3945 char *res;
3946 const char *pp;
3947 size_t ssz, sz;
3948 enum mandoc_esc esc;
3949
3950 if (NULL == r->xmbtab && NULL == r->xtab)
3951 return mandoc_strdup(p);
3952 else if ('\0' == *p)
3953 return mandoc_strdup("");
3954
3955 /*
3956 * Step through each character looking for term matches
3957 * (remember that a `tr' can be invoked with an escape, which is
3958 * a glyph but the escape is multi-character).
3959 * We only do this if the character hash has been initialised
3960 * and the string is >0 length.
3961 */
3962
3963 res = NULL;
3964 ssz = 0;
3965
3966 while ('\0' != *p) {
3967 assert((unsigned int)*p < 128);
3968 if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
3969 sz = r->xtab[(int)*p].sz;
3970 res = mandoc_realloc(res, ssz + sz + 1);
3971 memcpy(res + ssz, r->xtab[(int)*p].p, sz);
3972 ssz += sz;
3973 p++;
3974 continue;
3975 } else if ('\\' != *p) {
3976 res = mandoc_realloc(res, ssz + 2);
3977 res[ssz++] = *p++;
3978 continue;
3979 }
3980
3981 /* Search for term matches. */
3982 for (cp = r->xmbtab; cp; cp = cp->next)
3983 if (0 == strncmp(p, cp->key.p, cp->key.sz))
3984 break;
3985
3986 if (NULL != cp) {
3987 /*
3988 * A match has been found.
3989 * Append the match to the array and move
3990 * forward by its keysize.
3991 */
3992 res = mandoc_realloc(res,
3993 ssz + cp->val.sz + 1);
3994 memcpy(res + ssz, cp->val.p, cp->val.sz);
3995 ssz += cp->val.sz;
3996 p += (int)cp->key.sz;
3997 continue;
3998 }
3999
4000 /*
4001 * Handle escapes carefully: we need to copy
4002 * over just the escape itself, or else we might
4003 * do replacements within the escape itself.
4004 * Make sure to pass along the bogus string.
4005 */
4006 pp = p++;
4007 esc = mandoc_escape(&p, NULL, NULL);
4008 if (ESCAPE_ERROR == esc) {
4009 sz = strlen(pp);
4010 res = mandoc_realloc(res, ssz + sz + 1);
4011 memcpy(res + ssz, pp, sz);
4012 break;
4013 }
4014 /*
4015 * We bail out on bad escapes.
4016 * No need to warn: we already did so when
4017 * roff_res() was called.
4018 */
4019 sz = (int)(p - pp);
4020 res = mandoc_realloc(res, ssz + sz + 1);
4021 memcpy(res + ssz, pp, sz);
4022 ssz += sz;
4023 }
4024
4025 res[(int)ssz] = '\0';
4026 return res;
4027 }
4028
4029 int
4030 roff_getformat(const struct roff *r)
4031 {
4032
4033 return r->format;
4034 }
4035
4036 /*
4037 * Find out whether a line is a macro line or not.
4038 * If it is, adjust the current position and return one; if it isn't,
4039 * return zero and don't change the current position.
4040 * If the control character has been set with `.cc', then let that grain
4041 * precedence.
4042 * This is slighly contrary to groff, where using the non-breaking
4043 * control character when `cc' has been invoked will cause the
4044 * non-breaking macro contents to be printed verbatim.
4045 */
4046 int
4047 roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
4048 {
4049 int pos;
4050
4051 pos = *ppos;
4052
4053 if (r->control != '\0' && cp[pos] == r->control)
4054 pos++;
4055 else if (r->control != '\0')
4056 return 0;
4057 else if ('\\' == cp[pos] && '.' == cp[pos + 1])
4058 pos += 2;
4059 else if ('.' == cp[pos] || '\'' == cp[pos])
4060 pos++;
4061 else
4062 return 0;
4063
4064 while (' ' == cp[pos] || '\t' == cp[pos])
4065 pos++;
4066
4067 *ppos = pos;
4068 return 1;
4069 }