Annotation of mandoc/roff.c, Revision 1.104
1.104 ! kristaps 1: /* $Id: roff.c,v 1.103 2010/12/01 10:21:25 kristaps Exp $ */
1.1 kristaps 2: /*
1.67 kristaps 3: * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.93 schwarze 4: * Copyright (c) 2010 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.66 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.89 kristaps 23: #include <errno.h>
1.85 kristaps 24: #include <ctype.h>
1.89 kristaps 25: #include <limits.h>
1.1 kristaps 26: #include <stdlib.h>
1.67 kristaps 27: #include <string.h>
1.75 kristaps 28: #include <stdio.h>
1.1 kristaps 29:
1.67 kristaps 30: #include "mandoc.h"
1.43 kristaps 31: #include "roff.h"
1.94 kristaps 32: #include "libmandoc.h"
1.33 kristaps 33:
1.82 kristaps 34: #define RSTACK_MAX 128
35:
1.75 kristaps 36: #define ROFF_CTL(c) \
37: ('.' == (c) || '\'' == (c))
38:
1.88 kristaps 39: #if 1
40: #define ROFF_DEBUG(fmt, args...) \
41: do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
42: #else
43: #define ROFF_DEBUG(fmt, args...) \
44: do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
45: #endif
46:
1.67 kristaps 47: enum rofft {
1.103 kristaps 48: ROFF_ad,
1.80 kristaps 49: ROFF_am,
50: ROFF_ami,
51: ROFF_am1,
52: ROFF_de,
53: ROFF_dei,
54: ROFF_de1,
1.83 schwarze 55: ROFF_ds,
1.82 kristaps 56: ROFF_el,
1.103 kristaps 57: ROFF_hy,
1.82 kristaps 58: ROFF_ie,
1.75 kristaps 59: ROFF_if,
1.76 kristaps 60: ROFF_ig,
1.103 kristaps 61: ROFF_ne,
62: ROFF_nh,
1.104 ! kristaps 63: ROFF_nr,
1.83 schwarze 64: ROFF_rm,
65: ROFF_tr,
1.76 kristaps 66: ROFF_cblock,
1.100 kristaps 67: ROFF_ccond, /* FIXME: remove this. */
1.67 kristaps 68: ROFF_MAX
69: };
70:
1.82 kristaps 71: enum roffrule {
72: ROFFRULE_ALLOW,
73: ROFFRULE_DENY
74: };
75:
1.94 kristaps 76:
77: struct roffstr {
78: char *name; /* key of symbol */
79: char *string; /* current value */
80: struct roffstr *next; /* next in list */
81: };
82:
1.67 kristaps 83: struct roff {
84: struct roffnode *last; /* leaf of stack */
85: mandocmsg msg; /* err/warn/fatal messages */
86: void *data; /* privdata for messages */
1.82 kristaps 87: enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
88: int rstackpos; /* position in rstack */
1.90 kristaps 89: struct regset *regs; /* read/writable registers */
1.94 kristaps 90: struct roffstr *first_string;
1.79 kristaps 91: };
92:
1.67 kristaps 93: struct roffnode {
94: enum rofft tok; /* type of node */
95: struct roffnode *parent; /* up one in stack */
96: int line; /* parse line */
97: int col; /* parse col */
1.79 kristaps 98: char *end; /* end-rules: custom token */
99: int endspan; /* end-rules: next-line or infty */
1.82 kristaps 100: enum roffrule rule; /* current evaluation rule */
1.67 kristaps 101: };
102:
103: #define ROFF_ARGS struct roff *r, /* parse ctx */ \
1.72 kristaps 104: enum rofft tok, /* tok of macro */ \
1.67 kristaps 105: char **bufp, /* input buffer */ \
106: size_t *szp, /* size of input buffer */ \
107: int ln, /* parse line */ \
1.75 kristaps 108: int ppos, /* original pos in buffer */ \
109: int pos, /* current pos in buffer */ \
1.74 kristaps 110: int *offs /* reset offset of buffer data */
1.67 kristaps 111:
112: typedef enum rofferr (*roffproc)(ROFF_ARGS);
113:
114: struct roffmac {
115: const char *name; /* macro name */
1.79 kristaps 116: roffproc proc; /* process new macro */
117: roffproc text; /* process as child text of macro */
118: roffproc sub; /* process as child of macro */
119: int flags;
120: #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
1.85 kristaps 121: struct roffmac *next;
1.67 kristaps 122: };
123:
1.80 kristaps 124: static enum rofferr roff_block(ROFF_ARGS);
125: static enum rofferr roff_block_text(ROFF_ARGS);
126: static enum rofferr roff_block_sub(ROFF_ARGS);
127: static enum rofferr roff_cblock(ROFF_ARGS);
128: static enum rofferr roff_ccond(ROFF_ARGS);
1.82 kristaps 129: static enum rofferr roff_cond(ROFF_ARGS);
130: static enum rofferr roff_cond_text(ROFF_ARGS);
131: static enum rofferr roff_cond_sub(ROFF_ARGS);
1.92 schwarze 132: static enum rofferr roff_ds(ROFF_ARGS);
1.94 kristaps 133: static enum roffrule roff_evalcond(const char *, int *);
134: static void roff_freestr(struct roff *);
135: static const char *roff_getstrn(const struct roff *,
136: const char *, size_t);
1.103 kristaps 137: static enum rofferr roff_line_ignore(ROFF_ARGS);
1.104 ! kristaps 138: static enum rofferr roff_line_error(ROFF_ARGS);
1.89 kristaps 139: static enum rofferr roff_nr(ROFF_ARGS);
1.95 kristaps 140: static int roff_res(struct roff *,
141: char **, size_t *, int);
1.94 kristaps 142: static void roff_setstr(struct roff *,
143: const char *, const char *);
1.99 kristaps 144: static char *roff_strdup(const char *);
1.67 kristaps 145:
1.85 kristaps 146: /* See roff_hash_find() */
147:
148: #define ASCII_HI 126
149: #define ASCII_LO 33
150: #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
151:
152: static struct roffmac *hash[HASHWIDTH];
153:
154: static struct roffmac roffs[ROFF_MAX] = {
1.103 kristaps 155: { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 156: { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
157: { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
158: { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
159: { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
160: { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
161: { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92 schwarze 162: { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85 kristaps 163: { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.103 kristaps 164: { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 165: { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
166: { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
167: { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.103 kristaps 168: { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
169: { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.104 ! kristaps 170: { "nr", roff_nr, NULL, NULL, 0, NULL },
! 171: { "rm", roff_line_error, NULL, NULL, 0, NULL },
1.103 kristaps 172: { "tr", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 173: { ".", roff_cblock, NULL, NULL, 0, NULL },
174: { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.67 kristaps 175: };
176:
177: static void roff_free1(struct roff *);
178: static enum rofft roff_hash_find(const char *);
1.85 kristaps 179: static void roff_hash_init(void);
1.76 kristaps 180: static void roffnode_cleanscope(struct roff *);
1.98 schwarze 181: static void roffnode_push(struct roff *,
1.67 kristaps 182: enum rofft, int, int);
183: static void roffnode_pop(struct roff *);
184: static enum rofft roff_parse(const char *, int *);
1.91 kristaps 185: static int roff_parse_nat(const char *, unsigned int *);
1.67 kristaps 186:
1.85 kristaps 187: /* See roff_hash_find() */
188: #define ROFF_HASH(p) (p[0] - ASCII_LO)
189:
190: static void
191: roff_hash_init(void)
192: {
193: struct roffmac *n;
194: int buc, i;
195:
196: for (i = 0; i < (int)ROFF_MAX; i++) {
197: assert(roffs[i].name[0] >= ASCII_LO);
198: assert(roffs[i].name[0] <= ASCII_HI);
199:
200: buc = ROFF_HASH(roffs[i].name);
201:
202: if (NULL != (n = hash[buc])) {
203: for ( ; n->next; n = n->next)
204: /* Do nothing. */ ;
205: n->next = &roffs[i];
206: } else
207: hash[buc] = &roffs[i];
208: }
209: }
210:
1.67 kristaps 211:
212: /*
213: * Look up a roff token by its name. Returns ROFF_MAX if no macro by
214: * the nil-terminated string name could be found.
215: */
216: static enum rofft
217: roff_hash_find(const char *p)
218: {
1.85 kristaps 219: int buc;
220: struct roffmac *n;
1.67 kristaps 221:
1.85 kristaps 222: /*
223: * libroff has an extremely simple hashtable, for the time
224: * being, which simply keys on the first character, which must
225: * be printable, then walks a chain. It works well enough until
226: * optimised.
227: */
228:
229: if (p[0] < ASCII_LO || p[0] > ASCII_HI)
230: return(ROFF_MAX);
231:
232: buc = ROFF_HASH(p);
233:
234: if (NULL == (n = hash[buc]))
235: return(ROFF_MAX);
236: for ( ; n; n = n->next)
237: if (0 == strcmp(n->name, p))
238: return((enum rofft)(n - roffs));
1.67 kristaps 239:
240: return(ROFF_MAX);
241: }
242:
243:
244: /*
245: * Pop the current node off of the stack of roff instructions currently
246: * pending.
247: */
248: static void
249: roffnode_pop(struct roff *r)
250: {
251: struct roffnode *p;
252:
1.75 kristaps 253: assert(r->last);
254: p = r->last;
1.82 kristaps 255:
256: if (ROFF_el == p->tok)
257: if (r->rstackpos > -1)
258: r->rstackpos--;
259:
1.101 kristaps 260: ROFF_DEBUG("roff: popping scope\n");
1.75 kristaps 261: r->last = r->last->parent;
1.74 kristaps 262: if (p->end)
263: free(p->end);
1.67 kristaps 264: free(p);
265: }
266:
267:
268: /*
269: * Push a roff node onto the instruction stack. This must later be
270: * removed with roffnode_pop().
271: */
1.98 schwarze 272: static void
1.67 kristaps 273: roffnode_push(struct roff *r, enum rofft tok, int line, int col)
274: {
275: struct roffnode *p;
276:
1.98 schwarze 277: p = mandoc_calloc(1, sizeof(struct roffnode));
1.67 kristaps 278: p->tok = tok;
279: p->parent = r->last;
280: p->line = line;
281: p->col = col;
1.79 kristaps 282: p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67 kristaps 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.94 kristaps 294: roff_freestr(r);
1.67 kristaps 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.98 schwarze 316: roff_alloc(struct regset *regs, void *data, const mandocmsg msg)
1.67 kristaps 317: {
318: struct roff *r;
319:
1.98 schwarze 320: r = mandoc_calloc(1, sizeof(struct roff));
1.90 kristaps 321: r->regs = regs;
1.67 kristaps 322: r->msg = msg;
323: r->data = data;
1.82 kristaps 324: r->rstackpos = -1;
1.85 kristaps 325:
326: roff_hash_init();
1.67 kristaps 327: return(r);
328: }
329:
330:
1.94 kristaps 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.95 kristaps 337: roff_res(struct roff *r, char **bufp, size_t *szp, int pos)
1.94 kristaps 338: {
339: const char *cp, *cpp, *st, *res;
340: int i, maxl;
341: size_t nsz;
342: char *n;
343:
1.95 kristaps 344: /* LINTED */
1.94 kristaps 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: ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i, st);
378:
379: nsz = *szp + strlen(res) + 1;
380: n = mandoc_malloc(nsz);
381:
382: *n = '\0';
383:
384: strlcat(n, *bufp, (size_t)(cpp - *bufp + 1));
385: strlcat(n, res, nsz);
386: strlcat(n, cp + (maxl ? 0 : 1), nsz);
387:
388: free(*bufp);
389:
390: *bufp = n;
391: *szp = nsz;
392: return(0);
393: }
394:
395: return(1);
396: }
397:
398:
1.67 kristaps 399: enum rofferr
1.90 kristaps 400: roff_parseln(struct roff *r, int ln, char **bufp,
401: size_t *szp, int pos, int *offs)
1.67 kristaps 402: {
403: enum rofft t;
1.79 kristaps 404: int ppos;
405:
406: /*
1.94 kristaps 407: * Run the reserved-word filter only if we have some reserved
408: * words to fill in.
409: */
410:
1.95 kristaps 411: if (r->first_string && ! roff_res(r, bufp, szp, pos))
1.94 kristaps 412: return(ROFF_RERUN);
413:
414: /*
1.79 kristaps 415: * First, if a scope is open and we're not a macro, pass the
416: * text through the macro's filter. If a scope isn't open and
417: * we're not a macro, just let it through.
418: */
1.74 kristaps 419:
1.75 kristaps 420: if (r->last && ! ROFF_CTL((*bufp)[pos])) {
1.78 kristaps 421: t = r->last->tok;
422: assert(roffs[t].text);
1.88 kristaps 423: ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
424: roffs[t].name, &(*bufp)[pos]);
1.78 kristaps 425: return((*roffs[t].text)
1.90 kristaps 426: (r, t, bufp, szp,
427: ln, pos, pos, offs));
1.94 kristaps 428: } else if ( ! ROFF_CTL((*bufp)[pos]))
1.67 kristaps 429: return(ROFF_CONT);
430:
1.79 kristaps 431: /*
432: * If a scope is open, go to the child handler for that macro,
433: * as it may want to preprocess before doing anything with it.
434: */
1.78 kristaps 435:
1.79 kristaps 436: if (r->last) {
437: t = r->last->tok;
438: assert(roffs[t].sub);
1.101 kristaps 439: ROFF_DEBUG("roff: intercept scoped context: %s, [%s]\n",
440: roffs[t].name, &(*bufp)[pos]);
1.79 kristaps 441: return((*roffs[t].sub)
1.90 kristaps 442: (r, t, bufp, szp,
443: ln, pos, pos, offs));
1.79 kristaps 444: }
1.78 kristaps 445:
1.79 kristaps 446: /*
447: * Lastly, as we've no scope open, try to look up and execute
448: * the new macro. If no macro is found, simply return and let
449: * the compilers handle it.
450: */
1.67 kristaps 451:
1.75 kristaps 452: ppos = pos;
1.94 kristaps 453: if (ROFF_MAX == (t = roff_parse(*bufp, &pos)))
1.79 kristaps 454: return(ROFF_CONT);
1.67 kristaps 455:
1.88 kristaps 456: ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
457: roffs[t].name, &(*bufp)[pos]);
1.75 kristaps 458: assert(roffs[t].proc);
1.78 kristaps 459: return((*roffs[t].proc)
1.90 kristaps 460: (r, t, bufp, szp,
461: ln, ppos, pos, offs));
1.74 kristaps 462: }
463:
464:
465: int
466: roff_endparse(struct roff *r)
467: {
468:
469: if (NULL == r->last)
470: return(1);
471: return((*r->msg)(MANDOCERR_SCOPEEXIT, r->data, r->last->line,
472: r->last->col, NULL));
1.67 kristaps 473: }
474:
475:
476: /*
477: * Parse a roff node's type from the input buffer. This must be in the
478: * form of ".foo xxx" in the usual way.
479: */
480: static enum rofft
481: roff_parse(const char *buf, int *pos)
482: {
483: int j;
484: char mac[5];
485: enum rofft t;
486:
1.75 kristaps 487: assert(ROFF_CTL(buf[*pos]));
488: (*pos)++;
1.67 kristaps 489:
490: while (buf[*pos] && (' ' == buf[*pos] || '\t' == buf[*pos]))
491: (*pos)++;
492:
493: if ('\0' == buf[*pos])
494: return(ROFF_MAX);
495:
496: for (j = 0; j < 4; j++, (*pos)++)
497: if ('\0' == (mac[j] = buf[*pos]))
498: break;
1.82 kristaps 499: else if (' ' == buf[*pos] || (j && '\\' == buf[*pos]))
1.67 kristaps 500: break;
501:
502: if (j == 4 || j < 1)
503: return(ROFF_MAX);
504:
505: mac[j] = '\0';
506:
507: if (ROFF_MAX == (t = roff_hash_find(mac)))
508: return(t);
509:
510: while (buf[*pos] && ' ' == buf[*pos])
511: (*pos)++;
512:
513: return(t);
514: }
515:
516:
1.89 kristaps 517: static int
1.91 kristaps 518: roff_parse_nat(const char *buf, unsigned int *res)
1.89 kristaps 519: {
520: char *ep;
521: long lval;
522:
523: errno = 0;
524: lval = strtol(buf, &ep, 10);
525: if (buf[0] == '\0' || *ep != '\0')
526: return(0);
527: if ((errno == ERANGE &&
528: (lval == LONG_MAX || lval == LONG_MIN)) ||
1.91 kristaps 529: (lval > INT_MAX || lval < 0))
1.89 kristaps 530: return(0);
531:
1.91 kristaps 532: *res = (unsigned int)lval;
1.89 kristaps 533: return(1);
534: }
535:
536:
1.67 kristaps 537: /* ARGSUSED */
538: static enum rofferr
1.76 kristaps 539: roff_cblock(ROFF_ARGS)
1.67 kristaps 540: {
541:
1.79 kristaps 542: /*
543: * A block-close `..' should only be invoked as a child of an
544: * ignore macro, otherwise raise a warning and just ignore it.
545: */
546:
1.76 kristaps 547: if (NULL == r->last) {
548: if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
549: return(ROFF_ERR);
550: return(ROFF_IGN);
551: }
1.67 kristaps 552:
1.81 kristaps 553: switch (r->last->tok) {
554: case (ROFF_am):
555: /* FALLTHROUGH */
556: case (ROFF_ami):
557: /* FALLTHROUGH */
558: case (ROFF_am1):
559: /* FALLTHROUGH */
560: case (ROFF_de):
561: /* FALLTHROUGH */
562: case (ROFF_dei):
563: /* FALLTHROUGH */
564: case (ROFF_de1):
565: /* FALLTHROUGH */
566: case (ROFF_ig):
567: break;
568: default:
1.76 kristaps 569: if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
570: return(ROFF_ERR);
1.67 kristaps 571: return(ROFF_IGN);
1.76 kristaps 572: }
1.67 kristaps 573:
1.76 kristaps 574: if ((*bufp)[pos])
575: if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
576: return(ROFF_ERR);
1.71 kristaps 577:
578: roffnode_pop(r);
1.76 kristaps 579: roffnode_cleanscope(r);
580: return(ROFF_IGN);
1.71 kristaps 581:
1.67 kristaps 582: }
583:
584:
1.76 kristaps 585: static void
586: roffnode_cleanscope(struct roff *r)
1.67 kristaps 587: {
588:
1.76 kristaps 589: while (r->last) {
590: if (--r->last->endspan < 0)
591: break;
592: roffnode_pop(r);
593: }
1.67 kristaps 594: }
595:
596:
1.75 kristaps 597: /* ARGSUSED */
1.74 kristaps 598: static enum rofferr
1.75 kristaps 599: roff_ccond(ROFF_ARGS)
1.74 kristaps 600: {
601:
1.76 kristaps 602: if (NULL == r->last) {
603: if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
604: return(ROFF_ERR);
605: return(ROFF_IGN);
606: }
607:
1.82 kristaps 608: switch (r->last->tok) {
609: case (ROFF_el):
610: /* FALLTHROUGH */
611: case (ROFF_ie):
612: /* FALLTHROUGH */
613: case (ROFF_if):
614: break;
615: default:
1.75 kristaps 616: if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
617: return(ROFF_ERR);
618: return(ROFF_IGN);
619: }
620:
1.76 kristaps 621: if (r->last->endspan > -1) {
622: if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
623: return(ROFF_ERR);
624: return(ROFF_IGN);
625: }
626:
627: if ((*bufp)[pos])
628: if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
629: return(ROFF_ERR);
630:
1.75 kristaps 631: roffnode_pop(r);
1.76 kristaps 632: roffnode_cleanscope(r);
633: return(ROFF_IGN);
634: }
635:
1.75 kristaps 636:
1.76 kristaps 637: /* ARGSUSED */
638: static enum rofferr
1.80 kristaps 639: roff_block(ROFF_ARGS)
1.76 kristaps 640: {
1.78 kristaps 641: int sv;
642: size_t sz;
1.76 kristaps 643:
1.80 kristaps 644: if (ROFF_ig != tok && '\0' == (*bufp)[pos]) {
645: if ( ! (*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
646: return(ROFF_ERR);
647: return(ROFF_IGN);
648: } else if (ROFF_ig != tok) {
649: while ((*bufp)[pos] && ' ' != (*bufp)[pos])
650: pos++;
651: while (' ' == (*bufp)[pos])
652: pos++;
653: }
654:
1.98 schwarze 655: roffnode_push(r, tok, ln, ppos);
1.76 kristaps 656:
1.79 kristaps 657: if ('\0' == (*bufp)[pos])
1.78 kristaps 658: return(ROFF_IGN);
659:
660: sv = pos;
661: while ((*bufp)[pos] && ' ' != (*bufp)[pos] &&
662: '\t' != (*bufp)[pos])
663: pos++;
664:
665: /*
666: * Note: groff does NOT like escape characters in the input.
667: * Instead of detecting this, we're just going to let it fly and
668: * to hell with it.
669: */
670:
671: assert(pos > sv);
672: sz = (size_t)(pos - sv);
673:
1.79 kristaps 674: if (1 == sz && '.' == (*bufp)[sv])
675: return(ROFF_IGN);
676:
1.98 schwarze 677: r->last->end = mandoc_malloc(sz + 1);
1.78 kristaps 678:
679: memcpy(r->last->end, *bufp + sv, sz);
680: r->last->end[(int)sz] = '\0';
681:
1.77 kristaps 682: if ((*bufp)[pos])
683: if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
684: return(ROFF_ERR);
1.74 kristaps 685:
1.78 kristaps 686: return(ROFF_IGN);
687: }
688:
689:
690: /* ARGSUSED */
691: static enum rofferr
1.80 kristaps 692: roff_block_sub(ROFF_ARGS)
1.79 kristaps 693: {
694: enum rofft t;
695: int i, j;
696:
697: /*
698: * First check whether a custom macro exists at this level. If
699: * it does, then check against it. This is some of groff's
700: * stranger behaviours. If we encountered a custom end-scope
701: * tag and that tag also happens to be a "real" macro, then we
702: * need to try interpreting it again as a real macro. If it's
703: * not, then return ignore. Else continue.
704: */
705:
706: if (r->last->end) {
707: i = pos + 1;
708: while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
709: i++;
710:
711: for (j = 0; r->last->end[j]; j++, i++)
712: if ((*bufp)[i] != r->last->end[j])
713: break;
714:
715: if ('\0' == r->last->end[j] &&
716: ('\0' == (*bufp)[i] ||
717: ' ' == (*bufp)[i] ||
718: '\t' == (*bufp)[i])) {
719: roffnode_pop(r);
720: roffnode_cleanscope(r);
721:
722: if (ROFF_MAX != roff_parse(*bufp, &pos))
723: return(ROFF_RERUN);
724: return(ROFF_IGN);
725: }
726: }
727:
728: /*
729: * If we have no custom end-query or lookup failed, then try
730: * pulling it out of the hashtable.
731: */
732:
733: ppos = pos;
734: t = roff_parse(*bufp, &pos);
735:
736: /* If we're not a comment-end, then throw it away. */
737: if (ROFF_cblock != t)
738: return(ROFF_IGN);
739:
740: assert(roffs[t].proc);
1.90 kristaps 741: return((*roffs[t].proc)(r, t, bufp, szp,
742: ln, ppos, pos, offs));
1.79 kristaps 743: }
744:
745:
746: /* ARGSUSED */
747: static enum rofferr
1.80 kristaps 748: roff_block_text(ROFF_ARGS)
1.78 kristaps 749: {
750:
751: return(ROFF_IGN);
752: }
753:
754:
755: /* ARGSUSED */
756: static enum rofferr
1.82 kristaps 757: roff_cond_sub(ROFF_ARGS)
758: {
759: enum rofft t;
760: enum roffrule rr;
761:
762: ppos = pos;
763: rr = r->last->rule;
764:
1.87 kristaps 765: /*
766: * Clean out scope. If we've closed ourselves, then don't
767: * continue.
768: */
769:
770: roffnode_cleanscope(r);
1.82 kristaps 771:
1.100 kristaps 772: if (ROFF_MAX == (t = roff_parse(*bufp, &pos))) {
773: if ('\\' == (*bufp)[pos] && '}' == (*bufp)[pos + 1])
774: return(roff_ccond
775: (r, ROFF_ccond, bufp, szp,
776: ln, pos, pos + 2, offs));
1.82 kristaps 777: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.100 kristaps 778: }
1.82 kristaps 779:
780: /*
781: * A denied conditional must evaluate its children if and only
782: * if they're either structurally required (such as loops and
783: * conditionals) or a closing macro.
784: */
785: if (ROFFRULE_DENY == rr)
786: if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
787: if (ROFF_ccond != t)
788: return(ROFF_IGN);
789:
790: assert(roffs[t].proc);
1.90 kristaps 791: return((*roffs[t].proc)(r, t, bufp, szp,
792: ln, ppos, pos, offs));
1.82 kristaps 793: }
794:
795:
796: /* ARGSUSED */
797: static enum rofferr
798: roff_cond_text(ROFF_ARGS)
1.78 kristaps 799: {
800: char *ep, *st;
1.82 kristaps 801: enum roffrule rr;
802:
803: rr = r->last->rule;
804:
805: /*
806: * We display the value of the text if out current evaluation
807: * scope permits us to do so.
808: */
1.100 kristaps 809:
810: /* FIXME: use roff_ccond? */
1.78 kristaps 811:
812: st = &(*bufp)[pos];
813: if (NULL == (ep = strstr(st, "\\}"))) {
814: roffnode_cleanscope(r);
1.82 kristaps 815: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.78 kristaps 816: }
817:
1.86 kristaps 818: if (ep == st || (ep > st && '\\' != *(ep - 1)))
1.78 kristaps 819: roffnode_pop(r);
820:
821: roffnode_cleanscope(r);
1.82 kristaps 822: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74 kristaps 823: }
824:
825:
1.88 kristaps 826: static enum roffrule
827: roff_evalcond(const char *v, int *pos)
828: {
829:
830: switch (v[*pos]) {
831: case ('n'):
832: (*pos)++;
833: return(ROFFRULE_ALLOW);
834: case ('e'):
835: /* FALLTHROUGH */
836: case ('o'):
837: /* FALLTHROUGH */
838: case ('t'):
839: (*pos)++;
840: return(ROFFRULE_DENY);
841: default:
842: break;
843: }
844:
845: while (v[*pos] && ' ' != v[*pos])
846: (*pos)++;
847: return(ROFFRULE_DENY);
848: }
849:
1.75 kristaps 850: /* ARGSUSED */
1.74 kristaps 851: static enum rofferr
1.103 kristaps 852: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 853: {
854:
855: return(ROFF_IGN);
856: }
857:
1.104 ! kristaps 858: /* ARGSUSED */
! 859: static enum rofferr
! 860: roff_line_error(ROFF_ARGS)
! 861: {
! 862:
! 863: (*r->msg)(MANDOCERR_REQUEST, r->data, ln, ppos, roffs[tok].name);
! 864: return(ROFF_IGN);
! 865: }
1.89 kristaps 866:
867: /* ARGSUSED */
868: static enum rofferr
1.82 kristaps 869: roff_cond(ROFF_ARGS)
1.74 kristaps 870: {
1.77 kristaps 871: int sv;
1.88 kristaps 872: enum roffrule rule;
1.74 kristaps 873:
1.82 kristaps 874: /* Stack overflow! */
875:
876: if (ROFF_ie == tok && r->rstackpos == RSTACK_MAX - 1) {
877: (*r->msg)(MANDOCERR_MEM, r->data, ln, ppos, NULL);
878: return(ROFF_ERR);
879: }
1.74 kristaps 880:
1.88 kristaps 881: /* First, evaluate the conditional. */
1.84 schwarze 882:
1.88 kristaps 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.77 kristaps 894:
895: sv = pos;
1.88 kristaps 896:
1.75 kristaps 897: while (' ' == (*bufp)[pos])
898: pos++;
1.74 kristaps 899:
1.77 kristaps 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.88 kristaps 906:
1.77 kristaps 907: if ('\0' == (*bufp)[pos] && sv != pos) {
1.88 kristaps 908: if ((*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
909: return(ROFF_IGN);
910: return(ROFF_ERR);
1.77 kristaps 911: }
912:
1.98 schwarze 913: roffnode_push(r, tok, ln, ppos);
1.77 kristaps 914:
1.88 kristaps 915: r->last->rule = rule;
916:
917: ROFF_DEBUG("roff: cond: %s -> %s\n", roffs[tok].name,
918: ROFFRULE_ALLOW == rule ? "allow" : "deny");
1.82 kristaps 919:
1.84 schwarze 920: if (ROFF_ie == tok) {
1.82 kristaps 921: /*
922: * An if-else will put the NEGATION of the current
923: * evaluated conditional into the stack.
924: */
925: r->rstackpos++;
926: if (ROFFRULE_DENY == r->last->rule)
927: r->rstack[r->rstackpos] = ROFFRULE_ALLOW;
928: else
929: r->rstack[r->rstackpos] = ROFFRULE_DENY;
930: }
1.88 kristaps 931:
932: /* If the parent has false as its rule, then so do we. */
933:
934: if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule) {
1.84 schwarze 935: r->last->rule = ROFFRULE_DENY;
1.88 kristaps 936: ROFF_DEBUG("roff: cond override: %s -> deny\n",
937: roffs[tok].name);
938: }
939:
940: /*
941: * Determine scope. If we're invoked with "\{" trailing the
942: * conditional, then we're in a multiline scope. Else our scope
943: * expires on the next line.
944: */
1.74 kristaps 945:
1.75 kristaps 946: r->last->endspan = 1;
947:
948: if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
949: r->last->endspan = -1;
950: pos += 2;
1.88 kristaps 951: ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
952: roffs[tok].name);
953: } else
954: ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
955: roffs[tok].name);
1.74 kristaps 956:
1.77 kristaps 957: /*
958: * If there are no arguments on the line, the next-line scope is
959: * assumed.
960: */
961:
1.75 kristaps 962: if ('\0' == (*bufp)[pos])
963: return(ROFF_IGN);
1.77 kristaps 964:
965: /* Otherwise re-run the roff parser after recalculating. */
1.74 kristaps 966:
1.75 kristaps 967: *offs = pos;
968: return(ROFF_RERUN);
1.83 schwarze 969: }
970:
971:
972: /* ARGSUSED */
973: static enum rofferr
1.92 schwarze 974: roff_ds(ROFF_ARGS)
975: {
1.96 kristaps 976: char *name, *string;
977:
978: /*
979: * A symbol is named by the first word following the macro
980: * invocation up to a space. Its value is anything after the
981: * name's trailing whitespace and optional double-quote. Thus,
982: *
983: * [.ds foo "bar " ]
984: *
985: * will have `bar " ' as its value.
986: */
1.92 schwarze 987:
988: name = *bufp + pos;
989: if ('\0' == *name)
990: return(ROFF_IGN);
991:
992: string = name;
1.96 kristaps 993: /* Read until end of name. */
1.92 schwarze 994: while (*string && ' ' != *string)
995: string++;
1.96 kristaps 996:
997: /* Nil-terminate name. */
1.92 schwarze 998: if (*string)
1.96 kristaps 999: *(string++) = '\0';
1000:
1001: /* Read past spaces. */
1002: while (*string && ' ' == *string)
1003: string++;
1004:
1005: /* Read passed initial double-quote. */
1.92 schwarze 1006: if (*string && '"' == *string)
1007: string++;
1008:
1.96 kristaps 1009: /* The rest is the value. */
1.94 kristaps 1010: roff_setstr(r, name, string);
1.92 schwarze 1011: return(ROFF_IGN);
1012: }
1013:
1014:
1015: /* ARGSUSED */
1016: static enum rofferr
1.89 kristaps 1017: roff_nr(ROFF_ARGS)
1.83 schwarze 1018: {
1.89 kristaps 1019: const char *key, *val;
1.91 kristaps 1020: struct reg *rg;
1.89 kristaps 1021:
1022: key = &(*bufp)[pos];
1.91 kristaps 1023: rg = r->regs->regs;
1.89 kristaps 1024:
1025: /* Parse register request. */
1026: while ((*bufp)[pos] && ' ' != (*bufp)[pos])
1027: pos++;
1028:
1029: /*
1030: * Set our nil terminator. Because this line is going to be
1031: * ignored anyway, we can munge it as we please.
1032: */
1033: if ((*bufp)[pos])
1034: (*bufp)[pos++] = '\0';
1035:
1036: /* Skip whitespace to register token. */
1037: while ((*bufp)[pos] && ' ' == (*bufp)[pos])
1038: pos++;
1039:
1040: val = &(*bufp)[pos];
1041:
1042: /* Process register token. */
1043:
1044: if (0 == strcmp(key, "nS")) {
1.91 kristaps 1045: rg[(int)REG_nS].set = 1;
1046: if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u))
1047: rg[(int)REG_nS].v.u = 0;
1.89 kristaps 1048:
1.91 kristaps 1049: ROFF_DEBUG("roff: register nS: %u\n",
1050: rg[(int)REG_nS].v.u);
1.89 kristaps 1051: } else
1052: ROFF_DEBUG("roff: ignoring register: %s\n", key);
1.83 schwarze 1053:
1054: return(ROFF_IGN);
1.92 schwarze 1055: }
1056:
1057:
1.99 kristaps 1058: static char *
1059: roff_strdup(const char *name)
1060: {
1061: char *namecopy, *sv;
1062:
1063: /*
1064: * This isn't a nice simple mandoc_strdup() because we must
1065: * handle roff's stupid double-escape rule.
1066: */
1067: sv = namecopy = mandoc_malloc(strlen(name) + 1);
1068: while (*name) {
1069: if ('\\' == *name && '\\' == *(name + 1))
1070: name++;
1071: *namecopy++ = *name++;
1072: }
1073:
1074: *namecopy = '\0';
1075: return(sv);
1076: }
1077:
1078:
1.94 kristaps 1079: static void
1080: roff_setstr(struct roff *r, const char *name, const char *string)
1.92 schwarze 1081: {
1082: struct roffstr *n;
1083: char *namecopy;
1084:
1.94 kristaps 1085: n = r->first_string;
1.92 schwarze 1086: while (n && strcmp(name, n->name))
1087: n = n->next;
1.94 kristaps 1088:
1089: if (NULL == n) {
1090: namecopy = mandoc_strdup(name);
1091: n = mandoc_malloc(sizeof(struct roffstr));
1092: n->name = namecopy;
1093: n->next = r->first_string;
1094: r->first_string = n;
1095: } else
1.92 schwarze 1096: free(n->string);
1.94 kristaps 1097:
1.99 kristaps 1098: /* Don't use mandoc_strdup: clean out double-escapes. */
1099: n->string = string ? roff_strdup(string) : NULL;
1100: ROFF_DEBUG("roff: new symbol: [%s] = [%s]\n", name, n->string);
1.92 schwarze 1101: }
1102:
1103:
1.94 kristaps 1104: static const char *
1105: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 1106: {
1.94 kristaps 1107: const struct roffstr *n;
1.92 schwarze 1108:
1.94 kristaps 1109: n = r->first_string;
1.97 kristaps 1110: while (n && (strncmp(name, n->name, len) || '\0' != n->name[(int)len]))
1.92 schwarze 1111: n = n->next;
1.94 kristaps 1112:
1113: return(n ? n->string : NULL);
1.92 schwarze 1114: }
1115:
1.94 kristaps 1116:
1117: static void
1118: roff_freestr(struct roff *r)
1.92 schwarze 1119: {
1120: struct roffstr *n, *nn;
1121:
1.94 kristaps 1122: for (n = r->first_string; n; n = nn) {
1.92 schwarze 1123: free(n->name);
1124: free(n->string);
1125: nn = n->next;
1126: free(n);
1127: }
1.94 kristaps 1128:
1129: r->first_string = NULL;
1.74 kristaps 1130: }
CVSweb