bc: do not append duplicate NUL, reduce indentation in bc_read_line()

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2018-12-13 19:28:41 +01:00
parent 82ea67fbfa
commit 915c72b273

View File

@ -1343,73 +1343,72 @@ static int bad_input_byte(char c)
static void bc_read_line(BcVec *vec) static void bc_read_line(BcVec *vec)
{ {
again: again:
fflush_and_check(); fflush_and_check();
#if ENABLE_FEATURE_BC_SIGNALS #if ENABLE_FEATURE_BC_SIGNALS
if (G_interrupt) { // ^C was pressed if (G_interrupt) { // ^C was pressed
intr: intr:
G_interrupt = 0; G_interrupt = 0;
// GNU bc says "interrupted execution." // GNU bc says "interrupted execution."
// GNU dc says "Interrupt!" // GNU dc says "Interrupt!"
fputs("\ninterrupted execution\n", stderr); fputs("\ninterrupted execution\n", stderr);
} }
# if ENABLE_FEATURE_EDITING # if ENABLE_FEATURE_EDITING
if (G_ttyin) { if (G_ttyin) {
int n, i; int n, i;
# define line_buf bb_common_bufsiz1 # define line_buf bb_common_bufsiz1
n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); 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) { // read errors or EOF, or ^D, or ^C
if (n == 0) // ^C if (n == 0) // ^C
goto intr; goto intr;
break; break;
} }
i = 0; i = 0;
for (;;) { for (;;) {
char c = line_buf[i++]; char c = line_buf[i++];
if (!c) break; if (!c) break;
if (bad_input_byte(c)) goto again; if (bad_input_byte(c)) goto again;
} }
bc_vec_concat(vec, line_buf); bc_vec_concat(vec, line_buf);
# undef line_buf # undef line_buf
} else } else
# endif # endif
#endif #endif
{ {
int c; int c;
bool bad_chars = 0; bool bad_chars = 0;
size_t len = vec->len; size_t len = vec->len;
IF_FEATURE_BC_SIGNALS(errno = 0;) IF_FEATURE_BC_SIGNALS(errno = 0;)
do { do {
c = fgetc(stdin); c = fgetc(stdin);
#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
// Both conditions appear simultaneously, check both just in case // Both conditions appear simultaneously, check both just in case
if (errno == EINTR || G_interrupt) { if (errno == EINTR || G_interrupt) {
// ^C was pressed // ^C was pressed
clearerr(stdin); clearerr(stdin);
goto intr; 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;
} }
#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;
} }
bc_vec_pushZeroByte(vec);
bc_vec_pushZeroByte(vec); }
} }
static char* bc_read_file(const char *path) static char* bc_read_file(const char *path)