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