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