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