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

Annotation of mandoc/eqn.c, Revision 1.67

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

CVSweb