Initialize Tizen 2.3
[external/bash.git] / builtins / evalfile.c
1 /* evalfile.c - read and evaluate commands from a file or file descriptor */
2
3 /* Copyright (C) 1996-2009 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Bash is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22
23 #if defined (HAVE_UNISTD_H)
24 #  include <unistd.h>
25 #endif
26
27 #include "../bashtypes.h"
28 #include "posixstat.h"
29 #include "filecntl.h"
30
31 #include <stdio.h>
32 #include <signal.h>
33 #include <errno.h>
34
35 #include "../bashansi.h"
36 #include "../bashintl.h"
37
38 #include "../shell.h"
39 #include "../jobs.h"
40 #include "../builtins.h"
41 #include "../flags.h"
42 #include "../input.h"
43 #include "../execute_cmd.h"
44 #include "../trap.h"
45
46 #if defined (HISTORY)
47 #  include "../bashhist.h"
48 #endif
49
50 #include <typemax.h>
51
52 #include "common.h"
53
54 #if !defined (errno)
55 extern int errno;
56 #endif
57
58 /* Flags for _evalfile() */
59 #define FEVAL_ENOENTOK          0x001
60 #define FEVAL_BUILTIN           0x002
61 #define FEVAL_UNWINDPROT        0x004
62 #define FEVAL_NONINT            0x008
63 #define FEVAL_LONGJMP           0x010
64 #define FEVAL_HISTORY           0x020
65 #define FEVAL_CHECKBINARY       0x040
66 #define FEVAL_REGFILE           0x080
67 #define FEVAL_NOPUSHARGS        0x100
68
69 extern int posixly_correct;
70 extern int indirection_level, subshell_environment;
71 extern int return_catch_flag, return_catch_value;
72 extern int last_command_exit_value;
73
74 /* How many `levels' of sourced files we have. */
75 int sourcelevel = 0;
76
77 static int
78 _evalfile (filename, flags)
79      const char *filename;
80      int flags;
81 {
82   volatile int old_interactive;
83   procenv_t old_return_catch;
84   int return_val, fd, result, pflags, i, nnull;
85   ssize_t nr;                   /* return value from read(2) */
86   char *string;
87   struct stat finfo;
88   size_t file_size;
89   sh_vmsg_func_t *errfunc;
90 #if defined (ARRAY_VARS)
91   SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v;
92   ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
93 #  if defined (DEBUGGER)
94   SHELL_VAR *bash_argv_v, *bash_argc_v;
95   ARRAY *bash_argv_a, *bash_argc_a;
96 #  endif
97   char *t, tt[2];
98 #endif
99
100   USE_VAR(pflags);
101
102 #if defined (ARRAY_VARS)
103   GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
104   GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
105   GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
106 #  if defined (DEBUGGER)
107   GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v, bash_argv_a);
108   GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v, bash_argc_a);
109 #  endif
110 #endif
111   
112   fd = open (filename, O_RDONLY);
113
114   if (fd < 0 || (fstat (fd, &finfo) == -1))
115     {
116 file_error_and_exit:
117       if (((flags & FEVAL_ENOENTOK) == 0) || errno != ENOENT)
118         file_error (filename);
119
120       if (flags & FEVAL_LONGJMP)
121         {
122           last_command_exit_value = 1;
123           jump_to_top_level (EXITPROG);
124         }
125
126       return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE
127                                       : ((errno == ENOENT) ? 0 : -1));
128     }
129
130   errfunc = ((flags & FEVAL_BUILTIN) ? builtin_error : internal_error);
131
132   if (S_ISDIR (finfo.st_mode))
133     {
134       (*errfunc) (_("%s: is a directory"), filename);
135       return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1);
136     }
137   else if ((flags & FEVAL_REGFILE) && S_ISREG (finfo.st_mode) == 0)
138     {
139       (*errfunc) (_("%s: not a regular file"), filename);
140       return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1);
141     }
142
143   file_size = (size_t)finfo.st_size;
144   /* Check for overflow with large files. */
145   if (file_size != finfo.st_size || file_size + 1 < file_size)
146     {
147       (*errfunc) (_("%s: file is too large"), filename);
148       return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1);
149     }      
150
151 #if defined (__CYGWIN__) && defined (O_TEXT)
152   setmode (fd, O_TEXT);
153 #endif
154
155   if (S_ISREG (finfo.st_mode) && file_size <= SSIZE_MAX)
156     {
157       string = (char *)xmalloc (1 + file_size);
158       nr = read (fd, string, file_size);
159       if (nr >= 0)
160         string[nr] = '\0';
161     }
162   else
163     nr = zmapfd (fd, &string, 0);
164
165   return_val = errno;
166   close (fd);
167   errno = return_val;
168
169   if (nr < 0)           /* XXX was != file_size, not < 0 */
170     {
171       free (string);
172       goto file_error_and_exit;
173     }
174
175   if (nr == 0)
176     {
177       free (string);
178       return ((flags & FEVAL_BUILTIN) ? EXECUTION_SUCCESS : 1);
179     }
180       
181   if ((flags & FEVAL_CHECKBINARY) && 
182       check_binary_file (string, (nr > 80) ? 80 : nr))
183     {
184       free (string);
185       (*errfunc) (_("%s: cannot execute binary file"), filename);
186       return ((flags & FEVAL_BUILTIN) ? EX_BINARY_FILE : -1);
187     }
188
189   i = strlen (string);
190   if (i < nr)
191     {
192       for (nnull = i = 0; i < nr; i++)
193         if (string[i] == '\0')
194           {
195             memmove (string+i, string+i+1, nr - i);
196             nr--;
197             /* Even if the `check binary' flag is not set, we want to avoid
198                sourcing files with more than 256 null characters -- that
199                probably indicates a binary file. */
200             if ((flags & FEVAL_BUILTIN) && ++nnull > 256)
201               {
202                 free (string);
203                 (*errfunc) (_("%s: cannot execute binary file"), filename);
204                 return ((flags & FEVAL_BUILTIN) ? EX_BINARY_FILE : -1);
205               }
206           }
207     }
208
209   if (flags & FEVAL_UNWINDPROT)
210     {
211       begin_unwind_frame ("_evalfile");
212
213       unwind_protect_int (return_catch_flag);
214       unwind_protect_jmp_buf (return_catch);
215       if (flags & FEVAL_NONINT)
216         unwind_protect_int (interactive);
217       unwind_protect_int (sourcelevel);
218     }
219   else
220     {
221       COPY_PROCENV (return_catch, old_return_catch);
222       if (flags & FEVAL_NONINT)
223         old_interactive = interactive;
224     }
225
226   if (flags & FEVAL_NONINT)
227     interactive = 0;
228
229   return_catch_flag++;
230   sourcelevel++;
231
232 #if defined (ARRAY_VARS)
233   array_push (bash_source_a, (char *)filename);
234   t = itos (executing_line_number ());
235   array_push (bash_lineno_a, t);
236   free (t);
237   array_push (funcname_a, "source");    /* not exactly right */
238 #  if defined (DEBUGGER)
239   /* Have to figure out a better way to do this when `source' is supplied
240      arguments */
241   if ((flags & FEVAL_NOPUSHARGS) == 0)
242     {
243       array_push (bash_argv_a, (char *)filename);
244       tt[0] = '1'; tt[1] = '\0';
245       array_push (bash_argc_a, tt);
246     }
247 #  endif
248 #endif
249
250   /* set the flags to be passed to parse_and_execute */
251   pflags = SEVAL_RESETLINE;
252   pflags |= (flags & FEVAL_HISTORY) ? 0 : SEVAL_NOHIST;
253
254   if (flags & FEVAL_BUILTIN)
255     result = EXECUTION_SUCCESS;
256
257   return_val = setjmp (return_catch);
258
259   /* If `return' was seen outside of a function, but in the script, then
260      force parse_and_execute () to clean up. */
261   if (return_val)
262     {
263       parse_and_execute_cleanup ();
264       result = return_catch_value;
265     }
266   else
267     result = parse_and_execute (string, filename, pflags);
268
269   if (flags & FEVAL_UNWINDPROT)
270     run_unwind_frame ("_evalfile");
271   else
272     {
273       if (flags & FEVAL_NONINT)
274         interactive = old_interactive;
275       return_catch_flag--;
276       sourcelevel--;
277       COPY_PROCENV (old_return_catch, return_catch);
278     }
279
280 #if defined (ARRAY_VARS)
281   /* These two variables cannot be unset, and cannot be affected by the
282      sourced file. */
283   array_pop (bash_source_a);
284   array_pop (bash_lineno_a);
285
286   /* FUNCNAME can be unset, and so can potentially be changed by the
287      sourced file. */
288   GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a);
289   if (nfv == funcname_v)
290     array_pop (funcname_a);
291 #  if defined (DEBUGGER)
292   if ((flags & FEVAL_NOPUSHARGS) == 0)
293     {
294       array_pop (bash_argc_a);
295       array_pop (bash_argv_a);
296     }
297 #  endif
298 #endif
299
300   return ((flags & FEVAL_BUILTIN) ? result : 1);
301 }
302
303 int
304 maybe_execute_file (fname, force_noninteractive)
305      const char *fname;
306      int force_noninteractive;
307 {
308   char *filename;
309   int result, flags;
310
311   filename = bash_tilde_expand (fname, 0);
312   flags = FEVAL_ENOENTOK;
313   if (force_noninteractive)
314     flags |= FEVAL_NONINT;
315   result = _evalfile (filename, flags);
316   free (filename);
317   return result;
318 }
319
320 #if defined (HISTORY)
321 int
322 fc_execute_file (filename)
323      const char *filename;
324 {
325   int flags;
326
327   /* We want these commands to show up in the history list if
328      remember_on_history is set. */
329   flags = FEVAL_ENOENTOK|FEVAL_HISTORY|FEVAL_REGFILE;
330   return (_evalfile (filename, flags));
331 }
332 #endif /* HISTORY */
333
334 int
335 source_file (filename, sflags)
336      const char *filename;
337      int sflags;
338 {
339   int flags, rval;
340
341   flags = FEVAL_BUILTIN|FEVAL_UNWINDPROT|FEVAL_NONINT;
342   if (sflags)
343     flags |= FEVAL_NOPUSHARGS;
344   /* POSIX shells exit if non-interactive and file error. */
345   if (posixly_correct && !interactive_shell)
346     flags |= FEVAL_LONGJMP;
347   rval = _evalfile (filename, flags);
348
349   run_return_trap ();
350   return rval;
351 }