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