1 /* $Id: eqn.c,v 1.56 2014/10/25 15:06:30 schwarze Exp $ */
3 * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
30 #include "mandoc_aux.h"
31 #include "libmandoc.h"
34 #define EQN_MSG(t, x) \
35 mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL)
36 #define EQN_NEST_MAX 128 /* maximum nesting of defines */
37 #define STRNEQ(p1, sz1, p2, sz2) \
38 ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
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 NULL, /* EQN_TOK_EOF */
136 "above", /* EQN_TOK_ABOVE */
207 static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
208 { "alpha", "*a" }, /* EQNSYM_alpha */
209 { "beta", "*b" }, /* EQNSYM_beta */
210 { "chi", "*x" }, /* EQNSYM_chi */
211 { "delta", "*d" }, /* EQNSYM_delta */
212 { "epsilon", "*e" }, /* EQNSYM_epsilon */
213 { "eta", "*y" }, /* EQNSYM_eta */
214 { "gamma", "*g" }, /* EQNSYM_gamma */
215 { "iota", "*i" }, /* EQNSYM_iota */
216 { "kappa", "*k" }, /* EQNSYM_kappa */
217 { "lambda", "*l" }, /* EQNSYM_lambda */
218 { "mu", "*m" }, /* EQNSYM_mu */
219 { "nu", "*n" }, /* EQNSYM_nu */
220 { "omega", "*w" }, /* EQNSYM_omega */
221 { "omicron", "*o" }, /* EQNSYM_omicron */
222 { "phi", "*f" }, /* EQNSYM_phi */
223 { "pi", "*p" }, /* EQNSYM_pi */
224 { "psi", "*q" }, /* EQNSYM_psi */
225 { "rho", "*r" }, /* EQNSYM_rho */
226 { "sigma", "*s" }, /* EQNSYM_sigma */
227 { "tau", "*t" }, /* EQNSYM_tau */
228 { "theta", "*h" }, /* EQNSYM_theta */
229 { "upsilon", "*u" }, /* EQNSYM_upsilon */
230 { "xi", "*c" }, /* EQNSYM_xi */
231 { "zeta", "*z" }, /* EQNSYM_zeta */
232 { "DELTA", "*D" }, /* EQNSYM_DELTA */
233 { "GAMMA", "*G" }, /* EQNSYM_GAMMA */
234 { "LAMBDA", "*L" }, /* EQNSYM_LAMBDA */
235 { "OMEGA", "*W" }, /* EQNSYM_OMEGA */
236 { "PHI", "*F" }, /* EQNSYM_PHI */
237 { "PI", "*P" }, /* EQNSYM_PI */
238 { "PSI", "*Q" }, /* EQNSYM_PSI */
239 { "SIGMA", "*S" }, /* EQNSYM_SIGMA */
240 { "THETA", "*H" }, /* EQNSYM_THETA */
241 { "UPSILON", "*U" }, /* EQNSYM_UPSILON */
242 { "XI", "*C" }, /* EQNSYM_XI */
243 { "inter", "ca" }, /* EQNSYM_inter */
244 { "union", "cu" }, /* EQNSYM_union */
245 { "prod", "product" }, /* EQNSYM_prod */
246 { "int", "integral" }, /* EQNSYM_int */
247 { "sum", "sum" }, /* EQNSYM_sum */
248 { "grad", "gr" }, /* EQNSYM_grad */
249 { "del", "gr" }, /* EQNSYM_del */
250 { "times", "mu" }, /* EQNSYM_times */
251 { "cdot", "pc" }, /* EQNSYM_cdot */
252 { "nothing", "&" }, /* EQNSYM_nothing */
253 { "approx", "~~" }, /* EQNSYM_approx */
254 { "prime", "aq" }, /* EQNSYM_prime */
255 { "half", "12" }, /* EQNSYM_half */
256 { "partial", "pd" }, /* EQNSYM_partial */
257 { "inf", "if" }, /* EQNSYM_inf */
258 { ">>", ">>" }, /* EQNSYM_muchgreat */
259 { "<<", "<<" }, /* EQNSYM_muchless */
260 { "<-", "<-" }, /* EQNSYM_larrow */
261 { "->", "->" }, /* EQNSYM_rarrow */
262 { "+-", "+-" }, /* EQNSYM_pm */
263 { "!=", "!=" }, /* EQNSYM_nequal */
264 { "==", "==" }, /* EQNSYM_equiv */
265 { "<=", "<=" }, /* EQNSYM_lessequal */
266 { ">=", ">=" }, /* EQNSYM_moreequal */
270 eqn_read(struct eqn_node **epp, int ln,
271 const char *p, int pos, int *offs)
280 * If we're the terminating mark, unset our equation status and
281 * validate the full equation.
284 if (0 == strncmp(p, ".EN", 3)) {
287 while (' ' == *p || '\t' == *p)
291 mandoc_vmsg(MANDOCERR_ARG_SKIP, ep->parse,
292 ln, pos, "EN %s", p);
297 * Build up the full string, replacing all newlines with regular
301 sz = strlen(p + pos) + 1;
302 ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
304 /* First invocation: nil terminate the string. */
310 strlcat(ep->data, p + pos, ep->sz + 1);
311 strlcat(ep->data, " ", ep->sz + 1);
316 eqn_alloc(int pos, int line, struct mparse *parse)
320 p = mandoc_calloc(1, sizeof(struct eqn_node));
325 p->gsize = EQN_DEFSIZE;
331 * Find the key "key" of the give size within our eqn-defined values.
333 static struct eqn_def *
334 eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
338 for (i = 0; i < (int)ep->defsz; i++)
339 if (ep->defs[i].keysz && STRNEQ(ep->defs[i].key,
340 ep->defs[i].keysz, key, sz))
341 return(&ep->defs[i]);
347 * Get the next token from the input stream using the given quote
349 * Optionally make any replacements.
352 eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
365 /* Prevent self-definitions. */
367 if (lim >= EQN_NEST_MAX) {
368 EQN_MSG(MANDOCERR_ROFFLOOP, ep);
373 start = &ep->data[(int)ep->cur];
379 if (quote == *start) {
384 start = &ep->data[(int)ep->cur];
387 if ('{' == *start || '}' == *start)
390 ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
391 next = start + (int)ssz;
395 next = strchr(start, quote);
398 *sz = (size_t)(next - start);
402 while (' ' == ep->data[(int)ep->cur] ||
403 '\t' == ep->data[(int)ep->cur] ||
404 '^' == ep->data[(int)ep->cur] ||
405 '~' == ep->data[(int)ep->cur])
409 EQN_MSG(MANDOCERR_ARG_QUOTE, ep);
410 next = strchr(start, '\0');
411 *sz = (size_t)(next - start);
415 /* Quotes aren't expanded for values. */
420 if (NULL != (def = eqn_def_find(ep, start, *sz))) {
421 diff = def->valsz - *sz;
423 if (def->valsz > *sz) {
425 ep->data = mandoc_realloc(ep->data, ep->sz + 1);
426 ep->data[ep->sz] = '\0';
427 start = &ep->data[(int)ep->rew];
430 diff = def->valsz - *sz;
431 memmove(start + *sz + diff, start + *sz,
432 (strlen(start) - *sz) + 1);
433 memcpy(start, def->val, def->valsz);
441 * Get the next delimited token using the default current quote
445 eqn_nexttok(struct eqn_node *ep, size_t *sz)
448 return(eqn_next(ep, '"', sz, 1));
452 * Get next token without replacement.
455 eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
458 return(eqn_next(ep, '"', sz, 0));
462 * Parse a token from the stream of text.
463 * A token consists of one of the recognised eqn(7) strings.
464 * Strings are separated by delimiting marks.
465 * This returns EQN_TOK_EOF when there are no more tokens.
466 * If the token is an unrecognised string literal, then it returns
467 * EQN_TOK__MAX and sets the "p" pointer to an allocated, nil-terminated
469 * This must be later freed with free(3).
472 eqn_tok_parse(struct eqn_node *ep, char **p)
481 quoted = ep->data[ep->cur] == '"';
483 if (NULL == (start = eqn_nexttok(ep, &sz)))
488 *p = mandoc_strndup(start, sz);
489 return(EQN_TOK__MAX);
492 for (i = 0; i < EQN_TOK__MAX; i++) {
493 if (NULL == eqn_toks[i])
495 if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i])))
499 if (i == EQN_TOK__MAX && NULL != p)
500 *p = mandoc_strndup(start, sz);
506 eqn_box_free(struct eqn_box *bp)
510 eqn_box_free(bp->first);
512 eqn_box_free(bp->next);
523 * Allocate a box as the last child of the parent node.
525 static struct eqn_box *
526 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
530 bp = mandoc_calloc(1, sizeof(struct eqn_box));
533 bp->expectargs = UINT_MAX;
534 bp->size = ep->gsize;
536 if (NULL != parent->first) {
537 parent->last->next = bp;
538 bp->prev = parent->last;
547 * Reparent the current last node (of the current parent) under a new
548 * EQN_SUBEXPR as the first element.
549 * Then return the new parent.
550 * The new EQN_SUBEXPR will have a two-child limit.
552 static struct eqn_box *
553 eqn_box_makebinary(struct eqn_node *ep,
554 enum eqn_post pos, struct eqn_box *parent)
556 struct eqn_box *b, *newb;
558 assert(NULL != parent->last);
560 if (parent->last == parent->first)
561 parent->first = NULL;
563 parent->last = b->prev;
565 newb = eqn_box_alloc(ep, parent);
567 newb->type = EQN_SUBEXPR;
568 newb->expectargs = 2;
570 newb->first = newb->last = b;
571 newb->first->next = NULL;
577 * Parse the "delim" control statement.
580 eqn_delim(struct eqn_node *ep)
585 if ((start = eqn_nextrawtok(ep, &sz)) == NULL)
586 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
587 ep->eqn.ln, ep->eqn.pos, "delim");
588 else if (strncmp(start, "off", 3) == 0)
590 else if (strncmp(start, "on", 2) == 0) {
591 if (ep->odelim && ep->cdelim)
593 } else if (start[1] != '\0') {
594 ep->odelim = start[0];
595 ep->cdelim = start[1];
601 * Undefine a previously-defined string.
604 eqn_undef(struct eqn_node *ep)
610 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
611 EQN_MSG(MANDOCERR_EQNEOF, ep);
613 } else if (NULL != (def = eqn_def_find(ep, start, sz)))
620 eqn_def(struct eqn_node *ep)
627 if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
628 EQN_MSG(MANDOCERR_EQNEOF, ep);
633 * Search for a key that already exists.
634 * Create a new key if none is found.
636 if (NULL == (def = eqn_def_find(ep, start, sz))) {
637 /* Find holes in string array. */
638 for (i = 0; i < (int)ep->defsz; i++)
639 if (0 == ep->defs[i].keysz)
642 if (i == (int)ep->defsz) {
644 ep->defs = mandoc_reallocarray(ep->defs,
645 ep->defsz, sizeof(struct eqn_def));
646 ep->defs[i].key = ep->defs[i].val = NULL;
649 ep->defs[i].keysz = sz;
650 ep->defs[i].key = mandoc_realloc(
651 ep->defs[i].key, sz + 1);
653 memcpy(ep->defs[i].key, start, sz);
654 ep->defs[i].key[(int)sz] = '\0';
658 start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
661 EQN_MSG(MANDOCERR_EQNEOF, ep);
666 def->val = mandoc_realloc(def->val, sz + 1);
667 memcpy(def->val, start, sz);
668 def->val[(int)sz] = '\0';
673 * Recursively parse an eqn(7) expression.
676 eqn_parse(struct eqn_node *ep, struct eqn_box *parent)
679 enum eqn_tok tok, subtok;
687 assert(parent != NULL);
688 if (ep->data == NULL)
692 tok = eqn_tok_parse(ep, &p);
696 case (EQN_TOK_UNDEF):
697 if ((rc = eqn_undef(ep)) <= 0)
700 case (EQN_TOK_NDEFINE):
701 case (EQN_TOK_DEFINE):
702 if ((rc = eqn_def(ep)) <= 0)
705 case (EQN_TOK_TDEFINE):
706 if (NULL == eqn_nextrawtok(ep, NULL))
707 EQN_MSG(MANDOCERR_EQNEOF, ep);
708 else if (NULL == eqn_next(ep,
709 ep->data[(int)ep->cur], NULL, 0))
710 EQN_MSG(MANDOCERR_EQNEOF, ep);
712 case (EQN_TOK_DELIM):
715 case (EQN_TOK_GFONT):
716 if (eqn_nextrawtok(ep, NULL) == NULL)
717 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
718 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
721 case (EQN_TOK_LINEUP):
726 case (EQN_TOK_UNDER):
728 case (EQN_TOK_TILDE):
731 case (EQN_TOK_DOTDOT):
732 if (parent->last == NULL) {
733 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
734 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
735 cur = eqn_box_alloc(ep, parent);
736 cur->type = EQN_TEXT;
737 cur->text = mandoc_strdup("");
739 parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent);
740 parent->type = EQN_LISTONE;
741 parent->expectargs = 1;
743 case (EQN_TOK_DOTDOT):
744 strlcpy(sym, "\\[ad]", sizeof(sym));
747 strlcpy(sym, "\\[->]", sizeof(sym));
750 strlcpy(sym, "\\[<>]", sizeof(sym));
752 case (EQN_TOK_TILDE):
753 strlcpy(sym, "\\[a~]", sizeof(sym));
755 case (EQN_TOK_UNDER):
756 strlcpy(sym, "\\[ul]", sizeof(sym));
759 strlcpy(sym, "\\[rl]", sizeof(sym));
762 strlcpy(sym, "\\[a.]", sizeof(sym));
765 strlcpy(sym, "\\[ha]", sizeof(sym));
772 case (EQN_TOK_DOTDOT):
775 case (EQN_TOK_TILDE):
779 parent->top = mandoc_strdup(sym);
781 case (EQN_TOK_UNDER):
782 parent->bottom = mandoc_strdup(sym);
787 parent = parent->parent;
793 subtok = eqn_tok_parse(ep, NULL);
794 if (subtok != EQN_TOK__MAX) {
795 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
796 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
802 case (EQN_TOK_ROMAN):
803 case (EQN_TOK_ITALIC):
805 while (parent->args == parent->expectargs)
806 parent = parent->parent;
808 * These values apply to the next word or sequence of
809 * words; thus, we mark that we'll have a child with
810 * exactly one of those.
812 parent = eqn_box_alloc(ep, parent);
813 parent->type = EQN_LISTONE;
814 parent->expectargs = 1;
817 parent->font = EQNFONT_FAT;
819 case (EQN_TOK_ROMAN):
820 parent->font = EQNFONT_ROMAN;
822 case (EQN_TOK_ITALIC):
823 parent->font = EQNFONT_ITALIC;
826 parent->font = EQNFONT_BOLD;
833 case (EQN_TOK_GSIZE):
834 /* Accept two values: integral size and a single. */
835 if (NULL == (start = eqn_nexttok(ep, &sz))) {
836 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
837 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
840 size = mandoc_strntoi(start, sz, 10);
842 mandoc_msg(MANDOCERR_IT_NONUM, ep->parse,
843 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
846 if (EQN_TOK_GSIZE == tok) {
850 parent = eqn_box_alloc(ep, parent);
851 parent->type = EQN_LISTONE;
852 parent->expectargs = 1;
860 * We have a left-right-associative expression.
861 * Repivot under a positional node, open a child scope
862 * and keep on reading.
864 if (parent->last == NULL) {
865 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
866 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
867 cur = eqn_box_alloc(ep, parent);
868 cur->type = EQN_TEXT;
869 cur->text = mandoc_strdup("");
871 /* Handle the "subsup" and "fromto" positions. */
872 if (EQN_TOK_SUP == tok && parent->pos == EQNPOS_SUB) {
873 parent->expectargs = 3;
874 parent->pos = EQNPOS_SUBSUP;
877 if (EQN_TOK_TO == tok && parent->pos == EQNPOS_FROM) {
878 parent->expectargs = 3;
879 parent->pos = EQNPOS_FROMTO;
898 parent = eqn_box_makebinary(ep, pos, parent);
901 while (parent->args == parent->expectargs)
902 parent = parent->parent;
904 * Accept a left-right-associative set of arguments just
905 * like sub and sup and friends but without rebalancing
908 parent = eqn_box_alloc(ep, parent);
909 parent->type = EQN_SUBEXPR;
910 parent->pos = EQNPOS_SQRT;
911 parent->expectargs = 1;
915 * We have a right-left-associative fraction.
916 * Close out anything that's currently open, then
917 * rebalance and continue reading.
919 if (parent->last == NULL) {
920 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
921 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
922 cur = eqn_box_alloc(ep, parent);
923 cur->type = EQN_TEXT;
924 cur->text = mandoc_strdup("");
926 while (EQN_SUBEXPR == parent->type)
927 parent = parent->parent;
928 parent = eqn_box_makebinary(ep, EQNPOS_OVER, parent);
930 case (EQN_TOK_RIGHT):
931 case (EQN_TOK_BRACE_CLOSE):
933 * Close out the existing brace.
934 * FIXME: this is a shitty sentinel: we should really
935 * have a native EQN_BRACE type or whatnot.
937 for (cur = parent; cur != NULL; cur = cur->parent)
938 if (cur->type == EQN_LIST &&
939 (tok == EQN_TOK_BRACE_CLOSE ||
943 mandoc_msg(MANDOCERR_BLK_NOTOPEN, ep->parse,
944 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
948 if (EQN_TOK_RIGHT == tok) {
949 if (NULL == (start = eqn_nexttok(ep, &sz))) {
950 mandoc_msg(MANDOCERR_REQ_EMPTY,
951 ep->parse, ep->eqn.ln,
952 ep->eqn.pos, eqn_toks[tok]);
955 /* Handling depends on right/left. */
956 if (STRNEQ(start, sz, "ceiling", 7)) {
957 strlcpy(sym, "\\[rc]", sizeof(sym));
958 parent->right = mandoc_strdup(sym);
959 } else if (STRNEQ(start, sz, "floor", 5)) {
960 strlcpy(sym, "\\[rf]", sizeof(sym));
961 parent->right = mandoc_strdup(sym);
963 parent->right = mandoc_strndup(start, sz);
965 parent = parent->parent;
966 if (EQN_TOK_BRACE_CLOSE == tok && parent &&
967 (parent->type == EQN_PILE ||
968 parent->type == EQN_MATRIX))
969 parent = parent->parent;
970 /* Close out any "singleton" lists. */
971 while (parent->type == EQN_LISTONE &&
972 parent->args == parent->expectargs)
973 parent = parent->parent;
975 case (EQN_TOK_BRACE_OPEN):
978 * If we already have something in the stack and we're
979 * in an expression, then rewind til we're not any more
980 * (just like with the text node).
982 while (parent->args == parent->expectargs)
983 parent = parent->parent;
984 if (EQN_TOK_LEFT == tok &&
985 (start = eqn_nexttok(ep, &sz)) == NULL) {
986 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
987 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
990 parent = eqn_box_alloc(ep, parent);
991 parent->type = EQN_LIST;
992 if (EQN_TOK_LEFT == tok) {
993 if (STRNEQ(start, sz, "ceiling", 7)) {
994 strlcpy(sym, "\\[lc]", sizeof(sym));
995 parent->left = mandoc_strdup(sym);
996 } else if (STRNEQ(start, sz, "floor", 5)) {
997 strlcpy(sym, "\\[lf]", sizeof(sym));
998 parent->left = mandoc_strdup(sym);
1000 parent->left = mandoc_strndup(start, sz);
1003 case (EQN_TOK_PILE):
1004 case (EQN_TOK_LPILE):
1005 case (EQN_TOK_RPILE):
1006 case (EQN_TOK_CPILE):
1007 case (EQN_TOK_CCOL):
1008 case (EQN_TOK_LCOL):
1009 case (EQN_TOK_RCOL):
1010 while (parent->args == parent->expectargs)
1011 parent = parent->parent;
1012 parent = eqn_box_alloc(ep, parent);
1013 parent->type = EQN_PILE;
1014 parent->expectargs = 1;
1016 case (EQN_TOK_ABOVE):
1017 for (cur = parent; cur != NULL; cur = cur->parent)
1018 if (cur->type == EQN_PILE)
1021 mandoc_msg(MANDOCERR_IT_STRAY, ep->parse,
1022 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1025 parent = eqn_box_alloc(ep, cur);
1026 parent->type = EQN_LIST;
1028 case (EQN_TOK_MATRIX):
1029 while (parent->args == parent->expectargs)
1030 parent = parent->parent;
1031 parent = eqn_box_alloc(ep, parent);
1032 parent->type = EQN_MATRIX;
1033 parent->expectargs = 1;
1038 * TODO: make sure we're not in an open subexpression.
1042 assert(tok == EQN_TOK__MAX);
1045 * If we already have something in the stack and we're
1046 * in an expression, then rewind til we're not any more.
1048 while (parent->args == parent->expectargs)
1049 parent = parent->parent;
1050 cur = eqn_box_alloc(ep, parent);
1051 cur->type = EQN_TEXT;
1052 for (i = 0; i < EQNSYM__MAX; i++)
1053 if (0 == strcmp(eqnsyms[i].str, p)) {
1054 (void)snprintf(sym, sizeof(sym),
1055 "\\[%s]", eqnsyms[i].sym);
1056 cur->text = mandoc_strdup(sym);
1061 if (i == EQNSYM__MAX)
1064 * Post-process list status.
1066 while (parent->type == EQN_LISTONE &&
1067 parent->args == parent->expectargs)
1068 parent = parent->parent;
1075 eqn_end(struct eqn_node **epp)
1077 struct eqn_node *ep;
1082 ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
1083 ep->eqn.root->expectargs = UINT_MAX;
1084 return(0 == eqn_parse(ep, ep->eqn.root) ? ROFF_EQN : ROFF_IGN);
1088 eqn_free(struct eqn_node *p)
1092 eqn_box_free(p->eqn.root);
1094 for (i = 0; i < (int)p->defsz; i++) {
1095 free(p->defs[i].key);
1096 free(p->defs[i].val);