aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-07-02 22:28:51 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-07-02 22:28:51 +0200
commit4d902ea9def573cd15271177abbfa50fbf30c84f (patch)
tree17d9b8646d86f570506e9cc7b9098734620fc7cf
parent8bb03da906e1f8f750123214b15a19d7d4e166c1 (diff)
downloadbusybox-w32-4d902ea9def573cd15271177abbfa50fbf30c84f.tar.gz
busybox-w32-4d902ea9def573cd15271177abbfa50fbf30c84f.tar.bz2
busybox-w32-4d902ea9def573cd15271177abbfa50fbf30c84f.zip
awk: fix beavior of "exit" without parameter
function old new delta evaluate 3336 3339 +3 awk_exit 93 94 +1 awk_main 829 827 -2 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/1 up/down: 4/-2) Total: 2 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/awk.c40
-rwxr-xr-xtestsuite/awk.tests5
2 files changed, 27 insertions, 18 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 64fe81be4..86cb7a95f 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -578,6 +578,8 @@ struct globals2 {
578 rstream next_input_file__rsm; 578 rstream next_input_file__rsm;
579 smallint next_input_file__files_happen; 579 smallint next_input_file__files_happen;
580 580
581 smalluint exitcode;
582
581 unsigned evaluate__seed; 583 unsigned evaluate__seed;
582 var *evaluate__fnargs; 584 var *evaluate__fnargs;
583 regex_t evaluate__sreg; 585 regex_t evaluate__sreg;
@@ -655,7 +657,7 @@ static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
655static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; 657static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
656static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field"; 658static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field";
657 659
658static int awk_exit(int) NORETURN; 660static int awk_exit(void) NORETURN;
659 661
660static void syntax_error(const char *message) NORETURN; 662static void syntax_error(const char *message) NORETURN;
661static void syntax_error(const char *message) 663static void syntax_error(const char *message)
@@ -2779,14 +2781,14 @@ static var *evaluate(node *op, var *res)
2779 if ((opinfo & OF_REQUIRED) && !op1) 2781 if ((opinfo & OF_REQUIRED) && !op1)
2780 syntax_error(EMSG_TOO_FEW_ARGS); 2782 syntax_error(EMSG_TOO_FEW_ARGS);
2781 L.v = evaluate(op1, TMPVAR0); 2783 L.v = evaluate(op1, TMPVAR0);
2782 } 2784 if (opinfo & OF_STR1) {
2783 if (opinfo & OF_STR1) { 2785 L.s = getvar_s(L.v);
2784 L.s = getvar_s(L.v); 2786 debug_printf_eval("L.s:'%s'\n", L.s);
2785 debug_printf_eval("L.s:'%s'\n", L.s); 2787 }
2786 } 2788 if (opinfo & OF_NUM1) {
2787 if (opinfo & OF_NUM1) { 2789 L_d = getvar_i(L.v);
2788 L_d = getvar_i(L.v); 2790 debug_printf_eval("L_d:%f\n", L_d);
2789 debug_printf_eval("L_d:%f\n", L_d); 2791 }
2790 } 2792 }
2791 /* NB: Must get string/numeric values of L (done above) 2793 /* NB: Must get string/numeric values of L (done above)
2792 * _before_ evaluate()'ing R.v: if both L and R are $NNNs, 2794 * _before_ evaluate()'ing R.v: if both L and R are $NNNs,
@@ -2799,10 +2801,10 @@ static var *evaluate(node *op, var *res)
2799 R.v = evaluate(op->r.n, TMPVAR1); 2801 R.v = evaluate(op->r.n, TMPVAR1);
2800 //TODO: L.v may be invalid now, set L.v to NULL to catch bugs? 2802 //TODO: L.v may be invalid now, set L.v to NULL to catch bugs?
2801 //L.v = NULL; 2803 //L.v = NULL;
2802 } 2804 if (opinfo & OF_STR2) {
2803 if (opinfo & OF_STR2) { 2805 R.s = getvar_s(R.v);
2804 R.s = getvar_s(R.v); 2806 debug_printf_eval("R.s:'%s'\n", R.s);
2805 debug_printf_eval("R.s:'%s'\n", R.s); 2807 }
2806 } 2808 }
2807 2809
2808 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK)); 2810 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
@@ -2955,7 +2957,9 @@ static var *evaluate(node *op, var *res)
2955 2957
2956 case XC( OC_EXIT ): 2958 case XC( OC_EXIT ):
2957 debug_printf_eval("EXIT\n"); 2959 debug_printf_eval("EXIT\n");
2958 awk_exit(L_d); 2960 if (op1)
2961 G.exitcode = (int)L_d;
2962 awk_exit();
2959 2963
2960 /* -- recursive node type -- */ 2964 /* -- recursive node type -- */
2961 2965
@@ -3414,7 +3418,7 @@ static var *evaluate(node *op, var *res)
3414 3418
3415/* -------- main & co. -------- */ 3419/* -------- main & co. -------- */
3416 3420
3417static int awk_exit(int r) 3421static int awk_exit(void)
3418{ 3422{
3419 unsigned i; 3423 unsigned i;
3420 3424
@@ -3435,7 +3439,7 @@ static int awk_exit(int r)
3435 } 3439 }
3436 } 3440 }
3437 3441
3438 exit(r); 3442 exit(G.exitcode);
3439} 3443}
3440 3444
3441int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 3445int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -3560,7 +3564,7 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
3560 3564
3561 evaluate(beginseq.first, &G.main__tmpvar); 3565 evaluate(beginseq.first, &G.main__tmpvar);
3562 if (!mainseq.first && !endseq.first) 3566 if (!mainseq.first && !endseq.first)
3563 awk_exit(EXIT_SUCCESS); 3567 awk_exit();
3564 3568
3565 /* input file could already be opened in BEGIN block */ 3569 /* input file could already be opened in BEGIN block */
3566 if (!iF) 3570 if (!iF)
@@ -3587,6 +3591,6 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
3587 iF = next_input_file(); 3591 iF = next_input_file();
3588 } 3592 }
3589 3593
3590 awk_exit(EXIT_SUCCESS); 3594 awk_exit();
3591 /*return 0;*/ 3595 /*return 0;*/
3592} 3596}
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index 3c230393f..770d8ffce 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -445,4 +445,9 @@ testing 'awk $NF is empty' \
445 '' \ 445 '' \
446 'a=====123=' 446 'a=====123='
447 447
448testing "awk exit N propagates through END's exit" \
449 "awk 'BEGIN { exit 42 } END { exit }'; echo \$?" \
450 "42\n" \
451 '' ''
452
448exit $FAILCOUNT 453exit $FAILCOUNT