]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.341 2018/08/25 16:53:39 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 * A macro processing context.
90 * More than one is needed when macro calls are nested.
99 struct mparse
*parse
; /* parse point */
100 struct roff_man
*man
; /* mdoc or man parser */
101 struct roffnode
*last
; /* leaf of stack */
102 struct mctx
*mstack
; /* stack of macro contexts */
103 int *rstack
; /* stack of inverted `ie' values */
104 struct ohash
*reqtab
; /* request lookup table */
105 struct roffreg
*regtab
; /* number registers */
106 struct roffkv
*strtab
; /* user-defined strings & macros */
107 struct roffkv
*rentab
; /* renamed strings & macros */
108 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
109 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
110 const char *current_string
; /* value of last called user macro */
111 struct tbl_node
*first_tbl
; /* first table parsed */
112 struct tbl_node
*last_tbl
; /* last table parsed */
113 struct tbl_node
*tbl
; /* current table being parsed */
114 struct eqn_node
*last_eqn
; /* equation parser */
115 struct eqn_node
*eqn
; /* active equation parser */
116 int eqn_inline
; /* current equation is inline */
117 int options
; /* parse options */
118 int mstacksz
; /* current size of mstack */
119 int mstackpos
; /* position in mstack */
120 int rstacksz
; /* current size limit of rstack */
121 int rstackpos
; /* position in rstack */
122 int format
; /* current file in mdoc or man format */
123 char control
; /* control character */
124 char escape
; /* escape character */
128 enum roff_tok tok
; /* type of node */
129 struct roffnode
*parent
; /* up one in stack */
130 int line
; /* parse line */
131 int col
; /* parse col */
132 char *name
; /* node name, e.g. macro name */
133 char *end
; /* end-rules: custom token */
134 int endspan
; /* end-rules: next-line or infty */
135 int rule
; /* current evaluation rule */
138 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
139 enum roff_tok tok, /* tok of macro */ \
140 struct buf *buf, /* input buffer */ \
141 int ln, /* parse line */ \
142 int ppos, /* original pos in buffer */ \
143 int pos, /* current pos in buffer */ \
144 int *offs /* reset offset of buffer data */
146 typedef int (*roffproc
)(ROFF_ARGS
);
149 roffproc proc
; /* process new macro */
150 roffproc text
; /* process as child text of macro */
151 roffproc sub
; /* process as child of macro */
153 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
157 const char *name
; /* predefined input name */
158 const char *str
; /* replacement symbol */
161 #define PREDEF(__name, __str) \
162 { (__name), (__str) },
164 /* --- function prototypes ------------------------------------------------ */
166 static int roffnode_cleanscope(struct roff
*);
167 static int roffnode_pop(struct roff
*);
168 static void roffnode_push(struct roff
*, enum roff_tok
,
169 const char *, int, int);
170 static void roff_addtbl(struct roff_man
*, struct tbl_node
*);
171 static int roff_als(ROFF_ARGS
);
172 static int roff_block(ROFF_ARGS
);
173 static int roff_block_text(ROFF_ARGS
);
174 static int roff_block_sub(ROFF_ARGS
);
175 static int roff_br(ROFF_ARGS
);
176 static int roff_cblock(ROFF_ARGS
);
177 static int roff_cc(ROFF_ARGS
);
178 static int roff_ccond(struct roff
*, int, int);
179 static int roff_char(ROFF_ARGS
);
180 static int roff_cond(ROFF_ARGS
);
181 static int roff_cond_text(ROFF_ARGS
);
182 static int roff_cond_sub(ROFF_ARGS
);
183 static int roff_ds(ROFF_ARGS
);
184 static int roff_ec(ROFF_ARGS
);
185 static int roff_eo(ROFF_ARGS
);
186 static int roff_eqndelim(struct roff
*, struct buf
*, int);
187 static int roff_evalcond(struct roff
*r
, int, char *, int *);
188 static int roff_evalnum(struct roff
*, int,
189 const char *, int *, int *, int);
190 static int roff_evalpar(struct roff
*, int,
191 const char *, int *, int *, int);
192 static int roff_evalstrcond(const char *, int *);
193 static void roff_free1(struct roff
*);
194 static void roff_freereg(struct roffreg
*);
195 static void roff_freestr(struct roffkv
*);
196 static size_t roff_getname(struct roff
*, char **, int, int);
197 static int roff_getnum(const char *, int *, int *, int);
198 static int roff_getop(const char *, int *, char *);
199 static int roff_getregn(struct roff
*,
200 const char *, size_t, char);
201 static int roff_getregro(const struct roff
*,
203 static const char *roff_getstrn(struct roff
*,
204 const char *, size_t, int *);
205 static int roff_hasregn(const struct roff
*,
206 const char *, size_t);
207 static int roff_insec(ROFF_ARGS
);
208 static int roff_it(ROFF_ARGS
);
209 static int roff_line_ignore(ROFF_ARGS
);
210 static void roff_man_alloc1(struct roff_man
*);
211 static void roff_man_free1(struct roff_man
*);
212 static int roff_manyarg(ROFF_ARGS
);
213 static int roff_nop(ROFF_ARGS
);
214 static int roff_nr(ROFF_ARGS
);
215 static int roff_onearg(ROFF_ARGS
);
216 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
218 static int roff_parsetext(struct roff
*, struct buf
*,
220 static int roff_renamed(ROFF_ARGS
);
221 static int roff_res(struct roff
*, struct buf
*, int, int);
222 static int roff_return(ROFF_ARGS
);
223 static int roff_rm(ROFF_ARGS
);
224 static int roff_rn(ROFF_ARGS
);
225 static int roff_rr(ROFF_ARGS
);
226 static void roff_setregn(struct roff
*, const char *,
227 size_t, int, char, int);
228 static void roff_setstr(struct roff
*,
229 const char *, const char *, int);
230 static void roff_setstrn(struct roffkv
**, const char *,
231 size_t, const char *, size_t, int);
232 static int roff_shift(ROFF_ARGS
);
233 static int roff_so(ROFF_ARGS
);
234 static int roff_tr(ROFF_ARGS
);
235 static int roff_Dd(ROFF_ARGS
);
236 static int roff_TE(ROFF_ARGS
);
237 static int roff_TS(ROFF_ARGS
);
238 static int roff_EQ(ROFF_ARGS
);
239 static int roff_EN(ROFF_ARGS
);
240 static int roff_T_(ROFF_ARGS
);
241 static int roff_unsupp(ROFF_ARGS
);
242 static int roff_userdef(ROFF_ARGS
);
244 /* --- constant data ------------------------------------------------------ */
246 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
247 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
249 const char *__roff_name
[MAN_MAX
+ 1] = {
250 "br", "ce", "ft", "ll",
251 "mc", "po", "rj", "sp",
253 "ab", "ad", "af", "aln",
254 "als", "am", "am1", "ami",
255 "ami1", "as", "as1", "asciify",
256 "backtrace", "bd", "bleedat", "blm",
257 "box", "boxa", "bp", "BP",
258 "break", "breakchar", "brnl", "brp",
260 "cf", "cflags", "ch", "char",
261 "chop", "class", "close", "CL",
262 "color", "composite", "continue", "cp",
263 "cropat", "cs", "cu", "da",
264 "dch", "Dd", "de", "de1",
265 "defcolor", "dei", "dei1", "device",
266 "devicem", "di", "do", "ds",
267 "ds1", "dwh", "dt", "ec",
268 "ecr", "ecs", "el", "em",
269 "EN", "eo", "EP", "EQ",
270 "errprint", "ev", "evc", "ex",
271 "fallback", "fam", "fc", "fchar",
272 "fcolor", "fdeferlig", "feature", "fkern",
273 "fl", "flig", "fp", "fps",
274 "fschar", "fspacewidth", "fspecial", "ftr",
275 "fzoom", "gcolor", "hc", "hcode",
276 "hidechar", "hla", "hlm", "hpf",
277 "hpfa", "hpfcode", "hw", "hy",
278 "hylang", "hylen", "hym", "hypp",
279 "hys", "ie", "if", "ig",
280 "index", "it", "itc", "IX",
281 "kern", "kernafter", "kernbefore", "kernpair",
282 "lc", "lc_ctype", "lds", "length",
283 "letadj", "lf", "lg", "lhang",
284 "linetabs", "lnr", "lnrf", "lpfx",
286 "mediasize", "minss", "mk", "mso",
287 "na", "ne", "nh", "nhychar",
288 "nm", "nn", "nop", "nr",
289 "nrf", "nroff", "ns", "nx",
290 "open", "opena", "os", "output",
291 "padj", "papersize", "pc", "pev",
292 "pi", "PI", "pl", "pm",
294 "psbb", "pshape", "pso", "ptr",
295 "pvs", "rchar", "rd", "recursionlimit",
296 "return", "rfschar", "rhang",
297 "rm", "rn", "rnn", "rr",
298 "rs", "rt", "schar", "sentchar",
299 "shc", "shift", "sizes", "so",
300 "spacewidth", "special", "spreadwarn", "ss",
301 "sty", "substring", "sv", "sy",
304 "tm", "tm1", "tmc", "tr",
305 "track", "transchar", "trf", "trimat",
306 "trin", "trnt", "troff", "TS",
307 "uf", "ul", "unformat", "unwatch",
308 "unwatchn", "vpt", "vs", "warn",
309 "warnscale", "watch", "watchlength", "watchn",
310 "wh", "while", "write", "writec",
311 "writem", "xflag", ".", NULL
,
313 "Dd", "Dt", "Os", "Sh",
314 "Ss", "Pp", "D1", "Dl",
315 "Bd", "Ed", "Bl", "El",
316 "It", "Ad", "An", "Ap",
317 "Ar", "Cd", "Cm", "Dv",
318 "Er", "Ev", "Ex", "Fa",
319 "Fd", "Fl", "Fn", "Ft",
320 "Ic", "In", "Li", "Nd",
321 "Nm", "Op", "Ot", "Pa",
322 "Rv", "St", "Va", "Vt",
323 "Xr", "%A", "%B", "%D",
324 "%I", "%J", "%N", "%O",
325 "%P", "%R", "%T", "%V",
326 "Ac", "Ao", "Aq", "At",
327 "Bc", "Bf", "Bo", "Bq",
328 "Bsx", "Bx", "Db", "Dc",
329 "Do", "Dq", "Ec", "Ef",
330 "Em", "Eo", "Fx", "Ms",
331 "No", "Ns", "Nx", "Ox",
332 "Pc", "Pf", "Po", "Pq",
333 "Qc", "Ql", "Qo", "Qq",
334 "Re", "Rs", "Sc", "So",
335 "Sq", "Sm", "Sx", "Sy",
336 "Tn", "Ux", "Xc", "Xo",
337 "Fo", "Fc", "Oo", "Oc",
338 "Bk", "Ek", "Bt", "Hf",
339 "Fr", "Ud", "Lb", "Lp",
340 "Lk", "Mt", "Brq", "Bro",
341 "Brc", "%C", "Es", "En",
342 "Dx", "%Q", "%U", "Ta",
344 "TH", "SH", "SS", "TP",
346 "LP", "PP", "P", "IP",
347 "HP", "SM", "SB", "BI",
348 "IB", "BR", "RB", "R",
349 "B", "I", "IR", "RI",
351 "RE", "RS", "DT", "UC",
355 "UE", "MT", "ME", NULL
357 const char *const *roff_name
= __roff_name
;
359 static struct roffmac roffs
[TOKEN_NONE
] = {
360 { roff_br
, NULL
, NULL
, 0 }, /* br */
361 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
362 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
363 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
364 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
365 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
366 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
367 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
368 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
369 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
370 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
371 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
372 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
373 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
374 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
375 { roff_als
, NULL
, NULL
, 0 }, /* als */
376 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
377 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
378 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
379 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
380 { roff_ds
, NULL
, NULL
, 0 }, /* as */
381 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
382 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
383 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
385 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
386 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
387 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
388 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
389 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
390 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
391 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
392 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
393 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
394 { roff_br
, NULL
, NULL
, 0 }, /* brp */
395 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
396 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
397 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
398 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
399 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
400 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
401 { roff_char
, NULL
, NULL
, 0 }, /* char */
402 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
403 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
404 { roff_insec
, NULL
, NULL
, 0 }, /* close */
405 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
406 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
407 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
409 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
412 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
413 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
415 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
416 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
417 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
419 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
420 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
421 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
424 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
425 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
426 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
427 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
428 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
429 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
430 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
431 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
432 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
433 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
434 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
435 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
436 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
437 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
438 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
441 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
444 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
445 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
446 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
453 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
454 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
457 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
458 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
475 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
476 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
477 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
478 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
479 { roff_it
, NULL
, NULL
, 0 }, /* it */
480 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
486 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
487 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
488 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
489 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
491 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
494 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
495 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
496 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
497 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
498 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
499 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
504 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
509 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
510 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
511 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
512 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
513 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
515 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
516 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
517 { roff_insec
, NULL
, NULL
, 0 }, /* open */
518 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
519 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
520 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
521 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
522 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
524 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
525 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
526 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
532 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
533 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
534 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
536 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
537 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
538 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
540 { roff_return
, NULL
, NULL
, 0 }, /* return */
541 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
542 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
543 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
544 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
545 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
546 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
549 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
550 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
552 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
554 { roff_so
, NULL
, NULL
, 0 }, /* so */
555 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
560 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
561 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
562 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
563 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
564 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
565 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
566 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
568 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
572 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
574 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
575 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
576 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
577 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
578 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
579 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
580 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
581 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
582 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
583 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
584 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
587 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
589 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
590 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
592 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
593 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
594 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
595 { roff_insec
, NULL
, NULL
, 0 }, /* write */
596 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
597 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
598 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
599 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
600 { roff_renamed
, NULL
, NULL
, 0 },
601 { roff_userdef
, NULL
, NULL
, 0 }
604 /* Array of injected predefined strings. */
605 #define PREDEFS_MAX 38
606 static const struct predef predefs
[PREDEFS_MAX
] = {
607 #include "predefs.in"
610 static int roffce_lines
; /* number of input lines to center */
611 static struct roff_node
*roffce_node
; /* active request */
612 static int roffit_lines
; /* number of lines to delay */
613 static char *roffit_macro
; /* nil-terminated macro line */
616 /* --- request table ------------------------------------------------------ */
619 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
627 htab
= mandoc_malloc(sizeof(*htab
));
628 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
630 for (tok
= mintok
; tok
< maxtok
; tok
++) {
631 if (roff_name
[tok
] == NULL
)
633 sz
= strlen(roff_name
[tok
]);
634 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
636 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
637 slot
= ohash_qlookup(htab
, req
->name
);
638 ohash_insert(htab
, slot
, req
);
644 roffhash_free(struct ohash
*htab
)
651 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
652 req
= ohash_next(htab
, &slot
))
659 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
666 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
668 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
669 return req
== NULL
? TOKEN_NONE
: req
->tok
;
672 /* --- stack of request blocks -------------------------------------------- */
675 * Pop the current node off of the stack of roff instructions currently
679 roffnode_pop(struct roff
*r
)
685 inloop
= p
->tok
== ROFF_while
;
694 * Push a roff node onto the instruction stack. This must later be
695 * removed with roffnode_pop().
698 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
703 p
= mandoc_calloc(1, sizeof(struct roffnode
));
706 p
->name
= mandoc_strdup(name
);
710 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
715 /* --- roff parser state data management ---------------------------------- */
718 roff_free1(struct roff
*r
)
720 struct tbl_node
*tbl
;
723 while (NULL
!= (tbl
= r
->first_tbl
)) {
724 r
->first_tbl
= tbl
->next
;
727 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
729 if (r
->last_eqn
!= NULL
)
730 eqn_free(r
->last_eqn
);
731 r
->last_eqn
= r
->eqn
= NULL
;
733 while (r
->mstackpos
>= 0)
744 roff_freereg(r
->regtab
);
747 roff_freestr(r
->strtab
);
748 roff_freestr(r
->rentab
);
749 roff_freestr(r
->xmbtab
);
750 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
753 for (i
= 0; i
< 128; i
++)
760 roff_reset(struct roff
*r
)
763 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
773 roff_free(struct roff
*r
)
778 for (i
= 0; i
< r
->mstacksz
; i
++)
779 free(r
->mstack
[i
].argv
);
781 roffhash_free(r
->reqtab
);
786 roff_alloc(struct mparse
*parse
, int options
)
790 r
= mandoc_calloc(1, sizeof(struct roff
));
792 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
793 r
->options
= options
;
794 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
801 /* --- syntax tree state data management ---------------------------------- */
804 roff_man_free1(struct roff_man
*man
)
807 if (man
->first
!= NULL
)
808 roff_node_delete(man
, man
->first
);
809 free(man
->meta
.msec
);
812 free(man
->meta
.arch
);
813 free(man
->meta
.title
);
814 free(man
->meta
.name
);
815 free(man
->meta
.date
);
819 roff_man_alloc1(struct roff_man
*man
)
822 memset(&man
->meta
, 0, sizeof(man
->meta
));
823 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
824 man
->first
->type
= ROFFT_ROOT
;
825 man
->last
= man
->first
;
828 man
->macroset
= MACROSET_NONE
;
829 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
830 man
->next
= ROFF_NEXT_CHILD
;
834 roff_man_reset(struct roff_man
*man
)
838 roff_man_alloc1(man
);
842 roff_man_free(struct roff_man
*man
)
850 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
851 const char *os_s
, int quick
)
853 struct roff_man
*man
;
855 man
= mandoc_calloc(1, sizeof(*man
));
860 roff_man_alloc1(man
);
865 /* --- syntax tree handling ----------------------------------------------- */
868 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
869 enum roff_type type
, int tok
)
873 n
= mandoc_calloc(1, sizeof(*n
));
878 n
->sec
= man
->lastsec
;
880 if (man
->flags
& MDOC_SYNOPSIS
)
881 n
->flags
|= NODE_SYNPRETTY
;
883 n
->flags
&= ~NODE_SYNPRETTY
;
884 if (man
->flags
& MDOC_NEWLINE
)
885 n
->flags
|= NODE_LINE
;
886 man
->flags
&= ~MDOC_NEWLINE
;
892 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
896 case ROFF_NEXT_SIBLING
:
897 if (man
->last
->next
!= NULL
) {
898 n
->next
= man
->last
->next
;
899 man
->last
->next
->prev
= n
;
901 man
->last
->parent
->last
= n
;
904 n
->parent
= man
->last
->parent
;
906 case ROFF_NEXT_CHILD
:
907 if (man
->last
->child
!= NULL
) {
908 n
->next
= man
->last
->child
;
909 man
->last
->child
->prev
= n
;
912 man
->last
->child
= n
;
913 n
->parent
= man
->last
;
925 if (n
->end
!= ENDBODY_NOT
)
937 * Copy over the normalised-data pointer of our parent. Not
938 * everybody has one, but copying a null pointer is fine.
941 n
->norm
= n
->parent
->norm
;
942 assert(n
->parent
->type
== ROFFT_BLOCK
);
946 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
950 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
951 n
->string
= roff_strdup(man
->roff
, word
);
952 roff_node_append(man
, n
);
953 n
->flags
|= NODE_VALID
| NODE_ENDED
;
954 man
->next
= ROFF_NEXT_SIBLING
;
958 roff_word_append(struct roff_man
*man
, const char *word
)
961 char *addstr
, *newstr
;
964 addstr
= roff_strdup(man
->roff
, word
);
965 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
969 man
->next
= ROFF_NEXT_SIBLING
;
973 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
977 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
978 roff_node_append(man
, n
);
979 man
->next
= ROFF_NEXT_CHILD
;
983 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
987 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
988 roff_node_append(man
, n
);
989 man
->next
= ROFF_NEXT_CHILD
;
994 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
998 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
999 roff_node_append(man
, n
);
1000 man
->next
= ROFF_NEXT_CHILD
;
1005 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1007 struct roff_node
*n
;
1009 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1010 roff_node_append(man
, n
);
1011 man
->next
= ROFF_NEXT_CHILD
;
1016 roff_addtbl(struct roff_man
*man
, struct tbl_node
*tbl
)
1018 struct roff_node
*n
;
1019 const struct tbl_span
*span
;
1021 if (man
->macroset
== MACROSET_MAN
)
1022 man_breakscope(man
, ROFF_TS
);
1023 while ((span
= tbl_span(tbl
)) != NULL
) {
1024 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1026 roff_node_append(man
, n
);
1027 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1028 man
->next
= ROFF_NEXT_SIBLING
;
1033 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1036 /* Adjust siblings. */
1039 n
->prev
->next
= n
->next
;
1041 n
->next
->prev
= n
->prev
;
1043 /* Adjust parent. */
1045 if (n
->parent
!= NULL
) {
1046 if (n
->parent
->child
== n
)
1047 n
->parent
->child
= n
->next
;
1048 if (n
->parent
->last
== n
)
1049 n
->parent
->last
= n
->prev
;
1052 /* Adjust parse point. */
1056 if (man
->last
== n
) {
1057 if (n
->prev
== NULL
) {
1058 man
->last
= n
->parent
;
1059 man
->next
= ROFF_NEXT_CHILD
;
1061 man
->last
= n
->prev
;
1062 man
->next
= ROFF_NEXT_SIBLING
;
1065 if (man
->first
== n
)
1070 roff_node_free(struct roff_node
*n
)
1073 if (n
->args
!= NULL
)
1074 mdoc_argv_free(n
->args
);
1075 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1078 eqn_box_free(n
->eqn
);
1084 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1087 while (n
->child
!= NULL
)
1088 roff_node_delete(man
, n
->child
);
1089 roff_node_unlink(man
, n
);
1094 deroff(char **dest
, const struct roff_node
*n
)
1099 if (n
->type
!= ROFFT_TEXT
) {
1100 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1105 /* Skip leading whitespace. */
1107 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1108 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1109 strchr(" %&0^|~", cp
[1]) != NULL
)
1111 else if ( ! isspace((unsigned char)*cp
))
1115 /* Skip trailing backslash. */
1118 if (sz
> 0 && cp
[sz
- 1] == '\\')
1121 /* Skip trailing whitespace. */
1124 if ( ! isspace((unsigned char)cp
[sz
-1]))
1127 /* Skip empty strings. */
1132 if (*dest
== NULL
) {
1133 *dest
= mandoc_strndup(cp
, sz
);
1137 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1142 /* --- main functions of the roff parser ---------------------------------- */
1145 * In the current line, expand escape sequences that tend to get
1146 * used in numerical expressions and conditional requests.
1147 * Also check the syntax of the remaining escape sequences.
1150 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1152 struct mctx
*ctx
; /* current macro call context */
1153 char ubuf
[24]; /* buffer to print the number */
1154 struct roff_node
*n
; /* used for header comments */
1155 const char *start
; /* start of the string to process */
1156 char *stesc
; /* start of an escape sequence ('\\') */
1157 char *ep
; /* end of comment string */
1158 const char *stnam
; /* start of the name, after "[(*" */
1159 const char *cp
; /* end of the name, e.g. before ']' */
1160 const char *res
; /* the string to be substituted */
1161 char *nbuf
; /* new buffer to copy buf->buf to */
1162 size_t maxl
; /* expected length of the escape name */
1163 size_t naml
; /* actual length of the escape name */
1164 size_t asz
; /* length of the replacement */
1165 size_t rsz
; /* length of the rest of the string */
1166 enum mandoc_esc esc
; /* type of the escape sequence */
1167 int inaml
; /* length returned from mandoc_escape() */
1168 int expand_count
; /* to avoid infinite loops */
1169 int npos
; /* position in numeric expression */
1170 int arg_complete
; /* argument not interrupted by eol */
1171 int quote_args
; /* true for \\$@, false for \\$* */
1172 int done
; /* no more input available */
1173 int deftype
; /* type of definition to paste */
1174 int rcsid
; /* kind of RCS id seen */
1175 char sign
; /* increment number register */
1176 char term
; /* character terminating the escape */
1178 /* Search forward for comments. */
1181 start
= buf
->buf
+ pos
;
1182 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1183 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1186 if (*stesc
!= '"' && *stesc
!= '#')
1189 /* Comment found, look for RCS id. */
1192 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1193 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1195 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1196 rcsid
= 1 << MANDOC_OS_NETBSD
;
1200 isalnum((unsigned char)*cp
) == 0 &&
1201 strchr(cp
, '$') != NULL
) {
1202 if (r
->man
->meta
.rcsids
& rcsid
)
1203 mandoc_msg(MANDOCERR_RCS_REP
, r
->parse
,
1204 ln
, stesc
+ 1 - buf
->buf
, stesc
+ 1);
1205 r
->man
->meta
.rcsids
|= rcsid
;
1208 /* Handle trailing whitespace. */
1210 ep
= strchr(stesc
--, '\0') - 1;
1215 if (*ep
== ' ' || *ep
== '\t')
1216 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1217 ln
, ep
- buf
->buf
, NULL
);
1220 * Save comments preceding the title macro
1221 * in the syntax tree.
1224 if (r
->format
== 0) {
1225 while (*ep
== ' ' || *ep
== '\t')
1228 n
= roff_node_alloc(r
->man
,
1229 ln
, stesc
+ 1 - buf
->buf
,
1230 ROFFT_COMMENT
, TOKEN_NONE
);
1231 n
->string
= mandoc_strdup(stesc
+ 2);
1232 roff_node_append(r
->man
, n
);
1233 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1234 r
->man
->next
= ROFF_NEXT_SIBLING
;
1237 /* Line continuation with comment. */
1239 if (stesc
[1] == '#') {
1241 return ROFF_IGN
| ROFF_APPEND
;
1244 /* Discard normal comments. */
1246 while (stesc
> start
&& stesc
[-1] == ' ' &&
1247 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1256 /* Notice the end of the input. */
1258 if (*stesc
== '\n') {
1264 while (stesc
>= start
) {
1266 /* Search backwards for the next backslash. */
1268 if (*stesc
!= r
->escape
) {
1269 if (*stesc
== '\\') {
1271 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1272 buf
->buf
, stesc
+ 1) + 1;
1274 stesc
= nbuf
+ (stesc
- buf
->buf
);
1282 /* If it is escaped, skip it. */
1284 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1285 if (*cp
!= r
->escape
)
1288 if ((stesc
- cp
) % 2 == 0) {
1292 } else if (stesc
[1] != '\0') {
1299 return ROFF_IGN
| ROFF_APPEND
;
1302 /* Decide whether to expand or to check only. */
1317 if (sign
== '+' || sign
== '-')
1322 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1323 if (esc
== ESCAPE_ERROR
||
1324 (esc
== ESCAPE_SPECIAL
&&
1325 mchars_spec2cp(stnam
, inaml
) < 0))
1326 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1327 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1328 "%.*s", (int)(cp
- stesc
), stesc
);
1333 if (EXPAND_LIMIT
< ++expand_count
) {
1334 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1335 ln
, (int)(stesc
- buf
->buf
), NULL
);
1340 * The third character decides the length
1341 * of the name of the string or register.
1342 * Save a pointer to the name.
1369 /* Advance to the end of the name. */
1373 while (maxl
== 0 || naml
< maxl
) {
1375 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1376 ln
, (int)(stesc
- buf
->buf
), stesc
);
1380 if (maxl
== 0 && *cp
== term
) {
1384 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1388 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1389 case ESCAPE_SPECIAL
:
1390 case ESCAPE_UNICODE
:
1391 case ESCAPE_NUMBERED
:
1392 case ESCAPE_OVERSTRIKE
:
1401 * Retrieve the replacement string; if it is
1402 * undefined, resume searching for escapes.
1408 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1409 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1412 * If not overriden, let \*(.T
1413 * through to the formatters.
1416 if (res
== NULL
&& naml
== 2 &&
1417 stnam
[0] == '.' && stnam
[1] == 'T') {
1418 roff_setstrn(&r
->strtab
,
1419 ".T", 2, NULL
, 0, 0);
1426 if (r
->mstackpos
< 0) {
1427 mandoc_vmsg(MANDOCERR_ARG_UNDEF
,
1428 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1432 ctx
= r
->mstack
+ r
->mstackpos
;
1433 npos
= stesc
[2] - '1';
1434 if (npos
>= 0 && npos
<= 8) {
1435 res
= npos
< ctx
->argc
?
1436 ctx
->argv
[npos
] : "";
1439 if (stesc
[2] == '*')
1441 else if (stesc
[2] == '@')
1444 mandoc_vmsg(MANDOCERR_ARG_NONUM
,
1445 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1450 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1454 asz
+= 2; /* quotes */
1455 asz
+= strlen(ctx
->argv
[npos
]);
1458 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1460 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1462 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1464 stesc
= nbuf
+ (stesc
- buf
->buf
);
1467 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1469 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1474 cp
= ctx
->argv
[npos
];
1483 ubuf
[0] = arg_complete
&&
1484 roff_evalnum(r
, ln
, stnam
, &npos
,
1485 NULL
, ROFFNUM_SCALE
) &&
1486 stnam
+ npos
+ 1 == cp
? '1' : '0';
1491 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1492 roff_getregn(r
, stnam
, naml
, sign
));
1497 /* use even incomplete args */
1498 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1504 if (stesc
[1] == '*')
1505 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1506 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1507 "%.*s", (int)naml
, stnam
);
1509 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1510 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1511 ln
, (int)(stesc
- buf
->buf
), NULL
);
1515 /* Replace the escape sequence by the string. */
1518 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1519 buf
->buf
, res
, cp
) + 1;
1521 /* Prepare for the next replacement. */
1524 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1532 * Process text streams.
1535 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1541 enum mandoc_esc esc
;
1543 /* Spring the input line trap. */
1545 if (roffit_lines
== 1) {
1546 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1553 return ROFF_REPARSE
;
1554 } else if (roffit_lines
> 1)
1557 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1558 if (roffce_lines
< 1) {
1559 r
->man
->last
= roffce_node
;
1560 r
->man
->next
= ROFF_NEXT_SIBLING
;
1567 /* Convert all breakable hyphens into ASCII_HYPH. */
1569 start
= p
= buf
->buf
+ pos
;
1571 while (*p
!= '\0') {
1572 sz
= strcspn(p
, "-\\");
1579 /* Skip over escapes. */
1581 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1582 if (esc
== ESCAPE_ERROR
)
1587 } else if (p
== start
) {
1592 if (isalpha((unsigned char)p
[-1]) &&
1593 isalpha((unsigned char)p
[1]))
1601 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1605 int pos
; /* parse point */
1606 int spos
; /* saved parse point for messages */
1607 int ppos
; /* original offset in buf->buf */
1608 int ctl
; /* macro line (boolean) */
1612 /* Handle in-line equation delimiters. */
1614 if (r
->tbl
== NULL
&&
1615 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1616 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1617 e
= roff_eqndelim(r
, buf
, pos
);
1618 if (e
== ROFF_REPARSE
)
1620 assert(e
== ROFF_CONT
);
1623 /* Expand some escape sequences. */
1625 e
= roff_res(r
, buf
, ln
, pos
);
1626 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1628 assert(e
== ROFF_CONT
);
1630 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1633 * First, if a scope is open and we're not a macro, pass the
1634 * text through the macro's filter.
1635 * Equations process all content themselves.
1636 * Tables process almost all content themselves, but we want
1637 * to warn about macros before passing it there.
1640 if (r
->last
!= NULL
&& ! ctl
) {
1642 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1643 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1648 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1649 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1652 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1653 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1654 roff_addtbl(r
->man
, r
->tbl
);
1658 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1660 /* Skip empty request lines. */
1662 if (buf
->buf
[pos
] == '"') {
1663 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1666 } else if (buf
->buf
[pos
] == '\0')
1670 * If a scope is open, go to the child handler for that macro,
1671 * as it may want to preprocess before doing anything with it.
1672 * Don't do so if an equation is open.
1677 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1680 /* No scope is open. This is a new request or macro. */
1683 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1685 /* Tables ignore most macros. */
1687 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1688 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1689 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1690 ln
, pos
, buf
->buf
+ spos
);
1691 if (t
!= TOKEN_NONE
)
1693 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1695 while (buf
->buf
[pos
] == ' ')
1697 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1698 roff_addtbl(r
->man
, r
->tbl
);
1702 /* For now, let high level macros abort .ce mode. */
1704 if (ctl
&& roffce_node
!= NULL
&&
1705 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1706 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1707 r
->man
->last
= roffce_node
;
1708 r
->man
->next
= ROFF_NEXT_SIBLING
;
1714 * This is neither a roff request nor a user-defined macro.
1715 * Let the standard macro set parsers handle it.
1718 if (t
== TOKEN_NONE
)
1721 /* Execute a roff request or a user defined macro. */
1723 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1727 * Internal interface function to tell the roff parser that execution
1728 * of the current macro ended. This is required because macro
1729 * definitions usually do not end with a .return request.
1732 roff_userret(struct roff
*r
)
1737 assert(r
->mstackpos
>= 0);
1738 ctx
= r
->mstack
+ r
->mstackpos
;
1739 for (i
= 0; i
< ctx
->argc
; i
++)
1746 roff_endparse(struct roff
*r
)
1748 if (r
->last
!= NULL
)
1749 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1750 r
->last
->line
, r
->last
->col
,
1751 roff_name
[r
->last
->tok
]);
1753 if (r
->eqn
!= NULL
) {
1754 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1755 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1760 if (r
->tbl
!= NULL
) {
1761 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1762 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1769 * Parse a roff node's type from the input buffer. This must be in the
1770 * form of ".foo xxx" in the usual way.
1772 static enum roff_tok
1773 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1783 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1787 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1789 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1790 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1799 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1802 if (t
!= TOKEN_NONE
)
1804 else if (deftype
== ROFFDEF_UNDEF
) {
1805 /* Using an undefined macro defines it to be empty. */
1806 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1807 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1812 /* --- handling of request blocks ----------------------------------------- */
1815 roff_cblock(ROFF_ARGS
)
1819 * A block-close `..' should only be invoked as a child of an
1820 * ignore macro, otherwise raise a warning and just ignore it.
1823 if (r
->last
== NULL
) {
1824 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1829 switch (r
->last
->tok
) {
1831 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1834 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1839 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1844 if (buf
->buf
[pos
] != '\0')
1845 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1846 ".. %s", buf
->buf
+ pos
);
1849 roffnode_cleanscope(r
);
1855 roffnode_cleanscope(struct roff
*r
)
1860 while (r
->last
!= NULL
) {
1861 if (--r
->last
->endspan
!= 0)
1863 inloop
+= roffnode_pop(r
);
1869 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1871 if (NULL
== r
->last
) {
1872 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1877 switch (r
->last
->tok
) {
1884 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1889 if (r
->last
->endspan
> -1) {
1890 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1895 return roffnode_pop(r
) + roffnode_cleanscope(r
);
1899 roff_block(ROFF_ARGS
)
1901 const char *name
, *value
;
1902 char *call
, *cp
, *iname
, *rname
;
1903 size_t csz
, namesz
, rsz
;
1906 /* Ignore groff compatibility mode for now. */
1908 if (tok
== ROFF_de1
)
1910 else if (tok
== ROFF_dei1
)
1912 else if (tok
== ROFF_am1
)
1914 else if (tok
== ROFF_ami1
)
1917 /* Parse the macro name argument. */
1919 cp
= buf
->buf
+ pos
;
1920 if (tok
== ROFF_ig
) {
1925 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1926 iname
[namesz
] = '\0';
1929 /* Resolve the macro name argument if it is indirect. */
1931 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1932 deftype
= ROFFDEF_USER
;
1933 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1935 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1936 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1937 "%.*s", (int)namesz
, iname
);
1940 namesz
= strlen(name
);
1944 if (namesz
== 0 && tok
!= ROFF_ig
) {
1945 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1946 ln
, ppos
, roff_name
[tok
]);
1950 roffnode_push(r
, tok
, name
, ln
, ppos
);
1953 * At the beginning of a `de' macro, clear the existing string
1954 * with the same name, if there is one. New content will be
1955 * appended from roff_block_text() in multiline mode.
1958 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
1959 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1960 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1961 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
1962 deftype
= ROFFDEF_ANY
;
1963 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1964 switch (deftype
) { /* Before appending, ... */
1965 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
1966 roff_setstrn(&r
->strtab
, name
, namesz
,
1967 value
, strlen(value
), 0);
1969 case ROFFDEF_REN
: /* call original standard macro. */
1970 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1971 (int)strlen(value
), value
);
1972 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1973 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1976 case ROFFDEF_STD
: /* rename and call standard macro. */
1977 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
1978 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
1979 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1981 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1993 /* Get the custom end marker. */
1996 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1998 /* Resolve the end marker if it is indirect. */
2000 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2001 deftype
= ROFFDEF_USER
;
2002 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2004 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
2005 r
->parse
, ln
, (int)(iname
- buf
->buf
),
2006 "%.*s", (int)namesz
, iname
);
2009 namesz
= strlen(name
);
2014 r
->last
->end
= mandoc_strndup(name
, namesz
);
2017 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2018 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2024 roff_block_sub(ROFF_ARGS
)
2030 * First check whether a custom macro exists at this level. If
2031 * it does, then check against it. This is some of groff's
2032 * stranger behaviours. If we encountered a custom end-scope
2033 * tag and that tag also happens to be a "real" macro, then we
2034 * need to try interpreting it again as a real macro. If it's
2035 * not, then return ignore. Else continue.
2039 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2040 if (buf
->buf
[i
] != r
->last
->end
[j
])
2043 if (r
->last
->end
[j
] == '\0' &&
2044 (buf
->buf
[i
] == '\0' ||
2045 buf
->buf
[i
] == ' ' ||
2046 buf
->buf
[i
] == '\t')) {
2048 roffnode_cleanscope(r
);
2050 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2054 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2062 * If we have no custom end-query or lookup failed, then try
2063 * pulling it out of the hashtable.
2066 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2068 if (t
!= ROFF_cblock
) {
2070 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2074 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2078 roff_block_text(ROFF_ARGS
)
2082 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2088 roff_cond_sub(ROFF_ARGS
)
2091 int endloop
, irc
, rr
;
2096 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2097 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2098 if (roffnode_cleanscope(r
))
2102 * If `\}' occurs on a macro line without a preceding macro,
2103 * drop the line completely.
2106 ep
= buf
->buf
+ pos
;
2107 if (ep
[0] == '\\' && ep
[1] == '}')
2110 /* Always check for the closing delimiter `\}'. */
2112 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2115 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2116 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2129 * Fully handle known macros when they are structurally
2130 * required or when the conditional evaluated to true.
2133 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2134 irc
|= t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
) ?
2135 (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) :
2136 rr
? ROFF_CONT
: ROFF_IGN
;
2141 roff_cond_text(ROFF_ARGS
)
2144 int endloop
, irc
, rr
;
2148 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2149 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2150 if (roffnode_cleanscope(r
))
2153 ep
= buf
->buf
+ pos
;
2154 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2155 if (*(++ep
) == '}') {
2157 if (roff_ccond(r
, ln
, ep
- buf
->buf
- 1))
2168 /* --- handling of numeric and conditional expressions -------------------- */
2171 * Parse a single signed integer number. Stop at the first non-digit.
2172 * If there is at least one digit, return success and advance the
2173 * parse point, else return failure and let the parse point unchanged.
2174 * Ignore overflows, treat them just like the C language.
2177 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2179 int myres
, scaled
, n
, p
;
2186 if (n
|| v
[p
] == '+')
2189 if (flags
& ROFFNUM_WHITE
)
2190 while (isspace((unsigned char)v
[p
]))
2193 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2194 *res
= 10 * *res
+ v
[p
] - '0';
2201 /* Each number may be followed by one optional scaling unit. */
2205 scaled
= *res
* 65536;
2208 scaled
= *res
* 240;
2211 scaled
= *res
* 240 / 2.54;
2222 scaled
= *res
* 10 / 3;
2228 scaled
= *res
* 6 / 25;
2235 if (flags
& ROFFNUM_SCALE
)
2243 * Evaluate a string comparison condition.
2244 * The first character is the delimiter.
2245 * Succeed if the string up to its second occurrence
2246 * matches the string up to its third occurence.
2247 * Advance the cursor after the third occurrence
2248 * or lacking that, to the end of the line.
2251 roff_evalstrcond(const char *v
, int *pos
)
2253 const char *s1
, *s2
, *s3
;
2257 s1
= v
+ *pos
; /* initial delimiter */
2258 s2
= s1
+ 1; /* for scanning the first string */
2259 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2261 if (NULL
== s3
) /* found no middle delimiter */
2264 while ('\0' != *++s3
) {
2265 if (*s2
!= *s3
) { /* mismatch */
2266 s3
= strchr(s3
, *s1
);
2269 if (*s3
== *s1
) { /* found the final delimiter */
2278 s3
= strchr(s2
, '\0');
2279 else if (*s3
!= '\0')
2286 * Evaluate an optionally negated single character, numerical,
2287 * or string condition.
2290 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2292 const char *start
, *end
;
2295 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2297 if ('!' == v
[*pos
]) {
2318 } while (v
[*pos
] == ' ');
2321 * Quirk for groff compatibility:
2322 * The horizontal tab is neither available nor unavailable.
2325 if (v
[*pos
] == '\t') {
2330 /* Printable ASCII characters are available. */
2332 if (v
[*pos
] != '\\') {
2338 switch (mandoc_escape(&end
, &start
, &len
)) {
2339 case ESCAPE_SPECIAL
:
2340 istrue
= mchars_spec2cp(start
, len
) != -1;
2342 case ESCAPE_UNICODE
:
2345 case ESCAPE_NUMBERED
:
2346 istrue
= mchars_num2char(start
, len
) != -1;
2353 return istrue
== wanttrue
;
2360 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2363 else if (v
[*pos
] == 'r')
2364 istrue
= roff_hasregn(r
, name
, sz
);
2366 deftype
= ROFFDEF_ANY
;
2367 roff_getstrn(r
, name
, sz
, &deftype
);
2371 return istrue
== wanttrue
;
2377 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2378 return (number
> 0) == wanttrue
;
2379 else if (*pos
== savepos
)
2380 return roff_evalstrcond(v
, pos
) == wanttrue
;
2386 roff_line_ignore(ROFF_ARGS
)
2393 roff_insec(ROFF_ARGS
)
2396 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2397 ln
, ppos
, roff_name
[tok
]);
2402 roff_unsupp(ROFF_ARGS
)
2405 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2406 ln
, ppos
, roff_name
[tok
]);
2411 roff_cond(ROFF_ARGS
)
2415 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2418 * An `.el' has no conditional body: it will consume the value
2419 * of the current rstack entry set in prior `ie' calls or
2422 * If we're not an `el', however, then evaluate the conditional.
2425 r
->last
->rule
= tok
== ROFF_el
?
2426 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2427 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2430 * An if-else will put the NEGATION of the current evaluated
2431 * conditional into the stack of rules.
2434 if (tok
== ROFF_ie
) {
2435 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2437 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2438 r
->rstacksz
, sizeof(int));
2440 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2443 /* If the parent has false as its rule, then so do we. */
2445 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2450 * If there is nothing on the line after the conditional,
2451 * not even whitespace, use next-line scope.
2452 * Except that .while does not support next-line scope.
2455 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2456 r
->last
->endspan
= 2;
2460 while (buf
->buf
[pos
] == ' ')
2463 /* An opening brace requests multiline scope. */
2465 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2466 r
->last
->endspan
= -1;
2468 while (buf
->buf
[pos
] == ' ')
2474 * Anything else following the conditional causes
2475 * single-line scope. Warn if the scope contains
2476 * nothing but trailing whitespace.
2479 if (buf
->buf
[pos
] == '\0')
2480 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2481 ln
, ppos
, roff_name
[tok
]);
2483 r
->last
->endspan
= 1;
2488 if (tok
== ROFF_while
)
2500 /* Ignore groff compatibility mode for now. */
2502 if (tok
== ROFF_ds1
)
2504 else if (tok
== ROFF_as1
)
2508 * The first word is the name of the string.
2509 * If it is empty or terminated by an escape sequence,
2510 * abort the `ds' request without defining anything.
2513 name
= string
= buf
->buf
+ pos
;
2517 namesz
= roff_getname(r
, &string
, ln
, pos
);
2518 if (name
[namesz
] == '\\')
2521 /* Read past the initial double-quote, if any. */
2525 /* The rest is the value. */
2526 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2528 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2533 * Parse a single operator, one or two characters long.
2534 * If the operator is recognized, return success and advance the
2535 * parse point, else return failure and let the parse point unchanged.
2538 roff_getop(const char *v
, int *pos
, char *res
)
2553 switch (v
[*pos
+ 1]) {
2571 switch (v
[*pos
+ 1]) {
2585 if ('=' == v
[*pos
+ 1])
2597 * Evaluate either a parenthesized numeric expression
2598 * or a single signed integer number.
2601 roff_evalpar(struct roff
*r
, int ln
,
2602 const char *v
, int *pos
, int *res
, int flags
)
2606 return roff_getnum(v
, pos
, res
, flags
);
2609 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2613 * Omission of the closing parenthesis
2614 * is an error in validation mode,
2615 * but ignored in evaluation mode.
2620 else if (NULL
== res
)
2627 * Evaluate a complete numeric expression.
2628 * Proceed left to right, there is no concept of precedence.
2631 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2632 int *pos
, int *res
, int flags
)
2634 int mypos
, operand2
;
2642 if (flags
& ROFFNUM_WHITE
)
2643 while (isspace((unsigned char)v
[*pos
]))
2646 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2650 if (flags
& ROFFNUM_WHITE
)
2651 while (isspace((unsigned char)v
[*pos
]))
2654 if ( ! roff_getop(v
, pos
, &operator))
2657 if (flags
& ROFFNUM_WHITE
)
2658 while (isspace((unsigned char)v
[*pos
]))
2661 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2664 if (flags
& ROFFNUM_WHITE
)
2665 while (isspace((unsigned char)v
[*pos
]))
2682 if (operand2
== 0) {
2683 mandoc_msg(MANDOCERR_DIVZERO
,
2684 r
->parse
, ln
, *pos
, v
);
2691 if (operand2
== 0) {
2692 mandoc_msg(MANDOCERR_DIVZERO
,
2693 r
->parse
, ln
, *pos
, v
);
2700 *res
= *res
< operand2
;
2703 *res
= *res
> operand2
;
2706 *res
= *res
<= operand2
;
2709 *res
= *res
>= operand2
;
2712 *res
= *res
== operand2
;
2715 *res
= *res
!= operand2
;
2718 *res
= *res
&& operand2
;
2721 *res
= *res
|| operand2
;
2724 if (operand2
< *res
)
2728 if (operand2
> *res
)
2738 /* --- register management ------------------------------------------------ */
2741 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2743 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2747 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2748 int val
, char sign
, int step
)
2750 struct roffreg
*reg
;
2752 /* Search for an existing register with the same name. */
2755 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2756 strncmp(reg
->key
.p
, name
, len
) != 0))
2760 /* Create a new register. */
2761 reg
= mandoc_malloc(sizeof(struct roffreg
));
2762 reg
->key
.p
= mandoc_strndup(name
, len
);
2766 reg
->next
= r
->regtab
;
2772 else if ('-' == sign
)
2776 if (step
!= INT_MIN
)
2781 * Handle some predefined read-only number registers.
2782 * For now, return -1 if the requested register is not predefined;
2783 * in case a predefined read-only register having the value -1
2784 * were to turn up, another special value would have to be chosen.
2787 roff_getregro(const struct roff
*r
, const char *name
)
2791 case '$': /* Number of arguments of the last macro evaluated. */
2792 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
2793 case 'A': /* ASCII approximation mode is always off. */
2795 case 'g': /* Groff compatibility mode is always on. */
2797 case 'H': /* Fixed horizontal resolution. */
2799 case 'j': /* Always adjust left margin only. */
2801 case 'T': /* Some output device is always defined. */
2803 case 'V': /* Fixed vertical resolution. */
2811 roff_getreg(struct roff
*r
, const char *name
)
2813 return roff_getregn(r
, name
, strlen(name
), '\0');
2817 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2819 struct roffreg
*reg
;
2822 if ('.' == name
[0] && 2 == len
) {
2823 val
= roff_getregro(r
, name
+ 1);
2828 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2829 if (len
== reg
->key
.sz
&&
2830 0 == strncmp(name
, reg
->key
.p
, len
)) {
2833 reg
->val
+= reg
->step
;
2836 reg
->val
-= reg
->step
;
2845 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
2850 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2852 struct roffreg
*reg
;
2855 if ('.' == name
[0] && 2 == len
) {
2856 val
= roff_getregro(r
, name
+ 1);
2861 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2862 if (len
== reg
->key
.sz
&&
2863 0 == strncmp(name
, reg
->key
.p
, len
))
2870 roff_freereg(struct roffreg
*reg
)
2872 struct roffreg
*old_reg
;
2874 while (NULL
!= reg
) {
2885 char *key
, *val
, *step
;
2890 key
= val
= buf
->buf
+ pos
;
2894 keysz
= roff_getname(r
, &val
, ln
, pos
);
2895 if (key
[keysz
] == '\\')
2899 if (sign
== '+' || sign
== '-')
2903 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
2907 while (isspace((unsigned char)*step
))
2909 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
2912 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
2919 struct roffreg
*reg
, **prev
;
2923 name
= cp
= buf
->buf
+ pos
;
2926 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2927 name
[namesz
] = '\0';
2932 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2944 /* --- handler functions for roff requests -------------------------------- */
2953 cp
= buf
->buf
+ pos
;
2954 while (*cp
!= '\0') {
2956 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2957 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2958 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2959 if (name
[namesz
] == '\\')
2970 /* Parse the number of lines. */
2972 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2973 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2974 ln
, ppos
, buf
->buf
+ 1);
2978 while (isspace((unsigned char)buf
->buf
[pos
]))
2982 * Arm the input line trap.
2983 * Special-casing "an-trap" is an ugly workaround to cope
2984 * with DocBook stupidly fiddling with man(7) internals.
2988 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2989 strcmp(buf
->buf
+ pos
, "an-trap") ?
2990 buf
->buf
+ pos
: "br");
2998 enum roff_tok t
, te
;
3005 r
->format
= MPARSE_MDOC
;
3006 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3012 r
->format
= MPARSE_MAN
;
3013 mask
= MPARSE_QUICK
;
3018 if ((r
->options
& mask
) == 0)
3019 for (t
= tok
; t
< te
; t
++)
3020 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3027 if (r
->tbl
== NULL
) {
3028 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3032 if (tbl_end(r
->tbl
) == 0) {
3035 buf
->buf
= mandoc_strdup(".sp");
3038 return ROFF_REPARSE
;
3049 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3052 tbl_restart(ln
, ppos
, r
->tbl
);
3058 * Handle in-line equation delimiters.
3061 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3064 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3067 * Outside equations, look for an opening delimiter.
3068 * If we are inside an equation, we already know it is
3069 * in-line, or this function wouldn't have been called;
3070 * so look for a closing delimiter.
3073 cp1
= buf
->buf
+ pos
;
3074 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3075 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3080 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3082 /* Handle preceding text, protecting whitespace. */
3084 if (*buf
->buf
!= '\0') {
3091 * Prepare replacing the delimiter with an equation macro
3092 * and drop leading white space from the equation.
3095 if (r
->eqn
== NULL
) {
3102 /* Handle following text, protecting whitespace. */
3110 /* Do the actual replacement. */
3112 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3113 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3117 /* Toggle the in-line state of the eqn subsystem. */
3119 r
->eqn_inline
= r
->eqn
== NULL
;
3120 return ROFF_REPARSE
;
3126 struct roff_node
*n
;
3128 if (r
->man
->macroset
== MACROSET_MAN
)
3129 man_breakscope(r
->man
, ROFF_EQ
);
3130 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3131 if (ln
> r
->man
->last
->line
)
3132 n
->flags
|= NODE_LINE
;
3133 n
->eqn
= mandoc_calloc(1, sizeof(*n
->eqn
));
3134 n
->eqn
->expectargs
= UINT_MAX
;
3135 roff_node_append(r
->man
, n
);
3136 r
->man
->next
= ROFF_NEXT_SIBLING
;
3138 assert(r
->eqn
== NULL
);
3139 if (r
->last_eqn
== NULL
)
3140 r
->last_eqn
= eqn_alloc(r
->parse
);
3142 eqn_reset(r
->last_eqn
);
3143 r
->eqn
= r
->last_eqn
;
3146 if (buf
->buf
[pos
] != '\0')
3147 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3148 ".EQ %s", buf
->buf
+ pos
);
3156 if (r
->eqn
!= NULL
) {
3160 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
3161 if (buf
->buf
[pos
] != '\0')
3162 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3163 "EN %s", buf
->buf
+ pos
);
3170 if (r
->tbl
!= NULL
) {
3171 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
3172 ln
, ppos
, "TS breaks TS");
3175 r
->tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
3177 r
->last_tbl
->next
= r
->tbl
;
3179 r
->first_tbl
= r
->tbl
;
3180 r
->last_tbl
= r
->tbl
;
3185 roff_onearg(ROFF_ARGS
)
3187 struct roff_node
*n
;
3191 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3192 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3194 man_breakscope(r
->man
, tok
);
3196 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3197 r
->man
->last
= roffce_node
;
3198 r
->man
->next
= ROFF_NEXT_SIBLING
;
3201 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3204 cp
= buf
->buf
+ pos
;
3206 while (*cp
!= '\0' && *cp
!= ' ')
3211 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
3212 r
->parse
, ln
, cp
- buf
->buf
,
3213 "%s ... %s", roff_name
[tok
], cp
);
3214 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3217 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3218 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3219 roff_word_alloc(r
->man
, ln
, pos
, "1");
3220 r
->man
->last
->flags
|= NODE_NOSRC
;
3223 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3224 &roffce_lines
, 0) == 0) {
3225 mandoc_vmsg(MANDOCERR_CE_NONUM
,
3226 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
3229 if (roffce_lines
< 1) {
3230 r
->man
->last
= r
->man
->last
->parent
;
3234 roffce_node
= r
->man
->last
->parent
;
3236 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3239 n
->flags
|= NODE_LINE
;
3240 r
->man
->next
= ROFF_NEXT_SIBLING
;
3245 roff_manyarg(ROFF_ARGS
)
3247 struct roff_node
*n
;
3250 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3253 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3254 while (*ep
!= '\0' && *ep
!= ' ')
3258 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3261 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3263 r
->man
->next
= ROFF_NEXT_SIBLING
;
3270 char *oldn
, *newn
, *end
, *value
;
3271 size_t oldsz
, newsz
, valsz
;
3273 newn
= oldn
= buf
->buf
+ pos
;
3277 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3278 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3282 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3286 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3288 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3289 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3297 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3298 man_breakscope(r
->man
, ROFF_br
);
3299 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3300 if (buf
->buf
[pos
] != '\0')
3301 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3302 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3303 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3304 r
->man
->next
= ROFF_NEXT_SIBLING
;
3315 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3319 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3320 ln
, p
- buf
->buf
, "cc ... %s", p
);
3326 roff_char(ROFF_ARGS
)
3328 const char *p
, *kp
, *vp
;
3332 /* Parse the character to be replaced. */
3334 kp
= buf
->buf
+ pos
;
3336 if (*kp
== '\0' || (*kp
== '\\' &&
3337 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3338 (*p
!= ' ' && *p
!= '\0')) {
3339 mandoc_vmsg(MANDOCERR_CHAR_ARG
, r
->parse
,
3340 ln
, pos
, "char %s", kp
);
3348 * If the replacement string contains a font escape sequence,
3349 * we have to restore the font at the end.
3355 while (*p
!= '\0') {
3358 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3360 case ESCAPE_FONTROMAN
:
3361 case ESCAPE_FONTITALIC
:
3362 case ESCAPE_FONTBOLD
:
3364 case ESCAPE_FONTPREV
:
3372 mandoc_msg(MANDOCERR_CHAR_FONT
, r
->parse
,
3373 ln
, vp
- buf
->buf
, vp
);
3376 * Approximate the effect of .char using the .tr tables.
3377 * XXX In groff, .char and .tr interact differently.
3381 if (r
->xtab
== NULL
)
3382 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3383 assert((unsigned int)*kp
< 128);
3384 free(r
->xtab
[(int)*kp
].p
);
3385 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3386 "%s%s", vp
, font
? "\fP" : "");
3388 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3390 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3406 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3407 ln
, p
- buf
->buf
, "ec ... %s", p
);
3416 if (buf
->buf
[pos
] != '\0')
3417 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3418 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3425 while (buf
->buf
[pos
] == ' ')
3434 const char *p
, *first
, *second
;
3436 enum mandoc_esc esc
;
3441 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3445 while (*p
!= '\0') {
3449 if (*first
== '\\') {
3450 esc
= mandoc_escape(&p
, NULL
, NULL
);
3451 if (esc
== ESCAPE_ERROR
) {
3452 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3453 ln
, (int)(p
- buf
->buf
), first
);
3456 fsz
= (size_t)(p
- first
);
3460 if (*second
== '\\') {
3461 esc
= mandoc_escape(&p
, NULL
, NULL
);
3462 if (esc
== ESCAPE_ERROR
) {
3463 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3464 ln
, (int)(p
- buf
->buf
), second
);
3467 ssz
= (size_t)(p
- second
);
3468 } else if (*second
== '\0') {
3469 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3470 ln
, first
- buf
->buf
, "tr %s", first
);
3476 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3481 if (r
->xtab
== NULL
)
3482 r
->xtab
= mandoc_calloc(128,
3483 sizeof(struct roffstr
));
3485 free(r
->xtab
[(int)*first
].p
);
3486 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3487 r
->xtab
[(int)*first
].sz
= ssz
;
3494 * Implementation of the .return request.
3495 * There is no need to call roff_userret() from here.
3496 * The read module will call that after rewinding the reader stack
3497 * to the place from where the current macro was called.
3500 roff_return(ROFF_ARGS
)
3502 if (r
->mstackpos
>= 0)
3503 return ROFF_IGN
| ROFF_USERRET
;
3505 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "return");
3513 char *oldn
, *newn
, *end
;
3514 size_t oldsz
, newsz
;
3517 oldn
= newn
= buf
->buf
+ pos
;
3521 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3522 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3526 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3530 deftype
= ROFFDEF_ANY
;
3531 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3534 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3535 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3536 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3539 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3540 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3543 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3544 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3545 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3548 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3549 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3552 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3553 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3560 roff_shift(ROFF_ARGS
)
3566 if (buf
->buf
[pos
] != '\0' &&
3567 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3568 mandoc_vmsg(MANDOCERR_CE_NONUM
, r
->parse
,
3569 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3572 if (r
->mstackpos
< 0) {
3573 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "shift");
3576 ctx
= r
->mstack
+ r
->mstackpos
;
3577 if (levels
> ctx
->argc
) {
3578 mandoc_vmsg(MANDOCERR_SHIFT
, r
->parse
,
3579 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3584 for (i
= 0; i
< levels
; i
++)
3586 ctx
->argc
-= levels
;
3587 for (i
= 0; i
< ctx
->argc
; i
++)
3588 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3597 name
= buf
->buf
+ pos
;
3598 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3601 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3602 * opening anything that's not in our cwd or anything beneath
3603 * it. Thus, explicitly disallow traversing up the file-system
3604 * or using absolute paths.
3607 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3608 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3610 buf
->sz
= mandoc_asprintf(&cp
,
3611 ".sp\nSee the file %s.\n.sp", name
) + 1;
3615 return ROFF_REPARSE
;
3622 /* --- user defined strings and macros ------------------------------------ */
3625 roff_userdef(ROFF_ARGS
)
3628 char *arg
, *ap
, *dst
, *src
;
3631 /* Initialize a new macro stack context. */
3633 if (++r
->mstackpos
== r
->mstacksz
) {
3634 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3635 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3638 ctx
= r
->mstack
+ r
->mstackpos
;
3644 * Collect pointers to macro argument strings,
3645 * NUL-terminating them and escaping quotes.
3648 src
= buf
->buf
+ pos
;
3649 while (*src
!= '\0') {
3650 if (ctx
->argc
== ctx
->argsz
) {
3652 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3653 ctx
->argsz
, sizeof(*ctx
->argv
));
3655 arg
= mandoc_getarg(r
->parse
, &src
, ln
, &pos
);
3656 sz
= 1; /* For the terminating NUL. */
3657 for (ap
= arg
; *ap
!= '\0'; ap
++)
3658 sz
+= *ap
== '"' ? 4 : 1;
3659 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3660 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3662 memcpy(dst
, "\\(dq", 4);
3670 /* Replace the macro invocation by the macro definition. */
3673 buf
->buf
= mandoc_strdup(r
->current_string
);
3674 buf
->sz
= strlen(buf
->buf
) + 1;
3677 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3678 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3682 * Calling a high-level macro that was renamed with .rn.
3683 * r->current_string has already been set up by roff_parse().
3686 roff_renamed(ROFF_ARGS
)
3690 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3691 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3699 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3708 /* Read until end of name and terminate it with NUL. */
3709 for (cp
= name
; 1; cp
++) {
3710 if ('\0' == *cp
|| ' ' == *cp
) {
3717 if ('{' == cp
[1] || '}' == cp
[1])
3722 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3723 "%.*s", (int)(cp
- name
+ 1), name
);
3724 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3728 /* Read past spaces. */
3737 * Store *string into the user-defined string called *name.
3738 * To clear an existing entry, call with (*r, *name, NULL, 0).
3739 * append == 0: replace mode
3740 * append == 1: single-line append mode
3741 * append == 2: multiline append mode, append '\n' after each call
3744 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3749 namesz
= strlen(name
);
3750 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3751 string
? strlen(string
) : 0, append
);
3752 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3756 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3757 const char *string
, size_t stringsz
, int append
)
3762 size_t oldch
, newch
;
3764 /* Search for an existing string with the same name. */
3767 while (n
&& (namesz
!= n
->key
.sz
||
3768 strncmp(n
->key
.p
, name
, namesz
)))
3772 /* Create a new string table entry. */
3773 n
= mandoc_malloc(sizeof(struct roffkv
));
3774 n
->key
.p
= mandoc_strndup(name
, namesz
);
3780 } else if (0 == append
) {
3790 * One additional byte for the '\n' in multiline mode,
3791 * and one for the terminating '\0'.
3793 newch
= stringsz
+ (1 < append
? 2u : 1u);
3795 if (NULL
== n
->val
.p
) {
3796 n
->val
.p
= mandoc_malloc(newch
);
3801 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3804 /* Skip existing content in the destination buffer. */
3805 c
= n
->val
.p
+ (int)oldch
;
3807 /* Append new content to the destination buffer. */
3809 while (i
< (int)stringsz
) {
3811 * Rudimentary roff copy mode:
3812 * Handle escaped backslashes.
3814 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3819 /* Append terminating bytes. */
3824 n
->val
.sz
= (int)(c
- n
->val
.p
);
3828 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
3831 const struct roffkv
*n
;
3836 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
3837 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3838 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3840 if (*deftype
& ROFFDEF_USER
) {
3841 *deftype
= ROFFDEF_USER
;
3848 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
3849 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3850 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3852 if (*deftype
& ROFFDEF_REN
) {
3853 *deftype
= ROFFDEF_REN
;
3860 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3861 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
3862 predefs
[i
].name
[len
] != '\0')
3864 if (*deftype
& ROFFDEF_PRE
) {
3865 *deftype
= ROFFDEF_PRE
;
3866 return predefs
[i
].str
;
3872 if (r
->man
->macroset
!= MACROSET_MAN
) {
3873 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
3874 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3875 roff_name
[tok
][len
] != '\0')
3877 if (*deftype
& ROFFDEF_STD
) {
3878 *deftype
= ROFFDEF_STD
;
3886 if (r
->man
->macroset
!= MACROSET_MDOC
) {
3887 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
3888 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3889 roff_name
[tok
][len
] != '\0')
3891 if (*deftype
& ROFFDEF_STD
) {
3892 *deftype
= ROFFDEF_STD
;
3901 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
3902 if (*deftype
& ROFFDEF_REN
) {
3904 * This might still be a request,
3905 * so do not treat it as undefined yet.
3907 *deftype
= ROFFDEF_UNDEF
;
3911 /* Using an undefined string defines it to be empty. */
3913 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
3914 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
3922 roff_freestr(struct roffkv
*r
)
3924 struct roffkv
*n
, *nn
;
3926 for (n
= r
; n
; n
= nn
) {
3934 /* --- accessors and utility functions ------------------------------------ */
3937 * Duplicate an input string, making the appropriate character
3938 * conversations (as stipulated by `tr') along the way.
3939 * Returns a heap-allocated string with all the replacements made.
3942 roff_strdup(const struct roff
*r
, const char *p
)
3944 const struct roffkv
*cp
;
3948 enum mandoc_esc esc
;
3950 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3951 return mandoc_strdup(p
);
3952 else if ('\0' == *p
)
3953 return mandoc_strdup("");
3956 * Step through each character looking for term matches
3957 * (remember that a `tr' can be invoked with an escape, which is
3958 * a glyph but the escape is multi-character).
3959 * We only do this if the character hash has been initialised
3960 * and the string is >0 length.
3966 while ('\0' != *p
) {
3967 assert((unsigned int)*p
< 128);
3968 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3969 sz
= r
->xtab
[(int)*p
].sz
;
3970 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3971 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3975 } else if ('\\' != *p
) {
3976 res
= mandoc_realloc(res
, ssz
+ 2);
3981 /* Search for term matches. */
3982 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3983 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3988 * A match has been found.
3989 * Append the match to the array and move
3990 * forward by its keysize.
3992 res
= mandoc_realloc(res
,
3993 ssz
+ cp
->val
.sz
+ 1);
3994 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3996 p
+= (int)cp
->key
.sz
;
4001 * Handle escapes carefully: we need to copy
4002 * over just the escape itself, or else we might
4003 * do replacements within the escape itself.
4004 * Make sure to pass along the bogus string.
4007 esc
= mandoc_escape(&p
, NULL
, NULL
);
4008 if (ESCAPE_ERROR
== esc
) {
4010 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4011 memcpy(res
+ ssz
, pp
, sz
);
4015 * We bail out on bad escapes.
4016 * No need to warn: we already did so when
4017 * roff_res() was called.
4020 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4021 memcpy(res
+ ssz
, pp
, sz
);
4025 res
[(int)ssz
] = '\0';
4030 roff_getformat(const struct roff
*r
)
4037 * Find out whether a line is a macro line or not.
4038 * If it is, adjust the current position and return one; if it isn't,
4039 * return zero and don't change the current position.
4040 * If the control character has been set with `.cc', then let that grain
4042 * This is slighly contrary to groff, where using the non-breaking
4043 * control character when `cc' has been invoked will cause the
4044 * non-breaking macro contents to be printed verbatim.
4047 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4053 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4055 else if (r
->control
!= '\0')
4057 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4059 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4064 while (' ' == cp
[pos
] || '\t' == cp
[pos
])