Line data Source code
1 : This file is return.def, from which is created return.c. 2 : It implements the builtin "return" in Bash. 3 : 4 : Copyright (C) 1987-2015 Free Software Foundation, Inc. 5 : 6 : This file is part of GNU Bash, the Bourne Again SHell. 7 : 8 : Bash is free software: you can redistribute it and/or modify 9 : it under the terms of the GNU General Public License as published by 10 : the Free Software Foundation, either version 3 of the License, or 11 : (at your option) any later version. 12 : 13 : Bash is distributed in the hope that it will be useful, 14 : but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : GNU General Public License for more details. 17 : 18 : You should have received a copy of the GNU General Public License 19 : along with Bash. If not, see <http://www.gnu.org/licenses/>. 20 : 21 : $PRODUCES return.c 22 : 23 : $BUILTIN return 24 : 25 : $FUNCTION return_builtin 26 : $SHORT_DOC return [n] 27 : Return from a shell function. 28 : 29 : Causes a function or sourced script to exit with the return value 30 : specified by N. If N is omitted, the return status is that of the 31 : last command executed within the function or script. 32 : 33 : Exit Status: 34 : Returns N, or failure if the shell is not executing a function or script. 35 : $END 36 : 37 : #include <config.h> 38 : 39 : #if defined (HAVE_UNISTD_H) 40 : # ifdef _MINIX 41 : # include <sys/types.h> 42 : # endif 43 : # include <unistd.h> 44 : #endif 45 : 46 : #include "../bashintl.h" 47 : 48 : #include "../shell.h" 49 : #include "common.h" 50 : #include "bashgetopt.h" 51 : 52 : extern int last_command_exit_value; 53 : extern int subshell_environment; 54 : extern int return_catch_flag, return_catch_value; 55 : 56 : /* If we are executing a user-defined function then exit with the value 57 : specified as an argument. if no argument is given, then the last 58 : exit status is used. */ 59 : int 60 13653 : return_builtin (list) 61 : WORD_LIST *list; 62 : { 63 13653 : CHECK_HELPOPT (list); 64 : 65 13653 : return_catch_value = get_exitstat (list); 66 : 67 13626 : if (return_catch_flag) 68 5099 : sh_longjmp (return_catch, 1); 69 : else 70 : { 71 8527 : builtin_error (_("can only `return' from a function or sourced script")); 72 8527 : return (EXECUTION_FAILURE); 73 : } 74 : }