=================================================================== RCS file: /cvs/mandoc/eqn.c,v retrieving revision 1.63 retrieving revision 1.69 diff -u -p -r1.63 -r1.69 --- mandoc/eqn.c 2017/06/20 17:24:35 1.63 +++ mandoc/eqn.c 2017/06/23 21:04:57 1.69 @@ -1,7 +1,7 @@ -/* $Id: eqn.c,v 1.63 2017/06/20 17:24:35 schwarze Exp $ */ +/* $Id: eqn.c,v 1.69 2017/06/23 21:04:57 schwarze Exp $ /* * Copyright (c) 2011, 2014 Kristaps Dzonsons - * Copyright (c) 2014, 2015 Ingo Schwarze + * Copyright (c) 2014, 2015, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -80,9 +81,12 @@ enum eqn_tok { EQN_TOK_TDEFINE, EQN_TOK_NDEFINE, EQN_TOK_UNDEF, - EQN_TOK_EOF, EQN_TOK_ABOVE, - EQN_TOK__MAX + EQN_TOK__MAX, + EQN_TOK_FUNC, + EQN_TOK_QUOTED, + EQN_TOK_SYM, + EQN_TOK_EOF }; static const char *eqn_toks[EQN_TOK__MAX] = { @@ -130,10 +134,16 @@ static const char *eqn_toks[EQN_TOK__MAX] = { "tdefine", /* EQN_TOK_TDEFINE */ "ndefine", /* EQN_TOK_NDEFINE */ "undef", /* EQN_TOK_UNDEF */ - NULL, /* EQN_TOK_EOF */ "above", /* EQN_TOK_ABOVE */ }; +static const char *const eqn_func[] = { + "acos", "acsc", "and", "arc", "asec", "asin", "atan", + "cos", "cosh", "coth", "csc", "det", "exp", "for", + "if", "lim", "ln", "log", "max", "min", + "sec", "sin", "sinh", "tan", "tanh", "Im", "Re", +}; + enum eqn_symt { EQNSYM_alpha, EQNSYM_beta, @@ -498,31 +508,40 @@ eqn_tok_parse(struct eqn_node *ep, char **p) size_t i, sz; int quoted; - if (NULL != p) + if (p != NULL) *p = NULL; quoted = ep->data[ep->cur] == '"'; - if (NULL == (start = eqn_nexttok(ep, &sz))) + if ((start = eqn_nexttok(ep, &sz)) == NULL) return EQN_TOK_EOF; if (quoted) { if (p != NULL) *p = mandoc_strndup(start, sz); - return EQN_TOK__MAX; + return EQN_TOK_QUOTED; } - for (i = 0; i < EQN_TOK__MAX; i++) { - if (NULL == eqn_toks[i]) - continue; + for (i = 0; i < EQN_TOK__MAX; i++) if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i]))) - break; + return i; + + for (i = 0; i < EQNSYM__MAX; i++) { + if (STRNEQ(start, sz, + eqnsyms[i].str, strlen(eqnsyms[i].str))) { + mandoc_asprintf(p, "\\[%s]", eqnsyms[i].sym); + return EQN_TOK_SYM; + } } - if (i == EQN_TOK__MAX && NULL != p) + if (p != NULL) *p = mandoc_strndup(start, sz); - return i; + for (i = 0; i < sizeof(eqn_func)/sizeof(*eqn_func); i++) + if (STRNEQ(start, sz, eqn_func[i], strlen(eqn_func[i]))) + return EQN_TOK_FUNC; + + return EQN_TOK__MAX; } static void @@ -554,6 +573,7 @@ eqn_box_alloc(struct eqn_node *ep, struct eqn_box *par bp->parent = parent; bp->parent->args++; bp->expectargs = UINT_MAX; + bp->font = bp->parent->font; bp->size = ep->gsize; if (NULL != parent->first) { @@ -702,12 +722,13 @@ static enum rofferr eqn_parse(struct eqn_node *ep, struct eqn_box *parent) { char sym[64]; - struct eqn_box *cur; - const char *start; + struct eqn_box *cur, *nbox; + const char *cp, *cpn, *start; char *p; - size_t i, sz; + size_t sz; enum eqn_tok tok, subtok; enum eqn_post pos; + enum { CCL_LET, CCL_DIG, CCL_PUN } ccl, ccln; int size; assert(parent != NULL); @@ -768,6 +789,7 @@ this_tok: parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent); parent->type = EQN_LISTONE; parent->expectargs = 1; + parent->font = EQNFONT_ROMAN; switch (tok) { case EQN_TOK_DOTDOT: strlcpy(sym, "\\[ad]", sizeof(sym)); @@ -1067,9 +1089,11 @@ this_tok: * TODO: make sure we're not in an open subexpression. */ return ROFF_EQN; - default: - assert(tok == EQN_TOK__MAX); - assert(NULL != p); + case EQN_TOK__MAX: + case EQN_TOK_FUNC: + case EQN_TOK_QUOTED: + case EQN_TOK_SYM: + assert(p != NULL); /* * If we already have something in the stack and we're * in an expression, then rewind til we're not any more. @@ -1078,17 +1102,78 @@ this_tok: parent = parent->parent; cur = eqn_box_alloc(ep, parent); cur->type = EQN_TEXT; - for (i = 0; i < EQNSYM__MAX; i++) - if (0 == strcmp(eqnsyms[i].str, p)) { - (void)snprintf(sym, sizeof(sym), - "\\[%s]", eqnsyms[i].sym); - cur->text = mandoc_strdup(sym); - free(p); + cur->text = p; + switch (tok) { + case EQN_TOK_FUNC: + cur->font = EQNFONT_ROMAN; + break; + case EQN_TOK_QUOTED: + if (cur->font == EQNFONT_NONE) + cur->font = EQNFONT_ITALIC; + break; + case EQN_TOK_SYM: + break; + default: + if (cur->font != EQNFONT_NONE || *p == '\0') break; + cpn = p - 1; + ccln = CCL_LET; + for (;;) { + /* Advance to next character. */ + cp = cpn++; + ccl = ccln; + ccln = isalpha((unsigned char)*cpn) ? CCL_LET : + isdigit((unsigned char)*cpn) || + (*cpn == '.' && (ccl == CCL_DIG || + isdigit((unsigned char)cpn[1]))) ? + CCL_DIG : CCL_PUN; + /* No boundary before first character. */ + if (cp < p) + continue; + cur->font = ccl == CCL_LET ? + EQNFONT_ITALIC : EQNFONT_ROMAN; + if (*cp == '\\') + mandoc_escape(&cpn, NULL, NULL); + /* No boundary after last character. */ + if (*cpn == '\0') + break; + if (ccln == ccl) + continue; + /* Boundary found, split the text. */ + if (parent->args == parent->expectargs) { + /* Remove the text from the tree. */ + if (cur->prev == NULL) + parent->first = cur->next; + else + cur->prev->next = NULL; + parent->last = cur->prev; + parent->args--; + /* Set up a list instead. */ + nbox = eqn_box_alloc(ep, parent); + nbox->type = EQN_LIST; + /* Insert the word into the list. */ + nbox->first = nbox->last = cur; + cur->parent = nbox; + cur->prev = NULL; + parent = nbox; + } + /* Append a new text box. */ + nbox = eqn_box_alloc(ep, parent); + nbox->type = EQN_TEXT; + nbox->text = mandoc_strdup(cpn); + /* Truncate the old box. */ + p = mandoc_strndup(cur->text, + cpn - cur->text); + free(cur->text); + cur->text = p; + /* Setup to process the new box. */ + cur = nbox; + p = nbox->text; + cpn = p - 1; + ccln = CCL_LET; } - - if (i == EQNSYM__MAX) - cur->text = p; + break; + } /* * Post-process list status. */ @@ -1096,6 +1181,8 @@ this_tok: parent->args == parent->expectargs) parent = parent->parent; break; + default: + abort(); } goto next_tok; }