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