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