diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-05 16:39:22 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-05 16:39:22 +0100 |
commit | 0409ad36a11ad51e47813b65a97e6ad68f42d381 (patch) | |
tree | b61128113f59b02e36b82847dbcdd24727842320 | |
parent | 0ad36c46c7a521760e5696ba9d6e9362a1a3777c (diff) | |
download | busybox-w32-0409ad36a11ad51e47813b65a97e6ad68f42d381.tar.gz busybox-w32-0409ad36a11ad51e47813b65a97e6ad68f42d381.tar.bz2 busybox-w32-0409ad36a11ad51e47813b65a97e6ad68f42d381.zip |
bc: restore printing of script name on errors
Examples:
On stdin, no file name is available:
$ echo 'print "' | busybox bc
bc: string end could not be found
When the same error is in file:
$ busybox bc FILE
bc 1.30.0.git
Copyright (c) 2018 Gavin D. Howard and contributors
Report bugs at: https://github.com/gavinhoward/bc
This is free software with ABSOLUTELY NO WARRANTY
FILE: string end could not be found
ready for more input
>>>
Line number printing to be added later...
function old new delta
bc_error_fmt 38 70 +32
bc_posix_error_fmt 60 90 +30
bc_vm_run 1900 1919 +19
bc_program_read 338 355 +17
bc_lex_file 15 12 -3
bc_program_stdin_name 8 - -8
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 4/1 up/down: 98/-11) Total: 87 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index fd9e31cc5..171bc8858 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -567,7 +567,6 @@ typedef struct BcLex { | |||
567 | const char *buf; | 567 | const char *buf; |
568 | size_t i; | 568 | size_t i; |
569 | size_t line; | 569 | size_t line; |
570 | const char *f; | ||
571 | size_t len; | 570 | size_t len; |
572 | bool newline; | 571 | bool newline; |
573 | 572 | ||
@@ -913,8 +912,6 @@ static const BcNumBinaryOp bc_program_ops[] = { | |||
913 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, | 912 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
914 | }; | 913 | }; |
915 | 914 | ||
916 | static const char bc_program_stdin_name[] = "<stdin>"; | ||
917 | |||
918 | static void fflush_and_check(void) | 915 | static void fflush_and_check(void) |
919 | { | 916 | { |
920 | fflush_all(); | 917 | fflush_all(); |
@@ -933,11 +930,18 @@ static void quit(void) | |||
933 | 930 | ||
934 | static NOINLINE int bc_error_fmt(const char *fmt, ...) | 931 | static NOINLINE int bc_error_fmt(const char *fmt, ...) |
935 | { | 932 | { |
933 | const char *sv; | ||
936 | va_list p; | 934 | va_list p; |
937 | 935 | ||
936 | sv = applet_name; | ||
937 | if (G.prog.file) | ||
938 | applet_name = G.prog.file; | ||
939 | |||
938 | va_start(p, fmt); | 940 | va_start(p, fmt); |
939 | bb_verror_msg(fmt, p, NULL); | 941 | bb_verror_msg(fmt, p, NULL); |
940 | va_end(p); | 942 | va_end(p); |
943 | applet_name = sv; | ||
944 | |||
941 | if (!G.ttyin) | 945 | if (!G.ttyin) |
942 | exit(1); | 946 | exit(1); |
943 | return BC_STATUS_FAILURE; | 947 | return BC_STATUS_FAILURE; |
@@ -945,15 +949,21 @@ static NOINLINE int bc_error_fmt(const char *fmt, ...) | |||
945 | 949 | ||
946 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) | 950 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
947 | { | 951 | { |
952 | const char *sv; | ||
948 | va_list p; | 953 | va_list p; |
949 | 954 | ||
950 | // Are non-POSIX constructs totally ok? | 955 | // Are non-POSIX constructs totally ok? |
951 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) | 956 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
952 | return BC_STATUS_SUCCESS; // yes | 957 | return BC_STATUS_SUCCESS; // yes |
953 | 958 | ||
959 | sv = applet_name; | ||
960 | if (G.prog.file) | ||
961 | applet_name = G.prog.file; | ||
962 | |||
954 | va_start(p, fmt); | 963 | va_start(p, fmt); |
955 | bb_verror_msg(fmt, p, NULL); | 964 | bb_verror_msg(fmt, p, NULL); |
956 | va_end(p); | 965 | va_end(p); |
966 | applet_name = sv; | ||
957 | 967 | ||
958 | // Do we treat non-POSIX constructs as errors? | 968 | // Do we treat non-POSIX constructs as errors? |
959 | if (!(option_mask32 & BC_FLAG_S)) | 969 | if (!(option_mask32 & BC_FLAG_S)) |
@@ -2860,11 +2870,10 @@ static void bc_lex_free(BcLex *l) | |||
2860 | bc_vec_free(&l->t.v); | 2870 | bc_vec_free(&l->t.v); |
2861 | } | 2871 | } |
2862 | 2872 | ||
2863 | static void bc_lex_file(BcLex *l, const char *file) | 2873 | static void bc_lex_file(BcLex *l) |
2864 | { | 2874 | { |
2865 | l->line = 1; | 2875 | l->line = 1; |
2866 | l->newline = false; | 2876 | l->newline = false; |
2867 | l->f = file; | ||
2868 | } | 2877 | } |
2869 | 2878 | ||
2870 | static BcStatus bc_lex_next(BcLex *l) | 2879 | static BcStatus bc_lex_next(BcLex *l) |
@@ -5337,6 +5346,7 @@ err: | |||
5337 | 5346 | ||
5338 | static BcStatus bc_program_read(void) | 5347 | static BcStatus bc_program_read(void) |
5339 | { | 5348 | { |
5349 | const char *sv_file; | ||
5340 | BcStatus s; | 5350 | BcStatus s; |
5341 | BcParse parse; | 5351 | BcParse parse; |
5342 | BcVec buf; | 5352 | BcVec buf; |
@@ -5353,11 +5363,14 @@ static BcStatus bc_program_read(void) | |||
5353 | bc_vec_pop_all(&f->code); | 5363 | bc_vec_pop_all(&f->code); |
5354 | bc_char_vec_init(&buf); | 5364 | bc_char_vec_init(&buf); |
5355 | 5365 | ||
5366 | sv_file = G.prog.file; | ||
5367 | G.prog.file = NULL; | ||
5368 | |||
5356 | s = bc_read_line(&buf, "read> "); | 5369 | s = bc_read_line(&buf, "read> "); |
5357 | if (s) goto io_err; | 5370 | if (s) goto io_err; |
5358 | 5371 | ||
5359 | common_parse_init(&parse, BC_PROG_READ); | 5372 | common_parse_init(&parse, BC_PROG_READ); |
5360 | bc_lex_file(&parse.l, bc_program_stdin_name); | 5373 | bc_lex_file(&parse.l); |
5361 | 5374 | ||
5362 | s = bc_parse_text(&parse, buf.v); | 5375 | s = bc_parse_text(&parse, buf.v); |
5363 | if (s) goto exec_err; | 5376 | if (s) goto exec_err; |
@@ -5380,6 +5393,7 @@ static BcStatus bc_program_read(void) | |||
5380 | bc_vec_push(&G.prog.stack, &ip); | 5393 | bc_vec_push(&G.prog.stack, &ip); |
5381 | 5394 | ||
5382 | exec_err: | 5395 | exec_err: |
5396 | G.prog.file = sv_file; | ||
5383 | bc_parse_free(&parse); | 5397 | bc_parse_free(&parse); |
5384 | io_err: | 5398 | io_err: |
5385 | bc_vec_free(&buf); | 5399 | bc_vec_free(&buf); |
@@ -6862,16 +6876,18 @@ static BcStatus bc_vm_process(const char *text) | |||
6862 | 6876 | ||
6863 | static BcStatus bc_vm_file(const char *file) | 6877 | static BcStatus bc_vm_file(const char *file) |
6864 | { | 6878 | { |
6865 | BcStatus s; | 6879 | const char *sv_file; |
6866 | char *data; | 6880 | char *data; |
6881 | BcStatus s; | ||
6867 | BcFunc *main_func; | 6882 | BcFunc *main_func; |
6868 | BcInstPtr *ip; | 6883 | BcInstPtr *ip; |
6869 | 6884 | ||
6870 | G.prog.file = file; | ||
6871 | data = bc_read_file(file); | 6885 | data = bc_read_file(file); |
6872 | if (!data) return bc_error_fmt("file '%s' is not text", file); | 6886 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
6873 | 6887 | ||
6874 | bc_lex_file(&G.prs.l, file); | 6888 | sv_file = G.prog.file; |
6889 | G.prog.file = file; | ||
6890 | bc_lex_file(&G.prs.l); | ||
6875 | s = bc_vm_process(data); | 6891 | s = bc_vm_process(data); |
6876 | if (s) goto err; | 6892 | if (s) goto err; |
6877 | 6893 | ||
@@ -6882,6 +6898,7 @@ static BcStatus bc_vm_file(const char *file) | |||
6882 | s = bc_error_fmt("file '%s' is not executable", file); | 6898 | s = bc_error_fmt("file '%s' is not executable", file); |
6883 | 6899 | ||
6884 | err: | 6900 | err: |
6901 | G.prog.file = sv_file; | ||
6885 | free(data); | 6902 | free(data); |
6886 | return s; | 6903 | return s; |
6887 | } | 6904 | } |
@@ -6893,8 +6910,8 @@ static BcStatus bc_vm_stdin(void) | |||
6893 | size_t len, i, str = 0; | 6910 | size_t len, i, str = 0; |
6894 | bool comment = false; | 6911 | bool comment = false; |
6895 | 6912 | ||
6896 | G.prog.file = bc_program_stdin_name; | 6913 | G.prog.file = NULL; |
6897 | bc_lex_file(&G.prs.l, bc_program_stdin_name); | 6914 | bc_lex_file(&G.prs.l); |
6898 | 6915 | ||
6899 | bc_char_vec_init(&buffer); | 6916 | bc_char_vec_init(&buffer); |
6900 | bc_char_vec_init(&buf); | 6917 | bc_char_vec_init(&buf); |
@@ -7156,7 +7173,7 @@ static BcStatus bc_vm_exec(void) | |||
7156 | // We know that internal library is not buggy, | 7173 | // We know that internal library is not buggy, |
7157 | // thus error checking is normally disabled. | 7174 | // thus error checking is normally disabled. |
7158 | # define DEBUG_LIB 0 | 7175 | # define DEBUG_LIB 0 |
7159 | bc_lex_file(&G.prs.l, ""); | 7176 | bc_lex_file(&G.prs.l); |
7160 | s = bc_parse_text(&G.prs, bc_lib); | 7177 | s = bc_parse_text(&G.prs, bc_lib); |
7161 | if (DEBUG_LIB && s) return s; | 7178 | if (DEBUG_LIB && s) return s; |
7162 | 7179 | ||