Annotation of src/usr.bin/mandoc/roff.c, Revision 1.68
1.68 ! schwarze 1: /* $Id: roff.c,v 1.67 2014/02/14 23:24:17 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.50 schwarze 1069: /* Always check for the closing delimiter `\}'. */
1.39 schwarze 1070:
1.50 schwarze 1071: ep = &(*bufp)[pos];
1072: while (NULL != (ep = strchr(ep, '\\'))) {
1073: if ('}' != *(++ep))
1074: continue;
1.2 schwarze 1075:
1.50 schwarze 1076: /*
1077: * If we're at the end of line, then just chop
1078: * off the \} and resize the buffer.
1079: * If we aren't, then convert it to spaces.
1080: */
1.37 schwarze 1081:
1.50 schwarze 1082: if ('\0' == *(ep + 1)) {
1083: *--ep = '\0';
1084: *szp -= 2;
1085: } else
1086: *(ep - 1) = *ep = ' ';
1.2 schwarze 1087:
1.68 ! schwarze 1088: roff_ccond(r, ln, pos);
1.50 schwarze 1089: }
1090: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.2 schwarze 1091: }
1092:
1093: /* ARGSUSED */
1094: static enum rofferr
1095: roff_cond_text(ROFF_ARGS)
1096: {
1.37 schwarze 1097: char *ep;
1.2 schwarze 1098: enum roffrule rr;
1099:
1100: rr = r->last->rule;
1.37 schwarze 1101: roffnode_cleanscope(r);
1.1 schwarze 1102:
1.37 schwarze 1103: ep = &(*bufp)[pos];
1104: for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
1105: ep++;
1106: if ('}' != *ep)
1107: continue;
1108: *ep = '&';
1.68 ! schwarze 1109: roff_ccond(r, ln, pos);
1.2 schwarze 1110: }
1111: return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1112: }
1113:
1.56 schwarze 1114: static int
1115: roff_getnum(const char *v, int *pos, int *res)
1116: {
1117: int p, n;
1118:
1119: p = *pos;
1120: n = v[p] == '-';
1121: if (n)
1122: p++;
1123:
1124: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1125: *res += 10 * *res + v[p] - '0';
1126: if (p == *pos + n)
1127: return 0;
1128:
1129: if (n)
1130: *res = -*res;
1131:
1132: *pos = p;
1133: return 1;
1134: }
1135:
1136: static int
1137: roff_getop(const char *v, int *pos, char *res)
1138: {
1139: int e;
1140:
1141: *res = v[*pos];
1142: e = v[*pos + 1] == '=';
1143:
1144: switch (*res) {
1145: case '=':
1146: break;
1147: case '>':
1148: if (e)
1149: *res = 'g';
1150: break;
1151: case '<':
1152: if (e)
1153: *res = 'l';
1154: break;
1155: default:
1156: return(0);
1157: }
1158:
1159: *pos += 1 + e;
1160:
1161: return(*res);
1162: }
1163:
1.5 schwarze 1164: static enum roffrule
1165: roff_evalcond(const char *v, int *pos)
1166: {
1.56 schwarze 1167: int not, lh, rh;
1168: char op;
1.5 schwarze 1169:
1170: switch (v[*pos]) {
1171: case ('n'):
1172: (*pos)++;
1173: return(ROFFRULE_ALLOW);
1174: case ('e'):
1175: /* FALLTHROUGH */
1176: case ('o'):
1177: /* FALLTHROUGH */
1178: case ('t'):
1179: (*pos)++;
1180: return(ROFFRULE_DENY);
1.56 schwarze 1181: case ('!'):
1182: (*pos)++;
1183: not = 1;
1184: break;
1.5 schwarze 1185: default:
1.56 schwarze 1186: not = 0;
1.5 schwarze 1187: break;
1188: }
1189:
1.56 schwarze 1190: if (!roff_getnum(v, pos, &lh))
1191: return ROFFRULE_DENY;
1192: if (!roff_getop(v, pos, &op)) {
1193: if (lh < 0)
1194: lh = 0;
1195: goto out;
1196: }
1197: if (!roff_getnum(v, pos, &rh))
1198: return ROFFRULE_DENY;
1199: switch (op) {
1200: case 'g':
1201: lh = lh >= rh;
1202: break;
1203: case 'l':
1204: lh = lh <= rh;
1205: break;
1206: case '=':
1207: lh = lh == rh;
1208: break;
1209: case '>':
1210: lh = lh > rh;
1211: break;
1212: case '<':
1213: lh = lh < rh;
1214: break;
1215: default:
1216: return ROFFRULE_DENY;
1217: }
1218: out:
1219: if (not)
1220: lh = !lh;
1221: return lh ? ROFFRULE_ALLOW : ROFFRULE_DENY;
1.5 schwarze 1222: }
1223:
1.2 schwarze 1224: /* ARGSUSED */
1225: static enum rofferr
1.21 schwarze 1226: roff_line_ignore(ROFF_ARGS)
1.6 schwarze 1227: {
1.30 schwarze 1228:
1.21 schwarze 1229: return(ROFF_IGN);
1230: }
1231:
1232: /* ARGSUSED */
1233: static enum rofferr
1.2 schwarze 1234: roff_cond(ROFF_ARGS)
1235: {
1.46 schwarze 1236:
1237: roffnode_push(r, tok, NULL, ln, ppos);
1.2 schwarze 1238:
1.35 schwarze 1239: /*
1240: * An `.el' has no conditional body: it will consume the value
1241: * of the current rstack entry set in prior `ie' calls or
1242: * defaults to DENY.
1243: *
1244: * If we're not an `el', however, then evaluate the conditional.
1245: */
1.1 schwarze 1246:
1.46 schwarze 1247: r->last->rule = ROFF_el == tok ?
1.35 schwarze 1248: (r->rstackpos < 0 ?
1249: ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
1250: roff_evalcond(*bufp, &pos);
1.2 schwarze 1251:
1.35 schwarze 1252: /*
1253: * An if-else will put the NEGATION of the current evaluated
1254: * conditional into the stack of rules.
1255: */
1256:
1.2 schwarze 1257: if (ROFF_ie == tok) {
1.35 schwarze 1258: if (r->rstackpos == RSTACK_MAX - 1) {
1259: mandoc_msg(MANDOCERR_MEM,
1260: r->parse, ln, ppos, NULL);
1261: return(ROFF_ERR);
1262: }
1263: r->rstack[++r->rstackpos] =
1264: ROFFRULE_DENY == r->last->rule ?
1265: ROFFRULE_ALLOW : ROFFRULE_DENY;
1.2 schwarze 1266: }
1.5 schwarze 1267:
1268: /* If the parent has false as its rule, then so do we. */
1269:
1.2 schwarze 1270: if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1271: r->last->rule = ROFFRULE_DENY;
1.5 schwarze 1272:
1273: /*
1.46 schwarze 1274: * Determine scope.
1275: * If there is nothing on the line after the conditional,
1276: * not even whitespace, use next-line scope.
1.5 schwarze 1277: */
1.2 schwarze 1278:
1.46 schwarze 1279: if ('\0' == (*bufp)[pos]) {
1280: r->last->endspan = 2;
1281: goto out;
1282: }
1283:
1284: while (' ' == (*bufp)[pos])
1285: pos++;
1286:
1287: /* An opening brace requests multiline scope. */
1.2 schwarze 1288:
1289: if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1290: r->last->endspan = -1;
1291: pos += 2;
1.46 schwarze 1292: goto out;
1.2 schwarze 1293: }
1294:
1295: /*
1.46 schwarze 1296: * Anything else following the conditional causes
1297: * single-line scope. Warn if the scope contains
1298: * nothing but trailing whitespace.
1.2 schwarze 1299: */
1300:
1301: if ('\0' == (*bufp)[pos])
1.46 schwarze 1302: mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.2 schwarze 1303:
1.46 schwarze 1304: r->last->endspan = 1;
1.1 schwarze 1305:
1.46 schwarze 1306: out:
1.2 schwarze 1307: *offs = pos;
1308: return(ROFF_RERUN);
1.1 schwarze 1309: }
1310:
1311:
1.2 schwarze 1312: /* ARGSUSED */
1313: static enum rofferr
1.7 schwarze 1314: roff_ds(ROFF_ARGS)
1315: {
1.10 schwarze 1316: char *name, *string;
1317:
1318: /*
1319: * A symbol is named by the first word following the macro
1320: * invocation up to a space. Its value is anything after the
1321: * name's trailing whitespace and optional double-quote. Thus,
1322: *
1323: * [.ds foo "bar " ]
1324: *
1325: * will have `bar " ' as its value.
1326: */
1.7 schwarze 1327:
1.28 schwarze 1328: string = *bufp + pos;
1329: name = roff_getname(r, &string, ln, pos);
1.7 schwarze 1330: if ('\0' == *name)
1331: return(ROFF_IGN);
1332:
1.28 schwarze 1333: /* Read past initial double-quote. */
1334: if ('"' == *string)
1.7 schwarze 1335: string++;
1336:
1.10 schwarze 1337: /* The rest is the value. */
1.66 schwarze 1338: roff_setstr(r, name, string, ROFF_as == tok);
1.7 schwarze 1339: return(ROFF_IGN);
1340: }
1341:
1.52 schwarze 1342: void
1.60 schwarze 1343: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.41 schwarze 1344: {
1.52 schwarze 1345: struct roffreg *reg;
1346:
1347: /* Search for an existing register with the same name. */
1348: reg = r->regtab;
1349:
1350: while (reg && strcmp(name, reg->key.p))
1351: reg = reg->next;
1.41 schwarze 1352:
1.52 schwarze 1353: if (NULL == reg) {
1354: /* Create a new register. */
1355: reg = mandoc_malloc(sizeof(struct roffreg));
1356: reg->key.p = mandoc_strdup(name);
1357: reg->key.sz = strlen(name);
1.60 schwarze 1358: reg->val = 0;
1.52 schwarze 1359: reg->next = r->regtab;
1360: r->regtab = reg;
1361: }
1362:
1.60 schwarze 1363: if ('+' == sign)
1364: reg->val += val;
1365: else if ('-' == sign)
1366: reg->val -= val;
1367: else
1368: reg->val = val;
1.41 schwarze 1369: }
1370:
1.65 schwarze 1371: /*
1372: * Handle some predefined read-only number registers.
1373: * For now, return -1 if the requested register is not predefined;
1374: * in case a predefined read-only register having the value -1
1375: * were to turn up, another special value would have to be chosen.
1376: */
1377: static int
1378: roff_getregro(const char *name)
1379: {
1380:
1381: switch (*name) {
1382: case ('A'): /* ASCII approximation mode is always off. */
1383: return(0);
1384: case ('g'): /* Groff compatibility mode is always on. */
1385: return(1);
1386: case ('H'): /* Fixed horizontal resolution. */
1387: return (24);
1388: case ('j'): /* Always adjust left margin only. */
1389: return(0);
1390: case ('T'): /* Some output device is always defined. */
1391: return(1);
1392: case ('V'): /* Fixed vertical resolution. */
1393: return (40);
1394: default:
1395: return (-1);
1396: }
1397: }
1398:
1.53 schwarze 1399: int
1.52 schwarze 1400: roff_getreg(const struct roff *r, const char *name)
1.41 schwarze 1401: {
1.52 schwarze 1402: struct roffreg *reg;
1.65 schwarze 1403: int val;
1404:
1405: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
1406: val = roff_getregro(name + 1);
1407: if (-1 != val)
1408: return (val);
1409: }
1.52 schwarze 1410:
1411: for (reg = r->regtab; reg; reg = reg->next)
1412: if (0 == strcmp(name, reg->key.p))
1.53 schwarze 1413: return(reg->val);
1414:
1415: return(0);
1416: }
1417:
1418: static int
1419: roff_getregn(const struct roff *r, const char *name, size_t len)
1420: {
1421: struct roffreg *reg;
1.65 schwarze 1422: int val;
1423:
1424: if ('.' == name[0] && 2 == len) {
1425: val = roff_getregro(name + 1);
1426: if (-1 != val)
1427: return (val);
1428: }
1.53 schwarze 1429:
1430: for (reg = r->regtab; reg; reg = reg->next)
1431: if (len == reg->key.sz &&
1432: 0 == strncmp(name, reg->key.p, len))
1433: return(reg->val);
1.41 schwarze 1434:
1.52 schwarze 1435: return(0);
1.41 schwarze 1436: }
1437:
1.52 schwarze 1438: static void
1439: roff_freereg(struct roffreg *reg)
1.41 schwarze 1440: {
1.52 schwarze 1441: struct roffreg *old_reg;
1.41 schwarze 1442:
1.52 schwarze 1443: while (NULL != reg) {
1444: free(reg->key.p);
1445: old_reg = reg;
1446: reg = reg->next;
1447: free(old_reg);
1448: }
1.41 schwarze 1449: }
1.7 schwarze 1450:
1451: /* ARGSUSED */
1452: static enum rofferr
1.6 schwarze 1453: roff_nr(ROFF_ARGS)
1.1 schwarze 1454: {
1.28 schwarze 1455: const char *key;
1456: char *val;
1.60 schwarze 1457: size_t sz;
1.37 schwarze 1458: int iv;
1.60 schwarze 1459: char sign;
1.6 schwarze 1460:
1.28 schwarze 1461: val = *bufp + pos;
1462: key = roff_getname(r, &val, ln, pos);
1.6 schwarze 1463:
1.60 schwarze 1464: sign = *val;
1465: if ('+' == sign || '-' == sign)
1466: val++;
1467:
1468: sz = strspn(val, "0123456789");
1469: iv = sz ? mandoc_strntoi(val, sz, 10) : 0;
1.52 schwarze 1470:
1.60 schwarze 1471: roff_setreg(r, key, iv, sign);
1.1 schwarze 1472:
1.29 schwarze 1473: return(ROFF_IGN);
1474: }
1475:
1476: /* ARGSUSED */
1477: static enum rofferr
1478: roff_rm(ROFF_ARGS)
1479: {
1480: const char *name;
1481: char *cp;
1482:
1483: cp = *bufp + pos;
1484: while ('\0' != *cp) {
1.34 schwarze 1485: name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1.29 schwarze 1486: if ('\0' != *name)
1487: roff_setstr(r, name, NULL, 0);
1488: }
1.51 schwarze 1489: return(ROFF_IGN);
1490: }
1491:
1492: /* ARGSUSED */
1493: static enum rofferr
1494: roff_it(ROFF_ARGS)
1495: {
1496: char *cp;
1497: size_t len;
1498: int iv;
1499:
1500: /* Parse the number of lines. */
1501: cp = *bufp + pos;
1502: len = strcspn(cp, " \t");
1503: cp[len] = '\0';
1504: if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1505: mandoc_msg(MANDOCERR_NUMERIC, r->parse,
1506: ln, ppos, *bufp + 1);
1507: return(ROFF_IGN);
1508: }
1509: cp += len + 1;
1510:
1511: /* Arm the input line trap. */
1512: roffit_lines = iv;
1513: roffit_macro = mandoc_strdup(cp);
1.2 schwarze 1514: return(ROFF_IGN);
1.47 schwarze 1515: }
1516:
1517: /* ARGSUSED */
1518: static enum rofferr
1519: roff_Dd(ROFF_ARGS)
1520: {
1521: const char *const *cp;
1522:
1.63 schwarze 1523: if (0 == r->quick && MPARSE_MDOC != r->parsetype)
1.47 schwarze 1524: for (cp = __mdoc_reserved; *cp; cp++)
1525: roff_setstr(r, *cp, NULL, 0);
1526:
1527: return(ROFF_CONT);
1528: }
1529:
1530: /* ARGSUSED */
1531: static enum rofferr
1532: roff_TH(ROFF_ARGS)
1533: {
1534: const char *const *cp;
1535:
1.63 schwarze 1536: if (0 == r->quick && MPARSE_MDOC != r->parsetype)
1.47 schwarze 1537: for (cp = __man_reserved; *cp; cp++)
1538: roff_setstr(r, *cp, NULL, 0);
1539:
1540: return(ROFF_CONT);
1.14 schwarze 1541: }
1542:
1543: /* ARGSUSED */
1544: static enum rofferr
1.27 schwarze 1545: roff_TE(ROFF_ARGS)
1546: {
1547:
1548: if (NULL == r->tbl)
1.35 schwarze 1549: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.27 schwarze 1550: else
1.41 schwarze 1551: tbl_end(&r->tbl);
1.27 schwarze 1552:
1553: return(ROFF_IGN);
1554: }
1555:
1556: /* ARGSUSED */
1557: static enum rofferr
1558: roff_T_(ROFF_ARGS)
1559: {
1560:
1561: if (NULL == r->tbl)
1.35 schwarze 1562: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.27 schwarze 1563: else
1564: tbl_restart(ppos, ln, r->tbl);
1565:
1566: return(ROFF_IGN);
1567: }
1568:
1.41 schwarze 1569: #if 0
1570: static int
1571: roff_closeeqn(struct roff *r)
1572: {
1573:
1574: return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1575: }
1576: #endif
1577:
1578: static void
1579: roff_openeqn(struct roff *r, const char *name, int line,
1580: int offs, const char *buf)
1.32 schwarze 1581: {
1.41 schwarze 1582: struct eqn_node *e;
1583: int poff;
1.32 schwarze 1584:
1585: assert(NULL == r->eqn);
1.41 schwarze 1586: e = eqn_alloc(name, offs, line, r->parse);
1.32 schwarze 1587:
1588: if (r->last_eqn)
1589: r->last_eqn->next = e;
1590: else
1591: r->first_eqn = r->last_eqn = e;
1592:
1593: r->eqn = r->last_eqn = e;
1.41 schwarze 1594:
1595: if (buf) {
1596: poff = 0;
1597: eqn_read(&r->eqn, line, buf, offs, &poff);
1598: }
1599: }
1600:
1601: /* ARGSUSED */
1602: static enum rofferr
1603: roff_EQ(ROFF_ARGS)
1604: {
1605:
1606: roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.32 schwarze 1607: return(ROFF_IGN);
1608: }
1609:
1610: /* ARGSUSED */
1611: static enum rofferr
1612: roff_EN(ROFF_ARGS)
1613: {
1614:
1.35 schwarze 1615: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.32 schwarze 1616: return(ROFF_IGN);
1617: }
1618:
1619: /* ARGSUSED */
1620: static enum rofferr
1.27 schwarze 1621: roff_TS(ROFF_ARGS)
1622: {
1.49 schwarze 1623: struct tbl_node *tbl;
1.27 schwarze 1624:
1625: if (r->tbl) {
1.35 schwarze 1626: mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.41 schwarze 1627: tbl_end(&r->tbl);
1.27 schwarze 1628: }
1629:
1.49 schwarze 1630: tbl = tbl_alloc(ppos, ln, r->parse);
1.27 schwarze 1631:
1632: if (r->last_tbl)
1.49 schwarze 1633: r->last_tbl->next = tbl;
1.27 schwarze 1634: else
1.49 schwarze 1635: r->first_tbl = r->last_tbl = tbl;
1.27 schwarze 1636:
1.49 schwarze 1637: r->tbl = r->last_tbl = tbl;
1.27 schwarze 1638: return(ROFF_IGN);
1639: }
1640:
1641: /* ARGSUSED */
1642: static enum rofferr
1.48 schwarze 1643: roff_cc(ROFF_ARGS)
1644: {
1645: const char *p;
1646:
1647: p = *bufp + pos;
1648:
1649: if ('\0' == *p || '.' == (r->control = *p++))
1650: r->control = 0;
1651:
1652: if ('\0' != *p)
1653: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1654:
1655: return(ROFF_IGN);
1656: }
1657:
1658: /* ARGSUSED */
1659: static enum rofferr
1.42 schwarze 1660: roff_tr(ROFF_ARGS)
1661: {
1662: const char *p, *first, *second;
1663: size_t fsz, ssz;
1664: enum mandoc_esc esc;
1665:
1666: p = *bufp + pos;
1667:
1668: if ('\0' == *p) {
1669: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1670: return(ROFF_IGN);
1671: }
1672:
1673: while ('\0' != *p) {
1674: fsz = ssz = 1;
1675:
1676: first = p++;
1677: if ('\\' == *first) {
1678: esc = mandoc_escape(&p, NULL, NULL);
1679: if (ESCAPE_ERROR == esc) {
1680: mandoc_msg
1681: (MANDOCERR_BADESCAPE, r->parse,
1682: ln, (int)(p - *bufp), NULL);
1683: return(ROFF_IGN);
1684: }
1685: fsz = (size_t)(p - first);
1686: }
1687:
1688: second = p++;
1689: if ('\\' == *second) {
1690: esc = mandoc_escape(&p, NULL, NULL);
1691: if (ESCAPE_ERROR == esc) {
1692: mandoc_msg
1693: (MANDOCERR_BADESCAPE, r->parse,
1694: ln, (int)(p - *bufp), NULL);
1695: return(ROFF_IGN);
1696: }
1697: ssz = (size_t)(p - second);
1698: } else if ('\0' == *second) {
1699: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1700: ln, (int)(p - *bufp), NULL);
1701: second = " ";
1702: p--;
1703: }
1704:
1705: if (fsz > 1) {
1706: roff_setstrn(&r->xmbtab, first,
1707: fsz, second, ssz, 0);
1708: continue;
1709: }
1710:
1711: if (NULL == r->xtab)
1712: r->xtab = mandoc_calloc
1713: (128, sizeof(struct roffstr));
1714:
1715: free(r->xtab[(int)*first].p);
1716: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
1717: r->xtab[(int)*first].sz = ssz;
1718: }
1719:
1720: return(ROFF_IGN);
1721: }
1722:
1723: /* ARGSUSED */
1724: static enum rofferr
1.14 schwarze 1725: roff_so(ROFF_ARGS)
1726: {
1727: char *name;
1.15 schwarze 1728:
1.35 schwarze 1729: mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1.14 schwarze 1730:
1.22 schwarze 1731: /*
1732: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1733: * opening anything that's not in our cwd or anything beneath
1734: * it. Thus, explicitly disallow traversing up the file-system
1735: * or using absolute paths.
1736: */
1737:
1.14 schwarze 1738: name = *bufp + pos;
1739: if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.35 schwarze 1740: mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1.14 schwarze 1741: return(ROFF_ERR);
1742: }
1743:
1744: *offs = pos;
1745: return(ROFF_SO);
1.7 schwarze 1746: }
1747:
1.16 schwarze 1748: /* ARGSUSED */
1749: static enum rofferr
1750: roff_userdef(ROFF_ARGS)
1.12 schwarze 1751: {
1.16 schwarze 1752: const char *arg[9];
1753: char *cp, *n1, *n2;
1.25 schwarze 1754: int i;
1.12 schwarze 1755:
1.16 schwarze 1756: /*
1757: * Collect pointers to macro argument strings
1.61 schwarze 1758: * and NUL-terminate them.
1.16 schwarze 1759: */
1760: cp = *bufp + pos;
1.25 schwarze 1761: for (i = 0; i < 9; i++)
1.26 schwarze 1762: arg[i] = '\0' == *cp ? "" :
1.35 schwarze 1763: mandoc_getarg(r->parse, &cp, ln, &pos);
1.16 schwarze 1764:
1765: /*
1766: * Expand macro arguments.
1.12 schwarze 1767: */
1.16 schwarze 1768: *szp = 0;
1769: n1 = cp = mandoc_strdup(r->current_string);
1770: while (NULL != (cp = strstr(cp, "\\$"))) {
1771: i = cp[2] - '1';
1772: if (0 > i || 8 < i) {
1773: /* Not an argument invocation. */
1774: cp += 2;
1775: continue;
1776: }
1777:
1778: *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1779: n2 = mandoc_malloc(*szp);
1780:
1781: strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1782: strlcat(n2, arg[i], *szp);
1783: strlcat(n2, cp + 3, *szp);
1784:
1785: cp = n2 + (cp - n1);
1786: free(n1);
1787: n1 = n2;
1.12 schwarze 1788: }
1789:
1.16 schwarze 1790: /*
1791: * Replace the macro invocation
1792: * by the expanded macro.
1793: */
1794: free(*bufp);
1795: *bufp = n1;
1796: if (0 == *szp)
1797: *szp = strlen(*bufp) + 1;
1798:
1.19 schwarze 1799: return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1.16 schwarze 1800: ROFF_REPARSE : ROFF_APPEND);
1.12 schwarze 1801: }
1.28 schwarze 1802:
1803: static char *
1804: roff_getname(struct roff *r, char **cpp, int ln, int pos)
1805: {
1806: char *name, *cp;
1807:
1808: name = *cpp;
1809: if ('\0' == *name)
1810: return(name);
1811:
1812: /* Read until end of name. */
1813: for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
1814: if ('\\' != *cp)
1815: continue;
1816: cp++;
1817: if ('\\' == *cp)
1818: continue;
1.35 schwarze 1819: mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1.28 schwarze 1820: *cp = '\0';
1821: name = cp;
1822: }
1823:
1824: /* Nil-terminate name. */
1825: if ('\0' != *cp)
1826: *(cp++) = '\0';
1827:
1828: /* Read past spaces. */
1829: while (' ' == *cp)
1830: cp++;
1831:
1832: *cpp = cp;
1833: return(name);
1834: }
1835:
1.16 schwarze 1836: /*
1837: * Store *string into the user-defined string called *name.
1838: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.66 schwarze 1839: * append == 0: replace mode
1840: * append == 1: single-line append mode
1841: * append == 2: multiline append mode, append '\n' after each call
1.16 schwarze 1842: */
1.8 schwarze 1843: static void
1.16 schwarze 1844: roff_setstr(struct roff *r, const char *name, const char *string,
1.66 schwarze 1845: int append)
1.7 schwarze 1846: {
1.42 schwarze 1847:
1848: roff_setstrn(&r->strtab, name, strlen(name), string,
1.66 schwarze 1849: string ? strlen(string) : 0, append);
1.42 schwarze 1850: }
1851:
1852: static void
1853: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.66 schwarze 1854: const char *string, size_t stringsz, int append)
1.42 schwarze 1855: {
1856: struct roffkv *n;
1857: char *c;
1858: int i;
1859: size_t oldch, newch;
1.7 schwarze 1860:
1.16 schwarze 1861: /* Search for an existing string with the same name. */
1.42 schwarze 1862: n = *r;
1863:
1864: while (n && strcmp(name, n->key.p))
1.7 schwarze 1865: n = n->next;
1.8 schwarze 1866:
1867: if (NULL == n) {
1.16 schwarze 1868: /* Create a new string table entry. */
1.42 schwarze 1869: n = mandoc_malloc(sizeof(struct roffkv));
1870: n->key.p = mandoc_strndup(name, namesz);
1871: n->key.sz = namesz;
1872: n->val.p = NULL;
1873: n->val.sz = 0;
1874: n->next = *r;
1875: *r = n;
1.66 schwarze 1876: } else if (0 == append) {
1.42 schwarze 1877: free(n->val.p);
1878: n->val.p = NULL;
1879: n->val.sz = 0;
1.16 schwarze 1880: }
1881:
1882: if (NULL == string)
1883: return;
1884:
1885: /*
1886: * One additional byte for the '\n' in multiline mode,
1887: * and one for the terminating '\0'.
1888: */
1.66 schwarze 1889: newch = stringsz + (1 < append ? 2u : 1u);
1.42 schwarze 1890:
1891: if (NULL == n->val.p) {
1892: n->val.p = mandoc_malloc(newch);
1893: *n->val.p = '\0';
1.16 schwarze 1894: oldch = 0;
1895: } else {
1.42 schwarze 1896: oldch = n->val.sz;
1897: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.16 schwarze 1898: }
1899:
1900: /* Skip existing content in the destination buffer. */
1.42 schwarze 1901: c = n->val.p + (int)oldch;
1.16 schwarze 1902:
1903: /* Append new content to the destination buffer. */
1.42 schwarze 1904: i = 0;
1905: while (i < (int)stringsz) {
1.16 schwarze 1906: /*
1907: * Rudimentary roff copy mode:
1908: * Handle escaped backslashes.
1909: */
1.42 schwarze 1910: if ('\\' == string[i] && '\\' == string[i + 1])
1911: i++;
1912: *c++ = string[i++];
1.16 schwarze 1913: }
1.8 schwarze 1914:
1.16 schwarze 1915: /* Append terminating bytes. */
1.66 schwarze 1916: if (1 < append)
1.16 schwarze 1917: *c++ = '\n';
1.42 schwarze 1918:
1.16 schwarze 1919: *c = '\0';
1.42 schwarze 1920: n->val.sz = (int)(c - n->val.p);
1.7 schwarze 1921: }
1922:
1.8 schwarze 1923: static const char *
1924: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.7 schwarze 1925: {
1.42 schwarze 1926: const struct roffkv *n;
1.64 schwarze 1927: int i;
1.7 schwarze 1928:
1.42 schwarze 1929: for (n = r->strtab; n; n = n->next)
1930: if (0 == strncmp(name, n->key.p, len) &&
1931: '\0' == n->key.p[(int)len])
1932: return(n->val.p);
1.64 schwarze 1933:
1934: for (i = 0; i < PREDEFS_MAX; i++)
1935: if (0 == strncmp(name, predefs[i].name, len) &&
1936: '\0' == predefs[i].name[(int)len])
1937: return(predefs[i].str);
1.8 schwarze 1938:
1.42 schwarze 1939: return(NULL);
1.7 schwarze 1940: }
1941:
1.8 schwarze 1942: static void
1.42 schwarze 1943: roff_freestr(struct roffkv *r)
1.7 schwarze 1944: {
1.42 schwarze 1945: struct roffkv *n, *nn;
1.7 schwarze 1946:
1.42 schwarze 1947: for (n = r; n; n = nn) {
1948: free(n->key.p);
1949: free(n->val.p);
1.7 schwarze 1950: nn = n->next;
1951: free(n);
1952: }
1.27 schwarze 1953: }
1954:
1955: const struct tbl_span *
1956: roff_span(const struct roff *r)
1957: {
1958:
1959: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.32 schwarze 1960: }
1961:
1962: const struct eqn *
1963: roff_eqn(const struct roff *r)
1964: {
1965:
1966: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.42 schwarze 1967: }
1968:
1969: /*
1970: * Duplicate an input string, making the appropriate character
1971: * conversations (as stipulated by `tr') along the way.
1972: * Returns a heap-allocated string with all the replacements made.
1973: */
1974: char *
1975: roff_strdup(const struct roff *r, const char *p)
1976: {
1977: const struct roffkv *cp;
1978: char *res;
1979: const char *pp;
1980: size_t ssz, sz;
1981: enum mandoc_esc esc;
1982:
1983: if (NULL == r->xmbtab && NULL == r->xtab)
1984: return(mandoc_strdup(p));
1985: else if ('\0' == *p)
1986: return(mandoc_strdup(""));
1987:
1988: /*
1989: * Step through each character looking for term matches
1990: * (remember that a `tr' can be invoked with an escape, which is
1991: * a glyph but the escape is multi-character).
1992: * We only do this if the character hash has been initialised
1993: * and the string is >0 length.
1994: */
1995:
1996: res = NULL;
1997: ssz = 0;
1998:
1999: while ('\0' != *p) {
2000: if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
2001: sz = r->xtab[(int)*p].sz;
2002: res = mandoc_realloc(res, ssz + sz + 1);
2003: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
2004: ssz += sz;
2005: p++;
2006: continue;
2007: } else if ('\\' != *p) {
2008: res = mandoc_realloc(res, ssz + 2);
2009: res[ssz++] = *p++;
2010: continue;
2011: }
2012:
2013: /* Search for term matches. */
2014: for (cp = r->xmbtab; cp; cp = cp->next)
2015: if (0 == strncmp(p, cp->key.p, cp->key.sz))
2016: break;
2017:
2018: if (NULL != cp) {
2019: /*
2020: * A match has been found.
2021: * Append the match to the array and move
2022: * forward by its keysize.
2023: */
2024: res = mandoc_realloc
2025: (res, ssz + cp->val.sz + 1);
2026: memcpy(res + ssz, cp->val.p, cp->val.sz);
2027: ssz += cp->val.sz;
2028: p += (int)cp->key.sz;
2029: continue;
2030: }
2031:
2032: /*
2033: * Handle escapes carefully: we need to copy
2034: * over just the escape itself, or else we might
2035: * do replacements within the escape itself.
2036: * Make sure to pass along the bogus string.
2037: */
2038: pp = p++;
2039: esc = mandoc_escape(&p, NULL, NULL);
2040: if (ESCAPE_ERROR == esc) {
2041: sz = strlen(pp);
2042: res = mandoc_realloc(res, ssz + sz + 1);
2043: memcpy(res + ssz, pp, sz);
2044: break;
2045: }
2046: /*
2047: * We bail out on bad escapes.
2048: * No need to warn: we already did so when
2049: * roff_res() was called.
2050: */
2051: sz = (int)(p - pp);
2052: res = mandoc_realloc(res, ssz + sz + 1);
2053: memcpy(res + ssz, pp, sz);
2054: ssz += sz;
2055: }
2056:
2057: res[(int)ssz] = '\0';
2058: return(res);
1.48 schwarze 2059: }
2060:
2061: /*
2062: * Find out whether a line is a macro line or not.
2063: * If it is, adjust the current position and return one; if it isn't,
2064: * return zero and don't change the current position.
2065: * If the control character has been set with `.cc', then let that grain
2066: * precedence.
2067: * This is slighly contrary to groff, where using the non-breaking
2068: * control character when `cc' has been invoked will cause the
2069: * non-breaking macro contents to be printed verbatim.
2070: */
2071: int
2072: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
2073: {
2074: int pos;
2075:
2076: pos = *ppos;
2077:
2078: if (0 != r->control && cp[pos] == r->control)
2079: pos++;
2080: else if (0 != r->control)
2081: return(0);
2082: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
2083: pos += 2;
2084: else if ('.' == cp[pos] || '\'' == cp[pos])
2085: pos++;
2086: else
2087: return(0);
2088:
2089: while (' ' == cp[pos] || '\t' == cp[pos])
2090: pos++;
2091:
2092: *ppos = pos;
2093: return(1);
1.1 schwarze 2094: }