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