Imported from ../bash-3.0.tar.gz.
[platform/upstream/bash.git] / builtins / evalstring.c
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2
3    This file is part of GNU Bash, the Bourne Again SHell.
4
5    Bash is free software; you can redistribute it and/or modify it under
6    the terms of the GNU General Public License as published by the Free
7    Software Foundation; either version 2, or (at your option) any later
8    version.
9
10    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
11    WARRANTY; without even the implied warranty of MERCHANTABILITY or
12    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13    for more details.
14    
15    You should have received a copy of the GNU General Public License along
16    with Bash; see the file COPYING.  If not, write to the Free Software
17    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
18
19 #include <config.h>
20
21 #if defined (HAVE_UNISTD_H)
22 #  ifdef _MINIX
23 #    include <sys/types.h>
24 #  endif
25 #  include <unistd.h>
26 #endif
27
28 #include <stdio.h>
29 #include <signal.h>
30
31 #include <errno.h>
32
33 #include "filecntl.h"
34 #include "../bashansi.h"
35
36 #include "../shell.h"
37 #include "../jobs.h"
38 #include "../builtins.h"
39 #include "../flags.h"
40 #include "../input.h"
41 #include "../execute_cmd.h"
42 #include "../redir.h"
43 #include "../trap.h"
44
45 #if defined (HISTORY)
46 #  include "../bashhist.h"
47 #endif
48
49 #include "common.h"
50
51 #if !defined (errno)
52 extern int errno;
53 #endif
54
55 #define IS_BUILTIN(s)   (builtin_address_internal(s, 0) != (struct builtin *)NULL)
56
57 extern int indirection_level, startup_state, subshell_environment;
58 extern int line_number;
59 extern int last_command_exit_value;
60 extern int running_trap;
61 extern int posixly_correct;
62
63 int parse_and_execute_level = 0;
64
65 static int cat_file __P((REDIRECT *));
66
67 /* How to force parse_and_execute () to clean up after itself. */
68 void
69 parse_and_execute_cleanup ()
70 {
71   if (running_trap)
72     {
73       run_trap_cleanup (running_trap - 1);
74       unfreeze_jobs_list ();
75     }
76   run_unwind_frame ("parse_and_execute_top");
77 }
78
79 /* Parse and execute the commands in STRING.  Returns whatever
80    execute_command () returns.  This frees STRING.  FLAGS is a
81    flags word; look in common.h for the possible values.  Actions
82    are:
83         (flags & SEVAL_NONINT) -> interactive = 0;
84         (flags & SEVAL_INTERACT) -> interactive = 1;
85         (flags & SEVAL_NOHIST) -> call bash_history_disable ()
86         (flags & SEVAL_NOFREE) -> don't free STRING when finished
87         (flags & SEVAL_RESETLINE) -> reset line_number to 1
88 */
89
90 int
91 parse_and_execute (string, from_file, flags)
92      char *string;
93      const char *from_file;
94      int flags;
95 {
96   int code, x, lreset;
97   volatile int should_jump_to_top_level, last_result;
98   char *orig_string;
99   COMMAND *volatile command;
100
101   orig_string = string;
102   /* Unwind protect this invocation of parse_and_execute (). */
103   begin_unwind_frame ("parse_and_execute_top");
104   unwind_protect_int (parse_and_execute_level);
105   unwind_protect_jmp_buf (top_level);
106   unwind_protect_int (indirection_level);
107   unwind_protect_int (line_number);
108   if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
109     unwind_protect_int (interactive);
110
111   lreset = flags & SEVAL_RESETLINE;
112
113 #if defined (HISTORY)
114   unwind_protect_int (remember_on_history);     /* can be used in scripts */
115 #  if defined (BANG_HISTORY)
116   if (interactive_shell)
117     {
118       unwind_protect_int (history_expansion_inhibited);
119     }
120 #  endif /* BANG_HISTORY */
121 #endif /* HISTORY */
122
123   if (interactive_shell)
124     {
125       x = get_current_prompt_level ();
126       add_unwind_protect (set_current_prompt_level, x);
127     }
128   
129   add_unwind_protect (pop_stream, (char *)NULL);
130   if (orig_string && ((flags & SEVAL_NOFREE) == 0))
131     add_unwind_protect (xfree, orig_string);
132   end_unwind_frame ();
133
134   parse_and_execute_level++;
135
136   /* Reset the line number if the caller wants us to.  If we don't reset the
137      line number, we have to subtract one, because we will add one just
138      before executing the next command (resetting the line number sets it to
139      0; the first line number is 1). */
140   push_stream (lreset);
141   if (lreset == 0)
142     line_number--;
143     
144   indirection_level++;
145   if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
146     interactive = (flags & SEVAL_NONINT) ? 0 : 1;
147
148 #if defined (HISTORY)
149   if (flags & SEVAL_NOHIST)
150     bash_history_disable ();
151 #endif /* HISTORY */
152
153   code = should_jump_to_top_level = 0;
154   last_result = EXECUTION_SUCCESS;
155
156   with_input_from_string (string, from_file);
157   while (*(bash_input.location.string))
158     {
159       command = (COMMAND *)NULL;
160
161       if (interrupt_state)
162         {
163           last_result = EXECUTION_FAILURE;
164           break;
165         }
166
167       /* Provide a location for functions which `longjmp (top_level)' to
168          jump to.  This prevents errors in substitution from restarting
169          the reader loop directly, for example. */
170       code = setjmp (top_level);
171
172       if (code)
173         {
174           should_jump_to_top_level = 0;
175           switch (code)
176             {
177             case FORCE_EOF:
178             case ERREXIT:
179             case EXITPROG:
180               if (command)
181                 run_unwind_frame ("pe_dispose");
182               /* Remember to call longjmp (top_level) after the old
183                  value for it is restored. */
184               should_jump_to_top_level = 1;
185               goto out;
186
187             case DISCARD:
188               if (command)
189                 run_unwind_frame ("pe_dispose");
190               last_result = last_command_exit_value = EXECUTION_FAILURE; /* XXX */
191               if (subshell_environment)
192                 {
193                   should_jump_to_top_level = 1;
194                   goto out;
195                 }
196               else
197                 {
198 #if 0
199                   dispose_command (command);    /* pe_dispose does this */
200 #endif
201                   continue;
202                 }
203
204             default:
205               command_error ("parse_and_execute", CMDERR_BADJUMP, code, 0);
206               break;
207             }
208         }
209           
210       if (parse_command () == 0)
211         {
212           if (interactive_shell == 0 && read_but_dont_execute)
213             {
214               last_result = EXECUTION_SUCCESS;
215               dispose_command (global_command);
216               global_command = (COMMAND *)NULL;
217             }
218           else if (command = global_command)
219             {
220               struct fd_bitmap *bitmap;
221
222               bitmap = new_fd_bitmap (FD_BITMAP_SIZE);
223               begin_unwind_frame ("pe_dispose");
224               add_unwind_protect (dispose_fd_bitmap, bitmap);
225               add_unwind_protect (dispose_command, command);    /* XXX */
226
227               global_command = (COMMAND *)NULL;
228
229 #if defined (ONESHOT)
230               /*
231                * IF
232                *   we were invoked as `bash -c' (startup_state == 2) AND
233                *   parse_and_execute has not been called recursively AND
234                *   we have parsed the full command (string == '\0') AND
235                *   we have a simple command without redirections AND
236                *   the command is not being timed
237                * THEN
238                *   tell the execution code that we don't need to fork
239                */
240               if (startup_state == 2 && parse_and_execute_level == 1 &&
241                   *bash_input.location.string == '\0' &&
242                   command->type == cm_simple &&
243                   !command->redirects && !command->value.Simple->redirects &&
244                   ((command->flags & CMD_TIME_PIPELINE) == 0))
245                 {
246                   command->flags |= CMD_NO_FORK;
247                   command->value.Simple->flags |= CMD_NO_FORK;
248                 }
249 #endif /* ONESHOT */
250
251               /* See if this is a candidate for $( <file ). */
252               if (startup_state == 2 &&
253                   (subshell_environment & SUBSHELL_COMSUB) &&
254                   *bash_input.location.string == '\0' &&
255                   command->type == cm_simple && !command->redirects &&
256                   (command->flags & CMD_TIME_PIPELINE) == 0 &&
257                   command->value.Simple->words == 0 &&
258                   command->value.Simple->redirects &&
259                   command->value.Simple->redirects->next == 0 &&
260                   command->value.Simple->redirects->instruction == r_input_direction)
261                 {
262                   int r;
263                   r = cat_file (command->value.Simple->redirects);
264                   last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
265                 }
266               else
267                 last_result = execute_command_internal
268                                 (command, 0, NO_PIPE, NO_PIPE, bitmap);
269
270               dispose_command (command);
271               dispose_fd_bitmap (bitmap);
272               discard_unwind_frame ("pe_dispose");
273             }
274         }
275       else
276         {
277           last_result = EXECUTION_FAILURE;
278
279           /* Since we are shell compatible, syntax errors in a script
280              abort the execution of the script.  Right? */
281           break;
282         }
283     }
284
285  out:
286
287   run_unwind_frame ("parse_and_execute_top");
288
289   if (interrupt_state && parse_and_execute_level == 0)
290     {
291       /* An interrupt during non-interactive execution in an
292          interactive shell (e.g. via $PROMPT_COMMAND) should
293          not cause the shell to exit. */
294       interactive = interactive_shell;
295       throw_to_top_level ();
296     }
297
298   if (should_jump_to_top_level)
299     jump_to_top_level (code);
300
301   return (last_result);
302 }
303
304 /* Handle a $( < file ) command substitution.  This expands the filename,
305    returning errors as appropriate, then just cats the file to the standard
306    output. */
307 static int
308 cat_file (r)
309      REDIRECT *r;
310 {
311   char lbuf[128], *fn;
312   int fd, rval;
313   ssize_t nr;
314
315   if (r->instruction != r_input_direction)
316     return -1;
317
318   /* Get the filename. */
319   if (posixly_correct && !interactive_shell)
320     disallow_filename_globbing++;
321   fn = redirection_expand (r->redirectee.filename);
322   if (posixly_correct && !interactive_shell)
323     disallow_filename_globbing--;
324
325   if (fn == 0)
326     {
327       redirection_error (r, AMBIGUOUS_REDIRECT);
328       return -1;
329     }
330
331   fd = open(fn, O_RDONLY);
332   if (fd < 0)
333     {
334       file_error (fn);
335       free (fn);
336       return -1;
337     }
338
339   rval = zcatfd (fd, 1, fn);
340
341   free (fn);
342   close (fd);
343
344   return (rval);
345 }