roff.c revision 1.17.2.1 1 /* Id: roff.c,v 1.284 2016/01/08 17:48:10 schwarze Exp */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 * Copyright (c) 2010-2015 Ingo Schwarze <schwarze (at) openbsd.org>
5 *
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.
9 *
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.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "main.h"
30 #include "mandoc.h"
31 #include "mandoc_aux.h"
32 #include "roff.h"
33 #include "libmandoc.h"
34 #include "roff_int.h"
35 #include "libroff.h"
36
37 /* Maximum number of string expansions per line, to break infinite loops. */
38 #define EXPAND_LIMIT 1000
39
40 /* --- data types --------------------------------------------------------- */
41
42 enum rofft {
43 ROFF_ab,
44 ROFF_ad,
45 ROFF_af,
46 ROFF_aln,
47 ROFF_als,
48 ROFF_am,
49 ROFF_am1,
50 ROFF_ami,
51 ROFF_ami1,
52 ROFF_as,
53 ROFF_as1,
54 ROFF_asciify,
55 ROFF_backtrace,
56 ROFF_bd,
57 ROFF_bleedat,
58 ROFF_blm,
59 ROFF_box,
60 ROFF_boxa,
61 ROFF_bp,
62 ROFF_BP,
63 /* MAN_br, MDOC_br */
64 ROFF_break,
65 ROFF_breakchar,
66 ROFF_brnl,
67 ROFF_brp,
68 ROFF_brpnl,
69 ROFF_c2,
70 ROFF_cc,
71 ROFF_ce,
72 ROFF_cf,
73 ROFF_cflags,
74 ROFF_ch,
75 ROFF_char,
76 ROFF_chop,
77 ROFF_class,
78 ROFF_close,
79 ROFF_CL,
80 ROFF_color,
81 ROFF_composite,
82 ROFF_continue,
83 ROFF_cp,
84 ROFF_cropat,
85 ROFF_cs,
86 ROFF_cu,
87 ROFF_da,
88 ROFF_dch,
89 ROFF_Dd,
90 ROFF_de,
91 ROFF_de1,
92 ROFF_defcolor,
93 ROFF_dei,
94 ROFF_dei1,
95 ROFF_device,
96 ROFF_devicem,
97 ROFF_di,
98 ROFF_do,
99 ROFF_ds,
100 ROFF_ds1,
101 ROFF_dwh,
102 ROFF_dt,
103 ROFF_ec,
104 ROFF_ecr,
105 ROFF_ecs,
106 ROFF_el,
107 ROFF_em,
108 ROFF_EN,
109 ROFF_eo,
110 ROFF_EP,
111 ROFF_EQ,
112 ROFF_errprint,
113 ROFF_ev,
114 ROFF_evc,
115 ROFF_ex,
116 ROFF_fallback,
117 ROFF_fam,
118 ROFF_fc,
119 ROFF_fchar,
120 ROFF_fcolor,
121 ROFF_fdeferlig,
122 ROFF_feature,
123 /* MAN_fi; ignored in mdoc(7) */
124 ROFF_fkern,
125 ROFF_fl,
126 ROFF_flig,
127 ROFF_fp,
128 ROFF_fps,
129 ROFF_fschar,
130 ROFF_fspacewidth,
131 ROFF_fspecial,
132 /* MAN_ft; ignored in mdoc(7) */
133 ROFF_ftr,
134 ROFF_fzoom,
135 ROFF_gcolor,
136 ROFF_hc,
137 ROFF_hcode,
138 ROFF_hidechar,
139 ROFF_hla,
140 ROFF_hlm,
141 ROFF_hpf,
142 ROFF_hpfa,
143 ROFF_hpfcode,
144 ROFF_hw,
145 ROFF_hy,
146 ROFF_hylang,
147 ROFF_hylen,
148 ROFF_hym,
149 ROFF_hypp,
150 ROFF_hys,
151 ROFF_ie,
152 ROFF_if,
153 ROFF_ig,
154 /* MAN_in; ignored in mdoc(7) */
155 ROFF_index,
156 ROFF_it,
157 ROFF_itc,
158 ROFF_IX,
159 ROFF_kern,
160 ROFF_kernafter,
161 ROFF_kernbefore,
162 ROFF_kernpair,
163 ROFF_lc,
164 ROFF_lc_ctype,
165 ROFF_lds,
166 ROFF_length,
167 ROFF_letadj,
168 ROFF_lf,
169 ROFF_lg,
170 ROFF_lhang,
171 ROFF_linetabs,
172 /* MAN_ll, MDOC_ll */
173 ROFF_lnr,
174 ROFF_lnrf,
175 ROFF_lpfx,
176 ROFF_ls,
177 ROFF_lsm,
178 ROFF_lt,
179 ROFF_mc,
180 ROFF_mediasize,
181 ROFF_minss,
182 ROFF_mk,
183 ROFF_mso,
184 ROFF_na,
185 ROFF_ne,
186 /* MAN_nf; ignored in mdoc(7) */
187 ROFF_nh,
188 ROFF_nhychar,
189 ROFF_nm,
190 ROFF_nn,
191 ROFF_nop,
192 ROFF_nr,
193 ROFF_nrf,
194 ROFF_nroff,
195 ROFF_ns,
196 ROFF_nx,
197 ROFF_open,
198 ROFF_opena,
199 ROFF_os,
200 ROFF_output,
201 ROFF_padj,
202 ROFF_papersize,
203 ROFF_pc,
204 ROFF_pev,
205 ROFF_pi,
206 ROFF_PI,
207 ROFF_pl,
208 ROFF_pm,
209 ROFF_pn,
210 ROFF_pnr,
211 ROFF_po,
212 ROFF_ps,
213 ROFF_psbb,
214 ROFF_pshape,
215 ROFF_pso,
216 ROFF_ptr,
217 ROFF_pvs,
218 ROFF_rchar,
219 ROFF_rd,
220 ROFF_recursionlimit,
221 ROFF_return,
222 ROFF_rfschar,
223 ROFF_rhang,
224 ROFF_rj,
225 ROFF_rm,
226 ROFF_rn,
227 ROFF_rnn,
228 ROFF_rr,
229 ROFF_rs,
230 ROFF_rt,
231 ROFF_schar,
232 ROFF_sentchar,
233 ROFF_shc,
234 ROFF_shift,
235 ROFF_sizes,
236 ROFF_so,
237 /* MAN_sp, MDOC_sp */
238 ROFF_spacewidth,
239 ROFF_special,
240 ROFF_spreadwarn,
241 ROFF_ss,
242 ROFF_sty,
243 ROFF_substring,
244 ROFF_sv,
245 ROFF_sy,
246 ROFF_T_,
247 ROFF_ta,
248 ROFF_tc,
249 ROFF_TE,
250 ROFF_TH,
251 ROFF_ti,
252 ROFF_tkf,
253 ROFF_tl,
254 ROFF_tm,
255 ROFF_tm1,
256 ROFF_tmc,
257 ROFF_tr,
258 ROFF_track,
259 ROFF_transchar,
260 ROFF_trf,
261 ROFF_trimat,
262 ROFF_trin,
263 ROFF_trnt,
264 ROFF_troff,
265 ROFF_TS,
266 ROFF_uf,
267 ROFF_ul,
268 ROFF_unformat,
269 ROFF_unwatch,
270 ROFF_unwatchn,
271 ROFF_vpt,
272 ROFF_vs,
273 ROFF_warn,
274 ROFF_warnscale,
275 ROFF_watch,
276 ROFF_watchlength,
277 ROFF_watchn,
278 ROFF_wh,
279 ROFF_while,
280 ROFF_write,
281 ROFF_writec,
282 ROFF_writem,
283 ROFF_xflag,
284 ROFF_cblock,
285 ROFF_USERDEF,
286 ROFF_MAX
287 };
288
289 /*
290 * An incredibly-simple string buffer.
291 */
292 struct roffstr {
293 char *p; /* nil-terminated buffer */
294 size_t sz; /* saved strlen(p) */
295 };
296
297 /*
298 * A key-value roffstr pair as part of a singly-linked list.
299 */
300 struct roffkv {
301 struct roffstr key;
302 struct roffstr val;
303 struct roffkv *next; /* next in list */
304 };
305
306 /*
307 * A single number register as part of a singly-linked list.
308 */
309 struct roffreg {
310 struct roffstr key;
311 int val;
312 struct roffreg *next;
313 };
314
315 struct roff {
316 struct mparse *parse; /* parse point */
317 struct roffnode *last; /* leaf of stack */
318 int *rstack; /* stack of inverted `ie' values */
319 struct roffreg *regtab; /* number registers */
320 struct roffkv *strtab; /* user-defined strings & macros */
321 struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
322 struct roffstr *xtab; /* single-byte trans table (`tr') */
323 const char *current_string; /* value of last called user macro */
324 struct tbl_node *first_tbl; /* first table parsed */
325 struct tbl_node *last_tbl; /* last table parsed */
326 struct tbl_node *tbl; /* current table being parsed */
327 struct eqn_node *last_eqn; /* last equation parsed */
328 struct eqn_node *first_eqn; /* first equation parsed */
329 struct eqn_node *eqn; /* current equation being parsed */
330 int eqn_inline; /* current equation is inline */
331 int options; /* parse options */
332 int rstacksz; /* current size limit of rstack */
333 int rstackpos; /* position in rstack */
334 int format; /* current file in mdoc or man format */
335 int argc; /* number of args of the last macro */
336 char control; /* control character */
337 };
338
339 struct roffnode {
340 enum rofft tok; /* type of node */
341 struct roffnode *parent; /* up one in stack */
342 int line; /* parse line */
343 int col; /* parse col */
344 char *name; /* node name, e.g. macro name */
345 char *end; /* end-rules: custom token */
346 int endspan; /* end-rules: next-line or infty */
347 int rule; /* current evaluation rule */
348 };
349
350 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
351 enum rofft tok, /* tok of macro */ \
352 struct buf *buf, /* input buffer */ \
353 int ln, /* parse line */ \
354 int ppos, /* original pos in buffer */ \
355 int pos, /* current pos in buffer */ \
356 int *offs /* reset offset of buffer data */
357
358 typedef enum rofferr (*roffproc)(ROFF_ARGS);
359
360 struct roffmac {
361 const char *name; /* macro name */
362 roffproc proc; /* process new macro */
363 roffproc text; /* process as child text of macro */
364 roffproc sub; /* process as child of macro */
365 int flags;
366 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
367 struct roffmac *next;
368 };
369
370 struct predef {
371 const char *name; /* predefined input name */
372 const char *str; /* replacement symbol */
373 };
374
375 #define PREDEF(__name, __str) \
376 { (__name), (__str) },
377
378 /* --- function prototypes ------------------------------------------------ */
379
380 static enum rofft roffhash_find(const char *, size_t);
381 static void roffhash_init(void);
382 static void roffnode_cleanscope(struct roff *);
383 static void roffnode_pop(struct roff *);
384 static void roffnode_push(struct roff *, enum rofft,
385 const char *, int, int);
386 static enum rofferr roff_block(ROFF_ARGS);
387 static enum rofferr roff_block_text(ROFF_ARGS);
388 static enum rofferr roff_block_sub(ROFF_ARGS);
389 static enum rofferr roff_brp(ROFF_ARGS);
390 static enum rofferr roff_cblock(ROFF_ARGS);
391 static enum rofferr roff_cc(ROFF_ARGS);
392 static void roff_ccond(struct roff *, int, int);
393 static enum rofferr roff_cond(ROFF_ARGS);
394 static enum rofferr roff_cond_text(ROFF_ARGS);
395 static enum rofferr roff_cond_sub(ROFF_ARGS);
396 static enum rofferr roff_ds(ROFF_ARGS);
397 static enum rofferr roff_eqndelim(struct roff *, struct buf *, int);
398 static int roff_evalcond(struct roff *r, int, char *, int *);
399 static int roff_evalnum(struct roff *, int,
400 const char *, int *, int *, int);
401 static int roff_evalpar(struct roff *, int,
402 const char *, int *, int *, int);
403 static int roff_evalstrcond(const char *, int *);
404 static void roff_free1(struct roff *);
405 static void roff_freereg(struct roffreg *);
406 static void roff_freestr(struct roffkv *);
407 static size_t roff_getname(struct roff *, char **, int, int);
408 static int roff_getnum(const char *, int *, int *, int);
409 static int roff_getop(const char *, int *, char *);
410 static int roff_getregn(const struct roff *,
411 const char *, size_t);
412 static int roff_getregro(const struct roff *,
413 const char *name);
414 static const char *roff_getstrn(const struct roff *,
415 const char *, size_t);
416 static int roff_hasregn(const struct roff *,
417 const char *, size_t);
418 static enum rofferr roff_insec(ROFF_ARGS);
419 static enum rofferr roff_it(ROFF_ARGS);
420 static enum rofferr roff_line_ignore(ROFF_ARGS);
421 static void roff_man_alloc1(struct roff_man *);
422 static void roff_man_free1(struct roff_man *);
423 static enum rofferr roff_nr(ROFF_ARGS);
424 static enum rofft roff_parse(struct roff *, char *, int *,
425 int, int);
426 static enum rofferr roff_parsetext(struct buf *, int, int *);
427 static enum rofferr roff_res(struct roff *, struct buf *, int, int);
428 static enum rofferr roff_rm(ROFF_ARGS);
429 static enum rofferr roff_rr(ROFF_ARGS);
430 static void roff_setstr(struct roff *,
431 const char *, const char *, int);
432 static void roff_setstrn(struct roffkv **, const char *,
433 size_t, const char *, size_t, int);
434 static enum rofferr roff_so(ROFF_ARGS);
435 static enum rofferr roff_tr(ROFF_ARGS);
436 static enum rofferr roff_Dd(ROFF_ARGS);
437 static enum rofferr roff_TH(ROFF_ARGS);
438 static enum rofferr roff_TE(ROFF_ARGS);
439 static enum rofferr roff_TS(ROFF_ARGS);
440 static enum rofferr roff_EQ(ROFF_ARGS);
441 static enum rofferr roff_EN(ROFF_ARGS);
442 static enum rofferr roff_T_(ROFF_ARGS);
443 static enum rofferr roff_unsupp(ROFF_ARGS);
444 static enum rofferr roff_userdef(ROFF_ARGS);
445
446 /* --- constant data ------------------------------------------------------ */
447
448 /* See roffhash_find() */
449
450 #define ASCII_HI 126
451 #define ASCII_LO 33
452 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
453
454 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
455 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
456
457 static struct roffmac *hash[HASHWIDTH];
458
459 static struct roffmac roffs[ROFF_MAX] = {
460 { "ab", roff_unsupp, NULL, NULL, 0, NULL },
461 { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
462 { "af", roff_line_ignore, NULL, NULL, 0, NULL },
463 { "aln", roff_unsupp, NULL, NULL, 0, NULL },
464 { "als", roff_unsupp, NULL, NULL, 0, NULL },
465 { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
466 { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
467 { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
468 { "ami1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
469 { "as", roff_ds, NULL, NULL, 0, NULL },
470 { "as1", roff_ds, NULL, NULL, 0, NULL },
471 { "asciify", roff_unsupp, NULL, NULL, 0, NULL },
472 { "backtrace", roff_line_ignore, NULL, NULL, 0, NULL },
473 { "bd", roff_line_ignore, NULL, NULL, 0, NULL },
474 { "bleedat", roff_line_ignore, NULL, NULL, 0, NULL },
475 { "blm", roff_unsupp, NULL, NULL, 0, NULL },
476 { "box", roff_unsupp, NULL, NULL, 0, NULL },
477 { "boxa", roff_unsupp, NULL, NULL, 0, NULL },
478 { "bp", roff_line_ignore, NULL, NULL, 0, NULL },
479 { "BP", roff_unsupp, NULL, NULL, 0, NULL },
480 { "break", roff_unsupp, NULL, NULL, 0, NULL },
481 { "breakchar", roff_line_ignore, NULL, NULL, 0, NULL },
482 { "brnl", roff_line_ignore, NULL, NULL, 0, NULL },
483 { "brp", roff_brp, NULL, NULL, 0, NULL },
484 { "brpnl", roff_line_ignore, NULL, NULL, 0, NULL },
485 { "c2", roff_unsupp, NULL, NULL, 0, NULL },
486 { "cc", roff_cc, NULL, NULL, 0, NULL },
487 { "ce", roff_line_ignore, NULL, NULL, 0, NULL },
488 { "cf", roff_insec, NULL, NULL, 0, NULL },
489 { "cflags", roff_line_ignore, NULL, NULL, 0, NULL },
490 { "ch", roff_line_ignore, NULL, NULL, 0, NULL },
491 { "char", roff_unsupp, NULL, NULL, 0, NULL },
492 { "chop", roff_unsupp, NULL, NULL, 0, NULL },
493 { "class", roff_line_ignore, NULL, NULL, 0, NULL },
494 { "close", roff_insec, NULL, NULL, 0, NULL },
495 { "CL", roff_unsupp, NULL, NULL, 0, NULL },
496 { "color", roff_line_ignore, NULL, NULL, 0, NULL },
497 { "composite", roff_unsupp, NULL, NULL, 0, NULL },
498 { "continue", roff_unsupp, NULL, NULL, 0, NULL },
499 { "cp", roff_line_ignore, NULL, NULL, 0, NULL },
500 { "cropat", roff_line_ignore, NULL, NULL, 0, NULL },
501 { "cs", roff_line_ignore, NULL, NULL, 0, NULL },
502 { "cu", roff_line_ignore, NULL, NULL, 0, NULL },
503 { "da", roff_unsupp, NULL, NULL, 0, NULL },
504 { "dch", roff_unsupp, NULL, NULL, 0, NULL },
505 { "Dd", roff_Dd, NULL, NULL, 0, NULL },
506 { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
507 { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
508 { "defcolor", roff_line_ignore, NULL, NULL, 0, NULL },
509 { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
510 { "dei1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
511 { "device", roff_unsupp, NULL, NULL, 0, NULL },
512 { "devicem", roff_unsupp, NULL, NULL, 0, NULL },
513 { "di", roff_unsupp, NULL, NULL, 0, NULL },
514 { "do", roff_unsupp, NULL, NULL, 0, NULL },
515 { "ds", roff_ds, NULL, NULL, 0, NULL },
516 { "ds1", roff_ds, NULL, NULL, 0, NULL },
517 { "dwh", roff_unsupp, NULL, NULL, 0, NULL },
518 { "dt", roff_unsupp, NULL, NULL, 0, NULL },
519 { "ec", roff_unsupp, NULL, NULL, 0, NULL },
520 { "ecr", roff_unsupp, NULL, NULL, 0, NULL },
521 { "ecs", roff_unsupp, NULL, NULL, 0, NULL },
522 { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
523 { "em", roff_unsupp, NULL, NULL, 0, NULL },
524 { "EN", roff_EN, NULL, NULL, 0, NULL },
525 { "eo", roff_unsupp, NULL, NULL, 0, NULL },
526 { "EP", roff_unsupp, NULL, NULL, 0, NULL },
527 { "EQ", roff_EQ, NULL, NULL, 0, NULL },
528 { "errprint", roff_line_ignore, NULL, NULL, 0, NULL },
529 { "ev", roff_unsupp, NULL, NULL, 0, NULL },
530 { "evc", roff_unsupp, NULL, NULL, 0, NULL },
531 { "ex", roff_unsupp, NULL, NULL, 0, NULL },
532 { "fallback", roff_line_ignore, NULL, NULL, 0, NULL },
533 { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
534 { "fc", roff_unsupp, NULL, NULL, 0, NULL },
535 { "fchar", roff_unsupp, NULL, NULL, 0, NULL },
536 { "fcolor", roff_line_ignore, NULL, NULL, 0, NULL },
537 { "fdeferlig", roff_line_ignore, NULL, NULL, 0, NULL },
538 { "feature", roff_line_ignore, NULL, NULL, 0, NULL },
539 { "fkern", roff_line_ignore, NULL, NULL, 0, NULL },
540 { "fl", roff_line_ignore, NULL, NULL, 0, NULL },
541 { "flig", roff_line_ignore, NULL, NULL, 0, NULL },
542 { "fp", roff_line_ignore, NULL, NULL, 0, NULL },
543 { "fps", roff_line_ignore, NULL, NULL, 0, NULL },
544 { "fschar", roff_unsupp, NULL, NULL, 0, NULL },
545 { "fspacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
546 { "fspecial", roff_line_ignore, NULL, NULL, 0, NULL },
547 { "ftr", roff_line_ignore, NULL, NULL, 0, NULL },
548 { "fzoom", roff_line_ignore, NULL, NULL, 0, NULL },
549 { "gcolor", roff_line_ignore, NULL, NULL, 0, NULL },
550 { "hc", roff_line_ignore, NULL, NULL, 0, NULL },
551 { "hcode", roff_line_ignore, NULL, NULL, 0, NULL },
552 { "hidechar", roff_line_ignore, NULL, NULL, 0, NULL },
553 { "hla", roff_line_ignore, NULL, NULL, 0, NULL },
554 { "hlm", roff_line_ignore, NULL, NULL, 0, NULL },
555 { "hpf", roff_line_ignore, NULL, NULL, 0, NULL },
556 { "hpfa", roff_line_ignore, NULL, NULL, 0, NULL },
557 { "hpfcode", roff_line_ignore, NULL, NULL, 0, NULL },
558 { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
559 { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
560 { "hylang", roff_line_ignore, NULL, NULL, 0, NULL },
561 { "hylen", roff_line_ignore, NULL, NULL, 0, NULL },
562 { "hym", roff_line_ignore, NULL, NULL, 0, NULL },
563 { "hypp", roff_line_ignore, NULL, NULL, 0, NULL },
564 { "hys", roff_line_ignore, NULL, NULL, 0, NULL },
565 { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
566 { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
567 { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
568 { "index", roff_unsupp, NULL, NULL, 0, NULL },
569 { "it", roff_it, NULL, NULL, 0, NULL },
570 { "itc", roff_unsupp, NULL, NULL, 0, NULL },
571 { "IX", roff_line_ignore, NULL, NULL, 0, NULL },
572 { "kern", roff_line_ignore, NULL, NULL, 0, NULL },
573 { "kernafter", roff_line_ignore, NULL, NULL, 0, NULL },
574 { "kernbefore", roff_line_ignore, NULL, NULL, 0, NULL },
575 { "kernpair", roff_line_ignore, NULL, NULL, 0, NULL },
576 { "lc", roff_unsupp, NULL, NULL, 0, NULL },
577 { "lc_ctype", roff_unsupp, NULL, NULL, 0, NULL },
578 { "lds", roff_unsupp, NULL, NULL, 0, NULL },
579 { "length", roff_unsupp, NULL, NULL, 0, NULL },
580 { "letadj", roff_line_ignore, NULL, NULL, 0, NULL },
581 { "lf", roff_insec, NULL, NULL, 0, NULL },
582 { "lg", roff_line_ignore, NULL, NULL, 0, NULL },
583 { "lhang", roff_line_ignore, NULL, NULL, 0, NULL },
584 { "linetabs", roff_unsupp, NULL, NULL, 0, NULL },
585 { "lnr", roff_unsupp, NULL, NULL, 0, NULL },
586 { "lnrf", roff_unsupp, NULL, NULL, 0, NULL },
587 { "lpfx", roff_unsupp, NULL, NULL, 0, NULL },
588 { "ls", roff_line_ignore, NULL, NULL, 0, NULL },
589 { "lsm", roff_unsupp, NULL, NULL, 0, NULL },
590 { "lt", roff_line_ignore, NULL, NULL, 0, NULL },
591 { "mc", roff_line_ignore, NULL, NULL, 0, NULL },
592 { "mediasize", roff_line_ignore, NULL, NULL, 0, NULL },
593 { "minss", roff_line_ignore, NULL, NULL, 0, NULL },
594 { "mk", roff_line_ignore, NULL, NULL, 0, NULL },
595 { "mso", roff_insec, NULL, NULL, 0, NULL },
596 { "na", roff_line_ignore, NULL, NULL, 0, NULL },
597 { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
598 { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
599 { "nhychar", roff_line_ignore, NULL, NULL, 0, NULL },
600 { "nm", roff_unsupp, NULL, NULL, 0, NULL },
601 { "nn", roff_unsupp, NULL, NULL, 0, NULL },
602 { "nop", roff_unsupp, NULL, NULL, 0, NULL },
603 { "nr", roff_nr, NULL, NULL, 0, NULL },
604 { "nrf", roff_unsupp, NULL, NULL, 0, NULL },
605 { "nroff", roff_line_ignore, NULL, NULL, 0, NULL },
606 { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
607 { "nx", roff_insec, NULL, NULL, 0, NULL },
608 { "open", roff_insec, NULL, NULL, 0, NULL },
609 { "opena", roff_insec, NULL, NULL, 0, NULL },
610 { "os", roff_line_ignore, NULL, NULL, 0, NULL },
611 { "output", roff_unsupp, NULL, NULL, 0, NULL },
612 { "padj", roff_line_ignore, NULL, NULL, 0, NULL },
613 { "papersize", roff_line_ignore, NULL, NULL, 0, NULL },
614 { "pc", roff_line_ignore, NULL, NULL, 0, NULL },
615 { "pev", roff_line_ignore, NULL, NULL, 0, NULL },
616 { "pi", roff_insec, NULL, NULL, 0, NULL },
617 { "PI", roff_unsupp, NULL, NULL, 0, NULL },
618 { "pl", roff_line_ignore, NULL, NULL, 0, NULL },
619 { "pm", roff_line_ignore, NULL, NULL, 0, NULL },
620 { "pn", roff_line_ignore, NULL, NULL, 0, NULL },
621 { "pnr", roff_line_ignore, NULL, NULL, 0, NULL },
622 { "po", roff_line_ignore, NULL, NULL, 0, NULL },
623 { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
624 { "psbb", roff_unsupp, NULL, NULL, 0, NULL },
625 { "pshape", roff_unsupp, NULL, NULL, 0, NULL },
626 { "pso", roff_insec, NULL, NULL, 0, NULL },
627 { "ptr", roff_line_ignore, NULL, NULL, 0, NULL },
628 { "pvs", roff_line_ignore, NULL, NULL, 0, NULL },
629 { "rchar", roff_unsupp, NULL, NULL, 0, NULL },
630 { "rd", roff_line_ignore, NULL, NULL, 0, NULL },
631 { "recursionlimit", roff_line_ignore, NULL, NULL, 0, NULL },
632 { "return", roff_unsupp, NULL, NULL, 0, NULL },
633 { "rfschar", roff_unsupp, NULL, NULL, 0, NULL },
634 { "rhang", roff_line_ignore, NULL, NULL, 0, NULL },
635 { "rj", roff_line_ignore, NULL, NULL, 0, NULL },
636 { "rm", roff_rm, NULL, NULL, 0, NULL },
637 { "rn", roff_unsupp, NULL, NULL, 0, NULL },
638 { "rnn", roff_unsupp, NULL, NULL, 0, NULL },
639 { "rr", roff_rr, NULL, NULL, 0, NULL },
640 { "rs", roff_line_ignore, NULL, NULL, 0, NULL },
641 { "rt", roff_line_ignore, NULL, NULL, 0, NULL },
642 { "schar", roff_unsupp, NULL, NULL, 0, NULL },
643 { "sentchar", roff_line_ignore, NULL, NULL, 0, NULL },
644 { "shc", roff_line_ignore, NULL, NULL, 0, NULL },
645 { "shift", roff_unsupp, NULL, NULL, 0, NULL },
646 { "sizes", roff_line_ignore, NULL, NULL, 0, NULL },
647 { "so", roff_so, NULL, NULL, 0, NULL },
648 { "spacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
649 { "special", roff_line_ignore, NULL, NULL, 0, NULL },
650 { "spreadwarn", roff_line_ignore, NULL, NULL, 0, NULL },
651 { "ss", roff_line_ignore, NULL, NULL, 0, NULL },
652 { "sty", roff_line_ignore, NULL, NULL, 0, NULL },
653 { "substring", roff_unsupp, NULL, NULL, 0, NULL },
654 { "sv", roff_line_ignore, NULL, NULL, 0, NULL },
655 { "sy", roff_insec, NULL, NULL, 0, NULL },
656 { "T&", roff_T_, NULL, NULL, 0, NULL },
657 { "ta", roff_unsupp, NULL, NULL, 0, NULL },
658 { "tc", roff_unsupp, NULL, NULL, 0, NULL },
659 { "TE", roff_TE, NULL, NULL, 0, NULL },
660 { "TH", roff_TH, NULL, NULL, 0, NULL },
661 { "ti", roff_unsupp, NULL, NULL, 0, NULL },
662 { "tkf", roff_line_ignore, NULL, NULL, 0, NULL },
663 { "tl", roff_unsupp, NULL, NULL, 0, NULL },
664 { "tm", roff_line_ignore, NULL, NULL, 0, NULL },
665 { "tm1", roff_line_ignore, NULL, NULL, 0, NULL },
666 { "tmc", roff_line_ignore, NULL, NULL, 0, NULL },
667 { "tr", roff_tr, NULL, NULL, 0, NULL },
668 { "track", roff_line_ignore, NULL, NULL, 0, NULL },
669 { "transchar", roff_line_ignore, NULL, NULL, 0, NULL },
670 { "trf", roff_insec, NULL, NULL, 0, NULL },
671 { "trimat", roff_line_ignore, NULL, NULL, 0, NULL },
672 { "trin", roff_unsupp, NULL, NULL, 0, NULL },
673 { "trnt", roff_unsupp, NULL, NULL, 0, NULL },
674 { "troff", roff_line_ignore, NULL, NULL, 0, NULL },
675 { "TS", roff_TS, NULL, NULL, 0, NULL },
676 { "uf", roff_line_ignore, NULL, NULL, 0, NULL },
677 { "ul", roff_line_ignore, NULL, NULL, 0, NULL },
678 { "unformat", roff_unsupp, NULL, NULL, 0, NULL },
679 { "unwatch", roff_line_ignore, NULL, NULL, 0, NULL },
680 { "unwatchn", roff_line_ignore, NULL, NULL, 0, NULL },
681 { "vpt", roff_line_ignore, NULL, NULL, 0, NULL },
682 { "vs", roff_line_ignore, NULL, NULL, 0, NULL },
683 { "warn", roff_line_ignore, NULL, NULL, 0, NULL },
684 { "warnscale", roff_line_ignore, NULL, NULL, 0, NULL },
685 { "watch", roff_line_ignore, NULL, NULL, 0, NULL },
686 { "watchlength", roff_line_ignore, NULL, NULL, 0, NULL },
687 { "watchn", roff_line_ignore, NULL, NULL, 0, NULL },
688 { "wh", roff_unsupp, NULL, NULL, 0, NULL },
689 { "while", roff_unsupp, NULL, NULL, 0, NULL },
690 { "write", roff_insec, NULL, NULL, 0, NULL },
691 { "writec", roff_insec, NULL, NULL, 0, NULL },
692 { "writem", roff_insec, NULL, NULL, 0, NULL },
693 { "xflag", roff_line_ignore, NULL, NULL, 0, NULL },
694 { ".", roff_cblock, NULL, NULL, 0, NULL },
695 { NULL, roff_userdef, NULL, NULL, 0, NULL },
696 };
697
698 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
699 const char *const __mdoc_reserved[] = {
700 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
701 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
702 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
703 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
704 "Dt", "Dv", "Dx", "D1",
705 "Ec", "Ed", "Ef", "Ek", "El", "Em",
706 "En", "Eo", "Er", "Es", "Ev", "Ex",
707 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
708 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
709 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
710 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
711 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
712 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
713 "Sc", "Sh", "Sm", "So", "Sq",
714 "Ss", "St", "Sx", "Sy",
715 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
716 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
717 "%P", "%Q", "%R", "%T", "%U", "%V",
718 NULL
719 };
720
721 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
722 const char *const __man_reserved[] = {
723 "AT", "B", "BI", "BR", "DT",
724 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
725 "LP", "OP", "P", "PD", "PP",
726 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
727 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
728 NULL
729 };
730
731 /* Array of injected predefined strings. */
732 #define PREDEFS_MAX 38
733 static const struct predef predefs[PREDEFS_MAX] = {
734 #include "predefs.in"
735 };
736
737 /* See roffhash_find() */
738 #define ROFF_HASH(p) (p[0] - ASCII_LO)
739
740 static int roffit_lines; /* number of lines to delay */
741 static char *roffit_macro; /* nil-terminated macro line */
742
743
744 /* --- request table ------------------------------------------------------ */
745
746 static void
747 roffhash_init(void)
748 {
749 struct roffmac *n;
750 int buc, i;
751
752 for (i = 0; i < (int)ROFF_USERDEF; i++) {
753 assert(roffs[i].name[0] >= ASCII_LO);
754 assert(roffs[i].name[0] <= ASCII_HI);
755
756 buc = ROFF_HASH(roffs[i].name);
757
758 if (NULL != (n = hash[buc])) {
759 for ( ; n->next; n = n->next)
760 /* Do nothing. */ ;
761 n->next = &roffs[i];
762 } else
763 hash[buc] = &roffs[i];
764 }
765 }
766
767 /*
768 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
769 * the nil-terminated string name could be found.
770 */
771 static enum rofft
772 roffhash_find(const char *p, size_t s)
773 {
774 int buc;
775 struct roffmac *n;
776
777 /*
778 * libroff has an extremely simple hashtable, for the time
779 * being, which simply keys on the first character, which must
780 * be printable, then walks a chain. It works well enough until
781 * optimised.
782 */
783
784 if (p[0] < ASCII_LO || p[0] > ASCII_HI)
785 return ROFF_MAX;
786
787 buc = ROFF_HASH(p);
788
789 if (NULL == (n = hash[buc]))
790 return ROFF_MAX;
791 for ( ; n; n = n->next)
792 if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
793 return (enum rofft)(n - roffs);
794
795 return ROFF_MAX;
796 }
797
798 /* --- stack of request blocks -------------------------------------------- */
799
800 /*
801 * Pop the current node off of the stack of roff instructions currently
802 * pending.
803 */
804 static void
805 roffnode_pop(struct roff *r)
806 {
807 struct roffnode *p;
808
809 assert(r->last);
810 p = r->last;
811
812 r->last = r->last->parent;
813 free(p->name);
814 free(p->end);
815 free(p);
816 }
817
818 /*
819 * Push a roff node onto the instruction stack. This must later be
820 * removed with roffnode_pop().
821 */
822 static void
823 roffnode_push(struct roff *r, enum rofft tok, const char *name,
824 int line, int col)
825 {
826 struct roffnode *p;
827
828 p = mandoc_calloc(1, sizeof(struct roffnode));
829 p->tok = tok;
830 if (name)
831 p->name = mandoc_strdup(name);
832 p->parent = r->last;
833 p->line = line;
834 p->col = col;
835 p->rule = p->parent ? p->parent->rule : 0;
836
837 r->last = p;
838 }
839
840 /* --- roff parser state data management ---------------------------------- */
841
842 static void
843 roff_free1(struct roff *r)
844 {
845 struct tbl_node *tbl;
846 struct eqn_node *e;
847 int i;
848
849 while (NULL != (tbl = r->first_tbl)) {
850 r->first_tbl = tbl->next;
851 tbl_free(tbl);
852 }
853 r->first_tbl = r->last_tbl = r->tbl = NULL;
854
855 while (NULL != (e = r->first_eqn)) {
856 r->first_eqn = e->next;
857 eqn_free(e);
858 }
859 r->first_eqn = r->last_eqn = r->eqn = NULL;
860
861 while (r->last)
862 roffnode_pop(r);
863
864 free (r->rstack);
865 r->rstack = NULL;
866 r->rstacksz = 0;
867 r->rstackpos = -1;
868
869 roff_freereg(r->regtab);
870 r->regtab = NULL;
871
872 roff_freestr(r->strtab);
873 roff_freestr(r->xmbtab);
874 r->strtab = r->xmbtab = NULL;
875
876 if (r->xtab)
877 for (i = 0; i < 128; i++)
878 free(r->xtab[i].p);
879 free(r->xtab);
880 r->xtab = NULL;
881 }
882
883 void
884 roff_reset(struct roff *r)
885 {
886
887 roff_free1(r);
888 r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
889 r->control = 0;
890 }
891
892 void
893 roff_free(struct roff *r)
894 {
895
896 roff_free1(r);
897 free(r);
898 }
899
900 struct roff *
901 roff_alloc(struct mparse *parse, int options)
902 {
903 struct roff *r;
904
905 r = mandoc_calloc(1, sizeof(struct roff));
906 r->parse = parse;
907 r->options = options;
908 r->format = options & (MPARSE_MDOC | MPARSE_MAN);
909 r->rstackpos = -1;
910
911 roffhash_init();
912
913 return r;
914 }
915
916 /* --- syntax tree state data management ---------------------------------- */
917
918 static void
919 roff_man_free1(struct roff_man *man)
920 {
921
922 if (man->first != NULL)
923 roff_node_delete(man, man->first);
924 free(man->meta.msec);
925 free(man->meta.vol);
926 free(man->meta.os);
927 free(man->meta.arch);
928 free(man->meta.title);
929 free(man->meta.name);
930 free(man->meta.date);
931 }
932
933 static void
934 roff_man_alloc1(struct roff_man *man)
935 {
936
937 memset(&man->meta, 0, sizeof(man->meta));
938 man->first = mandoc_calloc(1, sizeof(*man->first));
939 man->first->type = ROFFT_ROOT;
940 man->last = man->first;
941 man->last_es = NULL;
942 man->flags = 0;
943 man->macroset = MACROSET_NONE;
944 man->lastsec = man->lastnamed = SEC_NONE;
945 man->next = ROFF_NEXT_CHILD;
946 }
947
948 void
949 roff_man_reset(struct roff_man *man)
950 {
951
952 roff_man_free1(man);
953 roff_man_alloc1(man);
954 }
955
956 void
957 roff_man_free(struct roff_man *man)
958 {
959
960 roff_man_free1(man);
961 free(man);
962 }
963
964 struct roff_man *
965 roff_man_alloc(struct roff *roff, struct mparse *parse,
966 const char *defos, int quick)
967 {
968 struct roff_man *man;
969
970 man = mandoc_calloc(1, sizeof(*man));
971 man->parse = parse;
972 man->roff = roff;
973 man->defos = defos;
974 man->quick = quick;
975 roff_man_alloc1(man);
976 return man;
977 }
978
979 /* --- syntax tree handling ----------------------------------------------- */
980
981 struct roff_node *
982 roff_node_alloc(struct roff_man *man, int line, int pos,
983 enum roff_type type, int tok)
984 {
985 struct roff_node *n;
986
987 n = mandoc_calloc(1, sizeof(*n));
988 n->line = line;
989 n->pos = pos;
990 n->tok = tok;
991 n->type = type;
992 n->sec = man->lastsec;
993
994 if (man->flags & MDOC_SYNOPSIS)
995 n->flags |= MDOC_SYNPRETTY;
996 else
997 n->flags &= ~MDOC_SYNPRETTY;
998 if (man->flags & MDOC_NEWLINE)
999 n->flags |= MDOC_LINE;
1000 man->flags &= ~MDOC_NEWLINE;
1001
1002 return n;
1003 }
1004
1005 void
1006 roff_node_append(struct roff_man *man, struct roff_node *n)
1007 {
1008
1009 switch (man->next) {
1010 case ROFF_NEXT_SIBLING:
1011 if (man->last->next != NULL) {
1012 n->next = man->last->next;
1013 man->last->next->prev = n;
1014 } else
1015 man->last->parent->last = n;
1016 man->last->next = n;
1017 n->prev = man->last;
1018 n->parent = man->last->parent;
1019 break;
1020 case ROFF_NEXT_CHILD:
1021 man->last->child = n;
1022 n->parent = man->last;
1023 n->parent->last = n;
1024 break;
1025 default:
1026 abort();
1027 }
1028 man->last = n;
1029
1030 switch (n->type) {
1031 case ROFFT_HEAD:
1032 n->parent->head = n;
1033 break;
1034 case ROFFT_BODY:
1035 if (n->end != ENDBODY_NOT)
1036 return;
1037 n->parent->body = n;
1038 break;
1039 case ROFFT_TAIL:
1040 n->parent->tail = n;
1041 break;
1042 default:
1043 return;
1044 }
1045
1046 /*
1047 * Copy over the normalised-data pointer of our parent. Not
1048 * everybody has one, but copying a null pointer is fine.
1049 */
1050
1051 n->norm = n->parent->norm;
1052 assert(n->parent->type == ROFFT_BLOCK);
1053 }
1054
1055 void
1056 roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
1057 {
1058 struct roff_node *n;
1059
1060 n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
1061 n->string = roff_strdup(man->roff, word);
1062 roff_node_append(man, n);
1063 if (man->macroset == MACROSET_MDOC)
1064 n->flags |= MDOC_VALID | MDOC_ENDED;
1065 else
1066 n->flags |= MAN_VALID;
1067 man->next = ROFF_NEXT_SIBLING;
1068 }
1069
1070 void
1071 roff_word_append(struct roff_man *man, const char *word)
1072 {
1073 struct roff_node *n;
1074 char *addstr, *newstr;
1075
1076 n = man->last;
1077 addstr = roff_strdup(man->roff, word);
1078 mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1079 free(addstr);
1080 free(n->string);
1081 n->string = newstr;
1082 man->next = ROFF_NEXT_SIBLING;
1083 }
1084
1085 void
1086 roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1087 {
1088 struct roff_node *n;
1089
1090 n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
1091 roff_node_append(man, n);
1092 man->next = ROFF_NEXT_CHILD;
1093 }
1094
1095 struct roff_node *
1096 roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
1097 {
1098 struct roff_node *n;
1099
1100 n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
1101 roff_node_append(man, n);
1102 man->next = ROFF_NEXT_CHILD;
1103 return n;
1104 }
1105
1106 struct roff_node *
1107 roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
1108 {
1109 struct roff_node *n;
1110
1111 n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
1112 roff_node_append(man, n);
1113 man->next = ROFF_NEXT_CHILD;
1114 return n;
1115 }
1116
1117 struct roff_node *
1118 roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
1119 {
1120 struct roff_node *n;
1121
1122 n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
1123 roff_node_append(man, n);
1124 man->next = ROFF_NEXT_CHILD;
1125 return n;
1126 }
1127
1128 void
1129 roff_addeqn(struct roff_man *man, const struct eqn *eqn)
1130 {
1131 struct roff_node *n;
1132
1133 n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
1134 n->eqn = eqn;
1135 if (eqn->ln > man->last->line)
1136 n->flags |= MDOC_LINE;
1137 roff_node_append(man, n);
1138 man->next = ROFF_NEXT_SIBLING;
1139 }
1140
1141 void
1142 roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
1143 {
1144 struct roff_node *n;
1145
1146 if (man->macroset == MACROSET_MAN)
1147 man_breakscope(man, TOKEN_NONE);
1148 n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
1149 n->span = tbl;
1150 roff_node_append(man, n);
1151 if (man->macroset == MACROSET_MDOC)
1152 n->flags |= MDOC_VALID | MDOC_ENDED;
1153 else
1154 n->flags |= MAN_VALID;
1155 man->next = ROFF_NEXT_SIBLING;
1156 }
1157
1158 void
1159 roff_node_unlink(struct roff_man *man, struct roff_node *n)
1160 {
1161
1162 /* Adjust siblings. */
1163
1164 if (n->prev)
1165 n->prev->next = n->next;
1166 if (n->next)
1167 n->next->prev = n->prev;
1168
1169 /* Adjust parent. */
1170
1171 if (n->parent != NULL) {
1172 if (n->parent->child == n)
1173 n->parent->child = n->next;
1174 if (n->parent->last == n)
1175 n->parent->last = n->prev;
1176 }
1177
1178 /* Adjust parse point. */
1179
1180 if (man == NULL)
1181 return;
1182 if (man->last == n) {
1183 if (n->prev == NULL) {
1184 man->last = n->parent;
1185 man->next = ROFF_NEXT_CHILD;
1186 } else {
1187 man->last = n->prev;
1188 man->next = ROFF_NEXT_SIBLING;
1189 }
1190 }
1191 if (man->first == n)
1192 man->first = NULL;
1193 }
1194
1195 void
1196 roff_node_free(struct roff_node *n)
1197 {
1198
1199 if (n->args != NULL)
1200 mdoc_argv_free(n->args);
1201 if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
1202 free(n->norm);
1203 free(n->string);
1204 free(n);
1205 }
1206
1207 void
1208 roff_node_delete(struct roff_man *man, struct roff_node *n)
1209 {
1210
1211 while (n->child != NULL)
1212 roff_node_delete(man, n->child);
1213 roff_node_unlink(man, n);
1214 roff_node_free(n);
1215 }
1216
1217 void
1218 deroff(char **dest, const struct roff_node *n)
1219 {
1220 char *cp;
1221 size_t sz;
1222
1223 if (n->type != ROFFT_TEXT) {
1224 for (n = n->child; n != NULL; n = n->next)
1225 deroff(dest, n);
1226 return;
1227 }
1228
1229 /* Skip leading whitespace and escape sequences. */
1230
1231 cp = n->string;
1232 while (*cp != '\0') {
1233 if ('\\' == *cp) {
1234 cp++;
1235 mandoc_escape((void *)&cp, NULL, NULL);
1236 } else if (isspace((unsigned char)*cp))
1237 cp++;
1238 else
1239 break;
1240 }
1241
1242 /* Skip trailing whitespace. */
1243
1244 for (sz = strlen(cp); sz; sz--)
1245 if ( ! isspace((unsigned char)cp[sz-1]))
1246 break;
1247
1248 /* Skip empty strings. */
1249
1250 if (sz == 0)
1251 return;
1252
1253 if (*dest == NULL) {
1254 *dest = mandoc_strndup(cp, sz);
1255 return;
1256 }
1257
1258 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
1259 free(*dest);
1260 *dest = cp;
1261 }
1262
1263 /* --- main functions of the roff parser ---------------------------------- */
1264
1265 /*
1266 * In the current line, expand escape sequences that tend to get
1267 * used in numerical expressions and conditional requests.
1268 * Also check the syntax of the remaining escape sequences.
1269 */
1270 static enum rofferr
1271 roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1272 {
1273 char ubuf[24]; /* buffer to print the number */
1274 const char *start; /* start of the string to process */
1275 char *stesc; /* start of an escape sequence ('\\') */
1276 const char *stnam; /* start of the name, after "[(*" */
1277 const char *cp; /* end of the name, e.g. before ']' */
1278 const char *res; /* the string to be substituted */
1279 char *nbuf; /* new buffer to copy buf->buf to */
1280 size_t maxl; /* expected length of the escape name */
1281 size_t naml; /* actual length of the escape name */
1282 enum mandoc_esc esc; /* type of the escape sequence */
1283 int inaml; /* length returned from mandoc_escape() */
1284 int expand_count; /* to avoid infinite loops */
1285 int npos; /* position in numeric expression */
1286 int arg_complete; /* argument not interrupted by eol */
1287 char term; /* character terminating the escape */
1288
1289 expand_count = 0;
1290 start = buf->buf + pos;
1291 stesc = strchr(start, '\0') - 1;
1292 while (stesc-- > start) {
1293
1294 /* Search backwards for the next backslash. */
1295
1296 if (*stesc != '\\')
1297 continue;
1298
1299 /* If it is escaped, skip it. */
1300
1301 for (cp = stesc - 1; cp >= start; cp--)
1302 if (*cp != '\\')
1303 break;
1304
1305 if ((stesc - cp) % 2 == 0) {
1306 stesc = __UNCONST(cp);
1307 continue;
1308 }
1309
1310 /* Decide whether to expand or to check only. */
1311
1312 term = '\0';
1313 cp = stesc + 1;
1314 switch (*cp) {
1315 case '*':
1316 res = NULL;
1317 break;
1318 case 'B':
1319 case 'w':
1320 term = cp[1];
1321 /* FALLTHROUGH */
1322 case 'n':
1323 res = ubuf;
1324 break;
1325 default:
1326 esc = mandoc_escape(&cp, &stnam, &inaml);
1327 if (esc == ESCAPE_ERROR ||
1328 (esc == ESCAPE_SPECIAL &&
1329 mchars_spec2cp(stnam, inaml) < 0))
1330 mandoc_vmsg(MANDOCERR_ESC_BAD,
1331 r->parse, ln, (int)(stesc - buf->buf),
1332 "%.*s", (int)(cp - stesc), stesc);
1333 continue;
1334 }
1335
1336 if (EXPAND_LIMIT < ++expand_count) {
1337 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1338 ln, (int)(stesc - buf->buf), NULL);
1339 return ROFF_IGN;
1340 }
1341
1342 /*
1343 * The third character decides the length
1344 * of the name of the string or register.
1345 * Save a pointer to the name.
1346 */
1347
1348 if (term == '\0') {
1349 switch (*++cp) {
1350 case '\0':
1351 maxl = 0;
1352 break;
1353 case '(':
1354 cp++;
1355 maxl = 2;
1356 break;
1357 case '[':
1358 cp++;
1359 term = ']';
1360 maxl = 0;
1361 break;
1362 default:
1363 maxl = 1;
1364 break;
1365 }
1366 } else {
1367 cp += 2;
1368 maxl = 0;
1369 }
1370 stnam = cp;
1371
1372 /* Advance to the end of the name. */
1373
1374 naml = 0;
1375 arg_complete = 1;
1376 while (maxl == 0 || naml < maxl) {
1377 if (*cp == '\0') {
1378 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1379 ln, (int)(stesc - buf->buf), stesc);
1380 arg_complete = 0;
1381 break;
1382 }
1383 if (maxl == 0 && *cp == term) {
1384 cp++;
1385 break;
1386 }
1387 if (*cp++ != '\\' || stesc[1] != 'w') {
1388 naml++;
1389 continue;
1390 }
1391 switch (mandoc_escape(&cp, NULL, NULL)) {
1392 case ESCAPE_SPECIAL:
1393 case ESCAPE_UNICODE:
1394 case ESCAPE_NUMBERED:
1395 case ESCAPE_OVERSTRIKE:
1396 naml++;
1397 break;
1398 default:
1399 break;
1400 }
1401 }
1402
1403 /*
1404 * Retrieve the replacement string; if it is
1405 * undefined, resume searching for escapes.
1406 */
1407
1408 switch (stesc[1]) {
1409 case '*':
1410 if (arg_complete)
1411 res = roff_getstrn(r, stnam, naml);
1412 break;
1413 case 'B':
1414 npos = 0;
1415 ubuf[0] = arg_complete &&
1416 roff_evalnum(r, ln, stnam, &npos,
1417 NULL, ROFFNUM_SCALE) &&
1418 stnam + npos + 1 == cp ? '1' : '0';
1419 ubuf[1] = '\0';
1420 break;
1421 case 'n':
1422 if (arg_complete)
1423 (void)snprintf(ubuf, sizeof(ubuf), "%d",
1424 roff_getregn(r, stnam, naml));
1425 else
1426 ubuf[0] = '\0';
1427 break;
1428 case 'w':
1429 /* use even incomplete args */
1430 (void)snprintf(ubuf, sizeof(ubuf), "%d",
1431 24 * (int)naml);
1432 break;
1433 }
1434
1435 if (res == NULL) {
1436 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1437 r->parse, ln, (int)(stesc - buf->buf),
1438 "%.*s", (int)naml, stnam);
1439 res = "";
1440 } else if (buf->sz + strlen(res) > SHRT_MAX) {
1441 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1442 ln, (int)(stesc - buf->buf), NULL);
1443 return ROFF_IGN;
1444 }
1445
1446 /* Replace the escape sequence by the string. */
1447
1448 *stesc = '\0';
1449 buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
1450 buf->buf, res, cp) + 1;
1451
1452 /* Prepare for the next replacement. */
1453
1454 start = nbuf + pos;
1455 stesc = nbuf + (stesc - buf->buf) + strlen(res);
1456 free(buf->buf);
1457 buf->buf = nbuf;
1458 }
1459 return ROFF_CONT;
1460 }
1461
1462 /*
1463 * Process text streams.
1464 */
1465 static enum rofferr
1466 roff_parsetext(struct buf *buf, int pos, int *offs)
1467 {
1468 size_t sz;
1469 const char *start;
1470 char *p;
1471 int isz;
1472 enum mandoc_esc esc;
1473
1474 /* Spring the input line trap. */
1475
1476 if (roffit_lines == 1) {
1477 isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1478 free(buf->buf);
1479 buf->buf = p;
1480 buf->sz = isz + 1;
1481 *offs = 0;
1482 free(roffit_macro);
1483 roffit_lines = 0;
1484 return ROFF_REPARSE;
1485 } else if (roffit_lines > 1)
1486 --roffit_lines;
1487
1488 /* Convert all breakable hyphens into ASCII_HYPH. */
1489
1490 start = p = buf->buf + pos;
1491
1492 while (*p != '\0') {
1493 sz = strcspn(p, "-\\");
1494 p += sz;
1495
1496 if (*p == '\0')
1497 break;
1498
1499 if (*p == '\\') {
1500 /* Skip over escapes. */
1501 p++;
1502 esc = mandoc_escape((const char **)(void *)&p, NULL, NULL);
1503 if (esc == ESCAPE_ERROR)
1504 break;
1505 while (*p == '-')
1506 p++;
1507 continue;
1508 } else if (p == start) {
1509 p++;
1510 continue;
1511 }
1512
1513 if (isalpha((unsigned char)p[-1]) &&
1514 isalpha((unsigned char)p[1]))
1515 *p = ASCII_HYPH;
1516 p++;
1517 }
1518 return ROFF_CONT;
1519 }
1520
1521 enum rofferr
1522 roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1523 {
1524 enum rofft t;
1525 enum rofferr e;
1526 int pos; /* parse point */
1527 int spos; /* saved parse point for messages */
1528 int ppos; /* original offset in buf->buf */
1529 int ctl; /* macro line (boolean) */
1530
1531 ppos = pos = *offs;
1532
1533 /* Handle in-line equation delimiters. */
1534
1535 if (r->tbl == NULL &&
1536 r->last_eqn != NULL && r->last_eqn->delim &&
1537 (r->eqn == NULL || r->eqn_inline)) {
1538 e = roff_eqndelim(r, buf, pos);
1539 if (e == ROFF_REPARSE)
1540 return e;
1541 assert(e == ROFF_CONT);
1542 }
1543
1544 /* Expand some escape sequences. */
1545
1546 e = roff_res(r, buf, ln, pos);
1547 if (e == ROFF_IGN)
1548 return e;
1549 assert(e == ROFF_CONT);
1550
1551 ctl = roff_getcontrol(r, buf->buf, &pos);
1552
1553 /*
1554 * First, if a scope is open and we're not a macro, pass the
1555 * text through the macro's filter.
1556 * Equations process all content themselves.
1557 * Tables process almost all content themselves, but we want
1558 * to warn about macros before passing it there.
1559 */
1560
1561 if (r->last != NULL && ! ctl) {
1562 t = r->last->tok;
1563 assert(roffs[t].text);
1564 e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1565 assert(e == ROFF_IGN || e == ROFF_CONT);
1566 if (e != ROFF_CONT)
1567 return e;
1568 }
1569 if (r->eqn != NULL)
1570 return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);
1571 if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1572 return tbl_read(r->tbl, ln, buf->buf, ppos);
1573 if ( ! ctl)
1574 return roff_parsetext(buf, pos, offs);
1575
1576 /* Skip empty request lines. */
1577
1578 if (buf->buf[pos] == '"') {
1579 mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
1580 ln, pos, NULL);
1581 return ROFF_IGN;
1582 } else if (buf->buf[pos] == '\0')
1583 return ROFF_IGN;
1584
1585 /*
1586 * If a scope is open, go to the child handler for that macro,
1587 * as it may want to preprocess before doing anything with it.
1588 * Don't do so if an equation is open.
1589 */
1590
1591 if (r->last) {
1592 t = r->last->tok;
1593 assert(roffs[t].sub);
1594 return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1595 }
1596
1597 /* No scope is open. This is a new request or macro. */
1598
1599 spos = pos;
1600 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1601
1602 /* Tables ignore most macros. */
1603
1604 if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
1605 mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
1606 ln, pos, buf->buf + spos);
1607 if (t == ROFF_TS)
1608 return ROFF_IGN;
1609 while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1610 pos++;
1611 while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')
1612 pos++;
1613 return tbl_read(r->tbl, ln, buf->buf, pos);
1614 }
1615
1616 /*
1617 * This is neither a roff request nor a user-defined macro.
1618 * Let the standard macro set parsers handle it.
1619 */
1620
1621 if (t == ROFF_MAX)
1622 return ROFF_CONT;
1623
1624 /* Execute a roff request or a user defined macro. */
1625
1626 assert(roffs[t].proc);
1627 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1628 }
1629
1630 void
1631 roff_endparse(struct roff *r)
1632 {
1633
1634 if (r->last)
1635 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1636 r->last->line, r->last->col,
1637 roffs[r->last->tok].name);
1638
1639 if (r->eqn) {
1640 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1641 r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1642 eqn_end(&r->eqn);
1643 }
1644
1645 if (r->tbl) {
1646 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1647 r->tbl->line, r->tbl->pos, "TS");
1648 tbl_end(&r->tbl);
1649 }
1650 }
1651
1652 /*
1653 * Parse a roff node's type from the input buffer. This must be in the
1654 * form of ".foo xxx" in the usual way.
1655 */
1656 static enum rofft
1657 roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1658 {
1659 char *cp;
1660 const char *mac;
1661 size_t maclen;
1662 enum rofft t;
1663
1664 cp = buf + *pos;
1665
1666 if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1667 return ROFF_MAX;
1668
1669 mac = cp;
1670 maclen = roff_getname(r, &cp, ln, ppos);
1671
1672 t = (r->current_string = roff_getstrn(r, mac, maclen))
1673 ? ROFF_USERDEF : roffhash_find(mac, maclen);
1674
1675 if (ROFF_MAX != t)
1676 *pos = cp - buf;
1677
1678 return t;
1679 }
1680
1681 /* --- handling of request blocks ----------------------------------------- */
1682
1683 static enum rofferr
1684 roff_cblock(ROFF_ARGS)
1685 {
1686
1687 /*
1688 * A block-close `..' should only be invoked as a child of an
1689 * ignore macro, otherwise raise a warning and just ignore it.
1690 */
1691
1692 if (r->last == NULL) {
1693 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1694 ln, ppos, "..");
1695 return ROFF_IGN;
1696 }
1697
1698 switch (r->last->tok) {
1699 case ROFF_am:
1700 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1701 case ROFF_ami:
1702 case ROFF_de:
1703 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1704 case ROFF_dei:
1705 case ROFF_ig:
1706 break;
1707 default:
1708 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1709 ln, ppos, "..");
1710 return ROFF_IGN;
1711 }
1712
1713 if (buf->buf[pos] != '\0')
1714 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1715 ".. %s", buf->buf + pos);
1716
1717 roffnode_pop(r);
1718 roffnode_cleanscope(r);
1719 return ROFF_IGN;
1720
1721 }
1722
1723 static void
1724 roffnode_cleanscope(struct roff *r)
1725 {
1726
1727 while (r->last) {
1728 if (--r->last->endspan != 0)
1729 break;
1730 roffnode_pop(r);
1731 }
1732 }
1733
1734 static void
1735 roff_ccond(struct roff *r, int ln, int ppos)
1736 {
1737
1738 if (NULL == r->last) {
1739 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1740 ln, ppos, "\\}");
1741 return;
1742 }
1743
1744 switch (r->last->tok) {
1745 case ROFF_el:
1746 case ROFF_ie:
1747 case ROFF_if:
1748 break;
1749 default:
1750 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1751 ln, ppos, "\\}");
1752 return;
1753 }
1754
1755 if (r->last->endspan > -1) {
1756 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1757 ln, ppos, "\\}");
1758 return;
1759 }
1760
1761 roffnode_pop(r);
1762 roffnode_cleanscope(r);
1763 return;
1764 }
1765
1766 static enum rofferr
1767 roff_block(ROFF_ARGS)
1768 {
1769 const char *name;
1770 char *iname, *cp;
1771 size_t namesz;
1772
1773 /* Ignore groff compatibility mode for now. */
1774
1775 if (tok == ROFF_de1)
1776 tok = ROFF_de;
1777 else if (tok == ROFF_dei1)
1778 tok = ROFF_dei;
1779 else if (tok == ROFF_am1)
1780 tok = ROFF_am;
1781 else if (tok == ROFF_ami1)
1782 tok = ROFF_ami;
1783
1784 /* Parse the macro name argument. */
1785
1786 cp = buf->buf + pos;
1787 if (tok == ROFF_ig) {
1788 iname = NULL;
1789 namesz = 0;
1790 } else {
1791 iname = cp;
1792 namesz = roff_getname(r, &cp, ln, ppos);
1793 iname[namesz] = '\0';
1794 }
1795
1796 /* Resolve the macro name argument if it is indirect. */
1797
1798 if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1799 if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1800 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1801 r->parse, ln, (int)(iname - buf->buf),
1802 "%.*s", (int)namesz, iname);
1803 namesz = 0;
1804 } else
1805 namesz = strlen(name);
1806 } else
1807 name = iname;
1808
1809 if (namesz == 0 && tok != ROFF_ig) {
1810 mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1811 ln, ppos, roffs[tok].name);
1812 return ROFF_IGN;
1813 }
1814
1815 roffnode_push(r, tok, name, ln, ppos);
1816
1817 /*
1818 * At the beginning of a `de' macro, clear the existing string
1819 * with the same name, if there is one. New content will be
1820 * appended from roff_block_text() in multiline mode.
1821 */
1822
1823 if (tok == ROFF_de || tok == ROFF_dei)
1824 roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1825
1826 if (*cp == '\0')
1827 return ROFF_IGN;
1828
1829 /* Get the custom end marker. */
1830
1831 iname = cp;
1832 namesz = roff_getname(r, &cp, ln, ppos);
1833
1834 /* Resolve the end marker if it is indirect. */
1835
1836 if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1837 if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1838 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1839 r->parse, ln, (int)(iname - buf->buf),
1840 "%.*s", (int)namesz, iname);
1841 namesz = 0;
1842 } else
1843 namesz = strlen(name);
1844 } else
1845 name = iname;
1846
1847 if (namesz)
1848 r->last->end = mandoc_strndup(name, namesz);
1849
1850 if (*cp != '\0')
1851 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1852 ln, pos, ".%s ... %s", roffs[tok].name, cp);
1853
1854 return ROFF_IGN;
1855 }
1856
1857 static enum rofferr
1858 roff_block_sub(ROFF_ARGS)
1859 {
1860 enum rofft t;
1861 int i, j;
1862
1863 /*
1864 * First check whether a custom macro exists at this level. If
1865 * it does, then check against it. This is some of groff's
1866 * stranger behaviours. If we encountered a custom end-scope
1867 * tag and that tag also happens to be a "real" macro, then we
1868 * need to try interpreting it again as a real macro. If it's
1869 * not, then return ignore. Else continue.
1870 */
1871
1872 if (r->last->end) {
1873 for (i = pos, j = 0; r->last->end[j]; j++, i++)
1874 if (buf->buf[i] != r->last->end[j])
1875 break;
1876
1877 if (r->last->end[j] == '\0' &&
1878 (buf->buf[i] == '\0' ||
1879 buf->buf[i] == ' ' ||
1880 buf->buf[i] == '\t')) {
1881 roffnode_pop(r);
1882 roffnode_cleanscope(r);
1883
1884 while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1885 i++;
1886
1887 pos = i;
1888 if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1889 ROFF_MAX)
1890 return ROFF_RERUN;
1891 return ROFF_IGN;
1892 }
1893 }
1894
1895 /*
1896 * If we have no custom end-query or lookup failed, then try
1897 * pulling it out of the hashtable.
1898 */
1899
1900 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1901
1902 if (t != ROFF_cblock) {
1903 if (tok != ROFF_ig)
1904 roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1905 return ROFF_IGN;
1906 }
1907
1908 assert(roffs[t].proc);
1909 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1910 }
1911
1912 static enum rofferr
1913 roff_block_text(ROFF_ARGS)
1914 {
1915
1916 if (tok != ROFF_ig)
1917 roff_setstr(r, r->last->name, buf->buf + pos, 2);
1918
1919 return ROFF_IGN;
1920 }
1921
1922 static enum rofferr
1923 roff_cond_sub(ROFF_ARGS)
1924 {
1925 enum rofft t;
1926 char *ep;
1927 int rr;
1928
1929 rr = r->last->rule;
1930 roffnode_cleanscope(r);
1931 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1932
1933 /*
1934 * Fully handle known macros when they are structurally
1935 * required or when the conditional evaluated to true.
1936 */
1937
1938 if ((t != ROFF_MAX) &&
1939 (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1940 assert(roffs[t].proc);
1941 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1942 }
1943
1944 /*
1945 * If `\}' occurs on a macro line without a preceding macro,
1946 * drop the line completely.
1947 */
1948
1949 ep = buf->buf + pos;
1950 if (ep[0] == '\\' && ep[1] == '}')
1951 rr = 0;
1952
1953 /* Always check for the closing delimiter `\}'. */
1954
1955 while ((ep = strchr(ep, '\\')) != NULL) {
1956 if (*(++ep) == '}') {
1957 *ep = '&';
1958 roff_ccond(r, ln, ep - buf->buf - 1);
1959 }
1960 if (*ep != '\0')
1961 ++ep;
1962 }
1963 return rr ? ROFF_CONT : ROFF_IGN;
1964 }
1965
1966 static enum rofferr
1967 roff_cond_text(ROFF_ARGS)
1968 {
1969 char *ep;
1970 int rr;
1971
1972 rr = r->last->rule;
1973 roffnode_cleanscope(r);
1974
1975 ep = buf->buf + pos;
1976 while ((ep = strchr(ep, '\\')) != NULL) {
1977 if (*(++ep) == '}') {
1978 *ep = '&';
1979 roff_ccond(r, ln, ep - buf->buf - 1);
1980 }
1981 if (*ep != '\0')
1982 ++ep;
1983 }
1984 return rr ? ROFF_CONT : ROFF_IGN;
1985 }
1986
1987 /* --- handling of numeric and conditional expressions -------------------- */
1988
1989 /*
1990 * Parse a single signed integer number. Stop at the first non-digit.
1991 * If there is at least one digit, return success and advance the
1992 * parse point, else return failure and let the parse point unchanged.
1993 * Ignore overflows, treat them just like the C language.
1994 */
1995 static int
1996 roff_getnum(const char *v, int *pos, int *res, int flags)
1997 {
1998 int myres, scaled, n, p;
1999
2000 if (NULL == res)
2001 res = &myres;
2002
2003 p = *pos;
2004 n = v[p] == '-';
2005 if (n || v[p] == '+')
2006 p++;
2007
2008 if (flags & ROFFNUM_WHITE)
2009 while (isspace((unsigned char)v[p]))
2010 p++;
2011
2012 for (*res = 0; isdigit((unsigned char)v[p]); p++)
2013 *res = 10 * *res + v[p] - '0';
2014 if (p == *pos + n)
2015 return 0;
2016
2017 if (n)
2018 *res = -*res;
2019
2020 /* Each number may be followed by one optional scaling unit. */
2021
2022 switch (v[p]) {
2023 case 'f':
2024 scaled = *res * 65536;
2025 break;
2026 case 'i':
2027 scaled = *res * 240;
2028 break;
2029 case 'c':
2030 scaled = *res * 240 / 2.54;
2031 break;
2032 case 'v':
2033 case 'P':
2034 scaled = *res * 40;
2035 break;
2036 case 'm':
2037 case 'n':
2038 scaled = *res * 24;
2039 break;
2040 case 'p':
2041 scaled = *res * 10 / 3;
2042 break;
2043 case 'u':
2044 scaled = *res;
2045 break;
2046 case 'M':
2047 scaled = *res * 6 / 25;
2048 break;
2049 default:
2050 scaled = *res;
2051 p--;
2052 break;
2053 }
2054 if (flags & ROFFNUM_SCALE)
2055 *res = scaled;
2056
2057 *pos = p + 1;
2058 return 1;
2059 }
2060
2061 /*
2062 * Evaluate a string comparison condition.
2063 * The first character is the delimiter.
2064 * Succeed if the string up to its second occurrence
2065 * matches the string up to its third occurence.
2066 * Advance the cursor after the third occurrence
2067 * or lacking that, to the end of the line.
2068 */
2069 static int
2070 roff_evalstrcond(const char *v, int *pos)
2071 {
2072 const char *s1, *s2, *s3;
2073 int match;
2074
2075 match = 0;
2076 s1 = v + *pos; /* initial delimiter */
2077 s2 = s1 + 1; /* for scanning the first string */
2078 s3 = strchr(s2, *s1); /* for scanning the second string */
2079
2080 if (NULL == s3) /* found no middle delimiter */
2081 goto out;
2082
2083 while ('\0' != *++s3) {
2084 if (*s2 != *s3) { /* mismatch */
2085 s3 = strchr(s3, *s1);
2086 break;
2087 }
2088 if (*s3 == *s1) { /* found the final delimiter */
2089 match = 1;
2090 break;
2091 }
2092 s2++;
2093 }
2094
2095 out:
2096 if (NULL == s3)
2097 s3 = strchr(s2, '\0');
2098 else if (*s3 != '\0')
2099 s3++;
2100 *pos = s3 - v;
2101 return match;
2102 }
2103
2104 /*
2105 * Evaluate an optionally negated single character, numerical,
2106 * or string condition.
2107 */
2108 static int
2109 roff_evalcond(struct roff *r, int ln, char *v, int *pos)
2110 {
2111 char *cp, *name;
2112 size_t sz;
2113 int number, savepos, wanttrue;
2114
2115 if ('!' == v[*pos]) {
2116 wanttrue = 0;
2117 (*pos)++;
2118 } else
2119 wanttrue = 1;
2120
2121 switch (v[*pos]) {
2122 case '\0':
2123 return 0;
2124 case 'n':
2125 case 'o':
2126 (*pos)++;
2127 return wanttrue;
2128 case 'c':
2129 case 'd':
2130 case 'e':
2131 case 't':
2132 case 'v':
2133 (*pos)++;
2134 return !wanttrue;
2135 case 'r':
2136 cp = name = v + ++*pos;
2137 sz = roff_getname(r, &cp, ln, *pos);
2138 *pos = cp - v;
2139 return (sz && roff_hasregn(r, name, sz)) == wanttrue;
2140 default:
2141 break;
2142 }
2143
2144 savepos = *pos;
2145 if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
2146 return (number > 0) == wanttrue;
2147 else if (*pos == savepos)
2148 return roff_evalstrcond(v, pos) == wanttrue;
2149 else
2150 return 0;
2151 }
2152
2153 static enum rofferr
2154 roff_line_ignore(ROFF_ARGS)
2155 {
2156
2157 return ROFF_IGN;
2158 }
2159
2160 static enum rofferr
2161 roff_insec(ROFF_ARGS)
2162 {
2163
2164 mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
2165 ln, ppos, roffs[tok].name);
2166 return ROFF_IGN;
2167 }
2168
2169 static enum rofferr
2170 roff_unsupp(ROFF_ARGS)
2171 {
2172
2173 mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
2174 ln, ppos, roffs[tok].name);
2175 return ROFF_IGN;
2176 }
2177
2178 static enum rofferr
2179 roff_cond(ROFF_ARGS)
2180 {
2181
2182 roffnode_push(r, tok, NULL, ln, ppos);
2183
2184 /*
2185 * An `.el' has no conditional body: it will consume the value
2186 * of the current rstack entry set in prior `ie' calls or
2187 * defaults to DENY.
2188 *
2189 * If we're not an `el', however, then evaluate the conditional.
2190 */
2191
2192 r->last->rule = tok == ROFF_el ?
2193 (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
2194 roff_evalcond(r, ln, buf->buf, &pos);
2195
2196 /*
2197 * An if-else will put the NEGATION of the current evaluated
2198 * conditional into the stack of rules.
2199 */
2200
2201 if (tok == ROFF_ie) {
2202 if (r->rstackpos + 1 == r->rstacksz) {
2203 r->rstacksz += 16;
2204 r->rstack = mandoc_reallocarray(r->rstack,
2205 r->rstacksz, sizeof(int));
2206 }
2207 r->rstack[++r->rstackpos] = !r->last->rule;
2208 }
2209
2210 /* If the parent has false as its rule, then so do we. */
2211
2212 if (r->last->parent && !r->last->parent->rule)
2213 r->last->rule = 0;
2214
2215 /*
2216 * Determine scope.
2217 * If there is nothing on the line after the conditional,
2218 * not even whitespace, use next-line scope.
2219 */
2220
2221 if (buf->buf[pos] == '\0') {
2222 r->last->endspan = 2;
2223 goto out;
2224 }
2225
2226 while (buf->buf[pos] == ' ')
2227 pos++;
2228
2229 /* An opening brace requests multiline scope. */
2230
2231 if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
2232 r->last->endspan = -1;
2233 pos += 2;
2234 while (buf->buf[pos] == ' ')
2235 pos++;
2236 goto out;
2237 }
2238
2239 /*
2240 * Anything else following the conditional causes
2241 * single-line scope. Warn if the scope contains
2242 * nothing but trailing whitespace.
2243 */
2244
2245 if (buf->buf[pos] == '\0')
2246 mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
2247 ln, ppos, roffs[tok].name);
2248
2249 r->last->endspan = 1;
2250
2251 out:
2252 *offs = pos;
2253 return ROFF_RERUN;
2254 }
2255
2256 static enum rofferr
2257 roff_ds(ROFF_ARGS)
2258 {
2259 char *string;
2260 const char *name;
2261 size_t namesz;
2262
2263 /* Ignore groff compatibility mode for now. */
2264
2265 if (tok == ROFF_ds1)
2266 tok = ROFF_ds;
2267 else if (tok == ROFF_as1)
2268 tok = ROFF_as;
2269
2270 /*
2271 * The first word is the name of the string.
2272 * If it is empty or terminated by an escape sequence,
2273 * abort the `ds' request without defining anything.
2274 */
2275
2276 name = string = buf->buf + pos;
2277 if (*name == '\0')
2278 return ROFF_IGN;
2279
2280 namesz = roff_getname(r, &string, ln, pos);
2281 if (name[namesz] == '\\')
2282 return ROFF_IGN;
2283
2284 /* Read past the initial double-quote, if any. */
2285 if (*string == '"')
2286 string++;
2287
2288 /* The rest is the value. */
2289 roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2290 ROFF_as == tok);
2291 return ROFF_IGN;
2292 }
2293
2294 /*
2295 * Parse a single operator, one or two characters long.
2296 * If the operator is recognized, return success and advance the
2297 * parse point, else return failure and let the parse point unchanged.
2298 */
2299 static int
2300 roff_getop(const char *v, int *pos, char *res)
2301 {
2302
2303 *res = v[*pos];
2304
2305 switch (*res) {
2306 case '+':
2307 case '-':
2308 case '*':
2309 case '/':
2310 case '%':
2311 case '&':
2312 case ':':
2313 break;
2314 case '<':
2315 switch (v[*pos + 1]) {
2316 case '=':
2317 *res = 'l';
2318 (*pos)++;
2319 break;
2320 case '>':
2321 *res = '!';
2322 (*pos)++;
2323 break;
2324 case '?':
2325 *res = 'i';
2326 (*pos)++;
2327 break;
2328 default:
2329 break;
2330 }
2331 break;
2332 case '>':
2333 switch (v[*pos + 1]) {
2334 case '=':
2335 *res = 'g';
2336 (*pos)++;
2337 break;
2338 case '?':
2339 *res = 'a';
2340 (*pos)++;
2341 break;
2342 default:
2343 break;
2344 }
2345 break;
2346 case '=':
2347 if ('=' == v[*pos + 1])
2348 (*pos)++;
2349 break;
2350 default:
2351 return 0;
2352 }
2353 (*pos)++;
2354
2355 return *res;
2356 }
2357
2358 /*
2359 * Evaluate either a parenthesized numeric expression
2360 * or a single signed integer number.
2361 */
2362 static int
2363 roff_evalpar(struct roff *r, int ln,
2364 const char *v, int *pos, int *res, int flags)
2365 {
2366
2367 if ('(' != v[*pos])
2368 return roff_getnum(v, pos, res, flags);
2369
2370 (*pos)++;
2371 if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
2372 return 0;
2373
2374 /*
2375 * Omission of the closing parenthesis
2376 * is an error in validation mode,
2377 * but ignored in evaluation mode.
2378 */
2379
2380 if (')' == v[*pos])
2381 (*pos)++;
2382 else if (NULL == res)
2383 return 0;
2384
2385 return 1;
2386 }
2387
2388 /*
2389 * Evaluate a complete numeric expression.
2390 * Proceed left to right, there is no concept of precedence.
2391 */
2392 static int
2393 roff_evalnum(struct roff *r, int ln, const char *v,
2394 int *pos, int *res, int flags)
2395 {
2396 int mypos, operand2;
2397 char operator;
2398
2399 if (NULL == pos) {
2400 mypos = 0;
2401 pos = &mypos;
2402 }
2403
2404 if (flags & ROFFNUM_WHITE)
2405 while (isspace((unsigned char)v[*pos]))
2406 (*pos)++;
2407
2408 if ( ! roff_evalpar(r, ln, v, pos, res, flags))
2409 return 0;
2410
2411 while (1) {
2412 if (flags & ROFFNUM_WHITE)
2413 while (isspace((unsigned char)v[*pos]))
2414 (*pos)++;
2415
2416 if ( ! roff_getop(v, pos, &operator))
2417 break;
2418
2419 if (flags & ROFFNUM_WHITE)
2420 while (isspace((unsigned char)v[*pos]))
2421 (*pos)++;
2422
2423 if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
2424 return 0;
2425
2426 if (flags & ROFFNUM_WHITE)
2427 while (isspace((unsigned char)v[*pos]))
2428 (*pos)++;
2429
2430 if (NULL == res)
2431 continue;
2432
2433 switch (operator) {
2434 case '+':
2435 *res += operand2;
2436 break;
2437 case '-':
2438 *res -= operand2;
2439 break;
2440 case '*':
2441 *res *= operand2;
2442 break;
2443 case '/':
2444 if (operand2 == 0) {
2445 mandoc_msg(MANDOCERR_DIVZERO,
2446 r->parse, ln, *pos, v);
2447 *res = 0;
2448 break;
2449 }
2450 *res /= operand2;
2451 break;
2452 case '%':
2453 if (operand2 == 0) {
2454 mandoc_msg(MANDOCERR_DIVZERO,
2455 r->parse, ln, *pos, v);
2456 *res = 0;
2457 break;
2458 }
2459 *res %= operand2;
2460 break;
2461 case '<':
2462 *res = *res < operand2;
2463 break;
2464 case '>':
2465 *res = *res > operand2;
2466 break;
2467 case 'l':
2468 *res = *res <= operand2;
2469 break;
2470 case 'g':
2471 *res = *res >= operand2;
2472 break;
2473 case '=':
2474 *res = *res == operand2;
2475 break;
2476 case '!':
2477 *res = *res != operand2;
2478 break;
2479 case '&':
2480 *res = *res && operand2;
2481 break;
2482 case ':':
2483 *res = *res || operand2;
2484 break;
2485 case 'i':
2486 if (operand2 < *res)
2487 *res = operand2;
2488 break;
2489 case 'a':
2490 if (operand2 > *res)
2491 *res = operand2;
2492 break;
2493 default:
2494 abort();
2495 }
2496 }
2497 return 1;
2498 }
2499
2500 /* --- register management ------------------------------------------------ */
2501
2502 void
2503 roff_setreg(struct roff *r, const char *name, int val, char sign)
2504 {
2505 struct roffreg *reg;
2506
2507 /* Search for an existing register with the same name. */
2508 reg = r->regtab;
2509
2510 while (reg && strcmp(name, reg->key.p))
2511 reg = reg->next;
2512
2513 if (NULL == reg) {
2514 /* Create a new register. */
2515 reg = mandoc_malloc(sizeof(struct roffreg));
2516 reg->key.p = mandoc_strdup(name);
2517 reg->key.sz = strlen(name);
2518 reg->val = 0;
2519 reg->next = r->regtab;
2520 r->regtab = reg;
2521 }
2522
2523 if ('+' == sign)
2524 reg->val += val;
2525 else if ('-' == sign)
2526 reg->val -= val;
2527 else
2528 reg->val = val;
2529 }
2530
2531 /*
2532 * Handle some predefined read-only number registers.
2533 * For now, return -1 if the requested register is not predefined;
2534 * in case a predefined read-only register having the value -1
2535 * were to turn up, another special value would have to be chosen.
2536 */
2537 static int
2538 roff_getregro(const struct roff *r, const char *name)
2539 {
2540
2541 switch (*name) {
2542 case '$': /* Number of arguments of the last macro evaluated. */
2543 return r->argc;
2544 case 'A': /* ASCII approximation mode is always off. */
2545 return 0;
2546 case 'g': /* Groff compatibility mode is always on. */
2547 return 1;
2548 case 'H': /* Fixed horizontal resolution. */
2549 return 24;
2550 case 'j': /* Always adjust left margin only. */
2551 return 0;
2552 case 'T': /* Some output device is always defined. */
2553 return 1;
2554 case 'V': /* Fixed vertical resolution. */
2555 return 40;
2556 default:
2557 return -1;
2558 }
2559 }
2560
2561 int
2562 roff_getreg(const struct roff *r, const char *name)
2563 {
2564 struct roffreg *reg;
2565 int val;
2566
2567 if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
2568 val = roff_getregro(r, name + 1);
2569 if (-1 != val)
2570 return val;
2571 }
2572
2573 for (reg = r->regtab; reg; reg = reg->next)
2574 if (0 == strcmp(name, reg->key.p))
2575 return reg->val;
2576
2577 return 0;
2578 }
2579
2580 static int
2581 roff_getregn(const struct roff *r, const char *name, size_t len)
2582 {
2583 struct roffreg *reg;
2584 int val;
2585
2586 if ('.' == name[0] && 2 == len) {
2587 val = roff_getregro(r, name + 1);
2588 if (-1 != val)
2589 return val;
2590 }
2591
2592 for (reg = r->regtab; reg; reg = reg->next)
2593 if (len == reg->key.sz &&
2594 0 == strncmp(name, reg->key.p, len))
2595 return reg->val;
2596
2597 return 0;
2598 }
2599
2600 static int
2601 roff_hasregn(const struct roff *r, const char *name, size_t len)
2602 {
2603 struct roffreg *reg;
2604 int val;
2605
2606 if ('.' == name[0] && 2 == len) {
2607 val = roff_getregro(r, name + 1);
2608 if (-1 != val)
2609 return 1;
2610 }
2611
2612 for (reg = r->regtab; reg; reg = reg->next)
2613 if (len == reg->key.sz &&
2614 0 == strncmp(name, reg->key.p, len))
2615 return 1;
2616
2617 return 0;
2618 }
2619
2620 static void
2621 roff_freereg(struct roffreg *reg)
2622 {
2623 struct roffreg *old_reg;
2624
2625 while (NULL != reg) {
2626 free(reg->key.p);
2627 old_reg = reg;
2628 reg = reg->next;
2629 free(old_reg);
2630 }
2631 }
2632
2633 static enum rofferr
2634 roff_nr(ROFF_ARGS)
2635 {
2636 char *key, *val;
2637 size_t keysz;
2638 int iv;
2639 char sign;
2640
2641 key = val = buf->buf + pos;
2642 if (*key == '\0')
2643 return ROFF_IGN;
2644
2645 keysz = roff_getname(r, &val, ln, pos);
2646 if (key[keysz] == '\\')
2647 return ROFF_IGN;
2648 key[keysz] = '\0';
2649
2650 sign = *val;
2651 if (sign == '+' || sign == '-')
2652 val++;
2653
2654 if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
2655 roff_setreg(r, key, iv, sign);
2656
2657 return ROFF_IGN;
2658 }
2659
2660 static enum rofferr
2661 roff_rr(ROFF_ARGS)
2662 {
2663 struct roffreg *reg, **prev;
2664 char *name, *cp;
2665 size_t namesz;
2666
2667 name = cp = buf->buf + pos;
2668 if (*name == '\0')
2669 return ROFF_IGN;
2670 namesz = roff_getname(r, &cp, ln, pos);
2671 name[namesz] = '\0';
2672
2673 prev = &r->regtab;
2674 while (1) {
2675 reg = *prev;
2676 if (reg == NULL || !strcmp(name, reg->key.p))
2677 break;
2678 prev = ®->next;
2679 }
2680 if (reg != NULL) {
2681 *prev = reg->next;
2682 free(reg->key.p);
2683 free(reg);
2684 }
2685 return ROFF_IGN;
2686 }
2687
2688 /* --- handler functions for roff requests -------------------------------- */
2689
2690 static enum rofferr
2691 roff_rm(ROFF_ARGS)
2692 {
2693 const char *name;
2694 char *cp;
2695 size_t namesz;
2696
2697 cp = buf->buf + pos;
2698 while (*cp != '\0') {
2699 name = cp;
2700 namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
2701 roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
2702 if (name[namesz] == '\\')
2703 break;
2704 }
2705 return ROFF_IGN;
2706 }
2707
2708 static enum rofferr
2709 roff_it(ROFF_ARGS)
2710 {
2711 int iv;
2712
2713 /* Parse the number of lines. */
2714
2715 if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
2716 mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
2717 ln, ppos, buf->buf + 1);
2718 return ROFF_IGN;
2719 }
2720
2721 while (isspace((unsigned char)buf->buf[pos]))
2722 pos++;
2723
2724 /*
2725 * Arm the input line trap.
2726 * Special-casing "an-trap" is an ugly workaround to cope
2727 * with DocBook stupidly fiddling with man(7) internals.
2728 */
2729
2730 roffit_lines = iv;
2731 roffit_macro = mandoc_strdup(iv != 1 ||
2732 strcmp(buf->buf + pos, "an-trap") ?
2733 buf->buf + pos : "br");
2734 return ROFF_IGN;
2735 }
2736
2737 static enum rofferr
2738 roff_Dd(ROFF_ARGS)
2739 {
2740 const char *const *cp;
2741
2742 if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
2743 for (cp = __mdoc_reserved; *cp; cp++)
2744 roff_setstr(r, *cp, NULL, 0);
2745
2746 if (r->format == 0)
2747 r->format = MPARSE_MDOC;
2748
2749 return ROFF_CONT;
2750 }
2751
2752 static enum rofferr
2753 roff_TH(ROFF_ARGS)
2754 {
2755 const char *const *cp;
2756
2757 if ((r->options & MPARSE_QUICK) == 0)
2758 for (cp = __man_reserved; *cp; cp++)
2759 roff_setstr(r, *cp, NULL, 0);
2760
2761 if (r->format == 0)
2762 r->format = MPARSE_MAN;
2763
2764 return ROFF_CONT;
2765 }
2766
2767 static enum rofferr
2768 roff_TE(ROFF_ARGS)
2769 {
2770
2771 if (NULL == r->tbl)
2772 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2773 ln, ppos, "TE");
2774 else if ( ! tbl_end(&r->tbl)) {
2775 free(buf->buf);
2776 buf->buf = mandoc_strdup(".sp");
2777 buf->sz = 4;
2778 return ROFF_REPARSE;
2779 }
2780 return ROFF_IGN;
2781 }
2782
2783 static enum rofferr
2784 roff_T_(ROFF_ARGS)
2785 {
2786
2787 if (NULL == r->tbl)
2788 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2789 ln, ppos, "T&");
2790 else
2791 tbl_restart(ppos, ln, r->tbl);
2792
2793 return ROFF_IGN;
2794 }
2795
2796 /*
2797 * Handle in-line equation delimiters.
2798 */
2799 static enum rofferr
2800 roff_eqndelim(struct roff *r, struct buf *buf, int pos)
2801 {
2802 char *cp1, *cp2;
2803 const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
2804
2805 /*
2806 * Outside equations, look for an opening delimiter.
2807 * If we are inside an equation, we already know it is
2808 * in-line, or this function wouldn't have been called;
2809 * so look for a closing delimiter.
2810 */
2811
2812 cp1 = buf->buf + pos;
2813 cp2 = strchr(cp1, r->eqn == NULL ?
2814 r->last_eqn->odelim : r->last_eqn->cdelim);
2815 if (cp2 == NULL)
2816 return ROFF_CONT;
2817
2818 *cp2++ = '\0';
2819 bef_pr = bef_nl = aft_nl = aft_pr = "";
2820
2821 /* Handle preceding text, protecting whitespace. */
2822
2823 if (*buf->buf != '\0') {
2824 if (r->eqn == NULL)
2825 bef_pr = "\\&";
2826 bef_nl = "\n";
2827 }
2828
2829 /*
2830 * Prepare replacing the delimiter with an equation macro
2831 * and drop leading white space from the equation.
2832 */
2833
2834 if (r->eqn == NULL) {
2835 while (*cp2 == ' ')
2836 cp2++;
2837 mac = ".EQ";
2838 } else
2839 mac = ".EN";
2840
2841 /* Handle following text, protecting whitespace. */
2842
2843 if (*cp2 != '\0') {
2844 aft_nl = "\n";
2845 if (r->eqn != NULL)
2846 aft_pr = "\\&";
2847 }
2848
2849 /* Do the actual replacement. */
2850
2851 buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
2852 bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
2853 free(buf->buf);
2854 buf->buf = cp1;
2855
2856 /* Toggle the in-line state of the eqn subsystem. */
2857
2858 r->eqn_inline = r->eqn == NULL;
2859 return ROFF_REPARSE;
2860 }
2861
2862 static enum rofferr
2863 roff_EQ(ROFF_ARGS)
2864 {
2865 struct eqn_node *e;
2866
2867 assert(r->eqn == NULL);
2868 e = eqn_alloc(ppos, ln, r->parse);
2869
2870 if (r->last_eqn) {
2871 r->last_eqn->next = e;
2872 e->delim = r->last_eqn->delim;
2873 e->odelim = r->last_eqn->odelim;
2874 e->cdelim = r->last_eqn->cdelim;
2875 } else
2876 r->first_eqn = r->last_eqn = e;
2877
2878 r->eqn = r->last_eqn = e;
2879
2880 if (buf->buf[pos] != '\0')
2881 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
2882 ".EQ %s", buf->buf + pos);
2883
2884 return ROFF_IGN;
2885 }
2886
2887 static enum rofferr
2888 roff_EN(ROFF_ARGS)
2889 {
2890
2891 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
2892 return ROFF_IGN;
2893 }
2894
2895 static enum rofferr
2896 roff_TS(ROFF_ARGS)
2897 {
2898 struct tbl_node *tbl;
2899
2900 if (r->tbl) {
2901 mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
2902 ln, ppos, "TS breaks TS");
2903 tbl_end(&r->tbl);
2904 }
2905
2906 tbl = tbl_alloc(ppos, ln, r->parse);
2907
2908 if (r->last_tbl)
2909 r->last_tbl->next = tbl;
2910 else
2911 r->first_tbl = r->last_tbl = tbl;
2912
2913 r->tbl = r->last_tbl = tbl;
2914 return ROFF_IGN;
2915 }
2916
2917 static enum rofferr
2918 roff_brp(ROFF_ARGS)
2919 {
2920
2921 buf->buf[pos - 1] = '\0';
2922 return ROFF_CONT;
2923 }
2924
2925 static enum rofferr
2926 roff_cc(ROFF_ARGS)
2927 {
2928 const char *p;
2929
2930 p = buf->buf + pos;
2931
2932 if (*p == '\0' || (r->control = *p++) == '.')
2933 r->control = 0;
2934
2935 if (*p != '\0')
2936 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
2937 ln, p - buf->buf, "cc ... %s", p);
2938
2939 return ROFF_IGN;
2940 }
2941
2942 static enum rofferr
2943 roff_tr(ROFF_ARGS)
2944 {
2945 const char *p, *first, *second;
2946 size_t fsz, ssz;
2947 enum mandoc_esc esc;
2948
2949 p = buf->buf + pos;
2950
2951 if (*p == '\0') {
2952 mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
2953 return ROFF_IGN;
2954 }
2955
2956 while (*p != '\0') {
2957 fsz = ssz = 1;
2958
2959 first = p++;
2960 if (*first == '\\') {
2961 esc = mandoc_escape(&p, NULL, NULL);
2962 if (esc == ESCAPE_ERROR) {
2963 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
2964 ln, (int)(p - buf->buf), first);
2965 return ROFF_IGN;
2966 }
2967 fsz = (size_t)(p - first);
2968 }
2969
2970 second = p++;
2971 if (*second == '\\') {
2972 esc = mandoc_escape(&p, NULL, NULL);
2973 if (esc == ESCAPE_ERROR) {
2974 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
2975 ln, (int)(p - buf->buf), second);
2976 return ROFF_IGN;
2977 }
2978 ssz = (size_t)(p - second);
2979 } else if (*second == '\0') {
2980 mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
2981 ln, first - buf->buf, "tr %s", first);
2982 second = " ";
2983 p--;
2984 }
2985
2986 if (fsz > 1) {
2987 roff_setstrn(&r->xmbtab, first, fsz,
2988 second, ssz, 0);
2989 continue;
2990 }
2991
2992 if (r->xtab == NULL)
2993 r->xtab = mandoc_calloc(128,
2994 sizeof(struct roffstr));
2995
2996 free(r->xtab[(int)*first].p);
2997 r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
2998 r->xtab[(int)*first].sz = ssz;
2999 }
3000
3001 return ROFF_IGN;
3002 }
3003
3004 static enum rofferr
3005 roff_so(ROFF_ARGS)
3006 {
3007 char *name, *cp;
3008
3009 name = buf->buf + pos;
3010 mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
3011
3012 /*
3013 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3014 * opening anything that's not in our cwd or anything beneath
3015 * it. Thus, explicitly disallow traversing up the file-system
3016 * or using absolute paths.
3017 */
3018
3019 if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
3020 mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
3021 ".so %s", name);
3022 buf->sz = mandoc_asprintf(&cp,
3023 ".sp\nSee the file %s.\n.sp", name) + 1;
3024 free(buf->buf);
3025 buf->buf = cp;
3026 *offs = 0;
3027 return ROFF_REPARSE;
3028 }
3029
3030 *offs = pos;
3031 return ROFF_SO;
3032 }
3033
3034 /* --- user defined strings and macros ------------------------------------ */
3035
3036 static enum rofferr
3037 roff_userdef(ROFF_ARGS)
3038 {
3039 const char *arg[9], *ap;
3040 char *cp, *n1, *n2;
3041 int i, ib, ie;
3042 size_t asz, rsz;
3043
3044 /*
3045 * Collect pointers to macro argument strings
3046 * and NUL-terminate them.
3047 */
3048
3049 r->argc = 0;
3050 cp = buf->buf + pos;
3051 for (i = 0; i < 9; i++) {
3052 if (*cp == '\0')
3053 arg[i] = "";
3054 else {
3055 arg[i] = mandoc_getarg(r->parse, &cp, ln, &pos);
3056 r->argc = i + 1;
3057 }
3058 }
3059
3060 /*
3061 * Expand macro arguments.
3062 */
3063
3064 buf->sz = strlen(r->current_string) + 1;
3065 n1 = cp = mandoc_malloc(buf->sz);
3066 memcpy(n1, r->current_string, buf->sz);
3067 while (*cp != '\0') {
3068
3069 /* Scan ahead for the next argument invocation. */
3070
3071 if (*cp++ != '\\')
3072 continue;
3073 if (*cp++ != '$')
3074 continue;
3075 if (*cp == '*') { /* \\$* inserts all arguments */
3076 ib = 0;
3077 ie = r->argc - 1;
3078 } else { /* \\$1 .. \\$9 insert one argument */
3079 ib = ie = *cp - '1';
3080 if (ib < 0 || ib > 8)
3081 continue;
3082 }
3083 cp -= 2;
3084
3085 /*
3086 * Determine the size of the expanded argument,
3087 * taking escaping of quotes into account.
3088 */
3089
3090 asz = ie > ib ? ie - ib : 0; /* for blanks */
3091 for (i = ib; i <= ie; i++) {
3092 for (ap = arg[i]; *ap != '\0'; ap++) {
3093 asz++;
3094 if (*ap == '"')
3095 asz += 3;
3096 }
3097 }
3098 if (asz != 3) {
3099
3100 /*
3101 * Determine the size of the rest of the
3102 * unexpanded macro, including the NUL.
3103 */
3104
3105 rsz = buf->sz - (cp - n1) - 3;
3106
3107 /*
3108 * When shrinking, move before
3109 * releasing the storage.
3110 */
3111
3112 if (asz < 3)
3113 memmove(cp + asz, cp + 3, rsz);
3114
3115 /*
3116 * Resize the storage for the macro
3117 * and readjust the parse pointer.
3118 */
3119
3120 buf->sz += asz - 3;
3121 n2 = mandoc_realloc(n1, buf->sz);
3122 cp = n2 + (cp - n1);
3123 n1 = n2;
3124
3125 /*
3126 * When growing, make room
3127 * for the expanded argument.
3128 */
3129
3130 if (asz > 3)
3131 memmove(cp + asz, cp + 3, rsz);
3132 }
3133
3134 /* Copy the expanded argument, escaping quotes. */
3135
3136 n2 = cp;
3137 for (i = ib; i <= ie; i++) {
3138 for (ap = arg[i]; *ap != '\0'; ap++) {
3139 if (*ap == '"') {
3140 memcpy(n2, "\\(dq", 4);
3141 n2 += 4;
3142 } else
3143 *n2++ = *ap;
3144 }
3145 if (i < ie)
3146 *n2++ = ' ';
3147 }
3148 }
3149
3150 /*
3151 * Replace the macro invocation
3152 * by the expanded macro.
3153 */
3154
3155 free(buf->buf);
3156 buf->buf = n1;
3157 *offs = 0;
3158
3159 return buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
3160 ROFF_REPARSE : ROFF_APPEND;
3161 }
3162
3163 static size_t
3164 roff_getname(struct roff *r, char **cpp, int ln, int pos)
3165 {
3166 char *name, *cp;
3167 size_t namesz;
3168
3169 name = *cpp;
3170 if ('\0' == *name)
3171 return 0;
3172
3173 /* Read until end of name and terminate it with NUL. */
3174 for (cp = name; 1; cp++) {
3175 if ('\0' == *cp || ' ' == *cp) {
3176 namesz = cp - name;
3177 break;
3178 }
3179 if ('\\' != *cp)
3180 continue;
3181 namesz = cp - name;
3182 if ('{' == cp[1] || '}' == cp[1])
3183 break;
3184 cp++;
3185 if ('\\' == *cp)
3186 continue;
3187 mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
3188 "%.*s", (int)(cp - name + 1), name);
3189 mandoc_escape((const char **)(void *)&cp, NULL, NULL);
3190 break;
3191 }
3192
3193 /* Read past spaces. */
3194 while (' ' == *cp)
3195 cp++;
3196
3197 *cpp = cp;
3198 return namesz;
3199 }
3200
3201 /*
3202 * Store *string into the user-defined string called *name.
3203 * To clear an existing entry, call with (*r, *name, NULL, 0).
3204 * append == 0: replace mode
3205 * append == 1: single-line append mode
3206 * append == 2: multiline append mode, append '\n' after each call
3207 */
3208 static void
3209 roff_setstr(struct roff *r, const char *name, const char *string,
3210 int append)
3211 {
3212
3213 roff_setstrn(&r->strtab, name, strlen(name), string,
3214 string ? strlen(string) : 0, append);
3215 }
3216
3217 static void
3218 roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
3219 const char *string, size_t stringsz, int append)
3220 {
3221 struct roffkv *n;
3222 char *c;
3223 int i;
3224 size_t oldch, newch;
3225
3226 /* Search for an existing string with the same name. */
3227 n = *r;
3228
3229 while (n && (namesz != n->key.sz ||
3230 strncmp(n->key.p, name, namesz)))
3231 n = n->next;
3232
3233 if (NULL == n) {
3234 /* Create a new string table entry. */
3235 n = mandoc_malloc(sizeof(struct roffkv));
3236 n->key.p = mandoc_strndup(name, namesz);
3237 n->key.sz = namesz;
3238 n->val.p = NULL;
3239 n->val.sz = 0;
3240 n->next = *r;
3241 *r = n;
3242 } else if (0 == append) {
3243 free(n->val.p);
3244 n->val.p = NULL;
3245 n->val.sz = 0;
3246 }
3247
3248 if (NULL == string)
3249 return;
3250
3251 /*
3252 * One additional byte for the '\n' in multiline mode,
3253 * and one for the terminating '\0'.
3254 */
3255 newch = stringsz + (1 < append ? 2u : 1u);
3256
3257 if (NULL == n->val.p) {
3258 n->val.p = mandoc_malloc(newch);
3259 *n->val.p = '\0';
3260 oldch = 0;
3261 } else {
3262 oldch = n->val.sz;
3263 n->val.p = mandoc_realloc(n->val.p, oldch + newch);
3264 }
3265
3266 /* Skip existing content in the destination buffer. */
3267 c = n->val.p + (int)oldch;
3268
3269 /* Append new content to the destination buffer. */
3270 i = 0;
3271 while (i < (int)stringsz) {
3272 /*
3273 * Rudimentary roff copy mode:
3274 * Handle escaped backslashes.
3275 */
3276 if ('\\' == string[i] && '\\' == string[i + 1])
3277 i++;
3278 *c++ = string[i++];
3279 }
3280
3281 /* Append terminating bytes. */
3282 if (1 < append)
3283 *c++ = '\n';
3284
3285 *c = '\0';
3286 n->val.sz = (int)(c - n->val.p);
3287 }
3288
3289 static const char *
3290 roff_getstrn(const struct roff *r, const char *name, size_t len)
3291 {
3292 const struct roffkv *n;
3293 int i;
3294
3295 for (n = r->strtab; n; n = n->next)
3296 if (0 == strncmp(name, n->key.p, len) &&
3297 '\0' == n->key.p[(int)len])
3298 return n->val.p;
3299
3300 for (i = 0; i < PREDEFS_MAX; i++)
3301 if (0 == strncmp(name, predefs[i].name, len) &&
3302 '\0' == predefs[i].name[(int)len])
3303 return predefs[i].str;
3304
3305 return NULL;
3306 }
3307
3308 static void
3309 roff_freestr(struct roffkv *r)
3310 {
3311 struct roffkv *n, *nn;
3312
3313 for (n = r; n; n = nn) {
3314 free(n->key.p);
3315 free(n->val.p);
3316 nn = n->next;
3317 free(n);
3318 }
3319 }
3320
3321 /* --- accessors and utility functions ------------------------------------ */
3322
3323 const struct tbl_span *
3324 roff_span(const struct roff *r)
3325 {
3326
3327 return r->tbl ? tbl_span(r->tbl) : NULL;
3328 }
3329
3330 const struct eqn *
3331 roff_eqn(const struct roff *r)
3332 {
3333
3334 return r->last_eqn ? &r->last_eqn->eqn : NULL;
3335 }
3336
3337 /*
3338 * Duplicate an input string, making the appropriate character
3339 * conversations (as stipulated by `tr') along the way.
3340 * Returns a heap-allocated string with all the replacements made.
3341 */
3342 char *
3343 roff_strdup(const struct roff *r, const char *p)
3344 {
3345 const struct roffkv *cp;
3346 char *res;
3347 const char *pp;
3348 size_t ssz, sz;
3349 enum mandoc_esc esc;
3350
3351 if (NULL == r->xmbtab && NULL == r->xtab)
3352 return mandoc_strdup(p);
3353 else if ('\0' == *p)
3354 return mandoc_strdup("");
3355
3356 /*
3357 * Step through each character looking for term matches
3358 * (remember that a `tr' can be invoked with an escape, which is
3359 * a glyph but the escape is multi-character).
3360 * We only do this if the character hash has been initialised
3361 * and the string is >0 length.
3362 */
3363
3364 res = NULL;
3365 ssz = 0;
3366
3367 while ('\0' != *p) {
3368 if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
3369 sz = r->xtab[(int)*p].sz;
3370 res = mandoc_realloc(res, ssz + sz + 1);
3371 memcpy(res + ssz, r->xtab[(int)*p].p, sz);
3372 ssz += sz;
3373 p++;
3374 continue;
3375 } else if ('\\' != *p) {
3376 res = mandoc_realloc(res, ssz + 2);
3377 res[ssz++] = *p++;
3378 continue;
3379 }
3380
3381 /* Search for term matches. */
3382 for (cp = r->xmbtab; cp; cp = cp->next)
3383 if (0 == strncmp(p, cp->key.p, cp->key.sz))
3384 break;
3385
3386 if (NULL != cp) {
3387 /*
3388 * A match has been found.
3389 * Append the match to the array and move
3390 * forward by its keysize.
3391 */
3392 res = mandoc_realloc(res,
3393 ssz + cp->val.sz + 1);
3394 memcpy(res + ssz, cp->val.p, cp->val.sz);
3395 ssz += cp->val.sz;
3396 p += (int)cp->key.sz;
3397 continue;
3398 }
3399
3400 /*
3401 * Handle escapes carefully: we need to copy
3402 * over just the escape itself, or else we might
3403 * do replacements within the escape itself.
3404 * Make sure to pass along the bogus string.
3405 */
3406 pp = p++;
3407 esc = mandoc_escape(&p, NULL, NULL);
3408 if (ESCAPE_ERROR == esc) {
3409 sz = strlen(pp);
3410 res = mandoc_realloc(res, ssz + sz + 1);
3411 memcpy(res + ssz, pp, sz);
3412 break;
3413 }
3414 /*
3415 * We bail out on bad escapes.
3416 * No need to warn: we already did so when
3417 * roff_res() was called.
3418 */
3419 sz = (int)(p - pp);
3420 res = mandoc_realloc(res, ssz + sz + 1);
3421 memcpy(res + ssz, pp, sz);
3422 ssz += sz;
3423 }
3424
3425 res[(int)ssz] = '\0';
3426 return res;
3427 }
3428
3429 int
3430 roff_getformat(const struct roff *r)
3431 {
3432
3433 return r->format;
3434 }
3435
3436 /*
3437 * Find out whether a line is a macro line or not.
3438 * If it is, adjust the current position and return one; if it isn't,
3439 * return zero and don't change the current position.
3440 * If the control character has been set with `.cc', then let that grain
3441 * precedence.
3442 * This is slighly contrary to groff, where using the non-breaking
3443 * control character when `cc' has been invoked will cause the
3444 * non-breaking macro contents to be printed verbatim.
3445 */
3446 int
3447 roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
3448 {
3449 int pos;
3450
3451 pos = *ppos;
3452
3453 if (0 != r->control && cp[pos] == r->control)
3454 pos++;
3455 else if (0 != r->control)
3456 return 0;
3457 else if ('\\' == cp[pos] && '.' == cp[pos + 1])
3458 pos += 2;
3459 else if ('.' == cp[pos] || '\'' == cp[pos])
3460 pos++;
3461 else
3462 return 0;
3463
3464 while (' ' == cp[pos] || '\t' == cp[pos])
3465 pos++;
3466
3467 *ppos = pos;
3468 return 1;
3469 }
3470