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