[BACK]Return to eqn.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/eqn.c, Revision 1.64

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

CVSweb