Annotation of mandoc/eqn.c, Revision 1.66
1.66 ! schwarze 1: /* $OpenBSD: eqn.c,v 1.29 2017/06/21 20:47:46 schwarze Exp $ */
1.1 kristaps 2: /*
1.51 schwarze 3: * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.64 schwarze 4: * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.
17: */
18: #include "config.h"
1.45 schwarze 19:
20: #include <sys/types.h>
1.1 kristaps 21:
22: #include <assert.h>
1.66 ! schwarze 23: #include <ctype.h>
1.19 kristaps 24: #include <limits.h>
1.1 kristaps 25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <time.h>
29:
30: #include "mandoc.h"
1.39 schwarze 31: #include "mandoc_aux.h"
1.1 kristaps 32: #include "libmandoc.h"
33: #include "libroff.h"
34:
1.11 kristaps 35: #define EQN_NEST_MAX 128 /* maximum nesting of defines */
1.48 kristaps 36: #define STRNEQ(p1, sz1, p2, sz2) \
37: ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
1.8 kristaps 38:
1.48 kristaps 39: enum eqn_tok {
40: EQN_TOK_DYAD = 0,
41: EQN_TOK_VEC,
42: EQN_TOK_UNDER,
43: EQN_TOK_BAR,
44: EQN_TOK_TILDE,
45: EQN_TOK_HAT,
46: EQN_TOK_DOT,
47: EQN_TOK_DOTDOT,
48: EQN_TOK_FWD,
49: EQN_TOK_BACK,
50: EQN_TOK_DOWN,
51: EQN_TOK_UP,
52: EQN_TOK_FAT,
53: EQN_TOK_ROMAN,
54: EQN_TOK_ITALIC,
55: EQN_TOK_BOLD,
56: EQN_TOK_SIZE,
57: EQN_TOK_SUB,
58: EQN_TOK_SUP,
59: EQN_TOK_SQRT,
60: EQN_TOK_OVER,
61: EQN_TOK_FROM,
62: EQN_TOK_TO,
63: EQN_TOK_BRACE_OPEN,
64: EQN_TOK_BRACE_CLOSE,
65: EQN_TOK_GSIZE,
66: EQN_TOK_GFONT,
67: EQN_TOK_MARK,
68: EQN_TOK_LINEUP,
69: EQN_TOK_LEFT,
70: EQN_TOK_RIGHT,
71: EQN_TOK_PILE,
72: EQN_TOK_LPILE,
73: EQN_TOK_RPILE,
74: EQN_TOK_CPILE,
75: EQN_TOK_MATRIX,
76: EQN_TOK_CCOL,
77: EQN_TOK_LCOL,
78: EQN_TOK_RCOL,
79: EQN_TOK_DELIM,
80: EQN_TOK_DEFINE,
81: EQN_TOK_TDEFINE,
82: EQN_TOK_NDEFINE,
83: EQN_TOK_UNDEF,
84: EQN_TOK_ABOVE,
1.64 schwarze 85: EQN_TOK__MAX,
86: EQN_TOK_FUNC,
87: EQN_TOK_EOF
1.48 kristaps 88: };
89:
90: static const char *eqn_toks[EQN_TOK__MAX] = {
91: "dyad", /* EQN_TOK_DYAD */
92: "vec", /* EQN_TOK_VEC */
93: "under", /* EQN_TOK_UNDER */
94: "bar", /* EQN_TOK_BAR */
95: "tilde", /* EQN_TOK_TILDE */
96: "hat", /* EQN_TOK_HAT */
97: "dot", /* EQN_TOK_DOT */
98: "dotdot", /* EQN_TOK_DOTDOT */
99: "fwd", /* EQN_TOK_FWD * */
100: "back", /* EQN_TOK_BACK */
101: "down", /* EQN_TOK_DOWN */
102: "up", /* EQN_TOK_UP */
103: "fat", /* EQN_TOK_FAT */
104: "roman", /* EQN_TOK_ROMAN */
105: "italic", /* EQN_TOK_ITALIC */
106: "bold", /* EQN_TOK_BOLD */
107: "size", /* EQN_TOK_SIZE */
108: "sub", /* EQN_TOK_SUB */
109: "sup", /* EQN_TOK_SUP */
110: "sqrt", /* EQN_TOK_SQRT */
111: "over", /* EQN_TOK_OVER */
112: "from", /* EQN_TOK_FROM */
113: "to", /* EQN_TOK_TO */
114: "{", /* EQN_TOK_BRACE_OPEN */
115: "}", /* EQN_TOK_BRACE_CLOSE */
116: "gsize", /* EQN_TOK_GSIZE */
117: "gfont", /* EQN_TOK_GFONT */
118: "mark", /* EQN_TOK_MARK */
119: "lineup", /* EQN_TOK_LINEUP */
120: "left", /* EQN_TOK_LEFT */
121: "right", /* EQN_TOK_RIGHT */
122: "pile", /* EQN_TOK_PILE */
123: "lpile", /* EQN_TOK_LPILE */
124: "rpile", /* EQN_TOK_RPILE */
125: "cpile", /* EQN_TOK_CPILE */
126: "matrix", /* EQN_TOK_MATRIX */
127: "ccol", /* EQN_TOK_CCOL */
128: "lcol", /* EQN_TOK_LCOL */
129: "rcol", /* EQN_TOK_RCOL */
130: "delim", /* EQN_TOK_DELIM */
131: "define", /* EQN_TOK_DEFINE */
132: "tdefine", /* EQN_TOK_TDEFINE */
133: "ndefine", /* EQN_TOK_NDEFINE */
134: "undef", /* EQN_TOK_UNDEF */
135: "above", /* EQN_TOK_ABOVE */
1.20 kristaps 136: };
137:
1.64 schwarze 138: static const char *const eqn_func[] = {
139: "acos", "acsc", "and", "arc", "asec", "asin", "atan",
140: "cos", "cosh", "coth", "csc", "det", "exp", "for",
141: "if", "lim", "ln", "log", "max", "min",
142: "sec", "sin", "sinh", "tan", "tanh", "Im", "Re",
143: };
144:
1.27 kristaps 145: enum eqn_symt {
146: EQNSYM_alpha,
147: EQNSYM_beta,
148: EQNSYM_chi,
149: EQNSYM_delta,
150: EQNSYM_epsilon,
151: EQNSYM_eta,
152: EQNSYM_gamma,
153: EQNSYM_iota,
154: EQNSYM_kappa,
155: EQNSYM_lambda,
156: EQNSYM_mu,
157: EQNSYM_nu,
158: EQNSYM_omega,
159: EQNSYM_omicron,
160: EQNSYM_phi,
161: EQNSYM_pi,
162: EQNSYM_ps,
163: EQNSYM_rho,
164: EQNSYM_sigma,
165: EQNSYM_tau,
166: EQNSYM_theta,
167: EQNSYM_upsilon,
168: EQNSYM_xi,
169: EQNSYM_zeta,
170: EQNSYM_DELTA,
171: EQNSYM_GAMMA,
172: EQNSYM_LAMBDA,
173: EQNSYM_OMEGA,
174: EQNSYM_PHI,
175: EQNSYM_PI,
176: EQNSYM_PSI,
177: EQNSYM_SIGMA,
178: EQNSYM_THETA,
179: EQNSYM_UPSILON,
180: EQNSYM_XI,
1.28 kristaps 181: EQNSYM_inter,
182: EQNSYM_union,
183: EQNSYM_prod,
184: EQNSYM_int,
185: EQNSYM_sum,
186: EQNSYM_grad,
187: EQNSYM_del,
188: EQNSYM_times,
189: EQNSYM_cdot,
190: EQNSYM_nothing,
191: EQNSYM_approx,
192: EQNSYM_prime,
193: EQNSYM_half,
194: EQNSYM_partial,
195: EQNSYM_inf,
196: EQNSYM_muchgreat,
197: EQNSYM_muchless,
198: EQNSYM_larrow,
199: EQNSYM_rarrow,
200: EQNSYM_pm,
201: EQNSYM_nequal,
202: EQNSYM_equiv,
203: EQNSYM_lessequal,
204: EQNSYM_moreequal,
1.58 schwarze 205: EQNSYM_minus,
1.27 kristaps 206: EQNSYM__MAX
207: };
208:
209: struct eqnsym {
1.48 kristaps 210: const char *str;
1.28 kristaps 211: const char *sym;
1.27 kristaps 212: };
213:
214: static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
1.48 kristaps 215: { "alpha", "*a" }, /* EQNSYM_alpha */
216: { "beta", "*b" }, /* EQNSYM_beta */
217: { "chi", "*x" }, /* EQNSYM_chi */
218: { "delta", "*d" }, /* EQNSYM_delta */
219: { "epsilon", "*e" }, /* EQNSYM_epsilon */
220: { "eta", "*y" }, /* EQNSYM_eta */
221: { "gamma", "*g" }, /* EQNSYM_gamma */
222: { "iota", "*i" }, /* EQNSYM_iota */
223: { "kappa", "*k" }, /* EQNSYM_kappa */
224: { "lambda", "*l" }, /* EQNSYM_lambda */
225: { "mu", "*m" }, /* EQNSYM_mu */
226: { "nu", "*n" }, /* EQNSYM_nu */
227: { "omega", "*w" }, /* EQNSYM_omega */
228: { "omicron", "*o" }, /* EQNSYM_omicron */
229: { "phi", "*f" }, /* EQNSYM_phi */
230: { "pi", "*p" }, /* EQNSYM_pi */
231: { "psi", "*q" }, /* EQNSYM_psi */
232: { "rho", "*r" }, /* EQNSYM_rho */
233: { "sigma", "*s" }, /* EQNSYM_sigma */
234: { "tau", "*t" }, /* EQNSYM_tau */
235: { "theta", "*h" }, /* EQNSYM_theta */
236: { "upsilon", "*u" }, /* EQNSYM_upsilon */
237: { "xi", "*c" }, /* EQNSYM_xi */
238: { "zeta", "*z" }, /* EQNSYM_zeta */
239: { "DELTA", "*D" }, /* EQNSYM_DELTA */
240: { "GAMMA", "*G" }, /* EQNSYM_GAMMA */
241: { "LAMBDA", "*L" }, /* EQNSYM_LAMBDA */
242: { "OMEGA", "*W" }, /* EQNSYM_OMEGA */
243: { "PHI", "*F" }, /* EQNSYM_PHI */
244: { "PI", "*P" }, /* EQNSYM_PI */
245: { "PSI", "*Q" }, /* EQNSYM_PSI */
246: { "SIGMA", "*S" }, /* EQNSYM_SIGMA */
247: { "THETA", "*H" }, /* EQNSYM_THETA */
248: { "UPSILON", "*U" }, /* EQNSYM_UPSILON */
249: { "XI", "*C" }, /* EQNSYM_XI */
250: { "inter", "ca" }, /* EQNSYM_inter */
251: { "union", "cu" }, /* EQNSYM_union */
252: { "prod", "product" }, /* EQNSYM_prod */
253: { "int", "integral" }, /* EQNSYM_int */
254: { "sum", "sum" }, /* EQNSYM_sum */
255: { "grad", "gr" }, /* EQNSYM_grad */
256: { "del", "gr" }, /* EQNSYM_del */
257: { "times", "mu" }, /* EQNSYM_times */
258: { "cdot", "pc" }, /* EQNSYM_cdot */
259: { "nothing", "&" }, /* EQNSYM_nothing */
260: { "approx", "~~" }, /* EQNSYM_approx */
1.58 schwarze 261: { "prime", "fm" }, /* EQNSYM_prime */
1.48 kristaps 262: { "half", "12" }, /* EQNSYM_half */
263: { "partial", "pd" }, /* EQNSYM_partial */
264: { "inf", "if" }, /* EQNSYM_inf */
265: { ">>", ">>" }, /* EQNSYM_muchgreat */
266: { "<<", "<<" }, /* EQNSYM_muchless */
267: { "<-", "<-" }, /* EQNSYM_larrow */
268: { "->", "->" }, /* EQNSYM_rarrow */
269: { "+-", "+-" }, /* EQNSYM_pm */
270: { "!=", "!=" }, /* EQNSYM_nequal */
271: { "==", "==" }, /* EQNSYM_equiv */
272: { "<=", "<=" }, /* EQNSYM_lessequal */
273: { ">=", ">=" }, /* EQNSYM_moreequal */
1.58 schwarze 274: { "-", "mi" }, /* EQNSYM_minus */
1.27 kristaps 275: };
276:
1.57 schwarze 277: static struct eqn_box *eqn_box_alloc(struct eqn_node *, struct eqn_box *);
278: static void eqn_box_free(struct eqn_box *);
279: static struct eqn_box *eqn_box_makebinary(struct eqn_node *,
280: enum eqn_post, struct eqn_box *);
281: static void eqn_def(struct eqn_node *);
282: static struct eqn_def *eqn_def_find(struct eqn_node *, const char *, size_t);
283: static void eqn_delim(struct eqn_node *);
284: static const char *eqn_next(struct eqn_node *, char, size_t *, int);
285: static const char *eqn_nextrawtok(struct eqn_node *, size_t *);
286: static const char *eqn_nexttok(struct eqn_node *, size_t *);
287: static enum rofferr eqn_parse(struct eqn_node *, struct eqn_box *);
288: static enum eqn_tok eqn_tok_parse(struct eqn_node *, char **);
289: static void eqn_undef(struct eqn_node *);
290:
291:
1.1 kristaps 292: enum rofferr
1.40 schwarze 293: eqn_read(struct eqn_node **epp, int ln,
1.6 kristaps 294: const char *p, int pos, int *offs)
1.1 kristaps 295: {
1.8 kristaps 296: size_t sz;
297: struct eqn_node *ep;
1.12 kristaps 298: enum rofferr er;
299:
300: ep = *epp;
301:
302: /*
303: * If we're the terminating mark, unset our equation status and
304: * validate the full equation.
305: */
1.1 kristaps 306:
1.35 kristaps 307: if (0 == strncmp(p, ".EN", 3)) {
1.38 kristaps 308: er = eqn_end(epp);
1.35 kristaps 309: p += 3;
310: while (' ' == *p || '\t' == *p)
311: p++;
1.40 schwarze 312: if ('\0' == *p)
1.59 schwarze 313: return er;
1.43 schwarze 314: mandoc_vmsg(MANDOCERR_ARG_SKIP, ep->parse,
315: ln, pos, "EN %s", p);
1.59 schwarze 316: return er;
1.1 kristaps 317: }
318:
1.12 kristaps 319: /*
320: * Build up the full string, replacing all newlines with regular
321: * whitespace.
322: */
1.6 kristaps 323:
1.12 kristaps 324: sz = strlen(p + pos) + 1;
325: ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
1.6 kristaps 326:
1.12 kristaps 327: /* First invocation: nil terminate the string. */
1.8 kristaps 328:
1.12 kristaps 329: if (0 == ep->sz)
330: *ep->data = '\0';
1.8 kristaps 331:
1.12 kristaps 332: ep->sz += sz;
333: strlcat(ep->data, p + pos, ep->sz + 1);
334: strlcat(ep->data, " ", ep->sz + 1);
1.59 schwarze 335: return ROFF_IGN;
1.11 kristaps 336: }
337:
1.1 kristaps 338: struct eqn_node *
1.55 schwarze 339: eqn_alloc(int pos, int line, struct mparse *parse)
1.1 kristaps 340: {
341: struct eqn_node *p;
342:
343: p = mandoc_calloc(1, sizeof(struct eqn_node));
1.36 kristaps 344:
1.5 kristaps 345: p->parse = parse;
1.12 kristaps 346: p->eqn.ln = line;
1.2 kristaps 347: p->eqn.pos = pos;
1.29 kristaps 348: p->gsize = EQN_DEFSIZE;
1.1 kristaps 349:
1.59 schwarze 350: return p;
1.1 kristaps 351: }
352:
1.48 kristaps 353: /*
354: * Find the key "key" of the give size within our eqn-defined values.
355: */
356: static struct eqn_def *
357: eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
1.1 kristaps 358: {
1.6 kristaps 359: int i;
1.1 kristaps 360:
1.48 kristaps 361: for (i = 0; i < (int)ep->defsz; i++)
362: if (ep->defs[i].keysz && STRNEQ(ep->defs[i].key,
363: ep->defs[i].keysz, key, sz))
1.59 schwarze 364: return &ep->defs[i];
1.6 kristaps 365:
1.59 schwarze 366: return NULL;
1.20 kristaps 367: }
368:
1.48 kristaps 369: /*
370: * Get the next token from the input stream using the given quote
371: * character.
372: * Optionally make any replacements.
373: */
1.12 kristaps 374: static const char *
1.14 kristaps 375: eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
1.6 kristaps 376: {
1.62 schwarze 377: static size_t last_len;
378: static int lim;
379:
1.12 kristaps 380: char *start, *next;
1.62 schwarze 381: int q, diff;
1.22 kristaps 382: size_t ssz, dummy;
1.12 kristaps 383: struct eqn_def *def;
384:
385: if (NULL == sz)
1.22 kristaps 386: sz = &dummy;
1.6 kristaps 387:
1.62 schwarze 388: if (ep->cur >= last_len)
389: lim = 0;
1.20 kristaps 390: ep->rew = ep->cur;
1.13 kristaps 391: again:
392: /* Prevent self-definitions. */
393:
394: if (lim >= EQN_NEST_MAX) {
1.57 schwarze 395: mandoc_msg(MANDOCERR_ROFFLOOP, ep->parse,
396: ep->eqn.ln, ep->eqn.pos, NULL);
1.59 schwarze 397: return NULL;
1.13 kristaps 398: }
399:
1.20 kristaps 400: ep->cur = ep->rew;
1.12 kristaps 401: start = &ep->data[(int)ep->cur];
1.6 kristaps 402: q = 0;
403:
404: if ('\0' == *start)
1.59 schwarze 405: return NULL;
1.6 kristaps 406:
1.12 kristaps 407: if (quote == *start) {
408: ep->cur++;
1.6 kristaps 409: q = 1;
410: }
411:
1.12 kristaps 412: start = &ep->data[(int)ep->cur];
1.22 kristaps 413:
414: if ( ! q) {
415: if ('{' == *start || '}' == *start)
416: ssz = 1;
417: else
1.31 kristaps 418: ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
1.22 kristaps 419: next = start + (int)ssz;
420: if ('\0' == *next)
421: next = NULL;
422: } else
423: next = strchr(start, quote);
1.12 kristaps 424:
425: if (NULL != next) {
426: *sz = (size_t)(next - start);
427: ep->cur += *sz;
1.6 kristaps 428: if (q)
1.12 kristaps 429: ep->cur++;
1.22 kristaps 430: while (' ' == ep->data[(int)ep->cur] ||
1.40 schwarze 431: '\t' == ep->data[(int)ep->cur] ||
432: '^' == ep->data[(int)ep->cur] ||
433: '~' == ep->data[(int)ep->cur])
1.12 kristaps 434: ep->cur++;
1.6 kristaps 435: } else {
436: if (q)
1.57 schwarze 437: mandoc_msg(MANDOCERR_ARG_QUOTE, ep->parse,
438: ep->eqn.ln, ep->eqn.pos, NULL);
1.12 kristaps 439: next = strchr(start, '\0');
440: *sz = (size_t)(next - start);
441: ep->cur += *sz;
442: }
443:
1.13 kristaps 444: /* Quotes aren't expanded for values. */
445:
1.14 kristaps 446: if (q || ! repl)
1.59 schwarze 447: return start;
1.13 kristaps 448:
1.12 kristaps 449: if (NULL != (def = eqn_def_find(ep, start, *sz))) {
450: diff = def->valsz - *sz;
451:
452: if (def->valsz > *sz) {
453: ep->sz += diff;
454: ep->data = mandoc_realloc(ep->data, ep->sz + 1);
455: ep->data[ep->sz] = '\0';
1.20 kristaps 456: start = &ep->data[(int)ep->rew];
1.12 kristaps 457: }
458:
459: diff = def->valsz - *sz;
1.40 schwarze 460: memmove(start + *sz + diff, start + *sz,
461: (strlen(start) - *sz) + 1);
1.12 kristaps 462: memcpy(start, def->val, def->valsz);
1.62 schwarze 463: last_len = start - ep->data + def->valsz;
1.60 schwarze 464: lim++;
1.12 kristaps 465: goto again;
1.6 kristaps 466: }
467:
1.59 schwarze 468: return start;
1.8 kristaps 469: }
470:
1.48 kristaps 471: /*
472: * Get the next delimited token using the default current quote
473: * character.
474: */
475: static const char *
476: eqn_nexttok(struct eqn_node *ep, size_t *sz)
477: {
478:
1.59 schwarze 479: return eqn_next(ep, '"', sz, 1);
1.48 kristaps 480: }
481:
482: /*
483: * Get next token without replacement.
484: */
485: static const char *
486: eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
487: {
488:
1.59 schwarze 489: return eqn_next(ep, '"', sz, 0);
1.48 kristaps 490: }
491:
492: /*
493: * Parse a token from the stream of text.
494: * A token consists of one of the recognised eqn(7) strings.
495: * Strings are separated by delimiting marks.
496: * This returns EQN_TOK_EOF when there are no more tokens.
497: * If the token is an unrecognised string literal, then it returns
498: * EQN_TOK__MAX and sets the "p" pointer to an allocated, nil-terminated
499: * string.
500: * This must be later freed with free(3).
501: */
502: static enum eqn_tok
503: eqn_tok_parse(struct eqn_node *ep, char **p)
504: {
505: const char *start;
506: size_t i, sz;
1.53 schwarze 507: int quoted;
1.48 kristaps 508:
1.64 schwarze 509: if (p != NULL)
1.48 kristaps 510: *p = NULL;
511:
1.53 schwarze 512: quoted = ep->data[ep->cur] == '"';
513:
1.64 schwarze 514: if ((start = eqn_nexttok(ep, &sz)) == NULL)
1.59 schwarze 515: return EQN_TOK_EOF;
1.53 schwarze 516:
517: if (quoted) {
518: if (p != NULL)
519: *p = mandoc_strndup(start, sz);
1.59 schwarze 520: return EQN_TOK__MAX;
1.53 schwarze 521: }
1.48 kristaps 522:
1.64 schwarze 523: for (i = 0; i < EQN_TOK__MAX; i++)
1.48 kristaps 524: if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i])))
1.64 schwarze 525: return i;
1.48 kristaps 526:
1.65 schwarze 527: for (i = 0; i < EQNSYM__MAX; i++) {
528: if (STRNEQ(start, sz,
529: eqnsyms[i].str, strlen(eqnsyms[i].str))) {
530: mandoc_asprintf(p, "\\[%s]", eqnsyms[i].sym);
531: return EQN_TOK__MAX;
532: }
533: }
534:
1.64 schwarze 535: if (p != NULL)
1.48 kristaps 536: *p = mandoc_strndup(start, sz);
537:
1.64 schwarze 538: for (i = 0; i < sizeof(eqn_func)/sizeof(*eqn_func); i++)
539: if (STRNEQ(start, sz, eqn_func[i], strlen(eqn_func[i])))
540: return EQN_TOK_FUNC;
541:
542: return EQN_TOK__MAX;
1.48 kristaps 543: }
544:
545: static void
546: eqn_box_free(struct eqn_box *bp)
1.33 kristaps 547: {
548:
1.48 kristaps 549: if (bp->first)
550: eqn_box_free(bp->first);
551: if (bp->next)
552: eqn_box_free(bp->next);
1.33 kristaps 553:
1.48 kristaps 554: free(bp->text);
555: free(bp->left);
556: free(bp->right);
557: free(bp->top);
558: free(bp->bottom);
559: free(bp);
1.33 kristaps 560: }
561:
1.48 kristaps 562: /*
563: * Allocate a box as the last child of the parent node.
564: */
565: static struct eqn_box *
566: eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
1.8 kristaps 567: {
1.48 kristaps 568: struct eqn_box *bp;
569:
570: bp = mandoc_calloc(1, sizeof(struct eqn_box));
571: bp->parent = parent;
572: bp->parent->args++;
573: bp->expectargs = UINT_MAX;
574: bp->size = ep->gsize;
575:
576: if (NULL != parent->first) {
577: parent->last->next = bp;
578: bp->prev = parent->last;
579: } else
580: parent->first = bp;
581:
582: parent->last = bp;
1.59 schwarze 583: return bp;
1.48 kristaps 584: }
1.8 kristaps 585:
1.48 kristaps 586: /*
587: * Reparent the current last node (of the current parent) under a new
588: * EQN_SUBEXPR as the first element.
589: * Then return the new parent.
590: * The new EQN_SUBEXPR will have a two-child limit.
591: */
592: static struct eqn_box *
1.51 schwarze 593: eqn_box_makebinary(struct eqn_node *ep,
1.48 kristaps 594: enum eqn_post pos, struct eqn_box *parent)
595: {
596: struct eqn_box *b, *newb;
1.36 kristaps 597:
1.48 kristaps 598: assert(NULL != parent->last);
599: b = parent->last;
600: if (parent->last == parent->first)
601: parent->first = NULL;
602: parent->args--;
603: parent->last = b->prev;
604: b->prev = NULL;
605: newb = eqn_box_alloc(ep, parent);
606: newb->pos = pos;
607: newb->type = EQN_SUBEXPR;
608: newb->expectargs = 2;
609: newb->args = 1;
610: newb->first = newb->last = b;
611: newb->first->next = NULL;
612: b->parent = newb;
1.59 schwarze 613: return newb;
1.36 kristaps 614: }
615:
1.48 kristaps 616: /*
1.54 schwarze 617: * Parse the "delim" control statement.
618: */
619: static void
620: eqn_delim(struct eqn_node *ep)
621: {
622: const char *start;
623: size_t sz;
624:
625: if ((start = eqn_nextrawtok(ep, &sz)) == NULL)
626: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
627: ep->eqn.ln, ep->eqn.pos, "delim");
628: else if (strncmp(start, "off", 3) == 0)
629: ep->delim = 0;
630: else if (strncmp(start, "on", 2) == 0) {
631: if (ep->odelim && ep->cdelim)
632: ep->delim = 1;
633: } else if (start[1] != '\0') {
634: ep->odelim = start[0];
635: ep->cdelim = start[1];
636: ep->delim = 1;
637: }
638: }
639:
640: /*
1.48 kristaps 641: * Undefine a previously-defined string.
642: */
1.57 schwarze 643: static void
1.48 kristaps 644: eqn_undef(struct eqn_node *ep)
1.36 kristaps 645: {
1.48 kristaps 646: const char *start;
647: struct eqn_def *def;
648: size_t sz;
1.36 kristaps 649:
1.57 schwarze 650: if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
651: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
652: ep->eqn.ln, ep->eqn.pos, "undef");
653: return;
654: }
655: if ((def = eqn_def_find(ep, start, sz)) == NULL)
656: return;
657: free(def->key);
658: free(def->val);
659: def->key = def->val = NULL;
660: def->keysz = def->valsz = 0;
1.8 kristaps 661: }
662:
1.57 schwarze 663: static void
1.48 kristaps 664: eqn_def(struct eqn_node *ep)
1.8 kristaps 665: {
666: const char *start;
667: size_t sz;
1.12 kristaps 668: struct eqn_def *def;
1.8 kristaps 669: int i;
670:
1.57 schwarze 671: if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
672: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
673: ep->eqn.ln, ep->eqn.pos, "define");
674: return;
1.8 kristaps 675: }
676:
1.40 schwarze 677: /*
678: * Search for a key that already exists.
1.12 kristaps 679: * Create a new key if none is found.
1.8 kristaps 680: */
1.12 kristaps 681: if (NULL == (def = eqn_def_find(ep, start, sz))) {
1.8 kristaps 682: /* Find holes in string array. */
683: for (i = 0; i < (int)ep->defsz; i++)
684: if (0 == ep->defs[i].keysz)
685: break;
686:
687: if (i == (int)ep->defsz) {
688: ep->defsz++;
1.42 schwarze 689: ep->defs = mandoc_reallocarray(ep->defs,
690: ep->defsz, sizeof(struct eqn_def));
1.9 kristaps 691: ep->defs[i].key = ep->defs[i].val = NULL;
1.8 kristaps 692: }
693:
1.57 schwarze 694: def = ep->defs + i;
695: free(def->key);
696: def->key = mandoc_strndup(start, sz);
697: def->keysz = sz;
1.8 kristaps 698: }
699:
1.14 kristaps 700: start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
1.57 schwarze 701: if (start == NULL) {
702: mandoc_vmsg(MANDOCERR_REQ_EMPTY, ep->parse,
703: ep->eqn.ln, ep->eqn.pos, "define %s", def->key);
704: free(def->key);
705: free(def->val);
706: def->key = def->val = NULL;
707: def->keysz = def->valsz = 0;
708: return;
1.8 kristaps 709: }
1.57 schwarze 710: free(def->val);
711: def->val = mandoc_strndup(start, sz);
1.12 kristaps 712: def->valsz = sz;
1.30 kristaps 713: }
714:
1.48 kristaps 715: /*
716: * Recursively parse an eqn(7) expression.
717: */
1.57 schwarze 718: static enum rofferr
1.48 kristaps 719: eqn_parse(struct eqn_node *ep, struct eqn_box *parent)
1.30 kristaps 720: {
1.57 schwarze 721: char sym[64];
1.66 ! schwarze 722: struct eqn_box *cur, *fontp, *nbox;
! 723: const char *cp, *cpn, *start;
1.48 kristaps 724: char *p;
1.65 schwarze 725: size_t sz;
1.52 schwarze 726: enum eqn_tok tok, subtok;
1.48 kristaps 727: enum eqn_post pos;
1.57 schwarze 728: int size;
1.30 kristaps 729:
1.56 schwarze 730: assert(parent != NULL);
1.57 schwarze 731:
732: /*
733: * Empty equation.
734: * Do not add it to the high-level syntax tree.
735: */
736:
1.56 schwarze 737: if (ep->data == NULL)
1.59 schwarze 738: return ROFF_IGN;
1.51 schwarze 739:
1.52 schwarze 740: next_tok:
741: tok = eqn_tok_parse(ep, &p);
742:
743: this_tok:
744: switch (tok) {
1.63 schwarze 745: case EQN_TOK_UNDEF:
1.57 schwarze 746: eqn_undef(ep);
1.48 kristaps 747: break;
1.63 schwarze 748: case EQN_TOK_NDEFINE:
749: case EQN_TOK_DEFINE:
1.57 schwarze 750: eqn_def(ep);
1.48 kristaps 751: break;
1.63 schwarze 752: case EQN_TOK_TDEFINE:
1.57 schwarze 753: if (eqn_nextrawtok(ep, NULL) == NULL ||
754: eqn_next(ep, ep->data[(int)ep->cur], NULL, 0) == NULL)
755: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
756: ep->eqn.ln, ep->eqn.pos, "tdefine");
1.48 kristaps 757: break;
1.63 schwarze 758: case EQN_TOK_DELIM:
1.54 schwarze 759: eqn_delim(ep);
760: break;
1.63 schwarze 761: case EQN_TOK_GFONT:
1.52 schwarze 762: if (eqn_nextrawtok(ep, NULL) == NULL)
763: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
764: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1.48 kristaps 765: break;
1.63 schwarze 766: case EQN_TOK_MARK:
767: case EQN_TOK_LINEUP:
1.48 kristaps 768: /* Ignore these. */
769: break;
1.63 schwarze 770: case EQN_TOK_DYAD:
771: case EQN_TOK_VEC:
772: case EQN_TOK_UNDER:
773: case EQN_TOK_BAR:
774: case EQN_TOK_TILDE:
775: case EQN_TOK_HAT:
776: case EQN_TOK_DOT:
777: case EQN_TOK_DOTDOT:
1.52 schwarze 778: if (parent->last == NULL) {
779: mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
780: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
781: cur = eqn_box_alloc(ep, parent);
782: cur->type = EQN_TEXT;
783: cur->text = mandoc_strdup("");
1.48 kristaps 784: }
1.51 schwarze 785: parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent);
1.48 kristaps 786: parent->type = EQN_LISTONE;
787: parent->expectargs = 1;
788: switch (tok) {
1.63 schwarze 789: case EQN_TOK_DOTDOT:
1.48 kristaps 790: strlcpy(sym, "\\[ad]", sizeof(sym));
791: break;
1.63 schwarze 792: case EQN_TOK_VEC:
1.48 kristaps 793: strlcpy(sym, "\\[->]", sizeof(sym));
794: break;
1.63 schwarze 795: case EQN_TOK_DYAD:
1.48 kristaps 796: strlcpy(sym, "\\[<>]", sizeof(sym));
797: break;
1.63 schwarze 798: case EQN_TOK_TILDE:
1.48 kristaps 799: strlcpy(sym, "\\[a~]", sizeof(sym));
800: break;
1.63 schwarze 801: case EQN_TOK_UNDER:
1.48 kristaps 802: strlcpy(sym, "\\[ul]", sizeof(sym));
803: break;
1.63 schwarze 804: case EQN_TOK_BAR:
1.48 kristaps 805: strlcpy(sym, "\\[rl]", sizeof(sym));
806: break;
1.63 schwarze 807: case EQN_TOK_DOT:
1.48 kristaps 808: strlcpy(sym, "\\[a.]", sizeof(sym));
809: break;
1.63 schwarze 810: case EQN_TOK_HAT:
1.48 kristaps 811: strlcpy(sym, "\\[ha]", sizeof(sym));
812: break;
813: default:
814: abort();
815: }
816:
817: switch (tok) {
1.63 schwarze 818: case EQN_TOK_DOTDOT:
819: case EQN_TOK_VEC:
820: case EQN_TOK_DYAD:
821: case EQN_TOK_TILDE:
822: case EQN_TOK_BAR:
823: case EQN_TOK_DOT:
824: case EQN_TOK_HAT:
1.48 kristaps 825: parent->top = mandoc_strdup(sym);
826: break;
1.63 schwarze 827: case EQN_TOK_UNDER:
1.48 kristaps 828: parent->bottom = mandoc_strdup(sym);
829: break;
830: default:
831: abort();
832: }
833: parent = parent->parent;
834: break;
1.63 schwarze 835: case EQN_TOK_FWD:
836: case EQN_TOK_BACK:
837: case EQN_TOK_DOWN:
838: case EQN_TOK_UP:
1.52 schwarze 839: subtok = eqn_tok_parse(ep, NULL);
840: if (subtok != EQN_TOK__MAX) {
841: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
842: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
843: tok = subtok;
844: goto this_tok;
1.48 kristaps 845: }
846: break;
1.63 schwarze 847: case EQN_TOK_FAT:
848: case EQN_TOK_ROMAN:
849: case EQN_TOK_ITALIC:
850: case EQN_TOK_BOLD:
1.48 kristaps 851: while (parent->args == parent->expectargs)
1.52 schwarze 852: parent = parent->parent;
1.48 kristaps 853: /*
854: * These values apply to the next word or sequence of
855: * words; thus, we mark that we'll have a child with
856: * exactly one of those.
857: */
858: parent = eqn_box_alloc(ep, parent);
859: parent->type = EQN_LISTONE;
860: parent->expectargs = 1;
861: switch (tok) {
1.63 schwarze 862: case EQN_TOK_FAT:
1.48 kristaps 863: parent->font = EQNFONT_FAT;
864: break;
1.63 schwarze 865: case EQN_TOK_ROMAN:
1.48 kristaps 866: parent->font = EQNFONT_ROMAN;
867: break;
1.63 schwarze 868: case EQN_TOK_ITALIC:
1.48 kristaps 869: parent->font = EQNFONT_ITALIC;
870: break;
1.63 schwarze 871: case EQN_TOK_BOLD:
1.48 kristaps 872: parent->font = EQNFONT_BOLD;
873: break;
874: default:
875: abort();
876: }
877: break;
1.63 schwarze 878: case EQN_TOK_SIZE:
879: case EQN_TOK_GSIZE:
1.48 kristaps 880: /* Accept two values: integral size and a single. */
881: if (NULL == (start = eqn_nexttok(ep, &sz))) {
1.52 schwarze 882: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
883: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
884: break;
1.48 kristaps 885: }
886: size = mandoc_strntoi(start, sz, 10);
887: if (-1 == size) {
1.52 schwarze 888: mandoc_msg(MANDOCERR_IT_NONUM, ep->parse,
889: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
890: break;
1.48 kristaps 891: }
892: if (EQN_TOK_GSIZE == tok) {
893: ep->gsize = size;
894: break;
895: }
896: parent = eqn_box_alloc(ep, parent);
897: parent->type = EQN_LISTONE;
898: parent->expectargs = 1;
899: parent->size = size;
900: break;
1.63 schwarze 901: case EQN_TOK_FROM:
902: case EQN_TOK_TO:
903: case EQN_TOK_SUB:
904: case EQN_TOK_SUP:
1.48 kristaps 905: /*
906: * We have a left-right-associative expression.
907: * Repivot under a positional node, open a child scope
908: * and keep on reading.
909: */
1.52 schwarze 910: if (parent->last == NULL) {
911: mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
912: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
913: cur = eqn_box_alloc(ep, parent);
914: cur->type = EQN_TEXT;
915: cur->text = mandoc_strdup("");
1.48 kristaps 916: }
917: /* Handle the "subsup" and "fromto" positions. */
918: if (EQN_TOK_SUP == tok && parent->pos == EQNPOS_SUB) {
919: parent->expectargs = 3;
920: parent->pos = EQNPOS_SUBSUP;
921: break;
922: }
923: if (EQN_TOK_TO == tok && parent->pos == EQNPOS_FROM) {
924: parent->expectargs = 3;
925: parent->pos = EQNPOS_FROMTO;
926: break;
927: }
928: switch (tok) {
1.63 schwarze 929: case EQN_TOK_FROM:
1.48 kristaps 930: pos = EQNPOS_FROM;
931: break;
1.63 schwarze 932: case EQN_TOK_TO:
1.48 kristaps 933: pos = EQNPOS_TO;
934: break;
1.63 schwarze 935: case EQN_TOK_SUP:
1.48 kristaps 936: pos = EQNPOS_SUP;
937: break;
1.63 schwarze 938: case EQN_TOK_SUB:
1.48 kristaps 939: pos = EQNPOS_SUB;
940: break;
941: default:
942: abort();
943: }
944: parent = eqn_box_makebinary(ep, pos, parent);
945: break;
1.63 schwarze 946: case EQN_TOK_SQRT:
1.48 kristaps 947: while (parent->args == parent->expectargs)
1.52 schwarze 948: parent = parent->parent;
1.51 schwarze 949: /*
1.48 kristaps 950: * Accept a left-right-associative set of arguments just
951: * like sub and sup and friends but without rebalancing
952: * under a pivot.
953: */
954: parent = eqn_box_alloc(ep, parent);
955: parent->type = EQN_SUBEXPR;
956: parent->pos = EQNPOS_SQRT;
957: parent->expectargs = 1;
958: break;
1.63 schwarze 959: case EQN_TOK_OVER:
1.48 kristaps 960: /*
961: * We have a right-left-associative fraction.
962: * Close out anything that's currently open, then
963: * rebalance and continue reading.
964: */
1.52 schwarze 965: if (parent->last == NULL) {
966: mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
967: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
968: cur = eqn_box_alloc(ep, parent);
969: cur->type = EQN_TEXT;
970: cur->text = mandoc_strdup("");
1.48 kristaps 971: }
972: while (EQN_SUBEXPR == parent->type)
1.52 schwarze 973: parent = parent->parent;
1.48 kristaps 974: parent = eqn_box_makebinary(ep, EQNPOS_OVER, parent);
975: break;
1.63 schwarze 976: case EQN_TOK_RIGHT:
977: case EQN_TOK_BRACE_CLOSE:
1.48 kristaps 978: /*
979: * Close out the existing brace.
980: * FIXME: this is a shitty sentinel: we should really
981: * have a native EQN_BRACE type or whatnot.
982: */
1.52 schwarze 983: for (cur = parent; cur != NULL; cur = cur->parent)
984: if (cur->type == EQN_LIST &&
985: (tok == EQN_TOK_BRACE_CLOSE ||
986: cur->left != NULL))
987: break;
988: if (cur == NULL) {
989: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ep->parse,
990: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
991: break;
992: }
993: parent = cur;
1.48 kristaps 994: if (EQN_TOK_RIGHT == tok) {
995: if (NULL == (start = eqn_nexttok(ep, &sz))) {
1.52 schwarze 996: mandoc_msg(MANDOCERR_REQ_EMPTY,
997: ep->parse, ep->eqn.ln,
998: ep->eqn.pos, eqn_toks[tok]);
999: break;
1.48 kristaps 1000: }
1001: /* Handling depends on right/left. */
1002: if (STRNEQ(start, sz, "ceiling", 7)) {
1003: strlcpy(sym, "\\[rc]", sizeof(sym));
1004: parent->right = mandoc_strdup(sym);
1005: } else if (STRNEQ(start, sz, "floor", 5)) {
1006: strlcpy(sym, "\\[rf]", sizeof(sym));
1007: parent->right = mandoc_strdup(sym);
1.51 schwarze 1008: } else
1.48 kristaps 1009: parent->right = mandoc_strndup(start, sz);
1010: }
1.52 schwarze 1011: parent = parent->parent;
1.61 schwarze 1012: if (tok == EQN_TOK_BRACE_CLOSE &&
1.51 schwarze 1013: (parent->type == EQN_PILE ||
1014: parent->type == EQN_MATRIX))
1.48 kristaps 1015: parent = parent->parent;
1016: /* Close out any "singleton" lists. */
1.51 schwarze 1017: while (parent->type == EQN_LISTONE &&
1018: parent->args == parent->expectargs)
1.52 schwarze 1019: parent = parent->parent;
1.48 kristaps 1020: break;
1.63 schwarze 1021: case EQN_TOK_BRACE_OPEN:
1022: case EQN_TOK_LEFT:
1.48 kristaps 1023: /*
1024: * If we already have something in the stack and we're
1025: * in an expression, then rewind til we're not any more
1026: * (just like with the text node).
1027: */
1028: while (parent->args == parent->expectargs)
1.52 schwarze 1029: parent = parent->parent;
1030: if (EQN_TOK_LEFT == tok &&
1031: (start = eqn_nexttok(ep, &sz)) == NULL) {
1032: mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
1033: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1034: break;
1035: }
1.48 kristaps 1036: parent = eqn_box_alloc(ep, parent);
1037: parent->type = EQN_LIST;
1038: if (EQN_TOK_LEFT == tok) {
1039: if (STRNEQ(start, sz, "ceiling", 7)) {
1040: strlcpy(sym, "\\[lc]", sizeof(sym));
1041: parent->left = mandoc_strdup(sym);
1042: } else if (STRNEQ(start, sz, "floor", 5)) {
1043: strlcpy(sym, "\\[lf]", sizeof(sym));
1044: parent->left = mandoc_strdup(sym);
1.51 schwarze 1045: } else
1.48 kristaps 1046: parent->left = mandoc_strndup(start, sz);
1047: }
1048: break;
1.63 schwarze 1049: case EQN_TOK_PILE:
1050: case EQN_TOK_LPILE:
1051: case EQN_TOK_RPILE:
1052: case EQN_TOK_CPILE:
1053: case EQN_TOK_CCOL:
1054: case EQN_TOK_LCOL:
1055: case EQN_TOK_RCOL:
1.48 kristaps 1056: while (parent->args == parent->expectargs)
1.52 schwarze 1057: parent = parent->parent;
1.48 kristaps 1058: parent = eqn_box_alloc(ep, parent);
1059: parent->type = EQN_PILE;
1.52 schwarze 1060: parent->expectargs = 1;
1.48 kristaps 1061: break;
1.63 schwarze 1062: case EQN_TOK_ABOVE:
1.52 schwarze 1063: for (cur = parent; cur != NULL; cur = cur->parent)
1064: if (cur->type == EQN_PILE)
1065: break;
1066: if (cur == NULL) {
1067: mandoc_msg(MANDOCERR_IT_STRAY, ep->parse,
1068: ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1069: break;
1070: }
1071: parent = eqn_box_alloc(ep, cur);
1.48 kristaps 1072: parent->type = EQN_LIST;
1073: break;
1.63 schwarze 1074: case EQN_TOK_MATRIX:
1.48 kristaps 1075: while (parent->args == parent->expectargs)
1.52 schwarze 1076: parent = parent->parent;
1.48 kristaps 1077: parent = eqn_box_alloc(ep, parent);
1078: parent->type = EQN_MATRIX;
1.52 schwarze 1079: parent->expectargs = 1;
1.48 kristaps 1080: break;
1.63 schwarze 1081: case EQN_TOK_EOF:
1.48 kristaps 1082: /*
1.51 schwarze 1083: * End of file!
1.48 kristaps 1084: * TODO: make sure we're not in an open subexpression.
1085: */
1.59 schwarze 1086: return ROFF_EQN;
1.64 schwarze 1087: case EQN_TOK_FUNC:
1088: case EQN_TOK__MAX:
1089: assert(p != NULL);
1.48 kristaps 1090: /*
1091: * If we already have something in the stack and we're
1092: * in an expression, then rewind til we're not any more.
1093: */
1094: while (parent->args == parent->expectargs)
1.52 schwarze 1095: parent = parent->parent;
1.66 ! schwarze 1096: /*
! 1097: * Wrap well-known function names in a roman box,
! 1098: * unless they already are in roman context.
! 1099: */
! 1100: for (fontp = parent; fontp != NULL; fontp = fontp->parent)
! 1101: if (fontp->font != EQNFONT_NONE)
! 1102: break;
! 1103: if (tok == EQN_TOK_FUNC &&
! 1104: (fontp == NULL || fontp->font != EQNFONT_ROMAN)) {
! 1105: parent = fontp = eqn_box_alloc(ep, parent);
! 1106: parent->type = EQN_LISTONE;
! 1107: parent->font = EQNFONT_ROMAN;
! 1108: parent->expectargs = 1;
1.64 schwarze 1109: }
1.48 kristaps 1110: cur = eqn_box_alloc(ep, parent);
1111: cur->type = EQN_TEXT;
1.65 schwarze 1112: cur->text = p;
1.66 ! schwarze 1113: /*
! 1114: * If not inside any explicit font context,
! 1115: * give every letter its own box.
! 1116: */
! 1117: if (fontp == NULL && *p != '\0') {
! 1118: cp = p;
! 1119: for (;;) {
! 1120: cpn = cp + 1;
! 1121: if (*cp == '\\')
! 1122: mandoc_escape(&cpn, NULL, NULL);
! 1123: if (*cpn == '\0')
! 1124: break;
! 1125: if (isalpha((unsigned char)*cp) == 0 &&
! 1126: isalpha((unsigned char)*cpn) == 0) {
! 1127: cp = cpn;
! 1128: continue;
! 1129: }
! 1130: nbox = eqn_box_alloc(ep, parent);
! 1131: nbox->type = EQN_TEXT;
! 1132: nbox->text = mandoc_strdup(cpn);
! 1133: p = mandoc_strndup(cur->text,
! 1134: cpn - cur->text);
! 1135: free(cur->text);
! 1136: cur->text = p;
! 1137: cur = nbox;
! 1138: cp = nbox->text;
! 1139: }
! 1140: }
1.48 kristaps 1141: /*
1142: * Post-process list status.
1143: */
1.51 schwarze 1144: while (parent->type == EQN_LISTONE &&
1.52 schwarze 1145: parent->args == parent->expectargs)
1146: parent = parent->parent;
1.48 kristaps 1147: break;
1.64 schwarze 1148: default:
1149: abort();
1.40 schwarze 1150: }
1.52 schwarze 1151: goto next_tok;
1.29 kristaps 1152: }
1153:
1.48 kristaps 1154: enum rofferr
1.51 schwarze 1155: eqn_end(struct eqn_node **epp)
1.8 kristaps 1156: {
1.48 kristaps 1157: struct eqn_node *ep;
1.8 kristaps 1158:
1.48 kristaps 1159: ep = *epp;
1160: *epp = NULL;
1.8 kristaps 1161:
1.48 kristaps 1162: ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
1163: ep->eqn.root->expectargs = UINT_MAX;
1.59 schwarze 1164: return eqn_parse(ep, ep->eqn.root);
1.12 kristaps 1165: }
1166:
1.48 kristaps 1167: void
1168: eqn_free(struct eqn_node *p)
1.12 kristaps 1169: {
1170: int i;
1.8 kristaps 1171:
1.48 kristaps 1172: eqn_box_free(p->eqn.root);
1173:
1174: for (i = 0; i < (int)p->defsz; i++) {
1175: free(p->defs[i].key);
1176: free(p->defs[i].val);
1177: }
1.8 kristaps 1178:
1.48 kristaps 1179: free(p->data);
1180: free(p->defs);
1181: free(p);
1.1 kristaps 1182: }
CVSweb