Annotation of mandoc/roff.c, Revision 1.228
1.228 ! schwarze 1: /* $Id: roff.c,v 1.227 2014/09/06 22:39:36 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.228 ! schwarze 761:
! 762: /* Skip empty request lines. */
! 763:
! 764: if ((*bufp)[pos] == '"') {
! 765: mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
! 766: ln, pos, NULL);
! 767: return(ROFF_IGN);
! 768: } else if ((*bufp)[pos] == '\0')
! 769: return(ROFF_IGN);
1.67 kristaps 770:
1.79 kristaps 771: /*
772: * If a scope is open, go to the child handler for that macro,
773: * as it may want to preprocess before doing anything with it.
1.125 kristaps 774: * Don't do so if an equation is open.
1.79 kristaps 775: */
1.78 kristaps 776:
1.79 kristaps 777: if (r->last) {
778: t = r->last->tok;
779: assert(roffs[t].sub);
1.207 schwarze 780: return((*roffs[t].sub)(r, t, bufp, szp,
781: ln, ppos, pos, offs));
1.79 kristaps 782: }
1.78 kristaps 783:
1.79 kristaps 784: /*
785: * Lastly, as we've no scope open, try to look up and execute
786: * the new macro. If no macro is found, simply return and let
787: * the compilers handle it.
788: */
1.67 kristaps 789:
1.214 schwarze 790: if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos, ln, ppos)))
1.79 kristaps 791: return(ROFF_CONT);
1.67 kristaps 792:
1.75 kristaps 793: assert(roffs[t].proc);
1.207 schwarze 794: return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
1.74 kristaps 795: }
796:
1.117 kristaps 797: void
1.74 kristaps 798: roff_endparse(struct roff *r)
799: {
800:
1.110 kristaps 801: if (r->last)
1.221 schwarze 802: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
803: r->last->line, r->last->col,
804: roffs[r->last->tok].name);
1.117 kristaps 805:
1.125 kristaps 806: if (r->eqn) {
1.221 schwarze 807: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
808: r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151 kristaps 809: eqn_end(&r->eqn);
1.125 kristaps 810: }
811:
1.117 kristaps 812: if (r->tbl) {
1.221 schwarze 813: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
814: r->tbl->line, r->tbl->pos, "TS");
1.151 kristaps 815: tbl_end(&r->tbl);
1.117 kristaps 816: }
1.67 kristaps 817: }
818:
819: /*
820: * Parse a roff node's type from the input buffer. This must be in the
821: * form of ".foo xxx" in the usual way.
822: */
823: static enum rofft
1.214 schwarze 824: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67 kristaps 825: {
1.214 schwarze 826: char *cp;
1.106 kristaps 827: const char *mac;
828: size_t maclen;
1.67 kristaps 829: enum rofft t;
830:
1.214 schwarze 831: cp = buf + *pos;
832:
833: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67 kristaps 834: return(ROFF_MAX);
835:
1.214 schwarze 836: mac = cp;
837: maclen = roff_getname(r, &cp, ln, ppos);
1.67 kristaps 838:
1.106 kristaps 839: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155 kristaps 840: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67 kristaps 841:
1.214 schwarze 842: if (ROFF_MAX != t)
843: *pos = cp - buf;
1.67 kristaps 844:
845: return(t);
846: }
847:
848: static enum rofferr
1.76 kristaps 849: roff_cblock(ROFF_ARGS)
1.67 kristaps 850: {
851:
1.79 kristaps 852: /*
853: * A block-close `..' should only be invoked as a child of an
854: * ignore macro, otherwise raise a warning and just ignore it.
855: */
856:
1.76 kristaps 857: if (NULL == r->last) {
1.221 schwarze 858: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
859: ln, ppos, "..");
1.76 kristaps 860: return(ROFF_IGN);
861: }
1.67 kristaps 862:
1.81 kristaps 863: switch (r->last->tok) {
1.207 schwarze 864: case ROFF_am:
1.220 schwarze 865: /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81 kristaps 866: /* FALLTHROUGH */
1.207 schwarze 867: case ROFF_ami:
1.81 kristaps 868: /* FALLTHROUGH */
1.207 schwarze 869: case ROFF_de:
1.108 schwarze 870: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81 kristaps 871: /* FALLTHROUGH */
1.207 schwarze 872: case ROFF_dei:
1.81 kristaps 873: /* FALLTHROUGH */
1.207 schwarze 874: case ROFF_ig:
1.81 kristaps 875: break;
876: default:
1.221 schwarze 877: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
878: ln, ppos, "..");
1.67 kristaps 879: return(ROFF_IGN);
1.76 kristaps 880: }
1.67 kristaps 881:
1.76 kristaps 882: if ((*bufp)[pos])
1.217 schwarze 883: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
884: ".. %s", *bufp + pos);
1.71 kristaps 885:
886: roffnode_pop(r);
1.76 kristaps 887: roffnode_cleanscope(r);
888: return(ROFF_IGN);
1.71 kristaps 889:
1.67 kristaps 890: }
891:
1.76 kristaps 892: static void
893: roffnode_cleanscope(struct roff *r)
1.67 kristaps 894: {
895:
1.76 kristaps 896: while (r->last) {
1.173 schwarze 897: if (--r->last->endspan != 0)
1.76 kristaps 898: break;
899: roffnode_pop(r);
900: }
1.67 kristaps 901: }
902:
1.195 schwarze 903: static void
904: roff_ccond(struct roff *r, int ln, int ppos)
1.74 kristaps 905: {
906:
1.76 kristaps 907: if (NULL == r->last) {
1.221 schwarze 908: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
909: ln, ppos, "\\}");
1.195 schwarze 910: return;
1.76 kristaps 911: }
912:
1.82 kristaps 913: switch (r->last->tok) {
1.207 schwarze 914: case ROFF_el:
1.82 kristaps 915: /* FALLTHROUGH */
1.207 schwarze 916: case ROFF_ie:
1.82 kristaps 917: /* FALLTHROUGH */
1.207 schwarze 918: case ROFF_if:
1.82 kristaps 919: break;
920: default:
1.221 schwarze 921: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
922: ln, ppos, "\\}");
1.195 schwarze 923: return;
1.75 kristaps 924: }
925:
1.76 kristaps 926: if (r->last->endspan > -1) {
1.221 schwarze 927: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
928: ln, ppos, "\\}");
1.195 schwarze 929: return;
1.76 kristaps 930: }
931:
1.75 kristaps 932: roffnode_pop(r);
1.76 kristaps 933: roffnode_cleanscope(r);
1.195 schwarze 934: return;
1.76 kristaps 935: }
936:
937: static enum rofferr
1.80 kristaps 938: roff_block(ROFF_ARGS)
1.76 kristaps 939: {
1.220 schwarze 940: const char *name;
941: char *iname, *cp;
1.213 schwarze 942: size_t namesz;
1.106 kristaps 943:
1.220 schwarze 944: /* Ignore groff compatibility mode for now. */
1.76 kristaps 945:
1.220 schwarze 946: if (ROFF_de1 == tok)
947: tok = ROFF_de;
948: else if (ROFF_am1 == tok)
949: tok = ROFF_am;
950:
951: /* Parse the macro name argument. */
952:
953: cp = *bufp + pos;
954: if (ROFF_ig == tok) {
955: iname = NULL;
956: namesz = 0;
957: } else {
958: iname = cp;
959: namesz = roff_getname(r, &cp, ln, ppos);
960: iname[namesz] = '\0';
961: }
1.107 kristaps 962:
1.220 schwarze 963: /* Resolve the macro name argument if it is indirect. */
1.107 kristaps 964:
1.220 schwarze 965: if (namesz && (ROFF_dei == tok || ROFF_ami == tok)) {
966: if (NULL == (name = roff_getstrn(r, iname, namesz))) {
967: mandoc_vmsg(MANDOCERR_STR_UNDEF,
968: r->parse, ln, (int)(iname - *bufp),
969: "%.*s", (int)namesz, iname);
970: namesz = 0;
971: } else
972: namesz = strlen(name);
973: } else
974: name = iname;
1.107 kristaps 975:
1.220 schwarze 976: if (0 == namesz && ROFF_ig != tok) {
977: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
978: ln, ppos, roffs[tok].name);
979: return(ROFF_IGN);
980: }
1.80 kristaps 981:
1.106 kristaps 982: roffnode_push(r, tok, name, ln, ppos);
983:
984: /*
985: * At the beginning of a `de' macro, clear the existing string
986: * with the same name, if there is one. New content will be
1.193 schwarze 987: * appended from roff_block_text() in multiline mode.
1.106 kristaps 988: */
1.107 kristaps 989:
1.220 schwarze 990: if (ROFF_de == tok || ROFF_dei == tok)
1.213 schwarze 991: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76 kristaps 992:
1.213 schwarze 993: if ('\0' == *cp)
1.78 kristaps 994: return(ROFF_IGN);
995:
1.220 schwarze 996: /* Get the custom end marker. */
1.107 kristaps 997:
1.220 schwarze 998: iname = cp;
1.213 schwarze 999: namesz = roff_getname(r, &cp, ln, ppos);
1.220 schwarze 1000:
1001: /* Resolve the end marker if it is indirect. */
1002:
1003: if (namesz && (ROFF_dei == tok || ROFF_ami == tok)) {
1004: if (NULL == (name = roff_getstrn(r, iname, namesz))) {
1005: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1006: r->parse, ln, (int)(iname - *bufp),
1007: "%.*s", (int)namesz, iname);
1008: namesz = 0;
1009: } else
1010: namesz = strlen(name);
1011: } else
1012: name = iname;
1013:
1.213 schwarze 1014: if (namesz)
1015: r->last->end = mandoc_strndup(name, namesz);
1.78 kristaps 1016:
1.213 schwarze 1017: if ('\0' != *cp)
1.217 schwarze 1018: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1019: ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74 kristaps 1020:
1.78 kristaps 1021: return(ROFF_IGN);
1022: }
1023:
1024: static enum rofferr
1.80 kristaps 1025: roff_block_sub(ROFF_ARGS)
1.79 kristaps 1026: {
1027: enum rofft t;
1028: int i, j;
1029:
1030: /*
1031: * First check whether a custom macro exists at this level. If
1032: * it does, then check against it. This is some of groff's
1033: * stranger behaviours. If we encountered a custom end-scope
1034: * tag and that tag also happens to be a "real" macro, then we
1035: * need to try interpreting it again as a real macro. If it's
1036: * not, then return ignore. Else continue.
1037: */
1038:
1039: if (r->last->end) {
1.130 kristaps 1040: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.79 kristaps 1041: if ((*bufp)[i] != r->last->end[j])
1042: break;
1043:
1.207 schwarze 1044: if ('\0' == r->last->end[j] &&
1045: ('\0' == (*bufp)[i] ||
1046: ' ' == (*bufp)[i] ||
1047: '\t' == (*bufp)[i])) {
1.79 kristaps 1048: roffnode_pop(r);
1049: roffnode_cleanscope(r);
1050:
1.130 kristaps 1051: while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
1052: i++;
1053:
1054: pos = i;
1.214 schwarze 1055: if (ROFF_MAX != roff_parse(r, *bufp, &pos, ln, ppos))
1.79 kristaps 1056: return(ROFF_RERUN);
1057: return(ROFF_IGN);
1058: }
1059: }
1060:
1061: /*
1062: * If we have no custom end-query or lookup failed, then try
1063: * pulling it out of the hashtable.
1064: */
1065:
1.214 schwarze 1066: t = roff_parse(r, *bufp, &pos, ln, ppos);
1.79 kristaps 1067:
1.106 kristaps 1068: if (ROFF_cblock != t) {
1.220 schwarze 1069: if (ROFF_ig != tok)
1.193 schwarze 1070: roff_setstr(r, r->last->name, *bufp + ppos, 2);
1.79 kristaps 1071: return(ROFF_IGN);
1.106 kristaps 1072: }
1.79 kristaps 1073:
1074: assert(roffs[t].proc);
1.207 schwarze 1075: return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
1.79 kristaps 1076: }
1077:
1078: static enum rofferr
1.80 kristaps 1079: roff_block_text(ROFF_ARGS)
1.78 kristaps 1080: {
1081:
1.220 schwarze 1082: if (ROFF_ig != tok)
1.193 schwarze 1083: roff_setstr(r, r->last->name, *bufp + pos, 2);
1.106 kristaps 1084:
1.78 kristaps 1085: return(ROFF_IGN);
1086: }
1087:
1088: static enum rofferr
1.82 kristaps 1089: roff_cond_sub(ROFF_ARGS)
1090: {
1091: enum rofft t;
1.139 kristaps 1092: char *ep;
1.198 schwarze 1093: int rr;
1.82 kristaps 1094:
1095: rr = r->last->rule;
1.139 kristaps 1096: roffnode_cleanscope(r);
1.214 schwarze 1097: t = roff_parse(r, *bufp, &pos, ln, ppos);
1.82 kristaps 1098:
1.139 kristaps 1099: /*
1.177 schwarze 1100: * Fully handle known macros when they are structurally
1101: * required or when the conditional evaluated to true.
1.87 kristaps 1102: */
1103:
1.177 schwarze 1104: if ((ROFF_MAX != t) &&
1.198 schwarze 1105: (rr || ROFFMAC_STRUCT & roffs[t].flags)) {
1.177 schwarze 1106: assert(roffs[t].proc);
1107: return((*roffs[t].proc)(r, t, bufp, szp,
1.207 schwarze 1108: ln, ppos, pos, offs));
1.177 schwarze 1109: }
1.144 kristaps 1110:
1.196 schwarze 1111: /*
1112: * If `\}' occurs on a macro line without a preceding macro,
1113: * drop the line completely.
1114: */
1115:
1116: ep = *bufp + pos;
1117: if ('\\' == ep[0] && '}' == ep[1])
1.198 schwarze 1118: rr = 0;
1.196 schwarze 1119:
1.177 schwarze 1120: /* Always check for the closing delimiter `\}'. */
1.144 kristaps 1121:
1.177 schwarze 1122: while (NULL != (ep = strchr(ep, '\\'))) {
1.197 schwarze 1123: if ('}' == *(++ep)) {
1124: *ep = '&';
1125: roff_ccond(r, ln, ep - *bufp - 1);
1126: }
1127: ++ep;
1.177 schwarze 1128: }
1.198 schwarze 1129: return(rr ? ROFF_CONT : ROFF_IGN);
1.82 kristaps 1130: }
1131:
1132: static enum rofferr
1133: roff_cond_text(ROFF_ARGS)
1.78 kristaps 1134: {
1.140 kristaps 1135: char *ep;
1.198 schwarze 1136: int rr;
1.82 kristaps 1137:
1138: rr = r->last->rule;
1.140 kristaps 1139: roffnode_cleanscope(r);
1.82 kristaps 1140:
1.197 schwarze 1141: ep = *bufp + pos;
1142: while (NULL != (ep = strchr(ep, '\\'))) {
1143: if ('}' == *(++ep)) {
1144: *ep = '&';
1145: roff_ccond(r, ln, ep - *bufp - 1);
1146: }
1147: ++ep;
1.78 kristaps 1148: }
1.198 schwarze 1149: return(rr ? ROFF_CONT : ROFF_IGN);
1.74 kristaps 1150: }
1151:
1.204 schwarze 1152: /*
1153: * Parse a single signed integer number. Stop at the first non-digit.
1154: * If there is at least one digit, return success and advance the
1155: * parse point, else return failure and let the parse point unchanged.
1156: * Ignore overflows, treat them just like the C language.
1157: */
1.184 schwarze 1158: static int
1159: roff_getnum(const char *v, int *pos, int *res)
1160: {
1.206 schwarze 1161: int myres, n, p;
1162:
1163: if (NULL == res)
1164: res = &myres;
1.184 schwarze 1165:
1166: p = *pos;
1167: n = v[p] == '-';
1168: if (n)
1169: p++;
1170:
1171: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204 schwarze 1172: *res = 10 * *res + v[p] - '0';
1.184 schwarze 1173: if (p == *pos + n)
1174: return 0;
1175:
1176: if (n)
1177: *res = -*res;
1178:
1179: *pos = p;
1180: return 1;
1181: }
1182:
1.198 schwarze 1183: /*
1184: * Evaluate a string comparison condition.
1185: * The first character is the delimiter.
1186: * Succeed if the string up to its second occurrence
1187: * matches the string up to its third occurence.
1188: * Advance the cursor after the third occurrence
1189: * or lacking that, to the end of the line.
1190: */
1191: static int
1192: roff_evalstrcond(const char *v, int *pos)
1193: {
1194: const char *s1, *s2, *s3;
1195: int match;
1196:
1197: match = 0;
1198: s1 = v + *pos; /* initial delimiter */
1199: s2 = s1 + 1; /* for scanning the first string */
1200: s3 = strchr(s2, *s1); /* for scanning the second string */
1201:
1202: if (NULL == s3) /* found no middle delimiter */
1203: goto out;
1204:
1205: while ('\0' != *++s3) {
1206: if (*s2 != *s3) { /* mismatch */
1207: s3 = strchr(s3, *s1);
1208: break;
1209: }
1210: if (*s3 == *s1) { /* found the final delimiter */
1211: match = 1;
1212: break;
1213: }
1214: s2++;
1215: }
1216:
1217: out:
1218: if (NULL == s3)
1219: s3 = strchr(s2, '\0');
1220: else
1221: s3++;
1222: *pos = s3 - v;
1223: return(match);
1224: }
1225:
1.204 schwarze 1226: /*
1227: * Evaluate an optionally negated single character, numerical,
1228: * or string condition.
1229: */
1.198 schwarze 1230: static int
1.88 kristaps 1231: roff_evalcond(const char *v, int *pos)
1232: {
1.204 schwarze 1233: int wanttrue, number;
1.88 kristaps 1234:
1.198 schwarze 1235: if ('!' == v[*pos]) {
1236: wanttrue = 0;
1237: (*pos)++;
1238: } else
1239: wanttrue = 1;
1240:
1.88 kristaps 1241: switch (v[*pos]) {
1.207 schwarze 1242: case 'n':
1.198 schwarze 1243: /* FALLTHROUGH */
1.207 schwarze 1244: case 'o':
1.88 kristaps 1245: (*pos)++;
1.198 schwarze 1246: return(wanttrue);
1.207 schwarze 1247: case 'c':
1.198 schwarze 1248: /* FALLTHROUGH */
1.207 schwarze 1249: case 'd':
1.198 schwarze 1250: /* FALLTHROUGH */
1.207 schwarze 1251: case 'e':
1.88 kristaps 1252: /* FALLTHROUGH */
1.207 schwarze 1253: case 'r':
1.88 kristaps 1254: /* FALLTHROUGH */
1.207 schwarze 1255: case 't':
1.88 kristaps 1256: (*pos)++;
1.198 schwarze 1257: return(!wanttrue);
1.88 kristaps 1258: default:
1259: break;
1260: }
1261:
1.204 schwarze 1262: if (roff_evalnum(v, pos, &number, 0))
1263: return((number > 0) == wanttrue);
1264: else
1.198 schwarze 1265: return(roff_evalstrcond(v, pos) == wanttrue);
1.88 kristaps 1266: }
1267:
1.74 kristaps 1268: static enum rofferr
1.103 kristaps 1269: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 1270: {
1.123 schwarze 1271:
1.89 kristaps 1272: return(ROFF_IGN);
1273: }
1274:
1.104 kristaps 1275: static enum rofferr
1.82 kristaps 1276: roff_cond(ROFF_ARGS)
1.74 kristaps 1277: {
1.173 schwarze 1278:
1279: roffnode_push(r, tok, NULL, ln, ppos);
1.74 kristaps 1280:
1.207 schwarze 1281: /*
1.134 kristaps 1282: * An `.el' has no conditional body: it will consume the value
1283: * of the current rstack entry set in prior `ie' calls or
1.207 schwarze 1284: * defaults to DENY.
1.134 kristaps 1285: *
1286: * If we're not an `el', however, then evaluate the conditional.
1287: */
1.133 kristaps 1288:
1.173 schwarze 1289: r->last->rule = ROFF_el == tok ?
1.207 schwarze 1290: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1291: roff_evalcond(*bufp, &pos);
1.77 kristaps 1292:
1.134 kristaps 1293: /*
1294: * An if-else will put the NEGATION of the current evaluated
1295: * conditional into the stack of rules.
1296: */
1297:
1.84 schwarze 1298: if (ROFF_ie == tok) {
1.223 schwarze 1299: if (r->rstackpos + 1 == r->rstacksz) {
1300: r->rstacksz += 16;
1301: r->rstack = mandoc_reallocarray(r->rstack,
1302: r->rstacksz, sizeof(int));
1.134 kristaps 1303: }
1.198 schwarze 1304: r->rstack[++r->rstackpos] = !r->last->rule;
1.82 kristaps 1305: }
1.88 kristaps 1306:
1307: /* If the parent has false as its rule, then so do we. */
1308:
1.198 schwarze 1309: if (r->last->parent && !r->last->parent->rule)
1310: r->last->rule = 0;
1.88 kristaps 1311:
1312: /*
1.173 schwarze 1313: * Determine scope.
1314: * If there is nothing on the line after the conditional,
1315: * not even whitespace, use next-line scope.
1.88 kristaps 1316: */
1.74 kristaps 1317:
1.173 schwarze 1318: if ('\0' == (*bufp)[pos]) {
1319: r->last->endspan = 2;
1320: goto out;
1321: }
1322:
1323: while (' ' == (*bufp)[pos])
1324: pos++;
1325:
1326: /* An opening brace requests multiline scope. */
1.75 kristaps 1327:
1328: if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1329: r->last->endspan = -1;
1330: pos += 2;
1.173 schwarze 1331: goto out;
1.207 schwarze 1332: }
1.74 kristaps 1333:
1.77 kristaps 1334: /*
1.173 schwarze 1335: * Anything else following the conditional causes
1336: * single-line scope. Warn if the scope contains
1337: * nothing but trailing whitespace.
1.77 kristaps 1338: */
1339:
1.75 kristaps 1340: if ('\0' == (*bufp)[pos])
1.216 schwarze 1341: mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
1342: ln, ppos, roffs[tok].name);
1.77 kristaps 1343:
1.173 schwarze 1344: r->last->endspan = 1;
1.74 kristaps 1345:
1.173 schwarze 1346: out:
1.75 kristaps 1347: *offs = pos;
1348: return(ROFF_RERUN);
1.83 schwarze 1349: }
1350:
1351: static enum rofferr
1.92 schwarze 1352: roff_ds(ROFF_ARGS)
1353: {
1.212 schwarze 1354: char *string;
1355: const char *name;
1356: size_t namesz;
1.96 kristaps 1357:
1358: /*
1.212 schwarze 1359: * The first word is the name of the string.
1360: * If it is empty or terminated by an escape sequence,
1361: * abort the `ds' request without defining anything.
1.96 kristaps 1362: */
1.92 schwarze 1363:
1.212 schwarze 1364: name = string = *bufp + pos;
1.92 schwarze 1365: if ('\0' == *name)
1366: return(ROFF_IGN);
1367:
1.212 schwarze 1368: namesz = roff_getname(r, &string, ln, pos);
1369: if ('\\' == name[namesz])
1370: return(ROFF_IGN);
1371:
1372: /* Read past the initial double-quote, if any. */
1.121 schwarze 1373: if ('"' == *string)
1.92 schwarze 1374: string++;
1375:
1.96 kristaps 1376: /* The rest is the value. */
1.212 schwarze 1377: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
1378: ROFF_as == tok);
1.92 schwarze 1379: return(ROFF_IGN);
1380: }
1381:
1.204 schwarze 1382: /*
1383: * Parse a single operator, one or two characters long.
1384: * If the operator is recognized, return success and advance the
1385: * parse point, else return failure and let the parse point unchanged.
1386: */
1387: static int
1388: roff_getop(const char *v, int *pos, char *res)
1389: {
1390:
1391: *res = v[*pos];
1392:
1393: switch (*res) {
1.207 schwarze 1394: case '+':
1.204 schwarze 1395: /* FALLTHROUGH */
1.207 schwarze 1396: case '-':
1.204 schwarze 1397: /* FALLTHROUGH */
1.207 schwarze 1398: case '*':
1.204 schwarze 1399: /* FALLTHROUGH */
1.207 schwarze 1400: case '/':
1.204 schwarze 1401: /* FALLTHROUGH */
1.207 schwarze 1402: case '%':
1.204 schwarze 1403: /* FALLTHROUGH */
1.207 schwarze 1404: case '&':
1.204 schwarze 1405: /* FALLTHROUGH */
1.207 schwarze 1406: case ':':
1.204 schwarze 1407: break;
1408: case '<':
1409: switch (v[*pos + 1]) {
1.207 schwarze 1410: case '=':
1.204 schwarze 1411: *res = 'l';
1412: (*pos)++;
1413: break;
1.207 schwarze 1414: case '>':
1.204 schwarze 1415: *res = '!';
1416: (*pos)++;
1417: break;
1.207 schwarze 1418: case '?':
1.204 schwarze 1419: *res = 'i';
1420: (*pos)++;
1421: break;
1422: default:
1423: break;
1424: }
1425: break;
1426: case '>':
1427: switch (v[*pos + 1]) {
1.207 schwarze 1428: case '=':
1.204 schwarze 1429: *res = 'g';
1430: (*pos)++;
1431: break;
1.207 schwarze 1432: case '?':
1.204 schwarze 1433: *res = 'a';
1434: (*pos)++;
1435: break;
1436: default:
1437: break;
1438: }
1439: break;
1440: case '=':
1441: if ('=' == v[*pos + 1])
1442: (*pos)++;
1443: break;
1444: default:
1445: return(0);
1446: }
1447: (*pos)++;
1448:
1449: return(*res);
1450: }
1451:
1452: /*
1453: * Evaluate either a parenthesized numeric expression
1454: * or a single signed integer number.
1455: */
1456: static int
1457: roff_evalpar(const char *v, int *pos, int *res)
1458: {
1459:
1460: if ('(' != v[*pos])
1461: return(roff_getnum(v, pos, res));
1462:
1463: (*pos)++;
1464: if ( ! roff_evalnum(v, pos, res, 1))
1465: return(0);
1466:
1.206 schwarze 1467: /*
1468: * Omission of the closing parenthesis
1469: * is an error in validation mode,
1470: * but ignored in evaluation mode.
1471: */
1472:
1.204 schwarze 1473: if (')' == v[*pos])
1474: (*pos)++;
1.206 schwarze 1475: else if (NULL == res)
1476: return(0);
1.204 schwarze 1477:
1478: return(1);
1479: }
1480:
1481: /*
1482: * Evaluate a complete numeric expression.
1483: * Proceed left to right, there is no concept of precedence.
1484: */
1485: static int
1486: roff_evalnum(const char *v, int *pos, int *res, int skipwhite)
1487: {
1488: int mypos, operand2;
1489: char operator;
1490:
1491: if (NULL == pos) {
1492: mypos = 0;
1493: pos = &mypos;
1494: }
1495:
1496: if (skipwhite)
1497: while (isspace((unsigned char)v[*pos]))
1498: (*pos)++;
1499:
1500: if ( ! roff_evalpar(v, pos, res))
1501: return(0);
1502:
1503: while (1) {
1504: if (skipwhite)
1505: while (isspace((unsigned char)v[*pos]))
1506: (*pos)++;
1507:
1508: if ( ! roff_getop(v, pos, &operator))
1509: break;
1510:
1511: if (skipwhite)
1512: while (isspace((unsigned char)v[*pos]))
1513: (*pos)++;
1514:
1515: if ( ! roff_evalpar(v, pos, &operand2))
1516: return(0);
1517:
1518: if (skipwhite)
1519: while (isspace((unsigned char)v[*pos]))
1520: (*pos)++;
1.206 schwarze 1521:
1522: if (NULL == res)
1523: continue;
1.204 schwarze 1524:
1525: switch (operator) {
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 *= operand2;
1534: break;
1.207 schwarze 1535: case '/':
1.204 schwarze 1536: *res /= operand2;
1537: break;
1.207 schwarze 1538: case '%':
1.204 schwarze 1539: *res %= operand2;
1540: break;
1.207 schwarze 1541: case '<':
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 'l':
1.204 schwarze 1548: *res = *res <= operand2;
1549: break;
1.207 schwarze 1550: case 'g':
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 '!':
1.204 schwarze 1557: *res = *res != operand2;
1558: break;
1.207 schwarze 1559: case '&':
1.204 schwarze 1560: *res = *res && operand2;
1561: break;
1.207 schwarze 1562: case ':':
1.204 schwarze 1563: *res = *res || operand2;
1564: break;
1.207 schwarze 1565: case 'i':
1.204 schwarze 1566: if (operand2 < *res)
1567: *res = operand2;
1568: break;
1.207 schwarze 1569: case 'a':
1.204 schwarze 1570: if (operand2 > *res)
1571: *res = operand2;
1572: break;
1573: default:
1574: abort();
1575: }
1576: }
1577: return(1);
1578: }
1579:
1.180 schwarze 1580: void
1.187 schwarze 1581: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147 kristaps 1582: {
1.180 schwarze 1583: struct roffreg *reg;
1584:
1585: /* Search for an existing register with the same name. */
1586: reg = r->regtab;
1587:
1588: while (reg && strcmp(name, reg->key.p))
1589: reg = reg->next;
1.147 kristaps 1590:
1.180 schwarze 1591: if (NULL == reg) {
1592: /* Create a new register. */
1593: reg = mandoc_malloc(sizeof(struct roffreg));
1594: reg->key.p = mandoc_strdup(name);
1595: reg->key.sz = strlen(name);
1.187 schwarze 1596: reg->val = 0;
1.180 schwarze 1597: reg->next = r->regtab;
1598: r->regtab = reg;
1599: }
1600:
1.187 schwarze 1601: if ('+' == sign)
1602: reg->val += val;
1603: else if ('-' == sign)
1604: reg->val -= val;
1605: else
1606: reg->val = val;
1.147 kristaps 1607: }
1608:
1.192 schwarze 1609: /*
1610: * Handle some predefined read-only number registers.
1611: * For now, return -1 if the requested register is not predefined;
1612: * in case a predefined read-only register having the value -1
1613: * were to turn up, another special value would have to be chosen.
1614: */
1615: static int
1616: roff_getregro(const char *name)
1617: {
1618:
1619: switch (*name) {
1.207 schwarze 1620: case 'A': /* ASCII approximation mode is always off. */
1.192 schwarze 1621: return(0);
1.207 schwarze 1622: case 'g': /* Groff compatibility mode is always on. */
1.192 schwarze 1623: return(1);
1.207 schwarze 1624: case 'H': /* Fixed horizontal resolution. */
1.192 schwarze 1625: return (24);
1.207 schwarze 1626: case 'j': /* Always adjust left margin only. */
1.192 schwarze 1627: return(0);
1.207 schwarze 1628: case 'T': /* Some output device is always defined. */
1.192 schwarze 1629: return(1);
1.207 schwarze 1630: case 'V': /* Fixed vertical resolution. */
1.192 schwarze 1631: return (40);
1632: default:
1633: return (-1);
1634: }
1635: }
1636:
1.181 schwarze 1637: int
1.180 schwarze 1638: roff_getreg(const struct roff *r, const char *name)
1.147 kristaps 1639: {
1.180 schwarze 1640: struct roffreg *reg;
1.192 schwarze 1641: int val;
1642:
1643: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
1644: val = roff_getregro(name + 1);
1645: if (-1 != val)
1646: return (val);
1647: }
1.180 schwarze 1648:
1649: for (reg = r->regtab; reg; reg = reg->next)
1650: if (0 == strcmp(name, reg->key.p))
1.181 schwarze 1651: return(reg->val);
1652:
1653: return(0);
1654: }
1655:
1656: static int
1657: roff_getregn(const struct roff *r, const char *name, size_t len)
1658: {
1659: struct roffreg *reg;
1.192 schwarze 1660: int val;
1661:
1662: if ('.' == name[0] && 2 == len) {
1663: val = roff_getregro(name + 1);
1664: if (-1 != val)
1665: return (val);
1666: }
1.181 schwarze 1667:
1668: for (reg = r->regtab; reg; reg = reg->next)
1669: if (len == reg->key.sz &&
1670: 0 == strncmp(name, reg->key.p, len))
1671: return(reg->val);
1.147 kristaps 1672:
1.180 schwarze 1673: return(0);
1.147 kristaps 1674: }
1675:
1.180 schwarze 1676: static void
1677: roff_freereg(struct roffreg *reg)
1.147 kristaps 1678: {
1.180 schwarze 1679: struct roffreg *old_reg;
1.147 kristaps 1680:
1.180 schwarze 1681: while (NULL != reg) {
1682: free(reg->key.p);
1683: old_reg = reg;
1684: reg = reg->next;
1685: free(old_reg);
1686: }
1.147 kristaps 1687: }
1.92 schwarze 1688:
1689: static enum rofferr
1.89 kristaps 1690: roff_nr(ROFF_ARGS)
1.83 schwarze 1691: {
1.212 schwarze 1692: char *key, *val;
1693: size_t keysz;
1.138 kristaps 1694: int iv;
1.187 schwarze 1695: char sign;
1.89 kristaps 1696:
1.212 schwarze 1697: key = val = *bufp + pos;
1698: if ('\0' == *key)
1699: return(ROFF_IGN);
1700:
1701: keysz = roff_getname(r, &val, ln, pos);
1702: if ('\\' == key[keysz])
1703: return(ROFF_IGN);
1704: key[keysz] = '\0';
1.89 kristaps 1705:
1.187 schwarze 1706: sign = *val;
1707: if ('+' == sign || '-' == sign)
1708: val++;
1709:
1.204 schwarze 1710: if (roff_evalnum(val, NULL, &iv, 0))
1711: roff_setreg(r, key, iv, sign);
1.109 kristaps 1712:
1.203 schwarze 1713: return(ROFF_IGN);
1714: }
1715:
1716: static enum rofferr
1717: roff_rr(ROFF_ARGS)
1718: {
1719: struct roffreg *reg, **prev;
1.212 schwarze 1720: char *name, *cp;
1721: size_t namesz;
1.203 schwarze 1722:
1.212 schwarze 1723: name = cp = *bufp + pos;
1724: if ('\0' == *name)
1725: return(ROFF_IGN);
1726: namesz = roff_getname(r, &cp, ln, pos);
1727: name[namesz] = '\0';
1.203 schwarze 1728:
1729: prev = &r->regtab;
1730: while (1) {
1731: reg = *prev;
1732: if (NULL == reg || !strcmp(name, reg->key.p))
1733: break;
1734: prev = ®->next;
1735: }
1736: if (NULL != reg) {
1737: *prev = reg->next;
1738: free(reg->key.p);
1739: free(reg);
1740: }
1.122 schwarze 1741: return(ROFF_IGN);
1742: }
1743:
1744: static enum rofferr
1745: roff_rm(ROFF_ARGS)
1746: {
1747: const char *name;
1748: char *cp;
1.212 schwarze 1749: size_t namesz;
1.122 schwarze 1750:
1751: cp = *bufp + pos;
1752: while ('\0' != *cp) {
1.212 schwarze 1753: name = cp;
1754: namesz = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1755: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1756: if ('\\' == name[namesz])
1757: break;
1.122 schwarze 1758: }
1.178 schwarze 1759: return(ROFF_IGN);
1760: }
1761:
1762: static enum rofferr
1763: roff_it(ROFF_ARGS)
1764: {
1765: char *cp;
1766: size_t len;
1767: int iv;
1768:
1769: /* Parse the number of lines. */
1770: cp = *bufp + pos;
1771: len = strcspn(cp, " \t");
1772: cp[len] = '\0';
1773: if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1.222 schwarze 1774: mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.207 schwarze 1775: ln, ppos, *bufp + 1);
1.178 schwarze 1776: return(ROFF_IGN);
1777: }
1778: cp += len + 1;
1779:
1780: /* Arm the input line trap. */
1781: roffit_lines = iv;
1782: roffit_macro = mandoc_strdup(cp);
1.109 kristaps 1783: return(ROFF_IGN);
1.175 schwarze 1784: }
1785:
1786: static enum rofferr
1787: roff_Dd(ROFF_ARGS)
1788: {
1789: const char *const *cp;
1790:
1.227 schwarze 1791: if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175 schwarze 1792: for (cp = __mdoc_reserved; *cp; cp++)
1793: roff_setstr(r, *cp, NULL, 0);
1794:
1.227 schwarze 1795: if (r->format == 0)
1796: r->format = MPARSE_MDOC;
1797:
1.175 schwarze 1798: return(ROFF_CONT);
1799: }
1800:
1801: static enum rofferr
1802: roff_TH(ROFF_ARGS)
1803: {
1804: const char *const *cp;
1805:
1.227 schwarze 1806: if ((r->options & MPARSE_QUICK) == 0)
1.175 schwarze 1807: for (cp = __man_reserved; *cp; cp++)
1808: roff_setstr(r, *cp, NULL, 0);
1809:
1.227 schwarze 1810: if (r->format == 0)
1811: r->format = MPARSE_MAN;
1812:
1.175 schwarze 1813: return(ROFF_CONT);
1.109 kristaps 1814: }
1815:
1816: static enum rofferr
1817: roff_TE(ROFF_ARGS)
1818: {
1819:
1820: if (NULL == r->tbl)
1.221 schwarze 1821: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1822: ln, ppos, "TE");
1.115 kristaps 1823: else
1.151 kristaps 1824: tbl_end(&r->tbl);
1.109 kristaps 1825:
1.112 kristaps 1826: return(ROFF_IGN);
1827: }
1828:
1829: static enum rofferr
1830: roff_T_(ROFF_ARGS)
1831: {
1832:
1833: if (NULL == r->tbl)
1.221 schwarze 1834: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1835: ln, ppos, "T&");
1.112 kristaps 1836: else
1.116 kristaps 1837: tbl_restart(ppos, ln, r->tbl);
1.112 kristaps 1838:
1.109 kristaps 1839: return(ROFF_IGN);
1840: }
1841:
1.156 kristaps 1842: #if 0
1843: static int
1.151 kristaps 1844: roff_closeeqn(struct roff *r)
1845: {
1846:
1847: return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
1848: }
1.156 kristaps 1849: #endif
1.151 kristaps 1850:
1.156 kristaps 1851: static void
1.207 schwarze 1852: roff_openeqn(struct roff *r, const char *name, int line,
1.151 kristaps 1853: int offs, const char *buf)
1.125 kristaps 1854: {
1.151 kristaps 1855: struct eqn_node *e;
1856: int poff;
1.125 kristaps 1857:
1858: assert(NULL == r->eqn);
1.151 kristaps 1859: e = eqn_alloc(name, offs, line, r->parse);
1.125 kristaps 1860:
1861: if (r->last_eqn)
1862: r->last_eqn->next = e;
1863: else
1864: r->first_eqn = r->last_eqn = e;
1865:
1866: r->eqn = r->last_eqn = e;
1.151 kristaps 1867:
1868: if (buf) {
1869: poff = 0;
1870: eqn_read(&r->eqn, line, buf, offs, &poff);
1871: }
1872: }
1873:
1874: static enum rofferr
1875: roff_EQ(ROFF_ARGS)
1876: {
1877:
1878: roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.125 kristaps 1879: return(ROFF_IGN);
1880: }
1881:
1882: static enum rofferr
1883: roff_EN(ROFF_ARGS)
1884: {
1885:
1.221 schwarze 1886: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125 kristaps 1887: return(ROFF_IGN);
1888: }
1889:
1890: static enum rofferr
1.109 kristaps 1891: roff_TS(ROFF_ARGS)
1892: {
1.176 schwarze 1893: struct tbl_node *tbl;
1.89 kristaps 1894:
1.115 kristaps 1895: if (r->tbl) {
1.221 schwarze 1896: mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
1897: ln, ppos, "TS breaks TS");
1.151 kristaps 1898: tbl_end(&r->tbl);
1.115 kristaps 1899: }
1.83 schwarze 1900:
1.176 schwarze 1901: tbl = tbl_alloc(ppos, ln, r->parse);
1.113 kristaps 1902:
1903: if (r->last_tbl)
1.176 schwarze 1904: r->last_tbl->next = tbl;
1.113 kristaps 1905: else
1.176 schwarze 1906: r->first_tbl = r->last_tbl = tbl;
1.113 kristaps 1907:
1.176 schwarze 1908: r->tbl = r->last_tbl = tbl;
1.83 schwarze 1909: return(ROFF_IGN);
1.92 schwarze 1910: }
1911:
1.105 kristaps 1912: static enum rofferr
1.174 kristaps 1913: roff_cc(ROFF_ARGS)
1914: {
1915: const char *p;
1916:
1917: p = *bufp + pos;
1918:
1919: if ('\0' == *p || '.' == (r->control = *p++))
1920: r->control = 0;
1921:
1922: if ('\0' != *p)
1923: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1924:
1925: return(ROFF_IGN);
1926: }
1927:
1928: static enum rofferr
1.164 kristaps 1929: roff_tr(ROFF_ARGS)
1930: {
1931: const char *p, *first, *second;
1932: size_t fsz, ssz;
1933: enum mandoc_esc esc;
1934:
1935: p = *bufp + pos;
1936:
1937: if ('\0' == *p) {
1938: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1939: return(ROFF_IGN);
1940: }
1941:
1942: while ('\0' != *p) {
1943: fsz = ssz = 1;
1944:
1945: first = p++;
1946: if ('\\' == *first) {
1947: esc = mandoc_escape(&p, NULL, NULL);
1948: if (ESCAPE_ERROR == esc) {
1.219 schwarze 1949: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1950: ln, (int)(p - *bufp), first);
1.164 kristaps 1951: return(ROFF_IGN);
1952: }
1953: fsz = (size_t)(p - first);
1954: }
1955:
1956: second = p++;
1957: if ('\\' == *second) {
1958: esc = mandoc_escape(&p, NULL, NULL);
1959: if (ESCAPE_ERROR == esc) {
1.219 schwarze 1960: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1961: ln, (int)(p - *bufp), second);
1.164 kristaps 1962: return(ROFF_IGN);
1963: }
1964: ssz = (size_t)(p - second);
1.165 kristaps 1965: } else if ('\0' == *second) {
1.207 schwarze 1966: mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
1967: ln, (int)(p - *bufp), NULL);
1.164 kristaps 1968: second = " ";
1.165 kristaps 1969: p--;
1.164 kristaps 1970: }
1971:
1.167 kristaps 1972: if (fsz > 1) {
1.207 schwarze 1973: roff_setstrn(&r->xmbtab, first, fsz,
1974: second, ssz, 0);
1.167 kristaps 1975: continue;
1976: }
1977:
1978: if (NULL == r->xtab)
1.207 schwarze 1979: r->xtab = mandoc_calloc(128,
1980: sizeof(struct roffstr));
1.167 kristaps 1981:
1982: free(r->xtab[(int)*first].p);
1983: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
1984: r->xtab[(int)*first].sz = ssz;
1.164 kristaps 1985: }
1986:
1987: return(ROFF_IGN);
1988: }
1989:
1990: static enum rofferr
1.105 kristaps 1991: roff_so(ROFF_ARGS)
1992: {
1993: char *name;
1994:
1.210 schwarze 1995: name = *bufp + pos;
1.224 schwarze 1996: mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105 kristaps 1997:
1998: /*
1999: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2000: * opening anything that's not in our cwd or anything beneath
2001: * it. Thus, explicitly disallow traversing up the file-system
2002: * or using absolute paths.
2003: */
2004:
2005: if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.210 schwarze 2006: mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
2007: ".so %s", name);
1.105 kristaps 2008: return(ROFF_ERR);
2009: }
2010:
2011: *offs = pos;
2012: return(ROFF_SO);
2013: }
1.92 schwarze 2014:
1.106 kristaps 2015: static enum rofferr
2016: roff_userdef(ROFF_ARGS)
1.99 kristaps 2017: {
1.106 kristaps 2018: const char *arg[9];
2019: char *cp, *n1, *n2;
1.119 schwarze 2020: int i;
1.106 kristaps 2021:
2022: /*
2023: * Collect pointers to macro argument strings
1.188 schwarze 2024: * and NUL-terminate them.
1.106 kristaps 2025: */
2026: cp = *bufp + pos;
1.119 schwarze 2027: for (i = 0; i < 9; i++)
1.120 schwarze 2028: arg[i] = '\0' == *cp ? "" :
1.136 kristaps 2029: mandoc_getarg(r->parse, &cp, ln, &pos);
1.99 kristaps 2030:
1.106 kristaps 2031: /*
2032: * Expand macro arguments.
1.99 kristaps 2033: */
1.106 kristaps 2034: *szp = 0;
2035: n1 = cp = mandoc_strdup(r->current_string);
2036: while (NULL != (cp = strstr(cp, "\\$"))) {
2037: i = cp[2] - '1';
2038: if (0 > i || 8 < i) {
2039: /* Not an argument invocation. */
2040: cp += 2;
2041: continue;
2042: }
1.209 schwarze 2043: *cp = '\0';
2044: *szp = mandoc_asprintf(&n2, "%s%s%s",
2045: n1, arg[i], cp + 3) + 1;
1.106 kristaps 2046: cp = n2 + (cp - n1);
2047: free(n1);
2048: n1 = n2;
1.99 kristaps 2049: }
2050:
1.106 kristaps 2051: /*
2052: * Replace the macro invocation
2053: * by the expanded macro.
2054: */
2055: free(*bufp);
2056: *bufp = n1;
2057: if (0 == *szp)
2058: *szp = strlen(*bufp) + 1;
2059:
2060: return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
2061: ROFF_REPARSE : ROFF_APPEND);
1.99 kristaps 2062: }
1.121 schwarze 2063:
1.212 schwarze 2064: static size_t
1.121 schwarze 2065: roff_getname(struct roff *r, char **cpp, int ln, int pos)
2066: {
2067: char *name, *cp;
1.212 schwarze 2068: size_t namesz;
1.121 schwarze 2069:
2070: name = *cpp;
2071: if ('\0' == *name)
1.212 schwarze 2072: return(0);
1.121 schwarze 2073:
1.212 schwarze 2074: /* Read until end of name and terminate it with NUL. */
2075: for (cp = name; 1; cp++) {
2076: if ('\0' == *cp || ' ' == *cp) {
2077: namesz = cp - name;
2078: break;
2079: }
1.121 schwarze 2080: if ('\\' != *cp)
2081: continue;
1.215 schwarze 2082: namesz = cp - name;
2083: if ('{' == cp[1] || '}' == cp[1])
2084: break;
1.121 schwarze 2085: cp++;
2086: if ('\\' == *cp)
2087: continue;
1.224 schwarze 2088: mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
2089: "%.*s", (int)(cp - name + 1), name);
1.212 schwarze 2090: mandoc_escape((const char **)&cp, NULL, NULL);
2091: break;
1.121 schwarze 2092: }
2093:
2094: /* Read past spaces. */
2095: while (' ' == *cp)
2096: cp++;
2097:
2098: *cpp = cp;
1.212 schwarze 2099: return(namesz);
1.121 schwarze 2100: }
2101:
1.106 kristaps 2102: /*
2103: * Store *string into the user-defined string called *name.
2104: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193 schwarze 2105: * append == 0: replace mode
2106: * append == 1: single-line append mode
2107: * append == 2: multiline append mode, append '\n' after each call
1.106 kristaps 2108: */
1.94 kristaps 2109: static void
1.106 kristaps 2110: roff_setstr(struct roff *r, const char *name, const char *string,
1.193 schwarze 2111: int append)
1.92 schwarze 2112: {
1.164 kristaps 2113:
2114: roff_setstrn(&r->strtab, name, strlen(name), string,
1.207 schwarze 2115: string ? strlen(string) : 0, append);
1.164 kristaps 2116: }
2117:
2118: static void
1.166 kristaps 2119: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193 schwarze 2120: const char *string, size_t stringsz, int append)
1.164 kristaps 2121: {
1.166 kristaps 2122: struct roffkv *n;
1.164 kristaps 2123: char *c;
2124: int i;
2125: size_t oldch, newch;
1.92 schwarze 2126:
1.106 kristaps 2127: /* Search for an existing string with the same name. */
1.164 kristaps 2128: n = *r;
2129:
1.211 schwarze 2130: while (n && (namesz != n->key.sz ||
2131: strncmp(n->key.p, name, namesz)))
1.92 schwarze 2132: n = n->next;
1.94 kristaps 2133:
2134: if (NULL == n) {
1.106 kristaps 2135: /* Create a new string table entry. */
1.166 kristaps 2136: n = mandoc_malloc(sizeof(struct roffkv));
2137: n->key.p = mandoc_strndup(name, namesz);
2138: n->key.sz = namesz;
2139: n->val.p = NULL;
2140: n->val.sz = 0;
1.164 kristaps 2141: n->next = *r;
2142: *r = n;
1.193 schwarze 2143: } else if (0 == append) {
1.166 kristaps 2144: free(n->val.p);
2145: n->val.p = NULL;
2146: n->val.sz = 0;
1.106 kristaps 2147: }
2148:
2149: if (NULL == string)
2150: return;
2151:
2152: /*
2153: * One additional byte for the '\n' in multiline mode,
2154: * and one for the terminating '\0'.
2155: */
1.193 schwarze 2156: newch = stringsz + (1 < append ? 2u : 1u);
1.164 kristaps 2157:
1.166 kristaps 2158: if (NULL == n->val.p) {
2159: n->val.p = mandoc_malloc(newch);
2160: *n->val.p = '\0';
1.106 kristaps 2161: oldch = 0;
2162: } else {
1.166 kristaps 2163: oldch = n->val.sz;
2164: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106 kristaps 2165: }
2166:
2167: /* Skip existing content in the destination buffer. */
1.166 kristaps 2168: c = n->val.p + (int)oldch;
1.106 kristaps 2169:
2170: /* Append new content to the destination buffer. */
1.164 kristaps 2171: i = 0;
2172: while (i < (int)stringsz) {
1.106 kristaps 2173: /*
2174: * Rudimentary roff copy mode:
2175: * Handle escaped backslashes.
2176: */
1.164 kristaps 2177: if ('\\' == string[i] && '\\' == string[i + 1])
2178: i++;
2179: *c++ = string[i++];
1.106 kristaps 2180: }
1.94 kristaps 2181:
1.106 kristaps 2182: /* Append terminating bytes. */
1.193 schwarze 2183: if (1 < append)
1.106 kristaps 2184: *c++ = '\n';
1.163 kristaps 2185:
1.106 kristaps 2186: *c = '\0';
1.166 kristaps 2187: n->val.sz = (int)(c - n->val.p);
1.92 schwarze 2188: }
2189:
1.94 kristaps 2190: static const char *
2191: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 2192: {
1.166 kristaps 2193: const struct roffkv *n;
1.191 schwarze 2194: int i;
1.92 schwarze 2195:
1.164 kristaps 2196: for (n = r->strtab; n; n = n->next)
1.207 schwarze 2197: if (0 == strncmp(name, n->key.p, len) &&
2198: '\0' == n->key.p[(int)len])
1.166 kristaps 2199: return(n->val.p);
1.191 schwarze 2200:
2201: for (i = 0; i < PREDEFS_MAX; i++)
2202: if (0 == strncmp(name, predefs[i].name, len) &&
2203: '\0' == predefs[i].name[(int)len])
2204: return(predefs[i].str);
1.94 kristaps 2205:
1.157 kristaps 2206: return(NULL);
1.92 schwarze 2207: }
2208:
1.94 kristaps 2209: static void
1.167 kristaps 2210: roff_freestr(struct roffkv *r)
1.92 schwarze 2211: {
1.166 kristaps 2212: struct roffkv *n, *nn;
1.92 schwarze 2213:
1.167 kristaps 2214: for (n = r; n; n = nn) {
1.166 kristaps 2215: free(n->key.p);
2216: free(n->val.p);
1.92 schwarze 2217: nn = n->next;
2218: free(n);
2219: }
1.114 kristaps 2220: }
2221:
2222: const struct tbl_span *
2223: roff_span(const struct roff *r)
2224: {
1.207 schwarze 2225:
1.114 kristaps 2226: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125 kristaps 2227: }
2228:
2229: const struct eqn *
2230: roff_eqn(const struct roff *r)
2231: {
1.207 schwarze 2232:
1.125 kristaps 2233: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164 kristaps 2234: }
2235:
2236: /*
2237: * Duplicate an input string, making the appropriate character
2238: * conversations (as stipulated by `tr') along the way.
2239: * Returns a heap-allocated string with all the replacements made.
2240: */
2241: char *
2242: roff_strdup(const struct roff *r, const char *p)
2243: {
1.166 kristaps 2244: const struct roffkv *cp;
1.164 kristaps 2245: char *res;
2246: const char *pp;
2247: size_t ssz, sz;
2248: enum mandoc_esc esc;
2249:
1.167 kristaps 2250: if (NULL == r->xmbtab && NULL == r->xtab)
1.164 kristaps 2251: return(mandoc_strdup(p));
2252: else if ('\0' == *p)
2253: return(mandoc_strdup(""));
2254:
2255: /*
2256: * Step through each character looking for term matches
2257: * (remember that a `tr' can be invoked with an escape, which is
2258: * a glyph but the escape is multi-character).
2259: * We only do this if the character hash has been initialised
2260: * and the string is >0 length.
2261: */
2262:
2263: res = NULL;
2264: ssz = 0;
2265:
2266: while ('\0' != *p) {
1.167 kristaps 2267: if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
2268: sz = r->xtab[(int)*p].sz;
2269: res = mandoc_realloc(res, ssz + sz + 1);
2270: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
2271: ssz += sz;
2272: p++;
2273: continue;
2274: } else if ('\\' != *p) {
2275: res = mandoc_realloc(res, ssz + 2);
2276: res[ssz++] = *p++;
2277: continue;
2278: }
2279:
1.164 kristaps 2280: /* Search for term matches. */
1.167 kristaps 2281: for (cp = r->xmbtab; cp; cp = cp->next)
1.166 kristaps 2282: if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164 kristaps 2283: break;
2284:
2285: if (NULL != cp) {
2286: /*
2287: * A match has been found.
2288: * Append the match to the array and move
2289: * forward by its keysize.
2290: */
1.207 schwarze 2291: res = mandoc_realloc(res,
2292: ssz + cp->val.sz + 1);
1.166 kristaps 2293: memcpy(res + ssz, cp->val.p, cp->val.sz);
2294: ssz += cp->val.sz;
2295: p += (int)cp->key.sz;
1.164 kristaps 2296: continue;
2297: }
2298:
1.167 kristaps 2299: /*
2300: * Handle escapes carefully: we need to copy
2301: * over just the escape itself, or else we might
2302: * do replacements within the escape itself.
2303: * Make sure to pass along the bogus string.
2304: */
2305: pp = p++;
2306: esc = mandoc_escape(&p, NULL, NULL);
2307: if (ESCAPE_ERROR == esc) {
2308: sz = strlen(pp);
1.164 kristaps 2309: res = mandoc_realloc(res, ssz + sz + 1);
2310: memcpy(res + ssz, pp, sz);
1.167 kristaps 2311: break;
1.164 kristaps 2312: }
1.207 schwarze 2313: /*
2314: * We bail out on bad escapes.
1.167 kristaps 2315: * No need to warn: we already did so when
2316: * roff_res() was called.
2317: */
2318: sz = (int)(p - pp);
2319: res = mandoc_realloc(res, ssz + sz + 1);
2320: memcpy(res + ssz, pp, sz);
2321: ssz += sz;
1.164 kristaps 2322: }
2323:
2324: res[(int)ssz] = '\0';
2325: return(res);
1.227 schwarze 2326: }
2327:
2328: int
2329: roff_getformat(const struct roff *r)
2330: {
2331:
2332: return(r->format);
1.174 kristaps 2333: }
2334:
2335: /*
1.207 schwarze 2336: * Find out whether a line is a macro line or not.
1.174 kristaps 2337: * If it is, adjust the current position and return one; if it isn't,
2338: * return zero and don't change the current position.
2339: * If the control character has been set with `.cc', then let that grain
2340: * precedence.
2341: * This is slighly contrary to groff, where using the non-breaking
2342: * control character when `cc' has been invoked will cause the
2343: * non-breaking macro contents to be printed verbatim.
2344: */
2345: int
2346: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
2347: {
2348: int pos;
2349:
2350: pos = *ppos;
2351:
2352: if (0 != r->control && cp[pos] == r->control)
2353: pos++;
2354: else if (0 != r->control)
2355: return(0);
2356: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
2357: pos += 2;
2358: else if ('.' == cp[pos] || '\'' == cp[pos])
2359: pos++;
2360: else
2361: return(0);
2362:
2363: while (' ' == cp[pos] || '\t' == cp[pos])
2364: pos++;
2365:
2366: *ppos = pos;
2367: return(1);
1.74 kristaps 2368: }
CVSweb