aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-05-27 18:05:42 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-05-27 18:05:42 +0200
commit528808bcd25f7d237874dc82fad2adcddf354b42 (patch)
treebfbc6d65a49d2e327a1681d97ccb6c15d131758c
parent84ff1825dd82e8de45020e3def34d1430d8e5a99 (diff)
downloadbusybox-w32-528808bcd25f7d237874dc82fad2adcddf354b42.tar.gz
busybox-w32-528808bcd25f7d237874dc82fad2adcddf354b42.tar.bz2
busybox-w32-528808bcd25f7d237874dc82fad2adcddf354b42.zip
awk: get rid of one indirection level for iF (input file structure)
function old new delta try_to_assign - 91 +91 next_input_file 214 216 +2 awk_main 827 826 -1 evaluate 3403 3396 -7 is_assignment 91 - -91 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 1/2 up/down: 93/-99) Total: -6 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/awk.c78
1 files changed, 41 insertions, 37 deletions
diff --git a/editors/awk.c b/editors/awk.c
index b3748b502..22f52417d 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -546,7 +546,6 @@ struct globals {
546 chain beginseq, mainseq, endseq; 546 chain beginseq, mainseq, endseq;
547 chain *seq; 547 chain *seq;
548 node *break_ptr, *continue_ptr; 548 node *break_ptr, *continue_ptr;
549 rstream *iF;
550 xhash *ahash; /* argument names, used only while parsing function bodies */ 549 xhash *ahash; /* argument names, used only while parsing function bodies */
551 xhash *fnhash; /* function names, used only in parsing stage */ 550 xhash *fnhash; /* function names, used only in parsing stage */
552 xhash *vhash; /* variables and arrays */ 551 xhash *vhash; /* variables and arrays */
@@ -579,11 +578,12 @@ struct globals2 {
579 578
580 var *intvar[NUM_INTERNAL_VARS]; /* often used */ 579 var *intvar[NUM_INTERNAL_VARS]; /* often used */
581 580
581 rstream iF;
582
582 /* former statics from various functions */ 583 /* former statics from various functions */
583 char *split_f0__fstrings; 584 char *split_f0__fstrings;
584 585
585 rstream next_input_file__rsm; 586 smallint next_input_file__input_file_seen;
586 smallint next_input_file__files_happen;
587 587
588 smalluint exitcode; 588 smalluint exitcode;
589 589
@@ -618,7 +618,6 @@ struct globals2 {
618#define seq (G1.seq ) 618#define seq (G1.seq )
619#define break_ptr (G1.break_ptr ) 619#define break_ptr (G1.break_ptr )
620#define continue_ptr (G1.continue_ptr) 620#define continue_ptr (G1.continue_ptr)
621#define iF (G1.iF )
622#define ahash (G1.ahash ) 621#define ahash (G1.ahash )
623#define fnhash (G1.fnhash ) 622#define fnhash (G1.fnhash )
624#define vhash (G1.vhash ) 623#define vhash (G1.vhash )
@@ -644,6 +643,7 @@ struct globals2 {
644#define t_string (G.t_string ) 643#define t_string (G.t_string )
645#define t_lineno (G.t_lineno ) 644#define t_lineno (G.t_lineno )
646#define intvar (G.intvar ) 645#define intvar (G.intvar )
646#define iF (G.iF )
647#define fsplitter (G.fsplitter ) 647#define fsplitter (G.fsplitter )
648#define rsplitter (G.rsplitter ) 648#define rsplitter (G.rsplitter )
649#define g_buf (G.g_buf ) 649#define g_buf (G.g_buf )
@@ -2799,7 +2799,7 @@ static NOINLINE var *exec_builtin(node *op, var *res)
2799 2799
2800/* if expr looks like "var=value", perform assignment and return 1, 2800/* if expr looks like "var=value", perform assignment and return 1,
2801 * otherwise return 0 */ 2801 * otherwise return 0 */
2802static int is_assignment(const char *expr) 2802static int try_to_assign(const char *expr)
2803{ 2803{
2804 char *exprc, *val; 2804 char *exprc, *val;
2805 2805
@@ -2819,39 +2819,44 @@ static int is_assignment(const char *expr)
2819} 2819}
2820 2820
2821/* switch to next input file */ 2821/* switch to next input file */
2822static rstream *next_input_file(void) 2822static int next_input_file(void)
2823{ 2823{
2824#define rsm (G.next_input_file__rsm) 2824#define input_file_seen (G.next_input_file__input_file_seen)
2825#define files_happen (G.next_input_file__files_happen) 2825 const char *fname;
2826
2827 const char *fname, *ind;
2828 2826
2829 if (rsm.F) 2827 if (iF.F) {
2830 fclose(rsm.F); 2828 fclose(iF.F);
2831 rsm.F = NULL; 2829 iF.F = NULL;
2832 rsm.pos = rsm.adv = 0; 2830 iF.pos = iF.adv = 0;
2831 }
2833 2832
2834 for (;;) { 2833 for (;;) {
2834 const char *ind;
2835
2835 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) { 2836 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
2836 if (files_happen) 2837 if (input_file_seen)
2837 return NULL; 2838 return FALSE;
2838 fname = "-"; 2839 fname = "-";
2839 rsm.F = stdin; 2840 iF.F = stdin;
2840 break; 2841 break;
2841 } 2842 }
2842 ind = getvar_s(incvar(intvar[ARGIND])); 2843 ind = getvar_s(incvar(intvar[ARGIND]));
2843 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind)); 2844 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
2844 if (fname && *fname && !is_assignment(fname)) { 2845 if (fname && *fname) {
2845 rsm.F = xfopen_stdin(fname); 2846 /* "If a filename on the command line has the form
2847 * var=val it is treated as a variable assignment"
2848 */
2849 if (try_to_assign(fname))
2850 continue;
2851 iF.F = xfopen_stdin(fname);
2846 break; 2852 break;
2847 } 2853 }
2848 } 2854 }
2849 2855
2850 files_happen = TRUE;
2851 setvar_s(intvar[FILENAME], fname); 2856 setvar_s(intvar[FILENAME], fname);
2852 return &rsm; 2857 input_file_seen = TRUE;
2853#undef rsm 2858 return TRUE;
2854#undef files_happen 2859#undef input_file_seen
2855} 2860}
2856 2861
2857/* 2862/*
@@ -3231,12 +3236,12 @@ static var *evaluate(node *op, var *res)
3231 } 3236 }
3232 } 3237 }
3233 } else { 3238 } else {
3234 if (!iF) 3239 if (!iF.F)
3235 iF = next_input_file(); 3240 next_input_file();
3236 rsm = iF; 3241 rsm = &iF;
3237 } 3242 }
3238 3243
3239 if (!rsm || !rsm->F) { 3244 if (!rsm->F) {
3240 setvar_i(intvar[ERRNO], errno); 3245 setvar_i(intvar[ERRNO], errno);
3241 setvar_i(res, -1); 3246 setvar_i(res, -1);
3242 break; 3247 break;
@@ -3659,7 +3664,7 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
3659 setvar_s(intvar[FS], opt_F); 3664 setvar_s(intvar[FS], opt_F);
3660 } 3665 }
3661 while (list_v) { 3666 while (list_v) {
3662 if (!is_assignment(llist_pop(&list_v))) 3667 if (!try_to_assign(llist_pop(&list_v)))
3663 bb_show_usage(); 3668 bb_show_usage();
3664 } 3669 }
3665 3670
@@ -3718,15 +3723,14 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
3718 awk_exit(); 3723 awk_exit();
3719 3724
3720 /* input file could already be opened in BEGIN block */ 3725 /* input file could already be opened in BEGIN block */
3721 if (!iF) 3726 if (!iF.F)
3722 iF = next_input_file(); 3727 goto next_file; /* no, it wasn't, go try opening */
3723 3728 /* Iterate over input files */
3724 /* passing through input files */ 3729 for (;;) {
3725 while (iF) {
3726 nextfile = FALSE; 3730 nextfile = FALSE;
3727 setvar_i(intvar[FNR], 0); 3731 setvar_i(intvar[FNR], 0);
3728 3732
3729 while ((i = awk_getline(iF, intvar[F0])) > 0) { 3733 while ((i = awk_getline(&iF, intvar[F0])) > 0) {
3730 nextrec = FALSE; 3734 nextrec = FALSE;
3731 incvar(intvar[NR]); 3735 incvar(intvar[NR]);
3732 incvar(intvar[FNR]); 3736 incvar(intvar[FNR]);
@@ -3735,11 +3739,11 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
3735 if (nextfile) 3739 if (nextfile)
3736 break; 3740 break;
3737 } 3741 }
3738
3739 if (i < 0) 3742 if (i < 0)
3740 syntax_error(strerror(errno)); 3743 syntax_error(strerror(errno));
3741 3744 next_file:
3742 iF = next_input_file(); 3745 if (!next_input_file())
3746 break;
3743 } 3747 }
3744 3748
3745 awk_exit(); 3749 awk_exit();