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