]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.322 2017/07/13 15:13:18 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
33 #include "mandoc_ohash.h"
35 #include "libmandoc.h"
39 /* Maximum number of string expansions per line, to break infinite loops. */
40 #define EXPAND_LIMIT 1000
42 /* Types of definitions of macros and strings. */
43 #define ROFFDEF_USER (1 << 1) /* User-defined. */
44 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
45 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
46 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
47 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
48 ROFFDEF_REN | ROFFDEF_STD)
50 /* --- data types --------------------------------------------------------- */
53 * An incredibly-simple string buffer.
56 char *p
; /* nil-terminated buffer */
57 size_t sz
; /* saved strlen(p) */
61 * A key-value roffstr pair as part of a singly-linked list.
66 struct roffkv
*next
; /* next in list */
70 * A single number register as part of a singly-linked list.
79 * Association of request and macro names with token IDs.
87 struct mparse
*parse
; /* parse point */
88 struct roff_man
*man
; /* mdoc or man parser */
89 struct roffnode
*last
; /* leaf of stack */
90 int *rstack
; /* stack of inverted `ie' values */
91 struct ohash
*reqtab
; /* request lookup table */
92 struct roffreg
*regtab
; /* number registers */
93 struct roffkv
*strtab
; /* user-defined strings & macros */
94 struct roffkv
*rentab
; /* renamed strings & macros */
95 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
96 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
97 const char *current_string
; /* value of last called user macro */
98 struct tbl_node
*first_tbl
; /* first table parsed */
99 struct tbl_node
*last_tbl
; /* last table parsed */
100 struct tbl_node
*tbl
; /* current table being parsed */
101 struct eqn_node
*last_eqn
; /* equation parser */
102 struct eqn_node
*eqn
; /* active equation parser */
103 int eqn_inline
; /* current equation is inline */
104 int options
; /* parse options */
105 int rstacksz
; /* current size limit of rstack */
106 int rstackpos
; /* position in rstack */
107 int format
; /* current file in mdoc or man format */
108 int argc
; /* number of args of the last macro */
109 char control
; /* control character */
110 char escape
; /* escape character */
114 enum roff_tok tok
; /* type of node */
115 struct roffnode
*parent
; /* up one in stack */
116 int line
; /* parse line */
117 int col
; /* parse col */
118 char *name
; /* node name, e.g. macro name */
119 char *end
; /* end-rules: custom token */
120 int endspan
; /* end-rules: next-line or infty */
121 int rule
; /* current evaluation rule */
124 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
125 enum roff_tok tok, /* tok of macro */ \
126 struct buf *buf, /* input buffer */ \
127 int ln, /* parse line */ \
128 int ppos, /* original pos in buffer */ \
129 int pos, /* current pos in buffer */ \
130 int *offs /* reset offset of buffer data */
132 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
135 roffproc proc
; /* process new macro */
136 roffproc text
; /* process as child text of macro */
137 roffproc sub
; /* process as child of macro */
139 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
143 const char *name
; /* predefined input name */
144 const char *str
; /* replacement symbol */
147 #define PREDEF(__name, __str) \
148 { (__name), (__str) },
150 /* --- function prototypes ------------------------------------------------ */
152 static void roffnode_cleanscope(struct roff
*);
153 static void roffnode_pop(struct roff
*);
154 static void roffnode_push(struct roff
*, enum roff_tok
,
155 const char *, int, int);
156 static void roff_addtbl(struct roff_man
*, struct tbl_node
*);
157 static enum rofferr
roff_als(ROFF_ARGS
);
158 static enum rofferr
roff_block(ROFF_ARGS
);
159 static enum rofferr
roff_block_text(ROFF_ARGS
);
160 static enum rofferr
roff_block_sub(ROFF_ARGS
);
161 static enum rofferr
roff_br(ROFF_ARGS
);
162 static enum rofferr
roff_cblock(ROFF_ARGS
);
163 static enum rofferr
roff_cc(ROFF_ARGS
);
164 static void roff_ccond(struct roff
*, int, int);
165 static enum rofferr
roff_cond(ROFF_ARGS
);
166 static enum rofferr
roff_cond_text(ROFF_ARGS
);
167 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
168 static enum rofferr
roff_ds(ROFF_ARGS
);
169 static enum rofferr
roff_ec(ROFF_ARGS
);
170 static enum rofferr
roff_eo(ROFF_ARGS
);
171 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
172 static int roff_evalcond(struct roff
*r
, int, char *, int *);
173 static int roff_evalnum(struct roff
*, int,
174 const char *, int *, int *, int);
175 static int roff_evalpar(struct roff
*, int,
176 const char *, int *, int *, int);
177 static int roff_evalstrcond(const char *, int *);
178 static void roff_free1(struct roff
*);
179 static void roff_freereg(struct roffreg
*);
180 static void roff_freestr(struct roffkv
*);
181 static size_t roff_getname(struct roff
*, char **, int, int);
182 static int roff_getnum(const char *, int *, int *, int);
183 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_getregro(const struct roff
*,
188 static const char *roff_getstrn(const struct roff
*,
189 const char *, size_t, int *);
190 static int roff_hasregn(const struct roff
*,
191 const char *, size_t);
192 static enum rofferr
roff_insec(ROFF_ARGS
);
193 static enum rofferr
roff_it(ROFF_ARGS
);
194 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
195 static void roff_man_alloc1(struct roff_man
*);
196 static void roff_man_free1(struct roff_man
*);
197 static enum rofferr
roff_manyarg(ROFF_ARGS
);
198 static enum rofferr
roff_nr(ROFF_ARGS
);
199 static enum rofferr
roff_onearg(ROFF_ARGS
);
200 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
202 static enum rofferr
roff_parsetext(struct roff
*, struct buf
*,
204 static enum rofferr
roff_renamed(ROFF_ARGS
);
205 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
206 static enum rofferr
roff_rm(ROFF_ARGS
);
207 static enum rofferr
roff_rn(ROFF_ARGS
);
208 static enum rofferr
roff_rr(ROFF_ARGS
);
209 static void roff_setstr(struct roff
*,
210 const char *, const char *, int);
211 static void roff_setstrn(struct roffkv
**, const char *,
212 size_t, const char *, size_t, int);
213 static enum rofferr
roff_so(ROFF_ARGS
);
214 static enum rofferr
roff_tr(ROFF_ARGS
);
215 static enum rofferr
roff_Dd(ROFF_ARGS
);
216 static enum rofferr
roff_TE(ROFF_ARGS
);
217 static enum rofferr
roff_TS(ROFF_ARGS
);
218 static enum rofferr
roff_EQ(ROFF_ARGS
);
219 static enum rofferr
roff_EN(ROFF_ARGS
);
220 static enum rofferr
roff_T_(ROFF_ARGS
);
221 static enum rofferr
roff_unsupp(ROFF_ARGS
);
222 static enum rofferr
roff_userdef(ROFF_ARGS
);
224 /* --- constant data ------------------------------------------------------ */
226 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
227 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
229 const char *__roff_name
[MAN_MAX
+ 1] = {
230 "br", "ce", "ft", "ll",
231 "mc", "po", "rj", "sp",
233 "ab", "ad", "af", "aln",
234 "als", "am", "am1", "ami",
235 "ami1", "as", "as1", "asciify",
236 "backtrace", "bd", "bleedat", "blm",
237 "box", "boxa", "bp", "BP",
238 "break", "breakchar", "brnl", "brp",
240 "cf", "cflags", "ch", "char",
241 "chop", "class", "close", "CL",
242 "color", "composite", "continue", "cp",
243 "cropat", "cs", "cu", "da",
244 "dch", "Dd", "de", "de1",
245 "defcolor", "dei", "dei1", "device",
246 "devicem", "di", "do", "ds",
247 "ds1", "dwh", "dt", "ec",
248 "ecr", "ecs", "el", "em",
249 "EN", "eo", "EP", "EQ",
250 "errprint", "ev", "evc", "ex",
251 "fallback", "fam", "fc", "fchar",
252 "fcolor", "fdeferlig", "feature", "fkern",
253 "fl", "flig", "fp", "fps",
254 "fschar", "fspacewidth", "fspecial", "ftr",
255 "fzoom", "gcolor", "hc", "hcode",
256 "hidechar", "hla", "hlm", "hpf",
257 "hpfa", "hpfcode", "hw", "hy",
258 "hylang", "hylen", "hym", "hypp",
259 "hys", "ie", "if", "ig",
260 "index", "it", "itc", "IX",
261 "kern", "kernafter", "kernbefore", "kernpair",
262 "lc", "lc_ctype", "lds", "length",
263 "letadj", "lf", "lg", "lhang",
264 "linetabs", "lnr", "lnrf", "lpfx",
266 "mediasize", "minss", "mk", "mso",
267 "na", "ne", "nh", "nhychar",
268 "nm", "nn", "nop", "nr",
269 "nrf", "nroff", "ns", "nx",
270 "open", "opena", "os", "output",
271 "padj", "papersize", "pc", "pev",
272 "pi", "PI", "pl", "pm",
274 "psbb", "pshape", "pso", "ptr",
275 "pvs", "rchar", "rd", "recursionlimit",
276 "return", "rfschar", "rhang",
277 "rm", "rn", "rnn", "rr",
278 "rs", "rt", "schar", "sentchar",
279 "shc", "shift", "sizes", "so",
280 "spacewidth", "special", "spreadwarn", "ss",
281 "sty", "substring", "sv", "sy",
284 "tm", "tm1", "tmc", "tr",
285 "track", "transchar", "trf", "trimat",
286 "trin", "trnt", "troff", "TS",
287 "uf", "ul", "unformat", "unwatch",
288 "unwatchn", "vpt", "vs", "warn",
289 "warnscale", "watch", "watchlength", "watchn",
290 "wh", "while", "write", "writec",
291 "writem", "xflag", ".", NULL
,
293 "Dd", "Dt", "Os", "Sh",
294 "Ss", "Pp", "D1", "Dl",
295 "Bd", "Ed", "Bl", "El",
296 "It", "Ad", "An", "Ap",
297 "Ar", "Cd", "Cm", "Dv",
298 "Er", "Ev", "Ex", "Fa",
299 "Fd", "Fl", "Fn", "Ft",
300 "Ic", "In", "Li", "Nd",
301 "Nm", "Op", "Ot", "Pa",
302 "Rv", "St", "Va", "Vt",
303 "Xr", "%A", "%B", "%D",
304 "%I", "%J", "%N", "%O",
305 "%P", "%R", "%T", "%V",
306 "Ac", "Ao", "Aq", "At",
307 "Bc", "Bf", "Bo", "Bq",
308 "Bsx", "Bx", "Db", "Dc",
309 "Do", "Dq", "Ec", "Ef",
310 "Em", "Eo", "Fx", "Ms",
311 "No", "Ns", "Nx", "Ox",
312 "Pc", "Pf", "Po", "Pq",
313 "Qc", "Ql", "Qo", "Qq",
314 "Re", "Rs", "Sc", "So",
315 "Sq", "Sm", "Sx", "Sy",
316 "Tn", "Ux", "Xc", "Xo",
317 "Fo", "Fc", "Oo", "Oc",
318 "Bk", "Ek", "Bt", "Hf",
319 "Fr", "Ud", "Lb", "Lp",
320 "Lk", "Mt", "Brq", "Bro",
321 "Brc", "%C", "Es", "En",
322 "Dx", "%Q", "%U", "Ta",
324 "TH", "SH", "SS", "TP",
325 "LP", "PP", "P", "IP",
326 "HP", "SM", "SB", "BI",
327 "IB", "BR", "RB", "R",
328 "B", "I", "IR", "RI",
330 "RE", "RS", "DT", "UC",
332 "OP", "EX", "EE", "UR",
333 "UE", "MT", "ME", NULL
335 const char *const *roff_name
= __roff_name
;
337 static struct roffmac roffs
[TOKEN_NONE
] = {
338 { roff_br
, NULL
, NULL
, 0 }, /* br */
339 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
340 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
341 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
342 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
343 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
344 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
345 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
346 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
347 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
348 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
349 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
350 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
351 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
352 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
353 { roff_als
, NULL
, NULL
, 0 }, /* als */
354 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
355 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
356 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
357 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
358 { roff_ds
, NULL
, NULL
, 0 }, /* as */
359 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
360 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
361 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
362 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
363 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
364 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
365 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
366 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
367 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
368 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
369 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
370 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
371 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
372 { roff_br
, NULL
, NULL
, 0 }, /* brp */
373 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
374 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
375 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
376 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
377 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
378 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
379 { roff_unsupp
, NULL
, NULL
, 0 }, /* char */
380 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
381 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
382 { roff_insec
, NULL
, NULL
, 0 }, /* close */
383 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
385 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
386 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
387 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
388 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
389 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
390 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
391 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
392 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
393 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
394 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
395 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
396 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
397 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
398 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
399 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
400 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
401 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
402 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
403 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
404 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
405 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
406 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
407 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
409 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
410 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
411 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
412 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
413 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
415 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
416 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
417 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
418 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
419 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
420 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
421 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
425 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
426 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
427 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
428 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
429 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
430 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
431 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
432 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
433 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
434 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
435 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
436 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
437 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
438 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
439 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
440 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
441 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
444 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
445 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
446 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
453 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
454 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
455 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
456 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
457 { roff_it
, NULL
, NULL
, 0 }, /* it */
458 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
464 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
465 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
466 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
467 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
469 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
472 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
473 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
474 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
475 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
476 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
477 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
478 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
479 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
482 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
487 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
488 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
489 { roff_unsupp
, NULL
, NULL
, 0 }, /* nop */
490 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
491 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
494 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
495 { roff_insec
, NULL
, NULL
, 0 }, /* open */
496 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
497 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
498 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
503 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
504 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
509 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
510 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
511 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
512 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
513 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
515 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
516 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
517 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
518 { roff_unsupp
, NULL
, NULL
, 0 }, /* return */
519 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
521 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
522 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
523 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
524 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
525 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
526 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
527 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
530 { roff_unsupp
, NULL
, NULL
, 0 }, /* shift */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
532 { roff_so
, NULL
, NULL
, 0 }, /* so */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
534 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
536 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
537 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
538 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
540 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
541 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
542 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
543 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
544 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
545 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
546 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
550 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
552 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
553 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
555 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
556 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
558 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
561 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
562 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
563 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
564 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
565 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
566 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
571 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
572 { roff_unsupp
, NULL
, NULL
, 0 }, /* while */
573 { roff_insec
, NULL
, NULL
, 0 }, /* write */
574 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
575 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
576 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
577 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
578 { roff_renamed
, NULL
, NULL
, 0 },
579 { roff_userdef
, NULL
, NULL
, 0 }
582 /* Array of injected predefined strings. */
583 #define PREDEFS_MAX 38
584 static const struct predef predefs
[PREDEFS_MAX
] = {
585 #include "predefs.in"
588 static int roffce_lines
; /* number of input lines to center */
589 static struct roff_node
*roffce_node
; /* active request */
590 static int roffit_lines
; /* number of lines to delay */
591 static char *roffit_macro
; /* nil-terminated macro line */
594 /* --- request table ------------------------------------------------------ */
597 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
605 htab
= mandoc_malloc(sizeof(*htab
));
606 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
608 for (tok
= mintok
; tok
< maxtok
; tok
++) {
609 if (roff_name
[tok
] == NULL
)
611 sz
= strlen(roff_name
[tok
]);
612 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
614 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
615 slot
= ohash_qlookup(htab
, req
->name
);
616 ohash_insert(htab
, slot
, req
);
622 roffhash_free(struct ohash
*htab
)
629 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
630 req
= ohash_next(htab
, &slot
))
637 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
644 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
646 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
647 return req
== NULL
? TOKEN_NONE
: req
->tok
;
650 /* --- stack of request blocks -------------------------------------------- */
653 * Pop the current node off of the stack of roff instructions currently
657 roffnode_pop(struct roff
*r
)
664 r
->last
= r
->last
->parent
;
671 * Push a roff node onto the instruction stack. This must later be
672 * removed with roffnode_pop().
675 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
680 p
= mandoc_calloc(1, sizeof(struct roffnode
));
683 p
->name
= mandoc_strdup(name
);
687 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
692 /* --- roff parser state data management ---------------------------------- */
695 roff_free1(struct roff
*r
)
697 struct tbl_node
*tbl
;
700 while (NULL
!= (tbl
= r
->first_tbl
)) {
701 r
->first_tbl
= tbl
->next
;
704 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
706 if (r
->last_eqn
!= NULL
)
707 eqn_free(r
->last_eqn
);
708 r
->last_eqn
= r
->eqn
= NULL
;
718 roff_freereg(r
->regtab
);
721 roff_freestr(r
->strtab
);
722 roff_freestr(r
->rentab
);
723 roff_freestr(r
->xmbtab
);
724 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
727 for (i
= 0; i
< 128; i
++)
734 roff_reset(struct roff
*r
)
737 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
747 roff_free(struct roff
*r
)
750 roffhash_free(r
->reqtab
);
755 roff_alloc(struct mparse
*parse
, int options
)
759 r
= mandoc_calloc(1, sizeof(struct roff
));
761 r
->reqtab
= roffhash_alloc(0, ROFF_USERDEF
);
762 r
->options
= options
;
763 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
769 /* --- syntax tree state data management ---------------------------------- */
772 roff_man_free1(struct roff_man
*man
)
775 if (man
->first
!= NULL
)
776 roff_node_delete(man
, man
->first
);
777 free(man
->meta
.msec
);
780 free(man
->meta
.arch
);
781 free(man
->meta
.title
);
782 free(man
->meta
.name
);
783 free(man
->meta
.date
);
787 roff_man_alloc1(struct roff_man
*man
)
790 memset(&man
->meta
, 0, sizeof(man
->meta
));
791 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
792 man
->first
->type
= ROFFT_ROOT
;
793 man
->last
= man
->first
;
796 man
->macroset
= MACROSET_NONE
;
797 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
798 man
->next
= ROFF_NEXT_CHILD
;
802 roff_man_reset(struct roff_man
*man
)
806 roff_man_alloc1(man
);
810 roff_man_free(struct roff_man
*man
)
818 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
819 const char *os_s
, int quick
)
821 struct roff_man
*man
;
823 man
= mandoc_calloc(1, sizeof(*man
));
828 roff_man_alloc1(man
);
833 /* --- syntax tree handling ----------------------------------------------- */
836 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
837 enum roff_type type
, int tok
)
841 n
= mandoc_calloc(1, sizeof(*n
));
846 n
->sec
= man
->lastsec
;
848 if (man
->flags
& MDOC_SYNOPSIS
)
849 n
->flags
|= NODE_SYNPRETTY
;
851 n
->flags
&= ~NODE_SYNPRETTY
;
852 if (man
->flags
& MDOC_NEWLINE
)
853 n
->flags
|= NODE_LINE
;
854 man
->flags
&= ~MDOC_NEWLINE
;
860 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
864 case ROFF_NEXT_SIBLING
:
865 if (man
->last
->next
!= NULL
) {
866 n
->next
= man
->last
->next
;
867 man
->last
->next
->prev
= n
;
869 man
->last
->parent
->last
= n
;
872 n
->parent
= man
->last
->parent
;
874 case ROFF_NEXT_CHILD
:
875 if (man
->last
->child
!= NULL
) {
876 n
->next
= man
->last
->child
;
877 man
->last
->child
->prev
= n
;
880 man
->last
->child
= n
;
881 n
->parent
= man
->last
;
893 if (n
->end
!= ENDBODY_NOT
)
905 * Copy over the normalised-data pointer of our parent. Not
906 * everybody has one, but copying a null pointer is fine.
909 n
->norm
= n
->parent
->norm
;
910 assert(n
->parent
->type
== ROFFT_BLOCK
);
914 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
918 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
919 n
->string
= roff_strdup(man
->roff
, word
);
920 roff_node_append(man
, n
);
921 n
->flags
|= NODE_VALID
| NODE_ENDED
;
922 man
->next
= ROFF_NEXT_SIBLING
;
926 roff_word_append(struct roff_man
*man
, const char *word
)
929 char *addstr
, *newstr
;
932 addstr
= roff_strdup(man
->roff
, word
);
933 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
937 man
->next
= ROFF_NEXT_SIBLING
;
941 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
945 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
946 roff_node_append(man
, n
);
947 man
->next
= ROFF_NEXT_CHILD
;
951 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
955 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
956 roff_node_append(man
, n
);
957 man
->next
= ROFF_NEXT_CHILD
;
962 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
966 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
967 roff_node_append(man
, n
);
968 man
->next
= ROFF_NEXT_CHILD
;
973 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
977 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
978 roff_node_append(man
, n
);
979 man
->next
= ROFF_NEXT_CHILD
;
984 roff_addtbl(struct roff_man
*man
, struct tbl_node
*tbl
)
987 const struct tbl_span
*span
;
989 if (man
->macroset
== MACROSET_MAN
)
990 man_breakscope(man
, ROFF_TS
);
991 while ((span
= tbl_span(tbl
)) != NULL
) {
992 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
994 roff_node_append(man
, n
);
995 n
->flags
|= NODE_VALID
| NODE_ENDED
;
996 man
->next
= ROFF_NEXT_SIBLING
;
1001 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1004 /* Adjust siblings. */
1007 n
->prev
->next
= n
->next
;
1009 n
->next
->prev
= n
->prev
;
1011 /* Adjust parent. */
1013 if (n
->parent
!= NULL
) {
1014 if (n
->parent
->child
== n
)
1015 n
->parent
->child
= n
->next
;
1016 if (n
->parent
->last
== n
)
1017 n
->parent
->last
= n
->prev
;
1020 /* Adjust parse point. */
1024 if (man
->last
== n
) {
1025 if (n
->prev
== NULL
) {
1026 man
->last
= n
->parent
;
1027 man
->next
= ROFF_NEXT_CHILD
;
1029 man
->last
= n
->prev
;
1030 man
->next
= ROFF_NEXT_SIBLING
;
1033 if (man
->first
== n
)
1038 roff_node_free(struct roff_node
*n
)
1041 if (n
->args
!= NULL
)
1042 mdoc_argv_free(n
->args
);
1043 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1046 eqn_box_free(n
->eqn
);
1052 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1055 while (n
->child
!= NULL
)
1056 roff_node_delete(man
, n
->child
);
1057 roff_node_unlink(man
, n
);
1062 deroff(char **dest
, const struct roff_node
*n
)
1067 if (n
->type
!= ROFFT_TEXT
) {
1068 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1073 /* Skip leading whitespace. */
1075 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1076 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1077 strchr(" %&0^|~", cp
[1]) != NULL
)
1079 else if ( ! isspace((unsigned char)*cp
))
1083 /* Skip trailing backslash. */
1086 if (sz
> 0 && cp
[sz
- 1] == '\\')
1089 /* Skip trailing whitespace. */
1092 if ( ! isspace((unsigned char)cp
[sz
-1]))
1095 /* Skip empty strings. */
1100 if (*dest
== NULL
) {
1101 *dest
= mandoc_strndup(cp
, sz
);
1105 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1110 /* --- main functions of the roff parser ---------------------------------- */
1113 * In the current line, expand escape sequences that tend to get
1114 * used in numerical expressions and conditional requests.
1115 * Also check the syntax of the remaining escape sequences.
1118 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1120 char ubuf
[24]; /* buffer to print the number */
1121 const char *start
; /* start of the string to process */
1122 char *stesc
; /* start of an escape sequence ('\\') */
1123 const char *stnam
; /* start of the name, after "[(*" */
1124 const char *cp
; /* end of the name, e.g. before ']' */
1125 const char *res
; /* the string to be substituted */
1126 char *nbuf
; /* new buffer to copy buf->buf to */
1127 size_t maxl
; /* expected length of the escape name */
1128 size_t naml
; /* actual length of the escape name */
1129 enum mandoc_esc esc
; /* type of the escape sequence */
1130 enum mandoc_os os_e
; /* kind of RCS id seen */
1131 int inaml
; /* length returned from mandoc_escape() */
1132 int expand_count
; /* to avoid infinite loops */
1133 int npos
; /* position in numeric expression */
1134 int arg_complete
; /* argument not interrupted by eol */
1135 int done
; /* no more input available */
1136 int deftype
; /* type of definition to paste */
1137 char term
; /* character terminating the escape */
1139 /* Search forward for comments. */
1142 start
= buf
->buf
+ pos
;
1143 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1144 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1147 if (*stesc
!= '"' && *stesc
!= '#')
1150 /* Comment found, look for RCS id. */
1152 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1153 os_e
= MANDOC_OS_OPENBSD
;
1155 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1156 os_e
= MANDOC_OS_NETBSD
;
1160 isalnum((unsigned char)*cp
) == 0 &&
1161 strchr(cp
, '$') != NULL
) {
1162 if (r
->man
->meta
.rcsids
& (1 << os_e
))
1163 mandoc_msg(MANDOCERR_RCS_REP
, r
->parse
,
1164 ln
, stesc
+ 1 - buf
->buf
, stesc
+ 1);
1165 r
->man
->meta
.rcsids
|= 1 << os_e
;
1168 /* Handle trailing whitespace. */
1170 cp
= strchr(stesc
--, '\0') - 1;
1175 if (*cp
== ' ' || *cp
== '\t')
1176 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1177 ln
, cp
- buf
->buf
, NULL
);
1178 while (stesc
> start
&& stesc
[-1] == ' ')
1187 /* Notice the end of the input. */
1189 if (*stesc
== '\n') {
1195 while (stesc
>= start
) {
1197 /* Search backwards for the next backslash. */
1199 if (*stesc
!= r
->escape
) {
1200 if (*stesc
== '\\') {
1202 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1203 buf
->buf
, stesc
+ 1) + 1;
1205 stesc
= nbuf
+ (stesc
- buf
->buf
);
1213 /* If it is escaped, skip it. */
1215 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1216 if (*cp
!= r
->escape
)
1219 if ((stesc
- cp
) % 2 == 0) {
1223 } else if (stesc
[1] != '\0') {
1233 /* Decide whether to expand or to check only. */
1249 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1250 if (esc
== ESCAPE_ERROR
||
1251 (esc
== ESCAPE_SPECIAL
&&
1252 mchars_spec2cp(stnam
, inaml
) < 0))
1253 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1254 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1255 "%.*s", (int)(cp
- stesc
), stesc
);
1260 if (EXPAND_LIMIT
< ++expand_count
) {
1261 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1262 ln
, (int)(stesc
- buf
->buf
), NULL
);
1267 * The third character decides the length
1268 * of the name of the string or register.
1269 * Save a pointer to the name.
1296 /* Advance to the end of the name. */
1300 while (maxl
== 0 || naml
< maxl
) {
1302 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1303 ln
, (int)(stesc
- buf
->buf
), stesc
);
1307 if (maxl
== 0 && *cp
== term
) {
1311 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1315 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1316 case ESCAPE_SPECIAL
:
1317 case ESCAPE_UNICODE
:
1318 case ESCAPE_NUMBERED
:
1319 case ESCAPE_OVERSTRIKE
:
1328 * Retrieve the replacement string; if it is
1329 * undefined, resume searching for escapes.
1335 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1336 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1341 ubuf
[0] = arg_complete
&&
1342 roff_evalnum(r
, ln
, stnam
, &npos
,
1343 NULL
, ROFFNUM_SCALE
) &&
1344 stnam
+ npos
+ 1 == cp
? '1' : '0';
1349 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1350 roff_getregn(r
, stnam
, naml
));
1355 /* use even incomplete args */
1356 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1362 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1363 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1364 "%.*s", (int)naml
, stnam
);
1366 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1367 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1368 ln
, (int)(stesc
- buf
->buf
), NULL
);
1372 /* Replace the escape sequence by the string. */
1375 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1376 buf
->buf
, res
, cp
) + 1;
1378 /* Prepare for the next replacement. */
1381 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1389 * Process text streams.
1392 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1398 enum mandoc_esc esc
;
1400 /* Spring the input line trap. */
1402 if (roffit_lines
== 1) {
1403 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1410 return ROFF_REPARSE
;
1411 } else if (roffit_lines
> 1)
1414 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1415 if (roffce_lines
< 1) {
1416 r
->man
->last
= roffce_node
;
1417 r
->man
->next
= ROFF_NEXT_SIBLING
;
1424 /* Convert all breakable hyphens into ASCII_HYPH. */
1426 start
= p
= buf
->buf
+ pos
;
1428 while (*p
!= '\0') {
1429 sz
= strcspn(p
, "-\\");
1436 /* Skip over escapes. */
1438 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1439 if (esc
== ESCAPE_ERROR
)
1444 } else if (p
== start
) {
1449 if (isalpha((unsigned char)p
[-1]) &&
1450 isalpha((unsigned char)p
[1]))
1458 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1462 int pos
; /* parse point */
1463 int spos
; /* saved parse point for messages */
1464 int ppos
; /* original offset in buf->buf */
1465 int ctl
; /* macro line (boolean) */
1469 /* Handle in-line equation delimiters. */
1471 if (r
->tbl
== NULL
&&
1472 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1473 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1474 e
= roff_eqndelim(r
, buf
, pos
);
1475 if (e
== ROFF_REPARSE
)
1477 assert(e
== ROFF_CONT
);
1480 /* Expand some escape sequences. */
1482 e
= roff_res(r
, buf
, ln
, pos
);
1483 if (e
== ROFF_IGN
|| e
== ROFF_APPEND
)
1485 assert(e
== ROFF_CONT
);
1487 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1490 * First, if a scope is open and we're not a macro, pass the
1491 * text through the macro's filter.
1492 * Equations process all content themselves.
1493 * Tables process almost all content themselves, but we want
1494 * to warn about macros before passing it there.
1497 if (r
->last
!= NULL
&& ! ctl
) {
1499 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1502 assert(e
== ROFF_CONT
);
1504 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1505 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1508 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1509 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1510 roff_addtbl(r
->man
, r
->tbl
);
1514 return roff_parsetext(r
, buf
, pos
, offs
);
1516 /* Skip empty request lines. */
1518 if (buf
->buf
[pos
] == '"') {
1519 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1522 } else if (buf
->buf
[pos
] == '\0')
1526 * If a scope is open, go to the child handler for that macro,
1527 * as it may want to preprocess before doing anything with it.
1528 * Don't do so if an equation is open.
1533 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1536 /* No scope is open. This is a new request or macro. */
1539 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1541 /* Tables ignore most macros. */
1543 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1544 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1545 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1546 ln
, pos
, buf
->buf
+ spos
);
1547 if (t
!= TOKEN_NONE
)
1549 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1551 while (buf
->buf
[pos
] == ' ')
1553 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1554 roff_addtbl(r
->man
, r
->tbl
);
1558 /* For now, let high level macros abort .ce mode. */
1560 if (ctl
&& roffce_node
!= NULL
&&
1561 (t
== TOKEN_NONE
|| t
== ROFF_EQ
|| t
== ROFF_TS
)) {
1562 r
->man
->last
= roffce_node
;
1563 r
->man
->next
= ROFF_NEXT_SIBLING
;
1569 * This is neither a roff request nor a user-defined macro.
1570 * Let the standard macro set parsers handle it.
1573 if (t
== TOKEN_NONE
)
1576 /* Execute a roff request or a user defined macro. */
1578 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1582 roff_endparse(struct roff
*r
)
1584 if (r
->last
!= NULL
)
1585 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1586 r
->last
->line
, r
->last
->col
,
1587 roff_name
[r
->last
->tok
]);
1589 if (r
->eqn
!= NULL
) {
1590 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1591 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1596 if (r
->tbl
!= NULL
) {
1597 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1598 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1605 * Parse a roff node's type from the input buffer. This must be in the
1606 * form of ".foo xxx" in the usual way.
1608 static enum roff_tok
1609 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1619 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1623 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1625 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1626 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1635 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1638 if (t
!= TOKEN_NONE
)
1643 /* --- handling of request blocks ----------------------------------------- */
1646 roff_cblock(ROFF_ARGS
)
1650 * A block-close `..' should only be invoked as a child of an
1651 * ignore macro, otherwise raise a warning and just ignore it.
1654 if (r
->last
== NULL
) {
1655 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1660 switch (r
->last
->tok
) {
1662 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1665 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1670 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1675 if (buf
->buf
[pos
] != '\0')
1676 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1677 ".. %s", buf
->buf
+ pos
);
1680 roffnode_cleanscope(r
);
1686 roffnode_cleanscope(struct roff
*r
)
1690 if (--r
->last
->endspan
!= 0)
1697 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1700 if (NULL
== r
->last
) {
1701 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1706 switch (r
->last
->tok
) {
1712 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1717 if (r
->last
->endspan
> -1) {
1718 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1724 roffnode_cleanscope(r
);
1729 roff_block(ROFF_ARGS
)
1731 const char *name
, *value
;
1732 char *call
, *cp
, *iname
, *rname
;
1733 size_t csz
, namesz
, rsz
;
1736 /* Ignore groff compatibility mode for now. */
1738 if (tok
== ROFF_de1
)
1740 else if (tok
== ROFF_dei1
)
1742 else if (tok
== ROFF_am1
)
1744 else if (tok
== ROFF_ami1
)
1747 /* Parse the macro name argument. */
1749 cp
= buf
->buf
+ pos
;
1750 if (tok
== ROFF_ig
) {
1755 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1756 iname
[namesz
] = '\0';
1759 /* Resolve the macro name argument if it is indirect. */
1761 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1762 deftype
= ROFFDEF_USER
;
1763 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1765 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1766 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1767 "%.*s", (int)namesz
, iname
);
1770 namesz
= strlen(name
);
1774 if (namesz
== 0 && tok
!= ROFF_ig
) {
1775 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1776 ln
, ppos
, roff_name
[tok
]);
1780 roffnode_push(r
, tok
, name
, ln
, ppos
);
1783 * At the beginning of a `de' macro, clear the existing string
1784 * with the same name, if there is one. New content will be
1785 * appended from roff_block_text() in multiline mode.
1788 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
1789 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1790 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1791 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
1792 deftype
= ROFFDEF_ANY
;
1793 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1794 switch (deftype
) { /* Before appending, ... */
1795 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
1796 roff_setstrn(&r
->strtab
, name
, namesz
,
1797 value
, strlen(value
), 0);
1799 case ROFFDEF_REN
: /* call original standard macro. */
1800 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1801 (int)strlen(value
), value
);
1802 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1803 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1806 case ROFFDEF_STD
: /* rename and call standard macro. */
1807 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
1808 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
1809 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1811 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1823 /* Get the custom end marker. */
1826 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1828 /* Resolve the end marker if it is indirect. */
1830 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1831 deftype
= ROFFDEF_USER
;
1832 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1834 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1835 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1836 "%.*s", (int)namesz
, iname
);
1839 namesz
= strlen(name
);
1844 r
->last
->end
= mandoc_strndup(name
, namesz
);
1847 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1848 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
1854 roff_block_sub(ROFF_ARGS
)
1860 * First check whether a custom macro exists at this level. If
1861 * it does, then check against it. This is some of groff's
1862 * stranger behaviours. If we encountered a custom end-scope
1863 * tag and that tag also happens to be a "real" macro, then we
1864 * need to try interpreting it again as a real macro. If it's
1865 * not, then return ignore. Else continue.
1869 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1870 if (buf
->buf
[i
] != r
->last
->end
[j
])
1873 if (r
->last
->end
[j
] == '\0' &&
1874 (buf
->buf
[i
] == '\0' ||
1875 buf
->buf
[i
] == ' ' ||
1876 buf
->buf
[i
] == '\t')) {
1878 roffnode_cleanscope(r
);
1880 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1884 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1892 * If we have no custom end-query or lookup failed, then try
1893 * pulling it out of the hashtable.
1896 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1898 if (t
!= ROFF_cblock
) {
1900 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1904 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1908 roff_block_text(ROFF_ARGS
)
1912 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1918 roff_cond_sub(ROFF_ARGS
)
1925 roffnode_cleanscope(r
);
1928 * If `\}' occurs on a macro line without a preceding macro,
1929 * drop the line completely.
1932 ep
= buf
->buf
+ pos
;
1933 if (ep
[0] == '\\' && ep
[1] == '}')
1936 /* Always check for the closing delimiter `\}'. */
1938 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1941 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
1942 roff_ccond(r
, ln
, ep
- buf
->buf
);
1954 * Fully handle known macros when they are structurally
1955 * required or when the conditional evaluated to true.
1958 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1959 return t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)
1960 ? (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) : rr
1961 ? ROFF_CONT
: ROFF_IGN
;
1965 roff_cond_text(ROFF_ARGS
)
1971 roffnode_cleanscope(r
);
1973 ep
= buf
->buf
+ pos
;
1974 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1975 if (*(++ep
) == '}') {
1977 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1982 return rr
? ROFF_CONT
: ROFF_IGN
;
1985 /* --- handling of numeric and conditional expressions -------------------- */
1988 * Parse a single signed integer number. Stop at the first non-digit.
1989 * If there is at least one digit, return success and advance the
1990 * parse point, else return failure and let the parse point unchanged.
1991 * Ignore overflows, treat them just like the C language.
1994 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
1996 int myres
, scaled
, n
, p
;
2003 if (n
|| v
[p
] == '+')
2006 if (flags
& ROFFNUM_WHITE
)
2007 while (isspace((unsigned char)v
[p
]))
2010 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2011 *res
= 10 * *res
+ v
[p
] - '0';
2018 /* Each number may be followed by one optional scaling unit. */
2022 scaled
= *res
* 65536;
2025 scaled
= *res
* 240;
2028 scaled
= *res
* 240 / 2.54;
2039 scaled
= *res
* 10 / 3;
2045 scaled
= *res
* 6 / 25;
2052 if (flags
& ROFFNUM_SCALE
)
2060 * Evaluate a string comparison condition.
2061 * The first character is the delimiter.
2062 * Succeed if the string up to its second occurrence
2063 * matches the string up to its third occurence.
2064 * Advance the cursor after the third occurrence
2065 * or lacking that, to the end of the line.
2068 roff_evalstrcond(const char *v
, int *pos
)
2070 const char *s1
, *s2
, *s3
;
2074 s1
= v
+ *pos
; /* initial delimiter */
2075 s2
= s1
+ 1; /* for scanning the first string */
2076 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2078 if (NULL
== s3
) /* found no middle delimiter */
2081 while ('\0' != *++s3
) {
2082 if (*s2
!= *s3
) { /* mismatch */
2083 s3
= strchr(s3
, *s1
);
2086 if (*s3
== *s1
) { /* found the final delimiter */
2095 s3
= strchr(s2
, '\0');
2096 else if (*s3
!= '\0')
2103 * Evaluate an optionally negated single character, numerical,
2104 * or string condition.
2107 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2111 int deftype
, number
, savepos
, istrue
, wanttrue
;
2113 if ('!' == v
[*pos
]) {
2138 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2141 else if (v
[*pos
] == 'r')
2142 istrue
= roff_hasregn(r
, name
, sz
);
2144 deftype
= ROFFDEF_ANY
;
2145 roff_getstrn(r
, name
, sz
, &deftype
);
2149 return istrue
== wanttrue
;
2155 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2156 return (number
> 0) == wanttrue
;
2157 else if (*pos
== savepos
)
2158 return roff_evalstrcond(v
, pos
) == wanttrue
;
2164 roff_line_ignore(ROFF_ARGS
)
2171 roff_insec(ROFF_ARGS
)
2174 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2175 ln
, ppos
, roff_name
[tok
]);
2180 roff_unsupp(ROFF_ARGS
)
2183 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2184 ln
, ppos
, roff_name
[tok
]);
2189 roff_cond(ROFF_ARGS
)
2192 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2195 * An `.el' has no conditional body: it will consume the value
2196 * of the current rstack entry set in prior `ie' calls or
2199 * If we're not an `el', however, then evaluate the conditional.
2202 r
->last
->rule
= tok
== ROFF_el
?
2203 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2204 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2207 * An if-else will put the NEGATION of the current evaluated
2208 * conditional into the stack of rules.
2211 if (tok
== ROFF_ie
) {
2212 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2214 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2215 r
->rstacksz
, sizeof(int));
2217 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2220 /* If the parent has false as its rule, then so do we. */
2222 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2227 * If there is nothing on the line after the conditional,
2228 * not even whitespace, use next-line scope.
2231 if (buf
->buf
[pos
] == '\0') {
2232 r
->last
->endspan
= 2;
2236 while (buf
->buf
[pos
] == ' ')
2239 /* An opening brace requests multiline scope. */
2241 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2242 r
->last
->endspan
= -1;
2244 while (buf
->buf
[pos
] == ' ')
2250 * Anything else following the conditional causes
2251 * single-line scope. Warn if the scope contains
2252 * nothing but trailing whitespace.
2255 if (buf
->buf
[pos
] == '\0')
2256 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2257 ln
, ppos
, roff_name
[tok
]);
2259 r
->last
->endspan
= 1;
2273 /* Ignore groff compatibility mode for now. */
2275 if (tok
== ROFF_ds1
)
2277 else if (tok
== ROFF_as1
)
2281 * The first word is the name of the string.
2282 * If it is empty or terminated by an escape sequence,
2283 * abort the `ds' request without defining anything.
2286 name
= string
= buf
->buf
+ pos
;
2290 namesz
= roff_getname(r
, &string
, ln
, pos
);
2291 if (name
[namesz
] == '\\')
2294 /* Read past the initial double-quote, if any. */
2298 /* The rest is the value. */
2299 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2301 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2306 * Parse a single operator, one or two characters long.
2307 * If the operator is recognized, return success and advance the
2308 * parse point, else return failure and let the parse point unchanged.
2311 roff_getop(const char *v
, int *pos
, char *res
)
2326 switch (v
[*pos
+ 1]) {
2344 switch (v
[*pos
+ 1]) {
2358 if ('=' == v
[*pos
+ 1])
2370 * Evaluate either a parenthesized numeric expression
2371 * or a single signed integer number.
2374 roff_evalpar(struct roff
*r
, int ln
,
2375 const char *v
, int *pos
, int *res
, int flags
)
2379 return roff_getnum(v
, pos
, res
, flags
);
2382 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2386 * Omission of the closing parenthesis
2387 * is an error in validation mode,
2388 * but ignored in evaluation mode.
2393 else if (NULL
== res
)
2400 * Evaluate a complete numeric expression.
2401 * Proceed left to right, there is no concept of precedence.
2404 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2405 int *pos
, int *res
, int flags
)
2407 int mypos
, operand2
;
2415 if (flags
& ROFFNUM_WHITE
)
2416 while (isspace((unsigned char)v
[*pos
]))
2419 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2423 if (flags
& ROFFNUM_WHITE
)
2424 while (isspace((unsigned char)v
[*pos
]))
2427 if ( ! roff_getop(v
, pos
, &operator))
2430 if (flags
& ROFFNUM_WHITE
)
2431 while (isspace((unsigned char)v
[*pos
]))
2434 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2437 if (flags
& ROFFNUM_WHITE
)
2438 while (isspace((unsigned char)v
[*pos
]))
2455 if (operand2
== 0) {
2456 mandoc_msg(MANDOCERR_DIVZERO
,
2457 r
->parse
, ln
, *pos
, v
);
2464 if (operand2
== 0) {
2465 mandoc_msg(MANDOCERR_DIVZERO
,
2466 r
->parse
, ln
, *pos
, v
);
2473 *res
= *res
< operand2
;
2476 *res
= *res
> operand2
;
2479 *res
= *res
<= operand2
;
2482 *res
= *res
>= operand2
;
2485 *res
= *res
== operand2
;
2488 *res
= *res
!= operand2
;
2491 *res
= *res
&& operand2
;
2494 *res
= *res
|| operand2
;
2497 if (operand2
< *res
)
2501 if (operand2
> *res
)
2511 /* --- register management ------------------------------------------------ */
2514 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2516 struct roffreg
*reg
;
2518 /* Search for an existing register with the same name. */
2521 while (reg
&& strcmp(name
, reg
->key
.p
))
2525 /* Create a new register. */
2526 reg
= mandoc_malloc(sizeof(struct roffreg
));
2527 reg
->key
.p
= mandoc_strdup(name
);
2528 reg
->key
.sz
= strlen(name
);
2530 reg
->next
= r
->regtab
;
2536 else if ('-' == sign
)
2543 * Handle some predefined read-only number registers.
2544 * For now, return -1 if the requested register is not predefined;
2545 * in case a predefined read-only register having the value -1
2546 * were to turn up, another special value would have to be chosen.
2549 roff_getregro(const struct roff
*r
, const char *name
)
2553 case '$': /* Number of arguments of the last macro evaluated. */
2555 case 'A': /* ASCII approximation mode is always off. */
2557 case 'g': /* Groff compatibility mode is always on. */
2559 case 'H': /* Fixed horizontal resolution. */
2561 case 'j': /* Always adjust left margin only. */
2563 case 'T': /* Some output device is always defined. */
2565 case 'V': /* Fixed vertical resolution. */
2573 roff_getreg(const struct roff
*r
, const char *name
)
2575 struct roffreg
*reg
;
2578 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2579 val
= roff_getregro(r
, name
+ 1);
2584 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2585 if (0 == strcmp(name
, reg
->key
.p
))
2592 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2594 struct roffreg
*reg
;
2597 if ('.' == name
[0] && 2 == len
) {
2598 val
= roff_getregro(r
, name
+ 1);
2603 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2604 if (len
== reg
->key
.sz
&&
2605 0 == strncmp(name
, reg
->key
.p
, len
))
2612 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2614 struct roffreg
*reg
;
2617 if ('.' == name
[0] && 2 == len
) {
2618 val
= roff_getregro(r
, name
+ 1);
2623 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2624 if (len
== reg
->key
.sz
&&
2625 0 == strncmp(name
, reg
->key
.p
, len
))
2632 roff_freereg(struct roffreg
*reg
)
2634 struct roffreg
*old_reg
;
2636 while (NULL
!= reg
) {
2652 key
= val
= buf
->buf
+ pos
;
2656 keysz
= roff_getname(r
, &val
, ln
, pos
);
2657 if (key
[keysz
] == '\\')
2662 if (sign
== '+' || sign
== '-')
2665 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2666 roff_setreg(r
, key
, iv
, sign
);
2674 struct roffreg
*reg
, **prev
;
2678 name
= cp
= buf
->buf
+ pos
;
2681 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2682 name
[namesz
] = '\0';
2687 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2699 /* --- handler functions for roff requests -------------------------------- */
2708 cp
= buf
->buf
+ pos
;
2709 while (*cp
!= '\0') {
2711 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2712 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2713 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2714 if (name
[namesz
] == '\\')
2725 /* Parse the number of lines. */
2727 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2728 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2729 ln
, ppos
, buf
->buf
+ 1);
2733 while (isspace((unsigned char)buf
->buf
[pos
]))
2737 * Arm the input line trap.
2738 * Special-casing "an-trap" is an ugly workaround to cope
2739 * with DocBook stupidly fiddling with man(7) internals.
2743 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2744 strcmp(buf
->buf
+ pos
, "an-trap") ?
2745 buf
->buf
+ pos
: "br");
2753 enum roff_tok t
, te
;
2760 r
->format
= MPARSE_MDOC
;
2761 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
2767 r
->format
= MPARSE_MAN
;
2768 mask
= MPARSE_QUICK
;
2773 if ((r
->options
& mask
) == 0)
2774 for (t
= tok
; t
< te
; t
++)
2775 roff_setstr(r
, roff_name
[t
], NULL
, 0);
2782 if (r
->tbl
== NULL
) {
2783 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2787 if (tbl_end(r
->tbl
) == 0) {
2790 buf
->buf
= mandoc_strdup(".sp");
2792 return ROFF_REPARSE
;
2803 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2806 tbl_restart(ln
, ppos
, r
->tbl
);
2812 * Handle in-line equation delimiters.
2815 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2818 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2821 * Outside equations, look for an opening delimiter.
2822 * If we are inside an equation, we already know it is
2823 * in-line, or this function wouldn't have been called;
2824 * so look for a closing delimiter.
2827 cp1
= buf
->buf
+ pos
;
2828 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2829 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2834 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2836 /* Handle preceding text, protecting whitespace. */
2838 if (*buf
->buf
!= '\0') {
2845 * Prepare replacing the delimiter with an equation macro
2846 * and drop leading white space from the equation.
2849 if (r
->eqn
== NULL
) {
2856 /* Handle following text, protecting whitespace. */
2864 /* Do the actual replacement. */
2866 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2867 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2871 /* Toggle the in-line state of the eqn subsystem. */
2873 r
->eqn_inline
= r
->eqn
== NULL
;
2874 return ROFF_REPARSE
;
2880 struct roff_node
*n
;
2882 if (r
->man
->macroset
== MACROSET_MAN
)
2883 man_breakscope(r
->man
, ROFF_EQ
);
2884 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
2885 if (ln
> r
->man
->last
->line
)
2886 n
->flags
|= NODE_LINE
;
2887 n
->eqn
= mandoc_calloc(1, sizeof(*n
->eqn
));
2888 n
->eqn
->expectargs
= UINT_MAX
;
2889 roff_node_append(r
->man
, n
);
2890 r
->man
->next
= ROFF_NEXT_SIBLING
;
2892 assert(r
->eqn
== NULL
);
2893 if (r
->last_eqn
== NULL
)
2894 r
->last_eqn
= eqn_alloc(r
->parse
);
2896 eqn_reset(r
->last_eqn
);
2897 r
->eqn
= r
->last_eqn
;
2900 if (buf
->buf
[pos
] != '\0')
2901 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2902 ".EQ %s", buf
->buf
+ pos
);
2910 if (r
->eqn
!= NULL
) {
2914 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2915 if (buf
->buf
[pos
] != '\0')
2916 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2917 "EN %s", buf
->buf
+ pos
);
2924 if (r
->tbl
!= NULL
) {
2925 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2926 ln
, ppos
, "TS breaks TS");
2929 r
->tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2931 r
->last_tbl
->next
= r
->tbl
;
2933 r
->first_tbl
= r
->tbl
;
2934 r
->last_tbl
= r
->tbl
;
2939 roff_onearg(ROFF_ARGS
)
2941 struct roff_node
*n
;
2945 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
2946 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
2948 man_breakscope(r
->man
, tok
);
2950 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
2951 r
->man
->last
= roffce_node
;
2952 r
->man
->next
= ROFF_NEXT_SIBLING
;
2955 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
2958 cp
= buf
->buf
+ pos
;
2960 while (*cp
!= '\0' && *cp
!= ' ')
2965 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
2966 r
->parse
, ln
, cp
- buf
->buf
,
2967 "%s ... %s", roff_name
[tok
], cp
);
2968 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
2971 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
2972 if (r
->man
->last
->type
== ROFFT_ELEM
) {
2973 roff_word_alloc(r
->man
, ln
, pos
, "1");
2974 r
->man
->last
->flags
|= NODE_NOSRC
;
2977 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
2978 &roffce_lines
, 0) == 0) {
2979 mandoc_vmsg(MANDOCERR_CE_NONUM
,
2980 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
2983 if (roffce_lines
< 1) {
2984 r
->man
->last
= r
->man
->last
->parent
;
2988 roffce_node
= r
->man
->last
->parent
;
2990 n
->flags
|= NODE_VALID
| NODE_ENDED
;
2993 n
->flags
|= NODE_LINE
;
2994 r
->man
->next
= ROFF_NEXT_SIBLING
;
2999 roff_manyarg(ROFF_ARGS
)
3001 struct roff_node
*n
;
3004 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3007 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3008 while (*ep
!= '\0' && *ep
!= ' ')
3012 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3015 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3017 r
->man
->next
= ROFF_NEXT_SIBLING
;
3024 char *oldn
, *newn
, *end
, *value
;
3025 size_t oldsz
, newsz
, valsz
;
3027 newn
= oldn
= buf
->buf
+ pos
;
3031 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3032 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3036 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3040 valsz
= mandoc_asprintf(&value
, ".%.*s \\$*\\\"\n",
3042 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3043 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3051 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3052 man_breakscope(r
->man
, ROFF_br
);
3053 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3054 if (buf
->buf
[pos
] != '\0')
3055 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3056 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3057 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3058 r
->man
->next
= ROFF_NEXT_SIBLING
;
3069 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3073 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3074 ln
, p
- buf
->buf
, "cc ... %s", p
);
3090 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3091 ln
, p
- buf
->buf
, "ec ... %s", p
);
3100 if (buf
->buf
[pos
] != '\0')
3101 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3102 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3109 const char *p
, *first
, *second
;
3111 enum mandoc_esc esc
;
3116 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3120 while (*p
!= '\0') {
3124 if (*first
== '\\') {
3125 esc
= mandoc_escape(&p
, NULL
, NULL
);
3126 if (esc
== ESCAPE_ERROR
) {
3127 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3128 ln
, (int)(p
- buf
->buf
), first
);
3131 fsz
= (size_t)(p
- first
);
3135 if (*second
== '\\') {
3136 esc
= mandoc_escape(&p
, NULL
, NULL
);
3137 if (esc
== ESCAPE_ERROR
) {
3138 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3139 ln
, (int)(p
- buf
->buf
), second
);
3142 ssz
= (size_t)(p
- second
);
3143 } else if (*second
== '\0') {
3144 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3145 ln
, first
- buf
->buf
, "tr %s", first
);
3151 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3156 if (r
->xtab
== NULL
)
3157 r
->xtab
= mandoc_calloc(128,
3158 sizeof(struct roffstr
));
3160 free(r
->xtab
[(int)*first
].p
);
3161 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3162 r
->xtab
[(int)*first
].sz
= ssz
;
3172 char *oldn
, *newn
, *end
;
3173 size_t oldsz
, newsz
;
3176 oldn
= newn
= buf
->buf
+ pos
;
3180 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3181 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3185 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3189 deftype
= ROFFDEF_ANY
;
3190 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3193 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3194 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3195 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3198 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3199 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3202 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3203 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3204 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3207 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3208 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3211 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3212 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3223 name
= buf
->buf
+ pos
;
3224 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3227 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3228 * opening anything that's not in our cwd or anything beneath
3229 * it. Thus, explicitly disallow traversing up the file-system
3230 * or using absolute paths.
3233 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3234 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3236 buf
->sz
= mandoc_asprintf(&cp
,
3237 ".sp\nSee the file %s.\n.sp", name
) + 1;
3241 return ROFF_REPARSE
;
3248 /* --- user defined strings and macros ------------------------------------ */
3251 roff_userdef(ROFF_ARGS
)
3253 const char *arg
[16], *ap
;
3255 int expand_count
, i
, ib
, ie
;
3259 * Collect pointers to macro argument strings
3260 * and NUL-terminate them.
3264 cp
= buf
->buf
+ pos
;
3265 for (i
= 0; i
< 16; i
++) {
3269 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3275 * Expand macro arguments.
3278 buf
->sz
= strlen(r
->current_string
) + 1;
3279 n1
= n2
= cp
= mandoc_malloc(buf
->sz
);
3280 memcpy(n1
, r
->current_string
, buf
->sz
);
3282 while (*cp
!= '\0') {
3284 /* Scan ahead for the next argument invocation. */
3290 if (*cp
== '*') { /* \\$* inserts all arguments */
3293 } else { /* \\$1 .. \\$9 insert one argument */
3294 ib
= ie
= *cp
- '1';
3295 if (ib
< 0 || ib
> 8)
3301 * Prevent infinite recursion.
3306 else if (++expand_count
> EXPAND_LIMIT
) {
3307 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
3308 ln
, (int)(cp
- n1
), NULL
);
3315 * Determine the size of the expanded argument,
3316 * taking escaping of quotes into account.
3319 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3320 for (i
= ib
; i
<= ie
; i
++) {
3321 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3330 * Determine the size of the rest of the
3331 * unexpanded macro, including the NUL.
3334 rsz
= buf
->sz
- (cp
- n1
) - 3;
3337 * When shrinking, move before
3338 * releasing the storage.
3342 memmove(cp
+ asz
, cp
+ 3, rsz
);
3345 * Resize the storage for the macro
3346 * and readjust the parse pointer.
3350 n2
= mandoc_realloc(n1
, buf
->sz
);
3351 cp
= n2
+ (cp
- n1
);
3355 * When growing, make room
3356 * for the expanded argument.
3360 memmove(cp
+ asz
, cp
+ 3, rsz
);
3363 /* Copy the expanded argument, escaping quotes. */
3366 for (i
= ib
; i
<= ie
; i
++) {
3367 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3369 memcpy(n2
, "\\(dq", 4);
3380 * Replace the macro invocation
3381 * by the expanded macro.
3388 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3389 ROFF_REPARSE
: ROFF_APPEND
;
3393 * Calling a high-level macro that was renamed with .rn.
3394 * r->current_string has already been set up by roff_parse().
3397 roff_renamed(ROFF_ARGS
)
3401 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3402 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3409 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3418 /* Read until end of name and terminate it with NUL. */
3419 for (cp
= name
; 1; cp
++) {
3420 if ('\0' == *cp
|| ' ' == *cp
) {
3427 if ('{' == cp
[1] || '}' == cp
[1])
3432 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3433 "%.*s", (int)(cp
- name
+ 1), name
);
3434 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3438 /* Read past spaces. */
3447 * Store *string into the user-defined string called *name.
3448 * To clear an existing entry, call with (*r, *name, NULL, 0).
3449 * append == 0: replace mode
3450 * append == 1: single-line append mode
3451 * append == 2: multiline append mode, append '\n' after each call
3454 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3459 namesz
= strlen(name
);
3460 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3461 string
? strlen(string
) : 0, append
);
3462 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3466 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3467 const char *string
, size_t stringsz
, int append
)
3472 size_t oldch
, newch
;
3474 /* Search for an existing string with the same name. */
3477 while (n
&& (namesz
!= n
->key
.sz
||
3478 strncmp(n
->key
.p
, name
, namesz
)))
3482 /* Create a new string table entry. */
3483 n
= mandoc_malloc(sizeof(struct roffkv
));
3484 n
->key
.p
= mandoc_strndup(name
, namesz
);
3490 } else if (0 == append
) {
3500 * One additional byte for the '\n' in multiline mode,
3501 * and one for the terminating '\0'.
3503 newch
= stringsz
+ (1 < append
? 2u : 1u);
3505 if (NULL
== n
->val
.p
) {
3506 n
->val
.p
= mandoc_malloc(newch
);
3511 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3514 /* Skip existing content in the destination buffer. */
3515 c
= n
->val
.p
+ (int)oldch
;
3517 /* Append new content to the destination buffer. */
3519 while (i
< (int)stringsz
) {
3521 * Rudimentary roff copy mode:
3522 * Handle escaped backslashes.
3524 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3529 /* Append terminating bytes. */
3534 n
->val
.sz
= (int)(c
- n
->val
.p
);
3538 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
,
3541 const struct roffkv
*n
;
3545 if (*deftype
& ROFFDEF_USER
) {
3546 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
3547 if (strncmp(name
, n
->key
.p
, len
) == 0 &&
3548 n
->key
.p
[len
] == '\0' &&
3550 *deftype
= ROFFDEF_USER
;
3555 if (*deftype
& ROFFDEF_PRE
) {
3556 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3557 if (strncmp(name
, predefs
[i
].name
, len
) == 0 &&
3558 predefs
[i
].name
[len
] == '\0') {
3559 *deftype
= ROFFDEF_PRE
;
3560 return predefs
[i
].str
;
3564 if (*deftype
& ROFFDEF_REN
) {
3565 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
3566 if (strncmp(name
, n
->key
.p
, len
) == 0 &&
3567 n
->key
.p
[len
] == '\0' &&
3569 *deftype
= ROFFDEF_REN
;
3574 if (*deftype
& ROFFDEF_STD
) {
3575 if (r
->man
->macroset
!= MACROSET_MAN
) {
3576 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
3577 if (strncmp(name
, roff_name
[tok
], len
) == 0 &&
3578 roff_name
[tok
][len
] == '\0') {
3579 *deftype
= ROFFDEF_STD
;
3584 if (r
->man
->macroset
!= MACROSET_MDOC
) {
3585 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
3586 if (strncmp(name
, roff_name
[tok
], len
) == 0 &&
3587 roff_name
[tok
][len
] == '\0') {
3588 *deftype
= ROFFDEF_STD
;
3599 roff_freestr(struct roffkv
*r
)
3601 struct roffkv
*n
, *nn
;
3603 for (n
= r
; n
; n
= nn
) {
3611 /* --- accessors and utility functions ------------------------------------ */
3614 * Duplicate an input string, making the appropriate character
3615 * conversations (as stipulated by `tr') along the way.
3616 * Returns a heap-allocated string with all the replacements made.
3619 roff_strdup(const struct roff
*r
, const char *p
)
3621 const struct roffkv
*cp
;
3625 enum mandoc_esc esc
;
3627 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3628 return mandoc_strdup(p
);
3629 else if ('\0' == *p
)
3630 return mandoc_strdup("");
3633 * Step through each character looking for term matches
3634 * (remember that a `tr' can be invoked with an escape, which is
3635 * a glyph but the escape is multi-character).
3636 * We only do this if the character hash has been initialised
3637 * and the string is >0 length.
3643 while ('\0' != *p
) {
3644 assert((unsigned int)*p
< 128);
3645 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3646 sz
= r
->xtab
[(int)*p
].sz
;
3647 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3648 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3652 } else if ('\\' != *p
) {
3653 res
= mandoc_realloc(res
, ssz
+ 2);
3658 /* Search for term matches. */
3659 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3660 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3665 * A match has been found.
3666 * Append the match to the array and move
3667 * forward by its keysize.
3669 res
= mandoc_realloc(res
,
3670 ssz
+ cp
->val
.sz
+ 1);
3671 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3673 p
+= (int)cp
->key
.sz
;
3678 * Handle escapes carefully: we need to copy
3679 * over just the escape itself, or else we might
3680 * do replacements within the escape itself.
3681 * Make sure to pass along the bogus string.
3684 esc
= mandoc_escape(&p
, NULL
, NULL
);
3685 if (ESCAPE_ERROR
== esc
) {
3687 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3688 memcpy(res
+ ssz
, pp
, sz
);
3692 * We bail out on bad escapes.
3693 * No need to warn: we already did so when
3694 * roff_res() was called.
3697 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3698 memcpy(res
+ ssz
, pp
, sz
);
3702 res
[(int)ssz
] = '\0';
3707 roff_getformat(const struct roff
*r
)
3714 * Find out whether a line is a macro line or not.
3715 * If it is, adjust the current position and return one; if it isn't,
3716 * return zero and don't change the current position.
3717 * If the control character has been set with `.cc', then let that grain
3719 * This is slighly contrary to groff, where using the non-breaking
3720 * control character when `cc' has been invoked will cause the
3721 * non-breaking macro contents to be printed verbatim.
3724 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3730 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
3732 else if (r
->control
!= '\0')
3734 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3736 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3741 while (' ' == cp
[pos
] || '\t' == cp
[pos
])