diff options
author | Arun Prakash Jana <engineerarun@gmail.com> | 2022-12-11 09:26:51 +0530 |
---|---|---|
committer | Arun Prakash Jana <engineerarun@gmail.com> | 2022-12-11 09:42:22 +0530 |
commit | b367e6f2593d9f06f362614ab24f294711aeae17 (patch) | |
tree | d40d2766e4a37d01152fb5dc23220aa2deb9cb30 | |
parent | 24b564abd1593eb94295cfd4f632043476107450 (diff) |
Handle common errors
-rw-r--r-- | src/bcal.c | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -43,6 +43,7 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define MAX_BITS 128 #define ALIGNMENT_MASK_4BIT 0xF +#define ELEMENTS(x) (sizeof(x) / sizeof(*(x))) typedef unsigned char bool; typedef unsigned char uchar; @@ -89,6 +90,11 @@ static char float_buf[FLOAT_BUF_LEN]; static Data lastres = {"\0", 0}; static settings cfg = {0, 0, 0, 0, 0, INFO}; +static const char* const error_strings[] = { + "is undefined", + "Missing operator" +}; + static void debug_log(const char *func, int level, const char *format, ...) { va_list ap; @@ -295,10 +301,15 @@ static int try_bc(char *expr) while (isspace(*ptr)) /* calc results have space before them */ ++ptr; - printf("%s", ptr); - if (cfg.calc && strstr(ptr, "is undefined")) - return -1; + printf("%s", ptr); /* Print the result/error */ + + /* Detect common error conditions for calc and stop */ + if (cfg.calc) + for (size_t r = 0; r < ELEMENTS(error_strings); ++r) + if (strstr(ptr, error_strings[r])) + return -1; + /* Store the result in 'r' for next usage */ len = bstrlcpy(lastres.p, ptr, NUM_LEN); /* remove newline appended at the end of result by bc */ |