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