]> git.cameronkatri.com Git - mandoc.git/blob - roff.c
498e85be2ca37ff86fab7caab9e86b4462817f4c
[mandoc.git] / roff.c
1 /* $Id: roff.c,v 1.153 2011/07/26 14:24:06 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 int 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 int
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 /* Search for a leading backslash and save a pointer to it. */
417
418 cp = *bufp + pos;
419 while (NULL != (cp = strchr(cp, '\\'))) {
420 stesc = cp++;
421
422 /*
423 * The second character must be an asterisk.
424 * If it isn't, skip it anyway: It is escaped,
425 * so it can't start another escape sequence.
426 */
427
428 if ('\0' == *cp)
429 return(1);
430
431 if ('*' != *cp) {
432 res = cp;
433 esc = mandoc_escape(&cp, NULL, NULL);
434 if (ESCAPE_ERROR != esc)
435 continue;
436 cp = res;
437 mandoc_msg
438 (MANDOCERR_BADESCAPE, r->parse,
439 ln, (int)(stesc - *bufp), NULL);
440 continue;
441 }
442
443 cp++;
444
445 /*
446 * The third character decides the length
447 * of the name of the string.
448 * Save a pointer to the name.
449 */
450
451 switch (*cp) {
452 case ('\0'):
453 return(1);
454 case ('('):
455 cp++;
456 maxl = 2;
457 break;
458 case ('['):
459 cp++;
460 maxl = 0;
461 break;
462 default:
463 maxl = 1;
464 break;
465 }
466 stnam = cp;
467
468 /* Advance to the end of the name. */
469
470 for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
471 if ('\0' == *cp) {
472 mandoc_msg
473 (MANDOCERR_BADESCAPE,
474 r->parse, ln,
475 (int)(stesc - *bufp), NULL);
476 return(1);
477 }
478 if (0 == maxl && ']' == *cp)
479 break;
480 }
481
482 /*
483 * Retrieve the replacement string; if it is
484 * undefined, resume searching for escapes.
485 */
486
487 res = roff_getstrn(r, stnam, (size_t)i);
488
489 if (NULL == res) {
490 mandoc_msg
491 (MANDOCERR_BADESCAPE, r->parse,
492 ln, (int)(stesc - *bufp), NULL);
493 res = "";
494 }
495
496 /* Replace the escape sequence by the string. */
497
498 nsz = *szp + strlen(res) + 1;
499 n = mandoc_malloc(nsz);
500
501 strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
502 strlcat(n, res, nsz);
503 strlcat(n, cp + (maxl ? 0 : 1), nsz);
504
505 free(*bufp);
506
507 *bufp = n;
508 *szp = nsz;
509 return(0);
510 }
511
512 return(1);
513 }
514
515 enum rofferr
516 roff_parseln(struct roff *r, int ln, char **bufp,
517 size_t *szp, int pos, int *offs)
518 {
519 enum rofft t;
520 enum rofferr e;
521 int ppos, ctl;
522
523 /*
524 * Run the reserved-word filter only if we have some reserved
525 * words to fill in.
526 */
527
528 if ( ! roff_res(r, bufp, szp, ln, pos))
529 return(ROFF_REPARSE);
530
531 ppos = pos;
532 ctl = mandoc_getcontrol(*bufp, &pos);
533
534 /*
535 * First, if a scope is open and we're not a macro, pass the
536 * text through the macro's filter. If a scope isn't open and
537 * we're not a macro, just let it through.
538 * Finally, if there's an equation scope open, divert it into it
539 * no matter our state.
540 */
541
542 if (r->last && ! ctl) {
543 t = r->last->tok;
544 assert(roffs[t].text);
545 e = (*roffs[t].text)
546 (r, t, bufp, szp, ln, pos, pos, offs);
547 assert(ROFF_IGN == e || ROFF_CONT == e);
548 if (ROFF_CONT != e)
549 return(e);
550 if (r->eqn)
551 return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
552 if (r->tbl)
553 return(tbl_read(r->tbl, ln, *bufp, pos));
554 return(ROFF_CONT);
555 } else if ( ! ctl) {
556 if (r->eqn)
557 return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
558 if (r->tbl)
559 return(tbl_read(r->tbl, ln, *bufp, pos));
560 return(ROFF_CONT);
561 } else if (r->eqn)
562 return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
563
564 /*
565 * If a scope is open, go to the child handler for that macro,
566 * as it may want to preprocess before doing anything with it.
567 * Don't do so if an equation is open.
568 */
569
570 if (r->last) {
571 t = r->last->tok;
572 assert(roffs[t].sub);
573 return((*roffs[t].sub)
574 (r, t, bufp, szp,
575 ln, ppos, pos, offs));
576 }
577
578 /*
579 * Lastly, as we've no scope open, try to look up and execute
580 * the new macro. If no macro is found, simply return and let
581 * the compilers handle it.
582 */
583
584 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
585 return(ROFF_CONT);
586
587 assert(roffs[t].proc);
588 return((*roffs[t].proc)
589 (r, t, bufp, szp,
590 ln, ppos, pos, offs));
591 }
592
593
594 void
595 roff_endparse(struct roff *r)
596 {
597
598 if (r->last)
599 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
600 r->last->line, r->last->col, NULL);
601
602 if (r->eqn) {
603 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
604 r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
605 eqn_end(&r->eqn);
606 }
607
608 if (r->tbl) {
609 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
610 r->tbl->line, r->tbl->pos, NULL);
611 tbl_end(&r->tbl);
612 }
613 }
614
615 /*
616 * Parse a roff node's type from the input buffer. This must be in the
617 * form of ".foo xxx" in the usual way.
618 */
619 static enum rofft
620 roff_parse(struct roff *r, const char *buf, int *pos)
621 {
622 const char *mac;
623 size_t maclen;
624 enum rofft t;
625
626 if ('\0' == buf[*pos] || '"' == buf[*pos] ||
627 '\t' == buf[*pos] || ' ' == buf[*pos])
628 return(ROFF_MAX);
629
630 /*
631 * We stop the macro parse at an escape, tab, space, or nil.
632 * However, `\}' is also a valid macro, so make sure we don't
633 * clobber it by seeing the `\' as the end of token.
634 */
635
636 mac = buf + *pos;
637 maclen = strcspn(mac + 1, " \\\t\0") + 1;
638
639 t = (r->current_string = roff_getstrn(r, mac, maclen))
640 ? ROFF_USERDEF : roff_hash_find(mac, maclen);
641
642 *pos += (int)maclen;
643
644 while (buf[*pos] && ' ' == buf[*pos])
645 (*pos)++;
646
647 return(t);
648 }
649
650 /* ARGSUSED */
651 static enum rofferr
652 roff_cblock(ROFF_ARGS)
653 {
654
655 /*
656 * A block-close `..' should only be invoked as a child of an
657 * ignore macro, otherwise raise a warning and just ignore it.
658 */
659
660 if (NULL == r->last) {
661 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
662 return(ROFF_IGN);
663 }
664
665 switch (r->last->tok) {
666 case (ROFF_am):
667 /* FALLTHROUGH */
668 case (ROFF_ami):
669 /* FALLTHROUGH */
670 case (ROFF_am1):
671 /* FALLTHROUGH */
672 case (ROFF_de):
673 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
674 /* FALLTHROUGH */
675 case (ROFF_dei):
676 /* FALLTHROUGH */
677 case (ROFF_ig):
678 break;
679 default:
680 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
681 return(ROFF_IGN);
682 }
683
684 if ((*bufp)[pos])
685 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
686
687 roffnode_pop(r);
688 roffnode_cleanscope(r);
689 return(ROFF_IGN);
690
691 }
692
693
694 static void
695 roffnode_cleanscope(struct roff *r)
696 {
697
698 while (r->last) {
699 if (--r->last->endspan < 0)
700 break;
701 roffnode_pop(r);
702 }
703 }
704
705
706 /* ARGSUSED */
707 static enum rofferr
708 roff_ccond(ROFF_ARGS)
709 {
710
711 if (NULL == r->last) {
712 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
713 return(ROFF_IGN);
714 }
715
716 switch (r->last->tok) {
717 case (ROFF_el):
718 /* FALLTHROUGH */
719 case (ROFF_ie):
720 /* FALLTHROUGH */
721 case (ROFF_if):
722 break;
723 default:
724 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
725 return(ROFF_IGN);
726 }
727
728 if (r->last->endspan > -1) {
729 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
730 return(ROFF_IGN);
731 }
732
733 if ((*bufp)[pos])
734 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
735
736 roffnode_pop(r);
737 roffnode_cleanscope(r);
738 return(ROFF_IGN);
739 }
740
741
742 /* ARGSUSED */
743 static enum rofferr
744 roff_block(ROFF_ARGS)
745 {
746 int sv;
747 size_t sz;
748 char *name;
749
750 name = NULL;
751
752 if (ROFF_ig != tok) {
753 if ('\0' == (*bufp)[pos]) {
754 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
755 return(ROFF_IGN);
756 }
757
758 /*
759 * Re-write `de1', since we don't really care about
760 * groff's strange compatibility mode, into `de'.
761 */
762
763 if (ROFF_de1 == tok)
764 tok = ROFF_de;
765 if (ROFF_de == tok)
766 name = *bufp + pos;
767 else
768 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
769 roffs[tok].name);
770
771 while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
772 pos++;
773
774 while (isspace((unsigned char)(*bufp)[pos]))
775 (*bufp)[pos++] = '\0';
776 }
777
778 roffnode_push(r, tok, name, ln, ppos);
779
780 /*
781 * At the beginning of a `de' macro, clear the existing string
782 * with the same name, if there is one. New content will be
783 * added from roff_block_text() in multiline mode.
784 */
785
786 if (ROFF_de == tok)
787 roff_setstr(r, name, "", 0);
788
789 if ('\0' == (*bufp)[pos])
790 return(ROFF_IGN);
791
792 /* If present, process the custom end-of-line marker. */
793
794 sv = pos;
795 while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
796 pos++;
797
798 /*
799 * Note: groff does NOT like escape characters in the input.
800 * Instead of detecting this, we're just going to let it fly and
801 * to hell with it.
802 */
803
804 assert(pos > sv);
805 sz = (size_t)(pos - sv);
806
807 if (1 == sz && '.' == (*bufp)[sv])
808 return(ROFF_IGN);
809
810 r->last->end = mandoc_malloc(sz + 1);
811
812 memcpy(r->last->end, *bufp + sv, sz);
813 r->last->end[(int)sz] = '\0';
814
815 if ((*bufp)[pos])
816 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
817
818 return(ROFF_IGN);
819 }
820
821
822 /* ARGSUSED */
823 static enum rofferr
824 roff_block_sub(ROFF_ARGS)
825 {
826 enum rofft t;
827 int i, j;
828
829 /*
830 * First check whether a custom macro exists at this level. If
831 * it does, then check against it. This is some of groff's
832 * stranger behaviours. If we encountered a custom end-scope
833 * tag and that tag also happens to be a "real" macro, then we
834 * need to try interpreting it again as a real macro. If it's
835 * not, then return ignore. Else continue.
836 */
837
838 if (r->last->end) {
839 for (i = pos, j = 0; r->last->end[j]; j++, i++)
840 if ((*bufp)[i] != r->last->end[j])
841 break;
842
843 if ('\0' == r->last->end[j] &&
844 ('\0' == (*bufp)[i] ||
845 ' ' == (*bufp)[i] ||
846 '\t' == (*bufp)[i])) {
847 roffnode_pop(r);
848 roffnode_cleanscope(r);
849
850 while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
851 i++;
852
853 pos = i;
854 if (ROFF_MAX != roff_parse(r, *bufp, &pos))
855 return(ROFF_RERUN);
856 return(ROFF_IGN);
857 }
858 }
859
860 /*
861 * If we have no custom end-query or lookup failed, then try
862 * pulling it out of the hashtable.
863 */
864
865 t = roff_parse(r, *bufp, &pos);
866
867 /*
868 * Macros other than block-end are only significant
869 * in `de' blocks; elsewhere, simply throw them away.
870 */
871 if (ROFF_cblock != t) {
872 if (ROFF_de == tok)
873 roff_setstr(r, r->last->name, *bufp + ppos, 1);
874 return(ROFF_IGN);
875 }
876
877 assert(roffs[t].proc);
878 return((*roffs[t].proc)(r, t, bufp, szp,
879 ln, ppos, pos, offs));
880 }
881
882
883 /* ARGSUSED */
884 static enum rofferr
885 roff_block_text(ROFF_ARGS)
886 {
887
888 if (ROFF_de == tok)
889 roff_setstr(r, r->last->name, *bufp + pos, 1);
890
891 return(ROFF_IGN);
892 }
893
894
895 /* ARGSUSED */
896 static enum rofferr
897 roff_cond_sub(ROFF_ARGS)
898 {
899 enum rofft t;
900 enum roffrule rr;
901 char *ep;
902
903 rr = r->last->rule;
904 roffnode_cleanscope(r);
905
906 /*
907 * If the macro is unknown, first check if it contains a closing
908 * delimiter `\}'. If it does, close out our scope and return
909 * the currently-scoped rule (ignore or continue). Else, drop
910 * into the currently-scoped rule.
911 */
912
913 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
914 ep = &(*bufp)[pos];
915 for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
916 ep++;
917 if ('}' != *ep)
918 continue;
919
920 /*
921 * Make the \} go away.
922 * This is a little haphazard, as it's not quite
923 * clear how nroff does this.
924 * If we're at the end of line, then just chop
925 * off the \} and resize the buffer.
926 * If we aren't, then conver it to spaces.
927 */
928
929 if ('\0' == *(ep + 1)) {
930 *--ep = '\0';
931 *szp -= 2;
932 } else
933 *(ep - 1) = *ep = ' ';
934
935 roff_ccond(r, ROFF_ccond, bufp, szp,
936 ln, pos, pos + 2, offs);
937 break;
938 }
939 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
940 }
941
942 /*
943 * A denied conditional must evaluate its children if and only
944 * if they're either structurally required (such as loops and
945 * conditionals) or a closing macro.
946 */
947
948 if (ROFFRULE_DENY == rr)
949 if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
950 if (ROFF_ccond != t)
951 return(ROFF_IGN);
952
953 assert(roffs[t].proc);
954 return((*roffs[t].proc)(r, t, bufp, szp,
955 ln, ppos, pos, offs));
956 }
957
958 /* ARGSUSED */
959 static enum rofferr
960 roff_cond_text(ROFF_ARGS)
961 {
962 char *ep;
963 enum roffrule rr;
964
965 rr = r->last->rule;
966 roffnode_cleanscope(r);
967
968 ep = &(*bufp)[pos];
969 for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
970 ep++;
971 if ('}' != *ep)
972 continue;
973 *ep = '&';
974 roff_ccond(r, ROFF_ccond, bufp, szp,
975 ln, pos, pos + 2, offs);
976 }
977 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
978 }
979
980 static enum roffrule
981 roff_evalcond(const char *v, int *pos)
982 {
983
984 switch (v[*pos]) {
985 case ('n'):
986 (*pos)++;
987 return(ROFFRULE_ALLOW);
988 case ('e'):
989 /* FALLTHROUGH */
990 case ('o'):
991 /* FALLTHROUGH */
992 case ('t'):
993 (*pos)++;
994 return(ROFFRULE_DENY);
995 default:
996 break;
997 }
998
999 while (v[*pos] && ' ' != v[*pos])
1000 (*pos)++;
1001 return(ROFFRULE_DENY);
1002 }
1003
1004 /* ARGSUSED */
1005 static enum rofferr
1006 roff_line_ignore(ROFF_ARGS)
1007 {
1008
1009 if (ROFF_it == tok)
1010 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
1011
1012 return(ROFF_IGN);
1013 }
1014
1015 /* ARGSUSED */
1016 static enum rofferr
1017 roff_cond(ROFF_ARGS)
1018 {
1019 int sv;
1020 enum roffrule rule;
1021
1022 /*
1023 * An `.el' has no conditional body: it will consume the value
1024 * of the current rstack entry set in prior `ie' calls or
1025 * defaults to DENY.
1026 *
1027 * If we're not an `el', however, then evaluate the conditional.
1028 */
1029
1030 rule = ROFF_el == tok ?
1031 (r->rstackpos < 0 ?
1032 ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
1033 roff_evalcond(*bufp, &pos);
1034
1035 sv = pos;
1036 while (' ' == (*bufp)[pos])
1037 pos++;
1038
1039 /*
1040 * Roff is weird. If we have just white-space after the
1041 * conditional, it's considered the BODY and we exit without
1042 * really doing anything. Warn about this. It's probably
1043 * wrong.
1044 */
1045
1046 if ('\0' == (*bufp)[pos] && sv != pos) {
1047 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1048 return(ROFF_IGN);
1049 }
1050
1051 roffnode_push(r, tok, NULL, ln, ppos);
1052
1053 r->last->rule = rule;
1054
1055 /*
1056 * An if-else will put the NEGATION of the current evaluated
1057 * conditional into the stack of rules.
1058 */
1059
1060 if (ROFF_ie == tok) {
1061 if (r->rstackpos == RSTACK_MAX - 1) {
1062 mandoc_msg(MANDOCERR_MEM,
1063 r->parse, ln, ppos, NULL);
1064 return(ROFF_ERR);
1065 }
1066 r->rstack[++r->rstackpos] =
1067 ROFFRULE_DENY == r->last->rule ?
1068 ROFFRULE_ALLOW : ROFFRULE_DENY;
1069 }
1070
1071 /* If the parent has false as its rule, then so do we. */
1072
1073 if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1074 r->last->rule = ROFFRULE_DENY;
1075
1076 /*
1077 * Determine scope. If we're invoked with "\{" trailing the
1078 * conditional, then we're in a multiline scope. Else our scope
1079 * expires on the next line.
1080 */
1081
1082 r->last->endspan = 1;
1083
1084 if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1085 r->last->endspan = -1;
1086 pos += 2;
1087 }
1088
1089 /*
1090 * If there are no arguments on the line, the next-line scope is
1091 * assumed.
1092 */
1093
1094 if ('\0' == (*bufp)[pos])
1095 return(ROFF_IGN);
1096
1097 /* Otherwise re-run the roff parser after recalculating. */
1098
1099 *offs = pos;
1100 return(ROFF_RERUN);
1101 }
1102
1103
1104 /* ARGSUSED */
1105 static enum rofferr
1106 roff_ds(ROFF_ARGS)
1107 {
1108 char *name, *string;
1109
1110 /*
1111 * A symbol is named by the first word following the macro
1112 * invocation up to a space. Its value is anything after the
1113 * name's trailing whitespace and optional double-quote. Thus,
1114 *
1115 * [.ds foo "bar " ]
1116 *
1117 * will have `bar " ' as its value.
1118 */
1119
1120 string = *bufp + pos;
1121 name = roff_getname(r, &string, ln, pos);
1122 if ('\0' == *name)
1123 return(ROFF_IGN);
1124
1125 /* Read past initial double-quote. */
1126 if ('"' == *string)
1127 string++;
1128
1129 /* The rest is the value. */
1130 roff_setstr(r, name, string, 0);
1131 return(ROFF_IGN);
1132 }
1133
1134 int
1135 roff_regisset(const struct roff *r, enum regs reg)
1136 {
1137
1138 return(r->regs[(int)reg].set);
1139 }
1140
1141 unsigned int
1142 roff_regget(const struct roff *r, enum regs reg)
1143 {
1144
1145 return(r->regs[(int)reg].u);
1146 }
1147
1148 void
1149 roff_regunset(struct roff *r, enum regs reg)
1150 {
1151
1152 r->regs[(int)reg].set = 0;
1153 }
1154
1155 /* ARGSUSED */
1156 static enum rofferr
1157 roff_nr(ROFF_ARGS)
1158 {
1159 const char *key;
1160 char *val;
1161 int iv;
1162
1163 val = *bufp + pos;
1164 key = roff_getname(r, &val, ln, pos);
1165
1166 if (0 == strcmp(key, "nS")) {
1167 r->regs[(int)REG_nS].set = 1;
1168 if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
1169 r->regs[(int)REG_nS].u = (unsigned)iv;
1170 else
1171 r->regs[(int)REG_nS].u = 0u;
1172 }
1173
1174 return(ROFF_IGN);
1175 }
1176
1177 /* ARGSUSED */
1178 static enum rofferr
1179 roff_rm(ROFF_ARGS)
1180 {
1181 const char *name;
1182 char *cp;
1183
1184 cp = *bufp + pos;
1185 while ('\0' != *cp) {
1186 name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1187 if ('\0' != *name)
1188 roff_setstr(r, name, NULL, 0);
1189 }
1190 return(ROFF_IGN);
1191 }
1192
1193 /* ARGSUSED */
1194 static enum rofferr
1195 roff_TE(ROFF_ARGS)
1196 {
1197
1198 if (NULL == r->tbl)
1199 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1200 else
1201 tbl_end(&r->tbl);
1202
1203 return(ROFF_IGN);
1204 }
1205
1206 /* ARGSUSED */
1207 static enum rofferr
1208 roff_T_(ROFF_ARGS)
1209 {
1210
1211 if (NULL == r->tbl)
1212 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1213 else
1214 tbl_restart(ppos, ln, r->tbl);
1215
1216 return(ROFF_IGN);
1217 }
1218
1219 int
1220 roff_closeeqn(struct roff *r)
1221 {
1222
1223 return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1224 }
1225
1226 void
1227 roff_openeqn(struct roff *r, const char *name, int line,
1228 int offs, const char *buf)
1229 {
1230 struct eqn_node *e;
1231 int poff;
1232
1233 assert(NULL == r->eqn);
1234 e = eqn_alloc(name, offs, line, r->parse);
1235
1236 if (r->last_eqn)
1237 r->last_eqn->next = e;
1238 else
1239 r->first_eqn = r->last_eqn = e;
1240
1241 r->eqn = r->last_eqn = e;
1242
1243 if (buf) {
1244 poff = 0;
1245 eqn_read(&r->eqn, line, buf, offs, &poff);
1246 }
1247 }
1248
1249 /* ARGSUSED */
1250 static enum rofferr
1251 roff_EQ(ROFF_ARGS)
1252 {
1253
1254 roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1255 return(ROFF_IGN);
1256 }
1257
1258 /* ARGSUSED */
1259 static enum rofferr
1260 roff_EN(ROFF_ARGS)
1261 {
1262
1263 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1264 return(ROFF_IGN);
1265 }
1266
1267 /* ARGSUSED */
1268 static enum rofferr
1269 roff_TS(ROFF_ARGS)
1270 {
1271 struct tbl_node *t;
1272
1273 if (r->tbl) {
1274 mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1275 tbl_end(&r->tbl);
1276 }
1277
1278 t = tbl_alloc(ppos, ln, r->parse);
1279
1280 if (r->last_tbl)
1281 r->last_tbl->next = t;
1282 else
1283 r->first_tbl = r->last_tbl = t;
1284
1285 r->tbl = r->last_tbl = t;
1286 return(ROFF_IGN);
1287 }
1288
1289 /* ARGSUSED */
1290 static enum rofferr
1291 roff_so(ROFF_ARGS)
1292 {
1293 char *name;
1294
1295 mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1296
1297 /*
1298 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1299 * opening anything that's not in our cwd or anything beneath
1300 * it. Thus, explicitly disallow traversing up the file-system
1301 * or using absolute paths.
1302 */
1303
1304 name = *bufp + pos;
1305 if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1306 mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1307 return(ROFF_ERR);
1308 }
1309
1310 *offs = pos;
1311 return(ROFF_SO);
1312 }
1313
1314 /* ARGSUSED */
1315 static enum rofferr
1316 roff_userdef(ROFF_ARGS)
1317 {
1318 const char *arg[9];
1319 char *cp, *n1, *n2;
1320 int i;
1321
1322 /*
1323 * Collect pointers to macro argument strings
1324 * and null-terminate them.
1325 */
1326 cp = *bufp + pos;
1327 for (i = 0; i < 9; i++)
1328 arg[i] = '\0' == *cp ? "" :
1329 mandoc_getarg(r->parse, &cp, ln, &pos);
1330
1331 /*
1332 * Expand macro arguments.
1333 */
1334 *szp = 0;
1335 n1 = cp = mandoc_strdup(r->current_string);
1336 while (NULL != (cp = strstr(cp, "\\$"))) {
1337 i = cp[2] - '1';
1338 if (0 > i || 8 < i) {
1339 /* Not an argument invocation. */
1340 cp += 2;
1341 continue;
1342 }
1343
1344 *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1345 n2 = mandoc_malloc(*szp);
1346
1347 strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1348 strlcat(n2, arg[i], *szp);
1349 strlcat(n2, cp + 3, *szp);
1350
1351 cp = n2 + (cp - n1);
1352 free(n1);
1353 n1 = n2;
1354 }
1355
1356 /*
1357 * Replace the macro invocation
1358 * by the expanded macro.
1359 */
1360 free(*bufp);
1361 *bufp = n1;
1362 if (0 == *szp)
1363 *szp = strlen(*bufp) + 1;
1364
1365 return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1366 ROFF_REPARSE : ROFF_APPEND);
1367 }
1368
1369 static char *
1370 roff_getname(struct roff *r, char **cpp, int ln, int pos)
1371 {
1372 char *name, *cp;
1373
1374 name = *cpp;
1375 if ('\0' == *name)
1376 return(name);
1377
1378 /* Read until end of name. */
1379 for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
1380 if ('\\' != *cp)
1381 continue;
1382 cp++;
1383 if ('\\' == *cp)
1384 continue;
1385 mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1386 *cp = '\0';
1387 name = cp;
1388 }
1389
1390 /* Nil-terminate name. */
1391 if ('\0' != *cp)
1392 *(cp++) = '\0';
1393
1394 /* Read past spaces. */
1395 while (' ' == *cp)
1396 cp++;
1397
1398 *cpp = cp;
1399 return(name);
1400 }
1401
1402 /*
1403 * Store *string into the user-defined string called *name.
1404 * In multiline mode, append to an existing entry and append '\n';
1405 * else replace the existing entry, if there is one.
1406 * To clear an existing entry, call with (*r, *name, NULL, 0).
1407 */
1408 static void
1409 roff_setstr(struct roff *r, const char *name, const char *string,
1410 int multiline)
1411 {
1412 struct roffstr *n;
1413 char *c;
1414 size_t oldch, newch;
1415
1416 /* Search for an existing string with the same name. */
1417 n = r->first_string;
1418 while (n && strcmp(name, n->name))
1419 n = n->next;
1420
1421 if (NULL == n) {
1422 /* Create a new string table entry. */
1423 n = mandoc_malloc(sizeof(struct roffstr));
1424 n->name = mandoc_strdup(name);
1425 n->string = NULL;
1426 n->next = r->first_string;
1427 r->first_string = n;
1428 } else if (0 == multiline) {
1429 /* In multiline mode, append; else replace. */
1430 free(n->string);
1431 n->string = NULL;
1432 }
1433
1434 if (NULL == string)
1435 return;
1436
1437 /*
1438 * One additional byte for the '\n' in multiline mode,
1439 * and one for the terminating '\0'.
1440 */
1441 newch = strlen(string) + (multiline ? 2u : 1u);
1442 if (NULL == n->string) {
1443 n->string = mandoc_malloc(newch);
1444 *n->string = '\0';
1445 oldch = 0;
1446 } else {
1447 oldch = strlen(n->string);
1448 n->string = mandoc_realloc(n->string, oldch + newch);
1449 }
1450
1451 /* Skip existing content in the destination buffer. */
1452 c = n->string + (int)oldch;
1453
1454 /* Append new content to the destination buffer. */
1455 while (*string) {
1456 /*
1457 * Rudimentary roff copy mode:
1458 * Handle escaped backslashes.
1459 */
1460 if ('\\' == *string && '\\' == *(string + 1))
1461 string++;
1462 *c++ = *string++;
1463 }
1464
1465 /* Append terminating bytes. */
1466 if (multiline)
1467 *c++ = '\n';
1468 *c = '\0';
1469 }
1470
1471 static const char *
1472 roff_getstrn(const struct roff *r, const char *name, size_t len)
1473 {
1474 const struct roffstr *n;
1475
1476 n = r->first_string;
1477 while (n && (strncmp(name, n->name, len) || '\0' != n->name[(int)len]))
1478 n = n->next;
1479
1480 return(n ? n->string : NULL);
1481 }
1482
1483 static void
1484 roff_freestr(struct roff *r)
1485 {
1486 struct roffstr *n, *nn;
1487
1488 for (n = r->first_string; n; n = nn) {
1489 free(n->name);
1490 free(n->string);
1491 nn = n->next;
1492 free(n);
1493 }
1494
1495 r->first_string = NULL;
1496 }
1497
1498 const struct tbl_span *
1499 roff_span(const struct roff *r)
1500 {
1501
1502 return(r->tbl ? tbl_span(r->tbl) : NULL);
1503 }
1504
1505 const struct eqn *
1506 roff_eqn(const struct roff *r)
1507 {
1508
1509 return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1510 }
1511
1512 char
1513 roff_eqndelim(const struct roff *r)
1514 {
1515
1516 return('\0');
1517 }