]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.340 2018/08/24 23:12:33 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_cond(ROFF_ARGS
);
180 static int roff_cond_text(ROFF_ARGS
);
181 static int roff_cond_sub(ROFF_ARGS
);
182 static int roff_ds(ROFF_ARGS
);
183 static int roff_ec(ROFF_ARGS
);
184 static int roff_eo(ROFF_ARGS
);
185 static int roff_eqndelim(struct roff
*, struct buf
*, int);
186 static int roff_evalcond(struct roff
*r
, int, char *, int *);
187 static int roff_evalnum(struct roff
*, int,
188 const char *, int *, int *, int);
189 static int roff_evalpar(struct roff
*, int,
190 const char *, int *, int *, int);
191 static int roff_evalstrcond(const char *, int *);
192 static void roff_free1(struct roff
*);
193 static void roff_freereg(struct roffreg
*);
194 static void roff_freestr(struct roffkv
*);
195 static size_t roff_getname(struct roff
*, char **, int, int);
196 static int roff_getnum(const char *, int *, int *, int);
197 static int roff_getop(const char *, int *, char *);
198 static int roff_getregn(struct roff
*,
199 const char *, size_t, char);
200 static int roff_getregro(const struct roff
*,
202 static const char *roff_getstrn(struct roff
*,
203 const char *, size_t, int *);
204 static int roff_hasregn(const struct roff
*,
205 const char *, size_t);
206 static int roff_insec(ROFF_ARGS
);
207 static int roff_it(ROFF_ARGS
);
208 static int roff_line_ignore(ROFF_ARGS
);
209 static void roff_man_alloc1(struct roff_man
*);
210 static void roff_man_free1(struct roff_man
*);
211 static int roff_manyarg(ROFF_ARGS
);
212 static int roff_nop(ROFF_ARGS
);
213 static int roff_nr(ROFF_ARGS
);
214 static int roff_onearg(ROFF_ARGS
);
215 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
217 static int roff_parsetext(struct roff
*, struct buf
*,
219 static int roff_renamed(ROFF_ARGS
);
220 static int roff_res(struct roff
*, struct buf
*, int, int);
221 static int roff_return(ROFF_ARGS
);
222 static int roff_rm(ROFF_ARGS
);
223 static int roff_rn(ROFF_ARGS
);
224 static int roff_rr(ROFF_ARGS
);
225 static void roff_setregn(struct roff
*, const char *,
226 size_t, int, char, int);
227 static void roff_setstr(struct roff
*,
228 const char *, const char *, int);
229 static void roff_setstrn(struct roffkv
**, const char *,
230 size_t, const char *, size_t, int);
231 static int roff_shift(ROFF_ARGS
);
232 static int roff_so(ROFF_ARGS
);
233 static int roff_tr(ROFF_ARGS
);
234 static int roff_Dd(ROFF_ARGS
);
235 static int roff_TE(ROFF_ARGS
);
236 static int roff_TS(ROFF_ARGS
);
237 static int roff_EQ(ROFF_ARGS
);
238 static int roff_EN(ROFF_ARGS
);
239 static int roff_T_(ROFF_ARGS
);
240 static int roff_unsupp(ROFF_ARGS
);
241 static int roff_userdef(ROFF_ARGS
);
243 /* --- constant data ------------------------------------------------------ */
245 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
246 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
248 const char *__roff_name
[MAN_MAX
+ 1] = {
249 "br", "ce", "ft", "ll",
250 "mc", "po", "rj", "sp",
252 "ab", "ad", "af", "aln",
253 "als", "am", "am1", "ami",
254 "ami1", "as", "as1", "asciify",
255 "backtrace", "bd", "bleedat", "blm",
256 "box", "boxa", "bp", "BP",
257 "break", "breakchar", "brnl", "brp",
259 "cf", "cflags", "ch", "char",
260 "chop", "class", "close", "CL",
261 "color", "composite", "continue", "cp",
262 "cropat", "cs", "cu", "da",
263 "dch", "Dd", "de", "de1",
264 "defcolor", "dei", "dei1", "device",
265 "devicem", "di", "do", "ds",
266 "ds1", "dwh", "dt", "ec",
267 "ecr", "ecs", "el", "em",
268 "EN", "eo", "EP", "EQ",
269 "errprint", "ev", "evc", "ex",
270 "fallback", "fam", "fc", "fchar",
271 "fcolor", "fdeferlig", "feature", "fkern",
272 "fl", "flig", "fp", "fps",
273 "fschar", "fspacewidth", "fspecial", "ftr",
274 "fzoom", "gcolor", "hc", "hcode",
275 "hidechar", "hla", "hlm", "hpf",
276 "hpfa", "hpfcode", "hw", "hy",
277 "hylang", "hylen", "hym", "hypp",
278 "hys", "ie", "if", "ig",
279 "index", "it", "itc", "IX",
280 "kern", "kernafter", "kernbefore", "kernpair",
281 "lc", "lc_ctype", "lds", "length",
282 "letadj", "lf", "lg", "lhang",
283 "linetabs", "lnr", "lnrf", "lpfx",
285 "mediasize", "minss", "mk", "mso",
286 "na", "ne", "nh", "nhychar",
287 "nm", "nn", "nop", "nr",
288 "nrf", "nroff", "ns", "nx",
289 "open", "opena", "os", "output",
290 "padj", "papersize", "pc", "pev",
291 "pi", "PI", "pl", "pm",
293 "psbb", "pshape", "pso", "ptr",
294 "pvs", "rchar", "rd", "recursionlimit",
295 "return", "rfschar", "rhang",
296 "rm", "rn", "rnn", "rr",
297 "rs", "rt", "schar", "sentchar",
298 "shc", "shift", "sizes", "so",
299 "spacewidth", "special", "spreadwarn", "ss",
300 "sty", "substring", "sv", "sy",
303 "tm", "tm1", "tmc", "tr",
304 "track", "transchar", "trf", "trimat",
305 "trin", "trnt", "troff", "TS",
306 "uf", "ul", "unformat", "unwatch",
307 "unwatchn", "vpt", "vs", "warn",
308 "warnscale", "watch", "watchlength", "watchn",
309 "wh", "while", "write", "writec",
310 "writem", "xflag", ".", NULL
,
312 "Dd", "Dt", "Os", "Sh",
313 "Ss", "Pp", "D1", "Dl",
314 "Bd", "Ed", "Bl", "El",
315 "It", "Ad", "An", "Ap",
316 "Ar", "Cd", "Cm", "Dv",
317 "Er", "Ev", "Ex", "Fa",
318 "Fd", "Fl", "Fn", "Ft",
319 "Ic", "In", "Li", "Nd",
320 "Nm", "Op", "Ot", "Pa",
321 "Rv", "St", "Va", "Vt",
322 "Xr", "%A", "%B", "%D",
323 "%I", "%J", "%N", "%O",
324 "%P", "%R", "%T", "%V",
325 "Ac", "Ao", "Aq", "At",
326 "Bc", "Bf", "Bo", "Bq",
327 "Bsx", "Bx", "Db", "Dc",
328 "Do", "Dq", "Ec", "Ef",
329 "Em", "Eo", "Fx", "Ms",
330 "No", "Ns", "Nx", "Ox",
331 "Pc", "Pf", "Po", "Pq",
332 "Qc", "Ql", "Qo", "Qq",
333 "Re", "Rs", "Sc", "So",
334 "Sq", "Sm", "Sx", "Sy",
335 "Tn", "Ux", "Xc", "Xo",
336 "Fo", "Fc", "Oo", "Oc",
337 "Bk", "Ek", "Bt", "Hf",
338 "Fr", "Ud", "Lb", "Lp",
339 "Lk", "Mt", "Brq", "Bro",
340 "Brc", "%C", "Es", "En",
341 "Dx", "%Q", "%U", "Ta",
343 "TH", "SH", "SS", "TP",
345 "LP", "PP", "P", "IP",
346 "HP", "SM", "SB", "BI",
347 "IB", "BR", "RB", "R",
348 "B", "I", "IR", "RI",
350 "RE", "RS", "DT", "UC",
354 "UE", "MT", "ME", NULL
356 const char *const *roff_name
= __roff_name
;
358 static struct roffmac roffs
[TOKEN_NONE
] = {
359 { roff_br
, NULL
, NULL
, 0 }, /* br */
360 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
361 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
362 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
363 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
364 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
365 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
366 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
367 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
368 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
369 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
370 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
371 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
372 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
373 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
374 { roff_als
, NULL
, NULL
, 0 }, /* als */
375 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
376 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
377 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
378 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
379 { roff_ds
, NULL
, NULL
, 0 }, /* as */
380 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
381 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
382 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
383 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
385 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
386 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
387 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
388 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
389 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
390 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
391 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
392 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
393 { roff_br
, NULL
, NULL
, 0 }, /* brp */
394 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
395 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
396 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
397 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
398 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
399 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
400 { roff_unsupp
, NULL
, NULL
, 0 }, /* char */
401 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
402 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
403 { roff_insec
, NULL
, NULL
, 0 }, /* close */
404 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
405 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
406 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
407 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
408 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
409 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
412 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
413 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
414 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
415 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
416 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
417 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
418 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
419 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
420 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
421 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
424 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
425 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
426 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
427 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
428 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
429 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
430 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
431 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
432 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
433 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
434 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
435 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
436 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
437 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
438 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
441 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
443 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
444 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
445 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
446 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
453 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
454 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
457 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
458 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
474 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
475 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
476 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
477 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
478 { roff_it
, NULL
, NULL
, 0 }, /* it */
479 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
485 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
486 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
487 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
488 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
489 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
490 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
491 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
493 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
494 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
495 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
496 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
497 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
498 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
503 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
504 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
508 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
509 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
510 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
511 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
512 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
513 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
515 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
516 { roff_insec
, NULL
, NULL
, 0 }, /* open */
517 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
518 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
519 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
521 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
522 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
524 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
525 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
526 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
531 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
532 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
533 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
534 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
536 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
537 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
538 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
539 { roff_return
, NULL
, NULL
, 0 }, /* return */
540 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
541 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
542 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
543 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
544 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
545 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
546 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
548 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
550 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
551 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
552 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
553 { roff_so
, NULL
, NULL
, 0 }, /* so */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
555 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
559 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
561 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
562 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
563 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
564 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
565 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
566 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
567 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
571 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
572 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
574 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
575 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
576 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
577 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
578 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
579 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
580 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
581 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
582 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
583 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
584 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
587 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
589 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
590 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
592 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
593 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
594 { roff_insec
, NULL
, NULL
, 0 }, /* write */
595 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
596 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
597 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
598 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
599 { roff_renamed
, NULL
, NULL
, 0 },
600 { roff_userdef
, NULL
, NULL
, 0 }
603 /* Array of injected predefined strings. */
604 #define PREDEFS_MAX 38
605 static const struct predef predefs
[PREDEFS_MAX
] = {
606 #include "predefs.in"
609 static int roffce_lines
; /* number of input lines to center */
610 static struct roff_node
*roffce_node
; /* active request */
611 static int roffit_lines
; /* number of lines to delay */
612 static char *roffit_macro
; /* nil-terminated macro line */
615 /* --- request table ------------------------------------------------------ */
618 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
626 htab
= mandoc_malloc(sizeof(*htab
));
627 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
629 for (tok
= mintok
; tok
< maxtok
; tok
++) {
630 if (roff_name
[tok
] == NULL
)
632 sz
= strlen(roff_name
[tok
]);
633 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
635 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
636 slot
= ohash_qlookup(htab
, req
->name
);
637 ohash_insert(htab
, slot
, req
);
643 roffhash_free(struct ohash
*htab
)
650 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
651 req
= ohash_next(htab
, &slot
))
658 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
665 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
667 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
668 return req
== NULL
? TOKEN_NONE
: req
->tok
;
671 /* --- stack of request blocks -------------------------------------------- */
674 * Pop the current node off of the stack of roff instructions currently
678 roffnode_pop(struct roff
*r
)
684 inloop
= p
->tok
== ROFF_while
;
693 * Push a roff node onto the instruction stack. This must later be
694 * removed with roffnode_pop().
697 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
702 p
= mandoc_calloc(1, sizeof(struct roffnode
));
705 p
->name
= mandoc_strdup(name
);
709 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
714 /* --- roff parser state data management ---------------------------------- */
717 roff_free1(struct roff
*r
)
719 struct tbl_node
*tbl
;
722 while (NULL
!= (tbl
= r
->first_tbl
)) {
723 r
->first_tbl
= tbl
->next
;
726 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
728 if (r
->last_eqn
!= NULL
)
729 eqn_free(r
->last_eqn
);
730 r
->last_eqn
= r
->eqn
= NULL
;
732 while (r
->mstackpos
>= 0)
743 roff_freereg(r
->regtab
);
746 roff_freestr(r
->strtab
);
747 roff_freestr(r
->rentab
);
748 roff_freestr(r
->xmbtab
);
749 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
752 for (i
= 0; i
< 128; i
++)
759 roff_reset(struct roff
*r
)
762 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
772 roff_free(struct roff
*r
)
777 for (i
= 0; i
< r
->mstacksz
; i
++)
778 free(r
->mstack
[i
].argv
);
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_RENAMED
);
792 r
->options
= options
;
793 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
800 /* --- syntax tree state data management ---------------------------------- */
803 roff_man_free1(struct roff_man
*man
)
806 if (man
->first
!= NULL
)
807 roff_node_delete(man
, man
->first
);
808 free(man
->meta
.msec
);
811 free(man
->meta
.arch
);
812 free(man
->meta
.title
);
813 free(man
->meta
.name
);
814 free(man
->meta
.date
);
818 roff_man_alloc1(struct roff_man
*man
)
821 memset(&man
->meta
, 0, sizeof(man
->meta
));
822 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
823 man
->first
->type
= ROFFT_ROOT
;
824 man
->last
= man
->first
;
827 man
->macroset
= MACROSET_NONE
;
828 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
829 man
->next
= ROFF_NEXT_CHILD
;
833 roff_man_reset(struct roff_man
*man
)
837 roff_man_alloc1(man
);
841 roff_man_free(struct roff_man
*man
)
849 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
850 const char *os_s
, int quick
)
852 struct roff_man
*man
;
854 man
= mandoc_calloc(1, sizeof(*man
));
859 roff_man_alloc1(man
);
864 /* --- syntax tree handling ----------------------------------------------- */
867 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
868 enum roff_type type
, int tok
)
872 n
= mandoc_calloc(1, sizeof(*n
));
877 n
->sec
= man
->lastsec
;
879 if (man
->flags
& MDOC_SYNOPSIS
)
880 n
->flags
|= NODE_SYNPRETTY
;
882 n
->flags
&= ~NODE_SYNPRETTY
;
883 if (man
->flags
& MDOC_NEWLINE
)
884 n
->flags
|= NODE_LINE
;
885 man
->flags
&= ~MDOC_NEWLINE
;
891 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
895 case ROFF_NEXT_SIBLING
:
896 if (man
->last
->next
!= NULL
) {
897 n
->next
= man
->last
->next
;
898 man
->last
->next
->prev
= n
;
900 man
->last
->parent
->last
= n
;
903 n
->parent
= man
->last
->parent
;
905 case ROFF_NEXT_CHILD
:
906 if (man
->last
->child
!= NULL
) {
907 n
->next
= man
->last
->child
;
908 man
->last
->child
->prev
= n
;
911 man
->last
->child
= n
;
912 n
->parent
= man
->last
;
924 if (n
->end
!= ENDBODY_NOT
)
936 * Copy over the normalised-data pointer of our parent. Not
937 * everybody has one, but copying a null pointer is fine.
940 n
->norm
= n
->parent
->norm
;
941 assert(n
->parent
->type
== ROFFT_BLOCK
);
945 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
949 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
950 n
->string
= roff_strdup(man
->roff
, word
);
951 roff_node_append(man
, n
);
952 n
->flags
|= NODE_VALID
| NODE_ENDED
;
953 man
->next
= ROFF_NEXT_SIBLING
;
957 roff_word_append(struct roff_man
*man
, const char *word
)
960 char *addstr
, *newstr
;
963 addstr
= roff_strdup(man
->roff
, word
);
964 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
968 man
->next
= ROFF_NEXT_SIBLING
;
972 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
976 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
977 roff_node_append(man
, n
);
978 man
->next
= ROFF_NEXT_CHILD
;
982 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
986 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
987 roff_node_append(man
, n
);
988 man
->next
= ROFF_NEXT_CHILD
;
993 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
997 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
998 roff_node_append(man
, n
);
999 man
->next
= ROFF_NEXT_CHILD
;
1004 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1006 struct roff_node
*n
;
1008 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1009 roff_node_append(man
, n
);
1010 man
->next
= ROFF_NEXT_CHILD
;
1015 roff_addtbl(struct roff_man
*man
, struct tbl_node
*tbl
)
1017 struct roff_node
*n
;
1018 const struct tbl_span
*span
;
1020 if (man
->macroset
== MACROSET_MAN
)
1021 man_breakscope(man
, ROFF_TS
);
1022 while ((span
= tbl_span(tbl
)) != NULL
) {
1023 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1025 roff_node_append(man
, n
);
1026 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1027 man
->next
= ROFF_NEXT_SIBLING
;
1032 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1035 /* Adjust siblings. */
1038 n
->prev
->next
= n
->next
;
1040 n
->next
->prev
= n
->prev
;
1042 /* Adjust parent. */
1044 if (n
->parent
!= NULL
) {
1045 if (n
->parent
->child
== n
)
1046 n
->parent
->child
= n
->next
;
1047 if (n
->parent
->last
== n
)
1048 n
->parent
->last
= n
->prev
;
1051 /* Adjust parse point. */
1055 if (man
->last
== n
) {
1056 if (n
->prev
== NULL
) {
1057 man
->last
= n
->parent
;
1058 man
->next
= ROFF_NEXT_CHILD
;
1060 man
->last
= n
->prev
;
1061 man
->next
= ROFF_NEXT_SIBLING
;
1064 if (man
->first
== n
)
1069 roff_node_free(struct roff_node
*n
)
1072 if (n
->args
!= NULL
)
1073 mdoc_argv_free(n
->args
);
1074 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1077 eqn_box_free(n
->eqn
);
1083 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1086 while (n
->child
!= NULL
)
1087 roff_node_delete(man
, n
->child
);
1088 roff_node_unlink(man
, n
);
1093 deroff(char **dest
, const struct roff_node
*n
)
1098 if (n
->type
!= ROFFT_TEXT
) {
1099 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1104 /* Skip leading whitespace. */
1106 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1107 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1108 strchr(" %&0^|~", cp
[1]) != NULL
)
1110 else if ( ! isspace((unsigned char)*cp
))
1114 /* Skip trailing backslash. */
1117 if (sz
> 0 && cp
[sz
- 1] == '\\')
1120 /* Skip trailing whitespace. */
1123 if ( ! isspace((unsigned char)cp
[sz
-1]))
1126 /* Skip empty strings. */
1131 if (*dest
== NULL
) {
1132 *dest
= mandoc_strndup(cp
, sz
);
1136 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1141 /* --- main functions of the roff parser ---------------------------------- */
1144 * In the current line, expand escape sequences that tend to get
1145 * used in numerical expressions and conditional requests.
1146 * Also check the syntax of the remaining escape sequences.
1149 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1151 struct mctx
*ctx
; /* current macro call context */
1152 char ubuf
[24]; /* buffer to print the number */
1153 struct roff_node
*n
; /* used for header comments */
1154 const char *start
; /* start of the string to process */
1155 char *stesc
; /* start of an escape sequence ('\\') */
1156 char *ep
; /* end of comment string */
1157 const char *stnam
; /* start of the name, after "[(*" */
1158 const char *cp
; /* end of the name, e.g. before ']' */
1159 const char *res
; /* the string to be substituted */
1160 char *nbuf
; /* new buffer to copy buf->buf to */
1161 size_t maxl
; /* expected length of the escape name */
1162 size_t naml
; /* actual length of the escape name */
1163 size_t asz
; /* length of the replacement */
1164 size_t rsz
; /* length of the rest of the string */
1165 enum mandoc_esc esc
; /* type of the escape sequence */
1166 int inaml
; /* length returned from mandoc_escape() */
1167 int expand_count
; /* to avoid infinite loops */
1168 int npos
; /* position in numeric expression */
1169 int arg_complete
; /* argument not interrupted by eol */
1170 int quote_args
; /* true for \\$@, false for \\$* */
1171 int done
; /* no more input available */
1172 int deftype
; /* type of definition to paste */
1173 int rcsid
; /* kind of RCS id seen */
1174 char sign
; /* increment number register */
1175 char term
; /* character terminating the escape */
1177 /* Search forward for comments. */
1180 start
= buf
->buf
+ pos
;
1181 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1182 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1185 if (*stesc
!= '"' && *stesc
!= '#')
1188 /* Comment found, look for RCS id. */
1191 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1192 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1194 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1195 rcsid
= 1 << MANDOC_OS_NETBSD
;
1199 isalnum((unsigned char)*cp
) == 0 &&
1200 strchr(cp
, '$') != NULL
) {
1201 if (r
->man
->meta
.rcsids
& rcsid
)
1202 mandoc_msg(MANDOCERR_RCS_REP
, r
->parse
,
1203 ln
, stesc
+ 1 - buf
->buf
, stesc
+ 1);
1204 r
->man
->meta
.rcsids
|= rcsid
;
1207 /* Handle trailing whitespace. */
1209 ep
= strchr(stesc
--, '\0') - 1;
1214 if (*ep
== ' ' || *ep
== '\t')
1215 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1216 ln
, ep
- buf
->buf
, NULL
);
1219 * Save comments preceding the title macro
1220 * in the syntax tree.
1223 if (r
->format
== 0) {
1224 while (*ep
== ' ' || *ep
== '\t')
1227 n
= roff_node_alloc(r
->man
,
1228 ln
, stesc
+ 1 - buf
->buf
,
1229 ROFFT_COMMENT
, TOKEN_NONE
);
1230 n
->string
= mandoc_strdup(stesc
+ 2);
1231 roff_node_append(r
->man
, n
);
1232 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1233 r
->man
->next
= ROFF_NEXT_SIBLING
;
1236 /* Line continuation with comment. */
1238 if (stesc
[1] == '#') {
1240 return ROFF_IGN
| ROFF_APPEND
;
1243 /* Discard normal comments. */
1245 while (stesc
> start
&& stesc
[-1] == ' ' &&
1246 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1255 /* Notice the end of the input. */
1257 if (*stesc
== '\n') {
1263 while (stesc
>= start
) {
1265 /* Search backwards for the next backslash. */
1267 if (*stesc
!= r
->escape
) {
1268 if (*stesc
== '\\') {
1270 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1271 buf
->buf
, stesc
+ 1) + 1;
1273 stesc
= nbuf
+ (stesc
- buf
->buf
);
1281 /* If it is escaped, skip it. */
1283 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1284 if (*cp
!= r
->escape
)
1287 if ((stesc
- cp
) % 2 == 0) {
1291 } else if (stesc
[1] != '\0') {
1298 return ROFF_IGN
| ROFF_APPEND
;
1301 /* Decide whether to expand or to check only. */
1316 if (sign
== '+' || sign
== '-')
1321 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1322 if (esc
== ESCAPE_ERROR
||
1323 (esc
== ESCAPE_SPECIAL
&&
1324 mchars_spec2cp(stnam
, inaml
) < 0))
1325 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1326 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1327 "%.*s", (int)(cp
- stesc
), stesc
);
1332 if (EXPAND_LIMIT
< ++expand_count
) {
1333 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1334 ln
, (int)(stesc
- buf
->buf
), NULL
);
1339 * The third character decides the length
1340 * of the name of the string or register.
1341 * Save a pointer to the name.
1368 /* Advance to the end of the name. */
1372 while (maxl
== 0 || naml
< maxl
) {
1374 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1375 ln
, (int)(stesc
- buf
->buf
), stesc
);
1379 if (maxl
== 0 && *cp
== term
) {
1383 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1387 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1388 case ESCAPE_SPECIAL
:
1389 case ESCAPE_UNICODE
:
1390 case ESCAPE_NUMBERED
:
1391 case ESCAPE_OVERSTRIKE
:
1400 * Retrieve the replacement string; if it is
1401 * undefined, resume searching for escapes.
1407 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1408 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1411 * If not overriden, let \*(.T
1412 * through to the formatters.
1415 if (res
== NULL
&& naml
== 2 &&
1416 stnam
[0] == '.' && stnam
[1] == 'T') {
1417 roff_setstrn(&r
->strtab
,
1418 ".T", 2, NULL
, 0, 0);
1425 if (r
->mstackpos
< 0) {
1426 mandoc_vmsg(MANDOCERR_ARG_UNDEF
,
1427 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1431 ctx
= r
->mstack
+ r
->mstackpos
;
1432 npos
= stesc
[2] - '1';
1433 if (npos
>= 0 && npos
<= 8) {
1434 res
= npos
< ctx
->argc
?
1435 ctx
->argv
[npos
] : "";
1438 if (stesc
[2] == '*')
1440 else if (stesc
[2] == '@')
1443 mandoc_vmsg(MANDOCERR_ARG_NONUM
,
1444 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1449 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1453 asz
+= 2; /* quotes */
1454 asz
+= strlen(ctx
->argv
[npos
]);
1457 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1459 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1461 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1463 stesc
= nbuf
+ (stesc
- buf
->buf
);
1466 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1468 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1473 cp
= ctx
->argv
[npos
];
1482 ubuf
[0] = arg_complete
&&
1483 roff_evalnum(r
, ln
, stnam
, &npos
,
1484 NULL
, ROFFNUM_SCALE
) &&
1485 stnam
+ npos
+ 1 == cp
? '1' : '0';
1490 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1491 roff_getregn(r
, stnam
, naml
, sign
));
1496 /* use even incomplete args */
1497 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1503 if (stesc
[1] == '*')
1504 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1505 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1506 "%.*s", (int)naml
, stnam
);
1508 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1509 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1510 ln
, (int)(stesc
- buf
->buf
), NULL
);
1514 /* Replace the escape sequence by the string. */
1517 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1518 buf
->buf
, res
, cp
) + 1;
1520 /* Prepare for the next replacement. */
1523 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1531 * Process text streams.
1534 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1540 enum mandoc_esc esc
;
1542 /* Spring the input line trap. */
1544 if (roffit_lines
== 1) {
1545 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1552 return ROFF_REPARSE
;
1553 } else if (roffit_lines
> 1)
1556 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1557 if (roffce_lines
< 1) {
1558 r
->man
->last
= roffce_node
;
1559 r
->man
->next
= ROFF_NEXT_SIBLING
;
1566 /* Convert all breakable hyphens into ASCII_HYPH. */
1568 start
= p
= buf
->buf
+ pos
;
1570 while (*p
!= '\0') {
1571 sz
= strcspn(p
, "-\\");
1578 /* Skip over escapes. */
1580 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1581 if (esc
== ESCAPE_ERROR
)
1586 } else if (p
== start
) {
1591 if (isalpha((unsigned char)p
[-1]) &&
1592 isalpha((unsigned char)p
[1]))
1600 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1604 int pos
; /* parse point */
1605 int spos
; /* saved parse point for messages */
1606 int ppos
; /* original offset in buf->buf */
1607 int ctl
; /* macro line (boolean) */
1611 /* Handle in-line equation delimiters. */
1613 if (r
->tbl
== NULL
&&
1614 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1615 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1616 e
= roff_eqndelim(r
, buf
, pos
);
1617 if (e
== ROFF_REPARSE
)
1619 assert(e
== ROFF_CONT
);
1622 /* Expand some escape sequences. */
1624 e
= roff_res(r
, buf
, ln
, pos
);
1625 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1627 assert(e
== ROFF_CONT
);
1629 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1632 * First, if a scope is open and we're not a macro, pass the
1633 * text through the macro's filter.
1634 * Equations process all content themselves.
1635 * Tables process almost all content themselves, but we want
1636 * to warn about macros before passing it there.
1639 if (r
->last
!= NULL
&& ! ctl
) {
1641 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1642 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1647 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1648 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1651 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1652 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1653 roff_addtbl(r
->man
, r
->tbl
);
1657 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1659 /* Skip empty request lines. */
1661 if (buf
->buf
[pos
] == '"') {
1662 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1665 } else if (buf
->buf
[pos
] == '\0')
1669 * If a scope is open, go to the child handler for that macro,
1670 * as it may want to preprocess before doing anything with it.
1671 * Don't do so if an equation is open.
1676 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1679 /* No scope is open. This is a new request or macro. */
1682 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1684 /* Tables ignore most macros. */
1686 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1687 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1688 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1689 ln
, pos
, buf
->buf
+ spos
);
1690 if (t
!= TOKEN_NONE
)
1692 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1694 while (buf
->buf
[pos
] == ' ')
1696 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1697 roff_addtbl(r
->man
, r
->tbl
);
1701 /* For now, let high level macros abort .ce mode. */
1703 if (ctl
&& roffce_node
!= NULL
&&
1704 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1705 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1706 r
->man
->last
= roffce_node
;
1707 r
->man
->next
= ROFF_NEXT_SIBLING
;
1713 * This is neither a roff request nor a user-defined macro.
1714 * Let the standard macro set parsers handle it.
1717 if (t
== TOKEN_NONE
)
1720 /* Execute a roff request or a user defined macro. */
1722 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1726 * Internal interface function to tell the roff parser that execution
1727 * of the current macro ended. This is required because macro
1728 * definitions usually do not end with a .return request.
1731 roff_userret(struct roff
*r
)
1736 assert(r
->mstackpos
>= 0);
1737 ctx
= r
->mstack
+ r
->mstackpos
;
1738 for (i
= 0; i
< ctx
->argc
; i
++)
1745 roff_endparse(struct roff
*r
)
1747 if (r
->last
!= NULL
)
1748 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1749 r
->last
->line
, r
->last
->col
,
1750 roff_name
[r
->last
->tok
]);
1752 if (r
->eqn
!= NULL
) {
1753 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1754 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1759 if (r
->tbl
!= NULL
) {
1760 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1761 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1768 * Parse a roff node's type from the input buffer. This must be in the
1769 * form of ".foo xxx" in the usual way.
1771 static enum roff_tok
1772 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1782 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1786 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1788 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1789 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1798 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1801 if (t
!= TOKEN_NONE
)
1803 else if (deftype
== ROFFDEF_UNDEF
) {
1804 /* Using an undefined macro defines it to be empty. */
1805 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1806 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1811 /* --- handling of request blocks ----------------------------------------- */
1814 roff_cblock(ROFF_ARGS
)
1818 * A block-close `..' should only be invoked as a child of an
1819 * ignore macro, otherwise raise a warning and just ignore it.
1822 if (r
->last
== NULL
) {
1823 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1828 switch (r
->last
->tok
) {
1830 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1833 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1838 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1843 if (buf
->buf
[pos
] != '\0')
1844 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1845 ".. %s", buf
->buf
+ pos
);
1848 roffnode_cleanscope(r
);
1854 roffnode_cleanscope(struct roff
*r
)
1859 while (r
->last
!= NULL
) {
1860 if (--r
->last
->endspan
!= 0)
1862 inloop
+= roffnode_pop(r
);
1868 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1870 if (NULL
== r
->last
) {
1871 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1876 switch (r
->last
->tok
) {
1883 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1888 if (r
->last
->endspan
> -1) {
1889 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1894 return roffnode_pop(r
) + roffnode_cleanscope(r
);
1898 roff_block(ROFF_ARGS
)
1900 const char *name
, *value
;
1901 char *call
, *cp
, *iname
, *rname
;
1902 size_t csz
, namesz
, rsz
;
1905 /* Ignore groff compatibility mode for now. */
1907 if (tok
== ROFF_de1
)
1909 else if (tok
== ROFF_dei1
)
1911 else if (tok
== ROFF_am1
)
1913 else if (tok
== ROFF_ami1
)
1916 /* Parse the macro name argument. */
1918 cp
= buf
->buf
+ pos
;
1919 if (tok
== ROFF_ig
) {
1924 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1925 iname
[namesz
] = '\0';
1928 /* Resolve the macro name argument if it is indirect. */
1930 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1931 deftype
= ROFFDEF_USER
;
1932 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1934 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1935 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1936 "%.*s", (int)namesz
, iname
);
1939 namesz
= strlen(name
);
1943 if (namesz
== 0 && tok
!= ROFF_ig
) {
1944 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1945 ln
, ppos
, roff_name
[tok
]);
1949 roffnode_push(r
, tok
, name
, ln
, ppos
);
1952 * At the beginning of a `de' macro, clear the existing string
1953 * with the same name, if there is one. New content will be
1954 * appended from roff_block_text() in multiline mode.
1957 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
1958 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1959 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1960 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
1961 deftype
= ROFFDEF_ANY
;
1962 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1963 switch (deftype
) { /* Before appending, ... */
1964 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
1965 roff_setstrn(&r
->strtab
, name
, namesz
,
1966 value
, strlen(value
), 0);
1968 case ROFFDEF_REN
: /* call original standard macro. */
1969 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1970 (int)strlen(value
), value
);
1971 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1972 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1975 case ROFFDEF_STD
: /* rename and call standard macro. */
1976 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
1977 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
1978 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1980 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1992 /* Get the custom end marker. */
1995 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1997 /* Resolve the end marker if it is indirect. */
1999 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2000 deftype
= ROFFDEF_USER
;
2001 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2003 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
2004 r
->parse
, ln
, (int)(iname
- buf
->buf
),
2005 "%.*s", (int)namesz
, iname
);
2008 namesz
= strlen(name
);
2013 r
->last
->end
= mandoc_strndup(name
, namesz
);
2016 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2017 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2023 roff_block_sub(ROFF_ARGS
)
2029 * First check whether a custom macro exists at this level. If
2030 * it does, then check against it. This is some of groff's
2031 * stranger behaviours. If we encountered a custom end-scope
2032 * tag and that tag also happens to be a "real" macro, then we
2033 * need to try interpreting it again as a real macro. If it's
2034 * not, then return ignore. Else continue.
2038 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2039 if (buf
->buf
[i
] != r
->last
->end
[j
])
2042 if (r
->last
->end
[j
] == '\0' &&
2043 (buf
->buf
[i
] == '\0' ||
2044 buf
->buf
[i
] == ' ' ||
2045 buf
->buf
[i
] == '\t')) {
2047 roffnode_cleanscope(r
);
2049 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2053 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2061 * If we have no custom end-query or lookup failed, then try
2062 * pulling it out of the hashtable.
2065 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2067 if (t
!= ROFF_cblock
) {
2069 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2073 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2077 roff_block_text(ROFF_ARGS
)
2081 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2087 roff_cond_sub(ROFF_ARGS
)
2090 int endloop
, irc
, rr
;
2095 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2096 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2097 if (roffnode_cleanscope(r
))
2101 * If `\}' occurs on a macro line without a preceding macro,
2102 * drop the line completely.
2105 ep
= buf
->buf
+ pos
;
2106 if (ep
[0] == '\\' && ep
[1] == '}')
2109 /* Always check for the closing delimiter `\}'. */
2111 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2114 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2115 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2128 * Fully handle known macros when they are structurally
2129 * required or when the conditional evaluated to true.
2132 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2133 irc
|= t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
) ?
2134 (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) :
2135 rr
? ROFF_CONT
: ROFF_IGN
;
2140 roff_cond_text(ROFF_ARGS
)
2143 int endloop
, irc
, rr
;
2147 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2148 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2149 if (roffnode_cleanscope(r
))
2152 ep
= buf
->buf
+ pos
;
2153 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2154 if (*(++ep
) == '}') {
2156 if (roff_ccond(r
, ln
, ep
- buf
->buf
- 1))
2167 /* --- handling of numeric and conditional expressions -------------------- */
2170 * Parse a single signed integer number. Stop at the first non-digit.
2171 * If there is at least one digit, return success and advance the
2172 * parse point, else return failure and let the parse point unchanged.
2173 * Ignore overflows, treat them just like the C language.
2176 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2178 int myres
, scaled
, n
, p
;
2185 if (n
|| v
[p
] == '+')
2188 if (flags
& ROFFNUM_WHITE
)
2189 while (isspace((unsigned char)v
[p
]))
2192 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2193 *res
= 10 * *res
+ v
[p
] - '0';
2200 /* Each number may be followed by one optional scaling unit. */
2204 scaled
= *res
* 65536;
2207 scaled
= *res
* 240;
2210 scaled
= *res
* 240 / 2.54;
2221 scaled
= *res
* 10 / 3;
2227 scaled
= *res
* 6 / 25;
2234 if (flags
& ROFFNUM_SCALE
)
2242 * Evaluate a string comparison condition.
2243 * The first character is the delimiter.
2244 * Succeed if the string up to its second occurrence
2245 * matches the string up to its third occurence.
2246 * Advance the cursor after the third occurrence
2247 * or lacking that, to the end of the line.
2250 roff_evalstrcond(const char *v
, int *pos
)
2252 const char *s1
, *s2
, *s3
;
2256 s1
= v
+ *pos
; /* initial delimiter */
2257 s2
= s1
+ 1; /* for scanning the first string */
2258 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2260 if (NULL
== s3
) /* found no middle delimiter */
2263 while ('\0' != *++s3
) {
2264 if (*s2
!= *s3
) { /* mismatch */
2265 s3
= strchr(s3
, *s1
);
2268 if (*s3
== *s1
) { /* found the final delimiter */
2277 s3
= strchr(s2
, '\0');
2278 else if (*s3
!= '\0')
2285 * Evaluate an optionally negated single character, numerical,
2286 * or string condition.
2289 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2291 const char *start
, *end
;
2294 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2296 if ('!' == v
[*pos
]) {
2317 } while (v
[*pos
] == ' ');
2320 * Quirk for groff compatibility:
2321 * The horizontal tab is neither available nor unavailable.
2324 if (v
[*pos
] == '\t') {
2329 /* Printable ASCII characters are available. */
2331 if (v
[*pos
] != '\\') {
2337 switch (mandoc_escape(&end
, &start
, &len
)) {
2338 case ESCAPE_SPECIAL
:
2339 istrue
= mchars_spec2cp(start
, len
) != -1;
2341 case ESCAPE_UNICODE
:
2344 case ESCAPE_NUMBERED
:
2345 istrue
= mchars_num2char(start
, len
) != -1;
2352 return istrue
== wanttrue
;
2359 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2362 else if (v
[*pos
] == 'r')
2363 istrue
= roff_hasregn(r
, name
, sz
);
2365 deftype
= ROFFDEF_ANY
;
2366 roff_getstrn(r
, name
, sz
, &deftype
);
2370 return istrue
== wanttrue
;
2376 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2377 return (number
> 0) == wanttrue
;
2378 else if (*pos
== savepos
)
2379 return roff_evalstrcond(v
, pos
) == wanttrue
;
2385 roff_line_ignore(ROFF_ARGS
)
2392 roff_insec(ROFF_ARGS
)
2395 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2396 ln
, ppos
, roff_name
[tok
]);
2401 roff_unsupp(ROFF_ARGS
)
2404 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2405 ln
, ppos
, roff_name
[tok
]);
2410 roff_cond(ROFF_ARGS
)
2414 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2417 * An `.el' has no conditional body: it will consume the value
2418 * of the current rstack entry set in prior `ie' calls or
2421 * If we're not an `el', however, then evaluate the conditional.
2424 r
->last
->rule
= tok
== ROFF_el
?
2425 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2426 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2429 * An if-else will put the NEGATION of the current evaluated
2430 * conditional into the stack of rules.
2433 if (tok
== ROFF_ie
) {
2434 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2436 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2437 r
->rstacksz
, sizeof(int));
2439 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2442 /* If the parent has false as its rule, then so do we. */
2444 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2449 * If there is nothing on the line after the conditional,
2450 * not even whitespace, use next-line scope.
2451 * Except that .while does not support next-line scope.
2454 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2455 r
->last
->endspan
= 2;
2459 while (buf
->buf
[pos
] == ' ')
2462 /* An opening brace requests multiline scope. */
2464 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2465 r
->last
->endspan
= -1;
2467 while (buf
->buf
[pos
] == ' ')
2473 * Anything else following the conditional causes
2474 * single-line scope. Warn if the scope contains
2475 * nothing but trailing whitespace.
2478 if (buf
->buf
[pos
] == '\0')
2479 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2480 ln
, ppos
, roff_name
[tok
]);
2482 r
->last
->endspan
= 1;
2487 if (tok
== ROFF_while
)
2499 /* Ignore groff compatibility mode for now. */
2501 if (tok
== ROFF_ds1
)
2503 else if (tok
== ROFF_as1
)
2507 * The first word is the name of the string.
2508 * If it is empty or terminated by an escape sequence,
2509 * abort the `ds' request without defining anything.
2512 name
= string
= buf
->buf
+ pos
;
2516 namesz
= roff_getname(r
, &string
, ln
, pos
);
2517 if (name
[namesz
] == '\\')
2520 /* Read past the initial double-quote, if any. */
2524 /* The rest is the value. */
2525 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2527 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2532 * Parse a single operator, one or two characters long.
2533 * If the operator is recognized, return success and advance the
2534 * parse point, else return failure and let the parse point unchanged.
2537 roff_getop(const char *v
, int *pos
, char *res
)
2552 switch (v
[*pos
+ 1]) {
2570 switch (v
[*pos
+ 1]) {
2584 if ('=' == v
[*pos
+ 1])
2596 * Evaluate either a parenthesized numeric expression
2597 * or a single signed integer number.
2600 roff_evalpar(struct roff
*r
, int ln
,
2601 const char *v
, int *pos
, int *res
, int flags
)
2605 return roff_getnum(v
, pos
, res
, flags
);
2608 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2612 * Omission of the closing parenthesis
2613 * is an error in validation mode,
2614 * but ignored in evaluation mode.
2619 else if (NULL
== res
)
2626 * Evaluate a complete numeric expression.
2627 * Proceed left to right, there is no concept of precedence.
2630 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2631 int *pos
, int *res
, int flags
)
2633 int mypos
, operand2
;
2641 if (flags
& ROFFNUM_WHITE
)
2642 while (isspace((unsigned char)v
[*pos
]))
2645 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2649 if (flags
& ROFFNUM_WHITE
)
2650 while (isspace((unsigned char)v
[*pos
]))
2653 if ( ! roff_getop(v
, pos
, &operator))
2656 if (flags
& ROFFNUM_WHITE
)
2657 while (isspace((unsigned char)v
[*pos
]))
2660 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2663 if (flags
& ROFFNUM_WHITE
)
2664 while (isspace((unsigned char)v
[*pos
]))
2681 if (operand2
== 0) {
2682 mandoc_msg(MANDOCERR_DIVZERO
,
2683 r
->parse
, ln
, *pos
, v
);
2690 if (operand2
== 0) {
2691 mandoc_msg(MANDOCERR_DIVZERO
,
2692 r
->parse
, ln
, *pos
, v
);
2699 *res
= *res
< operand2
;
2702 *res
= *res
> operand2
;
2705 *res
= *res
<= operand2
;
2708 *res
= *res
>= operand2
;
2711 *res
= *res
== operand2
;
2714 *res
= *res
!= operand2
;
2717 *res
= *res
&& operand2
;
2720 *res
= *res
|| operand2
;
2723 if (operand2
< *res
)
2727 if (operand2
> *res
)
2737 /* --- register management ------------------------------------------------ */
2740 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2742 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2746 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2747 int val
, char sign
, int step
)
2749 struct roffreg
*reg
;
2751 /* Search for an existing register with the same name. */
2754 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2755 strncmp(reg
->key
.p
, name
, len
) != 0))
2759 /* Create a new register. */
2760 reg
= mandoc_malloc(sizeof(struct roffreg
));
2761 reg
->key
.p
= mandoc_strndup(name
, len
);
2765 reg
->next
= r
->regtab
;
2771 else if ('-' == sign
)
2775 if (step
!= INT_MIN
)
2780 * Handle some predefined read-only number registers.
2781 * For now, return -1 if the requested register is not predefined;
2782 * in case a predefined read-only register having the value -1
2783 * were to turn up, another special value would have to be chosen.
2786 roff_getregro(const struct roff
*r
, const char *name
)
2790 case '$': /* Number of arguments of the last macro evaluated. */
2791 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
2792 case 'A': /* ASCII approximation mode is always off. */
2794 case 'g': /* Groff compatibility mode is always on. */
2796 case 'H': /* Fixed horizontal resolution. */
2798 case 'j': /* Always adjust left margin only. */
2800 case 'T': /* Some output device is always defined. */
2802 case 'V': /* Fixed vertical resolution. */
2810 roff_getreg(struct roff
*r
, const char *name
)
2812 return roff_getregn(r
, name
, strlen(name
), '\0');
2816 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2818 struct roffreg
*reg
;
2821 if ('.' == name
[0] && 2 == len
) {
2822 val
= roff_getregro(r
, name
+ 1);
2827 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2828 if (len
== reg
->key
.sz
&&
2829 0 == strncmp(name
, reg
->key
.p
, len
)) {
2832 reg
->val
+= reg
->step
;
2835 reg
->val
-= reg
->step
;
2844 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
2849 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2851 struct roffreg
*reg
;
2854 if ('.' == name
[0] && 2 == len
) {
2855 val
= roff_getregro(r
, name
+ 1);
2860 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2861 if (len
== reg
->key
.sz
&&
2862 0 == strncmp(name
, reg
->key
.p
, len
))
2869 roff_freereg(struct roffreg
*reg
)
2871 struct roffreg
*old_reg
;
2873 while (NULL
!= reg
) {
2884 char *key
, *val
, *step
;
2889 key
= val
= buf
->buf
+ pos
;
2893 keysz
= roff_getname(r
, &val
, ln
, pos
);
2894 if (key
[keysz
] == '\\')
2898 if (sign
== '+' || sign
== '-')
2902 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
2906 while (isspace((unsigned char)*step
))
2908 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
2911 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
2918 struct roffreg
*reg
, **prev
;
2922 name
= cp
= buf
->buf
+ pos
;
2925 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2926 name
[namesz
] = '\0';
2931 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2943 /* --- handler functions for roff requests -------------------------------- */
2952 cp
= buf
->buf
+ pos
;
2953 while (*cp
!= '\0') {
2955 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2956 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2957 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2958 if (name
[namesz
] == '\\')
2969 /* Parse the number of lines. */
2971 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2972 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2973 ln
, ppos
, buf
->buf
+ 1);
2977 while (isspace((unsigned char)buf
->buf
[pos
]))
2981 * Arm the input line trap.
2982 * Special-casing "an-trap" is an ugly workaround to cope
2983 * with DocBook stupidly fiddling with man(7) internals.
2987 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2988 strcmp(buf
->buf
+ pos
, "an-trap") ?
2989 buf
->buf
+ pos
: "br");
2997 enum roff_tok t
, te
;
3004 r
->format
= MPARSE_MDOC
;
3005 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3011 r
->format
= MPARSE_MAN
;
3012 mask
= MPARSE_QUICK
;
3017 if ((r
->options
& mask
) == 0)
3018 for (t
= tok
; t
< te
; t
++)
3019 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3026 if (r
->tbl
== NULL
) {
3027 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3031 if (tbl_end(r
->tbl
) == 0) {
3034 buf
->buf
= mandoc_strdup(".sp");
3037 return ROFF_REPARSE
;
3048 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3051 tbl_restart(ln
, ppos
, r
->tbl
);
3057 * Handle in-line equation delimiters.
3060 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3063 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3066 * Outside equations, look for an opening delimiter.
3067 * If we are inside an equation, we already know it is
3068 * in-line, or this function wouldn't have been called;
3069 * so look for a closing delimiter.
3072 cp1
= buf
->buf
+ pos
;
3073 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3074 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3079 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3081 /* Handle preceding text, protecting whitespace. */
3083 if (*buf
->buf
!= '\0') {
3090 * Prepare replacing the delimiter with an equation macro
3091 * and drop leading white space from the equation.
3094 if (r
->eqn
== NULL
) {
3101 /* Handle following text, protecting whitespace. */
3109 /* Do the actual replacement. */
3111 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3112 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3116 /* Toggle the in-line state of the eqn subsystem. */
3118 r
->eqn_inline
= r
->eqn
== NULL
;
3119 return ROFF_REPARSE
;
3125 struct roff_node
*n
;
3127 if (r
->man
->macroset
== MACROSET_MAN
)
3128 man_breakscope(r
->man
, ROFF_EQ
);
3129 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3130 if (ln
> r
->man
->last
->line
)
3131 n
->flags
|= NODE_LINE
;
3132 n
->eqn
= mandoc_calloc(1, sizeof(*n
->eqn
));
3133 n
->eqn
->expectargs
= UINT_MAX
;
3134 roff_node_append(r
->man
, n
);
3135 r
->man
->next
= ROFF_NEXT_SIBLING
;
3137 assert(r
->eqn
== NULL
);
3138 if (r
->last_eqn
== NULL
)
3139 r
->last_eqn
= eqn_alloc(r
->parse
);
3141 eqn_reset(r
->last_eqn
);
3142 r
->eqn
= r
->last_eqn
;
3145 if (buf
->buf
[pos
] != '\0')
3146 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3147 ".EQ %s", buf
->buf
+ pos
);
3155 if (r
->eqn
!= NULL
) {
3159 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
3160 if (buf
->buf
[pos
] != '\0')
3161 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3162 "EN %s", buf
->buf
+ pos
);
3169 if (r
->tbl
!= NULL
) {
3170 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
3171 ln
, ppos
, "TS breaks TS");
3174 r
->tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
3176 r
->last_tbl
->next
= r
->tbl
;
3178 r
->first_tbl
= r
->tbl
;
3179 r
->last_tbl
= r
->tbl
;
3184 roff_onearg(ROFF_ARGS
)
3186 struct roff_node
*n
;
3190 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3191 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3193 man_breakscope(r
->man
, tok
);
3195 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3196 r
->man
->last
= roffce_node
;
3197 r
->man
->next
= ROFF_NEXT_SIBLING
;
3200 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3203 cp
= buf
->buf
+ pos
;
3205 while (*cp
!= '\0' && *cp
!= ' ')
3210 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
3211 r
->parse
, ln
, cp
- buf
->buf
,
3212 "%s ... %s", roff_name
[tok
], cp
);
3213 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3216 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3217 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3218 roff_word_alloc(r
->man
, ln
, pos
, "1");
3219 r
->man
->last
->flags
|= NODE_NOSRC
;
3222 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3223 &roffce_lines
, 0) == 0) {
3224 mandoc_vmsg(MANDOCERR_CE_NONUM
,
3225 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
3228 if (roffce_lines
< 1) {
3229 r
->man
->last
= r
->man
->last
->parent
;
3233 roffce_node
= r
->man
->last
->parent
;
3235 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3238 n
->flags
|= NODE_LINE
;
3239 r
->man
->next
= ROFF_NEXT_SIBLING
;
3244 roff_manyarg(ROFF_ARGS
)
3246 struct roff_node
*n
;
3249 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3252 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3253 while (*ep
!= '\0' && *ep
!= ' ')
3257 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3260 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3262 r
->man
->next
= ROFF_NEXT_SIBLING
;
3269 char *oldn
, *newn
, *end
, *value
;
3270 size_t oldsz
, newsz
, valsz
;
3272 newn
= oldn
= buf
->buf
+ pos
;
3276 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3277 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3281 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3285 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3287 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3288 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3296 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3297 man_breakscope(r
->man
, ROFF_br
);
3298 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3299 if (buf
->buf
[pos
] != '\0')
3300 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3301 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3302 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3303 r
->man
->next
= ROFF_NEXT_SIBLING
;
3314 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3318 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3319 ln
, p
- buf
->buf
, "cc ... %s", p
);
3335 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3336 ln
, p
- buf
->buf
, "ec ... %s", p
);
3345 if (buf
->buf
[pos
] != '\0')
3346 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3347 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3354 while (buf
->buf
[pos
] == ' ')
3363 const char *p
, *first
, *second
;
3365 enum mandoc_esc esc
;
3370 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3374 while (*p
!= '\0') {
3378 if (*first
== '\\') {
3379 esc
= mandoc_escape(&p
, NULL
, NULL
);
3380 if (esc
== ESCAPE_ERROR
) {
3381 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3382 ln
, (int)(p
- buf
->buf
), first
);
3385 fsz
= (size_t)(p
- first
);
3389 if (*second
== '\\') {
3390 esc
= mandoc_escape(&p
, NULL
, NULL
);
3391 if (esc
== ESCAPE_ERROR
) {
3392 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3393 ln
, (int)(p
- buf
->buf
), second
);
3396 ssz
= (size_t)(p
- second
);
3397 } else if (*second
== '\0') {
3398 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3399 ln
, first
- buf
->buf
, "tr %s", first
);
3405 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3410 if (r
->xtab
== NULL
)
3411 r
->xtab
= mandoc_calloc(128,
3412 sizeof(struct roffstr
));
3414 free(r
->xtab
[(int)*first
].p
);
3415 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3416 r
->xtab
[(int)*first
].sz
= ssz
;
3423 * Implementation of the .return request.
3424 * There is no need to call roff_userret() from here.
3425 * The read module will call that after rewinding the reader stack
3426 * to the place from where the current macro was called.
3429 roff_return(ROFF_ARGS
)
3431 if (r
->mstackpos
>= 0)
3432 return ROFF_IGN
| ROFF_USERRET
;
3434 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "return");
3442 char *oldn
, *newn
, *end
;
3443 size_t oldsz
, newsz
;
3446 oldn
= newn
= buf
->buf
+ pos
;
3450 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3451 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3455 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3459 deftype
= ROFFDEF_ANY
;
3460 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3463 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3464 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3465 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3468 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3469 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3472 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3473 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3474 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3477 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3478 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3481 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3482 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3489 roff_shift(ROFF_ARGS
)
3495 if (buf
->buf
[pos
] != '\0' &&
3496 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3497 mandoc_vmsg(MANDOCERR_CE_NONUM
, r
->parse
,
3498 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3501 if (r
->mstackpos
< 0) {
3502 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "shift");
3505 ctx
= r
->mstack
+ r
->mstackpos
;
3506 if (levels
> ctx
->argc
) {
3507 mandoc_vmsg(MANDOCERR_SHIFT
, r
->parse
,
3508 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3513 for (i
= 0; i
< levels
; i
++)
3515 ctx
->argc
-= levels
;
3516 for (i
= 0; i
< ctx
->argc
; i
++)
3517 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3526 name
= buf
->buf
+ pos
;
3527 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3530 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3531 * opening anything that's not in our cwd or anything beneath
3532 * it. Thus, explicitly disallow traversing up the file-system
3533 * or using absolute paths.
3536 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3537 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3539 buf
->sz
= mandoc_asprintf(&cp
,
3540 ".sp\nSee the file %s.\n.sp", name
) + 1;
3544 return ROFF_REPARSE
;
3551 /* --- user defined strings and macros ------------------------------------ */
3554 roff_userdef(ROFF_ARGS
)
3557 char *arg
, *ap
, *dst
, *src
;
3560 /* Initialize a new macro stack context. */
3562 if (++r
->mstackpos
== r
->mstacksz
) {
3563 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3564 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3567 ctx
= r
->mstack
+ r
->mstackpos
;
3573 * Collect pointers to macro argument strings,
3574 * NUL-terminating them and escaping quotes.
3577 src
= buf
->buf
+ pos
;
3578 while (*src
!= '\0') {
3579 if (ctx
->argc
== ctx
->argsz
) {
3581 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3582 ctx
->argsz
, sizeof(*ctx
->argv
));
3584 arg
= mandoc_getarg(r
->parse
, &src
, ln
, &pos
);
3585 sz
= 1; /* For the terminating NUL. */
3586 for (ap
= arg
; *ap
!= '\0'; ap
++)
3587 sz
+= *ap
== '"' ? 4 : 1;
3588 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3589 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3591 memcpy(dst
, "\\(dq", 4);
3599 /* Replace the macro invocation by the macro definition. */
3602 buf
->buf
= mandoc_strdup(r
->current_string
);
3603 buf
->sz
= strlen(buf
->buf
) + 1;
3606 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3607 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3611 * Calling a high-level macro that was renamed with .rn.
3612 * r->current_string has already been set up by roff_parse().
3615 roff_renamed(ROFF_ARGS
)
3619 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3620 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3628 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3637 /* Read until end of name and terminate it with NUL. */
3638 for (cp
= name
; 1; cp
++) {
3639 if ('\0' == *cp
|| ' ' == *cp
) {
3646 if ('{' == cp
[1] || '}' == cp
[1])
3651 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3652 "%.*s", (int)(cp
- name
+ 1), name
);
3653 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3657 /* Read past spaces. */
3666 * Store *string into the user-defined string called *name.
3667 * To clear an existing entry, call with (*r, *name, NULL, 0).
3668 * append == 0: replace mode
3669 * append == 1: single-line append mode
3670 * append == 2: multiline append mode, append '\n' after each call
3673 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3678 namesz
= strlen(name
);
3679 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3680 string
? strlen(string
) : 0, append
);
3681 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3685 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3686 const char *string
, size_t stringsz
, int append
)
3691 size_t oldch
, newch
;
3693 /* Search for an existing string with the same name. */
3696 while (n
&& (namesz
!= n
->key
.sz
||
3697 strncmp(n
->key
.p
, name
, namesz
)))
3701 /* Create a new string table entry. */
3702 n
= mandoc_malloc(sizeof(struct roffkv
));
3703 n
->key
.p
= mandoc_strndup(name
, namesz
);
3709 } else if (0 == append
) {
3719 * One additional byte for the '\n' in multiline mode,
3720 * and one for the terminating '\0'.
3722 newch
= stringsz
+ (1 < append
? 2u : 1u);
3724 if (NULL
== n
->val
.p
) {
3725 n
->val
.p
= mandoc_malloc(newch
);
3730 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3733 /* Skip existing content in the destination buffer. */
3734 c
= n
->val
.p
+ (int)oldch
;
3736 /* Append new content to the destination buffer. */
3738 while (i
< (int)stringsz
) {
3740 * Rudimentary roff copy mode:
3741 * Handle escaped backslashes.
3743 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3748 /* Append terminating bytes. */
3753 n
->val
.sz
= (int)(c
- n
->val
.p
);
3757 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
3760 const struct roffkv
*n
;
3765 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
3766 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3767 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3769 if (*deftype
& ROFFDEF_USER
) {
3770 *deftype
= ROFFDEF_USER
;
3777 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
3778 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3779 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3781 if (*deftype
& ROFFDEF_REN
) {
3782 *deftype
= ROFFDEF_REN
;
3789 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3790 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
3791 predefs
[i
].name
[len
] != '\0')
3793 if (*deftype
& ROFFDEF_PRE
) {
3794 *deftype
= ROFFDEF_PRE
;
3795 return predefs
[i
].str
;
3801 if (r
->man
->macroset
!= MACROSET_MAN
) {
3802 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
3803 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3804 roff_name
[tok
][len
] != '\0')
3806 if (*deftype
& ROFFDEF_STD
) {
3807 *deftype
= ROFFDEF_STD
;
3815 if (r
->man
->macroset
!= MACROSET_MDOC
) {
3816 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
3817 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3818 roff_name
[tok
][len
] != '\0')
3820 if (*deftype
& ROFFDEF_STD
) {
3821 *deftype
= ROFFDEF_STD
;
3830 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
3831 if (*deftype
& ROFFDEF_REN
) {
3833 * This might still be a request,
3834 * so do not treat it as undefined yet.
3836 *deftype
= ROFFDEF_UNDEF
;
3840 /* Using an undefined string defines it to be empty. */
3842 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
3843 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
3851 roff_freestr(struct roffkv
*r
)
3853 struct roffkv
*n
, *nn
;
3855 for (n
= r
; n
; n
= nn
) {
3863 /* --- accessors and utility functions ------------------------------------ */
3866 * Duplicate an input string, making the appropriate character
3867 * conversations (as stipulated by `tr') along the way.
3868 * Returns a heap-allocated string with all the replacements made.
3871 roff_strdup(const struct roff
*r
, const char *p
)
3873 const struct roffkv
*cp
;
3877 enum mandoc_esc esc
;
3879 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3880 return mandoc_strdup(p
);
3881 else if ('\0' == *p
)
3882 return mandoc_strdup("");
3885 * Step through each character looking for term matches
3886 * (remember that a `tr' can be invoked with an escape, which is
3887 * a glyph but the escape is multi-character).
3888 * We only do this if the character hash has been initialised
3889 * and the string is >0 length.
3895 while ('\0' != *p
) {
3896 assert((unsigned int)*p
< 128);
3897 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3898 sz
= r
->xtab
[(int)*p
].sz
;
3899 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3900 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3904 } else if ('\\' != *p
) {
3905 res
= mandoc_realloc(res
, ssz
+ 2);
3910 /* Search for term matches. */
3911 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3912 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3917 * A match has been found.
3918 * Append the match to the array and move
3919 * forward by its keysize.
3921 res
= mandoc_realloc(res
,
3922 ssz
+ cp
->val
.sz
+ 1);
3923 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3925 p
+= (int)cp
->key
.sz
;
3930 * Handle escapes carefully: we need to copy
3931 * over just the escape itself, or else we might
3932 * do replacements within the escape itself.
3933 * Make sure to pass along the bogus string.
3936 esc
= mandoc_escape(&p
, NULL
, NULL
);
3937 if (ESCAPE_ERROR
== esc
) {
3939 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3940 memcpy(res
+ ssz
, pp
, sz
);
3944 * We bail out on bad escapes.
3945 * No need to warn: we already did so when
3946 * roff_res() was called.
3949 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3950 memcpy(res
+ ssz
, pp
, sz
);
3954 res
[(int)ssz
] = '\0';
3959 roff_getformat(const struct roff
*r
)
3966 * Find out whether a line is a macro line or not.
3967 * If it is, adjust the current position and return one; if it isn't,
3968 * return zero and don't change the current position.
3969 * If the control character has been set with `.cc', then let that grain
3971 * This is slighly contrary to groff, where using the non-breaking
3972 * control character when `cc' has been invoked will cause the
3973 * non-breaking macro contents to be printed verbatim.
3976 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3982 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
3984 else if (r
->control
!= '\0')
3986 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3988 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3993 while (' ' == cp
[pos
] || '\t' == cp
[pos
])