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