]> git.cameronkatri.com Git - mandoc.git/blob - eqn.c
Start systematic improvements of error reporting.
[mandoc.git] / eqn.c
1 /* $Id: eqn.c,v 1.42 2014/04/23 21:06:41 schwarze 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 "mandoc_aux.h"
30 #include "libmandoc.h"
31 #include "libroff.h"
32
33 #define EQN_NEST_MAX 128 /* maximum nesting of defines */
34 #define EQN_MSG(t, x) mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL)
35
36 enum eqn_rest {
37 EQN_DESCOPE,
38 EQN_ERR,
39 EQN_OK,
40 EQN_EOF
41 };
42
43 enum eqn_symt {
44 EQNSYM_alpha,
45 EQNSYM_beta,
46 EQNSYM_chi,
47 EQNSYM_delta,
48 EQNSYM_epsilon,
49 EQNSYM_eta,
50 EQNSYM_gamma,
51 EQNSYM_iota,
52 EQNSYM_kappa,
53 EQNSYM_lambda,
54 EQNSYM_mu,
55 EQNSYM_nu,
56 EQNSYM_omega,
57 EQNSYM_omicron,
58 EQNSYM_phi,
59 EQNSYM_pi,
60 EQNSYM_ps,
61 EQNSYM_rho,
62 EQNSYM_sigma,
63 EQNSYM_tau,
64 EQNSYM_theta,
65 EQNSYM_upsilon,
66 EQNSYM_xi,
67 EQNSYM_zeta,
68 EQNSYM_DELTA,
69 EQNSYM_GAMMA,
70 EQNSYM_LAMBDA,
71 EQNSYM_OMEGA,
72 EQNSYM_PHI,
73 EQNSYM_PI,
74 EQNSYM_PSI,
75 EQNSYM_SIGMA,
76 EQNSYM_THETA,
77 EQNSYM_UPSILON,
78 EQNSYM_XI,
79 EQNSYM_inter,
80 EQNSYM_union,
81 EQNSYM_prod,
82 EQNSYM_int,
83 EQNSYM_sum,
84 EQNSYM_grad,
85 EQNSYM_del,
86 EQNSYM_times,
87 EQNSYM_cdot,
88 EQNSYM_nothing,
89 EQNSYM_approx,
90 EQNSYM_prime,
91 EQNSYM_half,
92 EQNSYM_partial,
93 EQNSYM_inf,
94 EQNSYM_muchgreat,
95 EQNSYM_muchless,
96 EQNSYM_larrow,
97 EQNSYM_rarrow,
98 EQNSYM_pm,
99 EQNSYM_nequal,
100 EQNSYM_equiv,
101 EQNSYM_lessequal,
102 EQNSYM_moreequal,
103 EQNSYM__MAX
104 };
105
106 enum eqnpartt {
107 EQN_DEFINE = 0,
108 EQN_NDEFINE,
109 EQN_TDEFINE,
110 EQN_SET,
111 EQN_UNDEF,
112 EQN_GFONT,
113 EQN_GSIZE,
114 EQN_BACK,
115 EQN_FWD,
116 EQN_UP,
117 EQN_DOWN,
118 EQN__MAX
119 };
120
121 struct eqnstr {
122 const char *name;
123 size_t sz;
124 };
125
126 #define STRNEQ(p1, sz1, p2, sz2) \
127 ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
128 #define EQNSTREQ(x, p, sz) \
129 STRNEQ((x)->name, (x)->sz, (p), (sz))
130
131 struct eqnpart {
132 struct eqnstr str;
133 int (*fp)(struct eqn_node *);
134 };
135
136 struct eqnsym {
137 struct eqnstr str;
138 const char *sym;
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
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(epp);
298 p += 3;
299 while (' ' == *p || '\t' == *p)
300 p++;
301 if ('\0' == *p)
302 return(er);
303 mandoc_msg(MANDOCERR_ARGSLOST, ep->parse, ln, pos, NULL);
304 return(er);
305 }
306
307 /*
308 * Build up the full string, replacing all newlines with regular
309 * whitespace.
310 */
311
312 sz = strlen(p + pos) + 1;
313 ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
314
315 /* First invocation: nil terminate the string. */
316
317 if (0 == ep->sz)
318 *ep->data = '\0';
319
320 ep->sz += sz;
321 strlcat(ep->data, p + pos, ep->sz + 1);
322 strlcat(ep->data, " ", ep->sz + 1);
323 return(ROFF_IGN);
324 }
325
326 struct eqn_node *
327 eqn_alloc(const char *name, int pos, int line, struct mparse *parse)
328 {
329 struct eqn_node *p;
330 size_t sz;
331 const char *end;
332
333 p = mandoc_calloc(1, sizeof(struct eqn_node));
334
335 if (name && '\0' != *name) {
336 sz = strlen(name);
337 assert(sz);
338 do {
339 sz--;
340 end = name + (int)sz;
341 } while (' ' == *end || '\t' == *end);
342 p->eqn.name = mandoc_strndup(name, sz + 1);
343 }
344
345 p->parse = parse;
346 p->eqn.ln = line;
347 p->eqn.pos = pos;
348 p->gsize = EQN_DEFSIZE;
349
350 return(p);
351 }
352
353 enum rofferr
354 eqn_end(struct eqn_node **epp)
355 {
356 struct eqn_node *ep;
357 struct eqn_box *root;
358 enum eqn_rest c;
359
360 ep = *epp;
361 *epp = NULL;
362
363 ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
364
365 root = ep->eqn.root;
366 root->type = EQN_ROOT;
367
368 if (0 == ep->sz)
369 return(ROFF_IGN);
370
371 if (EQN_DESCOPE == (c = eqn_eqn(ep, root))) {
372 EQN_MSG(MANDOCERR_EQNNSCOPE, ep);
373 c = EQN_ERR;
374 }
375
376 return(EQN_EOF == c ? ROFF_EQN : ROFF_IGN);
377 }
378
379 static enum eqn_rest
380 eqn_eqn(struct eqn_node *ep, struct eqn_box *last)
381 {
382 struct eqn_box *bp;
383 enum eqn_rest c;
384
385 bp = eqn_box_alloc(ep, last);
386 bp->type = EQN_SUBEXPR;
387
388 while (EQN_OK == (c = eqn_box(ep, bp)))
389 /* Spin! */ ;
390
391 return(c);
392 }
393
394 static enum eqn_rest
395 eqn_matrix(struct eqn_node *ep, struct eqn_box *last)
396 {
397 struct eqn_box *bp;
398 const char *start;
399 size_t sz;
400 enum eqn_rest c;
401
402 bp = eqn_box_alloc(ep, last);
403 bp->type = EQN_MATRIX;
404
405 if (NULL == (start = eqn_nexttok(ep, &sz))) {
406 EQN_MSG(MANDOCERR_EQNEOF, ep);
407 return(EQN_ERR);
408 }
409 if ( ! STRNEQ(start, sz, "{", 1)) {
410 EQN_MSG(MANDOCERR_EQNSYNT, ep);
411 return(EQN_ERR);
412 }
413
414 while (EQN_OK == (c = eqn_box(ep, bp)))
415 switch (bp->last->pile) {
416 case EQNPILE_LCOL:
417 /* FALLTHROUGH */
418 case EQNPILE_CCOL:
419 /* FALLTHROUGH */
420 case EQNPILE_RCOL:
421 continue;
422 default:
423 EQN_MSG(MANDOCERR_EQNSYNT, ep);
424 return(EQN_ERR);
425 };
426
427 if (EQN_DESCOPE != c) {
428 if (EQN_EOF == c)
429 EQN_MSG(MANDOCERR_EQNEOF, ep);
430 return(EQN_ERR);
431 }
432
433 eqn_rewind(ep);
434 start = eqn_nexttok(ep, &sz);
435 assert(start);
436 if (STRNEQ(start, sz, "}", 1))
437 return(EQN_OK);
438
439 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
440 return(EQN_ERR);
441 }
442
443 static enum eqn_rest
444 eqn_list(struct eqn_node *ep, struct eqn_box *last)
445 {
446 struct eqn_box *bp;
447 const char *start;
448 size_t sz;
449 enum eqn_rest c;
450
451 bp = eqn_box_alloc(ep, last);
452 bp->type = EQN_LIST;
453
454 if (NULL == (start = eqn_nexttok(ep, &sz))) {
455 EQN_MSG(MANDOCERR_EQNEOF, ep);
456 return(EQN_ERR);
457 }
458 if ( ! STRNEQ(start, sz, "{", 1)) {
459 EQN_MSG(MANDOCERR_EQNSYNT, ep);
460 return(EQN_ERR);
461 }
462
463 while (EQN_DESCOPE == (c = eqn_eqn(ep, bp))) {
464 eqn_rewind(ep);
465 start = eqn_nexttok(ep, &sz);
466 assert(start);
467 if ( ! STRNEQ(start, sz, "above", 5))
468 break;
469 }
470
471 if (EQN_DESCOPE != c) {
472 if (EQN_ERR != c)
473 EQN_MSG(MANDOCERR_EQNSCOPE, ep);
474 return(EQN_ERR);
475 }
476
477 eqn_rewind(ep);
478 start = eqn_nexttok(ep, &sz);
479 assert(start);
480 if (STRNEQ(start, sz, "}", 1))
481 return(EQN_OK);
482
483 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
484 return(EQN_ERR);
485 }
486
487 static enum eqn_rest
488 eqn_box(struct eqn_node *ep, struct eqn_box *last)
489 {
490 size_t sz;
491 const char *start;
492 char *left;
493 char sym[64];
494 enum eqn_rest c;
495 int i, size;
496 struct eqn_box *bp;
497
498 if (NULL == (start = eqn_nexttok(ep, &sz)))
499 return(EQN_EOF);
500
501 if (STRNEQ(start, sz, "}", 1))
502 return(EQN_DESCOPE);
503 else if (STRNEQ(start, sz, "right", 5))
504 return(EQN_DESCOPE);
505 else if (STRNEQ(start, sz, "above", 5))
506 return(EQN_DESCOPE);
507 else if (STRNEQ(start, sz, "mark", 4))
508 return(EQN_OK);
509 else if (STRNEQ(start, sz, "lineup", 6))
510 return(EQN_OK);
511
512 for (i = 0; i < (int)EQN__MAX; i++) {
513 if ( ! EQNSTREQ(&eqnparts[i].str, start, sz))
514 continue;
515 return((*eqnparts[i].fp)(ep) ? EQN_OK : EQN_ERR);
516 }
517
518 if (STRNEQ(start, sz, "{", 1)) {
519 if (EQN_DESCOPE != (c = eqn_eqn(ep, last))) {
520 if (EQN_ERR != c)
521 EQN_MSG(MANDOCERR_EQNSCOPE, ep);
522 return(EQN_ERR);
523 }
524 eqn_rewind(ep);
525 start = eqn_nexttok(ep, &sz);
526 assert(start);
527 if (STRNEQ(start, sz, "}", 1))
528 return(EQN_OK);
529 EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
530 return(EQN_ERR);
531 }
532
533 for (i = 0; i < (int)EQNPILE__MAX; i++) {
534 if ( ! EQNSTREQ(&eqnpiles[i], start, sz))
535 continue;
536 if (EQN_OK == (c = eqn_list(ep, last)))
537 last->last->pile = (enum eqn_pilet)i;
538 return(c);
539 }
540
541 if (STRNEQ(start, sz, "matrix", 6))
542 return(eqn_matrix(ep, last));
543
544 if (STRNEQ(start, sz, "left", 4)) {
545 if (NULL == (start = eqn_nexttok(ep, &sz))) {
546 EQN_MSG(MANDOCERR_EQNEOF, ep);
547 return(EQN_ERR);
548 }
549 left = mandoc_strndup(start, sz);
550 c = eqn_eqn(ep, last);
551 if (last->last)
552 last->last->left = left;
553 else
554 free(left);
555 if (EQN_DESCOPE != c)
556 return(c);
557 assert(last->last);
558 eqn_rewind(ep);
559 start = eqn_nexttok(ep, &sz);
560 assert(start);
561 if ( ! STRNEQ(start, sz, "right", 5))
562 return(EQN_DESCOPE);
563 if (NULL == (start = eqn_nexttok(ep, &sz))) {
564 EQN_MSG(MANDOCERR_EQNEOF, ep);
565 return(EQN_ERR);
566 }
567 last->last->right = mandoc_strndup(start, sz);
568 return(EQN_OK);
569 }
570
571 for (i = 0; i < (int)EQNPOS__MAX; i++) {
572 if ( ! EQNSTREQ(&eqnposs[i], start, sz))
573 continue;
574 if (NULL == last->last) {
575 EQN_MSG(MANDOCERR_EQNSYNT, ep);
576 return(EQN_ERR);
577 }
578 last->last->pos = (enum eqn_post)i;
579 if (EQN_EOF == (c = eqn_box(ep, last))) {
580 EQN_MSG(MANDOCERR_EQNEOF, ep);
581 return(EQN_ERR);
582 }
583 return(c);
584 }
585
586 for (i = 0; i < (int)EQNMARK__MAX; i++) {
587 if ( ! EQNSTREQ(&eqnmarks[i], start, sz))
588 continue;
589 if (NULL == last->last) {
590 EQN_MSG(MANDOCERR_EQNSYNT, ep);
591 return(EQN_ERR);
592 }
593 last->last->mark = (enum eqn_markt)i;
594 if (EQN_EOF == (c = eqn_box(ep, last))) {
595 EQN_MSG(MANDOCERR_EQNEOF, ep);
596 return(EQN_ERR);
597 }
598 return(c);
599 }
600
601 for (i = 0; i < (int)EQNFONT__MAX; i++) {
602 if ( ! EQNSTREQ(&eqnfonts[i], start, sz))
603 continue;
604 if (EQN_EOF == (c = eqn_box(ep, last))) {
605 EQN_MSG(MANDOCERR_EQNEOF, ep);
606 return(EQN_ERR);
607 } else if (EQN_OK == c)
608 last->last->font = (enum eqn_fontt)i;
609 return(c);
610 }
611
612 if (STRNEQ(start, sz, "size", 4)) {
613 if (NULL == (start = eqn_nexttok(ep, &sz))) {
614 EQN_MSG(MANDOCERR_EQNEOF, ep);
615 return(EQN_ERR);
616 }
617 size = mandoc_strntoi(start, sz, 10);
618 if (EQN_EOF == (c = eqn_box(ep, last))) {
619 EQN_MSG(MANDOCERR_EQNEOF, ep);
620 return(EQN_ERR);
621 } else if (EQN_OK != c)
622 return(c);
623 last->last->size = size;
624 }
625
626 bp = eqn_box_alloc(ep, last);
627 bp->type = EQN_TEXT;
628 for (i = 0; i < (int)EQNSYM__MAX; i++)
629 if (EQNSTREQ(&eqnsyms[i].str, start, sz)) {
630 sym[63] = '\0';
631 (void)snprintf(sym, 62, "\\[%s]", eqnsyms[i].sym);
632 bp->text = mandoc_strdup(sym);
633 return(EQN_OK);
634 }
635
636 bp->text = mandoc_strndup(start, sz);
637 return(EQN_OK);
638 }
639
640 void
641 eqn_free(struct eqn_node *p)
642 {
643 int i;
644
645 eqn_box_free(p->eqn.root);
646
647 for (i = 0; i < (int)p->defsz; i++) {
648 free(p->defs[i].key);
649 free(p->defs[i].val);
650 }
651
652 free(p->eqn.name);
653 free(p->data);
654 free(p->defs);
655 free(p);
656 }
657
658 static struct eqn_box *
659 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
660 {
661 struct eqn_box *bp;
662
663 bp = mandoc_calloc(1, sizeof(struct eqn_box));
664 bp->parent = parent;
665 bp->size = ep->gsize;
666
667 if (NULL == parent->first)
668 parent->first = bp;
669 else
670 parent->last->next = bp;
671
672 parent->last = bp;
673 return(bp);
674 }
675
676 static void
677 eqn_box_free(struct eqn_box *bp)
678 {
679
680 if (bp->first)
681 eqn_box_free(bp->first);
682 if (bp->next)
683 eqn_box_free(bp->next);
684
685 free(bp->text);
686 free(bp->left);
687 free(bp->right);
688 free(bp);
689 }
690
691 static const char *
692 eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
693 {
694
695 return(eqn_next(ep, '"', sz, 0));
696 }
697
698 static const char *
699 eqn_nexttok(struct eqn_node *ep, size_t *sz)
700 {
701
702 return(eqn_next(ep, '"', sz, 1));
703 }
704
705 static void
706 eqn_rewind(struct eqn_node *ep)
707 {
708
709 ep->cur = ep->rew;
710 }
711
712 static const char *
713 eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
714 {
715 char *start, *next;
716 int q, diff, lim;
717 size_t ssz, dummy;
718 struct eqn_def *def;
719
720 if (NULL == sz)
721 sz = &dummy;
722
723 lim = 0;
724 ep->rew = ep->cur;
725 again:
726 /* Prevent self-definitions. */
727
728 if (lim >= EQN_NEST_MAX) {
729 EQN_MSG(MANDOCERR_ROFFLOOP, ep);
730 return(NULL);
731 }
732
733 ep->cur = ep->rew;
734 start = &ep->data[(int)ep->cur];
735 q = 0;
736
737 if ('\0' == *start)
738 return(NULL);
739
740 if (quote == *start) {
741 ep->cur++;
742 q = 1;
743 }
744
745 start = &ep->data[(int)ep->cur];
746
747 if ( ! q) {
748 if ('{' == *start || '}' == *start)
749 ssz = 1;
750 else
751 ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
752 next = start + (int)ssz;
753 if ('\0' == *next)
754 next = NULL;
755 } else
756 next = strchr(start, quote);
757
758 if (NULL != next) {
759 *sz = (size_t)(next - start);
760 ep->cur += *sz;
761 if (q)
762 ep->cur++;
763 while (' ' == ep->data[(int)ep->cur] ||
764 '\t' == ep->data[(int)ep->cur] ||
765 '^' == ep->data[(int)ep->cur] ||
766 '~' == ep->data[(int)ep->cur])
767 ep->cur++;
768 } else {
769 if (q)
770 EQN_MSG(MANDOCERR_BADQUOTE, ep);
771 next = strchr(start, '\0');
772 *sz = (size_t)(next - start);
773 ep->cur += *sz;
774 }
775
776 /* Quotes aren't expanded for values. */
777
778 if (q || ! repl)
779 return(start);
780
781 if (NULL != (def = eqn_def_find(ep, start, *sz))) {
782 diff = def->valsz - *sz;
783
784 if (def->valsz > *sz) {
785 ep->sz += diff;
786 ep->data = mandoc_realloc(ep->data, ep->sz + 1);
787 ep->data[ep->sz] = '\0';
788 start = &ep->data[(int)ep->rew];
789 }
790
791 diff = def->valsz - *sz;
792 memmove(start + *sz + diff, start + *sz,
793 (strlen(start) - *sz) + 1);
794 memcpy(start, def->val, def->valsz);
795 goto again;
796 }
797
798 return(start);
799 }
800
801 static int
802 eqn_do_ign1(struct eqn_node *ep)
803 {
804
805 if (NULL == eqn_nextrawtok(ep, NULL))
806 EQN_MSG(MANDOCERR_EQNEOF, ep);
807 else
808 return(1);
809
810 return(0);
811 }
812
813 static int
814 eqn_do_ign2(struct eqn_node *ep)
815 {
816
817 if (NULL == eqn_nextrawtok(ep, NULL))
818 EQN_MSG(MANDOCERR_EQNEOF, ep);
819 else if (NULL == eqn_nextrawtok(ep, NULL))
820 EQN_MSG(MANDOCERR_EQNEOF, ep);
821 else
822 return(1);
823
824 return(0);
825 }
826
827 static int
828 eqn_do_tdefine(struct eqn_node *ep)
829 {
830
831 if (NULL == eqn_nextrawtok(ep, NULL))
832 EQN_MSG(MANDOCERR_EQNEOF, ep);
833 else if (NULL == eqn_next(ep, ep->data[(int)ep->cur], NULL, 0))
834 EQN_MSG(MANDOCERR_EQNEOF, ep);
835 else
836 return(1);
837
838 return(0);
839 }
840
841 static int
842 eqn_do_define(struct eqn_node *ep)
843 {
844 const char *start;
845 size_t sz;
846 struct eqn_def *def;
847 int i;
848
849 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
850 EQN_MSG(MANDOCERR_EQNEOF, ep);
851 return(0);
852 }
853
854 /*
855 * Search for a key that already exists.
856 * Create a new key if none is found.
857 */
858
859 if (NULL == (def = eqn_def_find(ep, start, sz))) {
860 /* Find holes in string array. */
861 for (i = 0; i < (int)ep->defsz; i++)
862 if (0 == ep->defs[i].keysz)
863 break;
864
865 if (i == (int)ep->defsz) {
866 ep->defsz++;
867 ep->defs = mandoc_reallocarray(ep->defs,
868 ep->defsz, sizeof(struct eqn_def));
869 ep->defs[i].key = ep->defs[i].val = NULL;
870 }
871
872 ep->defs[i].keysz = sz;
873 ep->defs[i].key = mandoc_realloc(
874 ep->defs[i].key, sz + 1);
875
876 memcpy(ep->defs[i].key, start, sz);
877 ep->defs[i].key[(int)sz] = '\0';
878 def = &ep->defs[i];
879 }
880
881 start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
882
883 if (NULL == start) {
884 EQN_MSG(MANDOCERR_EQNEOF, ep);
885 return(0);
886 }
887
888 def->valsz = sz;
889 def->val = mandoc_realloc(def->val, sz + 1);
890 memcpy(def->val, start, sz);
891 def->val[(int)sz] = '\0';
892 return(1);
893 }
894
895 static int
896 eqn_do_gfont(struct eqn_node *ep)
897 {
898
899 if (NULL == eqn_nextrawtok(ep, NULL)) {
900 EQN_MSG(MANDOCERR_EQNEOF, ep);
901 return(0);
902 }
903 return(1);
904 }
905
906 static int
907 eqn_do_gsize(struct eqn_node *ep)
908 {
909 const char *start;
910 size_t sz;
911
912 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
913 EQN_MSG(MANDOCERR_EQNEOF, ep);
914 return(0);
915 }
916 ep->gsize = mandoc_strntoi(start, sz, 10);
917 return(1);
918 }
919
920 static int
921 eqn_do_undef(struct eqn_node *ep)
922 {
923 const char *start;
924 struct eqn_def *def;
925 size_t sz;
926
927 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
928 EQN_MSG(MANDOCERR_EQNEOF, ep);
929 return(0);
930 } else if (NULL != (def = eqn_def_find(ep, start, sz)))
931 def->keysz = 0;
932
933 return(1);
934 }
935
936 static struct eqn_def *
937 eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
938 {
939 int i;
940
941 for (i = 0; i < (int)ep->defsz; i++)
942 if (ep->defs[i].keysz && STRNEQ(ep->defs[i].key,
943 ep->defs[i].keysz, key, sz))
944 return(&ep->defs[i]);
945
946 return(NULL);
947 }