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