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