]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/source.def
895e98bde023e93398dc7b274a22601b259ba63d
[thirdparty/bash.git] / builtins / source.def
1 This file is source.def, from which is created source.c.
2 It implements the builtins "." and "source" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 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 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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES source.c
23
24 $BUILTIN source
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.
29 $END
30 $BUILTIN .
31 $DOCNAME dot
32 $FUNCTION source_builtin
33 $SHORT_DOC . filename
34 Read and execute commands from FILENAME and return. The pathnames
35 in $PATH are used to find the directory containing FILENAME.
36 $END
37 /* source.c - Implements the `.' and `source' builtins. */
38
39 #include <sys/types.h>
40 #include <sys/file.h>
41 #include <errno.h>
42
43 #if defined (HAVE_STRING_H)
44 # include <string.h>
45 #else /* !HAVE_STRING_H */
46 # include <strings.h>
47 #endif /* !HAVE_STRING_H */
48
49 #include "../shell.h"
50 #include "../posixstat.h"
51 #include "../filecntl.h"
52 #include "../execute_cmd.h"
53
54 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
55 #if !defined (errno)
56 extern int errno;
57 #endif /* !errno */
58
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;
64
65 /* How many `levels' of sourced files we have. */
66 int sourcelevel = 0;
67
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. */
73 static void
74 maybe_pop_dollar_vars ()
75 {
76 if (dollar_vars_changed ())
77 {
78 dispose_saved_dollar_vars ();
79 set_dollar_vars_unchanged ();
80 }
81 else
82 pop_dollar_vars ();
83 }
84
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. */
89 source_builtin (list)
90 WORD_LIST *list;
91 {
92 int result, return_val;
93
94 /* Assume the best. */
95 result = EXECUTION_SUCCESS;
96
97 if (list)
98 {
99 char *string, *filename;
100 struct stat finfo;
101 int fd, tt;
102
103 filename = find_path_file (list->word->word);
104 if (!filename)
105 filename = savestring (list->word->word);
106
107 if (((fd = open (filename, O_RDONLY)) < 0) || (fstat (fd, &finfo) < 0))
108 goto file_error_exit;
109
110 string = (char *)xmalloc (1 + (int)finfo.st_size);
111 tt = read (fd, string, finfo.st_size);
112 string[finfo.st_size] = '\0';
113
114 /* Close the open file, preserving the state of errno. */
115 { int temp = errno; close (fd); errno = temp; }
116
117 if (tt != finfo.st_size)
118 {
119 free (string);
120
121 file_error_exit:
122 file_error (filename);
123 free (filename);
124
125 /* POSIX shells exit if non-interactive and file error. */
126 if (posixly_correct && !interactive_shell)
127 {
128 last_command_exit_value = 1;
129 longjmp (top_level, EXITPROG);
130 }
131
132 return (EXECUTION_FAILURE);
133 }
134
135 if (tt > 80)
136 tt = 80;
137
138 if (check_binary_file ((unsigned char *)string, tt))
139 {
140 free (string);
141 builtin_error ("%s: cannot execute binary file", filename);
142 free (filename);
143 return (EX_BINARY_FILE);
144 }
145
146 begin_unwind_frame ("File Sourcing");
147
148 if (list->next)
149 {
150 push_dollar_vars ();
151 add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
152 remember_args (list->next, 1);
153 }
154
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);
160 interactive = 0;
161 sourcelevel++;
162
163 set_dollar_vars_unchanged ();
164
165 return_catch_flag++;
166 return_val = setjmp (return_catch);
167
168 if (return_val)
169 parse_and_execute_cleanup ();
170 else
171 result = parse_and_execute (string, filename, -1);
172
173 run_unwind_frame ("File Sourcing");
174
175 /* If RETURN_VAL is non-zero, then we return the value given
176 to return_builtin (), since that is how we got here. */
177 if (return_val)
178 result = return_catch_value;
179 }
180 else
181 {
182 builtin_error ("filename argument required");
183 result = EXECUTION_FAILURE;
184 }
185 return (result);
186 }