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