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-2003 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION source_builtin
26 $SHORT_DOC source filename [arguments]
27 Read and execute commands from FILENAME and return. The pathnames
28 in $PATH are used to find the directory containing FILENAME. If any
29 ARGUMENTS are supplied, they become the positional parameters when
34 $FUNCTION source_builtin
35 $SHORT_DOC . filename [arguments]
36 Read and execute commands from FILENAME and return. The pathnames
37 in $PATH are used to find the directory containing FILENAME. If any
38 ARGUMENTS are supplied, they become the positional parameters when
41 /* source.c - Implements the `.' and `source' builtins. */
45 #include "../bashtypes.h"
46 #include "posixstat.h"
48 #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
49 # include <sys/file.h>
53 #if defined (HAVE_UNISTD_H)
57 #include "../bashansi.h"
58 #include "../bashintl.h"
62 #include "../findcmd.h"
64 #include "bashgetopt.h"
71 #if defined (RESTRICTED_SHELL)
72 extern int restricted;
75 /* If non-zero, `.' uses $PATH to look up the script to be sourced. */
76 int source_uses_path = 1;
78 /* If non-zero, `.' looks in the current directory if the filename argument
79 is not found in the $PATH. */
80 int source_searches_cwd = 1;
82 /* If this . script is supplied arguments, we save the dollar vars and
83 replace them with the script arguments for the duration of the script's
84 execution. If the script does not change the dollar vars, we restore
85 what we saved. If the dollar vars are changed in the script, and we are
86 not executing a shell function, we leave the new values alone and free
89 maybe_pop_dollar_vars ()
91 if (variable_context == 0 && (dollar_vars_changed () & ARGS_SETBLTIN))
92 dispose_saved_dollar_vars ();
96 pop_args (); /* restore BASH_ARGC and BASH_ARGV */
97 set_dollar_vars_unchanged ();
100 /* Read and execute commands from the file passed as argument. Guess what.
101 This cannot be done in a subshell, since things like variable assignments
102 take place in there. So, I open the file, place it into a large string,
103 close the file, and then execute the string. */
105 source_builtin (list)
109 char *filename, *debug_trap;
111 if (no_options (list))
117 builtin_error (_("filename argument required"));
122 #if defined (RESTRICTED_SHELL)
123 if (restricted && strchr (list->word->word, '/'))
125 sh_restricted (list->word->word);
126 return (EXECUTION_FAILURE);
130 filename = (char *)NULL;
131 if (source_uses_path)
132 filename = find_path_file (list->word->word);
135 if (source_searches_cwd == 0)
137 builtin_error (_("%s: file not found"), list->word->word);
138 return (EXECUTION_FAILURE);
141 filename = savestring (list->word->word);
144 begin_unwind_frame ("source");
145 add_unwind_protect ((Function *)xfree, filename);
150 add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
151 remember_args (list->next, 1);
153 push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */
155 set_dollar_vars_unchanged ();
157 /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded)
158 is set. XXX - should sourced files inherit the RETURN trap? Functions
160 debug_trap = TRAP_STRING (DEBUG_TRAP);
161 if (debug_trap && function_trace_mode == 0)
163 debug_trap = savestring (debug_trap);
164 add_unwind_protect (xfree, debug_trap);
165 add_unwind_protect (set_debug_trap, debug_trap);
166 restore_default_signal (DEBUG_TRAP);
169 result = source_file (filename, (list && list->next));
171 run_unwind_frame ("source");