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