diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-13 19:28:41 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-13 19:29:02 +0100 |
commit | 915c72b27301c933a8efbf4036293ddc1ea35b7d (patch) | |
tree | a60939039cebbd6299afc2f93fbb72f39003ddaa | |
parent | 82ea67fbfa98cfd6b8b422db764cb8c97fc3ea7b (diff) | |
download | busybox-w32-915c72b27301c933a8efbf4036293ddc1ea35b7d.tar.gz busybox-w32-915c72b27301c933a8efbf4036293ddc1ea35b7d.tar.bz2 busybox-w32-915c72b27301c933a8efbf4036293ddc1ea35b7d.zip |
bc: do not append duplicate NUL, reduce indentation in bc_read_line()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 105 |
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) | |||
1343 | static void bc_read_line(BcVec *vec) | 1343 | static void bc_read_line(BcVec *vec) |
1344 | { | 1344 | { |
1345 | again: | 1345 | again: |
1346 | fflush_and_check(); | 1346 | fflush_and_check(); |
1347 | 1347 | ||
1348 | #if ENABLE_FEATURE_BC_SIGNALS | 1348 | #if ENABLE_FEATURE_BC_SIGNALS |
1349 | if (G_interrupt) { // ^C was pressed | 1349 | if (G_interrupt) { // ^C was pressed |
1350 | intr: | 1350 | intr: |
1351 | G_interrupt = 0; | 1351 | G_interrupt = 0; |
1352 | // GNU bc says "interrupted execution." | 1352 | // GNU bc says "interrupted execution." |
1353 | // GNU dc says "Interrupt!" | 1353 | // GNU dc says "Interrupt!" |
1354 | fputs("\ninterrupted execution\n", stderr); | 1354 | fputs("\ninterrupted execution\n", stderr); |
1355 | } | 1355 | } |
1356 | 1356 | ||
1357 | # if ENABLE_FEATURE_EDITING | 1357 | # if ENABLE_FEATURE_EDITING |
1358 | if (G_ttyin) { | 1358 | if (G_ttyin) { |
1359 | int n, i; | 1359 | int n, i; |
1360 | # define line_buf bb_common_bufsiz1 | 1360 | # define line_buf bb_common_bufsiz1 |
1361 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); | 1361 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
1362 | if (n <= 0) { // read errors or EOF, or ^D, or ^C | 1362 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
1363 | if (n == 0) // ^C | 1363 | if (n == 0) // ^C |
1364 | goto intr; | 1364 | goto intr; |
1365 | break; | 1365 | break; |
1366 | } | 1366 | } |
1367 | i = 0; | 1367 | i = 0; |
1368 | for (;;) { | 1368 | for (;;) { |
1369 | char c = line_buf[i++]; | 1369 | char c = line_buf[i++]; |
1370 | if (!c) break; | 1370 | if (!c) break; |
1371 | if (bad_input_byte(c)) goto again; | 1371 | if (bad_input_byte(c)) goto again; |
1372 | } | 1372 | } |
1373 | bc_vec_concat(vec, line_buf); | 1373 | bc_vec_concat(vec, line_buf); |
1374 | # undef line_buf | 1374 | # undef line_buf |
1375 | } else | 1375 | } else |
1376 | # endif | 1376 | # endif |
1377 | #endif | 1377 | #endif |
1378 | { | 1378 | { |
1379 | int c; | 1379 | int c; |
1380 | bool bad_chars = 0; | 1380 | bool bad_chars = 0; |
1381 | size_t len = vec->len; | 1381 | size_t len = vec->len; |
1382 | 1382 | ||
1383 | IF_FEATURE_BC_SIGNALS(errno = 0;) | 1383 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
1384 | do { | 1384 | do { |
1385 | c = fgetc(stdin); | 1385 | c = fgetc(stdin); |
1386 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING | 1386 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
1387 | // Both conditions appear simultaneously, check both just in case | 1387 | // Both conditions appear simultaneously, check both just in case |
1388 | if (errno == EINTR || G_interrupt) { | 1388 | if (errno == EINTR || G_interrupt) { |
1389 | // ^C was pressed | 1389 | // ^C was pressed |
1390 | clearerr(stdin); | 1390 | clearerr(stdin); |
1391 | goto intr; | 1391 | goto intr; |
1392 | } | 1392 | } |
1393 | #endif | 1393 | #endif |
1394 | if (c == EOF) { | 1394 | if (c == EOF) { |
1395 | if (ferror(stdin)) | 1395 | if (ferror(stdin)) |
1396 | quit(); // this emits error message | 1396 | quit(); // this emits error message |
1397 | // Note: EOF does not append '\n', therefore: | 1397 | // Note: EOF does not append '\n', therefore: |
1398 | // printf 'print 123\n' | bc - works | 1398 | // printf 'print 123\n' | bc - works |
1399 | // printf 'print 123' | bc - fails (syntax error) | 1399 | // printf 'print 123' | bc - fails (syntax error) |
1400 | break; | 1400 | break; |
1401 | } | ||
1402 | bad_chars |= bad_input_byte(c); | ||
1403 | bc_vec_pushByte(vec, (char)c); | ||
1404 | } while (c != '\n'); | ||
1405 | if (bad_chars) { | ||
1406 | // Bad chars on this line, ignore entire line | ||
1407 | vec->len = len; | ||
1408 | goto again; | ||
1409 | } | 1401 | } |
1402 | bad_chars |= bad_input_byte(c); | ||
1403 | bc_vec_pushByte(vec, (char)c); | ||
1404 | } while (c != '\n'); | ||
1405 | if (bad_chars) { | ||
1406 | // Bad chars on this line, ignore entire line | ||
1407 | vec->len = len; | ||
1408 | goto again; | ||
1410 | } | 1409 | } |
1411 | 1410 | bc_vec_pushZeroByte(vec); | |
1412 | bc_vec_pushZeroByte(vec); | 1411 | } |
1413 | } | 1412 | } |
1414 | 1413 | ||
1415 | static char* bc_read_file(const char *path) | 1414 | static char* bc_read_file(const char *path) |