Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is source.def, from which is created source.c. |
| 2 | It implements the builtins "." and "source" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES source.c |
| 22 | |
| 23 | $BUILTIN source |
| 24 | $FUNCTION source_builtin |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 25 | $SHORT_DOC source filename [arguments] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Execute commands from a file in the current shell. |
| 27 | |
| 28 | Read and execute commands from FILENAME in the current shell. The |
| 29 | entries in $PATH are used to find the directory containing FILENAME. |
| 30 | If any ARGUMENTS are supplied, they become the positional parameters |
| 31 | when FILENAME is executed. |
| 32 | |
| 33 | Exit Status: |
| 34 | Returns the status of the last command executed in FILENAME; fails if |
| 35 | FILENAME cannot be read. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 36 | $END |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 37 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 38 | $BUILTIN . |
| 39 | $DOCNAME dot |
| 40 | $FUNCTION source_builtin |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 41 | $SHORT_DOC . filename [arguments] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 42 | Execute commands from a file in the current shell. |
| 43 | |
| 44 | Read and execute commands from FILENAME in the current shell. The |
| 45 | entries in $PATH are used to find the directory containing FILENAME. |
| 46 | If any ARGUMENTS are supplied, they become the positional parameters |
| 47 | when FILENAME is executed. |
| 48 | |
| 49 | Exit Status: |
| 50 | Returns the status of the last command executed in FILENAME; fails if |
| 51 | FILENAME cannot be read. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | $END |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 53 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 54 | #include <config.h> |
| 55 | |
| 56 | #include "../bashtypes.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 57 | #include "posixstat.h" |
| 58 | #include "filecntl.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 59 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 60 | # include <sys/file.h> |
| 61 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 62 | #include <errno.h> |
| 63 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | #if defined (HAVE_UNISTD_H) |
| 65 | # include <unistd.h> |
| 66 | #endif |
| 67 | |
| 68 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 69 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 70 | |
| 71 | #include "../shell.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 72 | #include "../flags.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 73 | #include "../findcmd.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | #include "common.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 75 | #include "bashgetopt.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 76 | #include "../trap.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 77 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 79 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | #endif /* !errno */ |
| 81 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 82 | extern int posixly_correct; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 83 | extern int last_command_exit_value; |
| 84 | extern int executing_command_builtin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 85 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 86 | static void maybe_pop_dollar_vars __P((void)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 87 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 88 | /* If non-zero, `.' uses $PATH to look up the script to be sourced. */ |
| 89 | int source_uses_path = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 90 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 91 | /* If non-zero, `.' looks in the current directory if the filename argument |
| 92 | is not found in the $PATH. */ |
| 93 | int source_searches_cwd = 1; |
| 94 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 95 | /* If this . script is supplied arguments, we save the dollar vars and |
| 96 | replace them with the script arguments for the duration of the script's |
| 97 | execution. If the script does not change the dollar vars, we restore |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 98 | what we saved. If the dollar vars are changed in the script, and we are |
| 99 | not executing a shell function, we leave the new values alone and free |
| 100 | the saved values. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 101 | static void |
| 102 | maybe_pop_dollar_vars () |
| 103 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 104 | if (variable_context == 0 && (dollar_vars_changed () & ARGS_SETBLTIN)) |
| 105 | dispose_saved_dollar_vars (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 106 | else |
| 107 | pop_dollar_vars (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 108 | if (debugging_mode) |
| 109 | pop_args (); /* restore BASH_ARGC and BASH_ARGV */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 110 | set_dollar_vars_unchanged (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Read and execute commands from the file passed as argument. Guess what. |
| 114 | This cannot be done in a subshell, since things like variable assignments |
| 115 | take place in there. So, I open the file, place it into a large string, |
| 116 | close the file, and then execute the string. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 118 | source_builtin (list) |
| 119 | WORD_LIST *list; |
| 120 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 121 | int result; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 122 | char *filename, *debug_trap; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 123 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 124 | if (no_options (list)) |
| 125 | return (EX_USAGE); |
| 126 | list = loptend; |
| 127 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 128 | if (list == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 129 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 130 | builtin_error (_("filename argument required")); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 131 | builtin_usage (); |
| 132 | return (EX_USAGE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 133 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 135 | #if defined (RESTRICTED_SHELL) |
| 136 | if (restricted && strchr (list->word->word, '/')) |
| 137 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 138 | sh_restricted (list->word->word); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | return (EXECUTION_FAILURE); |
| 140 | } |
| 141 | #endif |
| 142 | |
| 143 | filename = (char *)NULL; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 144 | /* XXX -- should this be absolute_pathname? */ |
| 145 | if (posixly_correct && strchr (list->word->word, '/')) |
| 146 | filename = savestring (list->word->word); |
| 147 | else if (absolute_pathname (list->word->word)) |
| 148 | filename = savestring (list->word->word); |
| 149 | else if (source_uses_path) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 150 | filename = find_path_file (list->word->word); |
| 151 | if (filename == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 152 | { |
| 153 | if (source_searches_cwd == 0) |
| 154 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 155 | builtin_error (_("%s: file not found"), list->word->word); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 156 | if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) |
| 157 | { |
| 158 | last_command_exit_value = 1; |
| 159 | jump_to_top_level (EXITPROG); |
| 160 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 161 | return (EXECUTION_FAILURE); |
| 162 | } |
| 163 | else |
| 164 | filename = savestring (list->word->word); |
| 165 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 166 | |
| 167 | begin_unwind_frame ("source"); |
| 168 | add_unwind_protect ((Function *)xfree, filename); |
| 169 | |
| 170 | if (list->next) |
| 171 | { |
| 172 | push_dollar_vars (); |
| 173 | add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL); |
| 174 | remember_args (list->next, 1); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 175 | if (debugging_mode) |
| 176 | push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 177 | } |
| 178 | set_dollar_vars_unchanged (); |
| 179 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 180 | /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded) |
| 181 | is set. XXX - should sourced files inherit the RETURN trap? Functions |
| 182 | don't. */ |
| 183 | debug_trap = TRAP_STRING (DEBUG_TRAP); |
| 184 | if (debug_trap && function_trace_mode == 0) |
| 185 | { |
| 186 | debug_trap = savestring (debug_trap); |
| 187 | add_unwind_protect (xfree, debug_trap); |
| 188 | add_unwind_protect (set_debug_trap, debug_trap); |
| 189 | restore_default_signal (DEBUG_TRAP); |
| 190 | } |
| 191 | |
| 192 | result = source_file (filename, (list && list->next)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 193 | |
| 194 | run_unwind_frame ("source"); |
| 195 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 196 | return (result); |
| 197 | } |