bc: use only G_interrupt as interrupt flag
[oweals/busybox.git] / miscutils / bc.c
index 117318bfa80954ea8566f44042ed2c62cad9efeb..0330c43e567e59c0dfd4a06d2355fe3a27c0e459 100644 (file)
 //config:           Also note that, like the FreeBSD dc, extended registers are not
 //config:           allowed unless the "-x" option is given.
 //config:
+//config:config FEATURE_DC_SMALL
+//config:      bool "Minimal dc implementation (4.2 kb), not using bc code base"
+//config:      depends on DC && !BC
+//config:      default y
+//config:
+//config:config FEATURE_DC_LIBM
+//config:      bool "Enable power and exp functions (requires libm)"
+//config:      default y
+//config:      depends on FEATURE_DC_SMALL
+//config:      help
+//config:      Enable power and exp functions.
+//config:      NOTE: This will require libm to be present for linking.
+//config:
 //config:config FEATURE_BC_SIGNALS
 //config:      bool "Enable bc/dc signal handling"
 //config:      default y
-//config:      depends on BC || DC
+//config:      depends on (BC || DC) && !FEATURE_DC_SMALL
 //config:      help
 //config:      Enable signal handling for bc and dc.
 //config:
 //config:config FEATURE_BC_LONG_OPTIONS
 //config:      bool "Enable bc/dc long options"
 //config:      default y
-//config:      depends on BC || DC
+//config:      depends on (BC || DC) && !FEATURE_DC_SMALL
 //config:      help
 //config:      Enable long options for bc and dc.
 
 //kbuild:lib-$(CONFIG_BC) += bc.o
 //kbuild:lib-$(CONFIG_DC) += bc.o
 
+//See www.gnu.org/software/bc/manual/bc.html
 //usage:#define bc_trivial_usage
-//usage:       "EXPRESSION...\n"
-//usage:       "function_definition\n"
+//usage:       "[-sqliw] FILE..."
 //usage:
-//usage:#define bc_full_usage "\n\n"
-//usage:       "See www.gnu.org/software/bc/manual/bc.html\n"
+//usage:#define bc_full_usage "\n"
+//usage:     "\nArbitrary precision calculator"
+//usage:     "\n"
+///////:     "\n       -i      Interactive" - has no effect for now
+//usage:     "\n       -q      Quiet"
+//usage:     "\n       -l      Load standard math library"
+//usage:     "\n       -s      Be POSIX compatible"
+//usage:     "\n       -w      Warn if extensions are used"
+///////:     "\n       -v      Version"
+//usage:     "\n"
+//usage:     "\n$BC_LINE_LENGTH changes output width"
 //usage:
 //usage:#define bc_example_usage
 //usage:       "3 + 4.129\n"
 //usage:       "obase = A\n"
 //usage:
 //usage:#define dc_trivial_usage
-//usage:       "EXPRESSION..."
+//usage:       "[-eSCRIPT]... [-fFILE]... [FILE]..."
 //usage:
-//usage:#define dc_full_usage "\n\n"
-//usage:       "Tiny RPN calculator. Operations:\n"
-//usage:       "+, add, -, sub, *, mul, /, div, %, mod, ^, exp, ~, divmod, |, "
-//usage:       "modular exponentiation,\n"
-//usage:       "p - print top of the stack (without popping),\n"
-//usage:       "f - print entire stack,\n"
-//usage:       "k - pop the value and set the precision.\n"
-//usage:       "i - pop the value and set input radix.\n"
-//usage:       "o - pop the value and set output radix.\n"
-//usage:       "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
+//usage:#define dc_full_usage "\n"
+//usage:     "\nTiny RPN calculator. Operations:"
+//usage:     "\n+, -, *, /, %, ^, exp, ~, divmod, |, "
+//usage:       "modular exponentiation,"
+//usage:     "\np - print top of the stack (without popping)"
+//usage:     "\nf - print entire stack"
+//usage:     "\nk - pop the value and set the precision"
+//usage:     "\ni - pop the value and set input radix"
+//usage:     "\no - pop the value and set output radix"
+//usage:     "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
 //usage:
 //usage:#define dc_example_usage
-//usage:       "$ dc 2 2 + p\n"
+//usage:       "$ dc -e'2 2 + p'\n"
 //usage:       "4\n"
-//usage:       "$ dc 8 8 \\* 2 2 + / p\n"
+//usage:       "$ dc -e'8 8 \\* 2 2 + / p'\n"
 //usage:       "16\n"
-//usage:       "$ dc 0 1 and p\n"
+//usage:       "$ dc -e'0 1 & p'\n"
 //usage:       "0\n"
-//usage:       "$ dc 0 1 or p\n"
+//usage:       "$ dc -e'0 1 | p'\n"
 //usage:       "1\n"
-//usage:       "$ echo 72 9 div 8 mul p | dc\n"
+//usage:       "$ echo '72 9 / 8 * p' | dc\n"
 //usage:       "64\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
-typedef enum BcStatus {
-
-       BC_STATUS_SUCCESS,
-
-       BC_STATUS_ALLOC_ERR,
-       BC_STATUS_INPUT_EOF,
-       BC_STATUS_BIN_FILE,
-       BC_STATUS_PATH_IS_DIR,
-
-       BC_STATUS_LEX_BAD_CHAR,
-       BC_STATUS_LEX_NO_STRING_END,
-       BC_STATUS_LEX_NO_COMMENT_END,
-       BC_STATUS_LEX_EOF,
-#if ENABLE_DC
-       BC_STATUS_LEX_EXTENDED_REG,
-#endif
-
-       BC_STATUS_PARSE_BAD_TOKEN,
-       BC_STATUS_PARSE_BAD_EXP,
-       BC_STATUS_PARSE_EMPTY_EXP,
-       BC_STATUS_PARSE_BAD_PRINT,
-       BC_STATUS_PARSE_BAD_FUNC,
-       BC_STATUS_PARSE_BAD_ASSIGN,
-       BC_STATUS_PARSE_NO_AUTO,
-       BC_STATUS_PARSE_DUPLICATE_LOCAL,
-       BC_STATUS_PARSE_NO_BLOCK_END,
-
-       BC_STATUS_MATH_NEGATIVE,
-       BC_STATUS_MATH_NON_INTEGER,
-       BC_STATUS_MATH_OVERFLOW,
-       BC_STATUS_MATH_DIVIDE_BY_ZERO,
-       BC_STATUS_MATH_BAD_STRING,
-
-       BC_STATUS_EXEC_FILE_ERR,
-       BC_STATUS_EXEC_MISMATCHED_PARAMS,
-       BC_STATUS_EXEC_UNDEFINED_FUNC,
-       BC_STATUS_EXEC_FILE_NOT_EXECUTABLE,
-       BC_STATUS_EXEC_NUM_LEN,
-       BC_STATUS_EXEC_NAME_LEN,
-       BC_STATUS_EXEC_STRING_LEN,
-       BC_STATUS_EXEC_ARRAY_LEN,
-       BC_STATUS_EXEC_BAD_IBASE,
-       BC_STATUS_EXEC_BAD_SCALE,
-       BC_STATUS_EXEC_BAD_READ_EXPR,
-       BC_STATUS_EXEC_REC_READ,
-       BC_STATUS_EXEC_BAD_TYPE,
-       BC_STATUS_EXEC_BAD_OBASE,
-       BC_STATUS_EXEC_SIGNAL,
-       BC_STATUS_EXEC_STACK,
-
-       BC_STATUS_VEC_OUT_OF_BOUNDS,
-       BC_STATUS_VEC_ITEM_EXISTS,
-
-#if ENABLE_BC
-       BC_STATUS_POSIX_NAME_LEN,
-       BC_STATUS_POSIX_COMMENT,
-       BC_STATUS_POSIX_BAD_KW,
-       BC_STATUS_POSIX_DOT,
-       BC_STATUS_POSIX_RET,
-       BC_STATUS_POSIX_BOOL,
-       BC_STATUS_POSIX_REL_POS,
-       BC_STATUS_POSIX_MULTIREL,
-       BC_STATUS_POSIX_FOR1,
-       BC_STATUS_POSIX_FOR2,
-       BC_STATUS_POSIX_FOR3,
-       BC_STATUS_POSIX_BRACE,
-#endif
-
-       BC_STATUS_QUIT,
-       BC_STATUS_LIMITS,
-
-       BC_STATUS_INVALID_OPTION,
+#if ENABLE_FEATURE_DC_SMALL
+# include "dc.c"
+#else
 
+typedef enum BcStatus {
+       BC_STATUS_SUCCESS = 0,
+       BC_STATUS_FAILURE = 1,
+       BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this
 } BcStatus;
 
-#define BC_ERR_IDX_VM (0)
-#define BC_ERR_IDX_LEX (1)
-#define BC_ERR_IDX_PARSE (2)
-#define BC_ERR_IDX_MATH (3)
-#define BC_ERR_IDX_EXEC (4)
-#define BC_ERR_IDX_VEC (5)
-#if ENABLE_BC
-#define BC_ERR_IDX_POSIX (6)
-#endif
-
 #define BC_VEC_INVALID_IDX ((size_t) -1)
 #define BC_VEC_START_CAP (1 << 5)
 
@@ -258,13 +206,6 @@ typedef struct BcVec {
        BcVecFree dtor;
 } BcVec;
 
-#define bc_vec_pop(v) (bc_vec_npop((v), 1))
-#define bc_vec_top(v) (bc_vec_item_rev((v), 0))
-
-#define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), bc_id_free))
-
-#define BC_READ_BIN_CHAR(c) ((((c) < ' ' && !isspace((c))) || (c) > '~'))
-
 typedef signed char BcDig;
 
 typedef struct BcNum {
@@ -275,16 +216,17 @@ typedef struct BcNum {
        bool neg;
 } BcNum;
 
-#define BC_NUM_MIN_BASE ((unsigned long) 2)
-#define BC_NUM_MAX_IBASE ((unsigned long) 16)
-#define BC_NUM_DEF_SIZE (16)
-#define BC_NUM_PRINT_WIDTH (69)
+#define BC_NUM_MIN_BASE         ((unsigned long) 2)
+#define BC_NUM_MAX_IBASE        ((unsigned long) 16)
+// larger value might speed up BIGNUM calculations a bit:
+#define BC_NUM_DEF_SIZE         (16)
+#define BC_NUM_PRINT_WIDTH      (69)
 
-#define BC_NUM_KARATSUBA_LEN (32)
+#define BC_NUM_KARATSUBA_LEN    (32)
 
-#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
-#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
-#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
+#define BC_NUM_NEG(n, neg)      ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
+#define BC_NUM_ONE(n)           ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
+#define BC_NUM_INT(n)           ((n)->len - (n)->rdx)
 #define BC_NUM_AREQ(a, b) \
        (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
 #define BC_NUM_MREQ(a, b, scale) \
@@ -293,14 +235,6 @@ typedef struct BcNum {
 typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t);
 typedef void (*BcNumDigitOp)(size_t, size_t, bool, size_t *, size_t);
 
-static void bc_num_init(BcNum *n, size_t req);
-static void bc_num_expand(BcNum *n, size_t req);
-static void bc_num_copy(BcNum *d, BcNum *s);
-static void bc_num_free(void *num);
-
-static BcStatus bc_num_ulong(BcNum *n, unsigned long *result);
-static BcStatus bc_num_ulong2num(BcNum *n, unsigned long val);
-
 static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale);
 static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale);
 static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale);
@@ -463,9 +397,6 @@ typedef struct BcInstPtr {
        size_t len;
 } BcInstPtr;
 
