aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-25 19:38:13 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-25 19:38:13 +0100
commit1fbe35a7d8579d53df0f4f70b31ba8b272099b7c (patch)
tree7b7243735ff3ad29fd12fa343a62b28f20602bd3
parent53e569c06b12ef2cb9ef2ceeff92f04cfbf113e3 (diff)
downloadbusybox-w32-1fbe35a7d8579d53df0f4f70b31ba8b272099b7c.tar.gz
busybox-w32-1fbe35a7d8579d53df0f4f70b31ba8b272099b7c.tar.bz2
busybox-w32-1fbe35a7d8579d53df0f4f70b31ba8b272099b7c.zip
bc: make zbc_program_read() and zdc_program_execStr() use G.prs
function old new delta zbc_program_print 656 683 +27 zbc_program_exec 3976 4003 +27 zdc_program_execStr 512 524 +12 bc_num_printNewline 45 54 +9 bc_num_printHex 61 67 +6 bc_num_printDigits 131 137 +6 dc_num_printChar 21 24 +3 bc_vm_init 675 678 +3 zbc_program_assign 424 426 +2 bc_read_line 410 407 -3 bc_verror_msg 99 93 -6 zbc_lex_next 2167 2158 -9 zbc_vm_execute_FILE 67 55 -12 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 9/4 up/down: 95/-30) Total: 65 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/bc.c87
1 files changed, 41 insertions, 46 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 79f981eaf..0fdb2963c 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -737,6 +737,9 @@ typedef struct BcParse {
737 IF_BC(BcVec conds;) 737 IF_BC(BcVec conds;)
738 IF_BC(BcVec ops;) 738 IF_BC(BcVec ops;)
739 739
740 const char *filename;
741 FILE *input_fp;
742
740 BcFunc *func; 743 BcFunc *func;
741 size_t fidx; 744 size_t fidx;
742 745
@@ -766,8 +769,6 @@ typedef struct BcProgram {
766 IF_DC(BcVec strs;) 769 IF_DC(BcVec strs;)
767 IF_DC(BcVec consts;) 770 IF_DC(BcVec consts;)
768 771
769 const char *file;
770
771 BcNum zero; 772 BcNum zero;
772 IF_BC(BcNum one;) 773 IF_BC(BcNum one;)
773 IF_BC(BcNum last;) 774 IF_BC(BcNum last;)
@@ -841,7 +842,6 @@ struct globals {
841 842
842 BcVec files; 843 BcVec files;
843 BcVec input_buffer; 844 BcVec input_buffer;
844 FILE *input_fp;
845 845
846 char *env_args; 846 char *env_args;
847 847
@@ -936,12 +936,12 @@ static void quit(void)
936static void bc_verror_msg(const char *fmt, va_list p) 936static void bc_verror_msg(const char *fmt, va_list p)
937{ 937{
938 const char *sv = sv; // for compiler 938 const char *sv = sv; // for compiler
939 if (G.prog.file) { 939 if (G.prs.filename) {
940 sv = applet_name; 940 sv = applet_name;
941 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); 941 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.filename, G.err_line);
942 } 942 }
943 bb_verror_msg(fmt, p, NULL); 943 bb_verror_msg(fmt, p, NULL);
944 if (G.prog.file) { 944 if (G.prs.filename) {
945 free((char*)applet_name); 945 free((char*)applet_name);
946 applet_name = sv; 946 applet_name = sv;
947 } 947 }
@@ -2581,12 +2581,12 @@ static void bc_read_line(BcVec *vec, FILE *fp)
2581 2581
2582 if (bad_chars) { 2582 if (bad_chars) {
2583 // Bad chars on this line 2583 // Bad chars on this line
2584 if (!G.prog.file) { // stdin 2584 if (!G.prs.filename) { // stdin
2585 // ignore entire line, get another one 2585 // ignore entire line, get another one
2586 vec->len = len; 2586 vec->len = len;
2587 goto again; 2587 goto again;
2588 } 2588 }
2589 bb_perror_msg_and_die("file '%s' is not text", G.prog.file); 2589 bb_perror_msg_and_die("file '%s' is not text", G.prs.filename);
2590 } 2590 }
2591 bc_vec_pushZeroByte(vec); 2591 bc_vec_pushZeroByte(vec);
2592 } 2592 }
@@ -2905,7 +2905,7 @@ static bool bc_lex_more_input(BcLex *l)
2905 size_t prevlen = G.input_buffer.len; 2905 size_t prevlen = G.input_buffer.len;
2906 char *string; 2906 char *string;
2907 2907
2908 bc_read_line(&G.input_buffer, G.input_fp); 2908 bc_read_line(&G.input_buffer, G.prs.input_fp);
2909 // No more input means EOF 2909 // No more input means EOF
2910 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte) 2910 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
2911 break; 2911 break;
@@ -2983,10 +2983,10 @@ static BC_STATUS zbc_lex_next(BcLex *l)
2983 do { 2983 do {
2984 if (l->i == l->len) { 2984 if (l->i == l->len) {
2985 l->lex = XC_LEX_EOF; 2985 l->lex = XC_LEX_EOF;
2986 if (!G.input_fp) 2986 if (!G.prs.input_fp)
2987 RETURN_STATUS(BC_STATUS_SUCCESS); 2987 RETURN_STATUS(BC_STATUS_SUCCESS);
2988 if (!bc_lex_more_input(l)) { 2988 if (!bc_lex_more_input(l)) {
2989 G.input_fp = NULL; 2989 G.prs.input_fp = NULL;
2990 RETURN_STATUS(BC_STATUS_SUCCESS); 2990 RETURN_STATUS(BC_STATUS_SUCCESS);
2991 } 2991 }
2992 // here it's guaranteed that l->i is below l->len 2992 // here it's guaranteed that l->i is below l->len
@@ -5256,35 +5256,32 @@ static BC_STATUS zbc_program_op(char inst)
5256 5256
5257static BC_STATUS zbc_program_read(void) 5257static BC_STATUS zbc_program_read(void)
5258{ 5258{
5259 const char *sv_file;
5260 BcStatus s; 5259 BcStatus s;
5261 BcParse parse; 5260 BcParse sv_parse;
5262 BcVec buf; 5261 BcVec buf;
5263 BcInstPtr ip; 5262 BcInstPtr ip;
5264 BcFunc *f; 5263 BcFunc *f;
5265 5264
5266 f = bc_program_func(BC_PROG_READ);
5267 bc_vec_pop_all(&f->code);
5268
5269 sv_file = G.prog.file;
5270 G.prog.file = NULL;
5271
5272 bc_char_vec_init(&buf); 5265 bc_char_vec_init(&buf);
5273 bc_read_line(&buf, stdin); 5266 bc_read_line(&buf, stdin);
5274 5267
5275 bc_parse_create(&parse, BC_PROG_READ); 5268 f = bc_program_func(BC_PROG_READ);
5276 bc_lex_file(&parse.l); 5269 bc_vec_pop_all(&f->code);
5277 5270
5278 s = zbc_parse_text_init(&parse, buf.v); 5271 sv_parse = G.prs; // struct copy
5272 bc_parse_create(&G.prs, BC_PROG_READ);
5273 //bc_lex_file(&G.prs.l); - not needed, error line info is not printed for read()
5274
5275 s = zbc_parse_text_init(&G.prs, buf.v);
5279 if (s) goto exec_err; 5276 if (s) goto exec_err;
5280 if (IS_BC) { 5277 if (IS_BC) {
5281 IF_BC(s = zbc_parse_expr(&parse, 0)); 5278 IF_BC(s = zbc_parse_expr(&G.prs, 0));
5282 } else { 5279 } else {
5283 IF_DC(s = zdc_parse_exprs_until_eof(&parse)); 5280 IF_DC(s = zdc_parse_exprs_until_eof(&G.prs));
5284 } 5281 }
5285 if (s) goto exec_err; 5282 if (s) goto exec_err;
5286 5283
5287 if (parse.l.lex != XC_LEX_NLINE && parse.l.lex != XC_LEX_EOF) { 5284 if (G.prs.l.lex != XC_LEX_NLINE && G.prs.l.lex != XC_LEX_EOF) {
5288 s = bc_error("bad read() expression"); 5285 s = bc_error("bad read() expression");
5289 goto exec_err; 5286 goto exec_err;
5290 } 5287 }
@@ -5293,11 +5290,12 @@ static BC_STATUS zbc_program_read(void)
5293 ip.inst_idx = 0; 5290 ip.inst_idx = 0;
5294 IF_BC(ip.results_len_before_call = G.prog.results.len;) 5291 IF_BC(ip.results_len_before_call = G.prog.results.len;)
5295 5292
5296 bc_parse_push(&parse, XC_INST_RET); 5293 bc_parse_push(&G.prs, XC_INST_RET);
5297 bc_vec_push(&G.prog.exestack, &ip); 5294 bc_vec_push(&G.prog.exestack, &ip);
5295
5298 exec_err: 5296 exec_err:
5299 bc_parse_free(&parse); 5297 bc_parse_free(&G.prs);
5300 G.prog.file = sv_file; 5298 G.prs = sv_parse; // struct copy
5301 bc_vec_free(&buf); 5299 bc_vec_free(&buf);
5302 RETURN_STATUS(s); 5300 RETURN_STATUS(s);
5303} 5301}
@@ -6402,30 +6400,27 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
6402 f = bc_program_func(fidx); 6400 f = bc_program_func(fidx);
6403 6401
6404 if (f->code.len == 0) { 6402 if (f->code.len == 0) {
6405 FILE *sv_input_fp; 6403 BcParse sv_parse;
6406 BcParse prs;
6407 char *str; 6404 char *str;
6408 6405
6409 bc_parse_create(&prs, fidx); 6406 sv_parse = G.prs; // struct copy
6407 bc_parse_create(&G.prs, fidx);
6410 str = *bc_program_str(sidx); 6408 str = *bc_program_str(sidx);
6411 s = zbc_parse_text_init(&prs, str); 6409 s = zbc_parse_text_init(&G.prs, str);
6412 if (s) goto err; 6410 if (s) goto err;
6413 6411
6414 sv_input_fp = G.input_fp; 6412 s = zdc_parse_exprs_until_eof(&G.prs);
6415 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6416 s = zdc_parse_exprs_until_eof(&prs);
6417 G.input_fp = sv_input_fp;
6418
6419 if (s) goto err; 6413 if (s) goto err;
6420 if (prs.l.lex != XC_LEX_EOF) { 6414 bc_parse_push(&G.prs, DC_INST_POP_EXEC);
6415 if (G.prs.l.lex != XC_LEX_EOF)
6421 s = bc_error_bad_expression(); 6416 s = bc_error_bad_expression();
6417 bc_parse_free(&G.prs);
6418 G.prs = sv_parse; // struct copy
6419 if (s) {
6422 err: 6420 err:
6423 bc_parse_free(&prs);
6424 bc_vec_pop_all(&f->code); 6421 bc_vec_pop_all(&f->code);
6425 goto exit; 6422 goto exit;
6426 } 6423 }
6427 bc_parse_push(&prs, DC_INST_POP_EXEC);
6428 bc_parse_free(&prs);
6429 } 6424 }
6430 6425
6431 ip.inst_idx = 0; 6426 ip.inst_idx = 0;
@@ -6871,12 +6866,12 @@ static BC_STATUS zbc_vm_process(const char *text)
6871static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) 6866static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
6872{ 6867{
6873 // So far bc/dc have no way to include a file from another file, 6868 // So far bc/dc have no way to include a file from another file,
6874 // therefore we know G.prog.file == NULL on entry 6869 // therefore we know G.prs.filename == NULL on entry
6875 //const char *sv_file; 6870 //const char *sv_file;
6876 BcStatus s; 6871 BcStatus s;
6877 6872
6878 G.prog.file = filename; 6873 G.prs.filename = filename;
6879 G.input_fp = fp; 6874 G.prs.input_fp = fp;
6880 bc_lex_file(&G.prs.l); 6875 bc_lex_file(&G.prs.l);
6881 6876
6882 do { 6877 do {
@@ -6885,8 +6880,8 @@ static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
6885 // Example: start interactive bc and enter "return". 6880 // Example: start interactive bc and enter "return".
6886 // It should say "'return' not in a function" 6881 // It should say "'return' not in a function"
6887 // but should not exit. 6882 // but should not exit.
6888 } while (G.input_fp == stdin); 6883 } while (G.prs.input_fp == stdin);
6889 G.prog.file = NULL; 6884 G.prs.filename = NULL;
6890 RETURN_STATUS(s); 6885 RETURN_STATUS(s);
6891} 6886}
6892#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS) 6887#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)