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