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