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