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