Annotation of src/usr.bin/mandoc/roff.c, Revision 1.77
1.77 ! schwarze 1: /* $Id: roff.c,v 1.76 2014/04/05 20:33:38 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 */ \
136: char **bufp, /* input buffer */ \
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.8 schwarze 192: static const char *roff_getstrn(const struct roff *,
193: const char *, size_t);
1.51 schwarze 194: static enum rofferr roff_it(ROFF_ARGS);
1.21 schwarze 195: static enum rofferr roff_line_ignore(ROFF_ARGS);
1.6 schwarze 196: static enum rofferr roff_nr(ROFF_ARGS);
1.41 schwarze 197: static void roff_openeqn(struct roff *, const char *,
198: int, int, const char *);
1.42 schwarze 199: static enum rofft roff_parse(struct roff *, const char *, int *);
1.51 schwarze 200: static enum rofferr roff_parsetext(char **, size_t *, int, int *);
1.45 schwarze 201: static enum rofferr roff_res(struct roff *,
1.37 schwarze 202: char **, size_t *, int, int);
1.29 schwarze 203: static enum rofferr roff_rm(ROFF_ARGS);
1.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.42 schwarze 207: static void roff_setstrn(struct roffkv **, const char *,
208: size_t, const char *, size_t, int);
1.14 schwarze 209: static enum rofferr roff_so(ROFF_ARGS);
1.42 schwarze 210: static enum rofferr roff_tr(ROFF_ARGS);
1.47 schwarze 211: static enum rofferr roff_Dd(ROFF_ARGS);
212: static enum rofferr roff_TH(ROFF_ARGS);
1.27 schwarze 213: static enum rofferr roff_TE(ROFF_ARGS);
214: static enum rofferr roff_TS(ROFF_ARGS);
1.32 schwarze 215: static enum rofferr roff_EQ(ROFF_ARGS);
216: static enum rofferr roff_EN(ROFF_ARGS);
1.27 schwarze 217: static enum rofferr roff_T_(ROFF_ARGS);
1.16 schwarze 218: static enum rofferr roff_userdef(ROFF_ARGS);
1.1 schwarze 219:
1.42 schwarze 220: /* See roffhash_find() */
1.3 schwarze 221:
222: #define ASCII_HI 126
223: #define ASCII_LO 33
224: #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
225:
226: static struct roffmac *hash[HASHWIDTH];
227:
228: static struct roffmac roffs[ROFF_MAX] = {
1.21 schwarze 229: { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.3 schwarze 230: { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
231: { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
232: { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.66 schwarze 233: { "as", roff_ds, NULL, NULL, 0, NULL },
1.48 schwarze 234: { "cc", roff_cc, NULL, NULL, 0, NULL },
1.67 schwarze 235: { "ce", roff_line_ignore, NULL, NULL, 0, NULL },
1.3 schwarze 236: { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
237: { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
238: { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.7 schwarze 239: { "ds", roff_ds, NULL, NULL, 0, NULL },
1.3 schwarze 240: { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.58 schwarze 241: { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
1.59 schwarze 242: { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
1.21 schwarze 243: { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.3 schwarze 244: { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
245: { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
246: { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.51 schwarze 247: { "it", roff_it, NULL, NULL, 0, NULL },
1.21 schwarze 248: { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
249: { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.14 schwarze 250: { "nr", roff_nr, NULL, NULL, 0, NULL },
1.31 schwarze 251: { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
252: { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.29 schwarze 253: { "rm", roff_rm, NULL, NULL, 0, NULL },
1.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.3 schwarze 314: static void
1.42 schwarze 315: roffhash_init(void)
1.3 schwarze 316: {
317: struct roffmac *n;
318: int buc, i;
319:
1.16 schwarze 320: for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.3 schwarze 321: assert(roffs[i].name[0] >= ASCII_LO);
322: assert(roffs[i].name[0] <= ASCII_HI);
323:
324: buc = ROFF_HASH(roffs[i].name);
325:
326: if (NULL != (n = hash[buc])) {
327: for ( ; n->next; n = n->next)
328: /* Do nothing. */ ;
329: n->next = &roffs[i];
330: } else
331: hash[buc] = &roffs[i];
332: }
333: }
334:
1.1 schwarze 335: /*
336: * Look up a roff token by its name. Returns ROFF_MAX if no macro by
337: * the nil-terminated string name could be found.
338: */
339: static enum rofft
1.42 schwarze 340: roffhash_find(const char *p, size_t s)
1.1 schwarze 341: {
1.3 schwarze 342: int buc;
343: struct roffmac *n;
1.1 schwarze 344:
1.3 schwarze 345: /*
346: * libroff has an extremely simple hashtable, for the time
347: * being, which simply keys on the first character, which must
348: * be printable, then walks a chain. It works well enough until
349: * optimised.
350: */
351:
352: if (p[0] < ASCII_LO || p[0] > ASCII_HI)
353: return(ROFF_MAX);
354:
355: buc = ROFF_HASH(p);
356:
357: if (NULL == (n = hash[buc]))
358: return(ROFF_MAX);
359: for ( ; n; n = n->next)
1.16 schwarze 360: if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.3 schwarze 361: return((enum rofft)(n - roffs));
1.1 schwarze 362:
363: return(ROFF_MAX);
364: }
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);
377: p = r->last;
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: /*
387: * Push a roff node onto the instruction stack. This must later be
388: * removed with roffnode_pop().
389: */
1.11 schwarze 390: static void
1.16 schwarze 391: roffnode_push(struct roff *r, enum rofft tok, const char *name,
392: int line, int col)
1.1 schwarze 393: {
394: struct roffnode *p;
395:
1.11 schwarze 396: p = mandoc_calloc(1, sizeof(struct roffnode));
1.1 schwarze 397: p->tok = tok;
1.16 schwarze 398: if (name)
399: p->name = mandoc_strdup(name);
1.1 schwarze 400: p->parent = r->last;
401: p->line = line;
402: p->col = col;
1.71 schwarze 403: p->rule = p->parent ? p->parent->rule : 0;
1.1 schwarze 404:
405: r->last = p;
406: }
407:
408:
409: static void
410: roff_free1(struct roff *r)
411: {
1.49 schwarze 412: struct tbl_node *tbl;
1.32 schwarze 413: struct eqn_node *e;
1.42 schwarze 414: int i;
1.27 schwarze 415:
1.49 schwarze 416: while (NULL != (tbl = r->first_tbl)) {
417: r->first_tbl = tbl->next;
418: tbl_free(tbl);
1.27 schwarze 419: }
420:
421: r->first_tbl = r->last_tbl = r->tbl = NULL;
1.1 schwarze 422:
1.32 schwarze 423: while (NULL != (e = r->first_eqn)) {
424: r->first_eqn = e->next;
425: eqn_free(e);
426: }
427:
428: r->first_eqn = r->last_eqn = r->eqn = NULL;
429:
1.1 schwarze 430: while (r->last)
431: roffnode_pop(r);
1.27 schwarze 432:
1.42 schwarze 433: roff_freestr(r->strtab);
434: roff_freestr(r->xmbtab);
435:
436: r->strtab = r->xmbtab = NULL;
437:
1.52 schwarze 438: roff_freereg(r->regtab);
439:
440: r->regtab = NULL;
441:
1.42 schwarze 442: if (r->xtab)
443: for (i = 0; i < 128; i++)
444: free(r->xtab[i].p);
445:
446: free(r->xtab);
447: r->xtab = NULL;
1.1 schwarze 448: }
449:
450: void
451: roff_reset(struct roff *r)
452: {
453:
454: roff_free1(r);
1.48 schwarze 455: r->control = 0;
1.1 schwarze 456: }
457:
458:
459: void
460: roff_free(struct roff *r)
461: {
462:
463: roff_free1(r);
464: free(r);
465: }
466:
467:
468: struct roff *
1.72 schwarze 469: roff_alloc(struct mparse *parse, int options)
1.1 schwarze 470: {
471: struct roff *r;
472:
1.11 schwarze 473: r = mandoc_calloc(1, sizeof(struct roff));
1.35 schwarze 474: r->parse = parse;
1.72 schwarze 475: r->options = options;
1.2 schwarze 476: r->rstackpos = -1;
1.3 schwarze 477:
1.42 schwarze 478: roffhash_init();
1.37 schwarze 479:
1.1 schwarze 480: return(r);
481: }
482:
1.8 schwarze 483: /*
1.53 schwarze 484: * In the current line, expand user-defined strings ("\*")
485: * and references to number registers ("\n").
486: * Also check the syntax of other escape sequences.
1.8 schwarze 487: */
1.45 schwarze 488: static enum rofferr
1.37 schwarze 489: roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
1.8 schwarze 490: {
1.53 schwarze 491: char ubuf[12]; /* buffer to print the number */
1.23 schwarze 492: const char *stesc; /* start of an escape sequence ('\\') */
493: const char *stnam; /* start of the name, after "[(*" */
494: const char *cp; /* end of the name, e.g. before ']' */
495: const char *res; /* the string to be substituted */
1.53 schwarze 496: char *nbuf; /* new buffer to copy bufp to */
497: size_t nsz; /* size of the new buffer */
498: size_t maxl; /* expected length of the escape name */
499: size_t naml; /* actual length of the escape name */
500: int expand_count; /* to avoid infinite loops */
1.8 schwarze 501:
1.43 schwarze 502: expand_count = 0;
503:
1.42 schwarze 504: again:
1.24 schwarze 505: cp = *bufp + pos;
506: while (NULL != (cp = strchr(cp, '\\'))) {
507: stesc = cp++;
1.23 schwarze 508:
509: /*
1.53 schwarze 510: * The second character must be an asterisk or an n.
1.23 schwarze 511: * If it isn't, skip it anyway: It is escaped,
512: * so it can't start another escape sequence.
513: */
514:
1.24 schwarze 515: if ('\0' == *cp)
1.45 schwarze 516: return(ROFF_CONT);
1.42 schwarze 517:
1.53 schwarze 518: switch (*cp) {
519: case ('*'):
520: res = NULL;
521: break;
522: case ('n'):
523: res = ubuf;
524: break;
525: default:
526: if (ESCAPE_ERROR != mandoc_escape(&cp, NULL, NULL))
1.42 schwarze 527: continue;
528: mandoc_msg
529: (MANDOCERR_BADESCAPE, r->parse,
530: ln, (int)(stesc - *bufp), NULL);
1.45 schwarze 531: return(ROFF_CONT);
1.42 schwarze 532: }
533:
534: cp++;
1.23 schwarze 535:
536: /*
537: * The third character decides the length
1.53 schwarze 538: * of the name of the string or register.
1.23 schwarze 539: * Save a pointer to the name.
540: */
541:
1.24 schwarze 542: switch (*cp) {
543: case ('\0'):
1.45 schwarze 544: return(ROFF_CONT);
1.8 schwarze 545: case ('('):
546: cp++;
547: maxl = 2;
548: break;
549: case ('['):
550: cp++;
551: maxl = 0;
552: break;
553: default:
554: maxl = 1;
555: break;
556: }
1.23 schwarze 557: stnam = cp;
1.8 schwarze 558:
1.23 schwarze 559: /* Advance to the end of the name. */
1.8 schwarze 560:
1.53 schwarze 561: for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
1.42 schwarze 562: if ('\0' == *cp) {
563: mandoc_msg
564: (MANDOCERR_BADESCAPE,
565: r->parse, ln,
566: (int)(stesc - *bufp), NULL);
1.45 schwarze 567: return(ROFF_CONT);
1.42 schwarze 568: }
1.8 schwarze 569: if (0 == maxl && ']' == *cp)
570: break;
571: }
572:
1.23 schwarze 573: /*
574: * Retrieve the replacement string; if it is
575: * undefined, resume searching for escapes.
576: */
577:
1.53 schwarze 578: if (NULL == res)
579: res = roff_getstrn(r, stnam, naml);
580: else
581: snprintf(ubuf, sizeof(ubuf), "%d",
582: roff_getregn(r, stnam, naml));
1.8 schwarze 583:
584: if (NULL == res) {
1.42 schwarze 585: mandoc_msg
586: (MANDOCERR_BADESCAPE, r->parse,
587: ln, (int)(stesc - *bufp), NULL);
1.37 schwarze 588: res = "";
1.8 schwarze 589: }
590:
1.23 schwarze 591: /* Replace the escape sequence by the string. */
592:
1.42 schwarze 593: pos = stesc - *bufp;
594:
1.8 schwarze 595: nsz = *szp + strlen(res) + 1;
1.53 schwarze 596: nbuf = mandoc_malloc(nsz);
1.8 schwarze 597:
1.53 schwarze 598: strlcpy(nbuf, *bufp, (size_t)(stesc - *bufp + 1));
599: strlcat(nbuf, res, nsz);
600: strlcat(nbuf, cp + (maxl ? 0 : 1), nsz);
1.8 schwarze 601:
602: free(*bufp);
603:
1.53 schwarze 604: *bufp = nbuf;
1.8 schwarze 605: *szp = nsz;
1.43 schwarze 606:
607: if (EXPAND_LIMIT >= ++expand_count)
608: goto again;
609:
610: /* Just leave the string unexpanded. */
611: mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
1.45 schwarze 612: return(ROFF_IGN);
1.42 schwarze 613: }
1.45 schwarze 614: return(ROFF_CONT);
1.42 schwarze 615: }
616:
617: /*
1.51 schwarze 618: * Process text streams:
619: * Convert all breakable hyphens into ASCII_HYPH.
620: * Decrement and spring input line trap.
1.42 schwarze 621: */
622: static enum rofferr
1.51 schwarze 623: roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
1.42 schwarze 624: {
625: size_t sz;
626: const char *start;
1.51 schwarze 627: char *p;
628: int isz;
1.42 schwarze 629: enum mandoc_esc esc;
630:
1.51 schwarze 631: start = p = *bufp + pos;
1.42 schwarze 632:
633: while ('\0' != *p) {
634: sz = strcspn(p, "-\\");
635: p += sz;
636:
637: if ('\0' == *p)
638: break;
639:
640: if ('\\' == *p) {
641: /* Skip over escapes. */
642: p++;
1.62 schwarze 643: esc = mandoc_escape((const char **)&p, NULL, NULL);
1.42 schwarze 644: if (ESCAPE_ERROR == esc)
645: break;
646: continue;
647: } else if (p == start) {
648: p++;
649: continue;
650: }
651:
1.44 schwarze 652: if (isalpha((unsigned char)p[-1]) &&
653: isalpha((unsigned char)p[1]))
1.42 schwarze 654: *p = ASCII_HYPH;
655: p++;
1.8 schwarze 656: }
657:
1.51 schwarze 658: /* Spring the input line trap. */
659: if (1 == roffit_lines) {
1.75 schwarze 660: isz = mandoc_asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
1.51 schwarze 661: free(*bufp);
662: *bufp = p;
663: *szp = isz + 1;
664: *offs = 0;
665: free(roffit_macro);
666: roffit_lines = 0;
667: return(ROFF_REPARSE);
668: } else if (1 < roffit_lines)
669: --roffit_lines;
1.42 schwarze 670: return(ROFF_CONT);
1.8 schwarze 671: }
672:
1.1 schwarze 673: enum rofferr
1.6 schwarze 674: roff_parseln(struct roff *r, int ln, char **bufp,
675: size_t *szp, int pos, int *offs)
1.1 schwarze 676: {
677: enum rofft t;
1.27 schwarze 678: enum rofferr e;
1.35 schwarze 679: int ppos, ctl;
1.1 schwarze 680:
1.2 schwarze 681: /*
1.8 schwarze 682: * Run the reserved-word filter only if we have some reserved
683: * words to fill in.
684: */
685:
1.45 schwarze 686: e = roff_res(r, bufp, szp, ln, pos);
687: if (ROFF_IGN == e)
688: return(e);
689: assert(ROFF_CONT == e);
1.8 schwarze 690:
1.35 schwarze 691: ppos = pos;
1.48 schwarze 692: ctl = roff_getcontrol(r, *bufp, &pos);
1.35 schwarze 693:
1.8 schwarze 694: /*
1.2 schwarze 695: * First, if a scope is open and we're not a macro, pass the
696: * text through the macro's filter. If a scope isn't open and
697: * we're not a macro, just let it through.
1.32 schwarze 698: * Finally, if there's an equation scope open, divert it into it
699: * no matter our state.
1.2 schwarze 700: */
701:
1.35 schwarze 702: if (r->last && ! ctl) {
1.2 schwarze 703: t = r->last->tok;
704: assert(roffs[t].text);
1.27 schwarze 705: e = (*roffs[t].text)
706: (r, t, bufp, szp, ln, pos, pos, offs);
707: assert(ROFF_IGN == e || ROFF_CONT == e);
1.32 schwarze 708: if (ROFF_CONT != e)
709: return(e);
1.54 schwarze 710: }
711: if (r->eqn)
712: return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
713: if ( ! ctl) {
1.32 schwarze 714: if (r->tbl)
1.35 schwarze 715: return(tbl_read(r->tbl, ln, *bufp, pos));
1.51 schwarze 716: return(roff_parsetext(bufp, szp, pos, offs));
1.54 schwarze 717: }
1.2 schwarze 718:
719: /*
720: * If a scope is open, go to the child handler for that macro,
721: * as it may want to preprocess before doing anything with it.
1.32 schwarze 722: * Don't do so if an equation is open.
1.2 schwarze 723: */
724:
725: if (r->last) {
1.1 schwarze 726: t = r->last->tok;
727: assert(roffs[t].sub);
1.2 schwarze 728: return((*roffs[t].sub)
1.8 schwarze 729: (r, t, bufp, szp,
1.35 schwarze 730: ln, ppos, pos, offs));
1.2 schwarze 731: }
732:
733: /*
734: * Lastly, as we've no scope open, try to look up and execute
735: * the new macro. If no macro is found, simply return and let
736: * the compilers handle it.
737: */
738:
1.16 schwarze 739: if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
1.1 schwarze 740: return(ROFF_CONT);
741:
1.2 schwarze 742: assert(roffs[t].proc);
743: return((*roffs[t].proc)
1.8 schwarze 744: (r, t, bufp, szp,
745: ln, ppos, pos, offs));
1.2 schwarze 746: }
747:
1.1 schwarze 748:
1.27 schwarze 749: void
1.2 schwarze 750: roff_endparse(struct roff *r)
751: {
1.1 schwarze 752:
1.27 schwarze 753: if (r->last)
1.35 schwarze 754: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.27 schwarze 755: r->last->line, r->last->col, NULL);
756:
1.32 schwarze 757: if (r->eqn) {
1.35 schwarze 758: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.41 schwarze 759: r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
760: eqn_end(&r->eqn);
1.32 schwarze 761: }
762:
1.27 schwarze 763: if (r->tbl) {
1.35 schwarze 764: mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.27 schwarze 765: r->tbl->line, r->tbl->pos, NULL);
1.41 schwarze 766: tbl_end(&r->tbl);
1.27 schwarze 767: }
1.1 schwarze 768: }
769:
770: /*
771: * Parse a roff node's type from the input buffer. This must be in the
772: * form of ".foo xxx" in the usual way.
773: */
774: static enum rofft
1.16 schwarze 775: roff_parse(struct roff *r, const char *buf, int *pos)
1.1 schwarze 776: {
1.16 schwarze 777: const char *mac;
778: size_t maclen;
1.1 schwarze 779: enum rofft t;
780:
1.39 schwarze 781: if ('\0' == buf[*pos] || '"' == buf[*pos] ||
782: '\t' == buf[*pos] || ' ' == buf[*pos])
1.1 schwarze 783: return(ROFF_MAX);
784:
1.68 schwarze 785: /* We stop the macro parse at an escape, tab, space, or nil. */
1.39 schwarze 786:
1.16 schwarze 787: mac = buf + *pos;
1.68 schwarze 788: maclen = strcspn(mac, " \\\t\0");
1.1 schwarze 789:
1.16 schwarze 790: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.42 schwarze 791: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.1 schwarze 792:
1.34 schwarze 793: *pos += (int)maclen;
1.35 schwarze 794:
1.1 schwarze 795: while (buf[*pos] && ' ' == buf[*pos])
796: (*pos)++;
797:
798: return(t);
799: }
800:
801: /* ARGSUSED */
802: static enum rofferr
1.2 schwarze 803: roff_cblock(ROFF_ARGS)
1.1 schwarze 804: {
805:
1.2 schwarze 806: /*
807: * A block-close `..' should only be invoked as a child of an
808: * ignore macro, otherwise raise a warning and just ignore it.
809: */
810:
811: if (NULL == r->last) {
1.35 schwarze 812: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.2 schwarze 813: return(ROFF_IGN);
814: }
1.1 schwarze 815:
1.2 schwarze 816: switch (r->last->tok) {
817: case (ROFF_am):
818: /* FALLTHROUGH */
819: case (ROFF_ami):
820: /* FALLTHROUGH */
821: case (ROFF_am1):
822: /* FALLTHROUGH */
823: case (ROFF_de):
1.23 schwarze 824: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.2 schwarze 825: /* FALLTHROUGH */
826: case (ROFF_dei):
827: /* FALLTHROUGH */
828: case (ROFF_ig):
829: break;
830: default:
1.35 schwarze 831: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.1 schwarze 832: return(ROFF_IGN);
1.2 schwarze 833: }
834:
835: if ((*bufp)[pos])
1.35 schwarze 836: mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.2 schwarze 837:
838: roffnode_pop(r);
839: roffnode_cleanscope(r);
840: return(ROFF_IGN);
841:
842: }
1.1 schwarze 843:
844:
1.2 schwarze 845: static void
846: roffnode_cleanscope(struct roff *r)
847: {
1.1 schwarze 848:
1.2 schwarze 849: while (r->last) {
1.46 schwarze 850: if (--r->last->endspan != 0)
1.2 schwarze 851: break;
852: roffnode_pop(r);
853: }
854: }
1.1 schwarze 855:
856:
1.68 schwarze 857: static void
858: roff_ccond(struct roff *r, int ln, int ppos)
1.2 schwarze 859: {
1.1 schwarze 860:
1.2 schwarze 861: if (NULL == r->last) {
1.35 schwarze 862: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.68 schwarze 863: return;
1.2 schwarze 864: }
1.1 schwarze 865:
1.2 schwarze 866: switch (r->last->tok) {
867: case (ROFF_el):
868: /* FALLTHROUGH */
869: case (ROFF_ie):
870: /* FALLTHROUGH */
871: case (ROFF_if):
872: break;
873: default:
1.35 schwarze 874: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.68 schwarze 875: return;
1.2 schwarze 876: }
1.1 schwarze 877:
1.2 schwarze 878: if (r->last->endspan > -1) {
1.35 schwarze 879: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.68 schwarze 880: return;
1.2 schwarze 881: }
882:
883: roffnode_pop(r);
884: roffnode_cleanscope(r);
1.68 schwarze 885: return;
1.1 schwarze 886: }
887:
888:
889: /* ARGSUSED */
890: static enum rofferr
1.2 schwarze 891: roff_block(ROFF_ARGS)
1.1 schwarze 892: {
1.2 schwarze 893: int sv;
894: size_t sz;
1.16 schwarze 895: char *name;
896:
897: name = NULL;
1.2 schwarze 898:
1.16 schwarze 899: if (ROFF_ig != tok) {
900: if ('\0' == (*bufp)[pos]) {
1.35 schwarze 901: mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.16 schwarze 902: return(ROFF_IGN);
903: }
1.22 schwarze 904:
905: /*
906: * Re-write `de1', since we don't really care about
907: * groff's strange compatibility mode, into `de'.
908: */
909:
1.18 schwarze 910: if (ROFF_de1 == tok)
911: tok = ROFF_de;
1.16 schwarze 912: if (ROFF_de == tok)
913: name = *bufp + pos;
1.21 schwarze 914: else
1.35 schwarze 915: mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
1.21 schwarze 916: roffs[tok].name);
1.22 schwarze 917:
1.33 schwarze 918: while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.2 schwarze 919: pos++;
1.22 schwarze 920:
1.33 schwarze 921: while (isspace((unsigned char)(*bufp)[pos]))
1.16 schwarze 922: (*bufp)[pos++] = '\0';
1.2 schwarze 923: }
924:
1.16 schwarze 925: roffnode_push(r, tok, name, ln, ppos);
926:
927: /*
928: * At the beginning of a `de' macro, clear the existing string
929: * with the same name, if there is one. New content will be
1.66 schwarze 930: * appended from roff_block_text() in multiline mode.
1.16 schwarze 931: */
1.22 schwarze 932:
1.16 schwarze 933: if (ROFF_de == tok)
1.19 schwarze 934: roff_setstr(r, name, "", 0);
1.2 schwarze 935:
936: if ('\0' == (*bufp)[pos])
937: return(ROFF_IGN);
1.1 schwarze 938:
1.22 schwarze 939: /* If present, process the custom end-of-line marker. */
940:
1.2 schwarze 941: sv = pos;
1.33 schwarze 942: while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.2 schwarze 943: pos++;
944:
945: /*
946: * Note: groff does NOT like escape characters in the input.
947: * Instead of detecting this, we're just going to let it fly and
948: * to hell with it.
949: */
950:
951: assert(pos > sv);
952: sz = (size_t)(pos - sv);
953:
954: if (1 == sz && '.' == (*bufp)[sv])
955: return(ROFF_IGN);
956:
1.11 schwarze 957: r->last->end = mandoc_malloc(sz + 1);
1.2 schwarze 958:
959: memcpy(r->last->end, *bufp + sv, sz);
960: r->last->end[(int)sz] = '\0';
961:
962: if ((*bufp)[pos])
1.35 schwarze 963: mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.1 schwarze 964:
965: return(ROFF_IGN);
966: }
967:
968:
969: /* ARGSUSED */
970: static enum rofferr
1.2 schwarze 971: roff_block_sub(ROFF_ARGS)
1.1 schwarze 972: {
1.2 schwarze 973: enum rofft t;
974: int i, j;
975:
976: /*
977: * First check whether a custom macro exists at this level. If
978: * it does, then check against it. This is some of groff's
979: * stranger behaviours. If we encountered a custom end-scope
980: * tag and that tag also happens to be a "real" macro, then we
981: * need to try interpreting it again as a real macro. If it's
982: * not, then return ignore. Else continue.
983: */
984:
985: if (r->last->end) {
1.35 schwarze 986: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.2 schwarze 987: if ((*bufp)[i] != r->last->end[j])
988: break;
1.1 schwarze 989:
1.2 schwarze 990: if ('\0' == r->last->end[j] &&
991: ('\0' == (*bufp)[i] ||
992: ' ' == (*bufp)[i] ||
993: '\t' == (*bufp)[i])) {
994: roffnode_pop(r);
995: roffnode_cleanscope(r);
1.1 schwarze 996:
1.35 schwarze 997: while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
998: i++;
999:
1000: pos = i;
1.16 schwarze 1001: if (ROFF_MAX != roff_parse(r, *bufp, &pos))
1.2 schwarze 1002: return(ROFF_RERUN);
1003: return(ROFF_IGN);
1004: }
1.1 schwarze 1005: }
1006:
1.2 schwarze 1007: /*
1008: * If we have no custom end-query or lookup failed, then try
1009: * pulling it out of the hashtable.
1010: */
1.1 schwarze 1011:
1.36 schwarze 1012: t = roff_parse(r, *bufp, &pos);
1.1 schwarze 1013:
1.16 schwarze 1014: /*
1015: * Macros other than block-end are only significant
1016: * in `de' blocks; elsewhere, simply throw them away.
1017: */
1018: if (ROFF_cblock != t) {
1019: if (ROFF_de == tok)
1.66 schwarze 1020: roff_setstr(r, r->last->name, *bufp + ppos, 2);
1.1 schwarze 1021: return(ROFF_IGN);
1.16 schwarze 1022: }
1.1 schwarze 1023:
1.2 schwarze 1024: assert(roffs[t].proc);
1.6 schwarze 1025: return((*roffs[t].proc)(r, t, bufp, szp,
1026: ln, ppos, pos, offs));
1.2 schwarze 1027: }
1028:
1029:
1030: /* ARGSUSED */
1031: static enum rofferr
1032: roff_block_text(ROFF_ARGS)
1033: {
1034:
1.16 schwarze 1035: if (ROFF_de == tok)
1.66 schwarze 1036: roff_setstr(r, r->last->name, *bufp + pos, 2);
1.16 schwarze 1037:
1.2 schwarze 1038: return(ROFF_IGN);
1039: }
1040:
1041:
1042: /* ARGSUSED */
1043: static enum rofferr
1044: roff_cond_sub(ROFF_ARGS)
1045: {
1046: enum rofft t;
1.37 schwarze 1047: char *ep;
1.71 schwarze 1048: int rr;
1.2 schwarze 1049:
1050: rr = r->last->rule;
1.37 schwarze 1051: roffnode_cleanscope(r);
1.50 schwarze 1052: t = roff_parse(r, *bufp, &pos);
1.2 schwarze 1053:
1.37 schwarze 1054: /*
1.50 schwarze 1055: * Fully handle known macros when they are structurally
1056: * required or when the conditional evaluated to true.
1.5 schwarze 1057: */
1058:
1.50 schwarze 1059: if ((ROFF_MAX != t) &&
1.71 schwarze 1060: (rr || ROFFMAC_STRUCT & roffs[t].flags)) {
1.50 schwarze 1061: assert(roffs[t].proc);
1062: return((*roffs[t].proc)(r, t, bufp, szp,
1063: ln, ppos, pos, offs));
1064: }
1.39 schwarze 1065:
1.69 schwarze 1066: /*
1067: * If `\}' occurs on a macro line without a preceding macro,
1068: * drop the line completely.
1069: */
1070:
1071: ep = *bufp + pos;
1072: if ('\\' == ep[0] && '}' == ep[1])
1.71 schwarze 1073: rr = 0;
1.69 schwarze 1074:
1.50 schwarze 1075: /* Always check for the closing delimiter `\}'. */
1.39 schwarze 1076:
1.50 schwarze 1077: while (NULL != (ep = strchr(ep, '\\'))) {
1.70 schwarze 1078: if ('}' == *(++ep)) {
1079: *ep = '&';
1080: roff_ccond(r, ln, ep - *bufp - 1);
1081: }
1082: ++ep;
1.50 schwarze 1083: }
1.71 schwarze 1084: return(rr ? ROFF_CONT : ROFF_IGN);
1.2 schwarze 1085: }
1086:
1087: /* ARGSUSED */
1088: static enum rofferr
1089: roff_cond_text(ROFF_ARGS)
1090: {
1.37 schwarze 1091: char *ep;
1.71 schwarze 1092: int rr;
1.2 schwarze 1093:
1094: rr = r->last->rule;
1.37 schwarze 1095: roffnode_cleanscope(r);
1.1 schwarze 1096:
1.70 schwarze 1097: ep = *bufp + pos;
1098: while (NULL != (ep = strchr(ep, '\\'))) {
1099: if ('}' == *(++ep)) {
1100: *ep = '&';
1101: roff_ccond(r, ln, ep - *bufp - 1);
1102: }
1103: ++ep;
1.2 schwarze 1104: }
1.71 schwarze 1105: return(rr ? ROFF_CONT : ROFF_IGN);
1.2 schwarze 1106: }
1107:
1.77 ! schwarze 1108: /*
! 1109: * Parse a single signed integer number. Stop at the first non-digit.
! 1110: * If there is at least one digit, return success and advance the
! 1111: * parse point, else return failure and let the parse point unchanged.
! 1112: * Ignore overflows, treat them just like the C language.
! 1113: */
1.56 schwarze 1114: static int
1115: roff_getnum(const char *v, int *pos, int *res)
1116: {
1117: int p, n;
1118:
1119: p = *pos;
1120: n = v[p] == '-';
1121: if (n)
1122: p++;
1123:
1124: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.77 ! schwarze 1125: *res = 10 * *res + v[p] - '0';
1.56 schwarze 1126: if (p == *pos + n)
1127: return 0;
1128:
1129: if (n)
1130: *res = -*res;
1131:
1132: *pos = p;
1133: return 1;
1134: }
1135:
1.71 schwarze 1136: /*
1137: * Evaluate a string comparison condition.
1138: * The first character is the delimiter.
1139: * Succeed if the string up to its second occurrence
1140: * matches the string up to its third occurence.
1141: * Advance the cursor after the third occurrence
1142: * or lacking that, to the end of the line.
1143: */
1144: static int
1145: roff_evalstrcond(const char *v, int *pos)
1146: {
1147: const char *s1, *s2, *s3;
1148: int match;
1149:
1150: match = 0;
1151: s1 = v + *pos; /* initial delimiter */
1152: s2 = s1 + 1; /* for scanning the first string */
1153: s3 = strchr(s2, *s1); /* for scanning the second string */
1154:
1155: if (NULL == s3) /* found no middle delimiter */
1156: goto out;
1157:
1158: while ('\0' != *++s3) {
1159: if (*s2 != *s3) { /* mismatch */
1160: s3 = strchr(s3, *s1);
1161: break;
1162: }
1163: if (*s3 == *s1) { /* found the final delimiter */
1164: match = 1;
1165: break;
1166: }
1167: s2++;
1168: }
1169:
1170: out:
1171: if (NULL == s3)
1172: s3 = strchr(s2, '\0');
1173: else
1174: s3++;
1175: *pos = s3 - v;
1176: return(match);
1177: }
1178:
1.77 ! schwarze 1179: /*
! 1180: * Evaluate an optionally negated single character, numerical,
! 1181: * or string condition.
! 1182: */
1.71 schwarze 1183: static int
1.5 schwarze 1184: roff_evalcond(const char *v, int *pos)
1185: {
1.77 ! schwarze 1186: int wanttrue, number;
1.5 schwarze 1187:
1.71 schwarze 1188: if ('!' == v[*pos]) {
1189: wanttrue = 0;
1190: (*pos)++;
1191: } else
1192: wanttrue = 1;
1193:
1.5 schwarze 1194: switch (v[*pos]) {
1195: case ('n'):
1.71 schwarze 1196: /* FALLTHROUGH */
1197: case ('o'):
1.5 schwarze 1198: (*pos)++;
1.71 schwarze 1199: return(wanttrue);
1200: case ('c'):
1201: /* FALLTHROUGH */
1202: case ('d'):
1203: /* FALLTHROUGH */
1.5 schwarze 1204: case ('e'):
1205: /* FALLTHROUGH */
1.71 schwarze 1206: case ('r'):
1.5 schwarze 1207: /* FALLTHROUGH */
1208: case ('t'):
1209: (*pos)++;
1.71 schwarze 1210: return(!wanttrue);
1.5 schwarze 1211: default:
1212: break;
1213: }
1214:
1.77 ! schwarze 1215: if (roff_evalnum(v, pos, &number, 0))
! 1216: return((number > 0) == wanttrue);
! 1217: else
1.71 schwarze 1218: return(roff_evalstrcond(v, pos) == wanttrue);
1.5 schwarze 1219: }
1220:
1.2 schwarze 1221: /* ARGSUSED */
1222: static enum rofferr
1.21 schwarze 1223: roff_line_ignore(ROFF_ARGS)
1.6 schwarze 1224: {
1.30 schwarze 1225:
1.21 schwarze 1226: return(ROFF_IGN);
1227: }
1228:
1229: /* ARGSUSED */
1230: static enum rofferr
1.2 schwarze 1231: roff_cond(ROFF_ARGS)
1232: {
1.46 schwarze 1233:
1234: roffnode_push(r, tok, NULL, ln, ppos);
1.2 schwarze 1235:
1.35 schwarze 1236: /*
1237: * An `.el' has no conditional body: it will consume the value
1238: * of the current rstack entry set in prior `ie' calls or
1239: * defaults to DENY.
1240: *
1241: * If we're not an `el', however, then evaluate the conditional.
1242: */
1.1 schwarze 1243:
1.46 schwarze 1244: r->last->rule = ROFF_el == tok ?
1.71 schwarze 1245: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.35 schwarze 1246: roff_evalcond(*bufp, &pos);
1.2 schwarze 1247:
1.35 schwarze 1248: /*
1249: * An if-else will put the NEGATION of the current evaluated
1250: * conditional into the stack of rules.
1251: */
1252:
1.2 schwarze 1253: if (ROFF_ie == tok) {
1.35 schwarze 1254: if (r->rstackpos == RSTACK_MAX - 1) {
1255: mandoc_msg(MANDOCERR_MEM,
1256: r->parse, ln, ppos, NULL);
1257: return(ROFF_ERR);
1258: }
1.71 schwarze 1259: r->rstack[++r->rstackpos] = !r->last->rule;
1.2 schwarze 1260: }
1.5 schwarze 1261:
1262: /* If the parent has false as its rule, then so do we. */
1263:
1.71 schwarze 1264: if (r->last->parent && !r->last->parent->rule)
1265: r->last->rule = 0;
1.5 schwarze 1266:
1267: /*
1.46 schwarze 1268: * Determine scope.
1269: * If there is nothing on the line after the conditional,
1270: * not even whitespace, use next-line scope.
1.5 schwarze 1271: */
1.2 schwarze 1272:
1.46 schwarze 1273: if ('\0' == (*bufp)[pos]) {
1274: r->last->endspan = 2;
1275: goto out;
1276: }
1277:
1278: while (' ' == (*bufp)[pos])
1279: pos++;
1280:
1281: /* An opening brace requests multiline scope. */
1.2 schwarze 1282:
1283: if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1284: r->last->endspan = -1;
1285: pos += 2;
1.46 schwarze 1286: goto out;
1.2 schwarze 1287: }
1288:
1289: /*
1.46 schwarze 1290: * Anything else following the conditional causes
1291: * single-line scope. Warn if the scope contains
1292: * nothing but trailing whitespace.
1.2 schwarze 1293: */
1294:
1295: if ('\0' == (*bufp)[pos])
1.46 schwarze 1296: mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.2 schwarze 1297:
1.46 schwarze 1298: r->last->endspan = 1;
1.1 schwarze 1299:
1.46 schwarze 1300: out:
1.2 schwarze 1301: *offs = pos;
1302: return(ROFF_RERUN);
1.1 schwarze 1303: }
1304:
1305:
1.2 schwarze 1306: /* ARGSUSED */
1307: static enum rofferr
1.7 schwarze 1308: roff_ds(ROFF_ARGS)
1309: {
1.10 schwarze 1310: char *name, *string;
1311:
1312: /*
1313: * A symbol is named by the first word following the macro
1314: * invocation up to a space. Its value is anything after the
1315: * name's trailing whitespace and optional double-quote. Thus,
1316: *
1317: * [.ds foo "bar " ]
1318: *
1319: * will have `bar " ' as its value.
1320: */
1.7 schwarze 1321:
1.28 schwarze 1322: string = *bufp + pos;
1323: name = roff_getname(r, &string, ln, pos);
1.7 schwarze 1324: if ('\0' == *name)
1325: return(ROFF_IGN);
1326:
1.28 schwarze 1327: /* Read past initial double-quote. */
1328: if ('"' == *string)
1.7 schwarze 1329: string++;
1330:
1.10 schwarze 1331: /* The rest is the value. */
1.66 schwarze 1332: roff_setstr(r, name, string, ROFF_as == tok);
1.7 schwarze 1333: return(ROFF_IGN);
1334: }
1335:
1.77 ! schwarze 1336: /*
! 1337: * Parse a single operator, one or two characters long.
! 1338: * If the operator is recognized, return success and advance the
! 1339: * parse point, else return failure and let the parse point unchanged.
! 1340: */
! 1341: static int
! 1342: roff_getop(const char *v, int *pos, char *res)
! 1343: {
! 1344:
! 1345: *res = v[*pos];
! 1346:
! 1347: switch (*res) {
! 1348: case ('+'):
! 1349: /* FALLTHROUGH */
! 1350: case ('-'):
! 1351: /* FALLTHROUGH */
! 1352: case ('*'):
! 1353: /* FALLTHROUGH */
! 1354: case ('/'):
! 1355: /* FALLTHROUGH */
! 1356: case ('%'):
! 1357: /* FALLTHROUGH */
! 1358: case ('&'):
! 1359: /* FALLTHROUGH */
! 1360: case (':'):
! 1361: break;
! 1362: case '<':
! 1363: switch (v[*pos + 1]) {
! 1364: case ('='):
! 1365: *res = 'l';
! 1366: (*pos)++;
! 1367: break;
! 1368: case ('>'):
! 1369: *res = '!';
! 1370: (*pos)++;
! 1371: break;
! 1372: case ('?'):
! 1373: *res = 'i';
! 1374: (*pos)++;
! 1375: break;
! 1376: default:
! 1377: break;
! 1378: }
! 1379: break;
! 1380: case '>':
! 1381: switch (v[*pos + 1]) {
! 1382: case ('='):
! 1383: *res = 'g';
! 1384: (*pos)++;
! 1385: break;
! 1386: case ('?'):
! 1387: *res = 'a';
! 1388: (*pos)++;
! 1389: break;
! 1390: default:
! 1391: break;
! 1392: }
! 1393: break;
! 1394: case '=':
! 1395: if ('=' == v[*pos + 1])
! 1396: (*pos)++;
! 1397: break;
! 1398: default:
! 1399: return(0);
! 1400: }
! 1401: (*pos)++;
! 1402:
! 1403: return(*res);
! 1404: }
! 1405:
! 1406: /*
! 1407: * Evaluate either a parenthesized numeric expression
! 1408: * or a single signed integer number.
! 1409: */
! 1410: static int
! 1411: roff_evalpar(const char *v, int *pos, int *res)
! 1412: {
! 1413:
! 1414: if ('(' != v[*pos])
! 1415: return(roff_getnum(v, pos, res));
! 1416:
! 1417: (*pos)++;
! 1418: if ( ! roff_evalnum(v, pos, res, 1))
! 1419: return(0);
! 1420:
! 1421: /* If the trailing parenthesis is missing, ignore the error. */
! 1422: if (')' == v[*pos])
! 1423: (*pos)++;
! 1424:
! 1425: return(1);
! 1426: }
! 1427:
! 1428: /*
! 1429: * Evaluate a complete numeric expression.
! 1430: * Proceed left to right, there is no concept of precedence.
! 1431: */
! 1432: static int
! 1433: roff_evalnum(const char *v, int *pos, int *res, int skipwhite)
! 1434: {
! 1435: int mypos, operand2;
! 1436: char operator;
! 1437:
! 1438: if (NULL == pos) {
! 1439: mypos = 0;
! 1440: pos = &mypos;
! 1441: }
! 1442:
! 1443: if (skipwhite)
! 1444: while (isspace((unsigned char)v[*pos]))
! 1445: (*pos)++;
! 1446:
! 1447: if ( ! roff_evalpar(v, pos, res))
! 1448: return(0);
! 1449:
! 1450: while (1) {
! 1451: if (skipwhite)
! 1452: while (isspace((unsigned char)v[*pos]))
! 1453: (*pos)++;
! 1454:
! 1455: if ( ! roff_getop(v, pos, &operator))
! 1456: break;
! 1457:
! 1458: if (skipwhite)
! 1459: while (isspace((unsigned char)v[*pos]))
! 1460: (*pos)++;
! 1461:
! 1462: if ( ! roff_evalpar(v, pos, &operand2))
! 1463: return(0);
! 1464:
! 1465: if (skipwhite)
! 1466: while (isspace((unsigned char)v[*pos]))
! 1467: (*pos)++;
! 1468:
! 1469: switch (operator) {
! 1470: case ('+'):
! 1471: *res += operand2;
! 1472: break;
! 1473: case ('-'):
! 1474: *res -= operand2;
! 1475: break;
! 1476: case ('*'):
! 1477: *res *= operand2;
! 1478: break;
! 1479: case ('/'):
! 1480: *res /= operand2;
! 1481: break;
! 1482: case ('%'):
! 1483: *res %= operand2;
! 1484: break;
! 1485: case ('<'):
! 1486: *res = *res < operand2;
! 1487: break;
! 1488: case ('>'):
! 1489: *res = *res > operand2;
! 1490: break;
! 1491: case ('l'):
! 1492: *res = *res <= operand2;
! 1493: break;
! 1494: case ('g'):
! 1495: *res = *res >= operand2;
! 1496: break;
! 1497: case ('='):
! 1498: *res = *res == operand2;
! 1499: break;
! 1500: case ('!'):
! 1501: *res = *res != operand2;
! 1502: break;
! 1503: case ('&'):
! 1504: *res = *res && operand2;
! 1505: break;
! 1506: case (':'):
! 1507: *res = *res || operand2;
! 1508: break;
! 1509: case ('i'):
! 1510: if (operand2 < *res)
! 1511: *res = operand2;
! 1512: break;
! 1513: case ('a'):
! 1514: if (operand2 > *res)
! 1515: *res = operand2;
! 1516: break;
! 1517: default:
! 1518: abort();
! 1519: }
! 1520: }
! 1521: return(1);
! 1522: }
! 1523:
1.52 schwarze 1524: void
1.60 schwarze 1525: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.41 schwarze 1526: {
1.52 schwarze 1527: struct roffreg *reg;
1528:
1529: /* Search for an existing register with the same name. */
1530: reg = r->regtab;
1531:
1532: while (reg && strcmp(name, reg->key.p))
1533: reg = reg->next;
1.41 schwarze 1534:
1.52 schwarze 1535: if (NULL == reg) {
1536: /* Create a new register. */
1537: reg = mandoc_malloc(sizeof(struct roffreg));
1538: reg->key.p = mandoc_strdup(name);
1539: reg->key.sz = strlen(name);
1.60 schwarze 1540: reg->val = 0;
1.52 schwarze 1541: reg->next = r->regtab;
1542: r->regtab = reg;
1543: }
1544:
1.60 schwarze 1545: if ('+' == sign)
1546: reg->val += val;
1547: else if ('-' == sign)
1548: reg->val -= val;
1549: else
1550: reg->val = val;
1.41 schwarze 1551: }
1552:
1.65 schwarze 1553: /*
1554: * Handle some predefined read-only number registers.
1555: * For now, return -1 if the requested register is not predefined;
1556: * in case a predefined read-only register having the value -1
1557: * were to turn up, another special value would have to be chosen.
1558: */
1559: static int
1560: roff_getregro(const char *name)
1561: {
1562:
1563: switch (*name) {
1564: case ('A'): /* ASCII approximation mode is always off. */
1565: return(0);
1566: case ('g'): /* Groff compatibility mode is always on. */
1567: return(1);
1568: case ('H'): /* Fixed horizontal resolution. */
1569: return (24);
1570: case ('j'): /* Always adjust left margin only. */
1571: return(0);
1572: case ('T'): /* Some output device is always defined. */
1573: return(1);
1574: case ('V'): /* Fixed vertical resolution. */
1575: return (40);
1576: default:
1577: return (-1);
1578: }
1579: }
1580:
1.53 schwarze 1581: int
1.52 schwarze 1582: roff_getreg(const struct roff *r, const char *name)
1.41 schwarze 1583: {
1.52 schwarze 1584: struct roffreg *reg;
1.65 schwarze 1585: int val;
1586:
1587: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
1588: val = roff_getregro(name + 1);
1589: if (-1 != val)
1590: return (val);
1591: }
1.52 schwarze 1592:
1593: for (reg = r->regtab; reg; reg = reg->next)
1594: if (0 == strcmp(name, reg->key.p))
1.53 schwarze 1595: return(reg->val);
1596:
1597: return(0);
1598: }
1599:
1600: static int
1601: roff_getregn(const struct roff *r, const char *name, size_t len)
1602: {
1603: struct roffreg *reg;
1.65 schwarze 1604: int val;
1605:
1606: if ('.' == name[0] && 2 == len) {
1607: val = roff_getregro(name + 1);
1608: if (-1 != val)
1609: return (val);
1610: }
1.53 schwarze 1611:
1612: for (reg = r->regtab; reg; reg = reg->next)
1613: if (len == reg->key.sz &&
1614: 0 == strncmp(name, reg->key.p, len))
1615: return(reg->val);
1.41 schwarze 1616:
1.52 schwarze 1617: return(0);
1.41 schwarze 1618: }
1619:
1.52 schwarze 1620: static void
1621: roff_freereg(struct roffreg *reg)
1.41 schwarze 1622: {
1.52 schwarze 1623: struct roffreg *old_reg;
1.41 schwarze 1624:
1.52 schwarze 1625: while (NULL != reg) {
1626: free(reg->key.p);
1627: old_reg = reg;
1628: reg = reg->next;
1629: free(old_reg);
1630: }
1.41 schwarze 1631: }
1.7 schwarze 1632:
1633: static enum rofferr
1.6 schwarze 1634: roff_nr(ROFF_ARGS)
1.1 schwarze 1635: {
1.28 schwarze 1636: const char *key;
1637: char *val;
1.37 schwarze 1638: int iv;
1.60 schwarze 1639: char sign;
1.6 schwarze 1640:
1.28 schwarze 1641: val = *bufp + pos;
1642: key = roff_getname(r, &val, ln, pos);
1.6 schwarze 1643:
1.60 schwarze 1644: sign = *val;
1645: if ('+' == sign || '-' == sign)
1646: val++;
1647:
1.77 ! schwarze 1648: if (roff_evalnum(val, NULL, &iv, 0))
! 1649: roff_setreg(r, key, iv, sign);
1.1 schwarze 1650:
1.76 schwarze 1651: return(ROFF_IGN);
1652: }
1653:
1654: static enum rofferr
1655: roff_rr(ROFF_ARGS)
1656: {
1657: struct roffreg *reg, **prev;
1658: const char *name;
1659: char *cp;
1660:
1661: cp = *bufp + pos;
1662: name = roff_getname(r, &cp, ln, pos);
1663:
1664: prev = &r->regtab;
1665: while (1) {
1666: reg = *prev;
1667: if (NULL == reg || !strcmp(name, reg->key.p))
1668: break;
1669: prev = ®->next;
1670: }
1671: if (NULL != reg) {
1672: *prev = reg->next;
1673: free(reg->key.p);
1674: free(reg);
1675: }
1.29 schwarze 1676: return(ROFF_IGN);
1677: }
1678:
1679: /* ARGSUSED */
1680: static enum rofferr
1681: roff_rm(ROFF_ARGS)
1682: {
1683: const char *name;
1684: char *cp;
1685:
1686: cp = *bufp + pos;
1687: while ('\0' != *cp) {
1.34 schwarze 1688: name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1.29 schwarze 1689: if ('\0' != *name)
1690: roff_setstr(r, name, NULL, 0);
1691: }
1.51 schwarze 1692: return(ROFF_IGN);
1693: }
1694:
1695: /* ARGSUSED */
1696: static enum rofferr
1697: roff_it(ROFF_ARGS)
1698: {
1699: char *cp;
1700: size_t len;
1701: int iv;
1702:
1703: /* Parse the number of lines. */
1704: cp = *bufp + pos;
1705: len = strcspn(cp, " \t");
1706: cp[len] = '\0';
1707: if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1708: mandoc_msg(MANDOCERR_NUMERIC, r->parse,
1709: ln, ppos, *bufp + 1);
1710: return(ROFF_IGN);
1711: }
1712: cp += len + 1;
1713:
1714: /* Arm the input line trap. */
1715: roffit_lines = iv;
1716: roffit_macro = mandoc_strdup(cp);
1.2 schwarze 1717: return(ROFF_IGN);
1.47 schwarze 1718: }
1719:
1720: /* ARGSUSED */
1721: static enum rofferr
1722: roff_Dd(ROFF_ARGS)
1723: {
1724: const char *const *cp;
1725:
1.72 schwarze 1726: if (0 == ((MPARSE_MDOC | MPARSE_QUICK) & r->options))
1.47 schwarze 1727: for (cp = __mdoc_reserved; *cp; cp++)
1728: roff_setstr(r, *cp, NULL, 0);
1729:
1730: return(ROFF_CONT);
1731: }
1732:
1733: /* ARGSUSED */
1734: static enum rofferr
1735: roff_TH(ROFF_ARGS)
1736: {
1737: const char *const *cp;
1738:
1.72 schwarze 1739: if (0 == (MPARSE_QUICK & r->options))
1.47 schwarze 1740: for (cp = __man_reserved; *cp; cp++)
1741: roff_setstr(r, *cp, NULL, 0);
1742:
1743: return(ROFF_CONT);
1.14 schwarze 1744: }
1745:
1746: /* ARGSUSED */
1747: static enum rofferr
1.27 schwarze 1748: roff_TE(ROFF_ARGS)
1749: {
1750:
1751: if (NULL == r->tbl)
1.35 schwarze 1752: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.27 schwarze 1753: else
1.41 schwarze 1754: tbl_end(&r->tbl);
1.27 schwarze 1755:
1756: return(ROFF_IGN);
1757: }
1758:
1759: /* ARGSUSED */
1760: static enum rofferr
1761: roff_T_(ROFF_ARGS)
1762: {
1763:
1764: if (NULL == r->tbl)
1.35 schwarze 1765: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.27 schwarze 1766: else
1767: tbl_restart(ppos, ln, r->tbl);
1768:
1769: return(ROFF_IGN);
1770: }
1771:
1.41 schwarze 1772: #if 0
1773: static int
1774: roff_closeeqn(struct roff *r)
1775: {
1776:
1777: return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1778: }
1779: #endif
1780:
1781: static void
1782: roff_openeqn(struct roff *r, const char *name, int line,
1783: int offs, const char *buf)
1.32 schwarze 1784: {
1.41 schwarze 1785: struct eqn_node *e;
1786: int poff;
1.32 schwarze 1787:
1788: assert(NULL == r->eqn);
1.41 schwarze 1789: e = eqn_alloc(name, offs, line, r->parse);
1.32 schwarze 1790:
1791: if (r->last_eqn)
1792: r->last_eqn->next = e;
1793: else
1794: r->first_eqn = r->last_eqn = e;
1795:
1796: r->eqn = r->last_eqn = e;
1.41 schwarze 1797:
1798: if (buf) {
1799: poff = 0;
1800: eqn_read(&r->eqn, line, buf, offs, &poff);
1801: }
1802: }
1803:
1804: /* ARGSUSED */
1805: static enum rofferr
1806: roff_EQ(ROFF_ARGS)
1807: {
1808:
1809: roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.32 schwarze 1810: return(ROFF_IGN);
1811: }
1812:
1813: /* ARGSUSED */
1814: static enum rofferr
1815: roff_EN(ROFF_ARGS)
1816: {
1817:
1.35 schwarze 1818: mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.32 schwarze 1819: return(ROFF_IGN);
1820: }
1821:
1822: /* ARGSUSED */
1823: static enum rofferr
1.27 schwarze 1824: roff_TS(ROFF_ARGS)
1825: {
1.49 schwarze 1826: struct tbl_node *tbl;
1.27 schwarze 1827:
1828: if (r->tbl) {
1.35 schwarze 1829: mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.41 schwarze 1830: tbl_end(&r->tbl);
1.27 schwarze 1831: }
1832:
1.49 schwarze 1833: tbl = tbl_alloc(ppos, ln, r->parse);
1.27 schwarze 1834:
1835: if (r->last_tbl)
1.49 schwarze 1836: r->last_tbl->next = tbl;
1.27 schwarze 1837: else
1.49 schwarze 1838: r->first_tbl = r->last_tbl = tbl;
1.27 schwarze 1839:
1.49 schwarze 1840: r->tbl = r->last_tbl = tbl;
1.27 schwarze 1841: return(ROFF_IGN);
1842: }
1843:
1844: /* ARGSUSED */
1845: static enum rofferr
1.48 schwarze 1846: roff_cc(ROFF_ARGS)
1847: {
1848: const char *p;
1849:
1850: p = *bufp + pos;
1851:
1852: if ('\0' == *p || '.' == (r->control = *p++))
1853: r->control = 0;
1854:
1855: if ('\0' != *p)
1856: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1857:
1858: return(ROFF_IGN);
1859: }
1860:
1861: /* ARGSUSED */
1862: static enum rofferr
1.42 schwarze 1863: roff_tr(ROFF_ARGS)
1864: {
1865: const char *p, *first, *second;
1866: size_t fsz, ssz;
1867: enum mandoc_esc esc;
1868:
1869: p = *bufp + pos;
1870:
1871: if ('\0' == *p) {
1872: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1873: return(ROFF_IGN);
1874: }
1875:
1876: while ('\0' != *p) {
1877: fsz = ssz = 1;
1878:
1879: first = p++;
1880: if ('\\' == *first) {
1881: esc = mandoc_escape(&p, NULL, NULL);
1882: if (ESCAPE_ERROR == esc) {
1883: mandoc_msg
1884: (MANDOCERR_BADESCAPE, r->parse,
1885: ln, (int)(p - *bufp), NULL);
1886: return(ROFF_IGN);
1887: }
1888: fsz = (size_t)(p - first);
1889: }
1890:
1891: second = p++;
1892: if ('\\' == *second) {
1893: esc = mandoc_escape(&p, NULL, NULL);
1894: if (ESCAPE_ERROR == esc) {
1895: mandoc_msg
1896: (MANDOCERR_BADESCAPE, r->parse,
1897: ln, (int)(p - *bufp), NULL);
1898: return(ROFF_IGN);
1899: }
1900: ssz = (size_t)(p - second);
1901: } else if ('\0' == *second) {
1902: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1903: ln, (int)(p - *bufp), NULL);
1904: second = " ";
1905: p--;
1906: }
1907:
1908: if (fsz > 1) {
1909: roff_setstrn(&r->xmbtab, first,
1910: fsz, second, ssz, 0);
1911: continue;
1912: }
1913:
1914: if (NULL == r->xtab)
1915: r->xtab = mandoc_calloc
1916: (128, sizeof(struct roffstr));
1917:
1918: free(r->xtab[(int)*first].p);
1919: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
1920: r->xtab[(int)*first].sz = ssz;
1921: }
1922:
1923: return(ROFF_IGN);
1924: }
1925:
1926: /* ARGSUSED */
1927: static enum rofferr
1.14 schwarze 1928: roff_so(ROFF_ARGS)
1929: {
1930: char *name;
1.15 schwarze 1931:
1.35 schwarze 1932: mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1.14 schwarze 1933:
1.22 schwarze 1934: /*
1935: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1936: * opening anything that's not in our cwd or anything beneath
1937: * it. Thus, explicitly disallow traversing up the file-system
1938: * or using absolute paths.
1939: */
1940:
1.14 schwarze 1941: name = *bufp + pos;
1942: if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.35 schwarze 1943: mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1.14 schwarze 1944: return(ROFF_ERR);
1945: }
1946:
1947: *offs = pos;
1948: return(ROFF_SO);
1.7 schwarze 1949: }
1950:
1.16 schwarze 1951: /* ARGSUSED */
1952: static enum rofferr
1953: roff_userdef(ROFF_ARGS)
1.12 schwarze 1954: {
1.16 schwarze 1955: const char *arg[9];
1956: char *cp, *n1, *n2;
1.25 schwarze 1957: int i;
1.12 schwarze 1958:
1.16 schwarze 1959: /*
1960: * Collect pointers to macro argument strings
1.61 schwarze 1961: * and NUL-terminate them.
1.16 schwarze 1962: */
1963: cp = *bufp + pos;
1.25 schwarze 1964: for (i = 0; i < 9; i++)
1.26 schwarze 1965: arg[i] = '\0' == *cp ? "" :
1.35 schwarze 1966: mandoc_getarg(r->parse, &cp, ln, &pos);
1.16 schwarze 1967:
1968: /*
1969: * Expand macro arguments.
1.12 schwarze 1970: */
1.16 schwarze 1971: *szp = 0;
1972: n1 = cp = mandoc_strdup(r->current_string);
1973: while (NULL != (cp = strstr(cp, "\\$"))) {
1974: i = cp[2] - '1';
1975: if (0 > i || 8 < i) {
1976: /* Not an argument invocation. */
1977: cp += 2;
1978: continue;
1979: }
1980:
1981: *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1982: n2 = mandoc_malloc(*szp);
1983:
1984: strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1985: strlcat(n2, arg[i], *szp);
1986: strlcat(n2, cp + 3, *szp);
1987:
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.66 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)
2133: if (0 == strncmp(name, n->key.p, len) &&
2134: '\0' == n->key.p[(int)len])
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: {
2161:
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: {
2168:
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: */
2227: res = mandoc_realloc
2228: (res, ssz + cp->val.sz + 1);
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: }
2249: /*
2250: * We bail out on bad escapes.
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: /*
2265: * Find out whether a line is a macro line or not.
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: }