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