aboutsummaryrefslogtreecommitdiff
path: root/miscutils/bc.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-13 19:28:41 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-13 19:29:02 +0100
commit915c72b27301c933a8efbf4036293ddc1ea35b7d (patch)
treea60939039cebbd6299afc2f93fbb72f39003ddaa /miscutils/bc.c
parent82ea67fbfa98cfd6b8b422db764cb8c97fc3ea7b (diff)
downloadbusybox-915c72b27301c933a8efbf4036293ddc1ea35b7d.tar.gz
bc: do not append duplicate NUL, reduce indentation in bc_read_line()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/bc.c')
-rw-r--r--miscutils/bc.c105
1 files changed, 52 insertions, 53 deletions
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)