-static void bc_array_expand(BcVec *a, size_t len);
-static int bc_id_cmp(const void *e1, const void *e2);
-
 // BC_LEX_NEG is not used in lexing; it is only for parsing.
 typedef enum BcLexType {
 
@@ -521,19 +452,21 @@ typedef enum BcLexType {
        BC_LEX_NAME,
        BC_LEX_NUMBER,
 
-       BC_LEX_KEY_AUTO,
+       BC_LEX_KEY_1st_keyword,
+       BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
        BC_LEX_KEY_BREAK,
        BC_LEX_KEY_CONTINUE,
        BC_LEX_KEY_DEFINE,
        BC_LEX_KEY_ELSE,
        BC_LEX_KEY_FOR,
        BC_LEX_KEY_HALT,
-       BC_LEX_KEY_IBASE,
+       // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
+       BC_LEX_KEY_IBASE,  // relative order should match for: BC_INST_IBASE
        BC_LEX_KEY_IF,
-       BC_LEX_KEY_LAST,
+       BC_LEX_KEY_LAST,   // relative order should match for: BC_INST_LAST
        BC_LEX_KEY_LENGTH,
        BC_LEX_KEY_LIMITS,
-       BC_LEX_KEY_OBASE,
+       BC_LEX_KEY_OBASE,  // relative order should match for: BC_INST_OBASE
        BC_LEX_KEY_PRINT,
        BC_LEX_KEY_QUIT,
        BC_LEX_KEY_READ,
@@ -570,8 +503,70 @@ typedef enum BcLexType {
        BC_LEX_NQUIT,
        BC_LEX_SCALE_FACTOR,
 #endif
-
 } BcLexType;
+// must match order of BC_LEX_KEY_foo etc above
+#if ENABLE_BC
+struct BcLexKeyword {
+       char name8[8];
+};
+#define BC_LEX_KW_ENTRY(a, b) \
+       { .name8 = a /*, .posix = b */ }
+static const struct BcLexKeyword bc_lex_kws[20] = {
+       BC_LEX_KW_ENTRY("auto"    , 1), // 0
+       BC_LEX_KW_ENTRY("break"   , 1), // 1
+       BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
+       BC_LEX_KW_ENTRY("define"  , 1), // 3
+
+       BC_LEX_KW_ENTRY("else"    , 0), // 4
+       BC_LEX_KW_ENTRY("for"     , 1), // 5
+       BC_LEX_KW_ENTRY("halt"    , 0), // 6
+       BC_LEX_KW_ENTRY("ibase"   , 1), // 7
+
+       BC_LEX_KW_ENTRY("if"      , 1), // 8
+       BC_LEX_KW_ENTRY("last"    , 0), // 9
+       BC_LEX_KW_ENTRY("length"  , 1), // 10
+       BC_LEX_KW_ENTRY("limits"  , 0), // 11
+
+       BC_LEX_KW_ENTRY("obase"   , 1), // 12
+       BC_LEX_KW_ENTRY("print"   , 0), // 13
+       BC_LEX_KW_ENTRY("quit"    , 1), // 14
+       BC_LEX_KW_ENTRY("read"    , 0), // 15
+
+       BC_LEX_KW_ENTRY("return"  , 1), // 16
+       BC_LEX_KW_ENTRY("scale"   , 1), // 17
+       BC_LEX_KW_ENTRY("sqrt"    , 1), // 18
+       BC_LEX_KW_ENTRY("while"   , 1), // 19
+};
+#undef BC_LEX_KW_ENTRY
+enum {
+       POSIX_KWORD_MASK = 0
+               | (1 << 0)
+               | (1 << 1)
+               | (0 << 2)
+               | (1 << 3)
+               \
+               | (0 << 4)
+               | (1 << 5)
+               | (0 << 6)
+               | (1 << 7)
+               \
+               | (1 << 8)
+               | (0 << 9)
+               | (1 << 10)
+               | (0 << 11)
+               \
+               | (1 << 12)
+               | (0 << 13)
+               | (1 << 14)
+               | (0 << 15)
+               \
+               | (1 << 16)
+               | (1 << 17)
+               | (1 << 18)
+               | (1 << 19)
+};
+#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
+#endif
 
 struct BcLex;
 typedef BcStatus (*BcLexNext)(struct BcLex *);
@@ -581,7 +576,6 @@ typedef struct BcLex {
        const char *buf;
        size_t i;
        size_t line;
-       const char *f;
        size_t len;
        bool newline;
 
@@ -597,15 +591,11 @@ typedef struct BcLex {
 
 #define BC_PARSE_STREND ((char) UCHAR_MAX)
 
-#define bc_parse_push(p, i) (bc_vec_pushByte(&(p)->func->code, (char) (i)))
-#define bc_parse_updateFunc(p, f) \
-       ((p)->func = bc_vec_item(&(p)->prog->fns, ((p)->fidx = (f))))
-
-#define BC_PARSE_REL (1 << 0)
-#define BC_PARSE_PRINT (1 << 1)
+#define BC_PARSE_REL    (1 << 0)
+#define BC_PARSE_PRINT  (1 << 1)
 #define BC_PARSE_NOCALL (1 << 2)
 #define BC_PARSE_NOREAD (1 << 3)
-#define BC_PARSE_ARRAY (1 << 4)
+#define BC_PARSE_ARRAY  (1 << 4)
 
 #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
 #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
@@ -642,29 +632,16 @@ typedef struct BcLex {
            BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF |   \
            BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
 
-typedef struct BcOp {
-       char prec;
-       bool left;
-} BcOp;
-
 typedef struct BcParseNext {
        uint32_t len;
        BcLexType tokens[4];
 } BcParseNext;
 
-#define BC_PARSE_NEXT_TOKENS(...) .tokens = { __VA_ARGS__ }
-#define BC_PARSE_NEXT(a, ...)                         \
-       {                                                 \
-               .len = (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) \
-       }
-
 struct BcParse;
 
 struct BcProgram;
 
-typedef void (*BcParseInit)(struct BcParse *, struct BcProgram *, size_t);
 typedef BcStatus (*BcParseParse)(struct BcParse *);
-typedef BcStatus (*BcParseExpr)(struct BcParse *, uint8_t);
 
 typedef struct BcParse {
 
@@ -679,7 +656,6 @@ typedef struct BcParse {
 
        BcVec ops;
 
-       struct BcProgram *prog;
        BcFunc *func;
        size_t fidx;
 
@@ -688,47 +664,6 @@ typedef struct BcParse {
 
 } BcParse;
 
-#if ENABLE_BC
-
-typedef struct BcLexKeyword {
-       const char name[9];
-       const char len;
-       const bool posix;
-} BcLexKeyword;
-
-#define BC_LEX_KW_ENTRY(a, b, c)            \
-       {                                       \
-               .name = a, .len = (b), .posix = (c) \
-       }
-
-static BcStatus bc_lex_token(BcLex *l);
-
-#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
-#define BC_PARSE_LEAF(p, rparen)                                \
-       (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
-        (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
-
-// We can calculate the conversion between tokens and exprs by subtracting the
-// position of the first operator in the lex enum and adding the position of the
-// first in the expr enum. Note: This only works for binary operators.
-#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
-
-static BcStatus bc_parse_parse(BcParse *p);
-static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
-
-#endif // ENABLE_BC
-
-#if ENABLE_DC
-
-#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
-
-static BcStatus dc_lex_token(BcLex *l);
-
-static void dc_parse_init(BcParse *p, struct BcProgram *prog, size_t func);
-static BcStatus dc_parse_expr(BcParse *p, uint8_t flags);
-
-#endif // ENABLE_DC
-
 typedef struct BcProgram {
 
        size_t len;
@@ -768,16 +703,12 @@ typedef struct BcProgram {
 
        size_t nchars;
 
-       BcParseInit parse_init;
-       BcParseExpr parse_expr;
-
 } BcProgram;
 
 #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
 
 #define BC_PROG_MAIN (0)
 #define BC_PROG_READ (1)
-
 #if ENABLE_DC
 #define BC_PROG_REQ_FUNCS (2)
 #endif
@@ -788,231 +719,127 @@ typedef struct BcProgram {
 
 typedef unsigned long (*BcProgramBuiltIn)(BcNum *);
 
-static void bc_program_addFunc(BcProgram *p, char *name, size_t *idx);
-static BcStatus bc_program_reset(BcProgram *p, BcStatus s);
-
-#define BC_FLAG_X (1 << 0)
-#define BC_FLAG_W (1 << 1)
-#define BC_FLAG_V (1 << 2)
-#define BC_FLAG_S (1 << 3)
-#define BC_FLAG_Q (1 << 4)
-#define BC_FLAG_L (1 << 5)
-#define BC_FLAG_I (1 << 6)
+#define BC_FLAG_W (1 << 0)
+#define BC_FLAG_V (1 << 1)
+#define BC_FLAG_S (1 << 2)
+#define BC_FLAG_Q (1 << 3)
+#define BC_FLAG_L (1 << 4)
+#define BC_FLAG_I (1 << 5)
+#define DC_FLAG_X (1 << 6)
 
 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
 
-#define BC_MAX_OBASE ((unsigned long) 999)
-#define BC_MAX_DIM ((unsigned long) INT_MAX)
-#define BC_MAX_SCALE ((unsigned long) UINT_MAX)
-#define BC_MAX_STRING ((unsigned long) UINT_MAX - 1)
-#define BC_MAX_NAME BC_MAX_STRING
-#define BC_MAX_NUM BC_MAX_STRING
-#define BC_MAX_EXP ((unsigned long) LONG_MAX)
-#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
+#define BC_MAX_OBASE  ((unsigned) 999)
+#define BC_MAX_DIM    ((unsigned) INT_MAX)
+#define BC_MAX_SCALE  ((unsigned) UINT_MAX)
+#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
+#define BC_MAX_NAME   BC_MAX_STRING
+#define BC_MAX_NUM    BC_MAX_STRING
+#define BC_MAX_EXP    ((unsigned long) LONG_MAX)
+#define BC_MAX_VARS   ((unsigned long) SIZE_MAX - 1)
 
 struct globals {
+       IF_FEATURE_BC_SIGNALS(smallint ttyin;)
+       IF_FEATURE_CLEAN_UP(smallint exiting;)
+       smallint eof;
        char sbgn;
        char send;
 
        BcParse prs;
        BcProgram prog;
 
-       unsigned flags;
+       // For error messages. Can be set to current parsed line,
+       // or [TODO] to current executing line (can be before last parsed one)
+       unsigned err_line;
+
        BcVec files;
 
        char *env_args;
 
-       smallint tty;
-       smallint ttyin;
+#if ENABLE_FEATURE_EDITING
+       line_input_t *line_input_state;
+#endif
 } FIX_ALIASING;
 #define G (*ptr_to_globals)
 #define INIT_G() do { \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 } while (0)
-#define G_posix (ENABLE_BC && (G.flags & BC_FLAG_S))
-#define G_warn  (ENABLE_BC && (G.flags & BC_FLAG_W))
-#define G_exreg (ENABLE_DC && (G.flags & BC_FLAG_X))
-#define G_interrupt (ENABLE_FEATURE_BC_SIGNALS ? bb_got_signal : 0)
-
-
-#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
-
-#if ENABLE_BC
-static BcStatus bc_vm_posixError(BcStatus s, const char *file, size_t line,
-                                 const char *msg);
-#endif
-
-static void bc_vm_info(void);
-
-static const char* const bc_args_env_name = "BC_ENV_ARGS";
-
-static const char bc_err_fmt[] = "\n%s error: %s\n";
-static const char bc_warn_fmt[] = "\n%s warning: %s\n";
-static const char bc_err_line[] = ":%zu\n\n";
-
-static const char *bc_errs[] = {
-       "VM",
-       "Lex",
-       "Parse",
-       "Math",
-       "Runtime",
-       "Vector",
-#if ENABLE_BC
-       "POSIX",
-#endif
-};
-
-static const uint8_t bc_err_ids[] = {
-       BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
-       BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX,
-#if ENABLE_DC
-       BC_ERR_IDX_LEX,
-#endif
-       BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
-       BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
-       BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH,
-       BC_ERR_IDX_MATH,
-#if ENABLE_DC
-       BC_ERR_IDX_MATH,
-#endif
-       BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
-       BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
-       BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
-       BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
-       BC_ERR_IDX_EXEC,
-       BC_ERR_IDX_VEC, BC_ERR_IDX_VEC,
-#if ENABLE_BC
-       BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
-       BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
-       BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
-#endif
-       BC_ERR_IDX_VM, BC_ERR_IDX_VM, BC_ERR_IDX_VM,
-};
-
-static const char *bc_err_msgs[] = {
-
-       NULL,
-       "memory allocation error",
-       "I/O error",
-       "file is not text:",
-       "path is a directory:",
-
-       "bad character",
-       "string end could not be found",
-       "comment end could not be found",
-       "end of file",
-#if ENABLE_DC
-       "extended register",
+#define FREE_G() do { \
+       FREE_PTR_TO_GLOBALS(); \
+} while (0)
+#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
+#define G_warn  (ENABLE_BC && (option_mask32 & BC_FLAG_W))
+#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
+#if ENABLE_FEATURE_BC_SIGNALS
+# define G_interrupt bb_got_signal
+# define G_ttyin     G.ttyin
+#else
+# define G_interrupt 0
+# define G_ttyin     0
 #endif
-
-       "bad token",
-       "bad expression",
-       "empty expression",
-       "bad print statement",
-       "bad function definition",
-       "bad assignment: left side must be scale, ibase, "
-               "obase, last, var, or array element",
-       "no auto variable found",
-       "function parameter or auto var has the same name as another",
-       "block end could not be found",
-
-       "negative number",
-       "non integer number",
-       "overflow",
-       "divide by zero",
-       "bad number string",
-
-       "could not open file:",
-       "mismatched parameters",
-       "undefined function",
-       "file is not executable:",
-       "number too long: must be [1, BC_NUM_MAX]",
-       "name too long: must be [1, BC_NAME_MAX]",
-       "string too long: must be [1, BC_STRING_MAX]",
-       "array too long; must be [1, BC_DIM_MAX]",
-       "bad ibase; must be [2, 16]",
-       "bad scale; must be [0, BC_SCALE_MAX]",
-       "bad read() expression",
-       "read() call inside of a read() call",
-       "variable is wrong type",
-       "bad obase; must be [2, BC_BASE_MAX]",
-       "signal caught and not handled",
-       "stack has too few elements",
-
-       "index is out of bounds",
-       "item already exists",
-
-#if ENABLE_BC
-       "POSIX only allows one character names; the following is bad:",
-       "POSIX does not allow '#' script comments",
-       "POSIX does not allow the following keyword:",
-       "POSIX does not allow a period ('.') as a shortcut for the last result",
-       "POSIX requires parentheses around return expressions",
-       "POSIX does not allow boolean operators; the following is bad:",
-       "POSIX does not allow comparison operators outside if or loops",
-       "POSIX requires exactly one comparison operator per condition",
-       "POSIX does not allow an empty init expression in a for loop",
-       "POSIX does not allow an empty condition expression in a for loop",
-       "POSIX does not allow an empty update expression in a for loop",
-       "POSIX requires the left brace be on the same line as the function header",
+#if ENABLE_FEATURE_CLEAN_UP
+# define G_exiting G.exiting
+#else
+# define G_exiting 0
 #endif
-
-};
-
-static const char bc_func_main[] = "(main)";
-static const char bc_func_read[] = "(read)";
+#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
 
 #if ENABLE_BC
-static const BcLexKeyword bc_lex_kws[20] = {
-       BC_LEX_KW_ENTRY("auto", 4, true),
-       BC_LEX_KW_ENTRY("break", 5, true),
-       BC_LEX_KW_ENTRY("continue", 8, false),
-       BC_LEX_KW_ENTRY("define", 6, true),
-       BC_LEX_KW_ENTRY("else", 4, false),
-       BC_LEX_KW_ENTRY("for", 3, true),
-       BC_LEX_KW_ENTRY("halt", 4, false),
-       BC_LEX_KW_ENTRY("ibase", 5, true),
-       BC_LEX_KW_ENTRY("if", 2, true),
-       BC_LEX_KW_ENTRY("last", 4, false),
-       BC_LEX_KW_ENTRY("length", 6, true),
-       BC_LEX_KW_ENTRY("limits", 6, false),
-       BC_LEX_KW_ENTRY("obase", 5, true),
-       BC_LEX_KW_ENTRY("print", 5, false),
-       BC_LEX_KW_ENTRY("quit", 4, true),
-       BC_LEX_KW_ENTRY("read", 4, false),
-       BC_LEX_KW_ENTRY("return", 6, true),
-       BC_LEX_KW_ENTRY("scale", 5, true),
-       BC_LEX_KW_ENTRY("sqrt", 4, true),
-       BC_LEX_KW_ENTRY("while", 5, true),
-};
 
-// This is an array that corresponds to token types. An entry is
+// This is a bit array that corresponds to token types. An entry is
 // true if the token is valid in an expression, false otherwise.
-static const bool bc_parse_exprs[] = {
-       false, false, true, true, true, true, true, true, true, true, true, true,
-       true, true, true, true, true, true, true, true, true, true, true, true,
-       true, true, true, false, false, true, true, false, false, false, false,
-       false, false, false, true, true, false, false, false, false, false, false,
-       false, true, false, true, true, true, true, false, false, true, false, true,
-       true, false,
+enum {
+       BC_PARSE_EXPRS_BITS = 0
+       + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
+       + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
+       + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
+       + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
+       + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
+       + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
+       + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
+       + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3)                                    ) << (7*8))
 };
+static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
+{
+#if ULONG_MAX > 0xffffffff
+       // 64-bit version (will not work correctly for 32-bit longs!)
+       return BC_PARSE_EXPRS_BITS & (1UL << i);
+#else
+       // 32-bit version
+       unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
+       if (i >= 32) {
+               m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
+               i &= 31;
+       }
+       return m & (1UL << i);
+#endif
+}
 
 // This is an array of data for operators that correspond to token types.
-static const BcOp bc_parse_ops[] = {
-       { 0, false }, { 0, false },
-       { 1, false },
-       { 2, false },
-       { 3, true }, { 3, true }, { 3, true },
-       { 4, true }, { 4, true },
-       { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true }, { 6, true },
-       { 1, false },
-       { 7, true }, { 7, true },
-       { 5, false }, { 5, false }, { 5, false }, { 5, false }, { 5, false },
-       { 5, false }, { 5, false },
+static const uint8_t bc_parse_ops[] = {
+#define OP(p,l) ((int)(l) * 0x10 + (p))
+       OP(0, false), OP( 0, false ), // inc dec
+       OP(1, false), // neg
+       OP(2, false),
+       OP(3, true ), OP( 3, true  ), OP( 3, true  ), // pow mul div
+       OP(4, true ), OP( 4, true  ), // mod + -
+       OP(6, true ), OP( 6, true  ), OP( 6, true  ), OP( 6, true  ), OP( 6, true  ), OP( 6, true ), // == <= >= != < >
+       OP(1, false), // not
+       OP(7, true ), OP( 7, true  ), // or and
+       OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
+       OP(5, false), OP( 5, false ), // -= =
+#undef OP
 };
+#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
+#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
 
 // These identify what tokens can come after expressions in certain cases.
+#define BC_PARSE_NEXT_TOKENS(...) .tokens = { __VA_ARGS__ }
+#define BC_PARSE_NEXT(a, ...)                         \
+       {                                                 \
+               .len = (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) \
+       }
 static const BcParseNext bc_parse_next_expr =
        BC_PARSE_NEXT(4, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF);
 static const BcParseNext bc_parse_next_param =
@@ -1034,8 +861,6 @@ static const BcLexType dc_lex_regs[] = {
        BC_LEX_STORE_PUSH,
 };
 
-static const size_t dc_lex_regs_len = sizeof(dc_lex_regs) / sizeof(BcLexType);
-
 static const BcLexType dc_lex_tokens[] = {
        BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
        BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
@@ -1095,95 +920,133 @@ static const BcNumBinaryOp bc_program_ops[] = {
        bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
 };
 
-static const char bc_program_stdin_name[] = "<stdin>";
-static const char bc_program_ready_msg[] = "ready for more input\n";
+static void fflush_and_check(void)
+{
+       fflush_all();
+       if (ferror(stdout) || ferror(stderr))
+               bb_perror_msg_and_die("output error");
+}
+
+#if ENABLE_FEATURE_CLEAN_UP
+#define QUIT_OR_RETURN_TO_MAIN \
+do { \
+       IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
+       G_exiting = 1; \
+       return BC_STATUS_FAILURE; \
+} while (0)
+#else
+#define QUIT_OR_RETURN_TO_MAIN quit()
+#endif
+
+static void quit(void) NORETURN;
+static void quit(void)
+{
+       if (ferror(stdin))
+               bb_perror_msg_and_die("input error");
+       fflush_and_check();
+       exit(0);
+}
+
+static void bc_verror_msg(const char *fmt, va_list p)
+{
+       const char *sv = sv; /* for compiler */
+       if (G.prog.file) {
+               sv = applet_name;
+               applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
+       }
+       bb_verror_msg(fmt, p, NULL);
+       if (G.prog.file) {
+               free((char*)applet_name);
+               applet_name = sv;
+       }
+}
+
+static NOINLINE int bc_error_fmt(const char *fmt, ...)
+{
+       va_list p;
+
+       va_start(p, fmt);
+       bc_verror_msg(fmt, p);
+       va_end(p);
+
+       if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
+               exit(1);
+       return BC_STATUS_FAILURE;
+}
 
 #if ENABLE_BC
-static const char *bc_lib_name = "gen/lib.bc";
+static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
+{
+       va_list p;
 
-static const char bc_lib[] = {
-  115,99,97,108,101,61,50,48,10,100,101,102,105,110,101,32,101,40,120,41,123,
-  10,9,97,117,116,111,32,98,44,115,44,110,44,114,44,100,44,105,44,112,44,102,
-  44,118,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,105,102,
-  40,120,60,48,41,123,10,9,9,110,61,49,10,9,9,120,61,45,120,10,9,125,10,9,115,
-  61,115,99,97,108,101,10,9,114,61,54,43,115,43,48,46,52,52,42,120,10,9,115,99,
-  97,108,101,61,115,99,97,108,101,40,120,41,43,49,10,9,119,104,105,108,101,40,
-  120,62,49,41,123,10,9,9,100,43,61,49,10,9,9,120,47,61,50,10,9,9,115,99,97,108,
-  101,43,61,49,10,9,125,10,9,115,99,97,108,101,61,114,10,9,114,61,120,43,49,10,
-  9,112,61,120,10,9,102,61,118,61,49,10,9,102,111,114,40,105,61,50,59,118,33,
-  61,48,59,43,43,105,41,123,10,9,9,112,42,61,120,10,9,9,102,42,61,105,10,9,9,
-  118,61,112,47,102,10,9,9,114,43,61,118,10,9,125,10,9,119,104,105,108,101,40,
-  40,100,45,45,41,33,61,48,41,114,42,61,114,10,9,115,99,97,108,101,61,115,10,
-  9,105,98,97,115,101,61,98,10,9,105,102,40,110,33,61,48,41,114,101,116,117,114,
-  110,40,49,47,114,41,10,9,114,101,116,117,114,110,40,114,47,49,41,10,125,10,
-  100,101,102,105,110,101,32,108,40,120,41,123,10,9,97,117,116,111,32,98,44,115,
-  44,114,44,112,44,97,44,113,44,105,44,118,10,9,98,61,105,98,97,115,101,10,9,
-  105,98,97,115,101,61,65,10,9,105,102,40,120,60,61,48,41,123,10,9,9,114,61,40,
-  49,45,49,48,94,115,99,97,108,101,41,47,49,10,9,9,105,98,97,115,101,61,98,10,
-  9,9,114,101,116,117,114,110,40,114,41,10,9,125,10,9,115,61,115,99,97,108,101,
-  10,9,115,99,97,108,101,43,61,54,10,9,112,61,50,10,9,119,104,105,108,101,40,
-  120,62,61,50,41,123,10,9,9,112,42,61,50,10,9,9,120,61,115,113,114,116,40,120,
-  41,10,9,125,10,9,119,104,105,108,101,40,120,60,61,48,46,53,41,123,10,9,9,112,
-  42,61,50,10,9,9,120,61,115,113,114,116,40,120,41,10,9,125,10,9,114,61,97,61,
-  40,120,45,49,41,47,40,120,43,49,41,10,9,113,61,97,42,97,10,9,118,61,49,10,9,
-  102,111,114,40,105,61,51,59,118,33,61,48,59,105,43,61,50,41,123,10,9,9,97,42,
-  61,113,10,9,9,118,61,97,47,105,10,9,9,114,43,61,118,10,9,125,10,9,114,42,61,
-  112,10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,114,101,
-  116,117,114,110,40,114,47,49,41,10,125,10,100,101,102,105,110,101,32,115,40,
-  120,41,123,10,9,97,117,116,111,32,98,44,115,44,114,44,110,44,97,44,113,44,105,
-  10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,115,61,115,99,
-  97,108,101,10,9,115,99,97,108,101,61,49,46,49,42,115,43,50,10,9,97,61,97,40,
-  49,41,10,9,105,102,40,120,60,48,41,123,10,9,9,110,61,49,10,9,9,120,61,45,120,
-  10,9,125,10,9,115,99,97,108,101,61,48,10,9,113,61,40,120,47,97,43,50,41,47,
-  52,10,9,120,61,120,45,52,42,113,42,97,10,9,105,102,40,113,37,50,33,61,48,41,
-  120,61,45,120,10,9,115,99,97,108,101,61,115,43,50,10,9,114,61,97,61,120,10,
-  9,113,61,45,120,42,120,10,9,102,111,114,40,105,61,51,59,97,33,61,48,59,105,
-  43,61,50,41,123,10,9,9,97,42,61,113,47,40,105,42,40,105,45,49,41,41,10,9,9,
-  114,43,61,97,10,9,125,10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,
-  61,98,10,9,105,102,40,110,33,61,48,41,114,101,116,117,114,110,40,45,114,47,
-  49,41,10,9,114,101,116,117,114,110,40,114,47,49,41,10,125,10,100,101,102,105,
-  110,101,32,99,40,120,41,123,10,9,97,117,116,111,32,98,44,115,10,9,98,61,105,
-  98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,115,61,115,99,97,108,101,10,
-  9,115,99,97,108,101,42,61,49,46,50,10,9,120,61,115,40,50,42,97,40,49,41,43,
-  120,41,10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,114,
-  101,116,117,114,110,40,120,47,49,41,10,125,10,100,101,102,105,110,101,32,97,
-  40,120,41,123,10,9,97,117,116,111,32,98,44,115,44,114,44,110,44,97,44,109,44,
-  116,44,102,44,105,44,117,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,
-  61,65,10,9,110,61,49,10,9,105,102,40,120,60,48,41,123,10,9,9,110,61,45,49,10,
-  9,9,120,61,45,120,10,9,125,10,9,105,102,40,120,61,61,49,41,123,10,9,9,105,102,
-  40,115,99,97,108,101,60,54,53,41,123,10,9,9,9,114,101,116,117,114,110,40,46,
-  55,56,53,51,57,56,49,54,51,51,57,55,52,52,56,51,48,57,54,49,53,54,54,48,56,
-  52,53,56,49,57,56,55,53,55,50,49,48,52,57,50,57,50,51,52,57,56,52,51,55,55,
-  54,52,53,53,50,52,51,55,51,54,49,52,56,48,47,110,41,10,9,9,125,10,9,125,10,
-  9,105,102,40,120,61,61,46,50,41,123,10,9,9,105,102,40,115,99,97,108,101,60,
-  54,53,41,123,10,9,9,9,114,101,116,117,114,110,40,46,49,57,55,51,57,53,53,53,
-  57,56,52,57,56,56,48,55,53,56,51,55,48,48,52,57,55,54,53,49,57,52,55,57,48,
-  50,57,51,52,52,55,53,56,53,49,48,51,55,56,55,56,53,50,49,48,49,53,49,55,54,
-  56,56,57,52,48,50,47,110,41,10,9,9,125,10,9,125,10,9,115,61,115,99,97,108,101,
-  10,9,105,102,40,120,62,46,50,41,123,10,9,9,115,99,97,108,101,43,61,53,10,9,
-  9,97,61,97,40,46,50,41,10,9,125,10,9,115,99,97,108,101,61,115,43,51,10,9,119,
-  104,105,108,101,40,120,62,46,50,41,123,10,9,9,109,43,61,49,10,9,9,120,61,40,
-  120,45,46,50,41,47,40,49,43,46,50,42,120,41,10,9,125,10,9,114,61,117,61,120,
-  10,9,102,61,45,120,42,120,10,9,116,61,49,10,9,102,111,114,40,105,61,51,59,116,
-  33,61,48,59,105,43,61,50,41,123,10,9,9,117,42,61,102,10,9,9,116,61,117,47,105,
-  10,9,9,114,43,61,116,10,9,125,10,9,115,99,97,108,101,61,115,10,9,105,98,97,
-  115,101,61,98,10,9,114,101,116,117,114,110,40,40,109,42,97,43,114,41,47,110,
-  41,10,125,10,100,101,102,105,110,101,32,106,40,110,44,120,41,123,10,9,97,117,
-  116,111,32,98,44,115,44,111,44,97,44,105,44,118,44,102,10,9,98,61,105,98,97,
-  115,101,10,9,105,98,97,115,101,61,65,10,9,115,61,115,99,97,108,101,10,9,115,
-  99,97,108,101,61,48,10,9,110,47,61,49,10,9,105,102,40,110,60,48,41,123,10,9,
-  9,110,61,45,110,10,9,9,105,102,40,110,37,50,61,61,49,41,111,61,49,10,9,125,
-  10,9,97,61,49,10,9,102,111,114,40,105,61,50,59,105,60,61,110,59,43,43,105,41,
-  97,42,61,105,10,9,115,99,97,108,101,61,49,46,53,42,115,10,9,97,61,40,120,94,
-  110,41,47,50,94,110,47,97,10,9,114,61,118,61,49,10,9,102,61,45,120,42,120,47,
-  52,10,9,115,99,97,108,101,61,115,99,97,108,101,43,108,101,110,103,116,104,40,
-  97,41,45,115,99,97,108,101,40,97,41,10,9,102,111,114,40,105,61,49,59,118,33,
-  61,48,59,43,43,105,41,123,10,9,9,118,61,118,42,102,47,105,47,40,110,43,105,
-  41,10,9,9,114,43,61,118,10,9,125,10,9,115,99,97,108,101,61,115,10,9,105,98,
-  97,115,101,61,98,10,9,105,102,40,111,33,61,48,41,97,61,45,97,10,9,114,101,116,
-  117,114,110,40,97,42,114,47,49,41,10,125,10,0
-};
-#endif // ENABLE_BC
+       // Are non-POSIX constructs totally ok?
+       if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
+               return BC_STATUS_SUCCESS; // yes
+
+       va_start(p, fmt);
+       bc_verror_msg(fmt, p);
+       va_end(p);
+
+       // Do we treat non-POSIX constructs as errors?
+       if (!(option_mask32 & BC_FLAG_S))
+               return BC_STATUS_SUCCESS; // no, it's a warning
+       if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
+               exit(1);
+       return BC_STATUS_FAILURE;
+}
+#endif
+
+// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
+// This idiom begs for tail-call optimization, but for it to work,
+// function must not have caller-cleaned parameters on stack.
+// Unfortunately, vararg function API does exactly that on most arches.
+// Thus, use these shims for the cases when we have no vararg PARAMS:
+static int bc_error(const char *msg)
+{
+       return bc_error_fmt("%s", msg);
+}
+#if ENABLE_BC
+static int bc_POSIX_requires(const char *msg)
+{
+       return bc_posix_error_fmt("POSIX requires %s", msg);
+}
+static int bc_POSIX_does_not_allow(const char *msg)
+{
+       return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
+}
+static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
+{
+       return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
+}
+static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
+{
+       return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
+}
+#endif
+static int bc_error_bad_character(char c)
+{
+       return bc_error_fmt("bad character '%c'", c);
+}
+static int bc_error_bad_expression(void)
+{
+       return bc_error("bad expression");
+}
+static int bc_error_bad_token(void)
+{
+       return bc_error("bad token");
+}
+static int bc_error_stack_has_too_few_elements(void)
+{
+       return bc_error("stack has too few elements");
+}
+static int bc_error_variable_is_wrong_type(void)
+{
+       return bc_error("variable is wrong type");
+}
+static int bc_error_nested_read_call(void)
+{
+       return bc_error("read() call inside of a read() call");
+}
 
 static void bc_vec_grow(BcVec *v, size_t n)
 {
@@ -1202,6 +1065,11 @@ static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
        v->v = xmalloc(esize * BC_VEC_START_CAP);
 }
 
+static void bc_char_vec_init(BcVec *v)
+{
+       bc_vec_init(v, sizeof(char), NULL);
+}
+
 static void bc_vec_expand(BcVec *v, size_t req)
 {
        if (v->cap < req) {
@@ -1210,6 +1078,13 @@ static void bc_vec_expand(BcVec *v, size_t req)
        }
 }
 
+static void bc_vec_pop(BcVec *v)
+{
+       v->len--;
+       if (v->dtor)
+               v->dtor(v->v + (v->size * v->len));
+}
+
 static void bc_vec_npop(BcVec *v, size_t n)
 {
        if (!v->dtor)
@@ -1220,6 +1095,11 @@ static void bc_vec_npop(BcVec *v, size_t n)
        }
 }
 
+static void bc_vec_pop_all(BcVec *v)
+{
+       bc_vec_npop(v, v->len);
+}
+
 static void bc_vec_push(BcVec *v, const void *data)
 {
        if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
@@ -1232,6 +1112,13 @@ static void bc_vec_pushByte(BcVec *v, char data)
        bc_vec_push(v, &data);
 }
 
+static void bc_vec_pushZeroByte(BcVec *v)
+{
+       //bc_vec_pushByte(v, '\0');
+       // better:
+       bc_vec_push(v, &const_int_0);
+}
+
 static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
 {
        if (idx == v->len)
@@ -1251,24 +1138,24 @@ static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
 
 static void bc_vec_string(BcVec *v, size_t len, const char *str)
 {
-       bc_vec_npop(v, v->len);
+       bc_vec_pop_all(v);
        bc_vec_expand(v, len + 1);
        memcpy(v->v, str, len);
        v->len = len;
 
-       bc_vec_pushByte(v, '\0');
+       bc_vec_pushZeroByte(v);
 }
 
 static void bc_vec_concat(BcVec *v, const char *str)
 {
        size_t len;
 
-       if (v->len == 0) bc_vec_pushByte(v, '\0');
+       if (v->len == 0) bc_vec_pushZeroByte(v);
 
        len = v->len + strlen(str);
 
        if (v->cap < len) bc_vec_grow(v, len - v->len);
-       strcat(v->v, str);
+       strcpy(v->v + v->len - 1, str);
 
        v->len = len;
 }
@@ -1283,13 +1170,28 @@ static void *bc_vec_item_rev(const BcVec *v, size_t idx)
        return v->v + v->size * (v->len - idx - 1);
 }
 
+static void *bc_vec_top(const BcVec *v)
+{
+       return v->v + v->size * (v->len - 1);
+}
+
 static void bc_vec_free(void *vec)
 {
        BcVec *v = (BcVec *) vec;
-       bc_vec_npop(v, v->len);
+       bc_vec_pop_all(v);
        free(v->v);
 }
 
+static int bc_id_cmp(const void *e1, const void *e2)
+{
+       return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
+}
+
+static void bc_id_free(void *id)
+{
+       free(((BcId *) id)->name);
+}
+
 static size_t bc_map_find(const BcVec *v, const void *ptr)
 {
        size_t low = 0, high = v->len;
@@ -1311,132 +1213,139 @@ static size_t bc_map_find(const BcVec *v, const void *ptr)
        return low;
 }
 
-static BcStatus bc_map_insert(BcVec *v, const void *ptr, size_t *i)
+static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
 {
-       BcStatus s = BC_STATUS_SUCCESS;
-
-       *i = bc_map_find(v, ptr);
+       size_t n = *i = bc_map_find(v, ptr);
 
-       if (*i == v->len)
+       if (n == v->len)
                bc_vec_push(v, ptr);
-       else if (!bc_id_cmp(ptr, bc_vec_item(v, *i)))
-               s = BC_STATUS_VEC_ITEM_EXISTS;
+       else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
+               return 0; // "was not inserted"
        else
-               bc_vec_pushAt(v, ptr, *i);
-
-       return s;
+               bc_vec_pushAt(v, ptr, n);
+       return 1; // "was inserted"
 }
 
+#if ENABLE_BC
 static size_t bc_map_index(const BcVec *v, const void *ptr)
 {
        size_t i = bc_map_find(v, ptr);
        if (i >= v->len) return BC_VEC_INVALID_IDX;
        return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
 }
+#endif
 
-static BcStatus bc_read_line(BcVec *vec, const char *prompt)
+static int push_input_byte(BcVec *vec, char c)
 {
-       int i;
-       signed char c;
+       if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
+        || c > 0x7e
+       ) {
+               // Bad chars on this line, ignore entire line
+               bc_error_fmt("illegal character 0x%02x", c);
+               return 1;
+       }
+       bc_vec_pushByte(vec, (char)c);
+       return 0;
+}
 
-       bc_vec_npop(vec, vec->len);
+static BcStatus bc_read_line(BcVec *vec, const char *prompt)
+{
+       bool bad_chars;
 
-       fflush(stdout);
-#if ENABLE_FEATURE_BC_SIGNALS
-       if (bb_got_signal) { /* ^C was pressed */
- intr:
-               fputs(IS_BC
-                       ? "\ninterrupt (type \"quit\" to exit)\n"
-                       : "\ninterrupt (type \"q\" to exit)\n"
-                       , stderr);
-       }
-       bb_got_signal = 0; /* resets G_interrupt to zero */
-#endif
-       if (G.ttyin && !G_posix)
-               fputs(prompt, stderr);
-       fflush(stderr);
+       if (G_posix) prompt = "";
 
-#if ENABLE_FEATURE_BC_SIGNALS
- again:
-       errno = 0;
-#endif
        do {
-               if (ferror(stdout) || ferror(stderr))
-                       bb_perror_msg_and_die("output error");
+               int c;
 
-               i = fgetc(stdin);
+               bad_chars = 0;
+               bc_vec_pop_all(vec);
 
-#if ENABLE_FEATURE_BC_SIGNALS
-               if (bb_got_signal) /* ^C was pressed */
-                       goto intr;
-#endif
+               fflush_and_check();
 
-               if (i == EOF) {
 #if ENABLE_FEATURE_BC_SIGNALS
-                       if (errno == EINTR) {
-                               clearerr(stdin);
-                               goto again;
+               if (G_interrupt) { // ^C was pressed
+ intr:
+                       G_interrupt = 0;
+                       fputs(IS_BC
+                               ? "\ninterrupt (type \"quit\" to exit)\n"
+                               : "\ninterrupt (type \"q\" to exit)\n"
+                               , stderr);
+               }
+# if ENABLE_FEATURE_EDITING
+               if (G_ttyin) {
+                       int n, i;
+#  define line_buf bb_common_bufsiz1
+                       n = read_line_input(G.line_input_state, prompt, line_buf, COMMON_BUFSIZE);
+                       if (n <= 0) { // read errors or EOF, or ^D, or ^C
+                               if (n == 0) // ^C
+                                       goto intr;
+                               G.eof = 1;
+                               break;
                        }
+                       i = 0;
+                       for (;;) {
+                               c = line_buf[i++];
+                               if (!c) break;
+                               bad_chars |= push_input_byte(vec, c);
+                       }
+#  undef line_buf
+               } else
+# endif
 #endif
-                       if (ferror(stdin))
-                               bb_perror_msg_and_die("input error");
-                       return BC_STATUS_INPUT_EOF;
+               {
+                       if (G_ttyin)
+                               fputs(prompt, stderr);
+                       IF_FEATURE_BC_SIGNALS(errno = 0;)
+                       do {
+                               c = fgetc(stdin);
+#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
+                               // Both conditions appear simultaneously, check both just in case
+                               if (errno == EINTR || G_interrupt) {
+                                       // ^C was pressed
+                                       clearerr(stdin);
+                                       goto intr;
+                               }
+#endif
+                               if (c == EOF) {
+                                       if (ferror(stdin))
+                                               quit(); // this emits error message
+                                       G.eof = 1;
+                                       // Note: EOF does not append '\n', therefore:
+                                       // printf 'print 123\n' | bc - works
+                                       // printf 'print 123' | bc   - fails (syntax error)
+                                       break;
+                               }
+                               bad_chars |= push_input_byte(vec, c);
+                       } while (c != '\n');
                }
+       } while (bad_chars);
 
-               c = (signed char) i;
-               if (i > UCHAR_MAX || BC_READ_BIN_CHAR(c)) return BC_STATUS_BIN_FILE;
-               bc_vec_push(vec, &c);
-       } while (c != '\n');
-
-       bc_vec_pushByte(vec, '\0');
+       bc_vec_pushZeroByte(vec);
 
        return BC_STATUS_SUCCESS;
 }
 
-static BcStatus bc_read_file(const char *path, char **buf)
+static char* bc_read_file(const char *path)
 {
-       BcStatus s = BC_STATUS_BIN_FILE;
+       char *buf;
        size_t size = ((size_t) -1);
        size_t i;
 
-       *buf = xmalloc_open_read_close(path, &size);
+       // Never returns NULL (dies on errors)
+       buf = xmalloc_xopen_read_close(path, &size);
 
        for (i = 0; i < size; ++i) {
-               if (BC_READ_BIN_CHAR((*buf)[i]))
-                       goto read_err;
+               char c = buf[i];
+               if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
+                || c > 0x7e
+               ) {
+                       free(buf);
+                       buf = NULL;
+                       break;
+               }
        }
 
-       return BC_STATUS_SUCCESS;
-
-read_err:
-       free(*buf);
-       return s;
-}
-
-static void bc_args(int argc, char **argv)
-{
-       int i;
-
-       GETOPT_RESET();
-#if ENABLE_FEATURE_BC_LONG_OPTIONS
-       G.flags = getopt32long(argv, "xwvsqli",
-               "extended-register\0" No_argument "x"
-               "warn\0"              No_argument "w"
-               "version\0"           No_argument "v"
-               "standard\0"          No_argument "s"
-               "quiet\0"             No_argument "q"
-               "mathlib\0"           No_argument "l"
-               "interactive\0"       No_argument "i"
-       );
-#else
-       G.flags = getopt32(argv, "xwvsqli");
-#endif
-
-       if (G.flags & BC_FLAG_V) bc_vm_info();
-       // should not be necessary, getopt32() handles this??
-       //if (argv[optind] && !strcmp(argv[optind], "--")) ++optind;
-
-       for (i = optind; i < argc; ++i) bc_vec_push(&G.files, argv + i);
+       return buf;
 }
 
 static void bc_num_setToZero(BcNum *n, size_t scale)
@@ -1466,24 +1375,91 @@ static void bc_num_ten(BcNum *n)
        n->num[1] = 1;
 }
 
-static BcStatus bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
+static void bc_num_init(BcNum *n, size_t req)
+{
+       req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
+       memset(n, 0, sizeof(BcNum));
+       n->num = xmalloc(req);
+       n->cap = req;
+}
+
+static void bc_num_expand(BcNum *n, size_t req)
+{
+       req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
+       if (req > n->cap) {
+               n->num = xrealloc(n->num, req);
+               n->cap = req;
+       }
+}
+
+static void bc_num_free(void *num)
+{
+       free(((BcNum *) num)->num);
+}
+
+static void bc_num_copy(BcNum *d, BcNum *s)
+{
+       if (d != s) {
+               bc_num_expand(d, s->cap);
+               d->len = s->len;
+               d->neg = s->neg;
+               d->rdx = s->rdx;
+               memcpy(d->num, s->num, sizeof(BcDig) * d->len);
+       }
+}
+
+static BcStatus bc_num_ulong(BcNum *n, unsigned long *result)
+{
+       size_t i;
+       unsigned long pow;
+
+       if (n->neg) return bc_error("negative number");
+
+       for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
+
+               unsigned long prev = *result, powprev = pow;
+
+               *result += ((unsigned long) n->num[i]) * pow;
+               pow *= 10;
+
+               if (*result < prev || pow < powprev)
+                       return bc_error("overflow");
+       }
+
+       return BC_STATUS_SUCCESS;
+}
+
+static void bc_num_ulong2num(BcNum *n, unsigned long val)
+{
+       size_t len;
+       BcDig *ptr;
+       unsigned long i;
+
+       bc_num_zero(n);
+
+       if (val == 0) return;
+
+       for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len);
+       for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
+}
+
+static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
                                  size_t len)
 {
        size_t i, j;
-       for (i = 0; !G_interrupt && i < len; ++i) {
-               for (a[i] -= b[i], j = 0; !G_interrupt && a[i + j] < 0;) {
+       for (i = 0; i < len; ++i) {
+               for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
                        a[i + j++] += 10;
                        a[i + j] -= 1;
                }
        }
-       return G_interrupt ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
 }
 
 static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
 {
        size_t i;
        int c = 0;
-       for (i = len - 1; !G_interrupt && i < len && !(c = a[i] - b[i]); --i);
+       for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
        return BC_NUM_NEG(i + 1, c < 0);
 }
 
@@ -1529,7 +1505,7 @@ static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
        cmp = bc_num_compare(max_num, min_num, b_int + min);
        if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
 
-       for (max_num -= diff, i = diff - 1; !G_interrupt && i < diff; --i) {
+       for (max_num -= diff, i = diff - 1; i < diff; --i) {
                if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
        }
 
@@ -1608,7 +1584,8 @@ static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
 static BcStatus bc_num_shift(BcNum *n, size_t places)
 {
        if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS;
-       if (places + n->len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
+       if (places + n->len > BC_MAX_NUM)
+               return bc_error("number too long: must be [1, BC_NUM_MAX]");
 
        if (n->rdx >= places)
                n->rdx -= places;
@@ -1688,13 +1665,13 @@ static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
                ptr = ptr_b;
        }
 
-       for (carry = 0, i = 0; !G_interrupt && i < min_rdx + min_int; ++i, ++c->len) {
+       for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
                in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
                carry = in / 10;
                ptr_c[i] = (BcDig)(in % 10);
        }
 
-       for (; !G_interrupt && i < max + min_rdx; ++i, ++c->len) {
+       for (; i < max + min_rdx; ++i, ++c->len) {
                in = ((int) ptr[i]) + carry;
                carry = in / 10;
                ptr_c[i] = (BcDig)(in % 10);
@@ -1702,12 +1679,11 @@ static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
 
        if (carry != 0) c->num[c->len++] = (BcDig) carry;
 
-       return G_interrupt ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
+       return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
 }
 
 static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
 {
-       BcStatus s;
        ssize_t cmp;
        BcNum *minuend, *subtrahend;
        size_t start;
@@ -1761,28 +1737,27 @@ static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
        else
                start = c->rdx - subtrahend->rdx;
 
-       s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
+       bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
 
        bc_num_clean(c);
 
-       return s;
+       return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
 }
 
 static BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
                          BcNum *restrict c)
 {
        BcStatus s;
-       int carry;
-       size_t i, j, len, max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
+       size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
        BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
-       bool aone = BC_NUM_ONE(a);
+       bool aone;
 
-       if (G_interrupt) return BC_STATUS_EXEC_SIGNAL;
        if (a->len == 0 || b->len == 0) {
                bc_num_zero(c);
                return BC_STATUS_SUCCESS;
        }
-       else if (aone || BC_NUM_ONE(b)) {
+       aone = BC_NUM_ONE(a);
+       if (aone || BC_NUM_ONE(b)) {
                bc_num_copy(c, aone ? b : a);
                return BC_STATUS_SUCCESS;
        }
@@ -1790,28 +1765,36 @@ static BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
        if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
            a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
        {
+               size_t i, j, len;
+               unsigned carry;
+
                bc_num_expand(c, a->len + b->len + 1);
 
                memset(c->num, 0, sizeof(BcDig) * c->cap);
-               c->len = carry = len = 0;
+               c->len = len = 0;
 
-               for (i = 0; !G_interrupt && i < b->len; ++i) {
+               for (i = 0; i < b->len; ++i) {
 
-                       for (j = 0; !G_interrupt && j < a->len; ++j) {
-                               int in = (int) c->num[i + j];
-                               in += ((int) a->num[j]) * ((int) b->num[i]) + carry;
+                       carry = 0;
+                       for (j = 0; j < a->len; ++j) {
+                               unsigned in = c->num[i + j];
+                               in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
+                               // note: compilers prefer _unsigned_ div/const
                                carry = in / 10;
                                c->num[i + j] = (BcDig)(in % 10);
                        }
 
                        c->num[i + j] += (BcDig) carry;
                        len = BC_MAX(len, i + j + !!carry);
-                       carry = 0;
+
+                       // a=2^1000000
+                       // a*a <- without check below, this will not be interruptible
+                       if (G_interrupt) return BC_STATUS_FAILURE;
                }
 
                c->len = len;
 
-               return G_interrupt ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
+               return BC_STATUS_SUCCESS;
        }
 
        bc_num_init(&l1, max);
@@ -1918,7 +1901,7 @@ static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
        bool zero = true;
 
        if (b->len == 0)
-               return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+               return bc_error("divide by zero");
        else if (a->len == 0) {
                bc_num_setToZero(c, scale);
                return BC_STATUS_SUCCESS;
@@ -1961,14 +1944,21 @@ static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
        c->len = cp.len;
        p = b->num;
 
-       for (i = end - 1; !G_interrupt && !s && i < end; --i) {
+       for (i = end - 1; !s && i < end; --i) {
                n = cp.num + i;
                for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
-                       s = bc_num_subArrays(n, p, len);
+                       bc_num_subArrays(n, p, len);
                c->num[i] = q;
+               // a=2^100000
+               // scale=40000
+               // 1/a <- without check below, this will not be interruptible
+               if (G_interrupt) {
+                       s = BC_STATUS_FAILURE;
+                       break;
+               }
        }
 
-       if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
+       bc_num_retireMul(c, scale, a->neg, b->neg);
        bc_num_free(&cp);
 
        return s;
@@ -1981,7 +1971,8 @@ static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
        BcNum temp;
        bool neg;
 
-       if (b->len == 0) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+       if (b->len == 0)
+               return bc_error("divide by zero");
 
        if (a->len == 0) {
                bc_num_setToZero(d, ts);
@@ -1989,7 +1980,8 @@ static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
        }
 
        bc_num_init(&temp, d->cap);
-       bc_num_d(a, b, c, scale);
+       s = bc_num_d(a, b, c, scale);
+       if (s) goto err;
 
        if (scale != 0) scale = ts;
 
@@ -2030,7 +2022,7 @@ static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
        size_t i, powrdx, resrdx;
        bool neg, zero;
 
-       if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
+       if (b->rdx) return bc_error("non integer number");
 
        if (b->len == 0) {
                bc_num_one(c);
@@ -2061,20 +2053,20 @@ static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
 
        b->neg = neg;
 
-       for (powrdx = a->rdx; !G_interrupt && !(pow & 1); pow >>= 1) {
+       for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
                powrdx <<= 1;
                s = bc_num_mul(&copy, &copy, &copy, powrdx);
                if (s) goto err;
-       }
-
-       if (G_interrupt) {
-               s = BC_STATUS_EXEC_SIGNAL;
-               goto err;
+               // Not needed: bc_num_mul() has a check for ^C:
+               //if (G_interrupt) {
+               //      s = BC_STATUS_FAILURE;
+               //      goto err;
+               //}
        }
 
        bc_num_copy(c, &copy);
 
-       for (resrdx = powrdx, pow >>= 1; !G_interrupt && pow != 0; pow >>= 1) {
+       for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
 
                powrdx <<= 1;
                s = bc_num_mul(&copy, &copy, &copy, powrdx);
@@ -2085,6 +2077,11 @@ static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
                        s = bc_num_mul(c, &copy, c, resrdx);
                        if (s) goto err;
                }
+               // Not needed: bc_num_mul() has a check for ^C:
+               //if (G_interrupt) {
+               //      s = BC_STATUS_FAILURE;
+               //      goto err;
+               //}
        }
 
        if (neg) {
@@ -2092,11 +2089,6 @@ static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
                if (s) goto err;
        }
 
-       if (G_interrupt) {
-               s = BC_STATUS_EXEC_SIGNAL;
-               goto err;
-       }
-
        if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
 
        // We can't use bc_num_clean() here.
@@ -2194,8 +2186,9 @@ static void bc_num_parseDecimal(BcNum *n, const char *val)
 
        ptr = strchr(val, '.');
 
-       // Explicitly test for NULL here to produce either a 0 or 1.
-       n->rdx = (size_t)((ptr != NULL) * ((val + len) - (ptr + 1)));
+       n->rdx = 0;
+       if (ptr != NULL)
+               n->rdx = (size_t)((val + len) - (ptr + 1));
 
        if (!zero) {
                for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
@@ -2229,8 +2222,7 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
 
                s = bc_num_mul(n, base, &mult, 0);
                if (s) goto int_err;
-               s = bc_num_ulong2num(&temp, v);
-               if (s) goto int_err;
+               bc_num_ulong2num(&temp, v);
                s = bc_num_add(&mult, &temp, n, 0);
                if (s) goto int_err;
        }
@@ -2253,8 +2245,7 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
 
                s = bc_num_mul(&result, base, &result, 0);
                if (s) goto err;
-               s = bc_num_ulong2num(&temp, v);
-               if (s) goto err;
+               bc_num_ulong2num(&temp, v);
                s = bc_num_add(&result, &temp, &result, 0);
                if (s) goto err;
                s = bc_num_mul(&mult, base, &mult, 0);
@@ -2392,8 +2383,7 @@ static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width,
                if (s) goto err;
                s = bc_num_ulong(&fracp, &dig);
                if (s) goto err;
-               s = bc_num_ulong2num(&intp, dig);
-               if (s) goto err;
+               bc_num_ulong2num(&intp, dig);
                s = bc_num_sub(&fracp, &intp, &fracp, 0);
                if (s) goto err;
                print(dig, width, radix, nchars, len);
@@ -2445,43 +2435,11 @@ static BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len)
 }
 #endif
 
-static void bc_num_init(BcNum *n, size_t req)
-{
-       req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
-       memset(n, 0, sizeof(BcNum));
-       n->num = xmalloc(req);
-       n->cap = req;
-}
-
-static void bc_num_expand(BcNum *n, size_t req)
-{
-       req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
-       if (req > n->cap) {
-               n->num = xrealloc(n->num, req);
-               n->cap = req;
-       }
-}
-
-static void bc_num_free(void *num)
-{
-       free(((BcNum *) num)->num);
-}
-
-static void bc_num_copy(BcNum *d, BcNum *s)
-{
-       if (d != s) {
-               bc_num_expand(d, s->cap);
-               d->len = s->len;
-               d->neg = s->neg;
-               d->rdx = s->rdx;
-               memcpy(d->num, s->num, sizeof(BcDig) * d->len);
-       }
-}
-
 static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base,
                              size_t base_t)
 {
-       if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
+       if (!bc_num_strValid(val, base_t))
+               return bc_error("bad number string");
 
        if (base_t == 10)
                bc_num_parseDecimal(n, val);
@@ -2515,42 +2473,6 @@ static BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
        return s;
 }
 
-static BcStatus bc_num_ulong(BcNum *n, unsigned long *result)
-{
-       size_t i;
-       unsigned long pow;
-
-       if (n->neg) return BC_STATUS_MATH_NEGATIVE;
-
-       for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
-
-               unsigned long prev = *result, powprev = pow;
-
-               *result += ((unsigned long) n->num[i]) * pow;
-               pow *= 10;
-
-               if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
-       }
-
-       return BC_STATUS_SUCCESS;
-}
-
-static BcStatus bc_num_ulong2num(BcNum *n, unsigned long val)
-{
-       size_t len;
-       BcDig *ptr;
-       unsigned long i;
-
-       bc_num_zero(n);
-
-       if (val == 0) return BC_STATUS_SUCCESS;
-
-       for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len);
-       for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
-
-       return BC_STATUS_SUCCESS;
-}
-
 static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
 {
        BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
@@ -2603,7 +2525,7 @@ static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
                return BC_STATUS_SUCCESS;
        }
        else if (a->neg)
-               return BC_STATUS_MATH_NEGATIVE;
+               return bc_error("negative number");
        else if (BC_NUM_ONE(a)) {
                bc_num_one(b);
                bc_num_extend(b, scale);
@@ -2649,7 +2571,7 @@ static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
        resrdx = scale + 2;
        len = BC_NUM_INT(x0) + resrdx - 1;
 
-       while (!G_interrupt && (cmp != 0 || digs < len)) {
+       while (cmp != 0 || digs < len) {
 
                s = bc_num_div(a, x0, &f, resrdx);
                if (s) goto err;
@@ -2677,11 +2599,6 @@ static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
                x1 = temp;
        }
 
-       if (G_interrupt) {
-               s = BC_STATUS_EXEC_SIGNAL;
-               goto err;
-       }
-
        bc_num_copy(b, x0);
        scale -= 1;
        if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
@@ -2727,9 +2644,12 @@ static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
        BcStatus s;
        BcNum base, exp, two, temp;
 
-       if (c->len == 0) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
-       if (a->rdx || b->rdx || c->rdx) return BC_STATUS_MATH_NON_INTEGER;
-       if (b->neg) return BC_STATUS_MATH_NEGATIVE;
+       if (c->len == 0)
+               return bc_error("divide by zero");
+       if (a->rdx || b->rdx || c->rdx)
+               return bc_error("non integer number");
+       if (b->neg)
+               return bc_error("negative number");
 
        bc_num_expand(d, c->len);
        bc_num_init(&base, c->len);
@@ -2772,24 +2692,15 @@ err:
 }
 #endif // ENABLE_DC
 
-static int bc_id_cmp(const void *e1, const void *e2)
-{
-       return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
-}
-
-static void bc_id_free(void *id)
-{
-       free(((BcId *) id)->name);
-}
-
+#if ENABLE_BC
 static BcStatus bc_func_insert(BcFunc *f, char *name, bool var)
 {
        BcId a;
        size_t i;
 
        for (i = 0; i < f->autos.len; ++i) {
-               if (!strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name))
-                       return BC_STATUS_PARSE_DUPLICATE_LOCAL;
+               if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
+                       return bc_error("function parameter or auto var has the same name as another");
        }
 
        a.idx = var;
@@ -2799,10 +2710,11 @@ static BcStatus bc_func_insert(BcFunc *f, char *name, bool var)
 
        return BC_STATUS_SUCCESS;
 }
+#endif
 
 static void bc_func_init(BcFunc *f)
 {
-       bc_vec_init(&f->code, sizeof(char), NULL);
+       bc_char_vec_init(&f->code);
        bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
        bc_vec_init(&f->labels, sizeof(size_t), NULL);
        f->nparams = 0;
@@ -2816,6 +2728,8 @@ static void bc_func_free(void *func)
        bc_vec_free(&f->labels);
 }
 
+static void bc_array_expand(BcVec *a, size_t len);
+
 static void bc_array_init(BcVec *a, bool nums)
 {
        if (nums)
@@ -2825,21 +2739,6 @@ static void bc_array_init(BcVec *a, bool nums)
        bc_array_expand(a, 1);
 }
 
-static void bc_array_copy(BcVec *d, const BcVec *s)
-{
-       size_t i;
-
-       bc_vec_npop(d, d->len);
-       bc_vec_expand(d, s->cap);
-       d->len = s->len;
-
-       for (i = 0; i < s->len; ++i) {
-               BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
-               bc_num_init(dnum, snum->len);
-               bc_num_copy(dnum, snum);
-       }
-}
-
 static void bc_array_expand(BcVec *a, size_t len)
 {
        BcResultData data;
@@ -2858,6 +2757,21 @@ static void bc_array_expand(BcVec *a, size_t len)
        }
 }
 
+static void bc_array_copy(BcVec *d, const BcVec *s)
+{
+       size_t i;
+
+       bc_vec_pop_all(d);
+       bc_vec_expand(d, s->cap);
+       d->len = s->len;
+
+       for (i = 0; i < s->len; ++i) {
+               BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
+               bc_num_init(dnum, snum->len);
+               bc_num_copy(dnum, snum);
+       }
+}
+
 static void bc_string_free(void *string)
 {
        free(*((char **) string));
@@ -2970,10 +2884,11 @@ static BcStatus bc_lex_number(BcLex *l, char start)
                c = buf[++i];
        }
 
-       len = i + 1 * !last_pt - bslashes * 2;
-       if (len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
+       len = i + !last_pt - bslashes * 2;
+       if (len > BC_MAX_NUM)
+               return bc_error("number too long: must be [1, BC_NUM_MAX]");
 
-       bc_vec_npop(&l->t.v, l->t.v.len);
+       bc_vec_pop_all(&l->t.v);
        bc_vec_expand(&l->t.v, len + 1);
        bc_vec_push(&l->t.v, &start);
 
@@ -2992,7 +2907,7 @@ static BcStatus bc_lex_number(BcLex *l, char start)
                bc_vec_push(&l->t.v, &c);
        }
 
-       bc_vec_pushByte(&l->t.v, '\0');
+       bc_vec_pushZeroByte(&l->t.v);
        l->i += i;
 
        return BC_STATUS_SUCCESS;
@@ -3008,7 +2923,8 @@ static BcStatus bc_lex_name(BcLex *l)
 
        while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
 
-       if (i > BC_MAX_STRING) return BC_STATUS_EXEC_NAME_LEN;
+       if (i > BC_MAX_STRING)
+               return bc_error("name too long: must be [1, BC_NAME_MAX]");
        bc_vec_string(&l->t.v, i, buf);
 
        // Increment the index. We minus 1 because it has already been incremented.
@@ -3020,7 +2936,7 @@ static BcStatus bc_lex_name(BcLex *l)
 static void bc_lex_init(BcLex *l, BcLexNext next)
 {
        l->next = next;
-       bc_vec_init(&l->t.v, sizeof(char), NULL);
+       bc_char_vec_init(&l->t.v);
 }
 
 static void bc_lex_free(BcLex *l)
@@ -3028,11 +2944,10 @@ static void bc_lex_free(BcLex *l)
        bc_vec_free(&l->t.v);
 }
 
-static void bc_lex_file(BcLex *l, const char *file)
+static void bc_lex_file(BcLex *l)
 {
-       l->line = 1;
+       G.err_line = l->line = 1;
        l->newline = false;
-       l->f = file;
 }
 
 static BcStatus bc_lex_next(BcLex *l)
@@ -3040,9 +2955,10 @@ static BcStatus bc_lex_next(BcLex *l)
        BcStatus s;
 
        l->t.last = l->t.t;
-       if (l->t.last == BC_LEX_EOF) return BC_STATUS_LEX_EOF;
+       if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
 
        l->line += l->newline;
+       G.err_line = l->line;
        l->t.t = BC_LEX_EOF;
 
        l->newline = (l->i == l->len);
@@ -3070,34 +2986,42 @@ static BcStatus bc_lex_text(BcLex *l, const char *text)
 static BcStatus bc_lex_identifier(BcLex *l)
 {
        BcStatus s;
-       size_t i;
+       unsigned i;
        const char *buf = l->buf + l->i - 1;
 
-       for (i = 0; i < sizeof(bc_lex_kws) / sizeof(bc_lex_kws[0]); ++i) {
-
-               unsigned long len = (unsigned long) bc_lex_kws[i].len;
-
-               if (strncmp(buf, bc_lex_kws[i].name, len) == 0) {
-
-                       l->t.t = BC_LEX_KEY_AUTO + (BcLexType) i;
-
-                       if (!bc_lex_kws[i].posix) {
-                               s = bc_vm_posixError(BC_STATUS_POSIX_BAD_KW, l->f, l->line,
-                                                    bc_lex_kws[i].name);
-                               if (s) return s;
-                       }
-
-                       // We minus 1 because the index has already been incremented.
-                       l->i += len - 1;
-                       return BC_STATUS_SUCCESS;
+       for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
+               const char *keyword8 = bc_lex_kws[i].name8;
+               unsigned j = 0;
+               while (buf[j] != '\0' && buf[j] == keyword8[j]) {
+                       j++;
+                       if (j == 8) goto match;
                }
+               if (keyword8[j] != '\0')
+                       continue;
+ match:
+               // buf starts with keyword bc_lex_kws[i]
+               l->t.t = BC_LEX_KEY_1st_keyword + i;
+               if (!bc_lex_kws_POSIX(i)) {
+                       s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
+                       if (s) return s;
+               }
+
+               // We minus 1 because the index has already been incremented.
+               l->i += j - 1;
+               return BC_STATUS_SUCCESS;
        }
 
        s = bc_lex_name(l);
        if (s) return s;
 
-       if (l->t.v.len - 1 > 1)
-               s = bc_vm_posixError(BC_STATUS_POSIX_NAME_LEN, l->f, l->line, buf);
+       if (l->t.v.len > 2) {
+               // Prevent this:
+               // >>> qwe=1
+               // bc: POSIX only allows one character names; the following is bad: 'qwe=1
+               // '
+               unsigned len = strchrnul(buf, '\n') - buf;
+               s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
+       }
 
        return s;
 }
@@ -3113,15 +3037,17 @@ static BcStatus bc_lex_string(BcLex *l)
 
        if (c == '\0') {
                l->i = i;
-               return BC_STATUS_LEX_NO_STRING_END;
+               return bc_error("string end could not be found");
        }
 
        len = i - l->i;
-       if (len > BC_MAX_STRING) return BC_STATUS_EXEC_STRING_LEN;
+       if (len > BC_MAX_STRING)
+               return bc_error("string too long: must be [1, BC_STRING_MAX]");
        bc_vec_string(&l->t.v, len, l->buf + l->i);
 
        l->i = i + 1;
        l->line += nls;
+       G.err_line = l->line;
 
        return BC_STATUS_SUCCESS;
 }
@@ -3140,25 +3066,29 @@ static BcStatus bc_lex_comment(BcLex *l)
 {
        size_t i, nls = 0;
        const char *buf = l->buf;
-       bool end = false;
-       char c;
 
        l->t.t = BC_LEX_WHITESPACE;
-
-       for (i = ++l->i; !end; i += !end) {
-
-               for (c = buf[i]; c != '*' && c != 0; c = buf[++i]) nls += (c == '\n');
-
-               if (c == 0 || buf[i + 1] == '\0') {
+       i = ++l->i;
+       for (;;) {
+               char c = buf[i];
+ check_star:
+               if (c == '*') {
+                       c = buf[++i];
+                       if (c == '/')
+                               break;
+                       goto check_star;
+               }
+               if (c == '\0') {
                        l->i = i;
-                       return BC_STATUS_LEX_NO_COMMENT_END;
+                       return bc_error("comment end could not be found");
                }
-
-               end = buf[i + 1] == '/';
+               nls += (c == '\n');
+               i++;
        }
 
-       l->i = i + 2;
+       l->i = i + 1;
        l->line += nls;
+       G.err_line = l->line;
 
        return BC_STATUS_SUCCESS;
 }
@@ -3194,7 +3124,7 @@ static BcStatus bc_lex_token(BcLex *l)
                        bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
 
                        if (l->t.t == BC_LEX_OP_BOOL_NOT) {
-                               s = bc_vm_posixError(BC_STATUS_POSIX_BOOL, l->f, l->line, "!");
+                               s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
                                if (s) return s;
                        }
 
@@ -3209,7 +3139,7 @@ static BcStatus bc_lex_token(BcLex *l)
 
                case '#':
                {
-                       s = bc_vm_posixError(BC_STATUS_POSIX_COMMENT, l->f, l->line, NULL);
+                       s = bc_POSIX_does_not_allow("'#' script comments");
                        if (s) return s;
 
                        bc_lex_lineComment(l);
@@ -3228,7 +3158,7 @@ static BcStatus bc_lex_token(BcLex *l)
                        c2 = l->buf[l->i];
                        if (c2 == '&') {
 
-                               s = bc_vm_posixError(BC_STATUS_POSIX_BOOL, l->f, l->line, "&&");
+                               s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
                                if (s) return s;
 
                                ++l->i;
@@ -3236,7 +3166,7 @@ static BcStatus bc_lex_token(BcLex *l)
                        }
                        else {
                                l->t.t = BC_LEX_INVALID;
-                               s = BC_STATUS_LEX_BAD_CHAR;
+                               s = bc_error_bad_character('&');
                        }
 
                        break;
@@ -3291,7 +3221,7 @@ static BcStatus bc_lex_token(BcLex *l)
                                s = bc_lex_number(l, c);
                        else {
                                l->t.t = BC_LEX_KEY_LAST;
-                               s = bc_vm_posixError(BC_STATUS_POSIX_DOT, l->f, l->line, NULL);
+                               s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
                        }
                        break;
                }
@@ -3365,7 +3295,7 @@ static BcStatus bc_lex_token(BcLex *l)
                                ++l->i;
                        }
                        else
-                               s = BC_STATUS_LEX_BAD_CHAR;
+                               s = bc_error_bad_character(c);
                        break;
                }
 
@@ -3418,8 +3348,7 @@ static BcStatus bc_lex_token(BcLex *l)
                        c2 = l->buf[l->i];
 
                        if (c2 == '|') {
-
-                               s = bc_vm_posixError(BC_STATUS_POSIX_BOOL, l->f, l->line, "||");
+                               s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
                                if (s) return s;
 
                                ++l->i;
@@ -3427,7 +3356,7 @@ static BcStatus bc_lex_token(BcLex *l)
                        }
                        else {
                                l->t.t = BC_LEX_INVALID;
-                               s = BC_STATUS_LEX_BAD_CHAR;
+                               s = bc_error_bad_character(c);
                        }
 
                        break;
@@ -3436,7 +3365,7 @@ static BcStatus bc_lex_token(BcLex *l)
                default:
                {
                        l->t.t = BC_LEX_INVALID;
-                       s = BC_STATUS_LEX_BAD_CHAR;
+                       s = bc_error_bad_character(c);
                        break;
                }
        }
@@ -3454,14 +3383,14 @@ static BcStatus dc_lex_register(BcLex *l)
                bc_lex_whitespace(l);
                ++l->i;
                if (!G_exreg)
-                       s = BC_STATUS_LEX_EXTENDED_REG;
+                       s = bc_error("extended register");
                else
                        s = bc_lex_name(l);
        }
        else {
-               bc_vec_npop(&l->t.v, l->t.v.len);
-               bc_vec_pushByte(&l->t.v, l->buf[l->i - 1]);
-               bc_vec_pushByte(&l->t.v, '\0');
+               bc_vec_pop_all(&l->t.v);
+               bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
+               bc_vec_pushZeroByte(&l->t.v);
                l->t.t = BC_LEX_NAME;
        }
 
@@ -3474,7 +3403,7 @@ static BcStatus dc_lex_string(BcLex *l)
        char c;
 
        l->t.t = BC_LEX_STR;
-       bc_vec_npop(&l->t.v, l->t.v.len);
+       bc_vec_pop_all(&l->t.v);
 
        for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
 
@@ -3487,14 +3416,16 @@ static BcStatus dc_lex_string(BcLex *l)
 
        if (c == '\0') {
                l->i = i;
-               return BC_STATUS_LEX_NO_STRING_END;
+               return bc_error("string end could not be found");
        }
 
-       bc_vec_pushByte(&l->t.v, '\0');
-       if (i - l->i > BC_MAX_STRING) return BC_STATUS_EXEC_STRING_LEN;
+       bc_vec_pushZeroByte(&l->t.v);
+       if (i - l->i > BC_MAX_STRING)
+               return bc_error("string too long: must be [1, BC_STRING_MAX]");
 
        l->i = i;
        l->line += nls;
+       G.err_line = l->line;
 
        return BC_STATUS_SUCCESS;
 }
@@ -3505,8 +3436,9 @@ static BcStatus dc_lex_token(BcLex *l)
        char c = l->buf[l->i++], c2;
        size_t i;
 
-       for (i = 0; i < dc_lex_regs_len; ++i) {
-               if (l->t.last == dc_lex_regs[i]) return dc_lex_register(l);
+       for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
+               if (l->t.last == dc_lex_regs[i])
+                       return dc_lex_register(l);
        }
 
        if (c >= '%' && c <= '~' &&
@@ -3547,7 +3479,7 @@ static BcStatus dc_lex_token(BcLex *l)
                        else if (c2 == '>')
                                l->t.t = BC_LEX_OP_REL_GE;
                        else
-                               return BC_STATUS_LEX_BAD_CHAR;
+                               return bc_error_bad_character(c);
 
                        ++l->i;
                        break;
@@ -3564,7 +3496,7 @@ static BcStatus dc_lex_token(BcLex *l)
                        if (isdigit(l->buf[l->i]))
                                s = bc_lex_number(l, c);
                        else
-                               s = BC_STATUS_LEX_BAD_CHAR;
+                               s = bc_error_bad_character(c);
                        break;
                }
 
@@ -3598,7 +3530,7 @@ static BcStatus dc_lex_token(BcLex *l)
                default:
                {
                        l->t.t = BC_LEX_INVALID;
-                       s = BC_STATUS_LEX_BAD_CHAR;
+                       s = bc_error_bad_character(c);
                        break;
                }
        }
@@ -3607,12 +3539,16 @@ static BcStatus dc_lex_token(BcLex *l)
 }
 #endif // ENABLE_DC
 
+static void bc_program_addFunc(char *name, size_t *idx);
+
 static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
 {
-       bc_program_addFunc(p->prog, name, idx);
-       p->func = bc_vec_item(&p->prog->fns, p->fidx);
+       bc_program_addFunc(name, idx);
+       p->func = bc_vec_item(&G.prog.fns, p->fidx);
 }
 
+#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
+
 static void bc_parse_pushName(BcParse *p, char *name)
 {
        size_t i = 0, len = strlen(name);
@@ -3639,9 +3575,9 @@ static void bc_parse_pushIndex(BcParse *p, size_t idx)
 static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
 {
        char *num = xstrdup(p->l.t.v.v);
-       size_t idx = p->prog->consts.len;
+       size_t idx = G.prog.consts.len;
 
-       bc_vec_push(&p->prog->consts, &num);
+       bc_vec_push(&G.prog.consts, &num);
 
        bc_parse_push(p, BC_INST_NUM);
        bc_parse_pushIndex(p, idx);
@@ -3654,26 +3590,46 @@ static BcStatus bc_parse_text(BcParse *p, const char *text)
 {
        BcStatus s;
 
-       p->func = bc_vec_item(&p->prog->fns, p->fidx);
+       p->func = bc_vec_item(&G.prog.fns, p->fidx);
 
-       if (!strcmp(text, "") && !BC_PARSE_CAN_EXEC(p)) {
+       if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
                p->l.t.t = BC_LEX_INVALID;
                s = p->parse(p);
                if (s) return s;
-               if (!BC_PARSE_CAN_EXEC(p)) return BC_STATUS_EXEC_FILE_NOT_EXECUTABLE;
+               if (!BC_PARSE_CAN_EXEC(p))
+                       return bc_error("file is not executable");
        }
 
        return bc_lex_text(&p->l, text);
 }
 
-static BcStatus bc_parse_reset(BcParse *p, BcStatus s)
+// Called when parsing or execution detects a failure,
+// resets execution structures.
+static void bc_program_reset(void)
 {
-       if (p->fidx != BC_PROG_MAIN) {
+       BcFunc *f;
+       BcInstPtr *ip;
+
+       bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
+       bc_vec_pop_all(&G.prog.results);
+
+       f = bc_vec_item(&G.prog.fns, 0);
+       ip = bc_vec_top(&G.prog.stack);
+       ip->idx = f->code.len;
+}
+
+#define bc_parse_updateFunc(p, f) \
+       ((p)->func = bc_vec_item(&G.prog.fns, ((p)->fidx = (f))))
 
+// Called when bc/dc_parse_parse() detects a failure,
+// resets parsing structures.
+static void bc_parse_reset(BcParse *p)
+{
+       if (p->fidx != BC_PROG_MAIN) {
                p->func->nparams = 0;
-               bc_vec_npop(&p->func->code, p->func->code.len);
-               bc_vec_npop(&p->func->autos, p->func->autos.len);
-               bc_vec_npop(&p->func->labels, p->func->labels.len);
+               bc_vec_pop_all(&p->func->code);
+               bc_vec_pop_all(&p->func->autos);
+               bc_vec_pop_all(&p->func->labels);
 
                bc_parse_updateFunc(p, BC_PROG_MAIN);
        }
@@ -3683,11 +3639,11 @@ static BcStatus bc_parse_reset(BcParse *p, BcStatus s)
        p->auto_part = (p->nbraces = 0);
 
        bc_vec_npop(&p->flags, p->flags.len - 1);
-       bc_vec_npop(&p->exits, p->exits.len);
-       bc_vec_npop(&p->conds, p->conds.len);
-       bc_vec_npop(&p->ops, p->ops.len);
+       bc_vec_pop_all(&p->exits);
+       bc_vec_pop_all(&p->conds);
+       bc_vec_pop_all(&p->ops);
 
-       return bc_program_reset(p->prog, s);
+       bc_program_reset();
 }
 
 static void bc_parse_free(BcParse *p)
@@ -3699,7 +3655,7 @@ static void bc_parse_free(BcParse *p)
        bc_lex_free(&p->l);
 }
 
-static void bc_parse_create(BcParse *p, BcProgram *prog, size_t func,
+static void bc_parse_create(BcParse *p, size_t func,
                             BcParseParse parse, BcLexNext next)
 {
        memset(p, 0, sizeof(BcParse));
@@ -3708,33 +3664,45 @@ static void bc_parse_create(BcParse *p, BcProgram *prog, size_t func,
        bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
        bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
        bc_vec_init(&p->conds, sizeof(size_t), NULL);
-       bc_vec_pushByte(&p->flags, 0);
+       bc_vec_pushZeroByte(&p->flags);
        bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
 
        p->parse = parse;
-       p->prog = prog;
-       p->auto_part = (p->nbraces = 0);
+       // p->auto_part = p->nbraces = 0; - already is
        bc_parse_updateFunc(p, func);
 }
 
 #if ENABLE_BC
+
+#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
+#define BC_PARSE_LEAF(p, rparen)                                \
+       (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
+        (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
+
+// We can calculate the conversion between tokens and exprs by subtracting the
+// position of the first operator in the lex enum and adding the position of the
+// first in the expr enum. Note: This only works for binary operators.
+#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
+
 static BcStatus bc_parse_else(BcParse *p);
 static BcStatus bc_parse_stmt(BcParse *p);
+static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
+static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
 
 static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
                                   size_t *nexprs, bool next)
 {
        BcStatus s = BC_STATUS_SUCCESS;
        BcLexType t;
-       char l, r = bc_parse_ops[type - BC_LEX_OP_INC].prec;
-       bool left = bc_parse_ops[type - BC_LEX_OP_INC].left;
+       char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
+       bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
 
        while (p->ops.len > start) {
 
                t = BC_PARSE_TOP_OP(p);
                if (t == BC_LEX_LPAREN) break;
 
-               l = bc_parse_ops[t - BC_LEX_OP_INC].prec;
+               l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
                if (l >= r && (l != r || !left)) break;
 
                bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
@@ -3752,7 +3720,8 @@ static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
 {
        BcLexType top;
 
-       if (p->ops.len <= ops_bgn) return BC_STATUS_PARSE_BAD_EXP;
+       if (p->ops.len <= ops_bgn)
+               return bc_error_bad_expression();
        top = BC_PARSE_TOP_OP(p);
 
        while (top != BC_LEX_LPAREN) {
@@ -3762,7 +3731,8 @@ static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
                bc_vec_pop(&p->ops);
                *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
 
-               if (p->ops.len <= ops_bgn) return BC_STATUS_PARSE_BAD_EXP;
+               if (p->ops.len <= ops_bgn)
+                       return bc_error_bad_expression();
                top = BC_PARSE_TOP_OP(p);
        }
 
@@ -3793,7 +3763,7 @@ static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
                }
        }
 
-       if (comma) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (comma) return bc_error_bad_token();
        bc_parse_push(p, BC_INST_CALL);
        bc_parse_pushIndex(p, nparams);
 
@@ -3812,22 +3782,22 @@ static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
        if (s) goto err;
 
        if (p->l.t.t != BC_LEX_RPAREN) {
-               s = BC_STATUS_PARSE_BAD_TOKEN;
+               s = bc_error_bad_token();
                goto err;
        }
 
-       idx = bc_map_index(&p->prog->fn_map, &entry);
+       idx = bc_map_index(&G.prog.fn_map, &entry);
 
        if (idx == BC_VEC_INVALID_IDX) {
                name = xstrdup(entry.name);
                bc_parse_addFunc(p, name, &idx);
-               idx = bc_map_index(&p->prog->fn_map, &entry);
+               idx = bc_map_index(&G.prog.fn_map, &entry);
                free(entry.name);
        }
        else
                free(name);
 
-       entry_ptr = bc_vec_item(&p->prog->fn_map, idx);
+       entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
        bc_parse_pushIndex(p, entry_ptr->idx);
 
        return bc_lex_next(&p->l);
@@ -3854,7 +3824,7 @@ static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
                if (p->l.t.t == BC_LEX_RBRACKET) {
 
                        if (!(flags & BC_PARSE_ARRAY)) {
-                               s = BC_STATUS_PARSE_BAD_EXP;
+                               s = bc_error_bad_expression();
                                goto err;
                        }
 
@@ -3877,7 +3847,7 @@ static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
        else if (p->l.t.t == BC_LEX_LPAREN) {
 
                if (flags & BC_PARSE_NOCALL) {
-                       s = BC_STATUS_PARSE_BAD_TOKEN;
+                       s = bc_error_bad_token();
                        goto err;
                }
 
@@ -3903,11 +3873,11 @@ static BcStatus bc_parse_read(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
 
        bc_parse_push(p, BC_INST_READ);
 
@@ -3921,7 +3891,7 @@ static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
 
        flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
 
@@ -3931,7 +3901,7 @@ static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
        s = bc_parse_expr(p, flags, bc_parse_next_rel);
        if (s) return s;
 
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
 
        *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
        bc_parse_push(p, *prev);
@@ -3960,7 +3930,7 @@ static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
 
        s = bc_parse_expr(p, flags, bc_parse_next_rel);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
        bc_parse_push(p, BC_INST_SCALE_FUNC);
 
        return bc_lex_next(&p->l);
@@ -4017,7 +3987,7 @@ static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
                                s = bc_lex_next(&p->l);
                                if (s) return s;
                                if (p->l.t.t == BC_LEX_LPAREN)
-                                       s = BC_STATUS_PARSE_BAD_TOKEN;
+                                       s = bc_error_bad_token();
                                else
                                        bc_parse_push(p, BC_INST_SCALE);
                                break;
@@ -4025,7 +3995,7 @@ static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
 
                        default:
                        {
-                               s = BC_STATUS_PARSE_BAD_TOKEN;
+                               s = bc_error_bad_token();
                                break;
                        }
                }
@@ -4067,8 +4037,8 @@ static BcStatus bc_parse_string(BcParse *p, char inst)
        char *str = xstrdup(p->l.t.v.v);
 
        bc_parse_push(p, BC_INST_STR);
-       bc_parse_pushIndex(p, p->prog->strs.len);
-       bc_vec_push(&p->prog->strs, &str);
+       bc_parse_pushIndex(p, G.prog.strs.len);
+       bc_vec_push(&G.prog.strs, &str);
        bc_parse_push(p, inst);
 
        return bc_lex_next(&p->l);
@@ -4086,7 +4056,7 @@ static BcStatus bc_parse_print(BcParse *p)
        type = p->l.t.t;
 
        if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
-               return BC_STATUS_PARSE_BAD_PRINT;
+               return bc_error("bad print statement");
 
        while (!s && type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
 
@@ -4106,7 +4076,7 @@ static BcStatus bc_parse_print(BcParse *p)
        }
 
        if (s) return s;
-       if (comma) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (comma) return bc_error_bad_token();
 
        return bc_lex_next(&p->l);
 }
@@ -4117,7 +4087,7 @@ static BcStatus bc_parse_return(BcParse *p)
        BcLexType t;
        bool paren;
 
-       if (!BC_PARSE_FUNC(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
 
        s = bc_lex_next(&p->l);
        if (s) return s;
@@ -4129,17 +4099,15 @@ static BcStatus bc_parse_return(BcParse *p)
                bc_parse_push(p, BC_INST_RET0);
        else {
 
-               s = bc_parse_expr(p, 0, bc_parse_next_expr);
-               if (s && s != BC_STATUS_PARSE_EMPTY_EXP)
-                       return s;
-               else if (s == BC_STATUS_PARSE_EMPTY_EXP) {
+               s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
+               if (s == BC_STATUS_PARSE_EMPTY_EXP) {
                        bc_parse_push(p, BC_INST_RET0);
                        s = bc_lex_next(&p->l);
-                       if (s) return s;
                }
+               if (s) return s;
 
                if (!paren || p->l.t.last != BC_LEX_RPAREN) {
-                       s = bc_vm_posixError(BC_STATUS_POSIX_RET, p->l.f, p->l.line, NULL);
+                       s = bc_POSIX_requires("parentheses around return expressions");
                        if (s) return s;
                }
 
@@ -4154,18 +4122,18 @@ static BcStatus bc_parse_endBody(BcParse *p, bool brace)
        BcStatus s = BC_STATUS_SUCCESS;
 
        if (p->flags.len <= 1 || (brace && p->nbraces == 0))
-               return BC_STATUS_PARSE_BAD_TOKEN;
+               return bc_error_bad_token();
 
        if (brace) {
 
                if (p->l.t.t == BC_LEX_RBRACE) {
-                       if (!p->nbraces) return BC_STATUS_PARSE_BAD_TOKEN;
+                       if (!p->nbraces) return bc_error_bad_token();
                        --p->nbraces;
                        s = bc_lex_next(&p->l);
                        if (s) return s;
                }
                else
-                       return BC_STATUS_PARSE_BAD_TOKEN;
+                       return bc_error_bad_token();
        }
 
        if (BC_PARSE_IF(p)) {
@@ -4251,13 +4219,13 @@ static BcStatus bc_parse_if(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
 
        s = bc_lex_next(&p->l);
        if (s) return s;
        s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
 
        s = bc_lex_next(&p->l);
        if (s) return s;
@@ -4278,7 +4246,7 @@ static BcStatus bc_parse_else(BcParse *p)
 {
        BcInstPtr ip;
 
-       if (!BC_PARSE_IF_END(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
 
        ip.idx = p->func->labels.len;
        ip.func = ip.len = 0;
@@ -4302,7 +4270,7 @@ static BcStatus bc_parse_while(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
        s = bc_lex_next(&p->l);
        if (s) return s;
 
@@ -4320,7 +4288,7 @@ static BcStatus bc_parse_while(BcParse *p)
 
        s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
        s = bc_lex_next(&p->l);
        if (s) return s;
 
@@ -4339,17 +4307,17 @@ static BcStatus bc_parse_for(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
        s = bc_lex_next(&p->l);
        if (s) return s;
 
        if (p->l.t.t != BC_LEX_SCOLON)
                s = bc_parse_expr(p, 0, bc_parse_next_for);
        else
-               s = bc_vm_posixError(BC_STATUS_POSIX_FOR1, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
 
        if (s) return s;
-       if (p->l.t.t != BC_LEX_SCOLON) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
        s = bc_lex_next(&p->l);
        if (s) return s;
 
@@ -4363,10 +4331,10 @@ static BcStatus bc_parse_for(BcParse *p)
        if (p->l.t.t != BC_LEX_SCOLON)
                s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
        else
-               s = bc_vm_posixError(BC_STATUS_POSIX_FOR2, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
 
        if (s) return s;
-       if (p->l.t.t != BC_LEX_SCOLON) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
 
        s = bc_lex_next(&p->l);
        if (s) return s;
@@ -4384,11 +4352,11 @@ static BcStatus bc_parse_for(BcParse *p)
        if (p->l.t.t != BC_LEX_RPAREN)
                s = bc_parse_expr(p, 0, bc_parse_next_rel);
        else
-               s = bc_vm_posixError(BC_STATUS_POSIX_FOR3, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
 
        if (s) return s;
 
-       if (p->l.t.t != BC_LEX_RPAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
        bc_parse_push(p, BC_INST_JUMP);
        bc_parse_pushIndex(p, cond_idx);
        bc_vec_push(&p->func->labels, &p->func->code.len);
@@ -4411,17 +4379,17 @@ static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
        size_t i;
        BcInstPtr *ip;
 
-       if (!BC_PARSE_LOOP(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
 
        if (type == BC_LEX_KEY_BREAK) {
 
-               if (p->exits.len == 0) return BC_STATUS_PARSE_BAD_TOKEN;
+               if (p->exits.len == 0) return bc_error_bad_token();
 
                i = p->exits.len - 1;
                ip = bc_vec_item(&p->exits, i);
 
                while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
-               if (i >= p->exits.len && !ip->func) return BC_STATUS_PARSE_BAD_TOKEN;
+               if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
 
                i = ip->idx;
        }
@@ -4435,7 +4403,7 @@ static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
        if (s) return s;
 
        if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
-               return BC_STATUS_PARSE_BAD_TOKEN;
+               return bc_error_bad_token();
 
        return bc_lex_next(&p->l);
 }
@@ -4449,20 +4417,23 @@ static BcStatus bc_parse_func(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_NAME) return BC_STATUS_PARSE_BAD_FUNC;
+       if (p->l.t.t != BC_LEX_NAME)
+               return bc_error("bad function definition");
 
        name = xstrdup(p->l.t.v.v);
        bc_parse_addFunc(p, name, &p->fidx);
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_LPAREN) return BC_STATUS_PARSE_BAD_FUNC;
+       if (p->l.t.t != BC_LEX_LPAREN)
+               return bc_error("bad function definition");
        s = bc_lex_next(&p->l);
        if (s) return s;
 
        while (p->l.t.t != BC_LEX_RPAREN) {
 
-               if (p->l.t.t != BC_LEX_NAME) return BC_STATUS_PARSE_BAD_FUNC;
+               if (p->l.t.t != BC_LEX_NAME)
+                       return bc_error("bad function definition");
 
                ++p->func->nparams;
 
@@ -4478,7 +4449,7 @@ static BcStatus bc_parse_func(BcParse *p)
                        if (s) goto err;
 
                        if (p->l.t.t != BC_LEX_RBRACKET) {
-                               s = BC_STATUS_PARSE_BAD_FUNC;
+                               s = bc_error("bad function definition");
                                goto err;
                        }
 
@@ -4496,7 +4467,7 @@ static BcStatus bc_parse_func(BcParse *p)
                if (s) goto err;
        }
 
-       if (comma) return BC_STATUS_PARSE_BAD_FUNC;
+       if (comma) return bc_error("bad function definition");
 
        flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
        bc_parse_startBody(p, flags);
@@ -4505,7 +4476,7 @@ static BcStatus bc_parse_func(BcParse *p)
        if (s) return s;
 
        if (p->l.t.t != BC_LEX_LBRACE)
-               s = bc_vm_posixError(BC_STATUS_POSIX_BRACE, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_requires("the left brace be on the same line as the function header");
 
        return s;
 
@@ -4520,7 +4491,7 @@ static BcStatus bc_parse_auto(BcParse *p)
        bool comma, var, one;
        char *name;
 
-       if (!p->auto_part) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (!p->auto_part) return bc_error_bad_token();
        s = bc_lex_next(&p->l);
        if (s) return s;
 
@@ -4540,7 +4511,7 @@ static BcStatus bc_parse_auto(BcParse *p)
                        if (s) goto err;
 
                        if (p->l.t.t != BC_LEX_RBRACKET) {
-                               s = BC_STATUS_PARSE_BAD_FUNC;
+                               s = bc_error("bad function definition");
                                goto err;
                        }
 
@@ -4558,11 +4529,11 @@ static BcStatus bc_parse_auto(BcParse *p)
                if (s) goto err;
        }
 
-       if (comma) return BC_STATUS_PARSE_BAD_FUNC;
-       if (!one) return BC_STATUS_PARSE_NO_AUTO;
+       if (comma) return bc_error("bad function definition");
+       if (!one) return bc_error("no auto variable found");
 
        if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
-               return BC_STATUS_PARSE_BAD_TOKEN;
+               return bc_error_bad_token();
 
        return bc_lex_next(&p->l);
 
@@ -4580,7 +4551,7 @@ static BcStatus bc_parse_body(BcParse *p, bool brace)
 
        if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
 
-               if (!brace) return BC_STATUS_PARSE_BAD_TOKEN;
+               if (!brace) return bc_error_bad_token();
                p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
 
                if (!p->auto_part) {
@@ -4617,7 +4588,7 @@ static BcStatus bc_parse_stmt(BcParse *p)
 
                case BC_LEX_LBRACE:
                {
-                       if (!BC_PARSE_BODY(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+                       if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
 
                        ++p->nbraces;
                        s = bc_lex_next(&p->l);
@@ -4719,9 +4690,18 @@ static BcStatus bc_parse_stmt(BcParse *p)
 
                case BC_LEX_KEY_LIMITS:
                {
+                       // "limits" is a compile-time command,
+                       // the output is produced at _parse time_.
                        s = bc_lex_next(&p->l);
                        if (s) return s;
-                       s = BC_STATUS_LIMITS;
+                       printf("BC_BASE_MAX     = %u\n", BC_MAX_OBASE);
+                       printf("BC_DIM_MAX      = %u\n", BC_MAX_DIM);
+                       printf("BC_SCALE_MAX    = %u\n", BC_MAX_SCALE);
+                       printf("BC_STRING_MAX   = %u\n", BC_MAX_STRING);
+                       printf("BC_NAME_MAX     = %u\n", BC_MAX_NAME);
+                       printf("BC_NUM_MAX      = %u\n", BC_MAX_NUM);
+                       printf("MAX Exponent    = %lu\n", BC_MAX_EXP);
+                       printf("Number of vars  = %lu\n", BC_MAX_VARS);
                        break;
                }
 
@@ -4733,10 +4713,10 @@ static BcStatus bc_parse_stmt(BcParse *p)
 
                case BC_LEX_KEY_QUIT:
                {
-                       // Quit is a compile-time command. We don't exit directly,
-                       // so the vm can clean up. Limits do the same thing.
-                       s = BC_STATUS_QUIT;
-                       break;
+                       // "quit" is a compile-time command. For example,
+                       // "if (0 == 1) quit" terminates when parsing the statement,
+                       // not when it is executed
+                       QUIT_OR_RETURN_TO_MAIN;
                }
 
                case BC_LEX_KEY_RETURN:
@@ -4753,7 +4733,7 @@ static BcStatus bc_parse_stmt(BcParse *p)
 
                default:
                {
-                       s = BC_STATUS_PARSE_BAD_TOKEN;
+                       s = bc_error_bad_token();
                        break;
                }
        }
@@ -4766,21 +4746,23 @@ static BcStatus bc_parse_parse(BcParse *p)
        BcStatus s;
 
        if (p->l.t.t == BC_LEX_EOF)
-               s = p->flags.len > 0 ? BC_STATUS_PARSE_NO_BLOCK_END : BC_STATUS_LEX_EOF;
+               s = p->flags.len > 0 ? bc_error("block end could not be found") : bc_error("end of file");
        else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
-               if (!BC_PARSE_CAN_EXEC(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+               if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
                s = bc_parse_func(p);
        }
        else
                s = bc_parse_stmt(p);
 
-       if ((s && s != BC_STATUS_QUIT && s != BC_STATUS_LIMITS) || G_interrupt)
-               s = bc_parse_reset(p, s);
+       if (s || G_interrupt) {
+               bc_parse_reset(p);
+               s = BC_STATUS_FAILURE;
+       }
 
        return s;
 }
 
-static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
+static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
 {
        BcStatus s = BC_STATUS_SUCCESS;
        BcInst prev = BC_INST_PRINT;
@@ -4794,7 +4776,7 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
        paren_expr = rprn = done = get_token = assign = false;
        bin_last = true;
 
-       for (; !G_interrupt && !s && !done && bc_parse_exprs[t]; t = p->l.t.t) {
+       for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
                switch (t) {
 
                        case BC_LEX_OP_INC:
@@ -4825,7 +4807,11 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                                    prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
                                    prev != BC_INST_OBASE && prev != BC_INST_LAST)
                                {
-                                       s = BC_STATUS_PARSE_BAD_ASSIGN;
+                                       s = bc_error("bad assignment:"
+                                               " left side must be scale,"
+                                               " ibase, obase, last, var,"
+                                               " or array element"
+                                       );
                                        break;
                                }
                        }
@@ -4845,10 +4831,10 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                        case BC_LEX_OP_BOOL_OR:
                        case BC_LEX_OP_BOOL_AND:
                        {
-                               if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) ||
-                                   (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT))
-                               {
-                                       return BC_STATUS_PARSE_BAD_EXP;
+                               if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
+                                || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
+                               {
+                                       return bc_error_bad_expression();
                                }
 
                                nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
@@ -4862,8 +4848,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
 
                        case BC_LEX_LPAREN:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                ++nparens;
                                paren_expr = rprn = bin_last = false;
                                get_token = true;
@@ -4875,7 +4861,7 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                        case BC_LEX_RPAREN:
                        {
                                if (bin_last || prev == BC_INST_BOOL_NOT)
-                                       return BC_STATUS_PARSE_BAD_EXP;
+                                       return bc_error_bad_expression();
 
                                if (nparens == 0) {
                                        s = BC_STATUS_SUCCESS;
@@ -4897,8 +4883,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
 
                        case BC_LEX_NAME:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                paren_expr = true;
                                rprn = get_token = bin_last = false;
                                s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
@@ -4909,8 +4895,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
 
                        case BC_LEX_NUMBER:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                bc_parse_number(p, &prev, &nexprs);
                                paren_expr = get_token = true;
                                rprn = bin_last = false;
@@ -4922,8 +4908,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                        case BC_LEX_KEY_LAST:
                        case BC_LEX_KEY_OBASE:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
                                bc_parse_push(p, (char) prev);
 
@@ -4937,8 +4923,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                        case BC_LEX_KEY_LENGTH:
                        case BC_LEX_KEY_SQRT:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                s = bc_parse_builtin(p, t, flags, &prev);
                                paren_expr = true;
                                rprn = get_token = bin_last = false;
@@ -4950,9 +4936,9 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                        case BC_LEX_KEY_READ:
                        {
                                if (BC_PARSE_LEAF(prev, rprn))
-                                       return BC_STATUS_PARSE_BAD_EXP;
+                                       return bc_error_bad_expression();
                                else if (flags & BC_PARSE_NOREAD)
-                                       s = BC_STATUS_EXEC_REC_READ;
+                                       s = bc_error_nested_read_call();
                                else
                                        s = bc_parse_read(p);
 
@@ -4966,8 +4952,8 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
 
                        case BC_LEX_KEY_SCALE:
                        {
-                               if (BC_PARSE_LEAF(prev, rprn)) return BC_STATUS_PARSE_BAD_EXP;
-
+                               if (BC_PARSE_LEAF(prev, rprn))
+                                       return bc_error_bad_expression();
                                s = bc_parse_scale(p, &prev, flags);
                                paren_expr = true;
                                rprn = get_token = bin_last = false;
@@ -4979,7 +4965,7 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
 
                        default:
                        {
-                               s = BC_STATUS_PARSE_BAD_TOKEN;
+                               s = bc_error_bad_token();
                                break;
                        }
                }
@@ -4988,7 +4974,7 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
        }
 
        if (s) return s;
-       if (G_interrupt) return BC_STATUS_EXEC_SIGNAL;
+       if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
 
        while (p->ops.len > ops_bgn) {
 
@@ -4996,7 +4982,7 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
 
                if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
-                       return BC_STATUS_PARSE_BAD_EXP;
+                       return bc_error_bad_expression();
 
                bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
 
@@ -5004,18 +4990,21 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
                bc_vec_pop(&p->ops);
        }
 
-       s = BC_STATUS_PARSE_BAD_EXP;
-       if (prev == BC_INST_BOOL_NOT || nexprs != 1) return s;
+       if (prev == BC_INST_BOOL_NOT || nexprs != 1)
+               return bc_error_bad_expression();
 
-       for (i = 0; s && i < next.len; ++i) s *= t != next.tokens[i];
-       if (s) return s;
+       for (i = 0; i < next.len; ++i)
+               if (t == next.tokens[i])
+                       goto ok;
+       return bc_error_bad_expression();
+ ok:
 
        if (!(flags & BC_PARSE_REL) && nrelops) {
-               s = bc_vm_posixError(BC_STATUS_POSIX_REL_POS, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
                if (s) return s;
        }
        else if ((flags & BC_PARSE_REL) && nrelops > 1) {
-               s = bc_vm_posixError(BC_STATUS_POSIX_MULTIREL, p->l.f, p->l.line, NULL);
+               s = bc_POSIX_requires("exactly one comparison operator per condition");
                if (s) return s;
        }
 
@@ -5027,18 +5016,32 @@ static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
        return s;
 }
 
-static void bc_parse_init(BcParse *p, BcProgram *prog, size_t func)
+static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
+{
+       BcStatus s;
+
+       s = bc_parse_expr_empty_ok(p, flags, next);
+       if (s == BC_STATUS_PARSE_EMPTY_EXP)
+               return bc_error("empty expression");
+       return s;
+}
+
+static void bc_parse_init(BcParse *p, size_t func)
 {
-       bc_parse_create(p, prog, func, bc_parse_parse, bc_lex_token);
+       bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
 }
 
 static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
 {
        return bc_parse_expr(p, flags, bc_parse_next_read);
 }
+
 #endif // ENABLE_BC
 
 #if ENABLE_DC
+
+#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
+
 static BcStatus dc_parse_register(BcParse *p)
 {
        BcStatus s;
@@ -5046,7 +5049,7 @@ static BcStatus dc_parse_register(BcParse *p)
 
        s = bc_lex_next(&p->l);
        if (s) return s;
-       if (p->l.t.t != BC_LEX_NAME) return BC_STATUS_PARSE_BAD_TOKEN;
+       if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
 
        name = xstrdup(p->l.t.v.v);
        bc_parse_pushName(p, name);
@@ -5057,7 +5060,7 @@ static BcStatus dc_parse_register(BcParse *p)
 static BcStatus dc_parse_string(BcParse *p)
 {
        char *str, *name, b[DC_PARSE_BUF_LEN + 1];
-       size_t idx, len = p->prog->strs.len;
+       size_t idx, len = G.prog.strs.len;
 
        sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
        name = xstrdup(b);
@@ -5065,7 +5068,7 @@ static BcStatus dc_parse_string(BcParse *p)
        str = xstrdup(p->l.t.v.v);
        bc_parse_push(p, BC_INST_STR);
        bc_parse_pushIndex(p, len);
-       bc_vec_push(&p->prog->strs, &str);
+       bc_vec_push(&G.prog.strs, &str);
        bc_parse_addFunc(p, name, &idx);
 
        return bc_lex_next(&p->l);
@@ -5153,7 +5156,8 @@ static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
                        if (t == BC_LEX_NEG) {
                                s = bc_lex_next(&p->l);
                                if (s) return s;
-                               if (p->l.t.t != BC_LEX_NUMBER) return BC_STATUS_PARSE_BAD_TOKEN;
+                               if (p->l.t.t != BC_LEX_NUMBER)
+                                       return bc_error_bad_token();
                        }
 
                        bc_parse_number(p, &prev, &p->nbraces);
@@ -5167,7 +5171,7 @@ static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
                case BC_LEX_KEY_READ:
                {
                        if (flags & BC_PARSE_NOREAD)
-                               s = BC_STATUS_EXEC_REC_READ;
+                               s = bc_error_nested_read_call();
                        else
                                bc_parse_push(p, BC_INST_READ);
                        get_token = true;
@@ -5202,7 +5206,7 @@ static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
 
                default:
                {
-                       s = BC_STATUS_PARSE_BAD_TOKEN;
+                       s = bc_error_bad_token();
                        get_token = true;
                        break;
                }
@@ -5219,7 +5223,7 @@ static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
        BcInst inst;
        BcLexType t;
 
-       if (flags & BC_PARSE_NOCALL) p->nbraces = p->prog->results.len;
+       if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
 
        for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
 
@@ -5244,37 +5248,57 @@ static BcStatus dc_parse_parse(BcParse *p)
        BcStatus s;
 
        if (p->l.t.t == BC_LEX_EOF)
-               s = BC_STATUS_LEX_EOF;
+               s = bc_error("end of file");
        else
                s = dc_parse_expr(p, 0);
 
-       if (s || G_interrupt) s = bc_parse_reset(p, s);
+       if (s || G_interrupt) {
+               bc_parse_reset(p);
+               s = BC_STATUS_FAILURE;
+       }
 
        return s;
 }
 
-static void dc_parse_init(BcParse *p, BcProgram *prog, size_t func)
+static void dc_parse_init(BcParse *p, size_t func)
 {
-       bc_parse_create(p, prog, func, dc_parse_parse, dc_lex_token);
+       bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
 }
+
 #endif // ENABLE_DC
 
-static void bc_program_search(BcProgram *p, char *id, BcVec **ret, bool var)
+static void common_parse_init(BcParse *p, size_t func)
+{
+       if (IS_BC) {
+               IF_BC(bc_parse_init(p, func);)
+       } else {
+               IF_DC(dc_parse_init(p, func);)
+       }
+}
+
+static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
+{
+       if (IS_BC) {
+               IF_BC(return bc_parse_expression(p, flags);)
+       } else {
+               IF_DC(return dc_parse_expr(p, flags);)
+       }
+}
+
+static BcVec* bc_program_search(char *id, bool var)
 {
-       BcStatus s;
        BcId e, *ptr;
        BcVec *v, *map;
        size_t i;
        BcResultData data;
-       bool new;
+       int new;
 
-       v = var ? &p->vars : &p->arrs;
-       map = var ? &p->var_map : &p->arr_map;
+       v = var ? &G.prog.vars : &G.prog.arrs;
+       map = var ? &G.prog.var_map : &G.prog.arr_map;
 
        e.name = id;
        e.idx = v->len;
-       s = bc_map_insert(map, &e, &i);
-       new = s != BC_STATUS_VEC_ITEM_EXISTS;
+       new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
 
        if (new) {
                bc_array_init(&data.v, var);
@@ -5283,10 +5307,10 @@ static void bc_program_search(BcProgram *p, char *id, BcVec **ret, bool var)
 
        ptr = bc_vec_item(map, i);
        if (new) ptr->name = xstrdup(e.name);
-       *ret = bc_vec_item(v, ptr->idx);
+       return bc_vec_item(v, ptr->idx);
 }
 
-static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
+static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex)
 {
        BcStatus s = BC_STATUS_SUCCESS;
 
@@ -5304,15 +5328,15 @@ static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
 
                case BC_RESULT_CONSTANT:
                {
-                       char **str = bc_vec_item(&p->consts, r->d.id.idx);
+                       char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
                        size_t base_t, len = strlen(*str);
                        BcNum *base;
 
                        bc_num_init(&r->d.n, len);
 
                        hex = hex && len == 1;
-                       base = hex ? &p->hexb : &p->ib;
-                       base_t = hex ? BC_NUM_MAX_IBASE : p->ib_t;
+                       base = hex ? &G.prog.hexb : &G.prog.ib;
+                       base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
                        s = bc_num_parse(&r->d.n, *str, base, base_t);
 
                        if (s) {
@@ -5332,7 +5356,7 @@ static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
                {
                        BcVec *v;
 
-                       bc_program_search(p, r->d.id.name, &v, r->t == BC_RESULT_VAR);
+                       v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
 
                        if (r->t == BC_RESULT_ARRAY_ELEM) {
                                v = bc_vec_top(v);
@@ -5347,13 +5371,13 @@ static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
 
                case BC_RESULT_LAST:
                {
-                       *num = &p->last;
+                       *num = &G.prog.last;
                        break;
                }
 
                case BC_RESULT_ONE:
                {
-                       *num = &p->one;
+                       *num = &G.prog.one;
                        break;
                }
        }
@@ -5361,84 +5385,88 @@ static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num, bool hex)
        return s;
 }
 
-static BcStatus bc_program_binOpPrep(BcProgram *p, BcResult **l, BcNum **ln,
+static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln,
                                      BcResult **r, BcNum **rn, bool assign)
 {
        BcStatus s;
        bool hex;
        BcResultType lt, rt;
 
-       if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+       if (!BC_PROG_STACK(&G.prog.results, 2))
+               return bc_error_stack_has_too_few_elements();
 
-       *r = bc_vec_item_rev(&p->results, 0);
-       *l = bc_vec_item_rev(&p->results, 1);
+       *r = bc_vec_item_rev(&G.prog.results, 0);
+       *l = bc_vec_item_rev(&G.prog.results, 1);
 
        lt = (*l)->t;
        rt = (*r)->t;
        hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
 
-       s = bc_program_num(p, *l, ln, false);
+       s = bc_program_num(*l, ln, false);
        if (s) return s;
-       s = bc_program_num(p, *r, rn, hex);
+       s = bc_program_num(*r, rn, hex);
        if (s) return s;
 
        // We run this again under these conditions in case any vector has been
        // reallocated out from under the BcNums or arrays we had.
        if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
-               s = bc_program_num(p, *l, ln, false);
+               s = bc_program_num(*l, ln, false);
                if (s) return s;
        }
 
        if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
-               return BC_STATUS_EXEC_BAD_TYPE;
-       if (!assign && !BC_PROG_NUM((*r), (*ln))) return BC_STATUS_EXEC_BAD_TYPE;
+               return bc_error_variable_is_wrong_type();
+       if (!assign && !BC_PROG_NUM((*r), (*ln)))
+               return bc_error_variable_is_wrong_type();
 
        return s;
 }
 
-static void bc_program_binOpRetire(BcProgram *p, BcResult *r)
+static void bc_program_binOpRetire(BcResult *r)
 {
        r->t = BC_RESULT_TEMP;
-       bc_vec_pop(&p->results);
-       bc_vec_pop(&p->results);
-       bc_vec_push(&p->results, r);
+       bc_vec_pop(&G.prog.results);
+       bc_vec_pop(&G.prog.results);
+       bc_vec_push(&G.prog.results, r);
 }
 
-static BcStatus bc_program_prep(BcProgram *p, BcResult **r, BcNum **n)
+static BcStatus bc_program_prep(BcResult **r, BcNum **n)
 {
        BcStatus s;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
-       *r = bc_vec_top(&p->results);
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
+       *r = bc_vec_top(&G.prog.results);
 
-       s = bc_program_num(p, *r, n, false);
+       s = bc_program_num(*r, n, false);
        if (s) return s;
 
-       if (!BC_PROG_NUM((*r), (*n))) return BC_STATUS_EXEC_BAD_TYPE;
+       if (!BC_PROG_NUM((*r), (*n)))
+               return bc_error_variable_is_wrong_type();
 
        return s;
 }
 
-static void bc_program_retire(BcProgram *p, BcResult *r, BcResultType t)
+static void bc_program_retire(BcResult *r, BcResultType t)
 {
        r->t = t;
-       bc_vec_pop(&p->results);
-       bc_vec_push(&p->results, r);
+       bc_vec_pop(&G.prog.results);
+       bc_vec_push(&G.prog.results, r);
 }
 
-static BcStatus bc_program_op(BcProgram *p, char inst)
+static BcStatus bc_program_op(char inst)
 {
        BcStatus s;
        BcResult *opd1, *opd2, res;
        BcNum *n1, *n2 = NULL;
 
-       s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+       s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
        if (s) return s;
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
 
-       s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, p->scale);
+       s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
        if (s) goto err;
-       bc_program_binOpRetire(p, &res);
+       bc_program_binOpRetire(&res);
 
        return s;
 
@@ -5449,51 +5477,54 @@ err:
 
 static BcStatus bc_program_read(void)
 {
-       BcProgram *p = &G.prog;
-
+       const char *sv_file;
        BcStatus s;
        BcParse parse;
        BcVec buf;
        BcInstPtr ip;
        size_t i;
-       BcFunc *f = bc_vec_item(&p->fns, BC_PROG_READ);
+       BcFunc *f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
 
-       for (i = 0; i < p->stack.len; ++i) {
-               BcInstPtr *ip_ptr = bc_vec_item(&p->stack, i);
-               if (ip_ptr->func == BC_PROG_READ) return BC_STATUS_EXEC_REC_READ;
+       for (i = 0; i < G.prog.stack.len; ++i) {
+               BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
+               if (ip_ptr->func == BC_PROG_READ)
+                       return bc_error_nested_read_call();
        }
 
-       bc_vec_npop(&f->code, f->code.len);
-       bc_vec_init(&buf, sizeof(char), NULL);
+       bc_vec_pop_all(&f->code);
+       bc_char_vec_init(&buf);
+
+       sv_file = G.prog.file;
+       G.prog.file = NULL;
 
        s = bc_read_line(&buf, "read> ");
        if (s) goto io_err;
 
-       p->parse_init(&parse, p, BC_PROG_READ);
-       bc_lex_file(&parse.l, bc_program_stdin_name);
+       common_parse_init(&parse, BC_PROG_READ);
+       bc_lex_file(&parse.l);
 
        s = bc_parse_text(&parse, buf.v);
        if (s) goto exec_err;
-/// replace by IS_BC selection
-       s = p->parse_expr(&parse, BC_PARSE_NOREAD);
+       s = common_parse_expr(&parse, BC_PARSE_NOREAD);
        if (s) goto exec_err;
 
        if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
-               s = BC_STATUS_EXEC_BAD_READ_EXPR;
+               s = bc_error("bad read() expression");
                goto exec_err;
        }
 
        ip.func = BC_PROG_READ;
        ip.idx = 0;
-       ip.len = p->results.len;
+       ip.len = G.prog.results.len;
 
        // Update this pointer, just in case.
-       f = bc_vec_item(&p->fns, BC_PROG_READ);
+       f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
 
        bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
-       bc_vec_push(&p->stack, &ip);
+       bc_vec_push(&G.prog.stack, &ip);
 
 exec_err:
+       G.prog.file = sv_file;
        bc_parse_free(&parse);
 io_err:
        bc_vec_free(&buf);
@@ -5613,7 +5644,7 @@ static void bc_program_printString(const char *str, size_t *nchars)
        }
 }
 
-static BcStatus bc_program_print(BcProgram *p, char inst, size_t idx)
+static BcStatus bc_program_print(char inst, size_t idx)
 {
        BcStatus s = BC_STATUS_SUCCESS;
        BcResult *r;
@@ -5622,59 +5653,60 @@ static BcStatus bc_program_print(BcProgram *p, char inst, size_t idx)
        BcNum *num = NULL;
        bool pop = inst != BC_INST_PRINT;
 
-       if (!BC_PROG_STACK(&p->results, idx + 1)) return BC_STATUS_EXEC_STACK;
+       if (!BC_PROG_STACK(&G.prog.results, idx + 1))
+               return bc_error_stack_has_too_few_elements();
 
-       r = bc_vec_item_rev(&p->results, idx);
-       s = bc_program_num(p, r, &num, false);
+       r = bc_vec_item_rev(&G.prog.results, idx);
+       s = bc_program_num(r, &num, false);
        if (s) return s;
 
        if (BC_PROG_NUM(r, num)) {
-               s = bc_num_print(num, &p->ob, p->ob_t, !pop, &p->nchars, p->len);
-               if (!s) bc_num_copy(&p->last, num);
+               s = bc_num_print(num, &G.prog.ob, G.prog.ob_t, !pop, &G.prog.nchars, G.prog.len);
+               if (!s) bc_num_copy(&G.prog.last, num);
        }
        else {
 
                idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
-               str = *((char **) bc_vec_item(&p->strs, idx));
+               str = *((char **) bc_vec_item(&G.prog.strs, idx));
 
                if (inst == BC_INST_PRINT_STR) {
                        for (i = 0, len = strlen(str); i < len; ++i) {
                                char c = str[i];
                                bb_putchar(c);
-                               if (c == '\n') p->nchars = SIZE_MAX;
-                               ++p->nchars;
+                               if (c == '\n') G.prog.nchars = SIZE_MAX;
+                               ++G.prog.nchars;
                        }
                }
                else {
-                       bc_program_printString(str, &p->nchars);
+                       bc_program_printString(str, &G.prog.nchars);
                        if (inst == BC_INST_PRINT) bb_putchar('\n');
                }
        }
 
-       if (!s && pop) bc_vec_pop(&p->results);
+       if (!s && pop) bc_vec_pop(&G.prog.results);
 
        return s;
 }
 
-static BcStatus bc_program_negate(BcProgram *p)
+static BcStatus bc_program_negate(void)
 {
        BcStatus s;
        BcResult res, *ptr;
        BcNum *num = NULL;
 
-       s = bc_program_prep(p, &ptr, &num);
+       s = bc_program_prep(&ptr, &num);
        if (s) return s;
 
        bc_num_init(&res.d.n, num->len);
        bc_num_copy(&res.d.n, num);
        if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
 
-       bc_program_retire(p, &res, BC_RESULT_TEMP);
+       bc_program_retire(&res, BC_RESULT_TEMP);
 
        return s;
 }
 
-static BcStatus bc_program_logical(BcProgram *p, char inst)
+static BcStatus bc_program_logical(char inst)
 {
        BcStatus s;
        BcResult *opd1, *opd2, res;
@@ -5682,14 +5714,14 @@ static BcStatus bc_program_logical(BcProgram *p, char inst)
        bool cond = 0;
        ssize_t cmp;
 
-       s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+       s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
        if (s) return s;
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
 
        if (inst == BC_INST_BOOL_AND)
-               cond = bc_num_cmp(n1, &p->zero) && bc_num_cmp(n2, &p->zero);
+               cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
        else if (inst == BC_INST_BOOL_OR)
-               cond = bc_num_cmp(n1, &p->zero) || bc_num_cmp(n2, &p->zero);
+               cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
        else {
 
                cmp = bc_num_cmp(n1, n2);
@@ -5736,13 +5768,13 @@ static BcStatus bc_program_logical(BcProgram *p, char inst)
 
        (cond ? bc_num_one : bc_num_zero)(&res.d.n);
 
-       bc_program_binOpRetire(p, &res);
+       bc_program_binOpRetire(&res);
 
        return s;
 }
 
 #if ENABLE_DC
-static BcStatus bc_program_assignStr(BcProgram *p, BcResult *r, BcVec *v,
+static BcStatus bc_program_assignStr(BcResult *r, BcVec *v,
                                      bool push)
 {
        BcNum n2;
@@ -5753,43 +5785,47 @@ static BcStatus bc_program_assignStr(BcProgram *p, BcResult *r, BcVec *v,
        res.t = BC_RESULT_STR;
 
        if (!push) {
-               if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+               if (!BC_PROG_STACK(&G.prog.results, 2))
+                       return bc_error_stack_has_too_few_elements();
                bc_vec_pop(v);
-               bc_vec_pop(&p->results);
+               bc_vec_pop(&G.prog.results);
        }
 
-       bc_vec_pop(&p->results);
+       bc_vec_pop(&G.prog.results);
 
-       bc_vec_push(&p->results, &res);
+       bc_vec_push(&G.prog.results, &res);
        bc_vec_push(v, &n2);
 
        return BC_STATUS_SUCCESS;
 }
 #endif // ENABLE_DC
 
-static BcStatus bc_program_copyToVar(BcProgram *p, char *name, bool var)
+static BcStatus bc_program_copyToVar(char *name, bool var)
 {
        BcStatus s;
        BcResult *ptr, r;
        BcVec *v;
        BcNum *n;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
 
-       ptr = bc_vec_top(&p->results);
-       if ((ptr->t == BC_RESULT_ARRAY) != !var) return BC_STATUS_EXEC_BAD_TYPE;
-       bc_program_search(p, name, &v, var);
+       ptr = bc_vec_top(&G.prog.results);
+       if ((ptr->t == BC_RESULT_ARRAY) != !var)
+               return bc_error_variable_is_wrong_type();
+       v = bc_program_search(name, var);
 
 #if ENABLE_DC
-       if (ptr->t == BC_RESULT_STR && !var) return BC_STATUS_EXEC_BAD_TYPE;
-       if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(p, ptr, v, true);
+       if (ptr->t == BC_RESULT_STR && !var)
+               return bc_error_variable_is_wrong_type();
+       if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true);
 #endif
 
-       s = bc_program_num(p, ptr, &n, false);
+       s = bc_program_num(ptr, &n, false);
        if (s) return s;
 
        // Do this once more to make sure that pointers were not invalidated.
-       bc_program_search(p, name, &v, var);
+       v = bc_program_search(name, var);
 
        if (var) {
                bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
@@ -5801,12 +5837,12 @@ static BcStatus bc_program_copyToVar(BcProgram *p, char *name, bool var)
        }
 
        bc_vec_push(v, &r.d);
-       bc_vec_pop(&p->results);
+       bc_vec_pop(&G.prog.results);
 
        return s;
 }
 
-static BcStatus bc_program_assign(BcProgram *p, char inst)
+static BcStatus bc_program_assign(char inst)
 {
        BcStatus s;
        BcResult *left, *right, res;
@@ -5814,7 +5850,7 @@ static BcStatus bc_program_assign(BcProgram *p, char inst)
        unsigned long val, max;
        bool assign = inst == BC_INST_ASSIGN, ib, sc;
 
-       s = bc_program_binOpPrep(p, &left, &l, &right, &r, assign);
+       s = bc_program_binOpPrep(&left, &l, &right, &r, assign);
        if (s) return s;
 
        ib = left->t == BC_RESULT_IBASE;
@@ -5826,24 +5862,29 @@ static BcStatus bc_program_assign(BcProgram *p, char inst)
 
                BcVec *v;
 
-               if (left->t != BC_RESULT_VAR) return BC_STATUS_EXEC_BAD_TYPE;
-               bc_program_search(p, left->d.id.name, &v, true);
+               if (left->t != BC_RESULT_VAR)
+                       return bc_error_variable_is_wrong_type();
+               v = bc_program_search(left->d.id.name, true);
 
-               return bc_program_assignStr(p, right, v, false);
+               return bc_program_assignStr(right, v, false);
        }
 #endif
 
        if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
-               return BC_STATUS_PARSE_BAD_ASSIGN;
+               return bc_error("bad assignment:"
+                               " left side must be scale,"
+                               " ibase, obase, last, var,"
+                               " or array element"
+               );
 
 #if ENABLE_BC
-       if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &p->zero))
-               return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+       if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
+               return bc_error("divide by zero");
 
        if (assign)
                bc_num_copy(l, r);
        else
-               s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, p->scale);
+               s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
 
        if (s) return s;
 #else
@@ -5851,25 +5892,35 @@ static BcStatus bc_program_assign(BcProgram *p, char inst)
 #endif
 
        if (ib || sc || left->t == BC_RESULT_OBASE) {
-
+               static const char *const msg[] = {
+                       "bad ibase; must be [2, 16]",           //BC_RESULT_IBASE
+                       "bad scale; must be [0, BC_SCALE_MAX]", //BC_RESULT_SCALE
+                       NULL, //can't happen                    //BC_RESULT_LAST
+                       NULL, //can't happen                    //BC_RESULT_CONSTANT
+                       NULL, //can't happen                    //BC_RESULT_ONE
+                       "bad obase; must be [2, BC_BASE_MAX]",  //BC_RESULT_OBASE
+               };
                size_t *ptr;
 
                s = bc_num_ulong(l, &val);
-               if (s) return s;
-               s = left->t - BC_RESULT_IBASE + BC_STATUS_EXEC_BAD_IBASE;
-
+               if (s)
+                       return s;
+               s = left->t - BC_RESULT_IBASE;
                if (sc) {
                        max = BC_MAX_SCALE;
-                       ptr = &p->scale;
+                       ptr = &G.prog.scale;
                }
                else {
-                       if (val < BC_NUM_MIN_BASE) return s;
+                       if (val < BC_NUM_MIN_BASE)
+                               return bc_error(msg[s]);
                        max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
-                       ptr = ib ? &p->ib_t : &p->ob_t;
+                       ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
                }
 
-               if (val > max) return s;
-               if (!sc) bc_num_copy(ib ? &p->ib : &p->ob, l);
+               if (val > max)
+                       return bc_error(msg[s]);
+               if (!sc)
+                       bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
 
                *ptr = (size_t) val;
                s = BC_STATUS_SUCCESS;
@@ -5877,63 +5928,64 @@ static BcStatus bc_program_assign(BcProgram *p, char inst)
 
        bc_num_init(&res.d.n, l->len);
        bc_num_copy(&res.d.n, l);
-       bc_program_binOpRetire(p, &res);
+       bc_program_binOpRetire(&res);
 
        return s;
 }
 
-static BcStatus bc_program_pushVar(BcProgram *p, char *code, size_t *bgn,
+#if !ENABLE_DC
+#define bc_program_pushVar(code, bgn, pop, copy) \
+       bc_program_pushVar(code, bgn)
+// for bc, 'pop' and 'copy' are always false
+#endif
+static BcStatus bc_program_pushVar(char *code, size_t *bgn,
                                    bool pop, bool copy)
 {
        BcStatus s = BC_STATUS_SUCCESS;
        BcResult r;
        char *name = bc_program_name(code, bgn);
-#if ENABLE_DC // Exclude
-       BcNum *num;
-       BcVec *v;
-#else
-       (void) pop, (void) copy;
-#endif
 
        r.t = BC_RESULT_VAR;
        r.d.id.name = name;
 
 #if ENABLE_DC
-       bc_program_search(p, name, &v, true);
-       num = bc_vec_top(v);
+       {
+               BcVec *v = bc_program_search(name, true);
+               BcNum *num = bc_vec_top(v);
 
-       if (pop || copy) {
+               if (pop || copy) {
+
+                       if (!BC_PROG_STACK(v, 2 - copy)) {
+                               free(name);
+                               return bc_error_stack_has_too_few_elements();
+                       }
 
-               if (!BC_PROG_STACK(v, 2 - copy)) {
                        free(name);
-                       return BC_STATUS_EXEC_STACK;
-               }
+                       name = NULL;
 
-               free(name);
-               name = NULL;
+                       if (!BC_PROG_STR(num)) {
 
-               if (!BC_PROG_STR(num)) {
+                               r.t = BC_RESULT_TEMP;
 
-                       r.t = BC_RESULT_TEMP;
+                               bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
+                               bc_num_copy(&r.d.n, num);
+                       }
+                       else {
+                               r.t = BC_RESULT_STR;
+                               r.d.id.idx = num->rdx;
+                       }
 
-                       bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
-                       bc_num_copy(&r.d.n, num);
+                       if (!copy) bc_vec_pop(v);
                }
-               else {
-                       r.t = BC_RESULT_STR;
-                       r.d.id.idx = num->rdx;
-               }
-
-               if (!copy) bc_vec_pop(v);
        }
 #endif // ENABLE_DC
 
-       bc_vec_push(&p->results, &r);
+       bc_vec_push(&G.prog.results, &r);
 
        return s;
 }
 
-static BcStatus bc_program_pushArray(BcProgram *p, char *code, size_t *bgn,
+static BcStatus bc_program_pushArray(char *code, size_t *bgn,
                                      char inst)
 {
        BcStatus s = BC_STATUS_SUCCESS;
@@ -5944,25 +5996,25 @@ static BcStatus bc_program_pushArray(BcProgram *p, char *code, size_t *bgn,
 
        if (inst == BC_INST_ARRAY) {
                r.t = BC_RESULT_ARRAY;
-               bc_vec_push(&p->results, &r);
+               bc_vec_push(&G.prog.results, &r);
        }
        else {
 
                BcResult *operand;
                unsigned long temp;
 
-               s = bc_program_prep(p, &operand, &num);
+               s = bc_program_prep(&operand, &num);
                if (s) goto err;
                s = bc_num_ulong(num, &temp);
                if (s) goto err;
 
                if (temp > BC_MAX_DIM) {
-                       s = BC_STATUS_EXEC_ARRAY_LEN;
+                       s = bc_error("array too long; must be [1, BC_DIM_MAX]");
                        goto err;
                }
 
                r.d.id.idx = (size_t) temp;
-               bc_program_retire(p, &r, BC_RESULT_ARRAY_ELEM);
+               bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
        }
 
 err:
@@ -5971,14 +6023,14 @@ err:
 }
 
 #if ENABLE_BC
-static BcStatus bc_program_incdec(BcProgram *p, char inst)
+static BcStatus bc_program_incdec(char inst)
 {
        BcStatus s;
        BcResult *ptr, res, copy;
        BcNum *num = NULL;
        char inst2 = inst;
 
-       s = bc_program_prep(p, &ptr, &num);
+       s = bc_program_prep(&ptr, &num);
        if (s) return s;
 
        if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
@@ -5992,52 +6044,56 @@ static BcStatus bc_program_incdec(BcProgram *p, char inst)
                   BC_INST_ASSIGN_PLUS :
                   BC_INST_ASSIGN_MINUS;
 
-       bc_vec_push(&p->results, &res);
-       bc_program_assign(p, inst);
+       bc_vec_push(&G.prog.results, &res);
+       bc_program_assign(inst);
 
        if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
-               bc_vec_pop(&p->results);
-               bc_vec_push(&p->results, &copy);
+               bc_vec_pop(&G.prog.results);
+               bc_vec_push(&G.prog.results, &copy);
        }
 
        return s;
 }
 
-static BcStatus bc_program_call(BcProgram *p, char *code, size_t *idx)
+static BcStatus bc_program_call(char *code, size_t *idx)
 {
        BcStatus s = BC_STATUS_SUCCESS;
        BcInstPtr ip;
        size_t i, nparams = bc_program_index(code, idx);
        BcFunc *func;
-       BcVec *v;
        BcId *a;
        BcResultData param;
        BcResult *arg;
 
        ip.idx = 0;
        ip.func = bc_program_index(code, idx);
-       func = bc_vec_item(&p->fns, ip.func);
+       func = bc_vec_item(&G.prog.fns, ip.func);
 
-       if (func->code.len == 0) return BC_STATUS_EXEC_UNDEFINED_FUNC;
-       if (nparams != func->nparams) return BC_STATUS_EXEC_MISMATCHED_PARAMS;
-       ip.len = p->results.len - nparams;
+       if (func->code.len == 0) {
+               return bc_error("undefined function");
+       }
+       if (nparams != func->nparams) {
+               return bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams);
+       }
+       ip.len = G.prog.results.len - nparams;
 
        for (i = 0; i < nparams; ++i) {
 
                a = bc_vec_item(&func->autos, nparams - 1 - i);
-               arg = bc_vec_top(&p->results);
+               arg = bc_vec_top(&G.prog.results);
 
                if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
-                       return BC_STATUS_EXEC_BAD_TYPE;
+                       return bc_error_variable_is_wrong_type();
 
-               s = bc_program_copyToVar(p, a->name, a->idx);
+               s = bc_program_copyToVar(a->name, a->idx);
                if (s) return s;
        }
 
        for (; i < func->autos.len; ++i) {
+               BcVec *v;
 
                a = bc_vec_item(&func->autos, i);
-               bc_program_search(p, a->name, &v, a->idx);
+               v = bc_program_search(a->name, a->idx);
 
                if (a->idx) {
                        bc_num_init(&param.n, BC_NUM_DEF_SIZE);
@@ -6049,31 +6105,31 @@ static BcStatus bc_program_call(BcProgram *p, char *code, size_t *idx)
                }
        }
 
-       bc_vec_push(&p->stack, &ip);
+       bc_vec_push(&G.prog.stack, &ip);
 
        return BC_STATUS_SUCCESS;
 }
 
-static BcStatus bc_program_return(BcProgram *p, char inst)
+static BcStatus bc_program_return(char inst)
 {
        BcStatus s;
        BcResult res;
        BcFunc *f;
        size_t i;
-       BcInstPtr *ip = bc_vec_top(&p->stack);
+       BcInstPtr *ip = bc_vec_top(&G.prog.stack);
 
-       if (!BC_PROG_STACK(&p->results, ip->len + inst == BC_INST_RET))
-               return BC_STATUS_EXEC_STACK;
+       if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
+               return bc_error_stack_has_too_few_elements();
 
-       f = bc_vec_item(&p->fns, ip->func);
+       f = bc_vec_item(&G.prog.fns, ip->func);
        res.t = BC_RESULT_TEMP;
 
        if (inst == BC_INST_RET) {
 
                BcNum *num;
-               BcResult *operand = bc_vec_top(&p->results);
+               BcResult *operand = bc_vec_top(&G.prog.results);
 
-               s = bc_program_num(p, operand, &num, false);
+               s = bc_program_num(operand, &num, false);
                if (s) return s;
                bc_num_init(&res.d.n, num->len);
                bc_num_copy(&res.d.n, num);
@@ -6089,13 +6145,13 @@ static BcStatus bc_program_return(BcProgram *p, char inst)
                BcVec *v;
                BcId *a = bc_vec_item(&f->autos, i);
 
-               bc_program_search(p, a->name, &v, a->idx);
+               v = bc_program_search(a->name, a->idx);
                bc_vec_pop(v);
        }
 
-       bc_vec_npop(&p->results, p->results.len - ip->len);
-       bc_vec_push(&p->results, &res);
-       bc_vec_pop(&p->stack);
+       bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
+       bc_vec_push(&G.prog.results, &res);
+       bc_vec_pop(&G.prog.stack);
 
        return BC_STATUS_SUCCESS;
 }
@@ -6117,7 +6173,7 @@ static unsigned long bc_program_len(BcNum *n)
        return len;
 }
 
-static BcStatus bc_program_builtin(BcProgram *p, char inst)
+static BcStatus bc_program_builtin(char inst)
 {
        BcStatus s;
        BcResult *opnd;
@@ -6125,22 +6181,24 @@ static BcStatus bc_program_builtin(BcProgram *p, char inst)
        BcResult res;
        bool len = inst == BC_INST_LENGTH;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
-       opnd = bc_vec_top(&p->results);
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
+       opnd = bc_vec_top(&G.prog.results);
 
-       s = bc_program_num(p, opnd, &num, false);
+       s = bc_program_num(opnd, &num, false);
        if (s) return s;
 
 #if ENABLE_DC
-       if (!BC_PROG_NUM(opnd, num) && !len) return BC_STATUS_EXEC_BAD_TYPE;
+       if (!BC_PROG_NUM(opnd, num) && !len)
+               return bc_error_variable_is_wrong_type();
 #endif
 
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
 
-       if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, p->scale);
+       if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
 #if ENABLE_BC
        else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
-               s = bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
+               bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
        }
 #endif
 #if ENABLE_DC
@@ -6149,45 +6207,39 @@ static BcStatus bc_program_builtin(BcProgram *p, char inst)
                char **str;
                size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
 
-               str = bc_vec_item(&p->strs, idx);
-               s = bc_num_ulong2num(&res.d.n, strlen(*str));
-               if (s) goto err;
+               str = bc_vec_item(&G.prog.strs, idx);
+               bc_num_ulong2num(&res.d.n, strlen(*str));
        }
 #endif
        else {
                BcProgramBuiltIn f = len ? bc_program_len : bc_program_scale;
-               s = bc_num_ulong2num(&res.d.n, f(num));
-               if (s) goto err;
+               bc_num_ulong2num(&res.d.n, f(num));
        }
 
-       bc_program_retire(p, &res, BC_RESULT_TEMP);
-
-       return s;
+       bc_program_retire(&res, BC_RESULT_TEMP);
 
-err:
-       bc_num_free(&res.d.n);
        return s;
 }
 
 #if ENABLE_DC
-static BcStatus bc_program_divmod(BcProgram *p)
+static BcStatus bc_program_divmod(void)
 {
        BcStatus s;
        BcResult *opd1, *opd2, res, res2;
        BcNum *n1, *n2 = NULL;
 
-       s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, false);
+       s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
        if (s) return s;
 
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
        bc_num_init(&res2.d.n, n2->len);
 
-       s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, p->scale);
+       s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
        if (s) goto err;
 
-       bc_program_binOpRetire(p, &res2);
+       bc_program_binOpRetire(&res2);
        res.t = BC_RESULT_TEMP;
-       bc_vec_push(&p->results, &res);
+       bc_vec_push(&G.prog.results, &res);
 
        return s;
 
@@ -6197,31 +6249,33 @@ err:
        return s;
 }
 
-static BcStatus bc_program_modexp(BcProgram *p)
+static BcStatus bc_program_modexp(void)
 {
        BcStatus s;
        BcResult *r1, *r2, *r3, res;
        BcNum *n1, *n2, *n3;
 
-       if (!BC_PROG_STACK(&p->results, 3)) return BC_STATUS_EXEC_STACK;
-       s = bc_program_binOpPrep(p, &r2, &n2, &r3, &n3, false);
+       if (!BC_PROG_STACK(&G.prog.results, 3))
+               return bc_error_stack_has_too_few_elements();
+       s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
        if (s) return s;
 
-       r1 = bc_vec_item_rev(&p->results, 2);
-       s = bc_program_num(p, r1, &n1, false);
+       r1 = bc_vec_item_rev(&G.prog.results, 2);
+       s = bc_program_num(r1, &n1, false);
        if (s) return s;
-       if (!BC_PROG_NUM(r1, n1)) return BC_STATUS_EXEC_BAD_TYPE;
+       if (!BC_PROG_NUM(r1, n1))
+               return bc_error_variable_is_wrong_type();
 
        // Make sure that the values have their pointers updated, if necessary.
        if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
 
                if (r1->t == r2->t) {
-                       s = bc_program_num(p, r2, &n2, false);
+                       s = bc_program_num(r2, &n2, false);
                        if (s) return s;
                }
 
                if (r1->t == r3->t) {
-                       s = bc_program_num(p, r3, &n3, false);
+                       s = bc_program_num(r3, &n3, false);
                        if (s) return s;
                }
        }
@@ -6230,8 +6284,8 @@ static BcStatus bc_program_modexp(BcProgram *p)
        s = bc_num_modexp(n1, n2, n3, &res.d.n);
        if (s) goto err;
 
-       bc_vec_pop(&p->results);
-       bc_program_binOpRetire(p, &res);
+       bc_vec_pop(&G.prog.results);
+       bc_program_binOpRetire(&res);
 
        return s;
 
@@ -6240,39 +6294,32 @@ err:
        return s;
 }
 
-static BcStatus bc_program_stackLen(BcProgram *p)
+static void bc_program_stackLen(void)
 {
-       BcStatus s;
        BcResult res;
-       size_t len = p->results.len;
+       size_t len = G.prog.results.len;
 
        res.t = BC_RESULT_TEMP;
 
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
-       s = bc_num_ulong2num(&res.d.n, len);
-       if (s) goto err;
-       bc_vec_push(&p->results, &res);
-
-       return s;
-
-err:
-       bc_num_free(&res.d.n);
-       return s;
+       bc_num_ulong2num(&res.d.n, len);
+       bc_vec_push(&G.prog.results, &res);
 }
 
-static BcStatus bc_program_asciify(BcProgram *p)
+static BcStatus bc_program_asciify(void)
 {
        BcStatus s;
        BcResult *r, res;
        BcNum *num = NULL, n;
        char *str, *str2, c;
-       size_t len = p->strs.len, idx;
+       size_t len = G.prog.strs.len, idx;
        unsigned long val;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
-       r = bc_vec_top(&p->results);
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
+       r = bc_vec_top(&G.prog.results);
 
-       s = bc_program_num(p, r, &num, false);
+       s = bc_program_num(r, &num, false);
        if (s) return s;
 
        if (BC_PROG_NUM(r, num)) {
@@ -6281,7 +6328,7 @@ static BcStatus bc_program_asciify(BcProgram *p)
                bc_num_copy(&n, num);
                bc_num_truncate(&n, n.rdx);
 
-               s = bc_num_mod(&n, &p->strmb, &n, 0);
+               s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
                if (s) goto num_err;
                s = bc_num_ulong(&n, &val);
                if (s) goto num_err;
@@ -6292,7 +6339,7 @@ static BcStatus bc_program_asciify(BcProgram *p)
        }
        else {
                idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
-               str2 = *((char **) bc_vec_item(&p->strs, idx));
+               str2 = *((char **) bc_vec_item(&G.prog.strs, idx));
                c = str2[0];
        }
 
@@ -6301,12 +6348,12 @@ static BcStatus bc_program_asciify(BcProgram *p)
        str[1] = '\0';
 
        str2 = xstrdup(str);
-       bc_program_addFunc(p, str2, &idx);
+       bc_program_addFunc(str2, &idx);
 
        if (idx != len + BC_PROG_REQ_FUNCS) {
 
-               for (idx = 0; idx < p->strs.len; ++idx) {
-                       if (!strcmp(*((char **) bc_vec_item(&p->strs, idx)), str)) {
+               for (idx = 0; idx < G.prog.strs.len; ++idx) {
+                       if (!strcmp(*((char **) bc_vec_item(&G.prog.strs, idx)), str)) {
                                len = idx;
                                break;
                        }
@@ -6315,12 +6362,12 @@ static BcStatus bc_program_asciify(BcProgram *p)
                free(str);
        }
        else
-               bc_vec_push(&p->strs, &str);
+               bc_vec_push(&G.prog.strs, &str);
 
        res.t = BC_RESULT_STR;
        res.d.id.idx = len;
-       bc_vec_pop(&p->results);
-       bc_vec_push(&p->results, &res);
+       bc_vec_pop(&G.prog.results);
+       bc_vec_push(&G.prog.results, &res);
 
        return BC_STATUS_SUCCESS;
 
@@ -6329,7 +6376,7 @@ num_err:
        return s;
 }
 
-static BcStatus bc_program_printStream(BcProgram *p)
+static BcStatus bc_program_printStream(void)
 {
        BcStatus s;
        BcResult *r;
@@ -6337,17 +6384,18 @@ static BcStatus bc_program_printStream(BcProgram *p)
        size_t idx;
        char *str;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
-       r = bc_vec_top(&p->results);
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
+       r = bc_vec_top(&G.prog.results);
 
-       s = bc_program_num(p, r, &n, false);
+       s = bc_program_num(r, &n, false);
        if (s) return s;
 
        if (BC_PROG_NUM(r, n))
-               s = bc_num_stream(n, &p->strmb, &p->nchars, p->len);
+               s = bc_num_stream(n, &G.prog.strmb, &G.prog.nchars, G.prog.len);
        else {
                idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
-               str = *((char **) bc_vec_item(&p->strs, idx));
+               str = *((char **) bc_vec_item(&G.prog.strs, idx));
                printf("%s", str);
        }
 
@@ -6356,26 +6404,25 @@ static BcStatus bc_program_printStream(BcProgram *p)
 
 static BcStatus bc_program_nquit(void)
 {
-       BcProgram *p = &G.prog;
-
        BcStatus s;
        BcResult *opnd;
        BcNum *num = NULL;
        unsigned long val;
 
-       s = bc_program_prep(p, &opnd, &num);
+       s = bc_program_prep(&opnd, &num);
        if (s) return s;
        s = bc_num_ulong(num, &val);
        if (s) return s;
 
-       bc_vec_pop(&p->results);
+       bc_vec_pop(&G.prog.results);
 
-       if (p->stack.len < val)
-               return BC_STATUS_EXEC_STACK;
-       else if (p->stack.len == val)
-               return BC_STATUS_QUIT;
+       if (G.prog.stack.len < val)
+               return bc_error_stack_has_too_few_elements();
+       if (G.prog.stack.len == val) {
+               QUIT_OR_RETURN_TO_MAIN;
+       }
 
-       bc_vec_npop(&p->stack, val);
+       bc_vec_npop(&G.prog.stack, val);
 
        return s;
 }
@@ -6383,8 +6430,6 @@ static BcStatus bc_program_nquit(void)
 static BcStatus bc_program_execStr(char *code, size_t *bgn,
                                    bool cond)
 {
-       BcProgram *p = &G.prog;
-
        BcStatus s = BC_STATUS_SUCCESS;
        BcResult *r;
        char **str;
@@ -6395,13 +6440,13 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
        BcNum *n;
        bool exec;
 
-       if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
+       if (!BC_PROG_STACK(&G.prog.results, 1))
+               return bc_error_stack_has_too_few_elements();
 
-       r = bc_vec_top(&p->results);
+       r = bc_vec_top(&G.prog.results);
 
        if (cond) {
 
-               BcVec *v;
                char *name, *then_name = bc_program_name(code, bgn), *else_name = NULL;
 
                if (code[*bgn] == BC_PARSE_STREND)
@@ -6419,7 +6464,8 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
                }
 
                if (exec) {
-                       bc_program_search(p, name, &v, true);
+                       BcVec *v;
+                       v = bc_program_search(name, true);
                        n = bc_vec_top(v);
                }
 
@@ -6428,7 +6474,7 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
 
                if (!exec) goto exit;
                if (!BC_PROG_STR(n)) {
-                       s = BC_STATUS_EXEC_BAD_TYPE;
+                       s = bc_error_variable_is_wrong_type();
                        goto exit;
                }
 
@@ -6439,7 +6485,7 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
                if (r->t == BC_RESULT_STR)
                        sidx = r->d.id.idx;
                else if (r->t == BC_RESULT_VAR) {
-                       s = bc_program_num(p, r, &n, false);
+                       s = bc_program_num(r, &n, false);
                        if (s || !BC_PROG_STR(n)) goto exit;
                        sidx = n->rdx;
                }
@@ -6449,20 +6495,18 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
 
        fidx = sidx + BC_PROG_REQ_FUNCS;
 
-       str = bc_vec_item(&p->strs, sidx);
-       f = bc_vec_item(&p->fns, fidx);
+       str = bc_vec_item(&G.prog.strs, sidx);
+       f = bc_vec_item(&G.prog.fns, fidx);
 
        if (f->code.len == 0) {
-/// replace by IS_BC selection
-               p->parse_init(&prs, p, fidx);
+               common_parse_init(&prs, fidx);
                s = bc_parse_text(&prs, *str);
                if (s) goto err;
-/// replace by IS_BC selection
-               s = p->parse_expr(&prs, BC_PARSE_NOCALL);
+               s = common_parse_expr(&prs, BC_PARSE_NOCALL);
                if (s) goto err;
 
                if (prs.l.t.t != BC_LEX_EOF) {
-                       s = BC_STATUS_PARSE_BAD_EXP;
+                       s = bc_error_bad_expression();
                        goto err;
                }
 
@@ -6470,119 +6514,81 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn,
        }
 
        ip.idx = 0;
-       ip.len = p->results.len;
+       ip.len = G.prog.results.len;
        ip.func = fidx;
 
-       bc_vec_pop(&p->results);
-       bc_vec_push(&p->stack, &ip);
+       bc_vec_pop(&G.prog.results);
+       bc_vec_push(&G.prog.stack, &ip);
 
        return BC_STATUS_SUCCESS;
 
 err:
        bc_parse_free(&prs);
-       f = bc_vec_item(&p->fns, fidx);
-       bc_vec_npop(&f->code, f->code.len);
+       f = bc_vec_item(&G.prog.fns, fidx);
+       bc_vec_pop_all(&f->code);
 exit:
-       bc_vec_pop(&p->results);
+       bc_vec_pop(&G.prog.results);
        return s;
 }
 #endif // ENABLE_DC
 
-static BcStatus bc_program_pushGlobal(BcProgram *p, char inst)
+static void bc_program_pushGlobal(char inst)
 {
-       BcStatus s;
        BcResult res;
        unsigned long val;
 
        res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
        if (inst == BC_INST_IBASE)
-               val = (unsigned long) p->ib_t;
+               val = (unsigned long) G.prog.ib_t;
        else if (inst == BC_INST_SCALE)
-               val = (unsigned long) p->scale;
+               val = (unsigned long) G.prog.scale;
        else
-               val = (unsigned long) p->ob_t;
+               val = (unsigned long) G.prog.ob_t;
 
        bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
-       s = bc_num_ulong2num(&res.d.n, val);
-       if (s) goto err;
-       bc_vec_push(&p->results, &res);
-
-       return s;
-
-err:
-       bc_num_free(&res.d.n);
-       return s;
+       bc_num_ulong2num(&res.d.n, val);
+       bc_vec_push(&G.prog.results, &res);
 }
 
-static void bc_program_addFunc(BcProgram *p, char *name, size_t *idx)
+static void bc_program_addFunc(char *name, size_t *idx)
 {
-       BcStatus s;
        BcId entry, *entry_ptr;
        BcFunc f;
+       int inserted;
 
        entry.name = name;
-       entry.idx = p->fns.len;
+       entry.idx = G.prog.fns.len;
 
-       s = bc_map_insert(&p->fn_map, &entry, idx);
-       if (s) free(name);
+       inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
+       if (!inserted) free(name);
 
-       entry_ptr = bc_vec_item(&p->fn_map, *idx);
+       entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
        *idx = entry_ptr->idx;
 
-       if (s == BC_STATUS_VEC_ITEM_EXISTS) {
+       if (!inserted) {
 
-               BcFunc *func = bc_vec_item(&p->fns, entry_ptr->idx);
+               BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx);
 
                // We need to reset these, so the function can be repopulated.
                func->nparams = 0;
-               bc_vec_npop(&func->autos, func->autos.len);
-               bc_vec_npop(&func->code, func->code.len);
-               bc_vec_npop(&func->labels, func->labels.len);
+               bc_vec_pop_all(&func->autos);
+               bc_vec_pop_all(&func->code);
+               bc_vec_pop_all(&func->labels);
        }
        else {
                bc_func_init(&f);
-               bc_vec_push(&p->fns, &f);
-       }
-}
-
-static BcStatus bc_program_reset(BcProgram *p, BcStatus s)
-{
-       BcFunc *f;
-       BcInstPtr *ip;
-
-       bc_vec_npop(&p->stack, p->stack.len - 1);
-       bc_vec_npop(&p->results, p->results.len);
-
-       f = bc_vec_item(&p->fns, 0);
-       ip = bc_vec_top(&p->stack);
-       ip->idx = f->code.len;
-
-       if (!s && G_interrupt && !G.tty) return BC_STATUS_QUIT;
-
-       if (!s || s == BC_STATUS_EXEC_SIGNAL) {
-               if (G.ttyin) {
-                       fflush(stdout);
-                       fputs(bc_program_ready_msg, stderr);
-                       fflush(stderr);
-                       s = BC_STATUS_SUCCESS;
-               }
-               else
-                       s = BC_STATUS_QUIT;
+               bc_vec_push(&G.prog.fns, &f);
        }
-
-       return s;
 }
 
 static BcStatus bc_program_exec(void)
 {
-       BcProgram *p = &G.prog;
-
        BcStatus s = BC_STATUS_SUCCESS;
        size_t idx;
        BcResult r, *ptr;
        BcNum *num;
-       BcInstPtr *ip = bc_vec_top(&p->stack);
-       BcFunc *func = bc_vec_item(&p->fns, ip->func);
+       BcInstPtr *ip = bc_vec_top(&G.prog.stack);
+       BcFunc *func = bc_vec_item(&G.prog.fns, ip->func);
        char *code = func->code.v;
        bool cond = false;
 
@@ -6595,10 +6601,10 @@ static BcStatus bc_program_exec(void)
 #if ENABLE_BC
                        case BC_INST_JUMP_ZERO:
                        {
-                               s = bc_program_prep(p, &ptr, &num);
+                               s = bc_program_prep(&ptr, &num);
                                if (s) return s;
-                               cond = !bc_num_cmp(num, &p->zero);
-                               bc_vec_pop(&p->results);
+                               cond = !bc_num_cmp(num, &G.prog.zero);
+                               bc_vec_pop(&G.prog.results);
                        }
                        // Fallthrough.
                        case BC_INST_JUMP:
@@ -6612,7 +6618,7 @@ static BcStatus bc_program_exec(void)
 
                        case BC_INST_CALL:
                        {
-                               s = bc_program_call(p, code, &ip->idx);
+                               s = bc_program_call(code, &ip->idx);
                                break;
                        }
 
@@ -6621,20 +6627,20 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_INC_POST:
                        case BC_INST_DEC_POST:
                        {
-                               s = bc_program_incdec(p, inst);
+                               s = bc_program_incdec(inst);
                                break;
                        }
 
                        case BC_INST_HALT:
                        {
-                               s = BC_STATUS_QUIT;
+                               QUIT_OR_RETURN_TO_MAIN;
                                break;
                        }
 
                        case BC_INST_RET:
                        case BC_INST_RET0:
                        {
-                               s = bc_program_return(p, inst);
+                               s = bc_program_return(inst);
                                break;
                        }
 
@@ -6648,7 +6654,7 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_REL_LT:
                        case BC_INST_REL_GT:
                        {
-                               s = bc_program_logical(p, inst);
+                               s = bc_program_logical(inst);
                                break;
                        }
 
@@ -6660,21 +6666,21 @@ static BcStatus bc_program_exec(void)
 
                        case BC_INST_VAR:
                        {
-                               s = bc_program_pushVar(p, code, &ip->idx, false, false);
+                               s = bc_program_pushVar(code, &ip->idx, false, false);
                                break;
                        }
 
                        case BC_INST_ARRAY_ELEM:
                        case BC_INST_ARRAY:
                        {
-                               s = bc_program_pushArray(p, code, &ip->idx, inst);
+                               s = bc_program_pushArray(code, &ip->idx, inst);
                                break;
                        }
 
                        case BC_INST_LAST:
                        {
                                r.t = BC_RESULT_LAST;
-                               bc_vec_push(&p->results, &r);
+                               bc_vec_push(&G.prog.results, &r);
                                break;
                        }
 
@@ -6682,7 +6688,7 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_SCALE:
                        case BC_INST_OBASE:
                        {
-                               s = bc_program_pushGlobal(p, inst);
+                               bc_program_pushGlobal(inst);
                                break;
                        }
 
@@ -6690,7 +6696,7 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_LENGTH:
                        case BC_INST_SQRT:
                        {
-                               s = bc_program_builtin(p, inst);
+                               s = bc_program_builtin(inst);
                                break;
                        }
 
@@ -6698,22 +6704,22 @@ static BcStatus bc_program_exec(void)
                        {
                                r.t = BC_RESULT_CONSTANT;
                                r.d.id.idx = bc_program_index(code, &ip->idx);
-                               bc_vec_push(&p->results, &r);
+                               bc_vec_push(&G.prog.results, &r);
                                break;
                        }
 
                        case BC_INST_POP:
                        {
-                               if (!BC_PROG_STACK(&p->results, 1))
-                                       s = BC_STATUS_EXEC_STACK;
+                               if (!BC_PROG_STACK(&G.prog.results, 1))
+                                       s = bc_error_stack_has_too_few_elements();
                                else
-                                       bc_vec_pop(&p->results);
+                                       bc_vec_pop(&G.prog.results);
                                break;
                        }
 
                        case BC_INST_POP_EXEC:
                        {
-                               bc_vec_pop(&p->stack);
+                               bc_vec_pop(&G.prog.stack);
                                break;
                        }
 
@@ -6721,7 +6727,7 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_PRINT_POP:
                        case BC_INST_PRINT_STR:
                        {
-                               s = bc_program_print(p, inst, 0);
+                               s = bc_program_print(inst, 0);
                                break;
                        }
 
@@ -6729,7 +6735,7 @@ static BcStatus bc_program_exec(void)
                        {
                                r.t = BC_RESULT_STR;
                                r.d.id.idx = bc_program_index(code, &ip->idx);
-                               bc_vec_push(&p->results, &r);
+                               bc_vec_push(&G.prog.results, &r);
                                break;
                        }
 
@@ -6740,25 +6746,25 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_PLUS:
                        case BC_INST_MINUS:
                        {
-                               s = bc_program_op(p, inst);
+                               s = bc_program_op(inst);
                                break;
                        }
 
                        case BC_INST_BOOL_NOT:
                        {
-                               s = bc_program_prep(p, &ptr, &num);
+                               s = bc_program_prep(&ptr, &num);
                                if (s) return s;
 
                                bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
-                               (!bc_num_cmp(num, &p->zero) ? bc_num_one : bc_num_zero)(&r.d.n);
-                               bc_program_retire(p, &r, BC_RESULT_TEMP);
+                               (!bc_num_cmp(num, &G.prog.zero) ? bc_num_one : bc_num_zero)(&r.d.n);
+                               bc_program_retire(&r, BC_RESULT_TEMP);
 
                                break;
                        }
 
                        case BC_INST_NEG:
                        {
-                               s = bc_program_negate(p);
+                               s = bc_program_negate();
                                break;
                        }
 
@@ -6772,19 +6778,19 @@ static BcStatus bc_program_exec(void)
 #endif
                        case BC_INST_ASSIGN:
                        {
-                               s = bc_program_assign(p, inst);
+                               s = bc_program_assign(inst);
                                break;
                        }
 #if ENABLE_DC
                        case BC_INST_MODEXP:
                        {
-                               s = bc_program_modexp(p);
+                               s = bc_program_modexp();
                                break;
                        }
 
                        case BC_INST_DIVMOD:
                        {
-                               s = bc_program_divmod(p);
+                               s = bc_program_divmod();
                                break;
                        }
 
@@ -6798,29 +6804,30 @@ static BcStatus bc_program_exec(void)
 
                        case BC_INST_PRINT_STACK:
                        {
-                               for (idx = 0; !s && idx < p->results.len; ++idx)
-                                       s = bc_program_print(p, BC_INST_PRINT, idx);
+                               for (idx = 0; !s && idx < G.prog.results.len; ++idx)
+                                       s = bc_program_print(BC_INST_PRINT, idx);
                                break;
                        }
 
                        case BC_INST_CLEAR_STACK:
                        {
-                               bc_vec_npop(&p->results, p->results.len);
+                               bc_vec_pop_all(&G.prog.results);
                                break;
                        }
 
                        case BC_INST_STACK_LEN:
                        {
-                               s = bc_program_stackLen(p);
+                               bc_program_stackLen();
                                break;
                        }
 
                        case BC_INST_DUPLICATE:
                        {
-                               if (!BC_PROG_STACK(&p->results, 1)) return BC_STATUS_EXEC_STACK;
-                               ptr = bc_vec_top(&p->results);
+                               if (!BC_PROG_STACK(&G.prog.results, 1))
+                                       return bc_error_stack_has_too_few_elements();
+                               ptr = bc_vec_top(&G.prog.results);
                                bc_result_copy(&r, ptr);
-                               bc_vec_push(&p->results, &r);
+                               bc_vec_push(&G.prog.results, &r);
                                break;
                        }
 
@@ -6828,10 +6835,11 @@ static BcStatus bc_program_exec(void)
                        {
                                BcResult *ptr2;
 
-                               if (!BC_PROG_STACK(&p->results, 2)) return BC_STATUS_EXEC_STACK;
+                               if (!BC_PROG_STACK(&G.prog.results, 2))
+                                       return bc_error_stack_has_too_few_elements();
 
-                               ptr = bc_vec_item_rev(&p->results, 0);
-                               ptr2 = bc_vec_item_rev(&p->results, 1);
+                               ptr = bc_vec_item_rev(&G.prog.results, 0);
+                               ptr2 = bc_vec_item_rev(&G.prog.results, 1);
                                memcpy(&r, ptr, sizeof(BcResult));
                                memcpy(ptr, ptr2, sizeof(BcResult));
                                memcpy(ptr2, &r, sizeof(BcResult));
@@ -6841,13 +6849,13 @@ static BcStatus bc_program_exec(void)
 
                        case BC_INST_ASCIIFY:
                        {
-                               s = bc_program_asciify(p);
+                               s = bc_program_asciify();
                                break;
                        }
 
                        case BC_INST_PRINT_STREAM:
                        {
-                               s = bc_program_printStream(p);
+                               s = bc_program_printStream();
                                break;
                        }
 
@@ -6855,24 +6863,23 @@ static BcStatus bc_program_exec(void)
                        case BC_INST_PUSH_VAR:
                        {
                                bool copy = inst == BC_INST_LOAD;
-                               s = bc_program_pushVar(p, code, &ip->idx, true, copy);
+                               s = bc_program_pushVar(code, &ip->idx, true, copy);
                                break;
                        }
 
                        case BC_INST_PUSH_TO_VAR:
                        {
                                char *name = bc_program_name(code, &ip->idx);
-                               s = bc_program_copyToVar(p, name, true);
+                               s = bc_program_copyToVar(name, true);
                                free(name);
                                break;
                        }
 
                        case BC_INST_QUIT:
                        {
-                               if (p->stack.len <= 2)
-                                       s = BC_STATUS_QUIT;
-                               else
-                                       bc_vec_npop(&p->stack, 2);
+                               if (G.prog.stack.len <= 2)
+                                       QUIT_OR_RETURN_TO_MAIN;
+                               bc_vec_npop(&G.prog.stack, 2);
                                break;
                        }
 
@@ -6884,17 +6891,21 @@ static BcStatus bc_program_exec(void)
 #endif // ENABLE_DC
                }
 
-               if ((s && s != BC_STATUS_QUIT) || G_interrupt) s = bc_program_reset(p, s);
+               if (s || G_interrupt) {
+                       bc_program_reset();
+                       break;
+               }
 
                // If the stack has changed, pointers may be invalid.
-               ip = bc_vec_top(&p->stack);
-               func = bc_vec_item(&p->fns, ip->func);
+               ip = bc_vec_top(&G.prog.stack);
+               func = bc_vec_item(&G.prog.fns, ip->func);
                code = func->code.v;
        }
 
        return s;
 }
 
+#if ENABLE_BC
 static void bc_vm_info(void)
 {
        printf("%s "BB_VER"\n"
@@ -6904,38 +6915,42 @@ static void bc_vm_info(void)
        , applet_name);
 }
 
-static BcStatus bc_vm_error(BcStatus s, const char *file, size_t line)
-{
-       if (!s || s > BC_STATUS_VEC_ITEM_EXISTS) return s;
-
-       fprintf(stderr, bc_err_fmt, bc_errs[bc_err_ids[s]], bc_err_msgs[s]);
-       fprintf(stderr, "    %s", file);
-       fprintf(stderr, bc_err_line + 4 * !line, line);
-
-       return s * (!G.ttyin || !!strcmp(file, bc_program_stdin_name));
-}
-
-#if ENABLE_BC
-static BcStatus bc_vm_posixError(BcStatus s, const char *file, size_t line,
-                                 const char *msg)
+static void bc_args(char **argv)
 {
-       int p = (int) G_posix, w = (int) G_warn;
-       const char *const fmt = p ? bc_err_fmt : bc_warn_fmt;
+       unsigned opts;
+       int i;
 
-       if (!(p || w) || s < BC_STATUS_POSIX_NAME_LEN) return BC_STATUS_SUCCESS;
+       GETOPT_RESET();
+#if ENABLE_FEATURE_BC_LONG_OPTIONS
+       opts = option_mask32 |= getopt32long(argv, "wvsqli",
+               "warn\0"              No_argument "w"
+               "version\0"           No_argument "v"
+               "standard\0"          No_argument "s"
+               "quiet\0"             No_argument "q"
+               "mathlib\0"           No_argument "l"
+               "interactive\0"       No_argument "i"
+       );
+#else
+       opts = option_mask32 |= getopt32(argv, "wvsqli");
+#endif
+       if (getenv("POSIXLY_CORRECT"))
+               option_mask32 |= BC_FLAG_S;
 
-       fprintf(stderr, fmt, bc_errs[bc_err_ids[s]], bc_err_msgs[s]);
-       if (msg) fprintf(stderr, "    %s\n", msg);
-       fprintf(stderr, "    %s", file);
-       fprintf(stderr, bc_err_line + 4 * !line, line);
+///should be in bc_vm_run() instead??
+       if (opts & BC_FLAG_V) {
+               bc_vm_info();
+               exit(0);
+       }
 
-       return s * (!G.ttyin && !!p);
+       for (i = optind; argv[i]; ++i)
+               bc_vec_push(&G.files, argv + i);
 }
 
 static void bc_vm_envArgs(void)
 {
        BcVec v;
-       char *env_args = getenv(bc_args_env_name), *buf;
+       char *buf;
+       char *env_args = getenv("BC_ENV_ARGS");
 
        if (!env_args) return;
 
@@ -6943,40 +6958,39 @@ static void bc_vm_envArgs(void)
        buf = G.env_args;
 
        bc_vec_init(&v, sizeof(char *), NULL);
-       bc_vec_push(&v, &bc_args_env_name);
 
-       while (*buf != 0) {
-               if (!isspace(*buf)) {
-                       bc_vec_push(&v, &buf);
-                       while (*buf != 0 && !isspace(*buf)) ++buf;
-                       if (*buf != 0) (*(buf++)) = '\0';
-               }
-               else
-                       ++buf;
+       while (*(buf = skip_whitespace(buf)) != '\0') {
+               bc_vec_push(&v, &buf);
+               buf = skip_non_whitespace(buf);
+               if (!*buf)
+                       break;
+               *buf++ = '\0';
        }
 
-       bc_args((int) v.len, (char **) v.v);
+       // NULL terminate, and pass argv[] so that first arg is argv[1]
+       if (sizeof(int) == sizeof(char*)) {
+               bc_vec_push(&v, &const_int_0);
+       } else {
+               static char *const nullptr = NULL;
+               bc_vec_push(&v, &nullptr);
+       }
+       bc_args(((char **)v.v) - 1);
 
        bc_vec_free(&v);
 }
 #endif // ENABLE_BC
 
-static size_t bc_vm_envLen(const char *var)
+static unsigned bc_vm_envLen(const char *var)
 {
-       char *lenv = getenv(var);
-       size_t i, len = BC_NUM_PRINT_WIDTH;
-       int num;
+       char *lenv;
+       unsigned len;
 
+       lenv = getenv(var);
+       len = BC_NUM_PRINT_WIDTH;
        if (!lenv) return len;
 
-       len = strlen(lenv);
-
-       for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]);
-       if (num) {
-               len = (size_t) atoi(lenv) - 1;
-               if (len < 2 || len >= INT32_MAX) len = BC_NUM_PRINT_WIDTH;
-       }
-       else
+       len = bb_strtou(lenv, NULL, 10) - 1;
+       if (errno || len < 2 || len >= INT_MAX)
                len = BC_NUM_PRINT_WIDTH;
 
        return len;
@@ -6986,40 +7000,18 @@ static BcStatus bc_vm_process(const char *text)
 {
        BcStatus s = bc_parse_text(&G.prs, text);
 
-       s = bc_vm_error(s, G.prs.l.f, G.prs.l.line);
        if (s) return s;
 
        while (G.prs.l.t.t != BC_LEX_EOF) {
-
                s = G.prs.parse(&G.prs);
-
-               if (s == BC_STATUS_LIMITS) {
-
-                       bb_putchar('\n');
-                       printf("BC_BASE_MAX     = %lu\n", BC_MAX_OBASE);
-                       printf("BC_DIM_MAX      = %lu\n", BC_MAX_DIM);
-                       printf("BC_SCALE_MAX    = %lu\n", BC_MAX_SCALE);
-                       printf("BC_STRING_MAX   = %lu\n", BC_MAX_STRING);
-                       printf("BC_NAME_MAX     = %lu\n", BC_MAX_NAME);
-                       printf("BC_NUM_MAX      = %lu\n", BC_MAX_NUM);
-                       printf("Max Exponent    = %lu\n", BC_MAX_EXP);
-                       printf("Number of Vars  = %lu\n", BC_MAX_VARS);
-                       bb_putchar('\n');
-
-                       s = BC_STATUS_SUCCESS;
-               }
-               else {
-                       if (s == BC_STATUS_QUIT) return s;
-                       s = bc_vm_error(s, G.prs.l.f, G.prs.l.line);
-                       if (s) return s;
-               }
+               if (s) return s;
        }
 
        if (BC_PARSE_CAN_EXEC(&G.prs)) {
                s = bc_program_exec();
-               if (!s && G.tty) fflush(stdout);
-               if (s && s != BC_STATUS_QUIT)
-                       s = bc_vm_error(bc_program_reset(&G.prog, s), G.prs.l.f, 0);
+               fflush_and_check();
+               if (s)
+                       bc_program_reset();
        }
 
        return s;
@@ -7027,49 +7019,53 @@ static BcStatus bc_vm_process(const char *text)
 
 static BcStatus bc_vm_file(const char *file)
 {
-       BcStatus s;
+       const char *sv_file;
        char *data;
+       BcStatus s;
        BcFunc *main_func;
        BcInstPtr *ip;
 
-       G.prog.file = file;
-       s = bc_read_file(file, &data);
-       if (s) return bc_vm_error(s, file, 0);
+       data = bc_read_file(file);
+       if (!data) return bc_error_fmt("file '%s' is not text", file);
 
-       bc_lex_file(&G.prs.l, file);
+       sv_file = G.prog.file;
+       G.prog.file = file;
+       bc_lex_file(&G.prs.l);
        s = bc_vm_process(data);
        if (s) goto err;
 
        main_func = bc_vec_item(&G.prog.fns, BC_PROG_MAIN);
        ip = bc_vec_item(&G.prog.stack, 0);
 
-       if (main_func->code.len < ip->idx) s = BC_STATUS_EXEC_FILE_NOT_EXECUTABLE;
+       if (main_func->code.len < ip->idx)
+               s = bc_error_fmt("file '%s' is not executable", file);
 
 err:
+       G.prog.file = sv_file;
        free(data);
        return s;
 }
 
 static BcStatus bc_vm_stdin(void)
 {
-       BcStatus s = BC_STATUS_SUCCESS;
+       BcStatus s;
        BcVec buf, buffer;
-       char c;
        size_t len, i, str = 0;
-       bool comment = false, notend;
+       bool comment = false;
 
-       G.prog.file = bc_program_stdin_name;
-       bc_lex_file(&G.prs.l, bc_program_stdin_name);
+       G.prog.file = NULL;
+       bc_lex_file(&G.prs.l);
 
-       bc_vec_init(&buffer, sizeof(char), NULL);
-       bc_vec_init(&buf, sizeof(char), NULL);
-       bc_vec_pushByte(&buffer, '\0');
+       bc_char_vec_init(&buffer);
+       bc_char_vec_init(&buf);
+       bc_vec_pushZeroByte(&buffer);
 
        // This loop is complex because the vm tries not to send any lines that end
        // with a backslash to the parser. The reason for that is because the parser
        // treats a backslash+newline combo as whitespace, per the bc spec. In that
        // case, and for strings and comments, the parser will expect more stuff.
-       for (s = bc_read_line(&buf, ">>> "); !s; s = bc_read_line(&buf, ">>> ")) {
+       s = BC_STATUS_SUCCESS;
+       while (!G.eof && (s = bc_read_line(&buf, ">>> ")) == BC_STATUS_SUCCESS) {
 
                char *string = buf.v;
 
@@ -7085,8 +7081,8 @@ static BcStatus bc_vm_stdin(void)
 
                        for (i = 0; i < len; ++i) {
 
-                               notend = len > i + 1;
-                               c = string[i];
+                               bool notend = len > i + 1;
+                               char c = string[i];
 
                                if (i - 1 > len || string[i - 1] != '\\') {
                                        if (G.sbgn == G.send)
@@ -7113,96 +7109,291 @@ static BcStatus bc_vm_stdin(void)
 
                bc_vec_concat(&buffer, buf.v);
                s = bc_vm_process(buffer.v);
-               if (s) goto err;
+               if (s) {
+                       if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
+                               // Debug config, non-interactive mode:
+                               // return all the way back to main.
+                               // Non-debug builds do not come here, they exit.
+                               break;
+                       }
+                       fflush_and_check();
+                       fputs("ready for more input\n", stderr);
+               }
 
-               bc_vec_npop(&buffer, buffer.len);
+               bc_vec_pop_all(&buffer);
        }
 
-       if (s == BC_STATUS_BIN_FILE) s = bc_vm_error(s, G.prs.l.f, 0);
-
-       // INPUT_EOF will always happen when stdin is
-       // closed. It's not a problem in that case.
-       if (s == BC_STATUS_INPUT_EOF || s == BC_STATUS_QUIT)
-               s = BC_STATUS_SUCCESS;
-
-       if (str)
-               s = bc_vm_error(BC_STATUS_LEX_NO_STRING_END, G.prs.l.f,
-                               G.prs.l.line);
-       else if (comment)
-               s = bc_vm_error(BC_STATUS_LEX_NO_COMMENT_END, G.prs.l.f,
-                               G.prs.l.line);
+       if (str) {
+               s = bc_error("string end could not be found");
+       }
+       else if (comment) {
+               s = bc_error("comment end could not be found");
+       }
 
-err:
        bc_vec_free(&buf);
        bc_vec_free(&buffer);
        return s;
 }
 
+#if ENABLE_BC
+static const char bc_lib[] = {
+       "scale=20"
+"\n"   "define e(x){"
+"\n"           "auto b,s,n,r,d,i,p,f,v"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "if(x<0){"
+"\n"                   "n=1"
+"\n"                   "x=-x"
+"\n"           "}"
+"\n"           "s=scale"
+"\n"           "r=6+s+0.44*x"
+"\n"           "scale=scale(x)+1"
+"\n"           "while(x>1){"
+"\n"                   "d+=1"
+"\n"                   "x/=2"
+"\n"                   "scale+=1"
+"\n"           "}"
+"\n"           "scale=r"
+"\n"           "r=x+1"
+"\n"           "p=x"
+"\n"           "f=v=1"
+"\n"           "for(i=2;v!=0;++i){"
+"\n"                   "p*=x"
+"\n"                   "f*=i"
+"\n"                   "v=p/f"
+"\n"                   "r+=v"
+"\n"           "}"
+"\n"           "while((d--)!=0)r*=r"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "if(n!=0)return(1/r)"
+"\n"           "return(r/1)"
+"\n"   "}"
+"\n"   "define l(x){"
+"\n"           "auto b,s,r,p,a,q,i,v"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "if(x<=0){"
+"\n"                   "r=(1-10^scale)/1"
+"\n"                   "ibase=b"
+"\n"                   "return(r)"
+"\n"           "}"
+"\n"           "s=scale"
+"\n"           "scale+=6"
+"\n"           "p=2"
+"\n"           "while(x>=2){"
+"\n"                   "p*=2"
+"\n"                   "x=sqrt(x)"
+"\n"           "}"
+"\n"           "while(x<=0.5){"
+"\n"                   "p*=2"
+"\n"                   "x=sqrt(x)"
+"\n"           "}"
+"\n"           "r=a=(x-1)/(x+1)"
+"\n"           "q=a*a"
+"\n"                   "v=1"
+"\n"           "for(i=3;v!=0;i+=2){"
+"\n"                   "a*=q"
+"\n"                   "v=a/i"
+"\n"                   "r+=v"
+"\n"           "}"
+"\n"           "r*=p"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "return(r/1)"
+"\n"   "}"
+"\n"   "define s(x){"
+"\n"           "auto b,s,r,n,a,q,i"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "s=scale"
+"\n"           "scale=1.1*s+2"
+"\n"           "a=a(1)"
+"\n"           "if(x<0){"
+"\n"                   "n=1"
+"\n"                   "x=-x"
+"\n"           "}"
+"\n"           "scale=0"
+"\n"           "q=(x/a+2)/4"
+"\n"           "x=x-4*q*a"
+"\n"           "if(q%2!=0)x=-x"
+"\n"           "scale=s+2"
+"\n"           "r=a=x"
+"\n"           "q=-x*x"
+"\n"           "for(i=3;a!=0;i+=2){"
+"\n"                   "a*=q/(i*(i-1))"
+"\n"                   "r+=a"
+"\n"           "}"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "if(n!=0)return(-r/1)"
+"\n"           "return(r/1)"
+"\n"   "}"
+"\n"   "define c(x){"
+"\n"           "auto b,s"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "s=scale"
+"\n"           "scale*=1.2"
+"\n"           "x=s(2*a(1)+x)"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "return(x/1)"
+"\n"   "}"
+"\n"   "define a(x){"
+"\n"           "auto b,s,r,n,a,m,t,f,i,u"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "n=1"
+"\n"           "if(x<0){"
+"\n"                   "n=-1"
+"\n"                   "x=-x"
+"\n"           "}"
+"\n"           "if(x==1){"
+"\n"                   "if(scale<65){"
+"\n"                           "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
+"\n"                   "}"
+"\n"           "}"
+"\n"           "if(x==.2){"
+"\n"                   "if(scale<65){"
+"\n"                           "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
+"\n"                   "}"
+"\n"           "}"
+"\n"           "s=scale"
+"\n"           "if(x>.2){"
+"\n"                   "scale+=5"
+"\n"                   "a=a(.2)"
+"\n"           "}"
+"\n"           "scale=s+3"
+"\n"           "while(x>.2){"
+"\n"                   "m+=1"
+"\n"                   "x=(x-.2)/(1+.2*x)"
+"\n"           "}"
+"\n"           "r=u=x"
+"\n"           "f=-x*x"
+"\n"           "t=1"
+"\n"           "for(i=3;t!=0;i+=2){"
+"\n"                   "u*=f"
+"\n"                   "t=u/i"
+"\n"                   "r+=t"
+"\n"           "}"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "return((m*a+r)/n)"
+"\n"   "}"
+"\n"   "define j(n,x){"
+"\n"           "auto b,s,o,a,i,v,f"
+"\n"           "b=ibase"
+"\n"           "ibase=A"
+"\n"           "s=scale"
+"\n"           "scale=0"
+"\n"           "n/=1"
+"\n"           "if(n<0){"
+"\n"                   "n=-n"
+"\n"                   "if(n%2==1)o=1"
+"\n"           "}"
+"\n"           "a=1"
+"\n"           "for(i=2;i<=n;++i)a*=i"
+"\n"           "scale=1.5*s"
+"\n"           "a=(x^n)/2^n/a"
+"\n"           "r=v=1"
+"\n"           "f=-x*x/4"
+"\n"           "scale=scale+length(a)-scale(a)"
+"\n"           "for(i=1;v!=0;++i){"
+"\n"                   "v=v*f/i/(n+i)"
+"\n"                   "r+=v"
+"\n"           "}"
+"\n"           "scale=s"
+"\n"           "ibase=b"
+"\n"           "if(o!=0)a=-a"
+"\n"           "return(a*r/1)"
+"\n"   "}"
+};
+#endif // ENABLE_BC
+
 static BcStatus bc_vm_exec(void)
 {
-       BcStatus s = BC_STATUS_SUCCESS;
+       BcStatus s;
        size_t i;
 
 #if ENABLE_BC
-       if (G.flags & BC_FLAG_L) {
+       if (option_mask32 & BC_FLAG_L) {
 
-               bc_lex_file(&G.prs.l, bc_lib_name);
+               // We know that internal library is not buggy,
+               // thus error checking is normally disabled.
+# define DEBUG_LIB 0
+               bc_lex_file(&G.prs.l);
                s = bc_parse_text(&G.prs, bc_lib);
+               if (DEBUG_LIB && s) return s;
 
-               while (!s && G.prs.l.t.t != BC_LEX_EOF) s = G.prs.parse(&G.prs);
-
-               if (s) return s;
+               while (G.prs.l.t.t != BC_LEX_EOF) {
+                       s = G.prs.parse(&G.prs);
+                       if (DEBUG_LIB && s) return s;
+               }
                s = bc_program_exec();
-               if (s) return s;
+               if (DEBUG_LIB && s) return s;
        }
 #endif
 
+       s = BC_STATUS_SUCCESS;
        for (i = 0; !s && i < G.files.len; ++i)
                s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
-       if (s && s != BC_STATUS_QUIT) return s;
+       if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
+               // Debug config, non-interactive mode:
+               // return all the way back to main.
+               // Non-debug builds do not come here, they exit.
+               return s;
+       }
+
+       if (IS_BC || (option_mask32 & BC_FLAG_I)) {
+               if (s) {
+                       fflush_and_check();
+                       fputs("ready for more input\n", stderr);
+               }
+               s = bc_vm_stdin();
+       }
 
-       if (IS_BC || !G.files.len) s = bc_vm_stdin();
-       if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) s = bc_vm_process("");
+       if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
+               s = bc_vm_process("");
 
-       if (s == BC_STATUS_QUIT)
-               s = BC_STATUS_SUCCESS;
        return s;
 }
 
 #if ENABLE_FEATURE_CLEAN_UP
-static void bc_program_free(BcProgram *p)
+static void bc_program_free(void)
 {
-       bc_num_free(&p->ib);
-       bc_num_free(&p->ob);
-       bc_num_free(&p->hexb);
+       bc_num_free(&G.prog.ib);
+       bc_num_free(&G.prog.ob);
+       bc_num_free(&G.prog.hexb);
 # if ENABLE_DC
-       bc_num_free(&p->strmb);
+       bc_num_free(&G.prog.strmb);
 # endif
-       bc_vec_free(&p->fns);
-       bc_vec_free(&p->fn_map);
-       bc_vec_free(&p->vars);
-       bc_vec_free(&p->var_map);
-       bc_vec_free(&p->arrs);
-       bc_vec_free(&p->arr_map);
-       bc_vec_free(&p->strs);
-       bc_vec_free(&p->consts);
-       bc_vec_free(&p->results);
-       bc_vec_free(&p->stack);
-       bc_num_free(&p->last);
-       bc_num_free(&p->zero);
-       bc_num_free(&p->one);
+       bc_vec_free(&G.prog.fns);
+       bc_vec_free(&G.prog.fn_map);
+       bc_vec_free(&G.prog.vars);
+       bc_vec_free(&G.prog.var_map);
+       bc_vec_free(&G.prog.arrs);
+       bc_vec_free(&G.prog.arr_map);
+       bc_vec_free(&G.prog.strs);
+       bc_vec_free(&G.prog.consts);
+       bc_vec_free(&G.prog.results);
+       bc_vec_free(&G.prog.stack);
+       bc_num_free(&G.prog.last);
+       bc_num_free(&G.prog.zero);
+       bc_num_free(&G.prog.one);
 }
 
 static void bc_vm_free(void)
 {
        bc_vec_free(&G.files);
-       bc_program_free(&G.prog);
+       bc_program_free();
        bc_parse_free(&G.prs);
        free(G.env_args);
 }
 #endif
 
-static void bc_program_init(size_t line_len)
+static void bc_program_init(void)
 {
        size_t idx;
        BcInstPtr ip;
@@ -7211,14 +7402,6 @@ static void bc_program_init(size_t line_len)
        memset(&ip, 0, sizeof(BcInstPtr));
 
        /* G.prog.nchars = G.prog.scale = 0; - already is */
-       G.prog.len = line_len;
-       if (IS_BC) {
-               G.prog.parse_init = bc_parse_init;
-               G.prog.parse_expr = bc_parse_expression;
-       } else {
-               G.prog.parse_init = dc_parse_init;
-               G.prog.parse_expr = dc_parse_expr;
-       }
 
        bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE);
        bc_num_ten(&G.prog.ib);
@@ -7247,16 +7430,16 @@ static void bc_program_init(size_t line_len)
        bc_num_one(&G.prog.one);
 
        bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
-       bc_map_init(&G.prog.fn_map);
+       bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
 
-       bc_program_addFunc(&G.prog, xstrdup(bc_func_main), &idx);
-       bc_program_addFunc(&G.prog, xstrdup(bc_func_read), &idx);
+       bc_program_addFunc(xstrdup("(main)"), &idx);
+       bc_program_addFunc(xstrdup("(read)"), &idx);
 
        bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
-       bc_map_init(&G.prog.var_map);
+       bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
 
        bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
-       bc_map_init(&G.prog.arr_map);
+       bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
 
        bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
        bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
@@ -7265,69 +7448,131 @@ static void bc_program_init(size_t line_len)
        bc_vec_push(&G.prog.stack, &ip);
 }
 
-static void bc_vm_init(const char *env_len)
+static int bc_vm_init(const char *env_len)
 {
-       BcParseInit init;
-       size_t len = bc_vm_envLen(env_len);
-
-#if ENABLE_FEATURE_BC_SIGNALS
-        signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
+#if ENABLE_FEATURE_EDITING
+       G.line_input_state = new_line_input_t(DO_HISTORY);
 #endif
+       G.prog.len = bc_vm_envLen(env_len);
 
        bc_vec_init(&G.files, sizeof(char *), NULL);
-
+       if (IS_BC)
+               IF_BC(bc_vm_envArgs();)
+       bc_program_init();
        if (IS_BC) {
-               if (getenv("POSIXLY_CORRECT"))
-                       G.flags |= BC_FLAG_S;
-               bc_vm_envArgs();
-               init = bc_parse_init;
+               IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
        } else {
-               init = dc_parse_init;
+               IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
        }
 
-       bc_program_init(len);
-       init(&G.prs, &G.prog, BC_PROG_MAIN);
+       if (isatty(0)) {
+#if ENABLE_FEATURE_BC_SIGNALS
+               G_ttyin = 1;
+               // With SA_RESTART, most system calls will restart
+               // (IOW: they won't fail with EINTR).
+               // In particular, this means ^C won't cause
+               // stdout to get into "error state" if SIGINT hits
+               // within write() syscall.
+               // The downside is that ^C while line input is taken
+               // will only be handled after [Enter] since read()
+               // from stdin is not interrupted by ^C either,
+               // it restarts, thus fgetc() does not return on ^C.
+               signal_SA_RESTART_empty_mask(SIGINT, record_signo);
+
+               // Without SA_RESTART, this exhibits a bug:
+               // "while (1) print 1" and try ^C-ing it.
+               // Intermittently, instead of returning to input line,
+               // you'll get "output error: Interrupted system call"
+               // and exit.
+               //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
+#endif
+               return 1; // "tty"
+       }
+       return 0; // "not a tty"
 }
 
-static BcStatus bc_vm_run(int argc, char *argv[],
-                          const char *env_len)
+static BcStatus bc_vm_run(void)
 {
-       BcStatus st;
-
-       bc_vm_init(env_len);
-       bc_args(argc, argv);
-
-       G.ttyin = isatty(0);
-       G.tty = G.ttyin || (G.flags & BC_FLAG_I) || isatty(1);
-
-       if (G.ttyin && !(G.flags & BC_FLAG_Q)) bc_vm_info();
-       st = bc_vm_exec();
-
+       BcStatus st = bc_vm_exec();
 #if ENABLE_FEATURE_CLEAN_UP
+       if (G_exiting) // it was actually "halt" or "quit"
+               st = EXIT_SUCCESS;
        bc_vm_free();
+# if ENABLE_FEATURE_EDITING
+       free_line_input_t(G.line_input_state);
+# endif
+       FREE_G();
 #endif
        return st;
 }
 
 #if ENABLE_BC
 int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int bc_main(int argc, char **argv)
+int bc_main(int argc UNUSED_PARAM, char **argv)
 {
+       int is_tty;
+
        INIT_G();
        G.sbgn = G.send = '"';
 
-       return bc_vm_run(argc, argv, "BC_LINE_LENGTH");
+       is_tty = bc_vm_init("BC_LINE_LENGTH");
+
+       bc_args(argv);
+
+       if (is_tty && !(option_mask32 & BC_FLAG_Q))
+               bc_vm_info();
+
+       return bc_vm_run();
 }
 #endif
 
 #if ENABLE_DC
 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int dc_main(int argc, char **argv)
+int dc_main(int argc UNUSED_PARAM, char **argv)
 {
+       int noscript;
+
        INIT_G();
        G.sbgn = '[';
        G.send = ']';
+       // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use default width
+       // 1 char narrower than bc from the same package. Do the same?
+       bc_vm_init("DC_LINE_LENGTH");
+
+       // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
+       noscript = BC_FLAG_I;
+       for (;;) {
+               int n = getopt(argc, argv, "e:f:x");
+               if (n <= 0)
+                       break;
+               switch (n) {
+               case 'e':
+                       noscript = 0;
+                       n = bc_vm_process(optarg);
+                       if (n) return n;
+                       break;
+               case 'f':
+                       noscript = 0;
+                       bc_vm_file(optarg);
+                       break;
+               case 'x':
+                       option_mask32 |= DC_FLAG_X;
+                       break;
+               default:
+                       bb_show_usage();
+               }
+       }
+       argv += optind;
 
-       return bc_vm_run(argc, argv, "DC_LINE_LENGTH");
+       while (*argv) {
+               noscript = 0;
+               bc_vec_push(&G.files, argv++);
+       }
+
+       option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
+
+       return bc_vm_run();
 }
 #endif
+
+#endif // not DC_SMALL