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