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