]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.309 2017/06/14 13:00:31 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 /* --- data types --------------------------------------------------------- */
45 * An incredibly-simple string buffer.
48 char *p
; /* nil-terminated buffer */
49 size_t sz
; /* saved strlen(p) */
53 * A key-value roffstr pair as part of a singly-linked list.
58 struct roffkv
*next
; /* next in list */
62 * A single number register as part of a singly-linked list.
71 * Association of request and macro names with token IDs.
79 struct mparse
*parse
; /* parse point */
80 struct roff_man
*man
; /* mdoc or man parser */
81 struct roffnode
*last
; /* leaf of stack */
82 int *rstack
; /* stack of inverted `ie' values */
83 struct ohash
*reqtab
; /* request lookup table */
84 struct roffreg
*regtab
; /* number registers */
85 struct roffkv
*strtab
; /* user-defined strings & macros */
86 struct roffkv
*rentab
; /* renamed strings & macros */
87 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
88 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
89 const char *current_string
; /* value of last called user macro */
90 struct tbl_node
*first_tbl
; /* first table parsed */
91 struct tbl_node
*last_tbl
; /* last table parsed */
92 struct tbl_node
*tbl
; /* current table being parsed */
93 struct eqn_node
*last_eqn
; /* last equation parsed */
94 struct eqn_node
*first_eqn
; /* first equation parsed */
95 struct eqn_node
*eqn
; /* current equation being parsed */
96 int eqn_inline
; /* current equation is inline */
97 int options
; /* parse options */
98 int rstacksz
; /* current size limit of rstack */
99 int rstackpos
; /* position in rstack */
100 int format
; /* current file in mdoc or man format */
101 int argc
; /* number of args of the last macro */
102 char control
; /* control character */
103 char escape
; /* escape character */
107 enum roff_tok tok
; /* type of node */
108 struct roffnode
*parent
; /* up one in stack */
109 int line
; /* parse line */
110 int col
; /* parse col */
111 char *name
; /* node name, e.g. macro name */
112 char *end
; /* end-rules: custom token */
113 int endspan
; /* end-rules: next-line or infty */
114 int rule
; /* current evaluation rule */
117 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
118 enum roff_tok tok, /* tok of macro */ \
119 struct buf *buf, /* input buffer */ \
120 int ln, /* parse line */ \
121 int ppos, /* original pos in buffer */ \
122 int pos, /* current pos in buffer */ \
123 int *offs /* reset offset of buffer data */
125 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
128 roffproc proc
; /* process new macro */
129 roffproc text
; /* process as child text of macro */
130 roffproc sub
; /* process as child of macro */
132 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
136 const char *name
; /* predefined input name */
137 const char *str
; /* replacement symbol */
140 #define PREDEF(__name, __str) \
141 { (__name), (__str) },
143 /* --- function prototypes ------------------------------------------------ */
145 static void roffnode_cleanscope(struct roff
*);
146 static void roffnode_pop(struct roff
*);
147 static void roffnode_push(struct roff
*, enum roff_tok
,
148 const char *, int, int);
149 static enum rofferr
roff_block(ROFF_ARGS
);
150 static enum rofferr
roff_block_text(ROFF_ARGS
);
151 static enum rofferr
roff_block_sub(ROFF_ARGS
);
152 static enum rofferr
roff_br(ROFF_ARGS
);
153 static enum rofferr
roff_cblock(ROFF_ARGS
);
154 static enum rofferr
roff_cc(ROFF_ARGS
);
155 static void roff_ccond(struct roff
*, int, int);
156 static enum rofferr
roff_cond(ROFF_ARGS
);
157 static enum rofferr
roff_cond_text(ROFF_ARGS
);
158 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
159 static enum rofferr
roff_ds(ROFF_ARGS
);
160 static enum rofferr
roff_ec(ROFF_ARGS
);
161 static enum rofferr
roff_eo(ROFF_ARGS
);
162 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
163 static int roff_evalcond(struct roff
*r
, int, char *, int *);
164 static int roff_evalnum(struct roff
*, int,
165 const char *, int *, int *, int);
166 static int roff_evalpar(struct roff
*, int,
167 const char *, int *, int *, int);
168 static int roff_evalstrcond(const char *, int *);
169 static void roff_free1(struct roff
*);
170 static void roff_freereg(struct roffreg
*);
171 static void roff_freestr(struct roffkv
*);
172 static size_t roff_getname(struct roff
*, char **, int, int);
173 static int roff_getnum(const char *, int *, int *, int);
174 static int roff_getop(const char *, int *, char *);
175 static int roff_getregn(const struct roff
*,
176 const char *, size_t);
177 static int roff_getregro(const struct roff
*,
179 static const char *roff_getrenn(const struct roff
*,
180 const char *, size_t);
181 static const char *roff_getstrn(const struct roff
*,
182 const char *, size_t);
183 static int roff_hasregn(const struct roff
*,
184 const char *, size_t);
185 static enum rofferr
roff_insec(ROFF_ARGS
);
186 static enum rofferr
roff_it(ROFF_ARGS
);
187 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
188 static void roff_man_alloc1(struct roff_man
*);
189 static void roff_man_free1(struct roff_man
*);
190 static enum rofferr
roff_manyarg(ROFF_ARGS
);
191 static enum rofferr
roff_nr(ROFF_ARGS
);
192 static enum rofferr
roff_onearg(ROFF_ARGS
);
193 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
195 static enum rofferr
roff_parsetext(struct roff
*, struct buf
*,
197 static enum rofferr
roff_renamed(ROFF_ARGS
);
198 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
199 static enum rofferr
roff_rm(ROFF_ARGS
);
200 static enum rofferr
roff_rn(ROFF_ARGS
);
201 static enum rofferr
roff_rr(ROFF_ARGS
);
202 static void roff_setstr(struct roff
*,
203 const char *, const char *, int);
204 static void roff_setstrn(struct roffkv
**, const char *,
205 size_t, const char *, size_t, int);
206 static enum rofferr
roff_so(ROFF_ARGS
);
207 static enum rofferr
roff_tr(ROFF_ARGS
);
208 static enum rofferr
roff_Dd(ROFF_ARGS
);
209 static enum rofferr
roff_TH(ROFF_ARGS
);
210 static enum rofferr
roff_TE(ROFF_ARGS
);
211 static enum rofferr
roff_TS(ROFF_ARGS
);
212 static enum rofferr
roff_EQ(ROFF_ARGS
);
213 static enum rofferr
roff_EN(ROFF_ARGS
);
214 static enum rofferr
roff_T_(ROFF_ARGS
);
215 static enum rofferr
roff_unsupp(ROFF_ARGS
);
216 static enum rofferr
roff_userdef(ROFF_ARGS
);
218 /* --- constant data ------------------------------------------------------ */
220 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
221 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
223 const char *__roff_name
[MAN_MAX
+ 1] = {
224 "br", "ce", "ft", "ll",
225 "mc", "rj", "sp", "ta",
227 "ab", "ad", "af", "aln",
228 "als", "am", "am1", "ami",
229 "ami1", "as", "as1", "asciify",
230 "backtrace", "bd", "bleedat", "blm",
231 "box", "boxa", "bp", "BP",
232 "break", "breakchar", "brnl", "brp",
234 "cf", "cflags", "ch", "char",
235 "chop", "class", "close", "CL",
236 "color", "composite", "continue", "cp",
237 "cropat", "cs", "cu", "da",
238 "dch", "Dd", "de", "de1",
239 "defcolor", "dei", "dei1", "device",
240 "devicem", "di", "do", "ds",
241 "ds1", "dwh", "dt", "ec",
242 "ecr", "ecs", "el", "em",
243 "EN", "eo", "EP", "EQ",
244 "errprint", "ev", "evc", "ex",
245 "fallback", "fam", "fc", "fchar",
246 "fcolor", "fdeferlig", "feature", "fkern",
247 "fl", "flig", "fp", "fps",
248 "fschar", "fspacewidth", "fspecial", "ftr",
249 "fzoom", "gcolor", "hc", "hcode",
250 "hidechar", "hla", "hlm", "hpf",
251 "hpfa", "hpfcode", "hw", "hy",
252 "hylang", "hylen", "hym", "hypp",
253 "hys", "ie", "if", "ig",
254 "index", "it", "itc", "IX",
255 "kern", "kernafter", "kernbefore", "kernpair",
256 "lc", "lc_ctype", "lds", "length",
257 "letadj", "lf", "lg", "lhang",
258 "linetabs", "lnr", "lnrf", "lpfx",
260 "mediasize", "minss", "mk", "mso",
261 "na", "ne", "nh", "nhychar",
262 "nm", "nn", "nop", "nr",
263 "nrf", "nroff", "ns", "nx",
264 "open", "opena", "os", "output",
265 "padj", "papersize", "pc", "pev",
266 "pi", "PI", "pl", "pm",
267 "pn", "pnr", "po", "ps",
268 "psbb", "pshape", "pso", "ptr",
269 "pvs", "rchar", "rd", "recursionlimit",
270 "return", "rfschar", "rhang",
271 "rm", "rn", "rnn", "rr",
272 "rs", "rt", "schar", "sentchar",
273 "shc", "shift", "sizes", "so",
274 "spacewidth", "special", "spreadwarn", "ss",
275 "sty", "substring", "sv", "sy",
278 "tm", "tm1", "tmc", "tr",
279 "track", "transchar", "trf", "trimat",
280 "trin", "trnt", "troff", "TS",
281 "uf", "ul", "unformat", "unwatch",
282 "unwatchn", "vpt", "vs", "warn",
283 "warnscale", "watch", "watchlength", "watchn",
284 "wh", "while", "write", "writec",
285 "writem", "xflag", ".", NULL
,
287 "Dd", "Dt", "Os", "Sh",
288 "Ss", "Pp", "D1", "Dl",
289 "Bd", "Ed", "Bl", "El",
290 "It", "Ad", "An", "Ap",
291 "Ar", "Cd", "Cm", "Dv",
292 "Er", "Ev", "Ex", "Fa",
293 "Fd", "Fl", "Fn", "Ft",
294 "Ic", "In", "Li", "Nd",
295 "Nm", "Op", "Ot", "Pa",
296 "Rv", "St", "Va", "Vt",
297 "Xr", "%A", "%B", "%D",
298 "%I", "%J", "%N", "%O",
299 "%P", "%R", "%T", "%V",
300 "Ac", "Ao", "Aq", "At",
301 "Bc", "Bf", "Bo", "Bq",
302 "Bsx", "Bx", "Db", "Dc",
303 "Do", "Dq", "Ec", "Ef",
304 "Em", "Eo", "Fx", "Ms",
305 "No", "Ns", "Nx", "Ox",
306 "Pc", "Pf", "Po", "Pq",
307 "Qc", "Ql", "Qo", "Qq",
308 "Re", "Rs", "Sc", "So",
309 "Sq", "Sm", "Sx", "Sy",
310 "Tn", "Ux", "Xc", "Xo",
311 "Fo", "Fc", "Oo", "Oc",
312 "Bk", "Ek", "Bt", "Hf",
313 "Fr", "Ud", "Lb", "Lp",
314 "Lk", "Mt", "Brq", "Bro",
315 "Brc", "%C", "Es", "En",
316 "Dx", "%Q", "%U", "Ta",
318 "TH", "SH", "SS", "TP",
319 "LP", "PP", "P", "IP",
320 "HP", "SM", "SB", "BI",
321 "IB", "BR", "RB", "R",
322 "B", "I", "IR", "RI",
324 "RE", "RS", "DT", "UC",
326 "OP", "EX", "EE", "UR",
329 const char *const *roff_name
= __roff_name
;
331 static struct roffmac roffs
[TOKEN_NONE
] = {
332 { roff_br
, NULL
, NULL
, 0 }, /* br */
333 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
334 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
335 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
336 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
337 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
338 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
339 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
340 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
341 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
342 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
343 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
344 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
345 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
346 { roff_unsupp
, NULL
, NULL
, 0 }, /* als */
347 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
348 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
349 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
350 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
351 { roff_ds
, NULL
, NULL
, 0 }, /* as */
352 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
353 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
354 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
355 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
356 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
357 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
358 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
359 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
360 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
361 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
362 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
363 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
364 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
365 { roff_br
, NULL
, NULL
, 0 }, /* brp */
366 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
367 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
368 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
369 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
370 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
371 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
372 { roff_unsupp
, NULL
, NULL
, 0 }, /* char */
373 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
374 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
375 { roff_insec
, NULL
, NULL
, 0 }, /* close */
376 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
377 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
378 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
379 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
380 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
381 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
382 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
383 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
384 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
385 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
386 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
387 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
388 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
389 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
390 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
391 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
392 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
393 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
394 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
395 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
396 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
397 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
398 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
399 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
400 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
401 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
402 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
403 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
404 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
405 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
406 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
407 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
408 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
409 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
410 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
411 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
412 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
413 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
414 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
415 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
416 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
417 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
419 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
420 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
421 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
422 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
423 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
425 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
426 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
427 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
428 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
429 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
430 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
431 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
432 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
433 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
434 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
435 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
436 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
437 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
438 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
439 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
440 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
441 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
444 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
445 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
446 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
447 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
448 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
449 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
450 { roff_it
, NULL
, NULL
, 0 }, /* it */
451 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
453 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
454 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
457 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
458 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
459 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
460 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
462 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
465 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
466 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
467 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
468 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
470 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
475 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
476 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
477 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
478 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
479 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
480 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
481 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
482 { roff_unsupp
, NULL
, NULL
, 0 }, /* nop */
483 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
484 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
487 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
488 { roff_insec
, NULL
, NULL
, 0 }, /* open */
489 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
491 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
494 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
495 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
496 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
497 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
498 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* po */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
504 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
505 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
506 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
509 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
510 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
511 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
512 { roff_unsupp
, NULL
, NULL
, 0 }, /* return */
513 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
515 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
516 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
517 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
518 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
519 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
521 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
522 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
524 { roff_unsupp
, NULL
, NULL
, 0 }, /* shift */
525 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
526 { roff_so
, NULL
, NULL
, 0 }, /* so */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
532 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
534 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
535 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
536 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
537 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
538 { roff_TH
, NULL
, NULL
, 0 }, /* TH */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
540 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
541 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
542 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
543 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
544 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
545 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
546 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
547 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
549 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
550 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
552 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
555 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
561 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
562 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
563 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
564 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
565 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
566 { roff_unsupp
, NULL
, NULL
, 0 }, /* while */
567 { roff_insec
, NULL
, NULL
, 0 }, /* write */
568 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
569 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
571 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
572 { roff_renamed
, NULL
, NULL
, 0 },
573 { roff_userdef
, NULL
, NULL
, 0 }
576 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
577 const char *const __mdoc_reserved
[] = {
578 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
579 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
580 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
581 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
582 "Dt", "Dv", "Dx", "D1",
583 "Ec", "Ed", "Ef", "Ek", "El", "Em",
584 "En", "Eo", "Er", "Es", "Ev", "Ex",
585 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
586 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
587 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
588 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
589 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
590 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
591 "Sc", "Sh", "Sm", "So", "Sq",
592 "Ss", "St", "Sx", "Sy",
593 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
594 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
595 "%P", "%Q", "%R", "%T", "%U", "%V",
599 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
600 const char *const __man_reserved
[] = {
601 "AT", "B", "BI", "BR", "DT",
602 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
603 "LP", "OP", "P", "PD", "PP",
604 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
605 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
609 /* Array of injected predefined strings. */
610 #define PREDEFS_MAX 38
611 static const struct predef predefs
[PREDEFS_MAX
] = {
612 #include "predefs.in"
615 static int roffce_lines
; /* number of input lines to center */
616 static struct roff_node
*roffce_node
; /* active request */
617 static int roffit_lines
; /* number of lines to delay */
618 static char *roffit_macro
; /* nil-terminated macro line */
621 /* --- request table ------------------------------------------------------ */
624 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
632 htab
= mandoc_malloc(sizeof(*htab
));
633 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
635 for (tok
= mintok
; tok
< maxtok
; tok
++) {
636 if (roff_name
[tok
] == NULL
)
638 sz
= strlen(roff_name
[tok
]);
639 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
641 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
642 slot
= ohash_qlookup(htab
, req
->name
);
643 ohash_insert(htab
, slot
, req
);
649 roffhash_free(struct ohash
*htab
)
656 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
657 req
= ohash_next(htab
, &slot
))
664 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
671 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
673 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
674 return req
== NULL
? TOKEN_NONE
: req
->tok
;
677 /* --- stack of request blocks -------------------------------------------- */
680 * Pop the current node off of the stack of roff instructions currently
684 roffnode_pop(struct roff
*r
)
691 r
->last
= r
->last
->parent
;
698 * Push a roff node onto the instruction stack. This must later be
699 * removed with roffnode_pop().
702 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
707 p
= mandoc_calloc(1, sizeof(struct roffnode
));
710 p
->name
= mandoc_strdup(name
);
714 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
719 /* --- roff parser state data management ---------------------------------- */
722 roff_free1(struct roff
*r
)
724 struct tbl_node
*tbl
;
728 while (NULL
!= (tbl
= r
->first_tbl
)) {
729 r
->first_tbl
= tbl
->next
;
732 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
734 while (NULL
!= (e
= r
->first_eqn
)) {
735 r
->first_eqn
= e
->next
;
738 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
748 roff_freereg(r
->regtab
);
751 roff_freestr(r
->strtab
);
752 roff_freestr(r
->rentab
);
753 roff_freestr(r
->xmbtab
);
754 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
757 for (i
= 0; i
< 128; i
++)
764 roff_reset(struct roff
*r
)
767 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
777 roff_free(struct roff
*r
)
780 roffhash_free(r
->reqtab
);
785 roff_alloc(struct mparse
*parse
, int options
)
789 r
= mandoc_calloc(1, sizeof(struct roff
));
791 r
->reqtab
= roffhash_alloc(0, ROFF_USERDEF
);
792 r
->options
= options
;
793 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
799 /* --- syntax tree state data management ---------------------------------- */
802 roff_man_free1(struct roff_man
*man
)
805 if (man
->first
!= NULL
)
806 roff_node_delete(man
, man
->first
);
807 free(man
->meta
.msec
);
810 free(man
->meta
.arch
);
811 free(man
->meta
.title
);
812 free(man
->meta
.name
);
813 free(man
->meta
.date
);
817 roff_man_alloc1(struct roff_man
*man
)
820 memset(&man
->meta
, 0, sizeof(man
->meta
));
821 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
822 man
->first
->type
= ROFFT_ROOT
;
823 man
->last
= man
->first
;
826 man
->macroset
= MACROSET_NONE
;
827 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
828 man
->next
= ROFF_NEXT_CHILD
;
832 roff_man_reset(struct roff_man
*man
)
836 roff_man_alloc1(man
);
840 roff_man_free(struct roff_man
*man
)
848 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
849 const char *defos
, int quick
)
851 struct roff_man
*man
;
853 man
= mandoc_calloc(1, sizeof(*man
));
858 roff_man_alloc1(man
);
863 /* --- syntax tree handling ----------------------------------------------- */
866 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
867 enum roff_type type
, int tok
)
871 n
= mandoc_calloc(1, sizeof(*n
));
876 n
->sec
= man
->lastsec
;
878 if (man
->flags
& MDOC_SYNOPSIS
)
879 n
->flags
|= NODE_SYNPRETTY
;
881 n
->flags
&= ~NODE_SYNPRETTY
;
882 if (man
->flags
& MDOC_NEWLINE
)
883 n
->flags
|= NODE_LINE
;
884 man
->flags
&= ~MDOC_NEWLINE
;
890 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
894 case ROFF_NEXT_SIBLING
:
895 if (man
->last
->next
!= NULL
) {
896 n
->next
= man
->last
->next
;
897 man
->last
->next
->prev
= n
;
899 man
->last
->parent
->last
= n
;
902 n
->parent
= man
->last
->parent
;
904 case ROFF_NEXT_CHILD
:
905 if (man
->last
->child
!= NULL
) {
906 n
->next
= man
->last
->child
;
907 man
->last
->child
->prev
= n
;
910 man
->last
->child
= n
;
911 n
->parent
= man
->last
;
923 if (n
->end
!= ENDBODY_NOT
)
935 * Copy over the normalised-data pointer of our parent. Not
936 * everybody has one, but copying a null pointer is fine.
939 n
->norm
= n
->parent
->norm
;
940 assert(n
->parent
->type
== ROFFT_BLOCK
);
944 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
948 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
949 n
->string
= roff_strdup(man
->roff
, word
);
950 roff_node_append(man
, n
);
951 n
->flags
|= NODE_VALID
| NODE_ENDED
;
952 man
->next
= ROFF_NEXT_SIBLING
;
956 roff_word_append(struct roff_man
*man
, const char *word
)
959 char *addstr
, *newstr
;
962 addstr
= roff_strdup(man
->roff
, word
);
963 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
967 man
->next
= ROFF_NEXT_SIBLING
;
971 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
975 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
976 roff_node_append(man
, n
);
977 man
->next
= ROFF_NEXT_CHILD
;
981 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
985 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
986 roff_node_append(man
, n
);
987 man
->next
= ROFF_NEXT_CHILD
;
992 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
996 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
997 roff_node_append(man
, n
);
998 man
->next
= ROFF_NEXT_CHILD
;
1003 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1005 struct roff_node
*n
;
1007 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1008 roff_node_append(man
, n
);
1009 man
->next
= ROFF_NEXT_CHILD
;
1014 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
1016 struct roff_node
*n
;
1018 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1020 if (eqn
->ln
> man
->last
->line
)
1021 n
->flags
|= NODE_LINE
;
1022 roff_node_append(man
, n
);
1023 man
->next
= ROFF_NEXT_SIBLING
;
1027 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1029 struct roff_node
*n
;
1031 if (man
->macroset
== MACROSET_MAN
)
1032 man_breakscope(man
, ROFF_TS
);
1033 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1035 roff_node_append(man
, n
);
1036 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1037 man
->next
= ROFF_NEXT_SIBLING
;
1041 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1044 /* Adjust siblings. */
1047 n
->prev
->next
= n
->next
;
1049 n
->next
->prev
= n
->prev
;
1051 /* Adjust parent. */
1053 if (n
->parent
!= NULL
) {
1054 if (n
->parent
->child
== n
)
1055 n
->parent
->child
= n
->next
;
1056 if (n
->parent
->last
== n
)
1057 n
->parent
->last
= n
->prev
;
1060 /* Adjust parse point. */
1064 if (man
->last
== n
) {
1065 if (n
->prev
== NULL
) {
1066 man
->last
= n
->parent
;
1067 man
->next
= ROFF_NEXT_CHILD
;
1069 man
->last
= n
->prev
;
1070 man
->next
= ROFF_NEXT_SIBLING
;
1073 if (man
->first
== n
)
1078 roff_node_free(struct roff_node
*n
)
1081 if (n
->args
!= NULL
)
1082 mdoc_argv_free(n
->args
);
1083 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1090 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1093 while (n
->child
!= NULL
)
1094 roff_node_delete(man
, n
->child
);
1095 roff_node_unlink(man
, n
);
1100 deroff(char **dest
, const struct roff_node
*n
)
1105 if (n
->type
!= ROFFT_TEXT
) {
1106 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1111 /* Skip leading whitespace. */
1113 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1114 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1115 strchr(" %&0^|~", cp
[1]) != NULL
)
1117 else if ( ! isspace((unsigned char)*cp
))
1121 /* Skip trailing backslash. */
1124 if (sz
> 0 && cp
[sz
- 1] == '\\')
1127 /* Skip trailing whitespace. */
1130 if ( ! isspace((unsigned char)cp
[sz
-1]))
1133 /* Skip empty strings. */
1138 if (*dest
== NULL
) {
1139 *dest
= mandoc_strndup(cp
, sz
);
1143 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1148 /* --- main functions of the roff parser ---------------------------------- */
1151 * In the current line, expand escape sequences that tend to get
1152 * used in numerical expressions and conditional requests.
1153 * Also check the syntax of the remaining escape sequences.
1156 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1158 char ubuf
[24]; /* buffer to print the number */
1159 const char *start
; /* start of the string to process */
1160 char *stesc
; /* start of an escape sequence ('\\') */
1161 const char *stnam
; /* start of the name, after "[(*" */
1162 const char *cp
; /* end of the name, e.g. before ']' */
1163 const char *res
; /* the string to be substituted */
1164 char *nbuf
; /* new buffer to copy buf->buf to */
1165 size_t maxl
; /* expected length of the escape name */
1166 size_t naml
; /* actual length of the escape name */
1167 enum mandoc_esc esc
; /* type of the escape sequence */
1168 int inaml
; /* length returned from mandoc_escape() */
1169 int expand_count
; /* to avoid infinite loops */
1170 int npos
; /* position in numeric expression */
1171 int arg_complete
; /* argument not interrupted by eol */
1172 int done
; /* no more input available */
1173 char term
; /* character terminating the escape */
1175 /* Search forward for comments. */
1178 start
= buf
->buf
+ pos
;
1179 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1180 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1183 if (*stesc
!= '"' && *stesc
!= '#')
1185 cp
= strchr(stesc
--, '\0') - 1;
1190 if (*cp
== ' ' || *cp
== '\t')
1191 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1192 ln
, cp
- buf
->buf
, NULL
);
1193 while (stesc
> start
&& stesc
[-1] == ' ')
1202 /* Notice the end of the input. */
1204 if (*stesc
== '\n') {
1210 while (stesc
>= start
) {
1212 /* Search backwards for the next backslash. */
1214 if (*stesc
!= r
->escape
) {
1215 if (*stesc
== '\\') {
1217 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1218 buf
->buf
, stesc
+ 1) + 1;
1220 stesc
= nbuf
+ (stesc
- buf
->buf
);
1228 /* If it is escaped, skip it. */
1230 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1231 if (*cp
!= r
->escape
)
1234 if ((stesc
- cp
) % 2 == 0) {
1238 } else if (stesc
[1] != '\0') {
1248 /* Decide whether to expand or to check only. */
1264 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1265 if (esc
== ESCAPE_ERROR
||
1266 (esc
== ESCAPE_SPECIAL
&&
1267 mchars_spec2cp(stnam
, inaml
) < 0))
1268 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1269 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1270 "%.*s", (int)(cp
- stesc
), stesc
);
1275 if (EXPAND_LIMIT
< ++expand_count
) {
1276 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1277 ln
, (int)(stesc
- buf
->buf
), NULL
);
1282 * The third character decides the length
1283 * of the name of the string or register.
1284 * Save a pointer to the name.
1311 /* Advance to the end of the name. */
1315 while (maxl
== 0 || naml
< maxl
) {
1317 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1318 ln
, (int)(stesc
- buf
->buf
), stesc
);
1322 if (maxl
== 0 && *cp
== term
) {
1326 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1330 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1331 case ESCAPE_SPECIAL
:
1332 case ESCAPE_UNICODE
:
1333 case ESCAPE_NUMBERED
:
1334 case ESCAPE_OVERSTRIKE
:
1343 * Retrieve the replacement string; if it is
1344 * undefined, resume searching for escapes.
1350 res
= roff_getstrn(r
, stnam
, naml
);
1354 ubuf
[0] = arg_complete
&&
1355 roff_evalnum(r
, ln
, stnam
, &npos
,
1356 NULL
, ROFFNUM_SCALE
) &&
1357 stnam
+ npos
+ 1 == cp
? '1' : '0';
1362 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1363 roff_getregn(r
, stnam
, naml
));
1368 /* use even incomplete args */
1369 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1375 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1376 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1377 "%.*s", (int)naml
, stnam
);
1379 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1380 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1381 ln
, (int)(stesc
- buf
->buf
), NULL
);
1385 /* Replace the escape sequence by the string. */
1388 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1389 buf
->buf
, res
, cp
) + 1;
1391 /* Prepare for the next replacement. */
1394 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1402 * Process text streams.
1405 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1411 enum mandoc_esc esc
;
1413 /* Spring the input line trap. */
1415 if (roffit_lines
== 1) {
1416 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1423 return ROFF_REPARSE
;
1424 } else if (roffit_lines
> 1)
1427 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1428 if (roffce_lines
< 1) {
1429 r
->man
->last
= roffce_node
;
1430 r
->man
->next
= ROFF_NEXT_SIBLING
;
1437 /* Convert all breakable hyphens into ASCII_HYPH. */
1439 start
= p
= buf
->buf
+ pos
;
1441 while (*p
!= '\0') {
1442 sz
= strcspn(p
, "-\\");
1449 /* Skip over escapes. */
1451 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1452 if (esc
== ESCAPE_ERROR
)
1457 } else if (p
== start
) {
1462 if (isalpha((unsigned char)p
[-1]) &&
1463 isalpha((unsigned char)p
[1]))
1471 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1475 int pos
; /* parse point */
1476 int spos
; /* saved parse point for messages */
1477 int ppos
; /* original offset in buf->buf */
1478 int ctl
; /* macro line (boolean) */
1482 /* Handle in-line equation delimiters. */
1484 if (r
->tbl
== NULL
&&
1485 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1486 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1487 e
= roff_eqndelim(r
, buf
, pos
);
1488 if (e
== ROFF_REPARSE
)
1490 assert(e
== ROFF_CONT
);
1493 /* Expand some escape sequences. */
1495 e
= roff_res(r
, buf
, ln
, pos
);
1496 if (e
== ROFF_IGN
|| e
== ROFF_APPEND
)
1498 assert(e
== ROFF_CONT
);
1500 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1503 * First, if a scope is open and we're not a macro, pass the
1504 * text through the macro's filter.
1505 * Equations process all content themselves.
1506 * Tables process almost all content themselves, but we want
1507 * to warn about macros before passing it there.
1510 if (r
->last
!= NULL
&& ! ctl
) {
1512 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1515 assert(e
== ROFF_CONT
);
1518 return eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
);
1519 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1520 return tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1522 return roff_parsetext(r
, buf
, pos
, offs
);
1524 /* Skip empty request lines. */
1526 if (buf
->buf
[pos
] == '"') {
1527 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1530 } else if (buf
->buf
[pos
] == '\0')
1534 * If a scope is open, go to the child handler for that macro,
1535 * as it may want to preprocess before doing anything with it.
1536 * Don't do so if an equation is open.
1541 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1544 /* No scope is open. This is a new request or macro. */
1547 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1549 /* Tables ignore most macros. */
1551 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1552 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1553 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1554 ln
, pos
, buf
->buf
+ spos
);
1555 if (t
!= TOKEN_NONE
)
1557 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1559 while (buf
->buf
[pos
] == ' ')
1561 return tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1564 /* For now, let high level macros abort .ce mode. */
1566 if (ctl
&& roffce_node
!= NULL
&&
1567 (t
== TOKEN_NONE
|| t
== ROFF_EQ
|| t
== ROFF_TS
)) {
1568 r
->man
->last
= roffce_node
;
1569 r
->man
->next
= ROFF_NEXT_SIBLING
;
1575 * This is neither a roff request nor a user-defined macro.
1576 * Let the standard macro set parsers handle it.
1579 if (t
== TOKEN_NONE
)
1582 /* Execute a roff request or a user defined macro. */
1584 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1588 roff_endparse(struct roff
*r
)
1592 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1593 r
->last
->line
, r
->last
->col
,
1594 roff_name
[r
->last
->tok
]);
1597 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1598 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1603 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1604 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1610 * Parse a roff node's type from the input buffer. This must be in the
1611 * form of ".foo xxx" in the usual way.
1613 static enum roff_tok
1614 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1623 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1627 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1629 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
)) ?
1631 (r
->current_string
= roff_getrenn(r
, mac
, maclen
)) ?
1632 ROFF_RENAMED
: roffhash_find(r
->reqtab
, mac
, maclen
);
1634 if (t
!= TOKEN_NONE
)
1640 /* --- handling of request blocks ----------------------------------------- */
1643 roff_cblock(ROFF_ARGS
)
1647 * A block-close `..' should only be invoked as a child of an
1648 * ignore macro, otherwise raise a warning and just ignore it.
1651 if (r
->last
== NULL
) {
1652 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1657 switch (r
->last
->tok
) {
1659 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1662 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1667 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1672 if (buf
->buf
[pos
] != '\0')
1673 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1674 ".. %s", buf
->buf
+ pos
);
1677 roffnode_cleanscope(r
);
1683 roffnode_cleanscope(struct roff
*r
)
1687 if (--r
->last
->endspan
!= 0)
1694 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1697 if (NULL
== r
->last
) {
1698 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1703 switch (r
->last
->tok
) {
1709 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1714 if (r
->last
->endspan
> -1) {
1715 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1721 roffnode_cleanscope(r
);
1726 roff_block(ROFF_ARGS
)
1732 /* Ignore groff compatibility mode for now. */
1734 if (tok
== ROFF_de1
)
1736 else if (tok
== ROFF_dei1
)
1738 else if (tok
== ROFF_am1
)
1740 else if (tok
== ROFF_ami1
)
1743 /* Parse the macro name argument. */
1745 cp
= buf
->buf
+ pos
;
1746 if (tok
== ROFF_ig
) {
1751 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1752 iname
[namesz
] = '\0';
1755 /* Resolve the macro name argument if it is indirect. */
1757 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1758 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1759 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1760 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1761 "%.*s", (int)namesz
, iname
);
1764 namesz
= strlen(name
);
1768 if (namesz
== 0 && tok
!= ROFF_ig
) {
1769 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1770 ln
, ppos
, roff_name
[tok
]);
1774 roffnode_push(r
, tok
, name
, ln
, ppos
);
1777 * At the beginning of a `de' macro, clear the existing string
1778 * with the same name, if there is one. New content will be
1779 * appended from roff_block_text() in multiline mode.
1782 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1783 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1788 /* Get the custom end marker. */
1791 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1793 /* Resolve the end marker if it is indirect. */
1795 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1796 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1797 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1798 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1799 "%.*s", (int)namesz
, iname
);
1802 namesz
= strlen(name
);
1807 r
->last
->end
= mandoc_strndup(name
, namesz
);
1810 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1811 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
1817 roff_block_sub(ROFF_ARGS
)
1823 * First check whether a custom macro exists at this level. If
1824 * it does, then check against it. This is some of groff's
1825 * stranger behaviours. If we encountered a custom end-scope
1826 * tag and that tag also happens to be a "real" macro, then we
1827 * need to try interpreting it again as a real macro. If it's
1828 * not, then return ignore. Else continue.
1832 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1833 if (buf
->buf
[i
] != r
->last
->end
[j
])
1836 if (r
->last
->end
[j
] == '\0' &&
1837 (buf
->buf
[i
] == '\0' ||
1838 buf
->buf
[i
] == ' ' ||
1839 buf
->buf
[i
] == '\t')) {
1841 roffnode_cleanscope(r
);
1843 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1847 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1855 * If we have no custom end-query or lookup failed, then try
1856 * pulling it out of the hashtable.
1859 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1861 if (t
!= ROFF_cblock
) {
1863 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1867 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1871 roff_block_text(ROFF_ARGS
)
1875 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1881 roff_cond_sub(ROFF_ARGS
)
1888 roffnode_cleanscope(r
);
1889 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1892 * Fully handle known macros when they are structurally
1893 * required or when the conditional evaluated to true.
1896 if (t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
))
1897 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1900 * If `\}' occurs on a macro line without a preceding macro,
1901 * drop the line completely.
1904 ep
= buf
->buf
+ pos
;
1905 if (ep
[0] == '\\' && ep
[1] == '}')
1908 /* Always check for the closing delimiter `\}'. */
1910 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1911 if (*(++ep
) == '}') {
1913 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1918 return rr
? ROFF_CONT
: ROFF_IGN
;
1922 roff_cond_text(ROFF_ARGS
)
1928 roffnode_cleanscope(r
);
1930 ep
= buf
->buf
+ pos
;
1931 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1932 if (*(++ep
) == '}') {
1934 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1939 return rr
? ROFF_CONT
: ROFF_IGN
;
1942 /* --- handling of numeric and conditional expressions -------------------- */
1945 * Parse a single signed integer number. Stop at the first non-digit.
1946 * If there is at least one digit, return success and advance the
1947 * parse point, else return failure and let the parse point unchanged.
1948 * Ignore overflows, treat them just like the C language.
1951 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
1953 int myres
, scaled
, n
, p
;
1960 if (n
|| v
[p
] == '+')
1963 if (flags
& ROFFNUM_WHITE
)
1964 while (isspace((unsigned char)v
[p
]))
1967 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1968 *res
= 10 * *res
+ v
[p
] - '0';
1975 /* Each number may be followed by one optional scaling unit. */
1979 scaled
= *res
* 65536;
1982 scaled
= *res
* 240;
1985 scaled
= *res
* 240 / 2.54;
1996 scaled
= *res
* 10 / 3;
2002 scaled
= *res
* 6 / 25;
2009 if (flags
& ROFFNUM_SCALE
)
2017 * Evaluate a string comparison condition.
2018 * The first character is the delimiter.
2019 * Succeed if the string up to its second occurrence
2020 * matches the string up to its third occurence.
2021 * Advance the cursor after the third occurrence
2022 * or lacking that, to the end of the line.
2025 roff_evalstrcond(const char *v
, int *pos
)
2027 const char *s1
, *s2
, *s3
;
2031 s1
= v
+ *pos
; /* initial delimiter */
2032 s2
= s1
+ 1; /* for scanning the first string */
2033 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2035 if (NULL
== s3
) /* found no middle delimiter */
2038 while ('\0' != *++s3
) {
2039 if (*s2
!= *s3
) { /* mismatch */
2040 s3
= strchr(s3
, *s1
);
2043 if (*s3
== *s1
) { /* found the final delimiter */
2052 s3
= strchr(s2
, '\0');
2053 else if (*s3
!= '\0')
2060 * Evaluate an optionally negated single character, numerical,
2061 * or string condition.
2064 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2068 int number
, savepos
, wanttrue
;
2070 if ('!' == v
[*pos
]) {
2091 cp
= name
= v
+ ++*pos
;
2092 sz
= roff_getname(r
, &cp
, ln
, *pos
);
2094 return (sz
&& roff_hasregn(r
, name
, sz
)) == wanttrue
;
2100 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2101 return (number
> 0) == wanttrue
;
2102 else if (*pos
== savepos
)
2103 return roff_evalstrcond(v
, pos
) == wanttrue
;
2109 roff_line_ignore(ROFF_ARGS
)
2116 roff_insec(ROFF_ARGS
)
2119 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2120 ln
, ppos
, roff_name
[tok
]);
2125 roff_unsupp(ROFF_ARGS
)
2128 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2129 ln
, ppos
, roff_name
[tok
]);
2134 roff_cond(ROFF_ARGS
)
2137 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2140 * An `.el' has no conditional body: it will consume the value
2141 * of the current rstack entry set in prior `ie' calls or
2144 * If we're not an `el', however, then evaluate the conditional.
2147 r
->last
->rule
= tok
== ROFF_el
?
2148 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2149 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2152 * An if-else will put the NEGATION of the current evaluated
2153 * conditional into the stack of rules.
2156 if (tok
== ROFF_ie
) {
2157 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2159 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2160 r
->rstacksz
, sizeof(int));
2162 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2165 /* If the parent has false as its rule, then so do we. */
2167 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2172 * If there is nothing on the line after the conditional,
2173 * not even whitespace, use next-line scope.
2176 if (buf
->buf
[pos
] == '\0') {
2177 r
->last
->endspan
= 2;
2181 while (buf
->buf
[pos
] == ' ')
2184 /* An opening brace requests multiline scope. */
2186 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2187 r
->last
->endspan
= -1;
2189 while (buf
->buf
[pos
] == ' ')
2195 * Anything else following the conditional causes
2196 * single-line scope. Warn if the scope contains
2197 * nothing but trailing whitespace.
2200 if (buf
->buf
[pos
] == '\0')
2201 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2202 ln
, ppos
, roff_name
[tok
]);
2204 r
->last
->endspan
= 1;
2218 /* Ignore groff compatibility mode for now. */
2220 if (tok
== ROFF_ds1
)
2222 else if (tok
== ROFF_as1
)
2226 * The first word is the name of the string.
2227 * If it is empty or terminated by an escape sequence,
2228 * abort the `ds' request without defining anything.
2231 name
= string
= buf
->buf
+ pos
;
2235 namesz
= roff_getname(r
, &string
, ln
, pos
);
2236 if (name
[namesz
] == '\\')
2239 /* Read past the initial double-quote, if any. */
2243 /* The rest is the value. */
2244 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2250 * Parse a single operator, one or two characters long.
2251 * If the operator is recognized, return success and advance the
2252 * parse point, else return failure and let the parse point unchanged.
2255 roff_getop(const char *v
, int *pos
, char *res
)
2270 switch (v
[*pos
+ 1]) {
2288 switch (v
[*pos
+ 1]) {
2302 if ('=' == v
[*pos
+ 1])
2314 * Evaluate either a parenthesized numeric expression
2315 * or a single signed integer number.
2318 roff_evalpar(struct roff
*r
, int ln
,
2319 const char *v
, int *pos
, int *res
, int flags
)
2323 return roff_getnum(v
, pos
, res
, flags
);
2326 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2330 * Omission of the closing parenthesis
2331 * is an error in validation mode,
2332 * but ignored in evaluation mode.
2337 else if (NULL
== res
)
2344 * Evaluate a complete numeric expression.
2345 * Proceed left to right, there is no concept of precedence.
2348 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2349 int *pos
, int *res
, int flags
)
2351 int mypos
, operand2
;
2359 if (flags
& ROFFNUM_WHITE
)
2360 while (isspace((unsigned char)v
[*pos
]))
2363 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2367 if (flags
& ROFFNUM_WHITE
)
2368 while (isspace((unsigned char)v
[*pos
]))
2371 if ( ! roff_getop(v
, pos
, &operator))
2374 if (flags
& ROFFNUM_WHITE
)
2375 while (isspace((unsigned char)v
[*pos
]))
2378 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2381 if (flags
& ROFFNUM_WHITE
)
2382 while (isspace((unsigned char)v
[*pos
]))
2399 if (operand2
== 0) {
2400 mandoc_msg(MANDOCERR_DIVZERO
,
2401 r
->parse
, ln
, *pos
, v
);
2408 if (operand2
== 0) {
2409 mandoc_msg(MANDOCERR_DIVZERO
,
2410 r
->parse
, ln
, *pos
, v
);
2417 *res
= *res
< operand2
;
2420 *res
= *res
> operand2
;
2423 *res
= *res
<= operand2
;
2426 *res
= *res
>= operand2
;
2429 *res
= *res
== operand2
;
2432 *res
= *res
!= operand2
;
2435 *res
= *res
&& operand2
;
2438 *res
= *res
|| operand2
;
2441 if (operand2
< *res
)
2445 if (operand2
> *res
)
2455 /* --- register management ------------------------------------------------ */
2458 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2460 struct roffreg
*reg
;
2462 /* Search for an existing register with the same name. */
2465 while (reg
&& strcmp(name
, reg
->key
.p
))
2469 /* Create a new register. */
2470 reg
= mandoc_malloc(sizeof(struct roffreg
));
2471 reg
->key
.p
= mandoc_strdup(name
);
2472 reg
->key
.sz
= strlen(name
);
2474 reg
->next
= r
->regtab
;
2480 else if ('-' == sign
)
2487 * Handle some predefined read-only number registers.
2488 * For now, return -1 if the requested register is not predefined;
2489 * in case a predefined read-only register having the value -1
2490 * were to turn up, another special value would have to be chosen.
2493 roff_getregro(const struct roff
*r
, const char *name
)
2497 case '$': /* Number of arguments of the last macro evaluated. */
2499 case 'A': /* ASCII approximation mode is always off. */
2501 case 'g': /* Groff compatibility mode is always on. */
2503 case 'H': /* Fixed horizontal resolution. */
2505 case 'j': /* Always adjust left margin only. */
2507 case 'T': /* Some output device is always defined. */
2509 case 'V': /* Fixed vertical resolution. */
2517 roff_getreg(const struct roff
*r
, const char *name
)
2519 struct roffreg
*reg
;
2522 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2523 val
= roff_getregro(r
, name
+ 1);
2528 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2529 if (0 == strcmp(name
, reg
->key
.p
))
2536 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2538 struct roffreg
*reg
;
2541 if ('.' == name
[0] && 2 == len
) {
2542 val
= roff_getregro(r
, name
+ 1);
2547 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2548 if (len
== reg
->key
.sz
&&
2549 0 == strncmp(name
, reg
->key
.p
, len
))
2556 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2558 struct roffreg
*reg
;
2561 if ('.' == name
[0] && 2 == len
) {
2562 val
= roff_getregro(r
, name
+ 1);
2567 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2568 if (len
== reg
->key
.sz
&&
2569 0 == strncmp(name
, reg
->key
.p
, len
))
2576 roff_freereg(struct roffreg
*reg
)
2578 struct roffreg
*old_reg
;
2580 while (NULL
!= reg
) {
2596 key
= val
= buf
->buf
+ pos
;
2600 keysz
= roff_getname(r
, &val
, ln
, pos
);
2601 if (key
[keysz
] == '\\')
2606 if (sign
== '+' || sign
== '-')
2609 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2610 roff_setreg(r
, key
, iv
, sign
);
2618 struct roffreg
*reg
, **prev
;
2622 name
= cp
= buf
->buf
+ pos
;
2625 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2626 name
[namesz
] = '\0';
2631 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2643 /* --- handler functions for roff requests -------------------------------- */
2652 cp
= buf
->buf
+ pos
;
2653 while (*cp
!= '\0') {
2655 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2656 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2657 if (name
[namesz
] == '\\')
2668 /* Parse the number of lines. */
2670 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2671 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2672 ln
, ppos
, buf
->buf
+ 1);
2676 while (isspace((unsigned char)buf
->buf
[pos
]))
2680 * Arm the input line trap.
2681 * Special-casing "an-trap" is an ugly workaround to cope
2682 * with DocBook stupidly fiddling with man(7) internals.
2686 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2687 strcmp(buf
->buf
+ pos
, "an-trap") ?
2688 buf
->buf
+ pos
: "br");
2695 const char *const *cp
;
2697 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2698 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2699 roff_setstr(r
, *cp
, NULL
, 0);
2702 r
->format
= MPARSE_MDOC
;
2710 const char *const *cp
;
2712 if ((r
->options
& MPARSE_QUICK
) == 0)
2713 for (cp
= __man_reserved
; *cp
; cp
++)
2714 roff_setstr(r
, *cp
, NULL
, 0);
2717 r
->format
= MPARSE_MAN
;
2727 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2729 else if ( ! tbl_end(&r
->tbl
)) {
2731 buf
->buf
= mandoc_strdup(".sp");
2733 return ROFF_REPARSE
;
2743 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2746 tbl_restart(ln
, ppos
, r
->tbl
);
2752 * Handle in-line equation delimiters.
2755 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2758 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2761 * Outside equations, look for an opening delimiter.
2762 * If we are inside an equation, we already know it is
2763 * in-line, or this function wouldn't have been called;
2764 * so look for a closing delimiter.
2767 cp1
= buf
->buf
+ pos
;
2768 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2769 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2774 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2776 /* Handle preceding text, protecting whitespace. */
2778 if (*buf
->buf
!= '\0') {
2785 * Prepare replacing the delimiter with an equation macro
2786 * and drop leading white space from the equation.
2789 if (r
->eqn
== NULL
) {
2796 /* Handle following text, protecting whitespace. */
2804 /* Do the actual replacement. */
2806 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2807 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2811 /* Toggle the in-line state of the eqn subsystem. */
2813 r
->eqn_inline
= r
->eqn
== NULL
;
2814 return ROFF_REPARSE
;
2822 assert(r
->eqn
== NULL
);
2823 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2826 r
->last_eqn
->next
= e
;
2827 e
->delim
= r
->last_eqn
->delim
;
2828 e
->odelim
= r
->last_eqn
->odelim
;
2829 e
->cdelim
= r
->last_eqn
->cdelim
;
2831 r
->first_eqn
= r
->last_eqn
= e
;
2833 r
->eqn
= r
->last_eqn
= e
;
2835 if (buf
->buf
[pos
] != '\0')
2836 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2837 ".EQ %s", buf
->buf
+ pos
);
2846 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2853 struct tbl_node
*tbl
;
2856 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2857 ln
, ppos
, "TS breaks TS");
2861 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2864 r
->last_tbl
->next
= tbl
;
2866 r
->first_tbl
= r
->last_tbl
= tbl
;
2868 r
->tbl
= r
->last_tbl
= tbl
;
2873 roff_onearg(ROFF_ARGS
)
2875 struct roff_node
*n
;
2879 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
2880 (tok
== ROFF_sp
|| tok
== ROFF_ti
))
2881 man_breakscope(r
->man
, tok
);
2883 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
2884 r
->man
->last
= roffce_node
;
2885 r
->man
->next
= ROFF_NEXT_SIBLING
;
2888 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
2891 cp
= buf
->buf
+ pos
;
2893 while (*cp
!= '\0' && *cp
!= ' ')
2898 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
2899 r
->parse
, ln
, cp
- buf
->buf
,
2900 "%s ... %s", roff_name
[tok
], cp
);
2901 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
2904 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
2905 if (r
->man
->last
->type
== ROFFT_ELEM
) {
2906 roff_word_alloc(r
->man
, ln
, pos
, "1");
2907 r
->man
->last
->flags
|= NODE_NOSRC
;
2910 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
2911 &roffce_lines
, 0) == 0) {
2912 mandoc_vmsg(MANDOCERR_CE_NONUM
,
2913 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
2916 if (roffce_lines
< 1) {
2917 r
->man
->last
= r
->man
->last
->parent
;
2921 roffce_node
= r
->man
->last
->parent
;
2923 n
->flags
|= NODE_VALID
| NODE_ENDED
;
2926 n
->flags
|= NODE_LINE
;
2927 r
->man
->next
= ROFF_NEXT_SIBLING
;
2932 roff_manyarg(ROFF_ARGS
)
2934 struct roff_node
*n
;
2937 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
2940 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
2941 while (*ep
!= '\0' && *ep
!= ' ')
2945 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
2948 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
2950 r
->man
->next
= ROFF_NEXT_SIBLING
;
2957 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
2958 man_breakscope(r
->man
, ROFF_br
);
2959 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
2960 if (buf
->buf
[pos
] != '\0')
2961 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2962 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
2963 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
2964 r
->man
->next
= ROFF_NEXT_SIBLING
;
2975 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2979 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2980 ln
, p
- buf
->buf
, "cc ... %s", p
);
2996 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2997 ln
, p
- buf
->buf
, "ec ... %s", p
);
3006 if (buf
->buf
[pos
] != '\0')
3007 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3008 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3015 const char *p
, *first
, *second
;
3017 enum mandoc_esc esc
;
3022 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3026 while (*p
!= '\0') {
3030 if (*first
== '\\') {
3031 esc
= mandoc_escape(&p
, NULL
, NULL
);
3032 if (esc
== ESCAPE_ERROR
) {
3033 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3034 ln
, (int)(p
- buf
->buf
), first
);
3037 fsz
= (size_t)(p
- first
);
3041 if (*second
== '\\') {
3042 esc
= mandoc_escape(&p
, NULL
, NULL
);
3043 if (esc
== ESCAPE_ERROR
) {
3044 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3045 ln
, (int)(p
- buf
->buf
), second
);
3048 ssz
= (size_t)(p
- second
);
3049 } else if (*second
== '\0') {
3050 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3051 ln
, first
- buf
->buf
, "tr %s", first
);
3057 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3062 if (r
->xtab
== NULL
)
3063 r
->xtab
= mandoc_calloc(128,
3064 sizeof(struct roffstr
));
3066 free(r
->xtab
[(int)*first
].p
);
3067 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3068 r
->xtab
[(int)*first
].sz
= ssz
;
3078 char *oldn
, *newn
, *end
;
3079 size_t oldsz
, newsz
;
3081 oldn
= newn
= buf
->buf
+ pos
;
3085 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3086 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3090 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3095 * Rename a user-defined macro bearing the old name,
3096 * overriding an existing renamed high-level macro
3097 * bearing the new name, if that exists.
3100 if ((value
= roff_getstrn(r
, oldn
, oldsz
)) != NULL
) {
3101 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3102 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3103 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3108 * Rename a high-level macro bearing the old name,
3109 * either renaming it a second time if it was already
3110 * renamed before, or renaming it for the first time.
3111 * In both cases, override an existing user-defined
3112 * macro bearing the new name, if that exists.
3115 if ((value
= roff_getrenn(r
, oldn
, oldsz
)) != NULL
) {
3116 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3117 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3119 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3120 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3129 name
= buf
->buf
+ pos
;
3130 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3133 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3134 * opening anything that's not in our cwd or anything beneath
3135 * it. Thus, explicitly disallow traversing up the file-system
3136 * or using absolute paths.
3139 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3140 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3142 buf
->sz
= mandoc_asprintf(&cp
,
3143 ".sp\nSee the file %s.\n.sp", name
) + 1;
3147 return ROFF_REPARSE
;
3154 /* --- user defined strings and macros ------------------------------------ */
3157 roff_userdef(ROFF_ARGS
)
3159 const char *arg
[9], *ap
;
3161 int expand_count
, i
, ib
, ie
;
3165 * Collect pointers to macro argument strings
3166 * and NUL-terminate them.
3170 cp
= buf
->buf
+ pos
;
3171 for (i
= 0; i
< 9; i
++) {
3175 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3181 * Expand macro arguments.
3184 buf
->sz
= strlen(r
->current_string
) + 1;
3185 n1
= n2
= cp
= mandoc_malloc(buf
->sz
);
3186 memcpy(n1
, r
->current_string
, buf
->sz
);
3188 while (*cp
!= '\0') {
3190 /* Scan ahead for the next argument invocation. */
3196 if (*cp
== '*') { /* \\$* inserts all arguments */
3199 } else { /* \\$1 .. \\$9 insert one argument */
3200 ib
= ie
= *cp
- '1';
3201 if (ib
< 0 || ib
> 8)
3207 * Prevent infinite recursion.
3212 else if (++expand_count
> EXPAND_LIMIT
) {
3213 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
3214 ln
, (int)(cp
- n1
), NULL
);
3221 * Determine the size of the expanded argument,
3222 * taking escaping of quotes into account.
3225 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3226 for (i
= ib
; i
<= ie
; i
++) {
3227 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3236 * Determine the size of the rest of the
3237 * unexpanded macro, including the NUL.
3240 rsz
= buf
->sz
- (cp
- n1
) - 3;
3243 * When shrinking, move before
3244 * releasing the storage.
3248 memmove(cp
+ asz
, cp
+ 3, rsz
);
3251 * Resize the storage for the macro
3252 * and readjust the parse pointer.
3256 n2
= mandoc_realloc(n1
, buf
->sz
);
3257 cp
= n2
+ (cp
- n1
);
3261 * When growing, make room
3262 * for the expanded argument.
3266 memmove(cp
+ asz
, cp
+ 3, rsz
);
3269 /* Copy the expanded argument, escaping quotes. */
3272 for (i
= ib
; i
<= ie
; i
++) {
3273 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3275 memcpy(n2
, "\\(dq", 4);
3286 * Replace the macro invocation
3287 * by the expanded macro.
3294 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3295 ROFF_REPARSE
: ROFF_APPEND
;
3299 * Calling a high-level macro that was renamed with .rn.
3300 * r->current_string has already been set up by roff_parse().
3303 roff_renamed(ROFF_ARGS
)
3307 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s %s", r
->current_string
,
3308 buf
->buf
+ pos
) + 1;
3315 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3324 /* Read until end of name and terminate it with NUL. */
3325 for (cp
= name
; 1; cp
++) {
3326 if ('\0' == *cp
|| ' ' == *cp
) {
3333 if ('{' == cp
[1] || '}' == cp
[1])
3338 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3339 "%.*s", (int)(cp
- name
+ 1), name
);
3340 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3344 /* Read past spaces. */
3353 * Store *string into the user-defined string called *name.
3354 * To clear an existing entry, call with (*r, *name, NULL, 0).
3355 * append == 0: replace mode
3356 * append == 1: single-line append mode
3357 * append == 2: multiline append mode, append '\n' after each call
3360 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3364 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3365 string
? strlen(string
) : 0, append
);
3369 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3370 const char *string
, size_t stringsz
, int append
)
3375 size_t oldch
, newch
;
3377 /* Search for an existing string with the same name. */
3380 while (n
&& (namesz
!= n
->key
.sz
||
3381 strncmp(n
->key
.p
, name
, namesz
)))
3385 /* Create a new string table entry. */
3386 n
= mandoc_malloc(sizeof(struct roffkv
));
3387 n
->key
.p
= mandoc_strndup(name
, namesz
);
3393 } else if (0 == append
) {
3403 * One additional byte for the '\n' in multiline mode,
3404 * and one for the terminating '\0'.
3406 newch
= stringsz
+ (1 < append
? 2u : 1u);
3408 if (NULL
== n
->val
.p
) {
3409 n
->val
.p
= mandoc_malloc(newch
);
3414 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3417 /* Skip existing content in the destination buffer. */
3418 c
= n
->val
.p
+ (int)oldch
;
3420 /* Append new content to the destination buffer. */
3422 while (i
< (int)stringsz
) {
3424 * Rudimentary roff copy mode:
3425 * Handle escaped backslashes.
3427 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3432 /* Append terminating bytes. */
3437 n
->val
.sz
= (int)(c
- n
->val
.p
);
3441 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3443 const struct roffkv
*n
;
3446 for (n
= r
->strtab
; n
; n
= n
->next
)
3447 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3448 '\0' == n
->key
.p
[(int)len
])
3451 for (i
= 0; i
< PREDEFS_MAX
; i
++)
3452 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
3453 '\0' == predefs
[i
].name
[(int)len
])
3454 return predefs
[i
].str
;
3460 * Check whether *name is the renamed name of a high-level macro.
3461 * Return the standard name, or NULL if it is not.
3464 roff_getrenn(const struct roff
*r
, const char *name
, size_t len
)
3466 const struct roffkv
*n
;
3468 for (n
= r
->rentab
; n
; n
= n
->next
)
3469 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3470 '\0' == n
->key
.p
[(int)len
])
3477 roff_freestr(struct roffkv
*r
)
3479 struct roffkv
*n
, *nn
;
3481 for (n
= r
; n
; n
= nn
) {
3489 /* --- accessors and utility functions ------------------------------------ */
3491 const struct tbl_span
*
3492 roff_span(const struct roff
*r
)
3495 return r
->tbl
? tbl_span(r
->tbl
) : NULL
;
3499 roff_eqn(const struct roff
*r
)
3502 return r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
;
3506 * Duplicate an input string, making the appropriate character
3507 * conversations (as stipulated by `tr') along the way.
3508 * Returns a heap-allocated string with all the replacements made.
3511 roff_strdup(const struct roff
*r
, const char *p
)
3513 const struct roffkv
*cp
;
3517 enum mandoc_esc esc
;
3519 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3520 return mandoc_strdup(p
);
3521 else if ('\0' == *p
)
3522 return mandoc_strdup("");
3525 * Step through each character looking for term matches
3526 * (remember that a `tr' can be invoked with an escape, which is
3527 * a glyph but the escape is multi-character).
3528 * We only do this if the character hash has been initialised
3529 * and the string is >0 length.
3535 while ('\0' != *p
) {
3536 assert((unsigned int)*p
< 128);
3537 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3538 sz
= r
->xtab
[(int)*p
].sz
;
3539 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3540 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3544 } else if ('\\' != *p
) {
3545 res
= mandoc_realloc(res
, ssz
+ 2);
3550 /* Search for term matches. */
3551 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3552 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3557 * A match has been found.
3558 * Append the match to the array and move
3559 * forward by its keysize.
3561 res
= mandoc_realloc(res
,
3562 ssz
+ cp
->val
.sz
+ 1);
3563 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3565 p
+= (int)cp
->key
.sz
;
3570 * Handle escapes carefully: we need to copy
3571 * over just the escape itself, or else we might
3572 * do replacements within the escape itself.
3573 * Make sure to pass along the bogus string.
3576 esc
= mandoc_escape(&p
, NULL
, NULL
);
3577 if (ESCAPE_ERROR
== esc
) {
3579 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3580 memcpy(res
+ ssz
, pp
, sz
);
3584 * We bail out on bad escapes.
3585 * No need to warn: we already did so when
3586 * roff_res() was called.
3589 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3590 memcpy(res
+ ssz
, pp
, sz
);
3594 res
[(int)ssz
] = '\0';
3599 roff_getformat(const struct roff
*r
)
3606 * Find out whether a line is a macro line or not.
3607 * If it is, adjust the current position and return one; if it isn't,
3608 * return zero and don't change the current position.
3609 * If the control character has been set with `.cc', then let that grain
3611 * This is slighly contrary to groff, where using the non-breaking
3612 * control character when `cc' has been invoked will cause the
3613 * non-breaking macro contents to be printed verbatim.
3616 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3622 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
3624 else if (r
->control
!= '\0')
3626 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3628 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3633 while (' ' == cp
[pos
] || '\t' == cp
[pos
])