1 /* $Id: roff.c,v 1.329 2018/08/01 15:40:17 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017, 2018 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)
49 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
51 /* --- data types --------------------------------------------------------- */
54 * An incredibly-simple string buffer.
57 char *p
; /* nil-terminated buffer */
58 size_t sz
; /* saved strlen(p) */
62 * A key-value roffstr pair as part of a singly-linked list.
67 struct roffkv
*next
; /* next in list */
71 * A single number register as part of a singly-linked list.
81 * Association of request and macro names with token IDs.
89 struct mparse
*parse
; /* parse point */
90 struct roff_man
*man
; /* mdoc or man parser */
91 struct roffnode
*last
; /* leaf of stack */
92 int *rstack
; /* stack of inverted `ie' values */
93 struct ohash
*reqtab
; /* request lookup table */
94 struct roffreg
*regtab
; /* number registers */
95 struct roffkv
*strtab
; /* user-defined strings & macros */
96 struct roffkv
*rentab
; /* renamed strings & macros */
97 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
98 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
99 const char *current_string
; /* value of last called user macro */
100 struct tbl_node
*first_tbl
; /* first table parsed */
101 struct tbl_node
*last_tbl
; /* last table parsed */
102 struct tbl_node
*tbl
; /* current table being parsed */
103 struct eqn_node
*last_eqn
; /* equation parser */
104 struct eqn_node
*eqn
; /* active equation parser */
105 int eqn_inline
; /* current equation is inline */
106 int options
; /* parse options */
107 int rstacksz
; /* current size limit of rstack */
108 int rstackpos
; /* position in rstack */
109 int format
; /* current file in mdoc or man format */
110 int argc
; /* number of args of the last macro */
111 char control
; /* control character */
112 char escape
; /* escape character */
116 enum roff_tok tok
; /* type of node */
117 struct roffnode
*parent
; /* up one in stack */
118 int line
; /* parse line */
119 int col
; /* parse col */
120 char *name
; /* node name, e.g. macro name */
121 char *end
; /* end-rules: custom token */
122 int endspan
; /* end-rules: next-line or infty */
123 int rule
; /* current evaluation rule */
126 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
127 enum roff_tok tok, /* tok of macro */ \
128 struct buf *buf, /* input buffer */ \
129 int ln, /* parse line */ \
130 int ppos, /* original pos in buffer */ \
131 int pos, /* current pos in buffer */ \
132 int *offs /* reset offset of buffer data */
134 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
137 roffproc proc
; /* process new macro */
138 roffproc text
; /* process as child text of macro */
139 roffproc sub
; /* process as child of macro */
141 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
145 const char *name
; /* predefined input name */
146 const char *str
; /* replacement symbol */
149 #define PREDEF(__name, __str) \
150 { (__name), (__str) },
152 /* --- function prototypes ------------------------------------------------ */
154 static void roffnode_cleanscope(struct roff
*);
155 static void roffnode_pop(struct roff
*);
156 static void roffnode_push(struct roff
*, enum roff_tok
,
157 const char *, int, int);
158 static void roff_addtbl(struct roff_man
*, struct tbl_node
*);
159 static enum rofferr
roff_als(ROFF_ARGS
);
160 static enum rofferr
roff_block(ROFF_ARGS
);
161 static enum rofferr
roff_block_text(ROFF_ARGS
);
162 static enum rofferr
roff_block_sub(ROFF_ARGS
);
163 static enum rofferr
roff_br(ROFF_ARGS
);
164 static enum rofferr
roff_cblock(ROFF_ARGS
);
165 static enum rofferr
roff_cc(ROFF_ARGS
);
166 static void roff_ccond(struct roff
*, int, int);
167 static enum rofferr
roff_cond(ROFF_ARGS
);
168 static enum rofferr
roff_cond_text(ROFF_ARGS
);
169 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
170 static enum rofferr
roff_ds(ROFF_ARGS
);
171 static enum rofferr
roff_ec(ROFF_ARGS
);
172 static enum rofferr
roff_eo(ROFF_ARGS
);
173 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
174 static int roff_evalcond(struct roff
*r
, int, char *, int *);
175 static int roff_evalnum(struct roff
*, int,
176 const char *, int *, int *, int);
177 static int roff_evalpar(struct roff
*, int,
178 const char *, int *, int *, int);
179 static int roff_evalstrcond(const char *, int *);
180 static void roff_free1(struct roff
*);
181 static void roff_freereg(struct roffreg
*);
182 static void roff_freestr(struct roffkv
*);
183 static size_t roff_getname(struct roff
*, char **, int, int);
184 static int roff_getnum(const char *, int *, int *, int);
185 static int roff_getop(const char *, int *, char *);
186 static int roff_getregn(struct roff
*,
187 const char *, size_t, char);
188 static int roff_getregro(const struct roff
*,
190 static const char *roff_getstrn(struct roff
*,
191 const char *, size_t, int *);
192 static int roff_hasregn(const struct roff
*,
193 const char *, size_t);
194 static enum rofferr
roff_insec(ROFF_ARGS
);
195 static enum rofferr
roff_it(ROFF_ARGS
);
196 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
197 static void roff_man_alloc1(struct roff_man
*);
198 static void roff_man_free1(struct roff_man
*);
199 static enum rofferr
roff_manyarg(ROFF_ARGS
);
200 static enum rofferr
roff_nr(ROFF_ARGS
);
201 static enum rofferr
roff_onearg(ROFF_ARGS
);
202 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
204 static enum rofferr
roff_parsetext(struct roff
*, struct buf
*,
206 static enum rofferr
roff_renamed(ROFF_ARGS
);
207 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
208 static enum rofferr
roff_rm(ROFF_ARGS
);
209 static enum rofferr
roff_rn(ROFF_ARGS
);
210 static enum rofferr
roff_rr(ROFF_ARGS
);
211 static void roff_setregn(struct roff
*, const char *,
212 size_t, int, char, int);
213 static void roff_setstr(struct roff
*,
214 const char *, const char *, int);
215 static void roff_setstrn(struct roffkv
**, const char *,
216 size_t, const char *, size_t, int);
217 static enum rofferr
roff_so(ROFF_ARGS
);
218 static enum rofferr
roff_tr(ROFF_ARGS
);
219 static enum rofferr
roff_Dd(ROFF_ARGS
);
220 static enum rofferr
roff_TE(ROFF_ARGS
);
221 static enum rofferr
roff_TS(ROFF_ARGS
);
222 static enum rofferr
roff_EQ(ROFF_ARGS
);
223 static enum rofferr
roff_EN(ROFF_ARGS
);
224 static enum rofferr
roff_T_(ROFF_ARGS
);
225 static enum rofferr
roff_unsupp(ROFF_ARGS
);
226 static enum rofferr
roff_userdef(ROFF_ARGS
);
228 /* --- constant data ------------------------------------------------------ */
230 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
231 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
233 const char *__roff_name
[MAN_MAX
+ 1] = {
234 "br", "ce", "ft", "ll",
235 "mc", "po", "rj", "sp",
237 "ab", "ad", "af", "aln",
238 "als", "am", "am1", "ami",
239 "ami1", "as", "as1", "asciify",
240 "backtrace", "bd", "bleedat", "blm",
241 "box", "boxa", "bp", "BP",
242 "break", "breakchar", "brnl", "brp",
244 "cf", "cflags", "ch", "char",
245 "chop", "class", "close", "CL",
246 "color", "composite", "continue", "cp",
247 "cropat", "cs", "cu", "da",
248 "dch", "Dd", "de", "de1",
249 "defcolor", "dei", "dei1", "device",
250 "devicem", "di", "do", "ds",
251 "ds1", "dwh", "dt", "ec",
252 "ecr", "ecs", "el", "em",
253 "EN", "eo", "EP", "EQ",
254 "errprint", "ev", "evc", "ex",
255 "fallback", "fam", "fc", "fchar",
256 "fcolor", "fdeferlig", "feature", "fkern",
257 "fl", "flig", "fp", "fps",
258 "fschar", "fspacewidth", "fspecial", "ftr",
259 "fzoom", "gcolor", "hc", "hcode",
260 "hidechar", "hla", "hlm", "hpf",
261 "hpfa", "hpfcode", "hw", "hy",
262 "hylang", "hylen", "hym", "hypp",
263 "hys", "ie", "if", "ig",
264 "index", "it", "itc", "IX",
265 "kern", "kernafter", "kernbefore", "kernpair",
266 "lc", "lc_ctype", "lds", "length",
267 "letadj", "lf", "lg", "lhang",
268 "linetabs", "lnr", "lnrf", "lpfx",
270 "mediasize", "minss", "mk", "mso",
271 "na", "ne", "nh", "nhychar",
272 "nm", "nn", "nop", "nr",
273 "nrf", "nroff", "ns", "nx",
274 "open", "opena", "os", "output",
275 "padj", "papersize", "pc", "pev",
276 "pi", "PI", "pl", "pm",
278 "psbb", "pshape", "pso", "ptr",
279 "pvs", "rchar", "rd", "recursionlimit",
280 "return", "rfschar", "rhang",
281 "rm", "rn", "rnn", "rr",
282 "rs", "rt", "schar", "sentchar",
283 "shc", "shift", "sizes", "so",
284 "spacewidth", "special", "spreadwarn", "ss",
285 "sty", "substring", "sv", "sy",
288 "tm", "tm1", "tmc", "tr",
289 "track", "transchar", "trf", "trimat",
290 "trin", "trnt", "troff", "TS",
291 "uf", "ul", "unformat", "unwatch",
292 "unwatchn", "vpt", "vs", "warn",
293 "warnscale", "watch", "watchlength", "watchn",
294 "wh", "while", "write", "writec",
295 "writem", "xflag", ".", NULL
,
297 "Dd", "Dt", "Os", "Sh",
298 "Ss", "Pp", "D1", "Dl",
299 "Bd", "Ed", "Bl", "El",
300 "It", "Ad", "An", "Ap",
301 "Ar", "Cd", "Cm", "Dv",
302 "Er", "Ev", "Ex", "Fa",
303 "Fd", "Fl", "Fn", "Ft",
304 "Ic", "In", "Li", "Nd",
305 "Nm", "Op", "Ot", "Pa",
306 "Rv", "St", "Va", "Vt",
307 "Xr", "%A", "%B", "%D",
308 "%I", "%J", "%N", "%O",
309 "%P", "%R", "%T", "%V",
310 "Ac", "Ao", "Aq", "At",
311 "Bc", "Bf", "Bo", "Bq",
312 "Bsx", "Bx", "Db", "Dc",
313 "Do", "Dq", "Ec", "Ef",
314 "Em", "Eo", "Fx", "Ms",
315 "No", "Ns", "Nx", "Ox",
316 "Pc", "Pf", "Po", "Pq",
317 "Qc", "Ql", "Qo", "Qq",
318 "Re", "Rs", "Sc", "So",
319 "Sq", "Sm", "Sx", "Sy",
320 "Tn", "Ux", "Xc", "Xo",
321 "Fo", "Fc", "Oo", "Oc",
322 "Bk", "Ek", "Bt", "Hf",
323 "Fr", "Ud", "Lb", "Lp",
324 "Lk", "Mt", "Brq", "Bro",
325 "Brc", "%C", "Es", "En",
326 "Dx", "%Q", "%U", "Ta",
328 "TH", "SH", "SS", "TP",
329 "LP", "PP", "P", "IP",
330 "HP", "SM", "SB", "BI",
331 "IB", "BR", "RB", "R",
332 "B", "I", "IR", "RI",
334 "RE", "RS", "DT", "UC",
336 "OP", "EX", "EE", "UR",
337 "UE", "MT", "ME", NULL
339 const char *const *roff_name
= __roff_name
;
341 static struct roffmac roffs
[TOKEN_NONE
] = {
342 { roff_br
, NULL
, NULL
, 0 }, /* br */
343 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
344 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
345 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
346 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
347 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
348 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
349 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
350 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
351 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
352 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
353 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
354 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
355 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
356 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
357 { roff_als
, NULL
, NULL
, 0 }, /* als */
358 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
359 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
360 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
361 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
362 { roff_ds
, NULL
, NULL
, 0 }, /* as */
363 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
364 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
365 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
366 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
367 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
368 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
369 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
370 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
371 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
372 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
373 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
374 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
375 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
376 { roff_br
, NULL
, NULL
, 0 }, /* brp */
377 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
378 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
379 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
380 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
381 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
382 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
383 { roff_unsupp
, NULL
, NULL
, 0 }, /* char */
384 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
385 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
386 { roff_insec
, NULL
, NULL
, 0 }, /* close */
387 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
388 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
389 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
390 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
391 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
392 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
393 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
394 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
395 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
396 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
397 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
398 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
399 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
400 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
401 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
402 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
403 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
404 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
405 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
406 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
407 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
408 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
409 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
410 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
411 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
412 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
413 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
414 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
415 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
416 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
417 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
418 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
419 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
420 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
421 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
425 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
426 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
427 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
428 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
429 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
430 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
431 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
432 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
433 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
434 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
435 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
436 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
437 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
438 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
439 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
440 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
441 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
444 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
445 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
446 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
453 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
454 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
457 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
458 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
459 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
460 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
461 { roff_it
, NULL
, NULL
, 0 }, /* it */
462 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
468 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
469 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
470 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
471 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
473 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
475 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
476 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
477 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
478 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
479 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
481 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
486 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
487 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
488 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
489 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
491 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
492 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
493 { roff_unsupp
, NULL
, NULL
, 0 }, /* nop */
494 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
495 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
496 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
497 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
498 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
499 { roff_insec
, NULL
, NULL
, 0 }, /* open */
500 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
502 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
504 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
507 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
508 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
509 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
510 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
511 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
512 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
513 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
514 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
515 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
516 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
517 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
518 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
519 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
521 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
522 { roff_unsupp
, NULL
, NULL
, 0 }, /* return */
523 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
524 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
525 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
526 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
527 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
528 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
531 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
532 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
534 { roff_unsupp
, NULL
, NULL
, 0 }, /* shift */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
536 { roff_so
, NULL
, NULL
, 0 }, /* so */
537 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
538 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
540 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
541 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
542 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
543 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
544 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
545 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
546 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
547 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
548 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
550 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
552 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
554 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
555 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
557 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
559 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
560 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
561 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
562 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
563 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
564 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
565 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
566 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
572 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
574 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
575 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
576 { roff_unsupp
, NULL
, NULL
, 0 }, /* while */
577 { roff_insec
, NULL
, NULL
, 0 }, /* write */
578 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
579 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
580 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
581 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
582 { roff_renamed
, NULL
, NULL
, 0 },
583 { roff_userdef
, NULL
, NULL
, 0 }
586 /* Array of injected predefined strings. */
587 #define PREDEFS_MAX 38
588 static const struct predef predefs
[PREDEFS_MAX
] = {
589 #include "predefs.in"
592 static int roffce_lines
; /* number of input lines to center */
593 static struct roff_node
*roffce_node
; /* active request */
594 static int roffit_lines
; /* number of lines to delay */
595 static char *roffit_macro
; /* nil-terminated macro line */
598 /* --- request table ------------------------------------------------------ */
601 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
609 htab
= mandoc_malloc(sizeof(*htab
));
610 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
612 for (tok
= mintok
; tok
< maxtok
; tok
++) {
613 if (roff_name
[tok
] == NULL
)
615 sz
= strlen(roff_name
[tok
]);
616 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
618 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
619 slot
= ohash_qlookup(htab
, req
->name
);
620 ohash_insert(htab
, slot
, req
);
626 roffhash_free(struct ohash
*htab
)
633 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
634 req
= ohash_next(htab
, &slot
))
641 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
648 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
650 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
651 return req
== NULL
? TOKEN_NONE
: req
->tok
;
654 /* --- stack of request blocks -------------------------------------------- */
657 * Pop the current node off of the stack of roff instructions currently
661 roffnode_pop(struct roff
*r
)
668 r
->last
= r
->last
->parent
;
675 * Push a roff node onto the instruction stack. This must later be
676 * removed with roffnode_pop().
679 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
684 p
= mandoc_calloc(1, sizeof(struct roffnode
));
687 p
->name
= mandoc_strdup(name
);
691 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
696 /* --- roff parser state data management ---------------------------------- */
699 roff_free1(struct roff
*r
)
701 struct tbl_node
*tbl
;
704 while (NULL
!= (tbl
= r
->first_tbl
)) {
705 r
->first_tbl
= tbl
->next
;
708 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
710 if (r
->last_eqn
!= NULL
)
711 eqn_free(r
->last_eqn
);
712 r
->last_eqn
= r
->eqn
= NULL
;
722 roff_freereg(r
->regtab
);
725 roff_freestr(r
->strtab
);
726 roff_freestr(r
->rentab
);
727 roff_freestr(r
->xmbtab
);
728 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
731 for (i
= 0; i
< 128; i
++)
738 roff_reset(struct roff
*r
)
741 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
751 roff_free(struct roff
*r
)
754 roffhash_free(r
->reqtab
);
759 roff_alloc(struct mparse
*parse
, int options
)
763 r
= mandoc_calloc(1, sizeof(struct roff
));
765 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
766 r
->options
= options
;
767 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
773 /* --- syntax tree state data management ---------------------------------- */
776 roff_man_free1(struct roff_man
*man
)
779 if (man
->first
!= NULL
)
780 roff_node_delete(man
, man
->first
);
781 free(man
->meta
.msec
);
784 free(man
->meta
.arch
);
785 free(man
->meta
.title
);
786 free(man
->meta
.name
);
787 free(man
->meta
.date
);
791 roff_man_alloc1(struct roff_man
*man
)
794 memset(&man
->meta
, 0, sizeof(man
->meta
));
795 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
796 man
->first
->type
= ROFFT_ROOT
;
797 man
->last
= man
->first
;
800 man
->macroset
= MACROSET_NONE
;
801 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
802 man
->next
= ROFF_NEXT_CHILD
;
806 roff_man_reset(struct roff_man
*man
)
810 roff_man_alloc1(man
);
814 roff_man_free(struct roff_man
*man
)
822 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
823 const char *os_s
, int quick
)
825 struct roff_man
*man
;
827 man
= mandoc_calloc(1, sizeof(*man
));
832 roff_man_alloc1(man
);
837 /* --- syntax tree handling ----------------------------------------------- */
840 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
841 enum roff_type type
, int tok
)
845 n
= mandoc_calloc(1, sizeof(*n
));
850 n
->sec
= man
->lastsec
;
852 if (man
->flags
& MDOC_SYNOPSIS
)
853 n
->flags
|= NODE_SYNPRETTY
;
855 n
->flags
&= ~NODE_SYNPRETTY
;
856 if (man
->flags
& MDOC_NEWLINE
)
857 n
->flags
|= NODE_LINE
;
858 man
->flags
&= ~MDOC_NEWLINE
;
864 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
868 case ROFF_NEXT_SIBLING
:
869 if (man
->last
->next
!= NULL
) {
870 n
->next
= man
->last
->next
;
871 man
->last
->next
->prev
= n
;
873 man
->last
->parent
->last
= n
;
876 n
->parent
= man
->last
->parent
;
878 case ROFF_NEXT_CHILD
:
879 if (man
->last
->child
!= NULL
) {
880 n
->next
= man
->last
->child
;
881 man
->last
->child
->prev
= n
;
884 man
->last
->child
= n
;
885 n
->parent
= man
->last
;
897 if (n
->end
!= ENDBODY_NOT
)
909 * Copy over the normalised-data pointer of our parent. Not
910 * everybody has one, but copying a null pointer is fine.
913 n
->norm
= n
->parent
->norm
;
914 assert(n
->parent
->type
== ROFFT_BLOCK
);
918 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
922 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
923 n
->string
= roff_strdup(man
->roff
, word
);
924 roff_node_append(man
, n
);
925 n
->flags
|= NODE_VALID
| NODE_ENDED
;
926 man
->next
= ROFF_NEXT_SIBLING
;
930 roff_word_append(struct roff_man
*man
, const char *word
)
933 char *addstr
, *newstr
;
936 addstr
= roff_strdup(man
->roff
, word
);
937 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
941 man
->next
= ROFF_NEXT_SIBLING
;
945 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
949 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
950 roff_node_append(man
, n
);
951 man
->next
= ROFF_NEXT_CHILD
;
955 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
959 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
960 roff_node_append(man
, n
);
961 man
->next
= ROFF_NEXT_CHILD
;
966 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
970 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
971 roff_node_append(man
, n
);
972 man
->next
= ROFF_NEXT_CHILD
;
977 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
981 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
982 roff_node_append(man
, n
);
983 man
->next
= ROFF_NEXT_CHILD
;
988 roff_addtbl(struct roff_man
*man
, struct tbl_node
*tbl
)
991 const struct tbl_span
*span
;
993 if (man
->macroset
== MACROSET_MAN
)
994 man_breakscope(man
, ROFF_TS
);
995 while ((span
= tbl_span(tbl
)) != NULL
) {
996 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
998 roff_node_append(man
, n
);
999 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1000 man
->next
= ROFF_NEXT_SIBLING
;
1005 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1008 /* Adjust siblings. */
1011 n
->prev
->next
= n
->next
;
1013 n
->next
->prev
= n
->prev
;
1015 /* Adjust parent. */
1017 if (n
->parent
!= NULL
) {
1018 if (n
->parent
->child
== n
)
1019 n
->parent
->child
= n
->next
;
1020 if (n
->parent
->last
== n
)
1021 n
->parent
->last
= n
->prev
;
1024 /* Adjust parse point. */
1028 if (man
->last
== n
) {
1029 if (n
->prev
== NULL
) {
1030 man
->last
= n
->parent
;
1031 man
->next
= ROFF_NEXT_CHILD
;
1033 man
->last
= n
->prev
;
1034 man
->next
= ROFF_NEXT_SIBLING
;
1037 if (man
->first
== n
)
1042 roff_node_free(struct roff_node
*n
)
1045 if (n
->args
!= NULL
)
1046 mdoc_argv_free(n
->args
);
1047 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1050 eqn_box_free(n
->eqn
);
1056 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1059 while (n
->child
!= NULL
)
1060 roff_node_delete(man
, n
->child
);
1061 roff_node_unlink(man
, n
);
1066 deroff(char **dest
, const struct roff_node
*n
)
1071 if (n
->type
!= ROFFT_TEXT
) {
1072 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1077 /* Skip leading whitespace. */
1079 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1080 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1081 strchr(" %&0^|~", cp
[1]) != NULL
)
1083 else if ( ! isspace((unsigned char)*cp
))
1087 /* Skip trailing backslash. */
1090 if (sz
> 0 && cp
[sz
- 1] == '\\')
1093 /* Skip trailing whitespace. */
1096 if ( ! isspace((unsigned char)cp
[sz
-1]))
1099 /* Skip empty strings. */
1104 if (*dest
== NULL
) {
1105 *dest
= mandoc_strndup(cp
, sz
);
1109 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1114 /* --- main functions of the roff parser ---------------------------------- */
1117 * In the current line, expand escape sequences that tend to get
1118 * used in numerical expressions and conditional requests.
1119 * Also check the syntax of the remaining escape sequences.
1122 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1124 char ubuf
[24]; /* buffer to print the number */
1125 struct roff_node
*n
; /* used for header comments */
1126 const char *start
; /* start of the string to process */
1127 char *stesc
; /* start of an escape sequence ('\\') */
1128 char *ep
; /* end of comment string */
1129 const char *stnam
; /* start of the name, after "[(*" */
1130 const char *cp
; /* end of the name, e.g. before ']' */
1131 const char *res
; /* the string to be substituted */
1132 char *nbuf
; /* new buffer to copy buf->buf to */
1133 size_t maxl
; /* expected length of the escape name */
1134 size_t naml
; /* actual length of the escape name */
1135 enum mandoc_esc esc
; /* type of the escape sequence */
1136 int inaml
; /* length returned from mandoc_escape() */
1137 int expand_count
; /* to avoid infinite loops */
1138 int npos
; /* position in numeric expression */
1139 int arg_complete
; /* argument not interrupted by eol */
1140 int done
; /* no more input available */
1141 int deftype
; /* type of definition to paste */
1142 int rcsid
; /* kind of RCS id seen */
1143 char sign
; /* increment number register */
1144 char term
; /* character terminating the escape */
1146 /* Search forward for comments. */
1149 start
= buf
->buf
+ pos
;
1150 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1151 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1154 if (*stesc
!= '"' && *stesc
!= '#')
1157 /* Comment found, look for RCS id. */
1160 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1161 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1163 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1164 rcsid
= 1 << MANDOC_OS_NETBSD
;
1168 isalnum((unsigned char)*cp
) == 0 &&
1169 strchr(cp
, '$') != NULL
) {
1170 if (r
->man
->meta
.rcsids
& rcsid
)
1171 mandoc_msg(MANDOCERR_RCS_REP
, r
->parse
,
1172 ln
, stesc
+ 1 - buf
->buf
, stesc
+ 1);
1173 r
->man
->meta
.rcsids
|= rcsid
;
1176 /* Handle trailing whitespace. */
1178 ep
= strchr(stesc
--, '\0') - 1;
1183 if (*ep
== ' ' || *ep
== '\t')
1184 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1185 ln
, ep
- buf
->buf
, NULL
);
1188 * Save comments preceding the title macro
1189 * in the syntax tree.
1192 if (r
->format
== 0) {
1193 while (*ep
== ' ' || *ep
== '\t')
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
;
1205 /* Discard comments. */
1207 while (stesc
> start
&& stesc
[-1] == ' ')
1216 /* Notice the end of the input. */
1218 if (*stesc
== '\n') {
1224 while (stesc
>= start
) {
1226 /* Search backwards for the next backslash. */
1228 if (*stesc
!= r
->escape
) {
1229 if (*stesc
== '\\') {
1231 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1232 buf
->buf
, stesc
+ 1) + 1;
1234 stesc
= nbuf
+ (stesc
- buf
->buf
);
1242 /* If it is escaped, skip it. */
1244 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1245 if (*cp
!= r
->escape
)
1248 if ((stesc
- cp
) % 2 == 0) {
1252 } else if (stesc
[1] != '\0') {
1262 /* Decide whether to expand or to check only. */
1276 if (sign
== '+' || sign
== '-')
1281 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1282 if (esc
== ESCAPE_ERROR
||
1283 (esc
== ESCAPE_SPECIAL
&&
1284 mchars_spec2cp(stnam
, inaml
) < 0))
1285 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1286 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1287 "%.*s", (int)(cp
- stesc
), stesc
);
1292 if (EXPAND_LIMIT
< ++expand_count
) {
1293 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1294 ln
, (int)(stesc
- buf
->buf
), NULL
);
1299 * The third character decides the length
1300 * of the name of the string or register.
1301 * Save a pointer to the name.
1328 /* Advance to the end of the name. */
1332 while (maxl
== 0 || naml
< maxl
) {
1334 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1335 ln
, (int)(stesc
- buf
->buf
), stesc
);
1339 if (maxl
== 0 && *cp
== term
) {
1343 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1347 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1348 case ESCAPE_SPECIAL
:
1349 case ESCAPE_UNICODE
:
1350 case ESCAPE_NUMBERED
:
1351 case ESCAPE_OVERSTRIKE
:
1360 * Retrieve the replacement string; if it is
1361 * undefined, resume searching for escapes.
1367 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1368 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1373 ubuf
[0] = arg_complete
&&
1374 roff_evalnum(r
, ln
, stnam
, &npos
,
1375 NULL
, ROFFNUM_SCALE
) &&
1376 stnam
+ npos
+ 1 == cp
? '1' : '0';
1381 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1382 roff_getregn(r
, stnam
, naml
, sign
));
1387 /* use even incomplete args */
1388 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1394 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1395 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1396 "%.*s", (int)naml
, stnam
);
1398 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1399 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1400 ln
, (int)(stesc
- buf
->buf
), NULL
);
1404 /* Replace the escape sequence by the string. */
1407 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1408 buf
->buf
, res
, cp
) + 1;
1410 /* Prepare for the next replacement. */
1413 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1421 * Process text streams.
1424 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1430 enum mandoc_esc esc
;
1432 /* Spring the input line trap. */
1434 if (roffit_lines
== 1) {
1435 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1442 return ROFF_REPARSE
;
1443 } else if (roffit_lines
> 1)
1446 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1447 if (roffce_lines
< 1) {
1448 r
->man
->last
= roffce_node
;
1449 r
->man
->next
= ROFF_NEXT_SIBLING
;
1456 /* Convert all breakable hyphens into ASCII_HYPH. */
1458 start
= p
= buf
->buf
+ pos
;
1460 while (*p
!= '\0') {
1461 sz
= strcspn(p
, "-\\");
1468 /* Skip over escapes. */
1470 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1471 if (esc
== ESCAPE_ERROR
)
1476 } else if (p
== start
) {
1481 if (isalpha((unsigned char)p
[-1]) &&
1482 isalpha((unsigned char)p
[1]))
1490 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1494 int pos
; /* parse point */
1495 int spos
; /* saved parse point for messages */
1496 int ppos
; /* original offset in buf->buf */
1497 int ctl
; /* macro line (boolean) */
1501 /* Handle in-line equation delimiters. */
1503 if (r
->tbl
== NULL
&&
1504 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1505 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1506 e
= roff_eqndelim(r
, buf
, pos
);
1507 if (e
== ROFF_REPARSE
)
1509 assert(e
== ROFF_CONT
);
1512 /* Expand some escape sequences. */
1514 e
= roff_res(r
, buf
, ln
, pos
);
1515 if (e
== ROFF_IGN
|| e
== ROFF_APPEND
)
1517 assert(e
== ROFF_CONT
);
1519 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1522 * First, if a scope is open and we're not a macro, pass the
1523 * text through the macro's filter.
1524 * Equations process all content themselves.
1525 * Tables process almost all content themselves, but we want
1526 * to warn about macros before passing it there.
1529 if (r
->last
!= NULL
&& ! ctl
) {
1531 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1534 assert(e
== ROFF_CONT
);
1536 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1537 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1540 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1541 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1542 roff_addtbl(r
->man
, r
->tbl
);
1546 return roff_parsetext(r
, buf
, pos
, offs
);
1548 /* Skip empty request lines. */
1550 if (buf
->buf
[pos
] == '"') {
1551 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1554 } else if (buf
->buf
[pos
] == '\0')
1558 * If a scope is open, go to the child handler for that macro,
1559 * as it may want to preprocess before doing anything with it.
1560 * Don't do so if an equation is open.
1565 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1568 /* No scope is open. This is a new request or macro. */
1571 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1573 /* Tables ignore most macros. */
1575 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1576 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1577 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1578 ln
, pos
, buf
->buf
+ spos
);
1579 if (t
!= TOKEN_NONE
)
1581 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1583 while (buf
->buf
[pos
] == ' ')
1585 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1586 roff_addtbl(r
->man
, r
->tbl
);
1590 /* For now, let high level macros abort .ce mode. */
1592 if (ctl
&& roffce_node
!= NULL
&&
1593 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1594 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1595 r
->man
->last
= roffce_node
;
1596 r
->man
->next
= ROFF_NEXT_SIBLING
;
1602 * This is neither a roff request nor a user-defined macro.
1603 * Let the standard macro set parsers handle it.
1606 if (t
== TOKEN_NONE
)
1609 /* Execute a roff request or a user defined macro. */
1611 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1615 roff_endparse(struct roff
*r
)
1617 if (r
->last
!= NULL
)
1618 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1619 r
->last
->line
, r
->last
->col
,
1620 roff_name
[r
->last
->tok
]);
1622 if (r
->eqn
!= NULL
) {
1623 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1624 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1629 if (r
->tbl
!= NULL
) {
1630 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1631 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1638 * Parse a roff node's type from the input buffer. This must be in the
1639 * form of ".foo xxx" in the usual way.
1641 static enum roff_tok
1642 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1652 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1656 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1658 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1659 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1668 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1671 if (t
!= TOKEN_NONE
)
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);
1681 /* --- handling of request blocks ----------------------------------------- */
1684 roff_cblock(ROFF_ARGS
)
1688 * A block-close `..' should only be invoked as a child of an
1689 * ignore macro, otherwise raise a warning and just ignore it.
1692 if (r
->last
== NULL
) {
1693 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1698 switch (r
->last
->tok
) {
1700 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1703 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1708 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1713 if (buf
->buf
[pos
] != '\0')
1714 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1715 ".. %s", buf
->buf
+ pos
);
1718 roffnode_cleanscope(r
);
1724 roffnode_cleanscope(struct roff
*r
)
1728 if (--r
->last
->endspan
!= 0)
1735 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1738 if (NULL
== r
->last
) {
1739 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1744 switch (r
->last
->tok
) {
1750 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1755 if (r
->last
->endspan
> -1) {
1756 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1762 roffnode_cleanscope(r
);
1767 roff_block(ROFF_ARGS
)
1769 const char *name
, *value
;
1770 char *call
, *cp
, *iname
, *rname
;
1771 size_t csz
, namesz
, rsz
;
1774 /* Ignore groff compatibility mode for now. */
1776 if (tok
== ROFF_de1
)
1778 else if (tok
== ROFF_dei1
)
1780 else if (tok
== ROFF_am1
)
1782 else if (tok
== ROFF_ami1
)
1785 /* Parse the macro name argument. */
1787 cp
= buf
->buf
+ pos
;
1788 if (tok
== ROFF_ig
) {
1793 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1794 iname
[namesz
] = '\0';
1797 /* Resolve the macro name argument if it is indirect. */
1799 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1800 deftype
= ROFFDEF_USER
;
1801 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1803 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1804 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1805 "%.*s", (int)namesz
, iname
);
1808 namesz
= strlen(name
);
1812 if (namesz
== 0 && tok
!= ROFF_ig
) {
1813 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1814 ln
, ppos
, roff_name
[tok
]);
1818 roffnode_push(r
, tok
, name
, ln
, ppos
);
1821 * At the beginning of a `de' macro, clear the existing string
1822 * with the same name, if there is one. New content will be
1823 * appended from roff_block_text() in multiline mode.
1826 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
1827 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1828 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1829 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
1830 deftype
= ROFFDEF_ANY
;
1831 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1832 switch (deftype
) { /* Before appending, ... */
1833 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
1834 roff_setstrn(&r
->strtab
, name
, namesz
,
1835 value
, strlen(value
), 0);
1837 case ROFFDEF_REN
: /* call original standard macro. */
1838 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1839 (int)strlen(value
), value
);
1840 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1841 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1844 case ROFFDEF_STD
: /* rename and call standard macro. */
1845 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
1846 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
1847 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1849 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1861 /* Get the custom end marker. */
1864 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1866 /* Resolve the end marker if it is indirect. */
1868 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1869 deftype
= ROFFDEF_USER
;
1870 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1872 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1873 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1874 "%.*s", (int)namesz
, iname
);
1877 namesz
= strlen(name
);
1882 r
->last
->end
= mandoc_strndup(name
, namesz
);
1885 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1886 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
1892 roff_block_sub(ROFF_ARGS
)
1898 * First check whether a custom macro exists at this level. If
1899 * it does, then check against it. This is some of groff's
1900 * stranger behaviours. If we encountered a custom end-scope
1901 * tag and that tag also happens to be a "real" macro, then we
1902 * need to try interpreting it again as a real macro. If it's
1903 * not, then return ignore. Else continue.
1907 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1908 if (buf
->buf
[i
] != r
->last
->end
[j
])
1911 if (r
->last
->end
[j
] == '\0' &&
1912 (buf
->buf
[i
] == '\0' ||
1913 buf
->buf
[i
] == ' ' ||
1914 buf
->buf
[i
] == '\t')) {
1916 roffnode_cleanscope(r
);
1918 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1922 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1930 * If we have no custom end-query or lookup failed, then try
1931 * pulling it out of the hashtable.
1934 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1936 if (t
!= ROFF_cblock
) {
1938 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1942 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1946 roff_block_text(ROFF_ARGS
)
1950 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1956 roff_cond_sub(ROFF_ARGS
)
1963 roffnode_cleanscope(r
);
1966 * If `\}' occurs on a macro line without a preceding macro,
1967 * drop the line completely.
1970 ep
= buf
->buf
+ pos
;
1971 if (ep
[0] == '\\' && ep
[1] == '}')
1974 /* Always check for the closing delimiter `\}'. */
1976 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1979 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
1980 roff_ccond(r
, ln
, ep
- buf
->buf
);
1992 * Fully handle known macros when they are structurally
1993 * required or when the conditional evaluated to true.
1996 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1997 return t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)
1998 ? (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) : rr
1999 ? ROFF_CONT
: ROFF_IGN
;
2003 roff_cond_text(ROFF_ARGS
)
2009 roffnode_cleanscope(r
);
2011 ep
= buf
->buf
+ pos
;
2012 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2013 if (*(++ep
) == '}') {
2015 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
2020 return rr
? ROFF_CONT
: ROFF_IGN
;
2023 /* --- handling of numeric and conditional expressions -------------------- */
2026 * Parse a single signed integer number. Stop at the first non-digit.
2027 * If there is at least one digit, return success and advance the
2028 * parse point, else return failure and let the parse point unchanged.
2029 * Ignore overflows, treat them just like the C language.
2032 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2034 int myres
, scaled
, n
, p
;
2041 if (n
|| v
[p
] == '+')
2044 if (flags
& ROFFNUM_WHITE
)
2045 while (isspace((unsigned char)v
[p
]))
2048 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2049 *res
= 10 * *res
+ v
[p
] - '0';
2056 /* Each number may be followed by one optional scaling unit. */
2060 scaled
= *res
* 65536;
2063 scaled
= *res
* 240;
2066 scaled
= *res
* 240 / 2.54;
2077 scaled
= *res
* 10 / 3;
2083 scaled
= *res
* 6 / 25;
2090 if (flags
& ROFFNUM_SCALE
)
2098 * Evaluate a string comparison condition.
2099 * The first character is the delimiter.
2100 * Succeed if the string up to its second occurrence
2101 * matches the string up to its third occurence.
2102 * Advance the cursor after the third occurrence
2103 * or lacking that, to the end of the line.
2106 roff_evalstrcond(const char *v
, int *pos
)
2108 const char *s1
, *s2
, *s3
;
2112 s1
= v
+ *pos
; /* initial delimiter */
2113 s2
= s1
+ 1; /* for scanning the first string */
2114 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2116 if (NULL
== s3
) /* found no middle delimiter */
2119 while ('\0' != *++s3
) {
2120 if (*s2
!= *s3
) { /* mismatch */
2121 s3
= strchr(s3
, *s1
);
2124 if (*s3
== *s1
) { /* found the final delimiter */
2133 s3
= strchr(s2
, '\0');
2134 else if (*s3
!= '\0')
2141 * Evaluate an optionally negated single character, numerical,
2142 * or string condition.
2145 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2149 int deftype
, number
, savepos
, istrue
, wanttrue
;
2151 if ('!' == v
[*pos
]) {
2176 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2179 else if (v
[*pos
] == 'r')
2180 istrue
= roff_hasregn(r
, name
, sz
);
2182 deftype
= ROFFDEF_ANY
;
2183 roff_getstrn(r
, name
, sz
, &deftype
);
2187 return istrue
== wanttrue
;
2193 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2194 return (number
> 0) == wanttrue
;
2195 else if (*pos
== savepos
)
2196 return roff_evalstrcond(v
, pos
) == wanttrue
;
2202 roff_line_ignore(ROFF_ARGS
)
2209 roff_insec(ROFF_ARGS
)
2212 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2213 ln
, ppos
, roff_name
[tok
]);
2218 roff_unsupp(ROFF_ARGS
)
2221 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2222 ln
, ppos
, roff_name
[tok
]);
2227 roff_cond(ROFF_ARGS
)
2230 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2233 * An `.el' has no conditional body: it will consume the value
2234 * of the current rstack entry set in prior `ie' calls or
2237 * If we're not an `el', however, then evaluate the conditional.
2240 r
->last
->rule
= tok
== ROFF_el
?
2241 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2242 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2245 * An if-else will put the NEGATION of the current evaluated
2246 * conditional into the stack of rules.
2249 if (tok
== ROFF_ie
) {
2250 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2252 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2253 r
->rstacksz
, sizeof(int));
2255 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2258 /* If the parent has false as its rule, then so do we. */
2260 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2265 * If there is nothing on the line after the conditional,
2266 * not even whitespace, use next-line scope.
2269 if (buf
->buf
[pos
] == '\0') {
2270 r
->last
->endspan
= 2;
2274 while (buf
->buf
[pos
] == ' ')
2277 /* An opening brace requests multiline scope. */
2279 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2280 r
->last
->endspan
= -1;
2282 while (buf
->buf
[pos
] == ' ')
2288 * Anything else following the conditional causes
2289 * single-line scope. Warn if the scope contains
2290 * nothing but trailing whitespace.
2293 if (buf
->buf
[pos
] == '\0')
2294 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2295 ln
, ppos
, roff_name
[tok
]);
2297 r
->last
->endspan
= 1;
2311 /* Ignore groff compatibility mode for now. */
2313 if (tok
== ROFF_ds1
)
2315 else if (tok
== ROFF_as1
)
2319 * The first word is the name of the string.
2320 * If it is empty or terminated by an escape sequence,
2321 * abort the `ds' request without defining anything.
2324 name
= string
= buf
->buf
+ pos
;
2328 namesz
= roff_getname(r
, &string
, ln
, pos
);
2329 if (name
[namesz
] == '\\')
2332 /* Read past the initial double-quote, if any. */
2336 /* The rest is the value. */
2337 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2339 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2344 * Parse a single operator, one or two characters long.
2345 * If the operator is recognized, return success and advance the
2346 * parse point, else return failure and let the parse point unchanged.
2349 roff_getop(const char *v
, int *pos
, char *res
)
2364 switch (v
[*pos
+ 1]) {
2382 switch (v
[*pos
+ 1]) {
2396 if ('=' == v
[*pos
+ 1])
2408 * Evaluate either a parenthesized numeric expression
2409 * or a single signed integer number.
2412 roff_evalpar(struct roff
*r
, int ln
,
2413 const char *v
, int *pos
, int *res
, int flags
)
2417 return roff_getnum(v
, pos
, res
, flags
);
2420 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2424 * Omission of the closing parenthesis
2425 * is an error in validation mode,
2426 * but ignored in evaluation mode.
2431 else if (NULL
== res
)
2438 * Evaluate a complete numeric expression.
2439 * Proceed left to right, there is no concept of precedence.
2442 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2443 int *pos
, int *res
, int flags
)
2445 int mypos
, operand2
;
2453 if (flags
& ROFFNUM_WHITE
)
2454 while (isspace((unsigned char)v
[*pos
]))
2457 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2461 if (flags
& ROFFNUM_WHITE
)
2462 while (isspace((unsigned char)v
[*pos
]))
2465 if ( ! roff_getop(v
, pos
, &operator))
2468 if (flags
& ROFFNUM_WHITE
)
2469 while (isspace((unsigned char)v
[*pos
]))
2472 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2475 if (flags
& ROFFNUM_WHITE
)
2476 while (isspace((unsigned char)v
[*pos
]))
2493 if (operand2
== 0) {
2494 mandoc_msg(MANDOCERR_DIVZERO
,
2495 r
->parse
, ln
, *pos
, v
);
2502 if (operand2
== 0) {
2503 mandoc_msg(MANDOCERR_DIVZERO
,
2504 r
->parse
, ln
, *pos
, v
);
2511 *res
= *res
< operand2
;
2514 *res
= *res
> operand2
;
2517 *res
= *res
<= operand2
;
2520 *res
= *res
>= operand2
;
2523 *res
= *res
== operand2
;
2526 *res
= *res
!= operand2
;
2529 *res
= *res
&& operand2
;
2532 *res
= *res
|| operand2
;
2535 if (operand2
< *res
)
2539 if (operand2
> *res
)
2549 /* --- register management ------------------------------------------------ */
2552 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2554 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2558 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2559 int val
, char sign
, int step
)
2561 struct roffreg
*reg
;
2563 /* Search for an existing register with the same name. */
2566 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2567 strncmp(reg
->key
.p
, name
, len
) != 0))
2571 /* Create a new register. */
2572 reg
= mandoc_malloc(sizeof(struct roffreg
));
2573 reg
->key
.p
= mandoc_strndup(name
, len
);
2577 reg
->next
= r
->regtab
;
2583 else if ('-' == sign
)
2587 if (step
!= INT_MIN
)
2592 * Handle some predefined read-only number registers.
2593 * For now, return -1 if the requested register is not predefined;
2594 * in case a predefined read-only register having the value -1
2595 * were to turn up, another special value would have to be chosen.
2598 roff_getregro(const struct roff
*r
, const char *name
)
2602 case '$': /* Number of arguments of the last macro evaluated. */
2604 case 'A': /* ASCII approximation mode is always off. */
2606 case 'g': /* Groff compatibility mode is always on. */
2608 case 'H': /* Fixed horizontal resolution. */
2610 case 'j': /* Always adjust left margin only. */
2612 case 'T': /* Some output device is always defined. */
2614 case 'V': /* Fixed vertical resolution. */
2622 roff_getreg(struct roff
*r
, const char *name
)
2624 return roff_getregn(r
, name
, strlen(name
), '\0');
2628 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2630 struct roffreg
*reg
;
2633 if ('.' == name
[0] && 2 == len
) {
2634 val
= roff_getregro(r
, name
+ 1);
2639 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2640 if (len
== reg
->key
.sz
&&
2641 0 == strncmp(name
, reg
->key
.p
, len
)) {
2644 reg
->val
+= reg
->step
;
2647 reg
->val
-= reg
->step
;
2656 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
2661 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2663 struct roffreg
*reg
;
2666 if ('.' == name
[0] && 2 == len
) {
2667 val
= roff_getregro(r
, name
+ 1);
2672 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2673 if (len
== reg
->key
.sz
&&
2674 0 == strncmp(name
, reg
->key
.p
, len
))
2681 roff_freereg(struct roffreg
*reg
)
2683 struct roffreg
*old_reg
;
2685 while (NULL
!= reg
) {
2696 char *key
, *val
, *step
;
2701 key
= val
= buf
->buf
+ pos
;
2705 keysz
= roff_getname(r
, &val
, ln
, pos
);
2706 if (key
[keysz
] == '\\')
2710 if (sign
== '+' || sign
== '-')
2714 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
2718 while (isspace((unsigned char)*step
))
2720 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
2723 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
2730 struct roffreg
*reg
, **prev
;
2734 name
= cp
= buf
->buf
+ pos
;
2737 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2738 name
[namesz
] = '\0';
2743 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2755 /* --- handler functions for roff requests -------------------------------- */
2764 cp
= buf
->buf
+ pos
;
2765 while (*cp
!= '\0') {
2767 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2768 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2769 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2770 if (name
[namesz
] == '\\')
2781 /* Parse the number of lines. */
2783 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2784 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2785 ln
, ppos
, buf
->buf
+ 1);
2789 while (isspace((unsigned char)buf
->buf
[pos
]))
2793 * Arm the input line trap.
2794 * Special-casing "an-trap" is an ugly workaround to cope
2795 * with DocBook stupidly fiddling with man(7) internals.
2799 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2800 strcmp(buf
->buf
+ pos
, "an-trap") ?
2801 buf
->buf
+ pos
: "br");
2809 enum roff_tok t
, te
;
2816 r
->format
= MPARSE_MDOC
;
2817 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
2823 r
->format
= MPARSE_MAN
;
2824 mask
= MPARSE_QUICK
;
2829 if ((r
->options
& mask
) == 0)
2830 for (t
= tok
; t
< te
; t
++)
2831 roff_setstr(r
, roff_name
[t
], NULL
, 0);
2838 if (r
->tbl
== NULL
) {
2839 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2843 if (tbl_end(r
->tbl
) == 0) {
2846 buf
->buf
= mandoc_strdup(".sp");
2849 return ROFF_REPARSE
;
2860 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2863 tbl_restart(ln
, ppos
, r
->tbl
);
2869 * Handle in-line equation delimiters.
2872 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2875 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2878 * Outside equations, look for an opening delimiter.
2879 * If we are inside an equation, we already know it is
2880 * in-line, or this function wouldn't have been called;
2881 * so look for a closing delimiter.
2884 cp1
= buf
->buf
+ pos
;
2885 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2886 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2891 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2893 /* Handle preceding text, protecting whitespace. */
2895 if (*buf
->buf
!= '\0') {
2902 * Prepare replacing the delimiter with an equation macro
2903 * and drop leading white space from the equation.
2906 if (r
->eqn
== NULL
) {
2913 /* Handle following text, protecting whitespace. */
2921 /* Do the actual replacement. */
2923 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2924 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2928 /* Toggle the in-line state of the eqn subsystem. */
2930 r
->eqn_inline
= r
->eqn
== NULL
;
2931 return ROFF_REPARSE
;
2937 struct roff_node
*n
;
2939 if (r
->man
->macroset
== MACROSET_MAN
)
2940 man_breakscope(r
->man
, ROFF_EQ
);
2941 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
2942 if (ln
> r
->man
->last
->line
)
2943 n
->flags
|= NODE_LINE
;
2944 n
->eqn
= mandoc_calloc(1, sizeof(*n
->eqn
));
2945 n
->eqn
->expectargs
= UINT_MAX
;
2946 roff_node_append(r
->man
, n
);
2947 r
->man
->next
= ROFF_NEXT_SIBLING
;
2949 assert(r
->eqn
== NULL
);
2950 if (r
->last_eqn
== NULL
)
2951 r
->last_eqn
= eqn_alloc(r
->parse
);
2953 eqn_reset(r
->last_eqn
);
2954 r
->eqn
= r
->last_eqn
;
2957 if (buf
->buf
[pos
] != '\0')
2958 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2959 ".EQ %s", buf
->buf
+ pos
);
2967 if (r
->eqn
!= NULL
) {
2971 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2972 if (buf
->buf
[pos
] != '\0')
2973 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2974 "EN %s", buf
->buf
+ pos
);
2981 if (r
->tbl
!= NULL
) {
2982 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2983 ln
, ppos
, "TS breaks TS");
2986 r
->tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2988 r
->last_tbl
->next
= r
->tbl
;
2990 r
->first_tbl
= r
->tbl
;
2991 r
->last_tbl
= r
->tbl
;
2996 roff_onearg(ROFF_ARGS
)
2998 struct roff_node
*n
;
3002 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3003 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3005 man_breakscope(r
->man
, tok
);
3007 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3008 r
->man
->last
= roffce_node
;
3009 r
->man
->next
= ROFF_NEXT_SIBLING
;
3012 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3015 cp
= buf
->buf
+ pos
;
3017 while (*cp
!= '\0' && *cp
!= ' ')
3022 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
3023 r
->parse
, ln
, cp
- buf
->buf
,
3024 "%s ... %s", roff_name
[tok
], cp
);
3025 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3028 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3029 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3030 roff_word_alloc(r
->man
, ln
, pos
, "1");
3031 r
->man
->last
->flags
|= NODE_NOSRC
;
3034 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3035 &roffce_lines
, 0) == 0) {
3036 mandoc_vmsg(MANDOCERR_CE_NONUM
,
3037 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
3040 if (roffce_lines
< 1) {
3041 r
->man
->last
= r
->man
->last
->parent
;
3045 roffce_node
= r
->man
->last
->parent
;
3047 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3050 n
->flags
|= NODE_LINE
;
3051 r
->man
->next
= ROFF_NEXT_SIBLING
;
3056 roff_manyarg(ROFF_ARGS
)
3058 struct roff_node
*n
;
3061 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3064 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3065 while (*ep
!= '\0' && *ep
!= ' ')
3069 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3072 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3074 r
->man
->next
= ROFF_NEXT_SIBLING
;
3081 char *oldn
, *newn
, *end
, *value
;
3082 size_t oldsz
, newsz
, valsz
;
3084 newn
= oldn
= buf
->buf
+ pos
;
3088 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3089 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3093 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3097 valsz
= mandoc_asprintf(&value
, ".%.*s \\$*\\\"\n",
3099 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3100 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3108 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3109 man_breakscope(r
->man
, ROFF_br
);
3110 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3111 if (buf
->buf
[pos
] != '\0')
3112 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3113 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3114 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3115 r
->man
->next
= ROFF_NEXT_SIBLING
;
3126 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3130 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3131 ln
, p
- buf
->buf
, "cc ... %s", p
);
3147 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3148 ln
, p
- buf
->buf
, "ec ... %s", p
);
3157 if (buf
->buf
[pos
] != '\0')
3158 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3159 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3166 const char *p
, *first
, *second
;
3168 enum mandoc_esc esc
;
3173 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3177 while (*p
!= '\0') {
3181 if (*first
== '\\') {
3182 esc
= mandoc_escape(&p
, NULL
, NULL
);
3183 if (esc
== ESCAPE_ERROR
) {
3184 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3185 ln
, (int)(p
- buf
->buf
), first
);
3188 fsz
= (size_t)(p
- first
);
3192 if (*second
== '\\') {
3193 esc
= mandoc_escape(&p
, NULL
, NULL
);
3194 if (esc
== ESCAPE_ERROR
) {
3195 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3196 ln
, (int)(p
- buf
->buf
), second
);
3199 ssz
= (size_t)(p
- second
);
3200 } else if (*second
== '\0') {
3201 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3202 ln
, first
- buf
->buf
, "tr %s", first
);
3208 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3213 if (r
->xtab
== NULL
)
3214 r
->xtab
= mandoc_calloc(128,
3215 sizeof(struct roffstr
));
3217 free(r
->xtab
[(int)*first
].p
);
3218 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3219 r
->xtab
[(int)*first
].sz
= ssz
;
3229 char *oldn
, *newn
, *end
;
3230 size_t oldsz
, newsz
;
3233 oldn
= newn
= buf
->buf
+ pos
;
3237 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3238 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3242 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3246 deftype
= ROFFDEF_ANY
;
3247 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3250 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3251 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3252 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3255 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3256 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3259 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3260 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3261 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3264 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3265 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3268 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3269 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3280 name
= buf
->buf
+ pos
;
3281 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3284 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3285 * opening anything that's not in our cwd or anything beneath
3286 * it. Thus, explicitly disallow traversing up the file-system
3287 * or using absolute paths.
3290 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3291 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3293 buf
->sz
= mandoc_asprintf(&cp
,
3294 ".sp\nSee the file %s.\n.sp", name
) + 1;
3298 return ROFF_REPARSE
;
3305 /* --- user defined strings and macros ------------------------------------ */
3308 roff_userdef(ROFF_ARGS
)
3310 const char *arg
[16], *ap
;
3312 int expand_count
, i
, ib
, ie
;
3316 * Collect pointers to macro argument strings
3317 * and NUL-terminate them.
3321 cp
= buf
->buf
+ pos
;
3322 for (i
= 0; i
< 16; i
++) {
3326 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3332 * Expand macro arguments.
3335 buf
->sz
= strlen(r
->current_string
) + 1;
3336 n1
= n2
= cp
= mandoc_malloc(buf
->sz
);
3337 memcpy(n1
, r
->current_string
, buf
->sz
);
3339 while (*cp
!= '\0') {
3341 /* Scan ahead for the next argument invocation. */
3347 if (*cp
== '*') { /* \\$* inserts all arguments */
3350 } else { /* \\$1 .. \\$9 insert one argument */
3351 ib
= ie
= *cp
- '1';
3352 if (ib
< 0 || ib
> 8)
3358 * Prevent infinite recursion.
3363 else if (++expand_count
> EXPAND_LIMIT
) {
3364 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
3365 ln
, (int)(cp
- n1
), NULL
);
3373 * Determine the size of the expanded argument,
3374 * taking escaping of quotes into account.
3377 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3378 for (i
= ib
; i
<= ie
; i
++) {
3379 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3388 * Determine the size of the rest of the
3389 * unexpanded macro, including the NUL.
3392 rsz
= buf
->sz
- (cp
- n1
) - 3;
3395 * When shrinking, move before
3396 * releasing the storage.
3400 memmove(cp
+ asz
, cp
+ 3, rsz
);
3403 * Resize the storage for the macro
3404 * and readjust the parse pointer.
3408 n2
= mandoc_realloc(n1
, buf
->sz
);
3409 cp
= n2
+ (cp
- n1
);
3413 * When growing, make room
3414 * for the expanded argument.
3418 memmove(cp
+ asz
, cp
+ 3, rsz
);
3421 /* Copy the expanded argument, escaping quotes. */
3424 for (i
= ib
; i
<= ie
; i
++) {
3425 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3427 memcpy(n2
, "\\(dq", 4);
3438 * Replace the macro invocation
3439 * by the expanded macro.
3446 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3447 ROFF_REPARSE
: ROFF_APPEND
;
3451 * Calling a high-level macro that was renamed with .rn.
3452 * r->current_string has already been set up by roff_parse().
3455 roff_renamed(ROFF_ARGS
)
3459 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3460 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3468 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3477 /* Read until end of name and terminate it with NUL. */
3478 for (cp
= name
; 1; cp
++) {
3479 if ('\0' == *cp
|| ' ' == *cp
) {
3486 if ('{' == cp
[1] || '}' == cp
[1])
3491 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3492 "%.*s", (int)(cp
- name
+ 1), name
);
3493 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3497 /* Read past spaces. */
3506 * Store *string into the user-defined string called *name.
3507 * To clear an existing entry, call with (*r, *name, NULL, 0).
3508 * append == 0: replace mode
3509 * append == 1: single-line append mode
3510 * append == 2: multiline append mode, append '\n' after each call
3513 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3518 namesz
= strlen(name
);
3519 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3520 string
? strlen(string
) : 0, append
);
3521 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3525 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3526 const char *string
, size_t stringsz
, int append
)
3531 size_t oldch
, newch
;
3533 /* Search for an existing string with the same name. */
3536 while (n
&& (namesz
!= n
->key
.sz
||
3537 strncmp(n
->key
.p
, name
, namesz
)))
3541 /* Create a new string table entry. */
3542 n
= mandoc_malloc(sizeof(struct roffkv
));
3543 n
->key
.p
= mandoc_strndup(name
, namesz
);
3549 } else if (0 == append
) {
3559 * One additional byte for the '\n' in multiline mode,
3560 * and one for the terminating '\0'.
3562 newch
= stringsz
+ (1 < append
? 2u : 1u);
3564 if (NULL
== n
->val
.p
) {
3565 n
->val
.p
= mandoc_malloc(newch
);
3570 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3573 /* Skip existing content in the destination buffer. */
3574 c
= n
->val
.p
+ (int)oldch
;
3576 /* Append new content to the destination buffer. */
3578 while (i
< (int)stringsz
) {
3580 * Rudimentary roff copy mode:
3581 * Handle escaped backslashes.
3583 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3588 /* Append terminating bytes. */
3593 n
->val
.sz
= (int)(c
- n
->val
.p
);
3597 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
3600 const struct roffkv
*n
;
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
)
3609 if (*deftype
& ROFFDEF_USER
) {
3610 *deftype
= ROFFDEF_USER
;
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
)
3621 if (*deftype
& ROFFDEF_REN
) {
3622 *deftype
= ROFFDEF_REN
;
3629 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3630 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
3631 predefs
[i
].name
[len
] != '\0')
3633 if (*deftype
& ROFFDEF_PRE
) {
3634 *deftype
= ROFFDEF_PRE
;
3635 return predefs
[i
].str
;
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')
3646 if (*deftype
& ROFFDEF_STD
) {
3647 *deftype
= ROFFDEF_STD
;
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')
3660 if (*deftype
& ROFFDEF_STD
) {
3661 *deftype
= ROFFDEF_STD
;
3670 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
3671 if (*deftype
& ROFFDEF_REN
) {
3673 * This might still be a request,
3674 * so do not treat it as undefined yet.
3676 *deftype
= ROFFDEF_UNDEF
;
3680 /* Using an undefined string defines it to be empty. */
3682 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
3683 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
3691 roff_freestr(struct roffkv
*r
)
3693 struct roffkv
*n
, *nn
;
3695 for (n
= r
; n
; n
= nn
) {
3703 /* --- accessors and utility functions ------------------------------------ */
3706 * Duplicate an input string, making the appropriate character
3707 * conversations (as stipulated by `tr') along the way.
3708 * Returns a heap-allocated string with all the replacements made.
3711 roff_strdup(const struct roff
*r
, const char *p
)
3713 const struct roffkv
*cp
;
3717 enum mandoc_esc esc
;
3719 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3720 return mandoc_strdup(p
);
3721 else if ('\0' == *p
)
3722 return mandoc_strdup("");
3725 * Step through each character looking for term matches
3726 * (remember that a `tr' can be invoked with an escape, which is
3727 * a glyph but the escape is multi-character).
3728 * We only do this if the character hash has been initialised
3729 * and the string is >0 length.
3735 while ('\0' != *p
) {
3736 assert((unsigned int)*p
< 128);
3737 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3738 sz
= r
->xtab
[(int)*p
].sz
;
3739 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3740 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3744 } else if ('\\' != *p
) {
3745 res
= mandoc_realloc(res
, ssz
+ 2);
3750 /* Search for term matches. */
3751 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3752 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3757 * A match has been found.
3758 * Append the match to the array and move
3759 * forward by its keysize.
3761 res
= mandoc_realloc(res
,
3762 ssz
+ cp
->val
.sz
+ 1);
3763 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3765 p
+= (int)cp
->key
.sz
;
3770 * Handle escapes carefully: we need to copy
3771 * over just the escape itself, or else we might
3772 * do replacements within the escape itself.
3773 * Make sure to pass along the bogus string.
3776 esc
= mandoc_escape(&p
, NULL
, NULL
);
3777 if (ESCAPE_ERROR
== esc
) {
3779 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3780 memcpy(res
+ ssz
, pp
, sz
);
3784 * We bail out on bad escapes.
3785 * No need to warn: we already did so when
3786 * roff_res() was called.
3789 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3790 memcpy(res
+ ssz
, pp
, sz
);
3794 res
[(int)ssz
] = '\0';
3799 roff_getformat(const struct roff
*r
)
3806 * Find out whether a line is a macro line or not.
3807 * If it is, adjust the current position and return one; if it isn't,
3808 * return zero and don't change the current position.
3809 * If the control character has been set with `.cc', then let that grain
3811 * This is slighly contrary to groff, where using the non-breaking
3812 * control character when `cc' has been invoked will cause the
3813 * non-breaking macro contents to be printed verbatim.
3816 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3822 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
3824 else if (r
->control
!= '\0')
3826 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3828 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3833 while (' ' == cp
[pos
] || '\t' == cp
[pos
])