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