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