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