Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mailrus!cwjcc!kiwi!chet From: chet@kiwi.CWRU.EDU (Chet Ramey) Newsgroups: gnu.bash.bug Subject: Re: return values of the function and pipe Summary: return 0 from function will never work in vanilla bash 1.02, but I think I have a fix Message-ID: <420@cwjcc.CWRU.Edu> Date: 12 Jul 89 22:05:35 GMT References: <8907111627.AA08421@vlsi.caltech.edu> Sender: news@cwjcc.CWRU.Edu Reply-To: chet@kiwi.INS.CWRU.Edu (Chet Ramey) Distribution: gnu Organization: CWRU Andrew R. Jennings Computing Center Lines: 107 In article <8907111627.AA08421@vlsi.caltech.edu> drazen@VLSI.CALTECH.EDU (Drazen Borkovic) writes: >There are 2 problems: >1. Return from a function doesn't work correctly. You're right, but I could not reproduce this exactly on a Sun-3 (SunOS 3.5), an IBM RT (4.3BSD), or a Vax (Ultrix 3.0). I never got the "can only `return' from a function" error you saw. > Value returned from the function is not correct. THIS CANNOT BE FIXED WITHOUT A REDESIGN (or at least a rethinking) OF THE FUNCTION EXECUTION MECHANISM. bash 1.02 relies on the property of longjmp that lets you specify what value the corresponding setjmp call will return when you call it. YOU CANNOT PORTABLY SPECIFY 0 HERE. Most longjmps that I know of (Sun, Ultrix) will change a 0 value to a 1, since setjmp will only return 0 the first time it is called. On 4.3 BSD, it appears that you can make setjmp return 0 by specifying 0 to longjmp, but that just makes bash recurse infinitely in execute_simple_command(). I just tried the YAGV (`Yet Another Global Variable') approach to fixing this, and it seems to work (at least for a variant of your pushd example) on the Sun, the VAX and the RT. Ugh. Patches appended, though they are not Officially Blessed >2. Value returned by the pipe is not correct. This looks like a race condition to me. *** bash-1.02/execute_cmd.c Sat Jul 1 21:44:45 1989 --- src-1.02/execute_cmd.c Wed Jul 12 17:23:17 1989 *************** *** 491,494 /* For catching RETURN in a function. */ int return_catch_flag = 0; jmp_buf return_catch; --- 491,495 ----- /* For catching RETURN in a function. */ int return_catch_flag = 0; + int return_return_val = 0; jmp_buf return_catch; *************** *** 636,640 if (return_val) { ! result = return_val; } else --- 637,641 ----- if (return_val) { ! result = return_return_val; } else *** bash-1.02/builtins.c Wed Jul 5 21:25:49 1989 --- src-1.02/builtins.c Wed Jul 12 17:12:46 1989 *************** *** 1642,1645 extern int last_command_exit_value; extern int return_catch_flag; extern jmp_buf return_catch; int return_val = get_numeric_arg ("return", list); --- 1662,1666 ----- extern int last_command_exit_value; extern int return_catch_flag; + extern int return_return_val; extern jmp_buf return_catch; int return_val = get_numeric_arg ("return", list); *************** *** 1648,1651 return_val = last_command_exit_value; if (return_catch_flag) longjmp (return_catch, return_val); --- 1669,1673 ----- return_val = last_command_exit_value; + return_return_val = return_val; if (return_catch_flag) longjmp (return_catch, 1); /* force a 1 */ *************** *** 1649,1653 if (return_catch_flag) ! longjmp (return_catch, return_val); else { report_error ("can only `return' from a function"); --- 1671,1675 ----- return_return_val = return_val; if (return_catch_flag) ! longjmp (return_catch, 1); /* force a 1 */ else { report_error ("can only `return' from a function"); Chet Ramey "We are preparing to think about contemplating Network Services Group, CWRU preliminary work on plans to develop a chet@cwjcc.INS.CWRU.Edu schedule for producing the 10th Edition of the Unix Programmers Manual." -- Andrew Hume