diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-07 16:35:43 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-07 16:35:43 +0100 |
commit | f522dd94207275ac4a2706c4927a12c37707ff5a (patch) | |
tree | 20cbddd9345b839fafeec59f9fd2e982873db2e6 | |
parent | 2d615fee3879f7eec6fd51c468ce074cc6e7a47c (diff) | |
download | busybox-w32-f522dd94207275ac4a2706c4927a12c37707ff5a.tar.gz busybox-w32-f522dd94207275ac4a2706c4927a12c37707ff5a.tar.bz2 busybox-w32-f522dd94207275ac4a2706c4927a12c37707ff5a.zip |
bc: replace G.eof with a special exit code of bc_vm_stdin()
function old new delta
bc_read_line 305 307 +2
bc_vm_run 701 689 -12
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 2/-12) Total: -10 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 2cfe87b1e..24e4b6392 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -191,6 +191,7 @@ typedef enum BcStatus { | |||
191 | BC_STATUS_SUCCESS = 0, | 191 | BC_STATUS_SUCCESS = 0, |
192 | BC_STATUS_FAILURE = 1, | 192 | BC_STATUS_FAILURE = 1, |
193 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this | 193 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this |
194 | BC_STATUS_EOF = 3, // bc_vm_stdin() uses this | ||
194 | } BcStatus; | 195 | } BcStatus; |
195 | 196 | ||
196 | #define BC_VEC_INVALID_IDX ((size_t) -1) | 197 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
@@ -754,7 +755,6 @@ typedef unsigned long (*BcProgramBuiltIn)(BcNum *); | |||
754 | struct globals { | 755 | struct globals { |
755 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) | 756 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
756 | IF_FEATURE_CLEAN_UP(smallint exiting;) | 757 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
757 | smallint eof; | ||
758 | char sbgn; | 758 | char sbgn; |
759 | char send; | 759 | char send; |
760 | 760 | ||
@@ -1270,10 +1270,12 @@ static int push_input_byte(BcVec *vec, char c) | |||
1270 | 1270 | ||
1271 | static BcStatus bc_read_line(BcVec *vec, const char *prompt) | 1271 | static BcStatus bc_read_line(BcVec *vec, const char *prompt) |
1272 | { | 1272 | { |
1273 | BcStatus s; | ||
1273 | bool bad_chars; | 1274 | bool bad_chars; |
1274 | 1275 | ||
1275 | if (G_posix) prompt = ""; | 1276 | if (G_posix) prompt = ""; |
1276 | 1277 | ||
1278 | s = BC_STATUS_SUCCESS; | ||
1277 | do { | 1279 | do { |
1278 | int c; | 1280 | int c; |
1279 | 1281 | ||
@@ -1299,7 +1301,7 @@ static BcStatus bc_read_line(BcVec *vec, const char *prompt) | |||
1299 | if (n <= 0) { // read errors or EOF, or ^D, or ^C | 1301 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
1300 | if (n == 0) // ^C | 1302 | if (n == 0) // ^C |
1301 | goto intr; | 1303 | goto intr; |
1302 | G.eof = 1; | 1304 | s = BC_STATUS_EOF; |
1303 | break; | 1305 | break; |
1304 | } | 1306 | } |
1305 | i = 0; | 1307 | i = 0; |
@@ -1329,7 +1331,7 @@ static BcStatus bc_read_line(BcVec *vec, const char *prompt) | |||
1329 | if (c == EOF) { | 1331 | if (c == EOF) { |
1330 | if (ferror(stdin)) | 1332 | if (ferror(stdin)) |
1331 | quit(); // this emits error message | 1333 | quit(); // this emits error message |
1332 | G.eof = 1; | 1334 | s = BC_STATUS_EOF; |
1333 | // Note: EOF does not append '\n', therefore: | 1335 | // Note: EOF does not append '\n', therefore: |
1334 | // printf 'print 123\n' | bc - works | 1336 | // printf 'print 123\n' | bc - works |
1335 | // printf 'print 123' | bc - fails (syntax error) | 1337 | // printf 'print 123' | bc - fails (syntax error) |
@@ -1342,7 +1344,7 @@ static BcStatus bc_read_line(BcVec *vec, const char *prompt) | |||
1342 | 1344 | ||
1343 | bc_vec_pushZeroByte(vec); | 1345 | bc_vec_pushZeroByte(vec); |
1344 | 1346 | ||
1345 | return BC_STATUS_SUCCESS; | 1347 | return s; |
1346 | } | 1348 | } |
1347 | 1349 | ||
1348 | static char* bc_read_file(const char *path) | 1350 | static char* bc_read_file(const char *path) |
@@ -7129,8 +7131,7 @@ static BcStatus bc_vm_stdin(void) | |||
7129 | // with a backslash to the parser. The reason for that is because the parser | 7131 | // with a backslash to the parser. The reason for that is because the parser |
7130 | // treats a backslash+newline combo as whitespace, per the bc spec. In that | 7132 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
7131 | // case, and for strings and comments, the parser will expect more stuff. | 7133 | // case, and for strings and comments, the parser will expect more stuff. |
7132 | s = BC_STATUS_SUCCESS; | 7134 | while ((s = bc_read_line(&buf, ">>> ")) == BC_STATUS_SUCCESS) { |
7133 | while (!G.eof && (s = bc_read_line(&buf, ">>> ")) == BC_STATUS_SUCCESS) { | ||
7134 | 7135 | ||
7135 | char *string = buf.v; | 7136 | char *string = buf.v; |
7136 | 7137 | ||
@@ -7187,6 +7188,8 @@ static BcStatus bc_vm_stdin(void) | |||
7187 | 7188 | ||
7188 | bc_vec_pop_all(&buffer); | 7189 | bc_vec_pop_all(&buffer); |
7189 | } | 7190 | } |
7191 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error | ||
7192 | s = BC_STATUS_SUCCESS; | ||
7190 | 7193 | ||
7191 | if (str) { | 7194 | if (str) { |
7192 | s = bc_error("string end could not be found"); | 7195 | s = bc_error("string end could not be found"); |