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