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