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, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION source_builtin
26 $SHORT_DOC source filename
27 Read and execute commands from FILENAME and return. The pathnames
28 in $PATH are used to find the directory containing FILENAME.
32 $FUNCTION source_builtin
34 Read and execute commands from FILENAME and return. The pathnames
35 in $PATH are used to find the directory containing FILENAME.
37 /* source.c - Implements the `.' and `source' builtins. */
39 #include <sys/types.h>
43 #if defined (HAVE_STRING_H)
45 #else /* !HAVE_STRING_H */
47 #endif /* !HAVE_STRING_H */
50 #include "../posixstat.h"
51 #include "../filecntl.h"
52 #include "../execute_cmd.h"
54 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
59 /* Variables used here but defined in other files. */
60 extern int return_catch_flag, return_catch_value;
61 extern jmp_buf return_catch;
62 extern int posixly_correct;
63 extern int interactive, interactive_shell, last_command_exit_value;
65 /* How many `levels' of sourced files we have. */
68 /* If this . script is supplied arguments, we save the dollar vars and
69 replace them with the script arguments for the duration of the script's
70 execution. If the script does not change the dollar vars, we restore
71 what we saved. If the dollar vars are changed in the script, we leave
72 the new values alone and free the saved values. */
74 maybe_pop_dollar_vars ()
76 if (dollar_vars_changed ())
78 dispose_saved_dollar_vars ();
79 set_dollar_vars_unchanged ();
85 /* Read and execute commands from the file passed as argument. Guess what.
86 This cannot be done in a subshell, since things like variable assignments
87 take place in there. So, I open the file, place it into a large string,
88 close the file, and then execute the string. */
92 int result, return_val;
94 /* Assume the best. */
95 result = EXECUTION_SUCCESS;
99 char *string, *filename;
103 filename = find_path_file (list->word->word);
105 filename = savestring (list->word->word);
107 if (((fd = open (filename, O_RDONLY)) < 0) || (fstat (fd, &finfo) < 0))
108 goto file_error_exit;
110 string = (char *)xmalloc (1 + (int)finfo.st_size);
111 tt = read (fd, string, finfo.st_size);
112 string[finfo.st_size] = '\0';
114 /* Close the open file, preserving the state of errno. */
115 { int temp = errno; close (fd); errno = temp; }
117 if (tt != finfo.st_size)
122 file_error (filename);
125 /* POSIX shells exit if non-interactive and file error. */
126 if (posixly_correct && !interactive_shell)
128 last_command_exit_value = 1;
129 longjmp (top_level, EXITPROG);
132 return (EXECUTION_FAILURE);
138 if (check_binary_file ((unsigned char *)string, tt))
141 builtin_error ("%s: cannot execute binary file", filename);
143 return (EX_BINARY_FILE);
146 begin_unwind_frame ("File Sourcing");
151 add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
152 remember_args (list->next, 1);
155 unwind_protect_int (return_catch_flag);
156 unwind_protect_jmp_buf (return_catch);
157 unwind_protect_int (interactive);
158 unwind_protect_int (sourcelevel);
159 add_unwind_protect ((Function *)xfree, filename);
163 set_dollar_vars_unchanged ();
166 return_val = setjmp (return_catch);
169 parse_and_execute_cleanup ();
171 result = parse_and_execute (string, filename, -1);
173 run_unwind_frame ("File Sourcing");
175 /* If RETURN_VAL is non-zero, then we return the value given
176 to return_builtin (), since that is how we got here. */
178 result = return_catch_value;
182 builtin_error ("filename argument required");
183 result = EXECUTION_FAILURE;