Annotation of mandoc/roff.c, Revision 1.165
1.165 ! kristaps 1: /* $Id: roff.c,v 1.164 2011/07/28 14:17:11 kristaps Exp $ */
1.1 kristaps 2: /*
1.119 schwarze 3: * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.66 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.106 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.66 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.106 kristaps 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.66 kristaps 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.66 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
1.30 kristaps 21:
1.67 kristaps 22: #include <assert.h>
1.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.147 kristaps 74: /*
75: * A single register entity. If "set" is zero, the value of the
76: * register should be the default one, which is per-register.
77: * Registers are assumed to be unsigned ints for now.
78: */
79: struct reg {
80: int set; /* whether set or not */
81: unsigned int u; /* unsigned integer */
82: };
83:
1.94 kristaps 84: struct roffstr {
1.160 kristaps 85: char *key; /* key of symbol */
1.162 kristaps 86: size_t keysz;
1.160 kristaps 87: char *val; /* current value */
1.162 kristaps 88: size_t valsz;
1.94 kristaps 89: struct roffstr *next; /* next in list */
90: };
91:
1.67 kristaps 92: struct roff {
1.128 kristaps 93: struct mparse *parse; /* parse point */
1.67 kristaps 94: struct roffnode *last; /* leaf of stack */
1.82 kristaps 95: enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
96: int rstackpos; /* position in rstack */
1.147 kristaps 97: struct reg regs[REG__MAX];
1.164 kristaps 98: struct roffstr *strtab; /* user-defined strings & macros */
99: struct roffstr *chrtab; /* user-defined characters */
1.106 kristaps 100: const char *current_string; /* value of last called user macro */
1.118 kristaps 101: struct tbl_node *first_tbl; /* first table parsed */
102: struct tbl_node *last_tbl; /* last table parsed */
103: struct tbl_node *tbl; /* current table being parsed */
1.125 kristaps 104: struct eqn_node *last_eqn; /* last equation parsed */
105: struct eqn_node *first_eqn; /* first equation parsed */
106: struct eqn_node *eqn; /* current equation being parsed */
1.79 kristaps 107: };
108:
1.67 kristaps 109: struct roffnode {
110: enum rofft tok; /* type of node */
111: struct roffnode *parent; /* up one in stack */
112: int line; /* parse line */
113: int col; /* parse col */
1.106 kristaps 114: char *name; /* node name, e.g. macro name */
1.79 kristaps 115: char *end; /* end-rules: custom token */
116: int endspan; /* end-rules: next-line or infty */
1.82 kristaps 117: enum roffrule rule; /* current evaluation rule */
1.67 kristaps 118: };
119:
120: #define ROFF_ARGS struct roff *r, /* parse ctx */ \
1.72 kristaps 121: enum rofft tok, /* tok of macro */ \
1.67 kristaps 122: char **bufp, /* input buffer */ \
123: size_t *szp, /* size of input buffer */ \
124: int ln, /* parse line */ \
1.75 kristaps 125: int ppos, /* original pos in buffer */ \
126: int pos, /* current pos in buffer */ \
1.74 kristaps 127: int *offs /* reset offset of buffer data */
1.67 kristaps 128:
129: typedef enum rofferr (*roffproc)(ROFF_ARGS);
130:
131: struct roffmac {
132: const char *name; /* macro name */
1.79 kristaps 133: roffproc proc; /* process new macro */
134: roffproc text; /* process as child text of macro */
135: roffproc sub; /* process as child of macro */
136: int flags;
137: #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
1.85 kristaps 138: struct roffmac *next;
1.67 kristaps 139: };
140:
1.141 kristaps 141: struct predef {
142: const char *name; /* predefined input name */
143: const char *str; /* replacement symbol */
144: };
145:
146: #define PREDEF(__name, __str) \
147: { (__name), (__str) },
148:
1.155 kristaps 149: static enum rofft roffhash_find(const char *, size_t);
150: static void roffhash_init(void);
151: static void roffnode_cleanscope(struct roff *);
152: static void roffnode_pop(struct roff *);
153: static void roffnode_push(struct roff *, enum rofft,
154: const char *, int, int);
1.80 kristaps 155: static enum rofferr roff_block(ROFF_ARGS);
156: static enum rofferr roff_block_text(ROFF_ARGS);
157: static enum rofferr roff_block_sub(ROFF_ARGS);
158: static enum rofferr roff_cblock(ROFF_ARGS);
159: static enum rofferr roff_ccond(ROFF_ARGS);
1.82 kristaps 160: static enum rofferr roff_cond(ROFF_ARGS);
161: static enum rofferr roff_cond_text(ROFF_ARGS);
162: static enum rofferr roff_cond_sub(ROFF_ARGS);
1.92 schwarze 163: static enum rofferr roff_ds(ROFF_ARGS);
1.94 kristaps 164: static enum roffrule roff_evalcond(const char *, int *);
1.155 kristaps 165: static void roff_free1(struct roff *);
1.164 kristaps 166: static void roff_freestr(struct roffstr **);
1.121 schwarze 167: static char *roff_getname(struct roff *, char **, int, int);
1.94 kristaps 168: static const char *roff_getstrn(const struct roff *,
169: const char *, size_t);
1.103 kristaps 170: static enum rofferr roff_line_ignore(ROFF_ARGS);
1.89 kristaps 171: static enum rofferr roff_nr(ROFF_ARGS);
1.156 kristaps 172: static void roff_openeqn(struct roff *, const char *,
173: int, int, const char *);
1.155 kristaps 174: static enum rofft roff_parse(struct roff *, const char *, int *);
175: static enum rofferr roff_parsetext(char *);
1.154 kristaps 176: static void roff_res(struct roff *,
1.142 kristaps 177: char **, size_t *, int, int);
1.122 schwarze 178: static enum rofferr roff_rm(ROFF_ARGS);
1.94 kristaps 179: static void roff_setstr(struct roff *,
1.106 kristaps 180: const char *, const char *, int);
1.164 kristaps 181: static void roff_setstrn(struct roffstr **, const char *,
182: size_t, const char *, size_t, int);
1.105 kristaps 183: static enum rofferr roff_so(ROFF_ARGS);
1.164 kristaps 184: static enum rofferr roff_tr(ROFF_ARGS);
1.109 kristaps 185: static enum rofferr roff_TE(ROFF_ARGS);
186: static enum rofferr roff_TS(ROFF_ARGS);
1.125 kristaps 187: static enum rofferr roff_EQ(ROFF_ARGS);
188: static enum rofferr roff_EN(ROFF_ARGS);
1.112 kristaps 189: static enum rofferr roff_T_(ROFF_ARGS);
1.106 kristaps 190: static enum rofferr roff_userdef(ROFF_ARGS);
1.67 kristaps 191:
1.155 kristaps 192: /* See roffhash_find() */
1.85 kristaps 193:
194: #define ASCII_HI 126
195: #define ASCII_LO 33
196: #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
197:
198: static struct roffmac *hash[HASHWIDTH];
199:
200: static struct roffmac roffs[ROFF_MAX] = {
1.103 kristaps 201: { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 202: { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
203: { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
204: { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
205: { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
206: { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
207: { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92 schwarze 208: { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85 kristaps 209: { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.103 kristaps 210: { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 211: { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
212: { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
213: { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.123 schwarze 214: { "it", roff_line_ignore, NULL, NULL, 0, NULL },
1.103 kristaps 215: { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
216: { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.104 kristaps 217: { "nr", roff_nr, NULL, NULL, 0, NULL },
1.124 schwarze 218: { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
219: { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.122 schwarze 220: { "rm", roff_rm, NULL, NULL, 0, NULL },
1.105 kristaps 221: { "so", roff_so, NULL, NULL, 0, NULL },
1.124 schwarze 222: { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
1.164 kristaps 223: { "tr", roff_tr, NULL, NULL, 0, NULL },
1.109 kristaps 224: { "TS", roff_TS, NULL, NULL, 0, NULL },
225: { "TE", roff_TE, NULL, NULL, 0, NULL },
1.112 kristaps 226: { "T&", roff_T_, NULL, NULL, 0, NULL },
1.125 kristaps 227: { "EQ", roff_EQ, NULL, NULL, 0, NULL },
228: { "EN", roff_EN, NULL, NULL, 0, NULL },
1.85 kristaps 229: { ".", roff_cblock, NULL, NULL, 0, NULL },
230: { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.106 kristaps 231: { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67 kristaps 232: };
233:
1.141 kristaps 234: /* Array of injected predefined strings. */
235: #define PREDEFS_MAX 38
236: static const struct predef predefs[PREDEFS_MAX] = {
237: #include "predefs.in"
238: };
239:
1.155 kristaps 240: /* See roffhash_find() */
1.85 kristaps 241: #define ROFF_HASH(p) (p[0] - ASCII_LO)
242:
243: static void
1.155 kristaps 244: roffhash_init(void)
1.85 kristaps 245: {
246: struct roffmac *n;
247: int buc, i;
248:
1.106 kristaps 249: for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85 kristaps 250: assert(roffs[i].name[0] >= ASCII_LO);
251: assert(roffs[i].name[0] <= ASCII_HI);
252:
253: buc = ROFF_HASH(roffs[i].name);
254:
255: if (NULL != (n = hash[buc])) {
256: for ( ; n->next; n = n->next)
257: /* Do nothing. */ ;
258: n->next = &roffs[i];
259: } else
260: hash[buc] = &roffs[i];
261: }
262: }
263:
1.67 kristaps 264: /*
265: * Look up a roff token by its name. Returns ROFF_MAX if no macro by
266: * the nil-terminated string name could be found.
267: */
268: static enum rofft
1.155 kristaps 269: roffhash_find(const char *p, size_t s)
1.67 kristaps 270: {
1.85 kristaps 271: int buc;
272: struct roffmac *n;
1.67 kristaps 273:
1.85 kristaps 274: /*
275: * libroff has an extremely simple hashtable, for the time
276: * being, which simply keys on the first character, which must
277: * be printable, then walks a chain. It works well enough until
278: * optimised.
279: */
280:
281: if (p[0] < ASCII_LO || p[0] > ASCII_HI)
282: return(ROFF_MAX);
283:
284: buc = ROFF_HASH(p);
285:
286: if (NULL == (n = hash[buc]))
287: return(ROFF_MAX);
288: for ( ; n; n = n->next)
1.106 kristaps 289: if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85 kristaps 290: return((enum rofft)(n - roffs));
1.67 kristaps 291:
292: return(ROFF_MAX);
293: }
294:
295:
296: /*
297: * Pop the current node off of the stack of roff instructions currently
298: * pending.
299: */
300: static void
301: roffnode_pop(struct roff *r)
302: {
303: struct roffnode *p;
304:
1.75 kristaps 305: assert(r->last);
306: p = r->last;
1.82 kristaps 307:
1.75 kristaps 308: r->last = r->last->parent;
1.106 kristaps 309: free(p->name);
310: free(p->end);
1.67 kristaps 311: free(p);
312: }
313:
314:
315: /*
316: * Push a roff node onto the instruction stack. This must later be
317: * removed with roffnode_pop().
318: */
1.98 schwarze 319: static void
1.106 kristaps 320: roffnode_push(struct roff *r, enum rofft tok, const char *name,
321: int line, int col)
1.67 kristaps 322: {
323: struct roffnode *p;
324:
1.98 schwarze 325: p = mandoc_calloc(1, sizeof(struct roffnode));
1.67 kristaps 326: p->tok = tok;
1.106 kristaps 327: if (name)
328: p->name = mandoc_strdup(name);
1.67 kristaps 329: p->parent = r->last;
330: p->line = line;
331: p->col = col;
1.79 kristaps 332: p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67 kristaps 333:
334: r->last = p;
335: }
336:
337:
338: static void
339: roff_free1(struct roff *r)
340: {
1.118 kristaps 341: struct tbl_node *t;
1.125 kristaps 342: struct eqn_node *e;
1.67 kristaps 343:
1.125 kristaps 344: while (NULL != (t = r->first_tbl)) {
1.113 kristaps 345: r->first_tbl = t->next;
346: tbl_free(t);
1.109 kristaps 347: }
348:
1.113 kristaps 349: r->first_tbl = r->last_tbl = r->tbl = NULL;
350:
1.125 kristaps 351: while (NULL != (e = r->first_eqn)) {
352: r->first_eqn = e->next;
353: eqn_free(e);
354: }
355:
356: r->first_eqn = r->last_eqn = r->eqn = NULL;
357:
1.67 kristaps 358: while (r->last)
359: roffnode_pop(r);
1.109 kristaps 360:
1.164 kristaps 361: roff_freestr(&r->strtab);
362: roff_freestr(&r->chrtab);
1.67 kristaps 363: }
364:
365:
366: void
367: roff_reset(struct roff *r)
368: {
1.143 kristaps 369: int i;
1.67 kristaps 370:
371: roff_free1(r);
1.143 kristaps 372:
1.147 kristaps 373: memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);
374:
1.143 kristaps 375: for (i = 0; i < PREDEFS_MAX; i++)
376: roff_setstr(r, predefs[i].name, predefs[i].str, 0);
1.67 kristaps 377: }
378:
379:
380: void
381: roff_free(struct roff *r)
382: {
383:
384: roff_free1(r);
385: free(r);
386: }
387:
388:
389: struct roff *
1.147 kristaps 390: roff_alloc(struct mparse *parse)
1.67 kristaps 391: {
392: struct roff *r;
1.141 kristaps 393: int i;
1.67 kristaps 394:
1.98 schwarze 395: r = mandoc_calloc(1, sizeof(struct roff));
1.128 kristaps 396: r->parse = parse;
1.82 kristaps 397: r->rstackpos = -1;
1.85 kristaps 398:
1.155 kristaps 399: roffhash_init();
1.141 kristaps 400:
401: for (i = 0; i < PREDEFS_MAX; i++)
402: roff_setstr(r, predefs[i].name, predefs[i].str, 0);
403:
1.67 kristaps 404: return(r);
405: }
406:
1.94 kristaps 407: /*
408: * Pre-filter each and every line for reserved words (one beginning with
409: * `\*', e.g., `\*(ab'). These must be handled before the actual line
410: * is processed.
1.153 kristaps 411: * This also checks the syntax of regular escapes.
1.154 kristaps 412: */
413: static void
1.142 kristaps 414: roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
1.94 kristaps 415: {
1.152 kristaps 416: enum mandoc_esc esc;
1.108 schwarze 417: const char *stesc; /* start of an escape sequence ('\\') */
418: const char *stnam; /* start of the name, after "[(*" */
419: const char *cp; /* end of the name, e.g. before ']' */
420: const char *res; /* the string to be substituted */
1.94 kristaps 421: int i, maxl;
422: size_t nsz;
423: char *n;
424:
1.154 kristaps 425: again:
1.108 schwarze 426: cp = *bufp + pos;
427: while (NULL != (cp = strchr(cp, '\\'))) {
428: stesc = cp++;
429:
430: /*
431: * The second character must be an asterisk.
432: * If it isn't, skip it anyway: It is escaped,
433: * so it can't start another escape sequence.
434: */
435:
436: if ('\0' == *cp)
1.154 kristaps 437: return;
1.152 kristaps 438:
439: if ('*' != *cp) {
440: res = cp;
441: esc = mandoc_escape(&cp, NULL, NULL);
442: if (ESCAPE_ERROR != esc)
443: continue;
444: cp = res;
1.153 kristaps 445: mandoc_msg
446: (MANDOCERR_BADESCAPE, r->parse,
447: ln, (int)(stesc - *bufp), NULL);
1.154 kristaps 448: return;
1.152 kristaps 449: }
450:
451: cp++;
1.108 schwarze 452:
453: /*
454: * The third character decides the length
455: * of the name of the string.
456: * Save a pointer to the name.
457: */
458:
1.94 kristaps 459: switch (*cp) {
1.108 schwarze 460: case ('\0'):
1.154 kristaps 461: return;
1.94 kristaps 462: case ('('):
463: cp++;
464: maxl = 2;
465: break;
466: case ('['):
467: cp++;
468: maxl = 0;
469: break;
470: default:
471: maxl = 1;
472: break;
473: }
1.108 schwarze 474: stnam = cp;
1.94 kristaps 475:
1.108 schwarze 476: /* Advance to the end of the name. */
1.94 kristaps 477:
478: for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
1.153 kristaps 479: if ('\0' == *cp) {
480: mandoc_msg
481: (MANDOCERR_BADESCAPE,
482: r->parse, ln,
483: (int)(stesc - *bufp), NULL);
1.154 kristaps 484: return;
1.153 kristaps 485: }
1.94 kristaps 486: if (0 == maxl && ']' == *cp)
487: break;
488: }
489:
1.108 schwarze 490: /*
491: * Retrieve the replacement string; if it is
492: * undefined, resume searching for escapes.
493: */
494:
495: res = roff_getstrn(r, stnam, (size_t)i);
1.94 kristaps 496:
497: if (NULL == res) {
1.153 kristaps 498: mandoc_msg
499: (MANDOCERR_BADESCAPE, r->parse,
500: ln, (int)(stesc - *bufp), NULL);
1.142 kristaps 501: res = "";
1.94 kristaps 502: }
503:
1.108 schwarze 504: /* Replace the escape sequence by the string. */
505:
1.161 kristaps 506: pos = stesc - *bufp;
1.154 kristaps 507:
1.94 kristaps 508: nsz = *szp + strlen(res) + 1;
509: n = mandoc_malloc(nsz);
510:
1.108 schwarze 511: strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
1.94 kristaps 512: strlcat(n, res, nsz);
513: strlcat(n, cp + (maxl ? 0 : 1), nsz);
514:
515: free(*bufp);
516:
517: *bufp = n;
518: *szp = nsz;
1.154 kristaps 519: goto again;
520: }
521: }
522:
523: /*
524: * Process text streams: convert all breakable hyphens into ASCII_HYPH.
525: */
526: static enum rofferr
527: roff_parsetext(char *p)
528: {
1.155 kristaps 529: char l, r;
1.154 kristaps 530: size_t sz;
531: const char *start;
532: enum mandoc_esc esc;
533:
534: start = p;
535:
536: while ('\0' != *p) {
537: sz = strcspn(p, "-\\");
538: p += sz;
539:
1.159 kristaps 540: if ('\0' == *p)
541: break;
542:
1.154 kristaps 543: if ('\\' == *p) {
544: /* Skip over escapes. */
545: p++;
546: esc = mandoc_escape
547: ((const char **)&p, NULL, NULL);
548: if (ESCAPE_ERROR == esc)
549: break;
1.155 kristaps 550: continue;
1.159 kristaps 551: } else if (p == start) {
1.158 kristaps 552: p++;
1.155 kristaps 553: continue;
1.158 kristaps 554: }
1.155 kristaps 555:
556: l = *(p - 1);
557: r = *(p + 1);
558: if ('\\' != l &&
559: '\t' != r && '\t' != l &&
560: ' ' != r && ' ' != l &&
561: '-' != r && '-' != l &&
562: ! isdigit((unsigned char)l) &&
1.159 kristaps 563: ! isdigit((unsigned char)r))
1.155 kristaps 564: *p = ASCII_HYPH;
565: p++;
1.94 kristaps 566: }
567:
1.154 kristaps 568: return(ROFF_CONT);
1.94 kristaps 569: }
570:
1.67 kristaps 571: enum rofferr
1.90 kristaps 572: roff_parseln(struct roff *r, int ln, char **bufp,
573: size_t *szp, int pos, int *offs)
1.67 kristaps 574: {
575: enum rofft t;
1.109 kristaps 576: enum rofferr e;
1.130 kristaps 577: int ppos, ctl;
1.79 kristaps 578:
579: /*
1.94 kristaps 580: * Run the reserved-word filter only if we have some reserved
581: * words to fill in.
582: */
583:
1.154 kristaps 584: roff_res(r, bufp, szp, ln, pos);
1.94 kristaps 585:
1.130 kristaps 586: ppos = pos;
587: ctl = mandoc_getcontrol(*bufp, &pos);
588:
1.94 kristaps 589: /*
1.79 kristaps 590: * First, if a scope is open and we're not a macro, pass the
591: * text through the macro's filter. If a scope isn't open and
592: * we're not a macro, just let it through.
1.125 kristaps 593: * Finally, if there's an equation scope open, divert it into it
594: * no matter our state.
1.79 kristaps 595: */
1.74 kristaps 596:
1.130 kristaps 597: if (r->last && ! ctl) {
1.78 kristaps 598: t = r->last->tok;
599: assert(roffs[t].text);
1.109 kristaps 600: e = (*roffs[t].text)
601: (r, t, bufp, szp, ln, pos, pos, offs);
602: assert(ROFF_IGN == e || ROFF_CONT == e);
1.125 kristaps 603: if (ROFF_CONT != e)
604: return(e);
605: if (r->eqn)
1.146 kristaps 606: return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.125 kristaps 607: if (r->tbl)
1.130 kristaps 608: return(tbl_read(r->tbl, ln, *bufp, pos));
1.154 kristaps 609: return(roff_parsetext(*bufp + pos));
1.130 kristaps 610: } else if ( ! ctl) {
1.125 kristaps 611: if (r->eqn)
1.146 kristaps 612: return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.109 kristaps 613: if (r->tbl)
1.130 kristaps 614: return(tbl_read(r->tbl, ln, *bufp, pos));
1.154 kristaps 615: return(roff_parsetext(*bufp + pos));
1.125 kristaps 616: } else if (r->eqn)
1.146 kristaps 617: return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
1.67 kristaps 618:
1.79 kristaps 619: /*
620: * If a scope is open, go to the child handler for that macro,
621: * as it may want to preprocess before doing anything with it.
1.125 kristaps 622: * Don't do so if an equation is open.
1.79 kristaps 623: */
1.78 kristaps 624:
1.79 kristaps 625: if (r->last) {
626: t = r->last->tok;
627: assert(roffs[t].sub);
628: return((*roffs[t].sub)
1.90 kristaps 629: (r, t, bufp, szp,
1.130 kristaps 630: ln, ppos, pos, offs));
1.79 kristaps 631: }
1.78 kristaps 632:
1.79 kristaps 633: /*
634: * Lastly, as we've no scope open, try to look up and execute
635: * the new macro. If no macro is found, simply return and let
636: * the compilers handle it.
637: */
1.67 kristaps 638:
1.106 kristaps 639: if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
1.79 kristaps 640: return(ROFF_CONT);
1.67 kristaps 641:
1.75 kristaps 642: assert(roffs[t].proc);
1.78 kristaps 643: return((*roffs[t].proc)
1.90 kristaps 644: (r, t, bufp, szp,
645: ln, ppos, pos, offs));
1.74 kristaps 646: }
647:
648:
1.117 kristaps 649: void
1.74 kristaps 650: roff_endparse(struct roff *r)
651: {
652:
1.110 kristaps 653: if (r->last)
1.128 kristaps 654: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.109 kristaps 655: r->last->line, r->last->col, NULL);
1.117 kristaps 656:
1.125 kristaps 657: if (r->eqn) {
1.128 kristaps 658: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.148 kristaps 659: r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
1.151 kristaps 660: eqn_end(&r->eqn);
1.125 kristaps 661: }
662:
1.117 kristaps 663: if (r->tbl) {
1.128 kristaps 664: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.117 kristaps 665: r->tbl->line, r->tbl->pos, NULL);
1.151 kristaps 666: tbl_end(&r->tbl);
1.117 kristaps 667: }
1.67 kristaps 668: }
669:
670: /*
671: * Parse a roff node's type from the input buffer. This must be in the
672: * form of ".foo xxx" in the usual way.
673: */
674: static enum rofft
1.106 kristaps 675: roff_parse(struct roff *r, const char *buf, int *pos)
1.67 kristaps 676: {
1.106 kristaps 677: const char *mac;
678: size_t maclen;
1.67 kristaps 679: enum rofft t;
680:
1.144 kristaps 681: if ('\0' == buf[*pos] || '"' == buf[*pos] ||
682: '\t' == buf[*pos] || ' ' == buf[*pos])
1.67 kristaps 683: return(ROFF_MAX);
684:
1.144 kristaps 685: /*
686: * We stop the macro parse at an escape, tab, space, or nil.
687: * However, `\}' is also a valid macro, so make sure we don't
688: * clobber it by seeing the `\' as the end of token.
689: */
690:
1.106 kristaps 691: mac = buf + *pos;
1.144 kristaps 692: maclen = strcspn(mac + 1, " \\\t\0") + 1;
1.67 kristaps 693:
1.106 kristaps 694: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155 kristaps 695: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67 kristaps 696:
1.127 kristaps 697: *pos += (int)maclen;
1.130 kristaps 698:
1.67 kristaps 699: while (buf[*pos] && ' ' == buf[*pos])
700: (*pos)++;
701:
702: return(t);
703: }
704:
705: /* ARGSUSED */
706: static enum rofferr
1.76 kristaps 707: roff_cblock(ROFF_ARGS)
1.67 kristaps 708: {
709:
1.79 kristaps 710: /*
711: * A block-close `..' should only be invoked as a child of an
712: * ignore macro, otherwise raise a warning and just ignore it.
713: */
714:
1.76 kristaps 715: if (NULL == r->last) {
1.128 kristaps 716: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76 kristaps 717: return(ROFF_IGN);
718: }
1.67 kristaps 719:
1.81 kristaps 720: switch (r->last->tok) {
721: case (ROFF_am):
722: /* FALLTHROUGH */
723: case (ROFF_ami):
724: /* FALLTHROUGH */
725: case (ROFF_am1):
726: /* FALLTHROUGH */
727: case (ROFF_de):
1.108 schwarze 728: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81 kristaps 729: /* FALLTHROUGH */
730: case (ROFF_dei):
731: /* FALLTHROUGH */
732: case (ROFF_ig):
733: break;
734: default:
1.128 kristaps 735: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.67 kristaps 736: return(ROFF_IGN);
1.76 kristaps 737: }
1.67 kristaps 738:
1.76 kristaps 739: if ((*bufp)[pos])
1.128 kristaps 740: mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.71 kristaps 741:
742: roffnode_pop(r);
1.76 kristaps 743: roffnode_cleanscope(r);
744: return(ROFF_IGN);
1.71 kristaps 745:
1.67 kristaps 746: }
747:
748:
1.76 kristaps 749: static void
750: roffnode_cleanscope(struct roff *r)
1.67 kristaps 751: {
752:
1.76 kristaps 753: while (r->last) {
754: if (--r->last->endspan < 0)
755: break;
756: roffnode_pop(r);
757: }
1.67 kristaps 758: }
759:
760:
1.75 kristaps 761: /* ARGSUSED */
1.74 kristaps 762: static enum rofferr
1.75 kristaps 763: roff_ccond(ROFF_ARGS)
1.74 kristaps 764: {
765:
1.76 kristaps 766: if (NULL == r->last) {
1.128 kristaps 767: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76 kristaps 768: return(ROFF_IGN);
769: }
770:
1.82 kristaps 771: switch (r->last->tok) {
772: case (ROFF_el):
773: /* FALLTHROUGH */
774: case (ROFF_ie):
775: /* FALLTHROUGH */
776: case (ROFF_if):
777: break;
778: default:
1.128 kristaps 779: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.75 kristaps 780: return(ROFF_IGN);
781: }
782:
1.76 kristaps 783: if (r->last->endspan > -1) {
1.128 kristaps 784: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76 kristaps 785: return(ROFF_IGN);
786: }
787:
788: if ((*bufp)[pos])
1.128 kristaps 789: mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.76 kristaps 790:
1.75 kristaps 791: roffnode_pop(r);
1.76 kristaps 792: roffnode_cleanscope(r);
793: return(ROFF_IGN);
794: }
795:
1.75 kristaps 796:
1.76 kristaps 797: /* ARGSUSED */
798: static enum rofferr
1.80 kristaps 799: roff_block(ROFF_ARGS)
1.76 kristaps 800: {
1.78 kristaps 801: int sv;
802: size_t sz;
1.106 kristaps 803: char *name;
804:
805: name = NULL;
1.76 kristaps 806:
1.106 kristaps 807: if (ROFF_ig != tok) {
808: if ('\0' == (*bufp)[pos]) {
1.128 kristaps 809: mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.106 kristaps 810: return(ROFF_IGN);
811: }
1.107 kristaps 812:
813: /*
814: * Re-write `de1', since we don't really care about
815: * groff's strange compatibility mode, into `de'.
816: */
817:
1.106 kristaps 818: if (ROFF_de1 == tok)
819: tok = ROFF_de;
820: if (ROFF_de == tok)
821: name = *bufp + pos;
822: else
1.128 kristaps 823: mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
1.106 kristaps 824: roffs[tok].name);
1.107 kristaps 825:
1.131 schwarze 826: while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.80 kristaps 827: pos++;
1.107 kristaps 828:
1.131 schwarze 829: while (isspace((unsigned char)(*bufp)[pos]))
1.106 kristaps 830: (*bufp)[pos++] = '\0';
1.80 kristaps 831: }
832:
1.106 kristaps 833: roffnode_push(r, tok, name, ln, ppos);
834:
835: /*
836: * At the beginning of a `de' macro, clear the existing string
837: * with the same name, if there is one. New content will be
838: * added from roff_block_text() in multiline mode.
839: */
1.107 kristaps 840:
1.106 kristaps 841: if (ROFF_de == tok)
1.108 schwarze 842: roff_setstr(r, name, "", 0);
1.76 kristaps 843:
1.79 kristaps 844: if ('\0' == (*bufp)[pos])
1.78 kristaps 845: return(ROFF_IGN);
846:
1.107 kristaps 847: /* If present, process the custom end-of-line marker. */
848:
1.78 kristaps 849: sv = pos;
1.131 schwarze 850: while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.78 kristaps 851: pos++;
852:
853: /*
854: * Note: groff does NOT like escape characters in the input.
855: * Instead of detecting this, we're just going to let it fly and
856: * to hell with it.
857: */
858:
859: assert(pos > sv);
860: sz = (size_t)(pos - sv);
861:
1.79 kristaps 862: if (1 == sz && '.' == (*bufp)[sv])
863: return(ROFF_IGN);
864:
1.98 schwarze 865: r->last->end = mandoc_malloc(sz + 1);
1.78 kristaps 866:
867: memcpy(r->last->end, *bufp + sv, sz);
868: r->last->end[(int)sz] = '\0';
869:
1.77 kristaps 870: if ((*bufp)[pos])
1.128 kristaps 871: mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.74 kristaps 872:
1.78 kristaps 873: return(ROFF_IGN);
874: }
875:
876:
877: /* ARGSUSED */
878: static enum rofferr
1.80 kristaps 879: roff_block_sub(ROFF_ARGS)
1.79 kristaps 880: {
881: enum rofft t;
882: int i, j;
883:
884: /*
885: * First check whether a custom macro exists at this level. If
886: * it does, then check against it. This is some of groff's
887: * stranger behaviours. If we encountered a custom end-scope
888: * tag and that tag also happens to be a "real" macro, then we
889: * need to try interpreting it again as a real macro. If it's
890: * not, then return ignore. Else continue.
891: */
892:
893: if (r->last->end) {
1.130 kristaps 894: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.79 kristaps 895: if ((*bufp)[i] != r->last->end[j])
896: break;
897:
898: if ('\0' == r->last->end[j] &&
899: ('\0' == (*bufp)[i] ||
900: ' ' == (*bufp)[i] ||
901: '\t' == (*bufp)[i])) {
902: roffnode_pop(r);
903: roffnode_cleanscope(r);
904:
1.130 kristaps 905: while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
906: i++;
907:
908: pos = i;
1.106 kristaps 909: if (ROFF_MAX != roff_parse(r, *bufp, &pos))
1.79 kristaps 910: return(ROFF_RERUN);
911: return(ROFF_IGN);
912: }
913: }
914:
915: /*
916: * If we have no custom end-query or lookup failed, then try
917: * pulling it out of the hashtable.
918: */
919:
1.137 schwarze 920: t = roff_parse(r, *bufp, &pos);
1.79 kristaps 921:
1.106 kristaps 922: /*
923: * Macros other than block-end are only significant
924: * in `de' blocks; elsewhere, simply throw them away.
925: */
926: if (ROFF_cblock != t) {
927: if (ROFF_de == tok)
928: roff_setstr(r, r->last->name, *bufp + ppos, 1);
1.79 kristaps 929: return(ROFF_IGN);
1.106 kristaps 930: }
1.79 kristaps 931:
932: assert(roffs[t].proc);
1.90 kristaps 933: return((*roffs[t].proc)(r, t, bufp, szp,
934: ln, ppos, pos, offs));
1.79 kristaps 935: }
936:
937:
938: /* ARGSUSED */
939: static enum rofferr
1.80 kristaps 940: roff_block_text(ROFF_ARGS)
1.78 kristaps 941: {
942:
1.106 kristaps 943: if (ROFF_de == tok)
944: roff_setstr(r, r->last->name, *bufp + pos, 1);
945:
1.78 kristaps 946: return(ROFF_IGN);
947: }
948:
949:
950: /* ARGSUSED */
951: static enum rofferr
1.82 kristaps 952: roff_cond_sub(ROFF_ARGS)
953: {
954: enum rofft t;
955: enum roffrule rr;
1.139 kristaps 956: char *ep;
1.82 kristaps 957:
958: rr = r->last->rule;
1.139 kristaps 959: roffnode_cleanscope(r);
1.82 kristaps 960:
1.139 kristaps 961: /*
962: * If the macro is unknown, first check if it contains a closing
963: * delimiter `\}'. If it does, close out our scope and return
964: * the currently-scoped rule (ignore or continue). Else, drop
965: * into the currently-scoped rule.
1.87 kristaps 966: */
967:
1.106 kristaps 968: if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
1.139 kristaps 969: ep = &(*bufp)[pos];
970: for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
971: ep++;
972: if ('}' != *ep)
973: continue;
1.144 kristaps 974:
975: /*
976: * Make the \} go away.
977: * This is a little haphazard, as it's not quite
978: * clear how nroff does this.
979: * If we're at the end of line, then just chop
980: * off the \} and resize the buffer.
981: * If we aren't, then conver it to spaces.
982: */
983:
984: if ('\0' == *(ep + 1)) {
985: *--ep = '\0';
986: *szp -= 2;
987: } else
988: *(ep - 1) = *ep = ' ';
989:
1.139 kristaps 990: roff_ccond(r, ROFF_ccond, bufp, szp,
991: ln, pos, pos + 2, offs);
992: break;
993: }
1.82 kristaps 994: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.100 kristaps 995: }
1.82 kristaps 996:
997: /*
998: * A denied conditional must evaluate its children if and only
999: * if they're either structurally required (such as loops and
1000: * conditionals) or a closing macro.
1001: */
1.139 kristaps 1002:
1.82 kristaps 1003: if (ROFFRULE_DENY == rr)
1004: if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
1005: if (ROFF_ccond != t)
1006: return(ROFF_IGN);
1007:
1008: assert(roffs[t].proc);
1.90 kristaps 1009: return((*roffs[t].proc)(r, t, bufp, szp,
1010: ln, ppos, pos, offs));
1.82 kristaps 1011: }
1012:
1013: /* ARGSUSED */
1014: static enum rofferr
1015: roff_cond_text(ROFF_ARGS)
1.78 kristaps 1016: {
1.140 kristaps 1017: char *ep;
1.82 kristaps 1018: enum roffrule rr;
1019:
1020: rr = r->last->rule;
1.140 kristaps 1021: roffnode_cleanscope(r);
1.82 kristaps 1022:
1.140 kristaps 1023: ep = &(*bufp)[pos];
1024: for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
1025: ep++;
1026: if ('}' != *ep)
1027: continue;
1028: *ep = '&';
1029: roff_ccond(r, ROFF_ccond, bufp, szp,
1030: ln, pos, pos + 2, offs);
1.78 kristaps 1031: }
1.82 kristaps 1032: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74 kristaps 1033: }
1034:
1.88 kristaps 1035: static enum roffrule
1036: roff_evalcond(const char *v, int *pos)
1037: {
1038:
1039: switch (v[*pos]) {
1040: case ('n'):
1041: (*pos)++;
1042: return(ROFFRULE_ALLOW);
1043: case ('e'):
1044: /* FALLTHROUGH */
1045: case ('o'):
1046: /* FALLTHROUGH */
1047: case ('t'):
1048: (*pos)++;
1049: return(ROFFRULE_DENY);
1050: default:
1051: break;
1052: }
1053:
1054: while (v[*pos] && ' ' != v[*pos])
1055: (*pos)++;
1056: return(ROFFRULE_DENY);
1057: }
1058:
1.75 kristaps 1059: /* ARGSUSED */
1.74 kristaps 1060: static enum rofferr
1.103 kristaps 1061: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 1062: {
1.123 schwarze 1063:
1064: if (ROFF_it == tok)
1.128 kristaps 1065: mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
1.89 kristaps 1066:
1067: return(ROFF_IGN);
1068: }
1069:
1.104 kristaps 1070: /* ARGSUSED */
1071: static enum rofferr
1.82 kristaps 1072: roff_cond(ROFF_ARGS)
1.74 kristaps 1073: {
1.77 kristaps 1074: int sv;
1.88 kristaps 1075: enum roffrule rule;
1.74 kristaps 1076:
1.134 kristaps 1077: /*
1078: * An `.el' has no conditional body: it will consume the value
1079: * of the current rstack entry set in prior `ie' calls or
1080: * defaults to DENY.
1081: *
1082: * If we're not an `el', however, then evaluate the conditional.
1083: */
1.133 kristaps 1084:
1.134 kristaps 1085: rule = ROFF_el == tok ?
1086: (r->rstackpos < 0 ?
1087: ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
1088: roff_evalcond(*bufp, &pos);
1.77 kristaps 1089:
1090: sv = pos;
1.75 kristaps 1091: while (' ' == (*bufp)[pos])
1092: pos++;
1.74 kristaps 1093:
1.77 kristaps 1094: /*
1095: * Roff is weird. If we have just white-space after the
1096: * conditional, it's considered the BODY and we exit without
1097: * really doing anything. Warn about this. It's probably
1098: * wrong.
1099: */
1.88 kristaps 1100:
1.77 kristaps 1101: if ('\0' == (*bufp)[pos] && sv != pos) {
1.128 kristaps 1102: mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.107 kristaps 1103: return(ROFF_IGN);
1.77 kristaps 1104: }
1105:
1.106 kristaps 1106: roffnode_push(r, tok, NULL, ln, ppos);
1.77 kristaps 1107:
1.88 kristaps 1108: r->last->rule = rule;
1109:
1.134 kristaps 1110: /*
1111: * An if-else will put the NEGATION of the current evaluated
1112: * conditional into the stack of rules.
1113: */
1114:
1.84 schwarze 1115: if (ROFF_ie == tok) {
1.134 kristaps 1116: if (r->rstackpos == RSTACK_MAX - 1) {
1117: mandoc_msg(MANDOCERR_MEM,
1118: r->parse, ln, ppos, NULL);
1119: return(ROFF_ERR);
1120: }
1121: r->rstack[++r->rstackpos] =
1122: ROFFRULE_DENY == r->last->rule ?
1123: ROFFRULE_ALLOW : ROFFRULE_DENY;
1.82 kristaps 1124: }
1.88 kristaps 1125:
1126: /* If the parent has false as its rule, then so do we. */
1127:
1.109 kristaps 1128: if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1.84 schwarze 1129: r->last->rule = ROFFRULE_DENY;
1.88 kristaps 1130:
1131: /*
1132: * Determine scope. If we're invoked with "\{" trailing the
1133: * conditional, then we're in a multiline scope. Else our scope
1134: * expires on the next line.
1135: */
1.74 kristaps 1136:
1.75 kristaps 1137: r->last->endspan = 1;
1138:
1139: if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1140: r->last->endspan = -1;
1141: pos += 2;
1.109 kristaps 1142: }
1.74 kristaps 1143:
1.77 kristaps 1144: /*
1145: * If there are no arguments on the line, the next-line scope is
1146: * assumed.
1147: */
1148:
1.75 kristaps 1149: if ('\0' == (*bufp)[pos])
1150: return(ROFF_IGN);
1.77 kristaps 1151:
1152: /* Otherwise re-run the roff parser after recalculating. */
1.74 kristaps 1153:
1.75 kristaps 1154: *offs = pos;
1155: return(ROFF_RERUN);
1.83 schwarze 1156: }
1157:
1158:
1159: /* ARGSUSED */
1160: static enum rofferr
1.92 schwarze 1161: roff_ds(ROFF_ARGS)
1162: {
1.96 kristaps 1163: char *name, *string;
1164:
1165: /*
1166: * A symbol is named by the first word following the macro
1167: * invocation up to a space. Its value is anything after the
1168: * name's trailing whitespace and optional double-quote. Thus,
1169: *
1170: * [.ds foo "bar " ]
1171: *
1172: * will have `bar " ' as its value.
1173: */
1.92 schwarze 1174:
1.121 schwarze 1175: string = *bufp + pos;
1176: name = roff_getname(r, &string, ln, pos);
1.92 schwarze 1177: if ('\0' == *name)
1178: return(ROFF_IGN);
1179:
1.121 schwarze 1180: /* Read past initial double-quote. */
1181: if ('"' == *string)
1.92 schwarze 1182: string++;
1183:
1.96 kristaps 1184: /* The rest is the value. */
1.106 kristaps 1185: roff_setstr(r, name, string, 0);
1.92 schwarze 1186: return(ROFF_IGN);
1187: }
1188:
1.147 kristaps 1189: int
1190: roff_regisset(const struct roff *r, enum regs reg)
1191: {
1192:
1193: return(r->regs[(int)reg].set);
1194: }
1195:
1196: unsigned int
1197: roff_regget(const struct roff *r, enum regs reg)
1198: {
1199:
1200: return(r->regs[(int)reg].u);
1201: }
1202:
1203: void
1204: roff_regunset(struct roff *r, enum regs reg)
1205: {
1206:
1207: r->regs[(int)reg].set = 0;
1208: }
1.92 schwarze 1209:
1210: /* ARGSUSED */
1211: static enum rofferr
1.89 kristaps 1212: roff_nr(ROFF_ARGS)
1.83 schwarze 1213: {
1.121 schwarze 1214: const char *key;
1215: char *val;
1.138 kristaps 1216: int iv;
1.89 kristaps 1217:
1.121 schwarze 1218: val = *bufp + pos;
1219: key = roff_getname(r, &val, ln, pos);
1.89 kristaps 1220:
1221: if (0 == strcmp(key, "nS")) {
1.147 kristaps 1222: r->regs[(int)REG_nS].set = 1;
1.149 kristaps 1223: if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
1.147 kristaps 1224: r->regs[(int)REG_nS].u = (unsigned)iv;
1.138 kristaps 1225: else
1.147 kristaps 1226: r->regs[(int)REG_nS].u = 0u;
1.109 kristaps 1227: }
1228:
1.122 schwarze 1229: return(ROFF_IGN);
1230: }
1231:
1232: /* ARGSUSED */
1233: static enum rofferr
1234: roff_rm(ROFF_ARGS)
1235: {
1236: const char *name;
1237: char *cp;
1238:
1239: cp = *bufp + pos;
1240: while ('\0' != *cp) {
1.127 kristaps 1241: name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1.122 schwarze 1242: if ('\0' != *name)
1243: roff_setstr(r, name, NULL, 0);
1244: }
1.109 kristaps 1245: return(ROFF_IGN);
1246: }
1247:
1248: /* ARGSUSED */
1249: static enum rofferr
1250: roff_TE(ROFF_ARGS)
1251: {
1252:
1253: if (NULL == r->tbl)
1.128 kristaps 1254: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.115 kristaps 1255: else
1.151 kristaps 1256: tbl_end(&r->tbl);
1.109 kristaps 1257:
1.112 kristaps 1258: return(ROFF_IGN);
1259: }
1260:
1261: /* ARGSUSED */
1262: static enum rofferr
1263: roff_T_(ROFF_ARGS)
1264: {
1265:
1266: if (NULL == r->tbl)
1.128 kristaps 1267: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.112 kristaps 1268: else
1.116 kristaps 1269: tbl_restart(ppos, ln, r->tbl);
1.112 kristaps 1270:
1.109 kristaps 1271: return(ROFF_IGN);
1272: }
1273:
1.156 kristaps 1274: #if 0
1275: static int
1.151 kristaps 1276: roff_closeeqn(struct roff *r)
1277: {
1278:
1279: return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1280: }
1.156 kristaps 1281: #endif
1.151 kristaps 1282:
1.156 kristaps 1283: static void
1.151 kristaps 1284: roff_openeqn(struct roff *r, const char *name, int line,
1285: int offs, const char *buf)
1.125 kristaps 1286: {
1.151 kristaps 1287: struct eqn_node *e;
1288: int poff;
1.125 kristaps 1289:
1290: assert(NULL == r->eqn);
1.151 kristaps 1291: e = eqn_alloc(name, offs, line, r->parse);
1.125 kristaps 1292:
1293: if (r->last_eqn)
1294: r->last_eqn->next = e;
1295: else
1296: r->first_eqn = r->last_eqn = e;
1297:
1298: r->eqn = r->last_eqn = e;
1.151 kristaps 1299:
1300: if (buf) {
1301: poff = 0;
1302: eqn_read(&r->eqn, line, buf, offs, &poff);
1303: }
1304: }
1305:
1306: /* ARGSUSED */
1307: static enum rofferr
1308: roff_EQ(ROFF_ARGS)
1309: {
1310:
1311: roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.125 kristaps 1312: return(ROFF_IGN);
1313: }
1314:
1315: /* ARGSUSED */
1316: static enum rofferr
1317: roff_EN(ROFF_ARGS)
1318: {
1319:
1.128 kristaps 1320: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.125 kristaps 1321: return(ROFF_IGN);
1322: }
1323:
1324: /* ARGSUSED */
1325: static enum rofferr
1.109 kristaps 1326: roff_TS(ROFF_ARGS)
1327: {
1.118 kristaps 1328: struct tbl_node *t;
1.89 kristaps 1329:
1.115 kristaps 1330: if (r->tbl) {
1.128 kristaps 1331: mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.151 kristaps 1332: tbl_end(&r->tbl);
1.115 kristaps 1333: }
1.83 schwarze 1334:
1.128 kristaps 1335: t = tbl_alloc(ppos, ln, r->parse);
1.113 kristaps 1336:
1337: if (r->last_tbl)
1338: r->last_tbl->next = t;
1339: else
1340: r->first_tbl = r->last_tbl = t;
1341:
1342: r->tbl = r->last_tbl = t;
1.83 schwarze 1343: return(ROFF_IGN);
1.92 schwarze 1344: }
1345:
1.105 kristaps 1346: /* ARGSUSED */
1347: static enum rofferr
1.164 kristaps 1348: roff_tr(ROFF_ARGS)
1349: {
1350: const char *p, *first, *second;
1351: size_t fsz, ssz;
1352: enum mandoc_esc esc;
1353:
1354: p = *bufp + pos;
1355:
1356: if ('\0' == *p) {
1357: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1358: return(ROFF_IGN);
1359: }
1360:
1361: while ('\0' != *p) {
1362: fsz = ssz = 1;
1363:
1364: first = p++;
1365: if ('\\' == *first) {
1366: esc = mandoc_escape(&p, NULL, NULL);
1367: if (ESCAPE_ERROR == esc) {
1368: mandoc_msg
1369: (MANDOCERR_BADESCAPE, r->parse,
1370: ln, (int)(p - *bufp), NULL);
1371: return(ROFF_IGN);
1372: }
1373: fsz = (size_t)(p - first);
1374: }
1375:
1376: second = p++;
1377: if ('\\' == *second) {
1378: esc = mandoc_escape(&p, NULL, NULL);
1379: if (ESCAPE_ERROR == esc) {
1380: mandoc_msg
1381: (MANDOCERR_BADESCAPE, r->parse,
1382: ln, (int)(p - *bufp), NULL);
1383: return(ROFF_IGN);
1384: }
1385: ssz = (size_t)(p - second);
1.165 ! kristaps 1386: } else if ('\0' == *second) {
1.164 kristaps 1387: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1388: ln, (int)(p - *bufp), NULL);
1389: second = " ";
1.165 ! kristaps 1390: p--;
1.164 kristaps 1391: }
1392:
1393: roff_setstrn(&r->chrtab, first, fsz, second, ssz, 0);
1394: }
1395:
1396: return(ROFF_IGN);
1397: }
1398:
1399: /* ARGSUSED */
1400: static enum rofferr
1.105 kristaps 1401: roff_so(ROFF_ARGS)
1402: {
1403: char *name;
1404:
1.128 kristaps 1405: mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1.105 kristaps 1406:
1407: /*
1408: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1409: * opening anything that's not in our cwd or anything beneath
1410: * it. Thus, explicitly disallow traversing up the file-system
1411: * or using absolute paths.
1412: */
1413:
1414: name = *bufp + pos;
1415: if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.128 kristaps 1416: mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1.105 kristaps 1417: return(ROFF_ERR);
1418: }
1419:
1420: *offs = pos;
1421: return(ROFF_SO);
1422: }
1.92 schwarze 1423:
1.106 kristaps 1424: /* ARGSUSED */
1425: static enum rofferr
1426: roff_userdef(ROFF_ARGS)
1.99 kristaps 1427: {
1.106 kristaps 1428: const char *arg[9];
1429: char *cp, *n1, *n2;
1.119 schwarze 1430: int i;
1.106 kristaps 1431:
1432: /*
1433: * Collect pointers to macro argument strings
1434: * and null-terminate them.
1435: */
1436: cp = *bufp + pos;
1.119 schwarze 1437: for (i = 0; i < 9; i++)
1.120 schwarze 1438: arg[i] = '\0' == *cp ? "" :
1.136 kristaps 1439: mandoc_getarg(r->parse, &cp, ln, &pos);
1.99 kristaps 1440:
1.106 kristaps 1441: /*
1442: * Expand macro arguments.
1.99 kristaps 1443: */
1.106 kristaps 1444: *szp = 0;
1445: n1 = cp = mandoc_strdup(r->current_string);
1446: while (NULL != (cp = strstr(cp, "\\$"))) {
1447: i = cp[2] - '1';
1448: if (0 > i || 8 < i) {
1449: /* Not an argument invocation. */
1450: cp += 2;
1451: continue;
1452: }
1453:
1454: *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1455: n2 = mandoc_malloc(*szp);
1456:
1457: strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1458: strlcat(n2, arg[i], *szp);
1459: strlcat(n2, cp + 3, *szp);
1460:
1461: cp = n2 + (cp - n1);
1462: free(n1);
1463: n1 = n2;
1.99 kristaps 1464: }
1465:
1.106 kristaps 1466: /*
1467: * Replace the macro invocation
1468: * by the expanded macro.
1469: */
1470: free(*bufp);
1471: *bufp = n1;
1472: if (0 == *szp)
1473: *szp = strlen(*bufp) + 1;
1474:
1475: return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1476: ROFF_REPARSE : ROFF_APPEND);
1.99 kristaps 1477: }
1.121 schwarze 1478:
1479: static char *
1480: roff_getname(struct roff *r, char **cpp, int ln, int pos)
1481: {
1482: char *name, *cp;
1483:
1484: name = *cpp;
1485: if ('\0' == *name)
1486: return(name);
1487:
1488: /* Read until end of name. */
1489: for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
1490: if ('\\' != *cp)
1491: continue;
1492: cp++;
1493: if ('\\' == *cp)
1494: continue;
1.128 kristaps 1495: mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1.121 schwarze 1496: *cp = '\0';
1497: name = cp;
1498: }
1499:
1500: /* Nil-terminate name. */
1501: if ('\0' != *cp)
1502: *(cp++) = '\0';
1503:
1504: /* Read past spaces. */
1505: while (' ' == *cp)
1506: cp++;
1507:
1508: *cpp = cp;
1509: return(name);
1510: }
1511:
1.106 kristaps 1512: /*
1513: * Store *string into the user-defined string called *name.
1514: * In multiline mode, append to an existing entry and append '\n';
1515: * else replace the existing entry, if there is one.
1516: * To clear an existing entry, call with (*r, *name, NULL, 0).
1517: */
1.94 kristaps 1518: static void
1.106 kristaps 1519: roff_setstr(struct roff *r, const char *name, const char *string,
1520: int multiline)
1.92 schwarze 1521: {
1.164 kristaps 1522:
1523: roff_setstrn(&r->strtab, name, strlen(name), string,
1524: string ? strlen(string) : 0, multiline);
1525: }
1526:
1527: static void
1528: roff_setstrn(struct roffstr **r, const char *name, size_t namesz,
1529: const char *string, size_t stringsz, int multiline)
1530: {
1531: struct roffstr *n;
1532: char *c;
1533: int i;
1534: size_t oldch, newch;
1.92 schwarze 1535:
1.106 kristaps 1536: /* Search for an existing string with the same name. */
1.164 kristaps 1537: n = *r;
1538:
1.160 kristaps 1539: while (n && strcmp(name, n->key))
1.92 schwarze 1540: n = n->next;
1.94 kristaps 1541:
1542: if (NULL == n) {
1.106 kristaps 1543: /* Create a new string table entry. */
1.94 kristaps 1544: n = mandoc_malloc(sizeof(struct roffstr));
1.164 kristaps 1545: n->key = mandoc_strndup(name, namesz);
1546: n->keysz = namesz;
1.160 kristaps 1547: n->val = NULL;
1.162 kristaps 1548: n->valsz = 0;
1.164 kristaps 1549: n->next = *r;
1550: *r = n;
1.106 kristaps 1551: } else if (0 == multiline) {
1552: /* In multiline mode, append; else replace. */
1.160 kristaps 1553: free(n->val);
1554: n->val = NULL;
1.162 kristaps 1555: n->valsz = 0;
1.106 kristaps 1556: }
1557:
1558: if (NULL == string)
1559: return;
1560:
1561: /*
1562: * One additional byte for the '\n' in multiline mode,
1563: * and one for the terminating '\0'.
1564: */
1.164 kristaps 1565: newch = stringsz + (multiline ? 2u : 1u);
1566:
1.160 kristaps 1567: if (NULL == n->val) {
1568: n->val = mandoc_malloc(newch);
1569: *n->val = '\0';
1.106 kristaps 1570: oldch = 0;
1571: } else {
1.162 kristaps 1572: oldch = n->valsz;
1.163 kristaps 1573: n->val = mandoc_realloc(n->val, oldch + newch);
1.106 kristaps 1574: }
1575:
1576: /* Skip existing content in the destination buffer. */
1.160 kristaps 1577: c = n->val + (int)oldch;
1.106 kristaps 1578:
1579: /* Append new content to the destination buffer. */
1.164 kristaps 1580: i = 0;
1581: while (i < (int)stringsz) {
1.106 kristaps 1582: /*
1583: * Rudimentary roff copy mode:
1584: * Handle escaped backslashes.
1585: */
1.164 kristaps 1586: if ('\\' == string[i] && '\\' == string[i + 1])
1587: i++;
1588: *c++ = string[i++];
1.106 kristaps 1589: }
1.94 kristaps 1590:
1.106 kristaps 1591: /* Append terminating bytes. */
1592: if (multiline)
1593: *c++ = '\n';
1.163 kristaps 1594:
1.106 kristaps 1595: *c = '\0';
1.163 kristaps 1596: n->valsz = (int)(c - n->val);
1.92 schwarze 1597: }
1598:
1.94 kristaps 1599: static const char *
1600: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 1601: {
1.94 kristaps 1602: const struct roffstr *n;
1.92 schwarze 1603:
1.164 kristaps 1604: for (n = r->strtab; n; n = n->next)
1.160 kristaps 1605: if (0 == strncmp(name, n->key, len) &&
1606: '\0' == n->key[(int)len])
1607: return(n->val);
1.94 kristaps 1608:
1.157 kristaps 1609: return(NULL);
1.92 schwarze 1610: }
1611:
1.94 kristaps 1612: static void
1.164 kristaps 1613: roff_freestr(struct roffstr **r)
1.92 schwarze 1614: {
1615: struct roffstr *n, *nn;
1616:
1.164 kristaps 1617: for (n = *r; n; n = nn) {
1.160 kristaps 1618: free(n->key);
1619: free(n->val);
1.92 schwarze 1620: nn = n->next;
1621: free(n);
1622: }
1.94 kristaps 1623:
1.164 kristaps 1624: *r = NULL;
1.114 kristaps 1625: }
1626:
1627: const struct tbl_span *
1628: roff_span(const struct roff *r)
1629: {
1630:
1631: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125 kristaps 1632: }
1633:
1634: const struct eqn *
1635: roff_eqn(const struct roff *r)
1636: {
1637:
1638: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.151 kristaps 1639: }
1640:
1641: char
1642: roff_eqndelim(const struct roff *r)
1643: {
1644:
1645: return('\0');
1.164 kristaps 1646: }
1647:
1648: /*
1649: * Duplicate an input string, making the appropriate character
1650: * conversations (as stipulated by `tr') along the way.
1651: * Returns a heap-allocated string with all the replacements made.
1652: */
1653: char *
1654: roff_strdup(const struct roff *r, const char *p)
1655: {
1656: const struct roffstr *cp;
1657: char *res;
1658: const char *pp;
1659: size_t ssz, sz;
1660: enum mandoc_esc esc;
1661:
1662: if (NULL == r->chrtab)
1663: return(mandoc_strdup(p));
1664: else if ('\0' == *p)
1665: return(mandoc_strdup(""));
1666:
1667: /*
1668: * Step through each character looking for term matches
1669: * (remember that a `tr' can be invoked with an escape, which is
1670: * a glyph but the escape is multi-character).
1671: * We only do this if the character hash has been initialised
1672: * and the string is >0 length.
1673: */
1674:
1675: res = NULL;
1676: ssz = 0;
1677:
1678: while ('\0' != *p) {
1679: /* Search for term matches. */
1680: for (cp = r->chrtab; cp; cp = cp->next)
1681: if (0 == strncmp(p, cp->key, cp->keysz))
1682: break;
1683:
1684: if (NULL != cp) {
1685: /*
1686: * A match has been found.
1687: * Append the match to the array and move
1688: * forward by its keysize.
1689: */
1690: res = mandoc_realloc(res, ssz + cp->valsz + 1);
1691: memcpy(res + ssz, cp->val, cp->valsz);
1692: ssz += cp->valsz;
1693: p += (int)cp->keysz;
1694: continue;
1695: }
1696:
1697: if ('\\' == *p) {
1698: /*
1699: * Handle escapes carefully: we need to copy
1700: * over just the escape itself, or else we might
1701: * do replacements within the escape itself.
1702: * Make sure to pass along the bogus string.
1703: */
1704: pp = p++;
1705: esc = mandoc_escape(&p, NULL, NULL);
1706: if (ESCAPE_ERROR == esc) {
1707: sz = strlen(pp);
1708: res = mandoc_realloc(res, ssz + sz + 1);
1709: memcpy(res + ssz, pp, sz);
1710: break;
1711: }
1712: /*
1713: * We bail out on bad escapes.
1714: * No need to warn: we already did so when
1715: * roff_res() was called.
1716: */
1717: sz = (int)(p - pp);
1718: res = mandoc_realloc(res, ssz + sz + 1);
1719: memcpy(res + ssz, pp, sz);
1720: ssz += sz;
1721: continue;
1722: }
1723:
1724: /* Just append the charater. */
1725: res = mandoc_realloc(res, ssz + 2);
1726: res[ssz++] = *p++;
1727: }
1728:
1729: res[(int)ssz] = '\0';
1730: return(res);
1.74 kristaps 1731: }
CVSweb