#ifndef NO_POSIX_SOURCE #undef _POSIX_SOURCE #define _POSIX_SOURCE 1 #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 2 #endif #include #include #include #include "cell.h" #include "default.h" #include "display.h" #include "eval.h" #include "parser.h" #include "main.h" const char *TokVariety_Name[] = { [BASE_CONT] = "Base Content", [ITER_CONT] = "Iterative Content", [STYLE_CONT] = "Style Content", [CURR_VAL] = "Current Value", [RES_VAL] = "Resultant Value", [STYLE_VAL] = "Current Style", [CONTINGENT] = "Contingent Content" }; /* initcellcontents - make a fresh cell into the "empty" one; don't worry about freeing anything there, that will have been handled. */ void initcellcontents(Cell *fresh) { for (TokVariety tv = BASE_CONT; tv < CONTINGENT; ++tv) fresh->tok[tv].type = EMPTY; fresh->label = NULL; fresh->updated = false; fresh->locked = false; fresh->ignored = false; fresh->clock_t0 = false; fresh->clock_t1 = false; fresh->clock_t2 = false; } /* getcont -- get contents */ Token gettok(const Cell *cell, TokVariety v) { Token emp; emp.type = EMPTY; if (cell == NULLCELL) return emp; if (v == CONTINGENT) v = (cell->clock_t0 && cell->tok[ITER_CONT].type != EMPTY) ? ITER_CONT : BASE_CONT; return cell->tok[v]; } /* locked -- is cell locked? */ bool locked(const Cell *cell) { return (cell != NULLCELL) && cell->locked; } /* ignored -- is cell ignored? */ bool ignored(const Cell *cell) { return (cell != NULLCELL) && cell->ignored; } /* getlabel -- get cell label */ const char *getlabel(const Cell* cell) { if (cell == NULLCELL || cell->label == (char*)0) return ""; return cell->label; } /* copytokens -- copy a sequence of tokens, possibly reallocating dest */ static void copytokens(Token*** totoks, Token** fromtoks) { size_t from_len = tveclen(fromtoks); if (from_len == 0) { tvecfree(*totoks); *totoks = EMPTY_TVEC; return; } size_t to_len = tveclen(*totoks); if (from_len > to_len || *totoks == fromtoks) { if (*totoks != fromtoks) tvecfree(*totoks); *totoks = malloc((from_len+1)*sizeof(Token*)); (*totoks)[from_len] = NULLTOKEN; } else { tvecfreetoks(*totoks); } for (size_t i = 0; itok[tv])); if (faded->label != NULL) free(faded->label); } /* copycell - copies one Cell to another, handling any allocation issues */ void copycell(Cell *to, const Cell *fromcell, LabelHandling lh) { assert(to != NULLCELL); if (to == fromcell) return; freecellcontents(to); if (fromcell != NULLCELL) { memcpy(to, fromcell, sizeof(Cell)); for (TokVariety tv = BASE_CONT; tv < CONTINGENT; ++tv) if (tv <= MAX_PERSIST_TV) to->tok[tv] = tcopy(fromcell->tok[tv]); else to->tok[tv].type = EMPTY; if (lh != PRESERVE_LABEL && fromcell->label != (char*)0) { size_t len = strlen(fromcell->label); to->label = strcpy(malloc(len+2), fromcell->label); (to->label)[len] = '_'; (to->label)[len+1] = '\0'; } } else { initcellcontents(to); } }