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