]> git.cameronkatri.com Git - mandoc.git/blob - eqn.c
Clarify what eqn actually is.
[mandoc.git] / eqn.c
1 /* $Id: eqn.c,v 1.37 2011/07/23 18:44:42 kristaps Exp $ */
2 /*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <assert.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include "mandoc.h"
29 #include "libmandoc.h"
30 #include "libroff.h"
31
32 #define EQN_NEST_MAX 128 /* maximum nesting of defines */
33 #define EQN_MSG(t, x) mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL)
34
35 enum eqn_rest {
36 EQN_DESCOPE,
37 EQN_ERR,
38 EQN_OK,
39 EQN_EOF
40 };
41
42 enum eqn_symt {
43 EQNSYM_alpha,
44 EQNSYM_beta,
45 EQNSYM_chi,
46 EQNSYM_delta,
47 EQNSYM_epsilon,
48 EQNSYM_eta,
49 EQNSYM_gamma,
50 EQNSYM_iota,
51 EQNSYM_kappa,
52 EQNSYM_lambda,
53 EQNSYM_mu,
54 EQNSYM_nu,
55 EQNSYM_omega,
56 EQNSYM_omicron,
57 EQNSYM_phi,
58 EQNSYM_pi,
59 EQNSYM_ps,
60 EQNSYM_rho,
61 EQNSYM_sigma,
62 EQNSYM_tau,
63 EQNSYM_theta,
64 EQNSYM_upsilon,
65 EQNSYM_xi,
66 EQNSYM_zeta,
67 EQNSYM_DELTA,
68 EQNSYM_GAMMA,
69 EQNSYM_LAMBDA,
70 EQNSYM_OMEGA,
71 EQNSYM_PHI,
72 EQNSYM_PI,
73 EQNSYM_PSI,
74 EQNSYM_SIGMA,
75 EQNSYM_THETA,
76 EQNSYM_UPSILON,
77 EQNSYM_XI,
78 EQNSYM_inter,
79 EQNSYM_union,
80 EQNSYM_prod,
81 EQNSYM_int,
82 EQNSYM_sum,
83 EQNSYM_grad,
84 EQNSYM_del,
85 EQNSYM_times,
86 EQNSYM_cdot,
87 EQNSYM_nothing,
88 EQNSYM_approx,
89 EQNSYM_prime,
90 EQNSYM_half,
91 EQNSYM_partial,
92 EQNSYM_inf,
93 EQNSYM_muchgreat,
94 EQNSYM_muchless,
95 EQNSYM_larrow,
96 EQNSYM_rarrow,
97 EQNSYM_pm,
98 EQNSYM_nequal,
99 EQNSYM_equiv,
100 EQNSYM_lessequal,
101 EQNSYM_moreequal,
102 EQNSYM__MAX
103 };
104
105 enum eqnpartt {
106 EQN_DEFINE = 0,
107 EQN_NDEFINE,
108 EQN_TDEFINE,
109 EQN_SET,
110 EQN_UNDEF,
111 EQN_GFONT,
112 EQN_GSIZE,
113 EQN_BACK,
114 EQN_FWD,
115 EQN_UP,
116 EQN_DOWN,
117 EQN__MAX
118 };
119
120 struct eqnstr {
121 const char *name;
122 size_t sz;
123 };
124
125 #define STRNEQ(p1, sz1, p2, sz2) \
126 ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
127 #define EQNSTREQ(x, p, sz) \
128 STRNEQ((x)->name, (x)->sz, (p), (sz))
129
130 struct eqnpart {
131 struct eqnstr str;
132 int (*fp)(struct eqn_node *);
133 };
134
135 struct eqnsym {
136 struct eqnstr str;
137 const char *sym;
138 };
139
140
141 static enum eqn_rest eqn_box(struct eqn_node *, struct eqn_box *);
142 static struct eqn_box *eqn_box_alloc(struct eqn_node *,
143 struct eqn_box *);
144 static void eqn_box_free(struct eqn_box *);
145 static struct eqn_def *eqn_def_find(struct eqn_node *,
146 const char *, size_t);
147 static int eqn_do_gfont(struct eqn_node *);
148 static int eqn_do_gsize(struct eqn_node *);
149 static int eqn_do_define(struct eqn_node *);
150 static int eqn_do_ign1(struct eqn_node *);
151 static int eqn_do_ign2(struct eqn_node *);
152 static int eqn_do_tdefine(struct eqn_node *);
153 static int eqn_do_undef(struct eqn_node *);
154 static enum eqn_rest eqn_eqn(struct eqn_node *, struct eqn_box *);
155 static enum eqn_rest eqn_list(struct eqn_node *, struct eqn_box *);
156 static enum eqn_rest eqn_matrix(struct eqn_node *, struct eqn_box *);
157 static const char *eqn_nexttok(struct eqn_node *, size_t *);
158 static const char *eqn_nextrawtok(struct eqn_node *, size_t *);
159 static const char *eqn_next(struct eqn_node *,
160 char, size_t *, int);
161 static void eqn_rewind(struct eqn_node *);
162
163 static const struct eqnpart eqnparts[EQN__MAX] = {
164 { { "define", 6 }, eqn_do_define }, /* EQN_DEFINE */
165 { { "ndefine", 7 }, eqn_do_define }, /* EQN_NDEFINE */
166 { { "tdefine", 7 }, eqn_do_tdefine }, /* EQN_TDEFINE */
167 { { "set", 3 }, eqn_do_ign2 }, /* EQN_SET */
168 { { "undef", 5 }, eqn_do_undef }, /* EQN_UNDEF */
169 { { "gfont", 5 }, eqn_do_gfont }, /* EQN_GFONT */
170 { { "gsize", 5 }, eqn_do_gsize }, /* EQN_GSIZE */
171 { { "back", 4 }, eqn_do_ign1 }, /* EQN_BACK */
172 { { "fwd", 3 }, eqn_do_ign1 }, /* EQN_FWD */
173 { { "up", 2 }, eqn_do_ign1 }, /* EQN_UP */
174 { { "down", 4 }, eqn_do_ign1 }, /* EQN_DOWN */
175 };
176
177 static const struct eqnstr eqnmarks[EQNMARK__MAX] = {
178 { "", 0 }, /* EQNMARK_NONE */
179 { "dot", 3 }, /* EQNMARK_DOT */
180 { "dotdot", 6 }, /* EQNMARK_DOTDOT */
181 { "hat", 3 }, /* EQNMARK_HAT */
182 { "tilde", 5 }, /* EQNMARK_TILDE */
183 { "vec", 3 }, /* EQNMARK_VEC */
184 { "dyad", 4 }, /* EQNMARK_DYAD */
185 { "bar", 3 }, /* EQNMARK_BAR */
186 { "under", 5 }, /* EQNMARK_UNDER */
187 };
188
189 static const struct eqnstr eqnfonts[EQNFONT__MAX] = {
190 { "", 0 }, /* EQNFONT_NONE */
191 { "roman", 5 }, /* EQNFONT_ROMAN */
192 { "bold", 4 }, /* EQNFONT_BOLD */
193 { "fat", 3 }, /* EQNFONT_FAT */
194 { "italic", 6 }, /* EQNFONT_ITALIC */
195 };
196
197 static const struct eqnstr eqnposs[EQNPOS__MAX] = {
198 { "", 0 }, /* EQNPOS_NONE */
199 { "over", 4 }, /* EQNPOS_OVER */
200 { "sup", 3 }, /* EQNPOS_SUP */
201 { "sub", 3 }, /* EQNPOS_SUB */
202 { "to", 2 }, /* EQNPOS_TO */
203 { "from", 4 }, /* EQNPOS_FROM */
204 };
205
206 static const struct eqnstr eqnpiles[EQNPILE__MAX] = {
207 { "", 0 }, /* EQNPILE_NONE */
208 { "pile", 4 }, /* EQNPILE_PILE */
209 { "cpile", 5 }, /* EQNPILE_CPILE */
210 { "rpile", 5 }, /* EQNPILE_RPILE */
211 { "lpile", 5 }, /* EQNPILE_LPILE */
212 { "col", 3 }, /* EQNPILE_COL */
213 { "ccol", 4 }, /* EQNPILE_CCOL */
214 { "rcol", 4 }, /* EQNPILE_RCOL */
215 { "lcol", 4 }, /* EQNPILE_LCOL */
216 };
217
218 static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
219 { { "alpha", 5 }, "*a" }, /* EQNSYM_alpha */
220 { { "beta", 4 }, "*b" }, /* EQNSYM_beta */
221 { { "chi", 3 }, "*x" }, /* EQNSYM_chi */
222 { { "delta", 5 }, "*d" }, /* EQNSYM_delta */
223 { { "epsilon", 7 }, "*e" }, /* EQNSYM_epsilon */
224 { { "eta", 3 }, "*y" }, /* EQNSYM_eta */
225 { { "gamma", 5 }, "*g" }, /* EQNSYM_gamma */
226 { { "iota", 4 }, "*i" }, /* EQNSYM_iota */
227 { { "kappa", 5 }, "*k" }, /* EQNSYM_kappa */
228 { { "lambda", 6 }, "*l" }, /* EQNSYM_lambda */
229 { { "mu", 2 }, "*m" }, /* EQNSYM_mu */
230 { { "nu", 2 }, "*n" }, /* EQNSYM_nu */
231 { { "omega", 5 }, "*w" }, /* EQNSYM_omega */
232 { { "omicron", 7 }, "*o" }, /* EQNSYM_omicron */
233 { { "phi", 3 }, "*f" }, /* EQNSYM_phi */
234 { { "pi", 2 }, "*p" }, /* EQNSYM_pi */
235 { { "psi", 2 }, "*q" }, /* EQNSYM_psi */
236 { { "rho", 3 }, "*r" }, /* EQNSYM_rho */
237 { { "sigma", 5 }, "*s" }, /* EQNSYM_sigma */
238 { { "tau", 3 }, "*t" }, /* EQNSYM_tau */
239 { { "theta", 5 }, "*h" }, /* EQNSYM_theta */
240 { { "upsilon", 7 }, "*u" }, /* EQNSYM_upsilon */
241 { { "xi", 2 }, "*c" }, /* EQNSYM_xi */
242 { { "zeta", 4 }, "*z" }, /* EQNSYM_zeta */
243 { { "DELTA", 5 }, "*D" }, /* EQNSYM_DELTA */
244 { { "GAMMA", 5 }, "*G" }, /* EQNSYM_GAMMA */
245 { { "LAMBDA", 6 }, "*L" }, /* EQNSYM_LAMBDA */
246 { { "OMEGA", 5 }, "*W" }, /* EQNSYM_OMEGA */
247 { { "PHI", 3 }, "*F" }, /* EQNSYM_PHI */
248 { { "PI", 2 }, "*P" }, /* EQNSYM_PI */
249 { { "PSI", 3 }, "*Q" }, /* EQNSYM_PSI */
250 { { "SIGMA", 5 }, "*S" }, /* EQNSYM_SIGMA */
251 { { "THETA", 5 }, "*H" }, /* EQNSYM_THETA */
252 { { "UPSILON", 7 }, "*U" }, /* EQNSYM_UPSILON */
253 { { "XI", 2 }, "*C" }, /* EQNSYM_XI */
254 { { "inter", 5 }, "ca" }, /* EQNSYM_inter */
255 { { "union", 5 }, "cu" }, /* EQNSYM_union */
256 { { "prod", 4 }, "product" }, /* EQNSYM_prod */
257 { { "int", 3 }, "integral" }, /* EQNSYM_int */
258 { { "sum", 3 }, "sum" }, /* EQNSYM_sum */
259 { { "grad", 4 }, "gr" }, /* EQNSYM_grad */
260 { { "del", 3 }, "gr" }, /* EQNSYM_del */
261 { { "times", 5 }, "mu" }, /* EQNSYM_times */
262 { { "cdot", 4 }, "pc" }, /* EQNSYM_cdot */
263 { { "nothing", 7 }, "&" }, /* EQNSYM_nothing */
264 { { "approx", 6 }, "~~" }, /* EQNSYM_approx */
265 { { "prime", 5 }, "aq" }, /* EQNSYM_prime */
266 { { "half", 4 }, "12" }, /* EQNSYM_half */
267 { { "partial", 7 }, "pd" }, /* EQNSYM_partial */
268 { { "inf", 3 }, "if" }, /* EQNSYM_inf */
269 { { ">>", 2 }, ">>" }, /* EQNSYM_muchgreat */
270 { { "<<", 2 }, "<<" }, /* EQNSYM_muchless */
271 { { "<-", 2 }, "<-" }, /* EQNSYM_larrow */
272 { { "->", 2 }, "->" }, /* EQNSYM_rarrow */
273 { { "+-", 2 }, "+-" }, /* EQNSYM_pm */
274 { { "!=", 2 }, "!=" }, /* EQNSYM_nequal */
275 { { "==", 2 }, "==" }, /* EQNSYM_equiv */
276 { { "<=", 2 }, "<=" }, /* EQNSYM_lessequal */
277 { { ">=", 2 }, ">=" }, /* EQNSYM_moreequal */
278 };
279
280 /* ARGSUSED */
281 enum rofferr
282 eqn_read(struct eqn_node **epp, int ln,
283 const char *p, int pos, int *offs)
284 {
285 size_t sz;
286 struct eqn_node *ep;
287 enum rofferr er;
288
289 ep = *epp;
290
291 /*
292 * If we're the terminating mark, unset our equation status and
293 * validate the full equation.
294 */
295
296 if (0 == strncmp(p, ".EN", 3)) {
297 er = eqn_end(ep);
298 *epp = NULL;
299 p += 3;
300 while (' ' == *p || '\t' == *p)
301 p++;
302 if ('\0' == *p)
303 return(er);
304 mandoc_msg(MANDOCERR_ARGSLOST, ep->parse, ln, pos, NULL);
305 return(er);
306 }
307
308 /*
309 * Build up the full string, replacing all newlines with regular
310 * whitespace.
311 */
312
313 sz = strlen(p + pos) + 1;
314 ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
315
316 /* First invocation: nil terminate the string. */
317
318 if (0 == ep->sz)
319 *ep->data = '\0';
320
321 ep->sz += sz;
322 strlcat(ep->data, p + pos, ep->sz + 1);
323 strlcat(ep->data, " ", ep->sz + 1);
324 return(ROFF_IGN);
325 }
326
327 struct eqn_node *
328 eqn_alloc(const char *name, int pos, int line, struct mparse *parse)
329 {
330 struct eqn_node *p;
331 size_t sz;
332 const char *end;
333
334 p = mandoc_calloc(1, sizeof(struct eqn_node));
335
336 if ('\0' != *name) {
337 sz = strlen(name);
338 assert(sz);
339 do {
340 sz--;
341 end = name + (int)sz;
342 } while (' ' == *end || '\t' == *end);
343 p->eqn.name = mandoc_strndup(name, sz + 1);
344 }
345
346 p->parse = parse;
347 p->eqn.ln = line;
348 p->eqn.pos = pos;
349 p->gsize = EQN_DEFSIZE;
350
351 return(p);
352 }
353
354 enum rofferr
355 eqn_end(struct eqn_node *ep)
356 {
357 struct eqn_box *root;
358 enum eqn_rest c;
359
360 ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
361
362 root = ep->eqn.root;
363 root->type = EQN_ROOT;
364
365 if (0 == ep->sz)
366 return(ROFF_IGN);
367
368 if (EQN_DESCOPE == (c = eqn_eqn(ep, root))) {
369 EQN_MSG(MANDOCERR_EQNNSCOPE, ep);
370 c = EQN_ERR;
371 }
372
373 return(EQN_EOF == c ? ROFF_EQN : ROFF_IGN);
374 }
375
376 static enum eqn_rest
377 eqn_eqn(struct eqn_node *ep, struct eqn_box *last)
378 {
379 struct eqn_box *bp;
380 enum eqn_rest c;
381
382 bp = eqn_box_alloc(ep, last);
383 bp->type = EQN_SUBEXPR;
384
385 while (EQN_OK == (c = eqn_box(ep, bp)))
386 /* Spin! */ ;
387
388 return(c);
389 }
390
391 static enum eqn_rest
392 eqn_matrix(struct eqn_node *ep, struct eqn_box *last)
393 {
394 struct eqn_box *bp;
395 const char *start;
396 size_t sz;
397 enum eqn_rest c;
398
399 bp = eqn_box_alloc(ep, last);
400 bp->type = EQN_MATRIX;
401
402 if (NULL == (start = eqn_nexttok(ep, &sz))) {
403 EQN_MSG(MANDOCERR_EQNEOF, ep);
404 return(EQN_ERR);
405 }
406 if ( ! STRNEQ(start, sz, "{", 1)) {
407 EQN_MSG(MANDOCERR_EQNSYNT, ep);
408 return(EQN_ERR);
409 }
410
411 while (EQN_OK == (c = eqn_box(ep, bp)))
412 switch (bp->last->pile) {
413 case (EQNPILE_LCOL):
414 /* FALLTHROUGH */
415 case (EQNPILE_CCOL):
416 /* FALLTHROUGH */
417 case (EQNPILE_RCOL):
418 continue;
419 default:
420 EQN_MSG(MANDOCERR_EQNSYNT, ep);
421 return(EQN_ERR);
422 };
423
424 if (EQN_DESCOPE != c) {
425 if (EQN_EOF == c)
426 EQN_MSG(MANDOCERR_EQNEOF, ep);
427 return(EQN_ERR);
428 }
429
430 eqn_rewind(ep);
431 start = eqn_nexttok(ep, &sz);
432 assert(start);
433 if (STRNEQ(start, sz, "}", 1))
434 return(EQN_OK);
435
436 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
437 return(EQN_ERR);
438 }
439
440 static enum eqn_rest
441 eqn_list(struct eqn_node *ep, struct eqn_box *last)
442 {
443 struct eqn_box *bp;
444 const char *start;
445 size_t sz;
446 enum eqn_rest c;
447
448 bp = eqn_box_alloc(ep, last);
449 bp->type = EQN_LIST;
450
451 if (NULL == (start = eqn_nexttok(ep, &sz))) {
452 EQN_MSG(MANDOCERR_EQNEOF, ep);
453 return(EQN_ERR);
454 }
455 if ( ! STRNEQ(start, sz, "{", 1)) {
456 EQN_MSG(MANDOCERR_EQNSYNT, ep);
457 return(EQN_ERR);
458 }
459
460 while (EQN_DESCOPE == (c = eqn_eqn(ep, bp))) {
461 eqn_rewind(ep);
462 start = eqn_nexttok(ep, &sz);
463 assert(start);
464 if ( ! STRNEQ(start, sz, "above", 5))
465 break;
466 }
467
468 if (EQN_DESCOPE != c) {
469 if (EQN_ERR != c)
470 EQN_MSG(MANDOCERR_EQNSCOPE, ep);
471 return(EQN_ERR);
472 }
473
474 eqn_rewind(ep);
475 start = eqn_nexttok(ep, &sz);
476 assert(start);
477 if (STRNEQ(start, sz, "}", 1))
478 return(EQN_OK);
479
480 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
481 return(EQN_ERR);
482 }
483
484 static enum eqn_rest
485 eqn_box(struct eqn_node *ep, struct eqn_box *last)
486 {
487 size_t sz;
488 const char *start;
489 char *left;
490 char sym[64];
491 enum eqn_rest c;
492 int i, size;
493 struct eqn_box *bp;
494
495 if (NULL == (start = eqn_nexttok(ep, &sz)))
496 return(EQN_EOF);
497
498 if (STRNEQ(start, sz, "}", 1))
499 return(EQN_DESCOPE);
500 else if (STRNEQ(start, sz, "right", 5))
501 return(EQN_DESCOPE);
502 else if (STRNEQ(start, sz, "above", 5))
503 return(EQN_DESCOPE);
504 else if (STRNEQ(start, sz, "mark", 4))
505 return(EQN_OK);
506 else if (STRNEQ(start, sz, "lineup", 6))
507 return(EQN_OK);
508
509 for (i = 0; i < (int)EQN__MAX; i++) {
510 if ( ! EQNSTREQ(&eqnparts[i].str, start, sz))
511 continue;
512 return((*eqnparts[i].fp)(ep) ?
513 EQN_OK : EQN_ERR);
514 }
515
516 if (STRNEQ(start, sz, "{", 1)) {
517 if (EQN_DESCOPE != (c = eqn_eqn(ep, last))) {
518 if (EQN_ERR != c)
519 EQN_MSG(MANDOCERR_EQNSCOPE, ep);
520 return(EQN_ERR);
521 }
522 eqn_rewind(ep);
523 start = eqn_nexttok(ep, &sz);
524 assert(start);
525 if (STRNEQ(start, sz, "}", 1))
526 return(EQN_OK);
527 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
528 return(EQN_ERR);
529 }
530
531 for (i = 0; i < (int)EQNPILE__MAX; i++) {
532 if ( ! EQNSTREQ(&eqnpiles[i], start, sz))
533 continue;
534 if (EQN_OK == (c = eqn_list(ep, last)))
535 last->last->pile = (enum eqn_pilet)i;
536 return(c);
537 }
538
539 if (STRNEQ(start, sz, "matrix", 6))
540 return(eqn_matrix(ep, last));
541
542 if (STRNEQ(start, sz, "left", 4)) {
543 if (NULL == (start = eqn_nexttok(ep, &sz))) {
544 EQN_MSG(MANDOCERR_EQNEOF, ep);
545 return(EQN_ERR);
546 }
547 left = mandoc_strndup(start, sz);
548 c = eqn_eqn(ep, last);
549 if (last->last)
550 last->last->left = left;
551 else
552 free(left);
553 if (EQN_DESCOPE != c)
554 return(c);
555 assert(last->last);
556 eqn_rewind(ep);
557 start = eqn_nexttok(ep, &sz);
558 assert(start);
559 if ( ! STRNEQ(start, sz, "right", 5))
560 return(EQN_DESCOPE);
561 if (NULL == (start = eqn_nexttok(ep, &sz))) {
562 EQN_MSG(MANDOCERR_EQNEOF, ep);
563 return(EQN_ERR);
564 }
565 last->last->right = mandoc_strndup(start, sz);
566 return(EQN_OK);
567 }
568
569 for (i = 0; i < (int)EQNPOS__MAX; i++) {
570 if ( ! EQNSTREQ(&eqnposs[i], start, sz))
571 continue;
572 if (NULL == last->last) {
573 EQN_MSG(MANDOCERR_EQNSYNT, ep);
574 return(EQN_ERR);
575 }
576 last->last->pos = (enum eqn_post)i;
577 if (EQN_EOF == (c = eqn_box(ep, last))) {
578 EQN_MSG(MANDOCERR_EQNEOF, ep);
579 return(EQN_ERR);
580 }
581 return(c);
582 }
583
584 for (i = 0; i < (int)EQNMARK__MAX; i++) {
585 if ( ! EQNSTREQ(&eqnmarks[i], start, sz))
586 continue;
587 if (NULL == last->last) {
588 EQN_MSG(MANDOCERR_EQNSYNT, ep);
589 return(EQN_ERR);
590 }
591 last->last->mark = (enum eqn_markt)i;
592 if (EQN_EOF == (c = eqn_box(ep, last))) {
593 EQN_MSG(MANDOCERR_EQNEOF, ep);
594 return(EQN_ERR);
595 }
596 return(c);
597 }
598
599 for (i = 0; i < (int)EQNFONT__MAX; i++) {
600 if ( ! EQNSTREQ(&eqnfonts[i], start, sz))
601 continue;
602 if (EQN_EOF == (c = eqn_box(ep, last))) {
603 EQN_MSG(MANDOCERR_EQNEOF, ep);
604 return(EQN_ERR);
605 } else if (EQN_OK == c)
606 last->last->font = (enum eqn_fontt)i;
607 return(c);
608 }
609
610 if (STRNEQ(start, sz, "size", 4)) {
611 if (NULL == (start = eqn_nexttok(ep, &sz))) {
612 EQN_MSG(MANDOCERR_EQNEOF, ep);
613 return(EQN_ERR);
614 }
615 size = mandoc_strntoi(start, sz, 10);
616 if (EQN_EOF == (c = eqn_box(ep, last))) {
617 EQN_MSG(MANDOCERR_EQNEOF, ep);
618 return(EQN_ERR);
619 } else if (EQN_OK != c)
620 return(c);
621 last->last->size = size;
622 }
623
624 bp = eqn_box_alloc(ep, last);
625 bp->type = EQN_TEXT;
626 for (i = 0; i < (int)EQNSYM__MAX; i++)
627 if (EQNSTREQ(&eqnsyms[i].str, start, sz)) {
628 sym[63] = '\0';
629 snprintf(sym, 62, "\\[%s]", eqnsyms[i].sym);
630 bp->text = mandoc_strdup(sym);
631 return(EQN_OK);
632 }
633
634 bp->text = mandoc_strndup(start, sz);
635 return(EQN_OK);
636 }
637
638 void
639 eqn_free(struct eqn_node *p)
640 {
641 int i;
642
643 eqn_box_free(p->eqn.root);
644
645 for (i = 0; i < (int)p->defsz; i++) {
646 free(p->defs[i].key);
647 free(p->defs[i].val);
648 }
649
650 free(p->eqn.name);
651 free(p->data);
652 free(p->defs);
653 free(p);
654 }
655
656 static struct eqn_box *
657 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
658 {
659 struct eqn_box *bp;
660
661 bp = mandoc_calloc(1, sizeof(struct eqn_box));
662 bp->parent = parent;
663 bp->size = ep->gsize;
664
665 if (NULL == parent->first)
666 parent->first = bp;
667 else
668 parent->last->next = bp;
669
670 parent->last = bp;
671 return(bp);
672 }
673
674 static void
675 eqn_box_free(struct eqn_box *bp)
676 {
677
678 if (bp->first)
679 eqn_box_free(bp->first);
680 if (bp->next)
681 eqn_box_free(bp->next);
682
683 free(bp->text);
684 free(bp->left);
685 free(bp->right);
686 free(bp);
687 }
688
689 static const char *
690 eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
691 {
692
693 return(eqn_next(ep, '"', sz, 0));
694 }
695
696 static const char *
697 eqn_nexttok(struct eqn_node *ep, size_t *sz)
698 {
699
700 return(eqn_next(ep, '"', sz, 1));
701 }
702
703 static void
704 eqn_rewind(struct eqn_node *ep)
705 {
706
707 ep->cur = ep->rew;
708 }
709
710 static const char *
711 eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
712 {
713 char *start, *next;
714 int q, diff, lim;
715 size_t ssz, dummy;
716 struct eqn_def *def;
717
718 if (NULL == sz)
719 sz = &dummy;
720
721 lim = 0;
722 ep->rew = ep->cur;
723 again:
724 /* Prevent self-definitions. */
725
726 if (lim >= EQN_NEST_MAX) {
727 EQN_MSG(MANDOCERR_ROFFLOOP, ep);
728 return(NULL);
729 }
730
731 ep->cur = ep->rew;
732 start = &ep->data[(int)ep->cur];
733 q = 0;
734
735 if ('\0' == *start)
736 return(NULL);
737
738 if (quote == *start) {
739 ep->cur++;
740 q = 1;
741 }
742
743 start = &ep->data[(int)ep->cur];
744
745 if ( ! q) {
746 if ('{' == *start || '}' == *start)
747 ssz = 1;
748 else
749 ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
750 next = start + (int)ssz;
751 if ('\0' == *next)
752 next = NULL;
753 } else
754 next = strchr(start, quote);
755
756 if (NULL != next) {
757 *sz = (size_t)(next - start);
758 ep->cur += *sz;
759 if (q)
760 ep->cur++;
761 while (' ' == ep->data[(int)ep->cur] ||
762 '\t' == ep->data[(int)ep->cur] ||
763 '^' == ep->data[(int)ep->cur] ||
764 '~' == ep->data[(int)ep->cur])
765 ep->cur++;
766 } else {
767 if (q)
768 EQN_MSG(MANDOCERR_BADQUOTE, ep);
769 next = strchr(start, '\0');
770 *sz = (size_t)(next - start);
771 ep->cur += *sz;
772 }
773
774 /* Quotes aren't expanded for values. */
775
776 if (q || ! repl)
777 return(start);
778
779 if (NULL != (def = eqn_def_find(ep, start, *sz))) {
780 diff = def->valsz - *sz;
781
782 if (def->valsz > *sz) {
783 ep->sz += diff;
784 ep->data = mandoc_realloc(ep->data, ep->sz + 1);
785 ep->data[ep->sz] = '\0';
786 start = &ep->data[(int)ep->rew];
787 }
788
789 diff = def->valsz - *sz;
790 memmove(start + *sz + diff, start + *sz,
791 (strlen(start) - *sz) + 1);
792 memcpy(start, def->val, def->valsz);
793 goto again;
794 }
795
796 return(start);
797 }
798
799 static int
800 eqn_do_ign1(struct eqn_node *ep)
801 {
802
803 if (NULL == eqn_nextrawtok(ep, NULL))
804 EQN_MSG(MANDOCERR_EQNEOF, ep);
805 else
806 return(1);
807
808 return(0);
809 }
810
811 static int
812 eqn_do_ign2(struct eqn_node *ep)
813 {
814
815 if (NULL == eqn_nextrawtok(ep, NULL))
816 EQN_MSG(MANDOCERR_EQNEOF, ep);
817 else if (NULL == eqn_nextrawtok(ep, NULL))
818 EQN_MSG(MANDOCERR_EQNEOF, ep);
819 else
820 return(1);
821
822 return(0);
823 }
824
825 static int
826 eqn_do_tdefine(struct eqn_node *ep)
827 {
828
829 if (NULL == eqn_nextrawtok(ep, NULL))
830 EQN_MSG(MANDOCERR_EQNEOF, ep);
831 else if (NULL == eqn_next(ep, ep->data[(int)ep->cur], NULL, 0))
832 EQN_MSG(MANDOCERR_EQNEOF, ep);
833 else
834 return(1);
835
836 return(0);
837 }
838
839 static int
840 eqn_do_define(struct eqn_node *ep)
841 {
842 const char *start;
843 size_t sz;
844 struct eqn_def *def;
845 int i;
846
847 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
848 EQN_MSG(MANDOCERR_EQNEOF, ep);
849 return(0);
850 }
851
852 /*
853 * Search for a key that already exists.
854 * Create a new key if none is found.
855 */
856
857 if (NULL == (def = eqn_def_find(ep, start, sz))) {
858 /* Find holes in string array. */
859 for (i = 0; i < (int)ep->defsz; i++)
860 if (0 == ep->defs[i].keysz)
861 break;
862
863 if (i == (int)ep->defsz) {
864 ep->defsz++;
865 ep->defs = mandoc_realloc
866 (ep->defs, ep->defsz *
867 sizeof(struct eqn_def));
868 ep->defs[i].key = ep->defs[i].val = NULL;
869 }
870
871 ep->defs[i].keysz = sz;
872 ep->defs[i].key = mandoc_realloc
873 (ep->defs[i].key, sz + 1);
874
875 memcpy(ep->defs[i].key, start, sz);
876 ep->defs[i].key[(int)sz] = '\0';
877 def = &ep->defs[i];
878 }
879
880 start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
881
882 if (NULL == start) {
883 EQN_MSG(MANDOCERR_EQNEOF, ep);
884 return(0);
885 }
886
887 def->valsz = sz;
888 def->val = mandoc_realloc(def->val, sz + 1);
889 memcpy(def->val, start, sz);
890 def->val[(int)sz] = '\0';
891 return(1);
892 }
893
894 static int
895 eqn_do_gfont(struct eqn_node *ep)
896 {
897
898 if (NULL == eqn_nextrawtok(ep, NULL)) {
899 EQN_MSG(MANDOCERR_EQNEOF, ep);
900 return(0);
901 }
902 return(1);
903 }
904
905 static int
906 eqn_do_gsize(struct eqn_node *ep)
907 {
908 const char *start;
909 size_t sz;
910
911 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
912 EQN_MSG(MANDOCERR_EQNEOF, ep);
913 return(0);
914 }
915 ep->gsize = mandoc_strntoi(start, sz, 10);
916 return(1);
917 }
918
919 static int
920 eqn_do_undef(struct eqn_node *ep)
921 {
922 const char *start;
923 struct eqn_def *def;
924 size_t sz;
925
926 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
927 EQN_MSG(MANDOCERR_EQNEOF, ep);
928 return(0);
929 } else if (NULL != (def = eqn_def_find(ep, start, sz)))
930 def->keysz = 0;
931
932 return(1);
933 }
934
935 static struct eqn_def *
936 eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
937 {
938 int i;
939
940 for (i = 0; i < (int)ep->defsz; i++)
941 if (ep->defs[i].keysz && STRNEQ(ep->defs[i].key,
942 ep->defs[i].keysz, key, sz))
943 return(&ep->defs[i]);
944
945 return(NULL);
946 }