From 8a89247e0a82b328958e010be5da4c815f9121be Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 12 Dec 2018 22:43:58 +0100 Subject: [PATCH] bc: remove BC_STATUS_EOF (again), the condition is detectable as len==0 function old new delta bc_read_line 147 129 -18 bc_vm_run 618 591 -27 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-45) Total: -45 bytes text data bss dec hex filename 980802 485 7296 988583 f15a7 busybox_old 980757 485 7296 988538 f157a busybox_unstripped Signed-off-by: Denys Vlasenko --- miscutils/bc.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/miscutils/bc.c b/miscutils/bc.c index 9cc29f0ea..9809fa72f 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -187,7 +187,6 @@ typedef enum BcStatus { BC_STATUS_SUCCESS = 0, BC_STATUS_FAILURE = 1, BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this - BC_STATUS_EOF = 3, // bc_vm_stdin() uses this } BcStatus; #define BC_VEC_INVALID_IDX ((size_t) -1) @@ -1364,15 +1363,10 @@ static int push_input_byte(BcVec *vec, char c) return 0; } -// This is not a "z" function: -// can return success (0) or BC_STATUS_EOF. -// Exits with error message if read error is detected. -static BcStatus bc_read_line(BcVec *vec) +static void bc_read_line(BcVec *vec) { - BcStatus s; bool bad_chars; - s = BC_STATUS_SUCCESS; do { int c; @@ -1397,7 +1391,6 @@ static BcStatus bc_read_line(BcVec *vec) if (n <= 0) { // read errors or EOF, or ^D, or ^C if (n == 0) // ^C goto intr; - s = BC_STATUS_EOF; break; } i = 0; @@ -1425,9 +1418,6 @@ static BcStatus bc_read_line(BcVec *vec) if (c == EOF) { if (ferror(stdin)) quit(); // this emits error message - // If we had some input before EOF, do not report EOF yet: - if (vec->len == 0) - s = BC_STATUS_EOF; // Note: EOF does not append '\n', therefore: // printf 'print 123\n' | bc - works // printf 'print 123' | bc - fails (syntax error) @@ -1439,8 +1429,6 @@ static BcStatus bc_read_line(BcVec *vec) } while (bad_chars); bc_vec_pushZeroByte(vec); - - return s; } static char* bc_read_file(const char *path) @@ -5416,8 +5404,7 @@ static BC_STATUS zbc_program_read(void) G.prog.file = NULL; G.in_read = 1; - s = bc_read_line(&buf); - //if (s) goto io_err; - wrong, nonzero return means EOF, not error + bc_read_line(&buf); common_parse_init(&parse, BC_PROG_READ); bc_lex_file(&parse.l); @@ -7091,13 +7078,18 @@ static BcStatus bc_vm_stdin(void) // 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. + s = BC_STATUS_SUCCESS; comment = false; str = 0; - while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { + for (;;) { size_t len; - char *string = buf.v; + char *string; + bc_read_line(&buf); len = buf.len - 1; + if (len == 0) // "" buf means EOF + break; + string = buf.v; if (len == 1) { if (str && buf.v[0] == G.send) str -= 1; @@ -7146,8 +7138,6 @@ static BcStatus bc_vm_stdin(void) bc_vec_pop_all(&buffer); } - if (s == BC_STATUS_EOF) // input EOF (^D) is not an error - s = BC_STATUS_SUCCESS; if (str) { s = bc_error("string end could not be found"); -- 2.25.1