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