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