]> git.cameronkatri.com Git - mandoc.git/blob - roff.c
Fix another regression introduced in 1.11.7:
[mandoc.git] / roff.c
1 /* $Id: roff.c,v 1.170 2011/09/18 23:51:31 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "libroff.h"
29 #include "libmandoc.h"
30
31 /* Maximum number of nested if-else conditionals. */
32 #define RSTACK_MAX 128
33
34 /* Maximum number of string expansions per line, to break infinite loops. */
35 #define EXPAND_LIMIT 1000
36
37 enum rofft {
38 ROFF_ad,
39 ROFF_am,
40 ROFF_ami,
41 ROFF_am1,
42 ROFF_de,
43 ROFF_dei,
44 ROFF_de1,
45 ROFF_ds,
46 ROFF_el,
47 ROFF_hy,
48 ROFF_ie,
49 ROFF_if,
50 ROFF_ig,
51 ROFF_it,
52 ROFF_ne,
53 ROFF_nh,
54 ROFF_nr,
55 ROFF_ns,
56 ROFF_ps,
57 ROFF_rm,
58 ROFF_so,
59 ROFF_ta,
60 ROFF_tr,
61 ROFF_TS,
62 ROFF_TE,
63 ROFF_T_,
64 ROFF_EQ,
65 ROFF_EN,
66 ROFF_cblock,
67 ROFF_ccond,
68 ROFF_USERDEF,
69 ROFF_MAX
70 };
71
72 enum roffrule {
73 ROFFRULE_ALLOW,
74 ROFFRULE_DENY
75 };
76
77 /*
78 * A single register entity. If "set" is zero, the value of the
79 * register should be the default one, which is per-register.
80 * Registers are assumed to be unsigned ints for now.
81 */
82 struct reg {
83 int set; /* whether set or not */
84 unsigned int u; /* unsigned integer */
85 };
86
87 /*
88 * An incredibly-simple string buffer.
89 */
90 struct roffstr {
91 char *p; /* nil-terminated buffer */
92 size_t sz; /* saved strlen(p) */
93 };
94
95 /*
96 * A key-value roffstr pair as part of a singly-linked list.
97 */
98 struct roffkv {
99 struct roffstr key;
100 struct roffstr val;
101 struct roffkv *next; /* next in list */
102 };
103
104 struct roff {
105 struct mparse *parse; /* parse point */
106 struct roffnode *last; /* leaf of stack */
107 enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
108 int rstackpos; /* position in rstack */
109 struct reg regs[REG__MAX];
110 struct roffkv *strtab; /* user-defined strings & macros */
111 struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
112 struct roffstr *xtab; /* single-byte trans table (`tr') */
113 const char *current_string; /* value of last called user macro */
114 struct tbl_node *first_tbl; /* first table parsed */
115 struct tbl_node *last_tbl; /* last table parsed */
116 struct tbl_node *tbl; /* current table being parsed */
117 struct eqn_node *last_eqn; /* last equation parsed */
118 struct eqn_node *first_eqn; /* first equation parsed */
119 struct eqn_node *eqn; /* current equation being parsed */
120 };
121
122 struct roffnode {
123 enum rofft tok; /* type of node */
124 struct roffnode *parent; /* up one in stack */
125 int line; /* parse line */
126 int col; /* parse col */
127 char *name; /* node name, e.g. macro name */
128 char *end; /* end-rules: custom token */
129 int endspan; /* end-rules: next-line or infty */
130 enum roffrule rule; /* current evaluation rule */
131 };
132
133 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
134 enum rofft tok, /* tok of macro */ \
135 char **bufp, /* input buffer */ \
136 size_t *szp, /* size of input buffer */ \
137 int ln, /* parse line */ \
138 int ppos, /* original pos in buffer */ \
139 int pos, /* current pos in buffer */ \
140 int *offs /* reset offset of buffer data */
141
142 typedef enum rofferr (*roffproc)(ROFF_ARGS);
143
144 struct roffmac {
145 const char *name; /* macro name */
146 roffproc proc; /* process new macro */
147 roffproc text; /* process as child text of macro */
148 roffproc sub; /* process as child of macro */
149 int flags;
150 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
151 struct roffmac *next;
152 };
153
154 struct predef {
155 const char *name; /* predefined input name */
156 const char *str; /* replacement symbol */
157 };
158
159 #define PREDEF(__name, __str) \
160 { (__name), (__str) },
161
162 static enum rofft roffhash_find(const char *, size_t);
163 static void roffhash_init(void);
164 static void roffnode_cleanscope(struct roff *);
165 static void roffnode_pop(struct roff *);
166 static void roffnode_push(struct roff *, enum rofft,
167 const char *, int, int);
168 static enum rofferr roff_block(ROFF_ARGS);
169 static enum rofferr roff_block_text(ROFF_ARGS);
170 static enum rofferr roff_block_sub(ROFF_ARGS);
171 static enum rofferr roff_cblock(ROFF_ARGS);
172 static enum rofferr roff_ccond(ROFF_ARGS);
173 static enum rofferr roff_cond(ROFF_ARGS);
174 static enum rofferr roff_cond_text(ROFF_ARGS);
175 static enum rofferr roff_cond_sub(ROFF_ARGS);
176 static enum rofferr roff_ds(ROFF_ARGS);
177 static enum roffrule roff_evalcond(const char *, int *);
178 static void roff_free1(struct roff *);
179 static void roff_freestr(struct roffkv *);
180 static char *roff_getname(struct roff *, char **, int, int);
181 static const char *roff_getstrn(const struct roff *,
182 const char *, size_t);
183 static enum rofferr roff_line_ignore(ROFF_ARGS);
184 static enum rofferr roff_nr(ROFF_ARGS);
185 static void roff_openeqn(struct roff *, const char *,
186 int, int, const char *);
187 static enum rofft roff_parse(struct roff *, const char *, int *);
188 static enum rofferr roff_parsetext(char *);
189 static void roff_res(struct roff *,
190 char **, size_t *, int, int);
191 static enum rofferr roff_rm(ROFF_ARGS);
192 static void roff_setstr(struct roff *,
193 const char *, const char *, int);
194 static void roff_setstrn(struct roffkv **, const char *,
195 size_t, const char *, size_t, int);
196 static enum rofferr roff_so(ROFF_ARGS);
197 static enum rofferr roff_tr(ROFF_ARGS);
198 static enum rofferr roff_TE(ROFF_ARGS);
199 static enum rofferr roff_TS(ROFF_ARGS);
200 static enum rofferr roff_EQ(ROFF_ARGS);
201 static enum rofferr roff_EN(ROFF_ARGS);
202 static enum rofferr roff_T_(ROFF_ARGS);
203 static enum rofferr roff_userdef(ROFF_ARGS);
204
205 /* See roffhash_find() */
206
207 #define ASCII_HI 126
208 #define ASCII_LO 33
209 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
210
211 static struct roffmac *hash[HASHWIDTH];
212
213 static struct roffmac roffs[ROFF_MAX] = {
214 { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
215 { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
216 { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
217 { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
218 { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
219 { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
220 { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
221 { "ds", roff_ds, NULL, NULL, 0, NULL },
222 { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
223 { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
224 { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
225 { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
226 { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
227 { "it", roff_line_ignore, NULL, NULL, 0, NULL },
228 { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
229 { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
230 { "nr", roff_nr, NULL, NULL, 0, NULL },
231 { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
232 { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
233 { "rm", roff_rm, NULL, NULL, 0, NULL },
234 { "so", roff_so, NULL, NULL, 0, NULL },
235 { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
236 { "tr", roff_tr, NULL, NULL, 0, NULL },
237 { "TS", roff_TS, NULL, NULL, 0, NULL },
238 { "TE", roff_TE, NULL, NULL, 0, NULL },
239 { "T&", roff_T_, NULL, NULL, 0, NULL },
240 { "EQ", roff_EQ, NULL, NULL, 0, NULL },
241 { "EN", roff_EN, NULL, NULL, 0, NULL },
242 { ".", roff_cblock, NULL, NULL, 0, NULL },
243 { "\\}", roff_ccond, NULL, NULL, 0, NULL },
244 { NULL, roff_userdef, NULL, NULL, 0, NULL },
245 };
246
247 /* Array of injected predefined strings. */
248 #define PREDEFS_MAX 38
249 static const struct predef predefs[PREDEFS_MAX] = {
250 #include "predefs.in"
251 };
252
253 /* See roffhash_find() */
254 #define ROFF_HASH(p) (p[0] - ASCII_LO)
255
256 static void
257 roffhash_init(void)
258 {
259 struct roffmac *n;
260 int buc, i;
261
262 for (i = 0; i < (int)ROFF_USERDEF; i++) {
263 assert(roffs[i].name[0] >= ASCII_LO);
264 assert(roffs[i].name[0] <= ASCII_HI);
265
266 buc = ROFF_HASH(roffs[i].name);
267
268 if (NULL != (n = hash[buc])) {
269 for ( ; n->next; n = n->next)
270 /* Do nothing. */ ;
271 n->next = &roffs[i];
272 } else
273 hash[buc] = &roffs[i];
274 }
275 }
276
277 /*
278 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
279 * the nil-terminated string name could be found.
280 */
281 static enum rofft
282 roffhash_find(const char *p, size_t s)
283 {
284 int buc;
285 struct roffmac *n;
286
287 /*
288 * libroff has an extremely simple hashtable, for the time
289 * being, which simply keys on the first character, which must
290 * be printable, then walks a chain. It works well enough until
291 * optimised.
292 */
293
294 if (p[0] < ASCII_LO || p[0] > ASCII_HI)
295 return(ROFF_MAX);
296
297 buc = ROFF_HASH(p);
298
299 if (NULL == (n = hash[buc]))
300 return(ROFF_MAX);
301 for ( ; n; n = n->next)
302 if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
303 return((enum rofft)(n - roffs));
304
305 return(ROFF_MAX);
306 }
307
308
309 /*
310 * Pop the current node off of the stack of roff instructions currently
311 * pending.
312 */
313 static void
314 roffnode_pop(struct roff *r)
315 {
316 struct roffnode *p;
317
318 assert(r->last);
319 p = r->last;
320
321 r->last = r->last->parent;
322 free(p->name);
323 free(p->end);
324 free(p);
325 }
326
327
328 /*
329 * Push a roff node onto the instruction stack. This must later be
330 * removed with roffnode_pop().
331 */
332 static void
333 roffnode_push(struct roff *r, enum rofft tok, const char *name,
334 int line, int col)
335 {
336 struct roffnode *p;
337
338 p = mandoc_calloc(1, sizeof(struct roffnode));
339 p->tok = tok;
340 if (name)
341 p->name = mandoc_strdup(name);
342 p->parent = r->last;
343 p->line = line;
344 p->col = col;
345 p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
346
347 r->last = p;
348 }
349
350
351 static void
352 roff_free1(struct roff *r)
353 {
354 struct tbl_node *t;
355 struct eqn_node *e;
356 int i;
357
358 while (NULL != (t = r->first_tbl)) {
359 r->first_tbl = t->next;
360 tbl_free(t);
361 }
362
363 r->first_tbl = r->last_tbl = r->tbl = NULL;
364
365 while (NULL != (e = r->first_eqn)) {
366 r->first_eqn = e->next;
367 eqn_free(e);
368 }
369
370 r->first_eqn = r->last_eqn = r->eqn = NULL;
371
372 while (r->last)
373 roffnode_pop(r);
374
375 roff_freestr(r->strtab);
376 roff_freestr(r->xmbtab);
377
378 r->strtab = r->xmbtab = NULL;
379
380 if (r->xtab)
381 for (i = 0; i < 128; i++)
382 free(r->xtab[i].p);
383
384 free(r->xtab);
385 r->xtab = NULL;
386 }
387
388 void
389 roff_reset(struct roff *r)
390 {
391 int i;
392
393 roff_free1(r);
394
395 memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);
396
397 for (i = 0; i < PREDEFS_MAX; i++)
398 roff_setstr(r, predefs[i].name, predefs[i].str, 0);
399 }
400
401
402 void
403 roff_free(struct roff *r)
404 {
405
406 roff_free1(r);
407 free(r);
408 }
409
410
411 struct roff *
412 roff_alloc(struct mparse *parse)
413 {
414 struct roff *r;
415 int i;
416
417 r = mandoc_calloc(1, sizeof(struct roff));
418 r->parse = parse;
419 r->rstackpos = -1;
420
421 roffhash_init();
422
423 for (i = 0; i < PREDEFS_MAX; i++)
424 roff_setstr(r, predefs[i].name, predefs[i].str, 0);
425
426 return(r);
427 }
428
429 /*
430 * Pre-filter each and every line for reserved words (one beginning with
431 * `\*', e.g., `\*(ab'). These must be handled before the actual line
432 * is processed.
433 * This also checks the syntax of regular escapes.
434 */
435 static void
436 roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
437 {
438 enum mandoc_esc esc;
439 const char *stesc; /* start of an escape sequence ('\\') */
440 const char *stnam; /* start of the name, after "[(*" */
441 const char *cp; /* end of the name, e.g. before ']' */
442 const char *res; /* the string to be substituted */
443 int i, maxl, expand_count;
444 size_t nsz;
445 char *n;
446
447 expand_count = 0;
448
449 again:
450 cp = *bufp + pos;
451 while (NULL != (cp = strchr(cp, '\\'))) {
452 stesc = cp++;
453
454 /*
455 * The second character must be an asterisk.
456 * If it isn't, skip it anyway: It is escaped,
457 * so it can't start another escape sequence.
458 */
459
460 if ('\0' == *cp)
461 return;
462
463 if ('*' != *cp) {
464 res = cp;
465 esc = mandoc_escape(&cp, NULL, NULL);
466 if (ESCAPE_ERROR != esc)
467 continue;
468 cp = res;
469 mandoc_msg
470 (MANDOCERR_BADESCAPE, r->parse,
471 ln, (int)(stesc - *bufp), NULL);
472 return;
473 }
474
475 cp++;
476
477 /*
478 * The third character decides the length
479 * of the name of the string.
480 * Save a pointer to the name.
481 */
482
483 switch (*cp) {
484 case ('\0'):
485 return;
486 case ('('):
487 cp++;
488 maxl = 2;
489 break;
490 case ('['):
491 cp++;
492 maxl = 0;
493 break;
494 default:
495 maxl = 1;
496 break;
497 }
498 stnam = cp;
499
500 /* Advance to the end of the name. */
501
502 for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
503 if ('\0' == *cp) {
504 mandoc_msg
505 (MANDOCERR_BADESCAPE,
506 r->parse, ln,
507 (int)(stesc - *bufp), NULL);
508 return;
509 }
510 if (0 == maxl && ']' == *cp)
511 break;
512 }
513
514 /*
515 * Retrieve the replacement string; if it is
516 * undefined, resume searching for escapes.
517 */
518
519 res = roff_getstrn(r, stnam, (size_t)i);
520
521 if (NULL == res) {
522 mandoc_msg
523 (MANDOCERR_BADESCAPE, r->parse,
524 ln, (int)(stesc - *bufp), NULL);
525 res = "";
526 }
527
528 /* Replace the escape sequence by the string. */
529
530 pos = stesc - *bufp;
531
532 nsz = *szp + strlen(res) + 1;
533 n = mandoc_malloc(nsz);
534
535 strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
536 strlcat(n, res, nsz);
537 strlcat(n, cp + (maxl ? 0 : 1), nsz);
538
539 free(*bufp);
540
541 *bufp = n;
542 *szp = nsz;
543
544 if (EXPAND_LIMIT >= ++expand_count)
545 goto again;
546
547 /* Just leave the string unexpanded. */
548 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
549 return;
550 }
551 }
552
553 /*
554 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
555 */
556 static enum rofferr
557 roff_parsetext(char *p)
558 {
559 char l, r;
560 size_t sz;
561 const char *start;
562 enum mandoc_esc esc;
563
564 start = p;
565
566 while ('\0' != *p) {
567 sz = strcspn(p, "-\\");
568 p += sz;
569
570 if ('\0' == *p)
571 break;
572
573 if ('\\' == *p) {
574 /* Skip over escapes. */
575 p++;
576 esc = mandoc_escape
577 ((const char **)&p, NULL, NULL);
578 if (ESCAPE_ERROR == esc)
579 break;
580 continue;
581 } else if (p == start) {
582 p++;
583 continue;
584 }
585
586 l = *(p - 1);
587 r = *(p + 1);
588 if ('\\' != l &&
589 '\t' != r && '\t' != l &&
590 ' ' != r && ' ' != l &&
591 '-' != r && '-' != l &&
592 ! isdigit((unsigned char)l) &&
593 ! isdigit((unsigned char)r))
594 *p = ASCII_HYPH;
595 p++;
596 }
597
598 return(ROFF_CONT);
599 }
600
601 enum rofferr
602 roff_parseln(struct roff *r, int ln, char **bufp,
603 size_t *szp, int pos, int *offs)
604 {
605 enum rofft t;
606 enum rofferr e;
607 int ppos, ctl;
608
609 /*
610 * Run the reserved-word filter only if we have some reserved
611 * words to fill in.
612 */
613
614 roff_res(r, bufp, szp, ln, pos);
615
616 ppos = pos;
617 ctl = mandoc_getcontrol(*bufp, &pos);
618
619 /*
620 * First, if a scope is open and we're not a macro, pass the
621 * text through the macro's filter. If a scope isn't open and
622 * we're not a macro, just let it through.
623 * Finally, if there's an equation scope open, divert it into it
624 * no matter our state.
625 */
626
627 if (r->last && ! ctl) {
628 t = r->last->tok;
629 assert(roffs[t].text);
630 e = (*roffs[t].text)
631 (r, t, bufp, szp, ln, pos, pos, offs);
632 assert(ROFF_IGN == e || ROFF_CONT == e);
633 if (ROFF_CONT != e)
634 return(e);
635 if (r->eqn)
636 return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
637 if (r->tbl)
638 return(tbl_read(r->tbl, ln, *bufp, pos));
639 return(roff_parsetext(*bufp + pos));
640 } else if ( ! ctl) {
641 if (r->eqn)
642 return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
643 if (r->tbl)
644 return(tbl_read(r->tbl, ln, *bufp, pos));
645 return(roff_parsetext(*bufp + pos));
646 } else if (r->eqn)
647 return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
648
649 /*
650 * If a scope is open, go to the child handler for that macro,
651 * as it may want to preprocess before doing anything with it.
652 * Don't do so if an equation is open.
653 */
654
655 if (r->last) {
656 t = r->last->tok;
657 assert(roffs[t].sub);
658 return((*roffs[t].sub)
659 (r, t, bufp, szp,
660 ln, ppos, pos, offs));
661 }
662
663 /*
664 * Lastly, as we've no scope open, try to look up and execute
665 * the new macro. If no macro is found, simply return and let
666 * the compilers handle it.
667 */
668
669 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
670 return(ROFF_CONT);
671
672 assert(roffs[t].proc);
673 return((*roffs[t].proc)
674 (r, t, bufp, szp,
675 ln, ppos, pos, offs));
676 }
677
678
679 void
680 roff_endparse(struct roff *r)
681 {
682
683 if (r->last)
684 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
685 r->last->line, r->last->col, NULL);
686
687 if (r->eqn) {
688 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
689 r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
690 eqn_end(&r->eqn);
691 }
692
693 if (r->tbl) {
694 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
695 r->tbl->line, r->tbl->pos, NULL);
696 tbl_end(&r->tbl);
697 }
698 }
699
700 /*
701 * Parse a roff node's type from the input buffer. This must be in the
702 * form of ".foo xxx" in the usual way.
703 */
704 static enum rofft
705 roff_parse(struct roff *r, const char *buf, int *pos)
706 {
707 const char *mac;
708 size_t maclen;
709 enum rofft t;
710
711 if ('\0' == buf[*pos] || '"' == buf[*pos] ||
712 '\t' == buf[*pos] || ' ' == buf[*pos])
713 return(ROFF_MAX);
714
715 /*
716 * We stop the macro parse at an escape, tab, space, or nil.
717 * However, `\}' is also a valid macro, so make sure we don't
718 * clobber it by seeing the `\' as the end of token.
719 */
720
721 mac = buf + *pos;
722 maclen = strcspn(mac + 1, " \\\t\0") + 1;
723
724 t = (r->current_string = roff_getstrn(r, mac, maclen))
725 ? ROFF_USERDEF : roffhash_find(mac, maclen);
726
727 *pos += (int)maclen;
728
729 while (buf[*pos] && ' ' == buf[*pos])
730 (*pos)++;
731
732 return(t);
733 }
734
735 /* ARGSUSED */
736 static enum rofferr
737 roff_cblock(ROFF_ARGS)
738 {
739
740 /*
741 * A block-close `..' should only be invoked as a child of an
742 * ignore macro, otherwise raise a warning and just ignore it.
743 */
744
745 if (NULL == r->last) {
746 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
747 return(ROFF_IGN);
748 }
749
750 switch (r->last->tok) {
751 case (ROFF_am):
752 /* FALLTHROUGH */
753 case (ROFF_ami):
754 /* FALLTHROUGH */
755 case (ROFF_am1):
756 /* FALLTHROUGH */
757 case (ROFF_de):
758 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
759 /* FALLTHROUGH */
760 case (ROFF_dei):
761 /* FALLTHROUGH */
762 case (ROFF_ig):
763 break;
764 default:
765 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
766 return(ROFF_IGN);
767 }
768
769 if ((*bufp)[pos])
770 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
771
772 roffnode_pop(r);
773 roffnode_cleanscope(r);
774 return(ROFF_IGN);
775
776 }
777
778
779 static void
780 roffnode_cleanscope(struct roff *r)
781 {
782
783 while (r->last) {
784 if (--r->last->endspan < 0)
785 break;
786 roffnode_pop(r);
787 }
788 }
789
790
791 /* ARGSUSED */
792 static enum rofferr
793 roff_ccond(ROFF_ARGS)
794 {
795
796 if (NULL == r->last) {
797 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
798 return(ROFF_IGN);
799 }
800
801 switch (r->last->tok) {
802 case (ROFF_el):
803 /* FALLTHROUGH */
804 case (ROFF_ie):
805 /* FALLTHROUGH */
806 case (ROFF_if):
807 break;
808 default:
809 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
810 return(ROFF_IGN);
811 }
812
813 if (r->last->endspan > -1) {
814 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
815 return(ROFF_IGN);
816 }
817
818 if ((*bufp)[pos])
819 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
820
821 roffnode_pop(r);
822 roffnode_cleanscope(r);
823 return(ROFF_IGN);
824 }
825
826
827 /* ARGSUSED */
828 static enum rofferr
829 roff_block(ROFF_ARGS)
830 {
831 int sv;
832 size_t sz;
833 char *name;
834
835 name = NULL;
836
837 if (ROFF_ig != tok) {
838 if ('\0' == (*bufp)[pos]) {
839 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
840 return(ROFF_IGN);
841 }
842
843 /*
844 * Re-write `de1', since we don't really care about
845 * groff's strange compatibility mode, into `de'.
846 */
847
848 if (ROFF_de1 == tok)
849 tok = ROFF_de;
850 if (ROFF_de == tok)
851 name = *bufp + pos;
852 else
853 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
854 roffs[tok].name);
855
856 while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
857 pos++;
858
859 while (isspace((unsigned char)(*bufp)[pos]))
860 (*bufp)[pos++] = '\0';
861 }
862
863 roffnode_push(r, tok, name, ln, ppos);
864
865 /*
866 * At the beginning of a `de' macro, clear the existing string
867 * with the same name, if there is one. New content will be
868 * added from roff_block_text() in multiline mode.
869 */
870
871 if (ROFF_de == tok)
872 roff_setstr(r, name, "", 0);
873
874 if ('\0' == (*bufp)[pos])
875 return(ROFF_IGN);
876
877 /* If present, process the custom end-of-line marker. */
878
879 sv = pos;
880 while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
881 pos++;
882
883 /*
884 * Note: groff does NOT like escape characters in the input.
885 * Instead of detecting this, we're just going to let it fly and
886 * to hell with it.
887 */
888
889 assert(pos > sv);
890 sz = (size_t)(pos - sv);
891
892 if (1 == sz && '.' == (*bufp)[sv])
893 return(ROFF_IGN);
894
895 r->last->end = mandoc_malloc(sz + 1);
896
897 memcpy(r->last->end, *bufp + sv, sz);
898 r->last->end[(int)sz] = '\0';
899
900 if ((*bufp)[pos])
901 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
902
903 return(ROFF_IGN);
904 }
905
906
907 /* ARGSUSED */
908 static enum rofferr
909 roff_block_sub(ROFF_ARGS)
910 {
911 enum rofft t;
912 int i, j;
913
914 /*
915 * First check whether a custom macro exists at this level. If
916 * it does, then check against it. This is some of groff's
917 * stranger behaviours. If we encountered a custom end-scope
918 * tag and that tag also happens to be a "real" macro, then we
919 * need to try interpreting it again as a real macro. If it's
920 * not, then return ignore. Else continue.
921 */
922
923 if (r->last->end) {
924 for (i = pos, j = 0; r->last->end[j]; j++, i++)
925 if ((*bufp)[i] != r->last->end[j])
926 break;
927
928 if ('\0' == r->last->end[j] &&
929 ('\0' == (*bufp)[i] ||
930 ' ' == (*bufp)[i] ||
931 '\t' == (*bufp)[i])) {
932 roffnode_pop(r);
933 roffnode_cleanscope(r);
934
935 while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
936 i++;
937
938 pos = i;
939 if (ROFF_MAX != roff_parse(r, *bufp, &pos))
940 return(ROFF_RERUN);
941 return(ROFF_IGN);
942 }
943 }
944
945 /*
946 * If we have no custom end-query or lookup failed, then try
947 * pulling it out of the hashtable.
948 */
949
950 t = roff_parse(r, *bufp, &pos);
951
952 /*
953 * Macros other than block-end are only significant
954 * in `de' blocks; elsewhere, simply throw them away.
955 */
956 if (ROFF_cblock != t) {
957 if (ROFF_de == tok)
958 roff_setstr(r, r->last->name, *bufp + ppos, 1);
959 return(ROFF_IGN);
960 }
961
962 assert(roffs[t].proc);
963 return((*roffs[t].proc)(r, t, bufp, szp,
964 ln, ppos, pos, offs));
965 }
966
967
968 /* ARGSUSED */
969 static enum rofferr
970 roff_block_text(ROFF_ARGS)
971 {
972
973 if (ROFF_de == tok)
974 roff_setstr(r, r->last->name, *bufp + pos, 1);
975
976 return(ROFF_IGN);
977 }
978
979
980 /* ARGSUSED */
981 static enum rofferr
982 roff_cond_sub(ROFF_ARGS)
983 {
984 enum rofft t;
985 enum roffrule rr;
986 char *ep;
987
988 rr = r->last->rule;
989 roffnode_cleanscope(r);
990
991 /*
992 * If the macro is unknown, first check if it contains a closing
993 * delimiter `\}'. If it does, close out our scope and return
994 * the currently-scoped rule (ignore or continue). Else, drop
995 * into the currently-scoped rule.
996 */
997
998 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
999 ep = &(*bufp)[pos];
1000 for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
1001 ep++;
1002 if ('}' != *ep)
1003 continue;
1004
1005 /*
1006 * Make the \} go away.
1007 * This is a little haphazard, as it's not quite
1008 * clear how nroff does this.
1009 * If we're at the end of line, then just chop
1010 * off the \} and resize the buffer.
1011 * If we aren't, then conver it to spaces.
1012 */
1013
1014 if ('\0' == *(ep + 1)) {
1015 *--ep = '\0';
1016 *szp -= 2;
1017 } else
1018 *(ep - 1) = *ep = ' ';
1019
1020 roff_ccond(r, ROFF_ccond, bufp, szp,
1021 ln, pos, pos + 2, offs);
1022 break;
1023 }
1024 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1025 }
1026
1027 /*
1028 * A denied conditional must evaluate its children if and only
1029 * if they're either structurally required (such as loops and
1030 * conditionals) or a closing macro.
1031 */
1032
1033 if (ROFFRULE_DENY == rr)
1034 if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
1035 if (ROFF_ccond != t)
1036 return(ROFF_IGN);
1037
1038 assert(roffs[t].proc);
1039 return((*roffs[t].proc)(r, t, bufp, szp,
1040 ln, ppos, pos, offs));
1041 }
1042
1043 /* ARGSUSED */
1044 static enum rofferr
1045 roff_cond_text(ROFF_ARGS)
1046 {
1047 char *ep;
1048 enum roffrule rr;
1049
1050 rr = r->last->rule;
1051 roffnode_cleanscope(r);
1052
1053 ep = &(*bufp)[pos];
1054 for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
1055 ep++;
1056 if ('}' != *ep)
1057 continue;
1058 *ep = '&';
1059 roff_ccond(r, ROFF_ccond, bufp, szp,
1060 ln, pos, pos + 2, offs);
1061 }
1062 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1063 }
1064
1065 static enum roffrule
1066 roff_evalcond(const char *v, int *pos)
1067 {
1068
1069 switch (v[*pos]) {
1070 case ('n'):
1071 (*pos)++;
1072 return(ROFFRULE_ALLOW);
1073 case ('e'):
1074 /* FALLTHROUGH */
1075 case ('o'):
1076 /* FALLTHROUGH */
1077 case ('t'):
1078 (*pos)++;
1079 return(ROFFRULE_DENY);
1080 default:
1081 break;
1082 }
1083
1084 while (v[*pos] && ' ' != v[*pos])
1085 (*pos)++;
1086 return(ROFFRULE_DENY);
1087 }
1088
1089 /* ARGSUSED */
1090 static enum rofferr
1091 roff_line_ignore(ROFF_ARGS)
1092 {
1093
1094 if (ROFF_it == tok)
1095 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
1096
1097 return(ROFF_IGN);
1098 }
1099
1100 /* ARGSUSED */
1101 static enum rofferr
1102 roff_cond(ROFF_ARGS)
1103 {
1104 int sv;
1105 enum roffrule rule;
1106
1107 /*
1108 * An `.el' has no conditional body: it will consume the value
1109 * of the current rstack entry set in prior `ie' calls or
1110 * defaults to DENY.
1111 *
1112 * If we're not an `el', however, then evaluate the conditional.
1113 */
1114
1115 rule = ROFF_el == tok ?
1116 (r->rstackpos < 0 ?
1117 ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
1118 roff_evalcond(*bufp, &pos);
1119
1120 sv = pos;
1121 while (' ' == (*bufp)[pos])
1122 pos++;
1123
1124 /*
1125 * Roff is weird. If we have just white-space after the
1126 * conditional, it's considered the BODY and we exit without
1127 * really doing anything. Warn about this. It's probably
1128 * wrong.
1129 */
1130
1131 if ('\0' == (*bufp)[pos] && sv != pos) {
1132 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1133 return(ROFF_IGN);
1134 }
1135
1136 roffnode_push(r, tok, NULL, ln, ppos);
1137
1138 r->last->rule = rule;
1139
1140 /*
1141 * An if-else will put the NEGATION of the current evaluated
1142 * conditional into the stack of rules.
1143 */
1144
1145 if (ROFF_ie == tok) {
1146 if (r->rstackpos == RSTACK_MAX - 1) {
1147 mandoc_msg(MANDOCERR_MEM,
1148 r->parse, ln, ppos, NULL);
1149 return(ROFF_ERR);
1150 }
1151 r->rstack[++r->rstackpos] =
1152 ROFFRULE_DENY == r->last->rule ?
1153 ROFFRULE_ALLOW : ROFFRULE_DENY;
1154 }
1155
1156 /* If the parent has false as its rule, then so do we. */
1157
1158 if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1159 r->last->rule = ROFFRULE_DENY;
1160
1161 /*
1162 * Determine scope. If we're invoked with "\{" trailing the
1163 * conditional, then we're in a multiline scope. Else our scope
1164 * expires on the next line.
1165 */
1166
1167 r->last->endspan = 1;
1168
1169 if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1170 r->last->endspan = -1;
1171 pos += 2;
1172 }
1173
1174 /*
1175 * If there are no arguments on the line, the next-line scope is
1176 * assumed.
1177 */
1178
1179 if ('\0' == (*bufp)[pos])
1180 return(ROFF_IGN);
1181
1182 /* Otherwise re-run the roff parser after recalculating. */
1183
1184 *offs = pos;
1185 return(ROFF_RERUN);
1186 }
1187
1188
1189 /* ARGSUSED */
1190 static enum rofferr
1191 roff_ds(ROFF_ARGS)
1192 {
1193 char *name, *string;
1194
1195 /*
1196 * A symbol is named by the first word following the macro
1197 * invocation up to a space. Its value is anything after the
1198 * name's trailing whitespace and optional double-quote. Thus,
1199 *
1200 * [.ds foo "bar " ]
1201 *
1202 * will have `bar " ' as its value.
1203 */
1204
1205 string = *bufp + pos;
1206 name = roff_getname(r, &string, ln, pos);
1207 if ('\0' == *name)
1208 return(ROFF_IGN);
1209
1210 /* Read past initial double-quote. */
1211 if ('"' == *string)
1212 string++;
1213
1214 /* The rest is the value. */
1215 roff_setstr(r, name, string, 0);
1216 return(ROFF_IGN);
1217 }
1218
1219 int
1220 roff_regisset(const struct roff *r, enum regs reg)
1221 {
1222
1223 return(r->regs[(int)reg].set);
1224 }
1225
1226 unsigned int
1227 roff_regget(const struct roff *r, enum regs reg)
1228 {
1229
1230 return(r->regs[(int)reg].u);
1231 }
1232
1233 void
1234 roff_regunset(struct roff *r, enum regs reg)
1235 {
1236
1237 r->regs[(int)reg].set = 0;
1238 }
1239
1240 /* ARGSUSED */
1241 static enum rofferr
1242 roff_nr(ROFF_ARGS)
1243 {
1244 const char *key;
1245 char *val;
1246 int iv;
1247
1248 val = *bufp + pos;
1249 key = roff_getname(r, &val, ln, pos);
1250
1251 if (0 == strcmp(key, "nS")) {
1252 r->regs[(int)REG_nS].set = 1;
1253 if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
1254 r->regs[(int)REG_nS].u = (unsigned)iv;
1255 else
1256 r->regs[(int)REG_nS].u = 0u;
1257 }
1258
1259 return(ROFF_IGN);
1260 }
1261
1262 /* ARGSUSED */
1263 static enum rofferr
1264 roff_rm(ROFF_ARGS)
1265 {
1266 const char *name;
1267 char *cp;
1268
1269 cp = *bufp + pos;
1270 while ('\0' != *cp) {
1271 name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1272 if ('\0' != *name)
1273 roff_setstr(r, name, NULL, 0);
1274 }
1275 return(ROFF_IGN);
1276 }
1277
1278 /* ARGSUSED */
1279 static enum rofferr
1280 roff_TE(ROFF_ARGS)
1281 {
1282
1283 if (NULL == r->tbl)
1284 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1285 else
1286 tbl_end(&r->tbl);
1287
1288 return(ROFF_IGN);
1289 }
1290
1291 /* ARGSUSED */
1292 static enum rofferr
1293 roff_T_(ROFF_ARGS)
1294 {
1295
1296 if (NULL == r->tbl)
1297 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1298 else
1299 tbl_restart(ppos, ln, r->tbl);
1300
1301 return(ROFF_IGN);
1302 }
1303
1304 #if 0
1305 static int
1306 roff_closeeqn(struct roff *r)
1307 {
1308
1309 return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1310 }
1311 #endif
1312
1313 static void
1314 roff_openeqn(struct roff *r, const char *name, int line,
1315 int offs, const char *buf)
1316 {
1317 struct eqn_node *e;
1318 int poff;
1319
1320 assert(NULL == r->eqn);
1321 e = eqn_alloc(name, offs, line, r->parse);
1322
1323 if (r->last_eqn)
1324 r->last_eqn->next = e;
1325 else
1326 r->first_eqn = r->last_eqn = e;
1327
1328 r->eqn = r->last_eqn = e;
1329
1330 if (buf) {
1331 poff = 0;
1332 eqn_read(&r->eqn, line, buf, offs, &poff);
1333 }
1334 }
1335
1336 /* ARGSUSED */
1337 static enum rofferr
1338 roff_EQ(ROFF_ARGS)
1339 {
1340
1341 roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1342 return(ROFF_IGN);
1343 }
1344
1345 /* ARGSUSED */
1346 static enum rofferr
1347 roff_EN(ROFF_ARGS)
1348 {
1349
1350 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1351 return(ROFF_IGN);
1352 }
1353
1354 /* ARGSUSED */
1355 static enum rofferr
1356 roff_TS(ROFF_ARGS)
1357 {
1358 struct tbl_node *t;
1359
1360 if (r->tbl) {
1361 mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1362 tbl_end(&r->tbl);
1363 }
1364
1365 t = tbl_alloc(ppos, ln, r->parse);
1366
1367 if (r->last_tbl)
1368 r->last_tbl->next = t;
1369 else
1370 r->first_tbl = r->last_tbl = t;
1371
1372 r->tbl = r->last_tbl = t;
1373 return(ROFF_IGN);
1374 }
1375
1376 /* ARGSUSED */
1377 static enum rofferr
1378 roff_tr(ROFF_ARGS)
1379 {
1380 const char *p, *first, *second;
1381 size_t fsz, ssz;
1382 enum mandoc_esc esc;
1383
1384 p = *bufp + pos;
1385
1386 if ('\0' == *p) {
1387 mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1388 return(ROFF_IGN);
1389 }
1390
1391 while ('\0' != *p) {
1392 fsz = ssz = 1;
1393
1394 first = p++;
1395 if ('\\' == *first) {
1396 esc = mandoc_escape(&p, NULL, NULL);
1397 if (ESCAPE_ERROR == esc) {
1398 mandoc_msg
1399 (MANDOCERR_BADESCAPE, r->parse,
1400 ln, (int)(p - *bufp), NULL);
1401 return(ROFF_IGN);
1402 }
1403 fsz = (size_t)(p - first);
1404 }
1405
1406 second = p++;
1407 if ('\\' == *second) {
1408 esc = mandoc_escape(&p, NULL, NULL);
1409 if (ESCAPE_ERROR == esc) {
1410 mandoc_msg
1411 (MANDOCERR_BADESCAPE, r->parse,
1412 ln, (int)(p - *bufp), NULL);
1413 return(ROFF_IGN);
1414 }
1415 ssz = (size_t)(p - second);
1416 } else if ('\0' == *second) {
1417 mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1418 ln, (int)(p - *bufp), NULL);
1419 second = " ";
1420 p--;
1421 }
1422
1423 if (fsz > 1) {
1424 roff_setstrn(&r->xmbtab, first,
1425 fsz, second, ssz, 0);
1426 continue;
1427 }
1428
1429 if (NULL == r->xtab)
1430 r->xtab = mandoc_calloc
1431 (128, sizeof(struct roffstr));
1432
1433 free(r->xtab[(int)*first].p);
1434 r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
1435 r->xtab[(int)*first].sz = ssz;
1436 }
1437
1438 return(ROFF_IGN);
1439 }
1440
1441 /* ARGSUSED */
1442 static enum rofferr
1443 roff_so(ROFF_ARGS)
1444 {
1445 char *name;
1446
1447 mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1448
1449 /*
1450 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1451 * opening anything that's not in our cwd or anything beneath
1452 * it. Thus, explicitly disallow traversing up the file-system
1453 * or using absolute paths.
1454 */
1455
1456 name = *bufp + pos;
1457 if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1458 mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1459 return(ROFF_ERR);
1460 }
1461
1462 *offs = pos;
1463 return(ROFF_SO);
1464 }
1465
1466 /* ARGSUSED */
1467 static enum rofferr
1468 roff_userdef(ROFF_ARGS)
1469 {
1470 const char *arg[9];
1471 char *cp, *n1, *n2;
1472 int i;
1473
1474 /*
1475 * Collect pointers to macro argument strings
1476 * and null-terminate them.
1477 */
1478 cp = *bufp + pos;
1479 for (i = 0; i < 9; i++)
1480 arg[i] = '\0' == *cp ? "" :
1481 mandoc_getarg(r->parse, &cp, ln, &pos);
1482
1483 /*
1484 * Expand macro arguments.
1485 */
1486 *szp = 0;
1487 n1 = cp = mandoc_strdup(r->current_string);
1488 while (NULL != (cp = strstr(cp, "\\$"))) {
1489 i = cp[2] - '1';
1490 if (0 > i || 8 < i) {
1491 /* Not an argument invocation. */
1492 cp += 2;
1493 continue;
1494 }
1495
1496 *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1497 n2 = mandoc_malloc(*szp);
1498
1499 strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1500 strlcat(n2, arg[i], *szp);
1501 strlcat(n2, cp + 3, *szp);
1502
1503 cp = n2 + (cp - n1);
1504 free(n1);
1505 n1 = n2;
1506 }
1507
1508 /*
1509 * Replace the macro invocation
1510 * by the expanded macro.
1511 */
1512 free(*bufp);
1513 *bufp = n1;
1514 if (0 == *szp)
1515 *szp = strlen(*bufp) + 1;
1516
1517 return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1518 ROFF_REPARSE : ROFF_APPEND);
1519 }
1520
1521 static char *
1522 roff_getname(struct roff *r, char **cpp, int ln, int pos)
1523 {
1524 char *name, *cp;
1525
1526 name = *cpp;
1527 if ('\0' == *name)
1528 return(name);
1529
1530 /* Read until end of name. */
1531 for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
1532 if ('\\' != *cp)
1533 continue;
1534 cp++;
1535 if ('\\' == *cp)
1536 continue;
1537 mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1538 *cp = '\0';
1539 name = cp;
1540 }
1541
1542 /* Nil-terminate name. */
1543 if ('\0' != *cp)
1544 *(cp++) = '\0';
1545
1546 /* Read past spaces. */
1547 while (' ' == *cp)
1548 cp++;
1549
1550 *cpp = cp;
1551 return(name);
1552 }
1553
1554 /*
1555 * Store *string into the user-defined string called *name.
1556 * In multiline mode, append to an existing entry and append '\n';
1557 * else replace the existing entry, if there is one.
1558 * To clear an existing entry, call with (*r, *name, NULL, 0).
1559 */
1560 static void
1561 roff_setstr(struct roff *r, const char *name, const char *string,
1562 int multiline)
1563 {
1564
1565 roff_setstrn(&r->strtab, name, strlen(name), string,
1566 string ? strlen(string) : 0, multiline);
1567 }
1568
1569 static void
1570 roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1571 const char *string, size_t stringsz, int multiline)
1572 {
1573 struct roffkv *n;
1574 char *c;
1575 int i;
1576 size_t oldch, newch;
1577
1578 /* Search for an existing string with the same name. */
1579 n = *r;
1580
1581 while (n && strcmp(name, n->key.p))
1582 n = n->next;
1583
1584 if (NULL == n) {
1585 /* Create a new string table entry. */
1586 n = mandoc_malloc(sizeof(struct roffkv));
1587 n->key.p = mandoc_strndup(name, namesz);
1588 n->key.sz = namesz;
1589 n->val.p = NULL;
1590 n->val.sz = 0;
1591 n->next = *r;
1592 *r = n;
1593 } else if (0 == multiline) {
1594 /* In multiline mode, append; else replace. */
1595 free(n->val.p);
1596 n->val.p = NULL;
1597 n->val.sz = 0;
1598 }
1599
1600 if (NULL == string)
1601 return;
1602
1603 /*
1604 * One additional byte for the '\n' in multiline mode,
1605 * and one for the terminating '\0'.
1606 */
1607 newch = stringsz + (multiline ? 2u : 1u);
1608
1609 if (NULL == n->val.p) {
1610 n->val.p = mandoc_malloc(newch);
1611 *n->val.p = '\0';
1612 oldch = 0;
1613 } else {
1614 oldch = n->val.sz;
1615 n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1616 }
1617
1618 /* Skip existing content in the destination buffer. */
1619 c = n->val.p + (int)oldch;
1620
1621 /* Append new content to the destination buffer. */
1622 i = 0;
1623 while (i < (int)stringsz) {
1624 /*
1625 * Rudimentary roff copy mode:
1626 * Handle escaped backslashes.
1627 */
1628 if ('\\' == string[i] && '\\' == string[i + 1])
1629 i++;
1630 *c++ = string[i++];
1631 }
1632
1633 /* Append terminating bytes. */
1634 if (multiline)
1635 *c++ = '\n';
1636
1637 *c = '\0';
1638 n->val.sz = (int)(c - n->val.p);
1639 }
1640
1641 static const char *
1642 roff_getstrn(const struct roff *r, const char *name, size_t len)
1643 {
1644 const struct roffkv *n;
1645
1646 for (n = r->strtab; n; n = n->next)
1647 if (0 == strncmp(name, n->key.p, len) &&
1648 '\0' == n->key.p[(int)len])
1649 return(n->val.p);
1650
1651 return(NULL);
1652 }
1653
1654 static void
1655 roff_freestr(struct roffkv *r)
1656 {
1657 struct roffkv *n, *nn;
1658
1659 for (n = r; n; n = nn) {
1660 free(n->key.p);
1661 free(n->val.p);
1662 nn = n->next;
1663 free(n);
1664 }
1665 }
1666
1667 const struct tbl_span *
1668 roff_span(const struct roff *r)
1669 {
1670
1671 return(r->tbl ? tbl_span(r->tbl) : NULL);
1672 }
1673
1674 const struct eqn *
1675 roff_eqn(const struct roff *r)
1676 {
1677
1678 return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1679 }
1680
1681 /*
1682 * Duplicate an input string, making the appropriate character
1683 * conversations (as stipulated by `tr') along the way.
1684 * Returns a heap-allocated string with all the replacements made.
1685 */
1686 char *
1687 roff_strdup(const struct roff *r, const char *p)
1688 {
1689 const struct roffkv *cp;
1690 char *res;
1691 const char *pp;
1692 size_t ssz, sz;
1693 enum mandoc_esc esc;
1694
1695 if (NULL == r->xmbtab && NULL == r->xtab)
1696 return(mandoc_strdup(p));
1697 else if ('\0' == *p)
1698 return(mandoc_strdup(""));
1699
1700 /*
1701 * Step through each character looking for term matches
1702 * (remember that a `tr' can be invoked with an escape, which is
1703 * a glyph but the escape is multi-character).
1704 * We only do this if the character hash has been initialised
1705 * and the string is >0 length.
1706 */
1707
1708 res = NULL;
1709 ssz = 0;
1710
1711 while ('\0' != *p) {
1712 if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
1713 sz = r->xtab[(int)*p].sz;
1714 res = mandoc_realloc(res, ssz + sz + 1);
1715 memcpy(res + ssz, r->xtab[(int)*p].p, sz);
1716 ssz += sz;
1717 p++;
1718 continue;
1719 } else if ('\\' != *p) {
1720 res = mandoc_realloc(res, ssz + 2);
1721 res[ssz++] = *p++;
1722 continue;
1723 }
1724
1725 /* Search for term matches. */
1726 for (cp = r->xmbtab; cp; cp = cp->next)
1727 if (0 == strncmp(p, cp->key.p, cp->key.sz))
1728 break;
1729
1730 if (NULL != cp) {
1731 /*
1732 * A match has been found.
1733 * Append the match to the array and move
1734 * forward by its keysize.
1735 */
1736 res = mandoc_realloc
1737 (res, ssz + cp->val.sz + 1);
1738 memcpy(res + ssz, cp->val.p, cp->val.sz);
1739 ssz += cp->val.sz;
1740 p += (int)cp->key.sz;
1741 continue;
1742 }
1743
1744 /*
1745 * Handle escapes carefully: we need to copy
1746 * over just the escape itself, or else we might
1747 * do replacements within the escape itself.
1748 * Make sure to pass along the bogus string.
1749 */
1750 pp = p++;
1751 esc = mandoc_escape(&p, NULL, NULL);
1752 if (ESCAPE_ERROR == esc) {
1753 sz = strlen(pp);
1754 res = mandoc_realloc(res, ssz + sz + 1);
1755 memcpy(res + ssz, pp, sz);
1756 break;
1757 }
1758 /*
1759 * We bail out on bad escapes.
1760 * No need to warn: we already did so when
1761 * roff_res() was called.
1762 */
1763 sz = (int)(p - pp);
1764 res = mandoc_realloc(res, ssz + sz + 1);
1765 memcpy(res + ssz, pp, sz);
1766 ssz += sz;
1767 }
1768
1769 res[(int)ssz] = '\0';
1770 return(res);
1771 }