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