8f172de7c1cf2eed0e32970cc901e481b8e4d5c6
[platform/upstream/bash.git] / builtins / evalstring.c
1 /* evalstring.c - evaluate a string as one or more shell commands. 
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 #  ifdef _MINIX
25 #    include <sys/types.h>
26 #  endif
27 #  include <unistd.h>
28 #endif
29
30 #include <stdio.h>
31 #include <signal.h>
32
33 #include <errno.h>
34
35 #include "filecntl.h"
36 #include "../bashansi.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 "../redir.h"
45 #include "../trap.h"
46
47 #include <y.tab.h>
48
49 #if defined (HISTORY)
50 #  include "../bashhist.h"
51 #endif
52
53 #include "common.h"
54
55 #if !defined (errno)
56 extern int errno;
57 #endif
58
59 #define IS_BUILTIN(s)   (builtin_address_internal(s, 0) != (struct builtin *)NULL)
60
61 extern int indirection_level, subshell_environment;
62 extern int line_number;
63 extern int current_token, shell_eof_token;
64 extern int last_command_exit_value;
65 extern int running_trap;
66 extern int loop_level;
67 extern int executing_list;
68 extern int comsub_ignore_return;
69 extern int posixly_correct;
70
71 int parse_and_execute_level = 0;
72
73 static int cat_file __P((REDIRECT *));
74
75 #define PE_TAG "parse_and_execute top"
76 #define PS_TAG "parse_string top"
77
78 #if defined (HISTORY)
79 static void
80 set_history_remembering ()
81 {
82   remember_on_history = enable_history_list;
83 }
84 #endif
85
86 /* How to force parse_and_execute () to clean up after itself. */
87 void
88 parse_and_execute_cleanup ()
89 {
90   if (running_trap)
91     {
92       run_trap_cleanup (running_trap - 1);
93       unfreeze_jobs_list ();
94     }
95
96   if (have_unwind_protects ())
97      run_unwind_frame (PE_TAG);
98   else
99     parse_and_execute_level = 0;                        /* XXX */
100 }
101
102 static void
103 parse_prologue (string, flags, tag)
104      char *string;
105      int flags;
106      char *tag;
107 {
108   char *orig_string;
109   int x;
110
111   orig_string = string;
112   /* Unwind protect this invocation of parse_and_execute (). */
113   begin_unwind_frame (tag);
114   unwind_protect_int (parse_and_execute_level);
115   unwind_protect_jmp_buf (top_level);
116   unwind_protect_int (indirection_level);
117   unwind_protect_int (line_number);
118   unwind_protect_int (loop_level);
119   unwind_protect_int (executing_list);
120   unwind_protect_int (comsub_ignore_return);
121   if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
122     unwind_protect_int (interactive);
123
124 #if defined (HISTORY)
125   if (parse_and_execute_level == 0)
126     add_unwind_protect (set_history_remembering, (char *)NULL);
127   else
128     unwind_protect_int (remember_on_history);   /* can be used in scripts */
129 #  if defined (BANG_HISTORY)
130   if (interactive_shell)
131     unwind_protect_int (history_expansion_inhibited);
132 #  endif /* BANG_HISTORY */
133 #endif /* HISTORY */
134
135   if (interactive_shell)
136     {
137       x = get_current_prompt_level ();
138       add_unwind_protect (set_current_prompt_level, x);
139     }
140   
141   add_unwind_protect (pop_stream, (char *)NULL);
142   if (orig_string && ((flags & SEVAL_NOFREE) == 0))
143     add_unwind_protect (xfree, orig_string);
144   end_unwind_frame ();
145
146   if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
147     interactive = (flags & SEVAL_NONINT) ? 0 : 1;
148
149 #if defined (HISTORY)
150   if (flags & SEVAL_NOHIST)
151     bash_history_disable ();
152 #endif /* HISTORY */
153 }
154
155 /* Parse and execute the commands in STRING.  Returns whatever
156    execute_command () returns.  This frees STRING.  FLAGS is a
157    flags word; look in common.h for the possible values.  Actions
158    are:
159         (flags & SEVAL_NONINT) -> interactive = 0;
160         (flags & SEVAL_INTERACT) -> interactive = 1;
161         (flags & SEVAL_NOHIST) -> call bash_history_disable ()
162         (flags & SEVAL_NOFREE) -> don't free STRING when finished
163         (flags & SEVAL_RESETLINE) -> reset line_number to 1
164 */
165
166 int
167 parse_and_execute (string, from_file, flags)
168      char *string;
169      const char *from_file;
170      int flags;
171 {
172   int code, lreset;
173   volatile int should_jump_to_top_level, last_result;
174   COMMAND *volatile command;
175
176   parse_prologue (string, flags, PE_TAG);
177
178   parse_and_execute_level++;
179
180   lreset = flags & SEVAL_RESETLINE;
181
182   /* Reset the line number if the caller wants us to.  If we don't reset the
183      line number, we have to subtract one, because we will add one just
184      before executing the next command (resetting the line number sets it to
185      0; the first line number is 1). */
186   push_stream (lreset);
187   if (lreset == 0)
188     line_number--;
189     
190   indirection_level++;
191
192   code = should_jump_to_top_level = 0;
193   last_result = EXECUTION_SUCCESS;
194
195   with_input_from_string (string, from_file);
196   while (*(bash_input.location.string))
197     {
198       command = (COMMAND *)NULL;
199
200       if (interrupt_state)
201         {
202           last_result = EXECUTION_FAILURE;
203           break;
204         }
205
206       /* Provide a location for functions which `longjmp (top_level)' to
207          jump to.  This prevents errors in substitution from restarting
208          the reader loop directly, for example. */
209       code = setjmp (top_level);
210
211       if (code)
212         {
213           should_jump_to_top_level = 0;
214           switch (code)
215             {
216             case FORCE_EOF:
217             case ERREXIT:
218             case EXITPROG:
219               if (command)
220                 run_unwind_frame ("pe_dispose");
221               /* Remember to call longjmp (top_level) after the old
222                  value for it is restored. */
223               should_jump_to_top_level = 1;
224               goto out;
225
226             case DISCARD:
227               if (command)
228                 run_unwind_frame ("pe_dispose");
229               last_result = last_command_exit_value = EXECUTION_FAILURE; /* XXX */
230               if (subshell_environment)
231                 {
232                   should_jump_to_top_level = 1;
233                   goto out;
234                 }
235               else
236                 {
237 #if 0
238                   dispose_command (command);    /* pe_dispose does this */
239 #endif
240                   continue;
241                 }
242
243             default:
244               command_error ("parse_and_execute", CMDERR_BADJUMP, code, 0);
245               break;
246             }
247         }
248           
249       if (parse_command () == 0)
250         {
251           if ((flags & SEVAL_PARSEONLY) || (interactive_shell == 0 && read_but_dont_execute))
252             {
253               last_result = EXECUTION_SUCCESS;
254               dispose_command (global_command);
255               global_command = (COMMAND *)NULL;
256             }
257           else if (command = global_command)
258             {
259               struct fd_bitmap *bitmap;
260
261               bitmap = new_fd_bitmap (FD_BITMAP_SIZE);
262               begin_unwind_frame ("pe_dispose");
263               add_unwind_protect (dispose_fd_bitmap, bitmap);
264               add_unwind_protect (dispose_command, command);    /* XXX */
265
266               global_command = (COMMAND *)NULL;
267
268               if ((subshell_environment & SUBSHELL_COMSUB) && comsub_ignore_return)
269 {
270                 command->flags |= CMD_IGNORE_RETURN;
271 itrace("parse_and_execute: turned on CMD_IGNORE_RETURN from comsub_ignore_return");
272 }
273
274 #if defined (ONESHOT)
275               /*
276                * IF
277                *   we were invoked as `bash -c' (startup_state == 2) AND
278                *   parse_and_execute has not been called recursively AND
279                *   we're not running a trap AND
280                *   we have parsed the full command (string == '\0') AND
281                *   we're not going to run the exit trap AND
282                *   we have a simple command without redirections AND
283                *   the command is not being timed AND
284                *   the command's return status is not being inverted
285                * THEN
286                *   tell the execution code that we don't need to fork
287                */
288               if (startup_state == 2 && parse_and_execute_level == 1 &&
289                   running_trap == 0 &&
290                   *bash_input.location.string == '\0' &&
291                   command->type == cm_simple &&
292                   signal_is_trapped (EXIT_TRAP) == 0 &&
293                   command->redirects == 0 && command->value.Simple->redirects == 0 &&
294                   ((command->flags & CMD_TIME_PIPELINE) == 0) &&
295                   ((command->flags & CMD_INVERT_RETURN) == 0))
296                 {
297                   command->flags |= CMD_NO_FORK;
298                   command->value.Simple->flags |= CMD_NO_FORK;
299                 }
300 #endif /* ONESHOT */
301
302               /* See if this is a candidate for $( <file ). */
303               if (startup_state == 2 &&
304                   (subshell_environment & SUBSHELL_COMSUB) &&
305                   *bash_input.location.string == '\0' &&
306                   command->type == cm_simple && !command->redirects &&
307                   (command->flags & CMD_TIME_PIPELINE) == 0 &&
308                   command->value.Simple->words == 0 &&
309                   command->value.Simple->redirects &&
310                   command->value.Simple->redirects->next == 0 &&
311                   command->value.Simple->redirects->instruction == r_input_direction)
312                 {
313                   int r;
314                   r = cat_file (command->value.Simple->redirects);
315                   last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
316                 }
317               else
318                 last_result = execute_command_internal
319                                 (command, 0, NO_PIPE, NO_PIPE, bitmap);
320
321               dispose_command (command);
322               dispose_fd_bitmap (bitmap);
323               discard_unwind_frame ("pe_dispose");
324             }
325         }
326       else
327         {
328           last_result = EXECUTION_FAILURE;
329
330           /* Since we are shell compatible, syntax errors in a script
331              abort the execution of the script.  Right? */
332           break;
333         }
334     }
335
336  out:
337
338   run_unwind_frame (PE_TAG);
339
340   if (interrupt_state && parse_and_execute_level == 0)
341     {
342       /* An interrupt during non-interactive execution in an
343          interactive shell (e.g. via $PROMPT_COMMAND) should
344          not cause the shell to exit. */
345       interactive = interactive_shell;
346       throw_to_top_level ();
347     }
348
349   if (should_jump_to_top_level)
350     jump_to_top_level (code);
351
352   return (last_result);
353 }
354
355 /* Parse a command contained in STRING according to FLAGS and return the
356    number of characters consumed from the string.  If non-NULL, set *ENDP
357    to the position in the string where the parse ended.  Used to validate
358    command substitutions during parsing to obey Posix rules about finding
359    the end of the command and balancing parens. */
360 int
361 parse_string (string, from_file, flags, endp)
362      char *string;
363      const char *from_file;
364      int flags;
365      char **endp;
366 {
367   int code, nc;
368   volatile int should_jump_to_top_level;
369   COMMAND *volatile command, *oglobal;
370   char *ostring;
371
372   parse_prologue (string, flags, PS_TAG);
373
374   /* Reset the line number if the caller wants us to.  If we don't reset the
375      line number, we have to subtract one, because we will add one just
376      before executing the next command (resetting the line number sets it to
377      0; the first line number is 1). */
378   push_stream (0);
379     
380   code = should_jump_to_top_level = 0;
381   oglobal = global_command;
382   ostring = string;
383
384   with_input_from_string (string, from_file);
385   while (*(bash_input.location.string))
386     {
387       command = (COMMAND *)NULL;
388
389 #if 0
390       if (interrupt_state)
391         break;
392 #endif
393
394       /* Provide a location for functions which `longjmp (top_level)' to
395          jump to. */
396       code = setjmp (top_level);
397
398       if (code)
399         {
400 #if defined (DEBUG)
401 itrace("parse_string: longjmp executed: code = %d", code);
402 #endif
403           should_jump_to_top_level = 0;
404           switch (code)
405             {
406             case FORCE_EOF:
407             case ERREXIT:
408             case EXITPROG:
409             case DISCARD:               /* XXX */
410               if (command)
411                 dispose_command (command);
412               /* Remember to call longjmp (top_level) after the old
413                  value for it is restored. */
414               should_jump_to_top_level = 1;
415               goto out;
416
417             default:
418               command_error ("parse_string", CMDERR_BADJUMP, code, 0);
419               break;
420             }
421         }
422           
423       if (parse_command () == 0)
424         {
425           dispose_command (global_command);
426           global_command = (COMMAND *)NULL;
427         }
428       else
429         {
430           if ((flags & SEVAL_NOLONGJMP) == 0)
431             {
432               should_jump_to_top_level = 1;
433               code = DISCARD;
434             }
435           else
436             reset_parser ();    /* XXX - sets token_to_read */
437           break;
438         }
439
440       if (current_token == yacc_EOF || current_token == shell_eof_token)
441           break;
442     }
443
444  out:
445
446   global_command = oglobal;
447   nc = bash_input.location.string - ostring;
448   if (endp)
449     *endp = bash_input.location.string;
450
451   run_unwind_frame (PS_TAG);
452
453   if (should_jump_to_top_level)
454     jump_to_top_level (code);
455
456   return (nc);
457 }
458
459 /* Handle a $( < file ) command substitution.  This expands the filename,
460    returning errors as appropriate, then just cats the file to the standard
461    output. */
462 static int
463 cat_file (r)
464      REDIRECT *r;
465 {
466   char *fn;
467   int fd, rval;
468
469   if (r->instruction != r_input_direction)
470     return -1;
471
472   /* Get the filename. */
473   if (posixly_correct && !interactive_shell)
474     disallow_filename_globbing++;
475   fn = redirection_expand (r->redirectee.filename);
476   if (posixly_correct && !interactive_shell)
477     disallow_filename_globbing--;
478
479   if (fn == 0)
480     {
481       redirection_error (r, AMBIGUOUS_REDIRECT);
482       return -1;
483     }
484
485   fd = open(fn, O_RDONLY);
486   if (fd < 0)
487     {
488       file_error (fn);
489       free (fn);
490       return -1;
491     }
492
493   rval = zcatfd (fd, 1, fn);
494
495   free (fn);
496   close (fd);
497
498   return (rval);
499 }