1 This file is source.def, from which is created source.c.
2 It implements the builtins "." and "source" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
24 $FUNCTION source_builtin
25 $SHORT_DOC source filename [arguments]
26 Execute commands from a file in the current shell.
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.
34 Returns the status of the last command executed in FILENAME; fails if
35 FILENAME cannot be read.
40 $FUNCTION source_builtin
41 $SHORT_DOC . filename [arguments]
42 Execute commands from a file in the current shell.
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.
50 Returns the status of the last command executed in FILENAME; fails if
51 FILENAME cannot be read.
56 #include "../bashtypes.h"
57 #include "posixstat.h"
59 #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
60 # include <sys/file.h>
64 #if defined (HAVE_UNISTD_H)
68 #include "../bashansi.h"
69 #include "../bashintl.h"
73 #include "../findcmd.h"
75 #include "bashgetopt.h"
82 extern int posixly_correct;
84 static void maybe_pop_dollar_vars __P((void));
86 /* If non-zero, `.' uses $PATH to look up the script to be sourced. */
87 int source_uses_path = 1;
89 /* If non-zero, `.' looks in the current directory if the filename argument
90 is not found in the $PATH. */
91 int source_searches_cwd = 1;
93 /* If this . script is supplied arguments, we save the dollar vars and
94 replace them with the script arguments for the duration of the script's
95 execution. If the script does not change the dollar vars, we restore
96 what we saved. If the dollar vars are changed in the script, and we are
97 not executing a shell function, we leave the new values alone and free
100 maybe_pop_dollar_vars ()
102 if (variable_context == 0 && (dollar_vars_changed () & ARGS_SETBLTIN))
103 dispose_saved_dollar_vars ();
107 pop_args (); /* restore BASH_ARGC and BASH_ARGV */
108 set_dollar_vars_unchanged ();
111 /* Read and execute commands from the file passed as argument. Guess what.
112 This cannot be done in a subshell, since things like variable assignments
113 take place in there. So, I open the file, place it into a large string,
114 close the file, and then execute the string. */
116 source_builtin (list)
120 char *filename, *debug_trap;
122 if (no_options (list))
128 builtin_error (_("filename argument required"));
133 #if defined (RESTRICTED_SHELL)
134 if (restricted && strchr (list->word->word, '/'))
136 sh_restricted (list->word->word);
137 return (EXECUTION_FAILURE);
141 filename = (char *)NULL;
142 /* XXX -- should this be absolute_pathname? */
143 if (posixly_correct && strchr (list->word->word, '/'))
144 filename = savestring (list->word->word);
145 else if (absolute_pathname (list->word->word))
146 filename = savestring (list->word->word);
147 else if (source_uses_path)
148 filename = find_path_file (list->word->word);
151 if (source_searches_cwd == 0)
153 builtin_error (_("%s: file not found"), list->word->word);
154 return (EXECUTION_FAILURE);
157 filename = savestring (list->word->word);
160 begin_unwind_frame ("source");
161 add_unwind_protect ((Function *)xfree, filename);
166 add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
167 remember_args (list->next, 1);
169 push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */
171 set_dollar_vars_unchanged ();
173 /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded)
174 is set. XXX - should sourced files inherit the RETURN trap? Functions
176 debug_trap = TRAP_STRING (DEBUG_TRAP);
177 if (debug_trap && function_trace_mode == 0)
179 debug_trap = savestring (debug_trap);
180 add_unwind_protect (xfree, debug_trap);
181 add_unwind_protect (set_debug_trap, debug_trap);
182 restore_default_signal (DEBUG_TRAP);
185 result = source_file (filename, (list && list->next));
187 run_unwind_frame ("source");