From 915c72b27301c933a8efbf4036293ddc1ea35b7d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 13 Dec 2018 19:28:41 +0100 Subject: bc: do not append duplicate NUL, reduce indentation in bc_read_line() Signed-off-by: Denys Vlasenko --- miscutils/bc.c | 105 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 53 deletions(-) (limited to 'miscutils/bc.c') diff --git a/miscutils/bc.c b/miscutils/bc.c index 7c4dfbb20..57c9ca565 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -1343,73 +1343,72 @@ static int bad_input_byte(char c) static void bc_read_line(BcVec *vec) { again: - fflush_and_check(); + fflush_and_check(); #if ENABLE_FEATURE_BC_SIGNALS - if (G_interrupt) { // ^C was pressed + if (G_interrupt) { // ^C was pressed intr: - G_interrupt = 0; - // GNU bc says "interrupted execution." - // GNU dc says "Interrupt!" - fputs("\ninterrupted execution\n", stderr); - } + G_interrupt = 0; + // GNU bc says "interrupted execution." + // GNU dc says "Interrupt!" + fputs("\ninterrupted execution\n", stderr); + } # if ENABLE_FEATURE_EDITING - if (G_ttyin) { - int n, i; + if (G_ttyin) { + int n, i; # define line_buf bb_common_bufsiz1 - n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); - if (n <= 0) { // read errors or EOF, or ^D, or ^C - if (n == 0) // ^C - goto intr; - break; - } - i = 0; - for (;;) { - char c = line_buf[i++]; - if (!c) break; - if (bad_input_byte(c)) goto again; - } - bc_vec_concat(vec, line_buf); + n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); + if (n <= 0) { // read errors or EOF, or ^D, or ^C + if (n == 0) // ^C + goto intr; + break; + } + i = 0; + for (;;) { + char c = line_buf[i++]; + if (!c) break; + if (bad_input_byte(c)) goto again; + } + bc_vec_concat(vec, line_buf); # undef line_buf - } else + } else # endif #endif - { - int c; - bool bad_chars = 0; - size_t len = vec->len; + { + int c; + bool bad_chars = 0; + size_t len = vec->len; - IF_FEATURE_BC_SIGNALS(errno = 0;) - do { - c = fgetc(stdin); + 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; - } + // 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 - // Note: EOF does not append '\n', therefore: - // printf 'print 123\n' | bc - works - // printf 'print 123' | bc - fails (syntax error) - break; - } - bad_chars |= bad_input_byte(c); - bc_vec_pushByte(vec, (char)c); - } while (c != '\n'); - if (bad_chars) { - // Bad chars on this line, ignore entire line - vec->len = len; - goto again; + if (c == EOF) { + if (ferror(stdin)) + quit(); // this emits error message + // Note: EOF does not append '\n', therefore: + // printf 'print 123\n' | bc - works + // printf 'print 123' | bc - fails (syntax error) + break; } + bad_chars |= bad_input_byte(c); + bc_vec_pushByte(vec, (char)c); + } while (c != '\n'); + if (bad_chars) { + // Bad chars on this line, ignore entire line + vec->len = len; + goto again; } - - bc_vec_pushZeroByte(vec); + bc_vec_pushZeroByte(vec); + } } static char* bc_read_file(const char *path) -- cgit v1.2.3