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