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