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