OpenCoverage

eval.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/eval.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* eval.c -- reading and evaluating commands. */-
2-
3/* Copyright (C) 1996-2011 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 "bashansi.h"-
31#include <stdio.h>-
32-
33#include <signal.h>-
34-
35#include "bashintl.h"-
36-
37#include "shell.h"-
38#include "parser.h"-
39#include "flags.h"-
40#include "trap.h"-
41-
42#include "builtins/common.h"-
43-
44#include "input.h"-
45#include "execute_cmd.h"-
46-
47#if defined (HISTORY)-
48# include "bashhist.h"-
49#endif-
50-
51#if defined (HAVE_POSIX_SIGNALS)-
52extern sigset_t top_level_mask;-
53#endif-
54-
55static void send_pwd_to_eterm __P((void));-
56static sighandler alrm_catcher __P((int));-
57-
58/* Read and execute commands until EOF is reached. This assumes that-
59 the input source has already been initialized. */-
60int-
61reader_loop ()-
62{-
63 int our_indirection_level;-
64 COMMAND * volatile current_command;-
65-
66 USE_VAR(current_command);-
67-
68 current_command = (COMMAND *)NULL;-
69-
70 our_indirection_level = ++indirection_level;-
71-
72 while (EOF_Reached == 0)
EOF_Reached == 0Description
TRUEevaluated 176017 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 399 times by 1 test
Evaluated by:
  • Self test
399-176017
73 {-
74 int code;-
75-
76 code = setjmp_nosigs (top_level);-
77-
78#if defined (PROCESS_SUBSTITUTION)-
79 unlink_fifo_list ();-
80#endif /* PROCESS_SUBSTITUTION */-
81-
82 /* XXX - why do we set this every time through the loop? And why do-
83 it if SIGINT is trapped in an interactive shell? */-
84 if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0)
interactive_shellDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 176466 times by 1 test
Evaluated by:
  • Self test
signal_is_ignored ( 2 ) == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
signal_is_trapped ( 2 ) == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-176466
85 set_signal_handler (SIGINT, sigint_sighandler);
executed 2 times by 1 test: set_signal_handler ( 2 , sigint_sighandler);
Executed by:
  • Self test
2
86-
87 if (code != NOT_JUMPED)
code != 0Description
TRUEevaluated 451 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 176017 times by 1 test
Evaluated by:
  • Self test
451-176017
88 {-
89 indirection_level = our_indirection_level;-
90-
91 switch (code)-
92 {-
93 /* Some kind of throw to top_level has occurred. */-
94 case FORCE_EOF:
executed 2 times by 1 test: case 1:
Executed by:
  • Self test
2
95 case ERREXIT:
executed 9 times by 1 test: case 4:
Executed by:
  • Self test
9
96 case EXITPROG:
executed 52 times by 1 test: case 3:
Executed by:
  • Self test
52
97 current_command = (COMMAND *)NULL;-
98 if (exit_immediately_on_error)
exit_immediately_on_errorDescription
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 56 times by 1 test
Evaluated by:
  • Self test
7-56
99 variable_context = 0; /* not in a function */
executed 7 times by 1 test: variable_context = 0;
Executed by:
  • Self test
7
100 EOF_Reached = EOF;-
101 goto exec_done;
executed 63 times by 1 test: goto exec_done;
Executed by:
  • Self test
63
102-
103 case DISCARD:
executed 388 times by 1 test: case 2:
Executed by:
  • Self test
388
104 /* Make sure the exit status is reset to a non-zero value, but-
105 leave existing non-zero values (e.g., > 128 on signal)-
106 alone. */-
107 if (last_command_exit_value == 0)
last_command_exit_value == 0Description
TRUEevaluated 17 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
17-371
108 last_command_exit_value = EXECUTION_FAILURE;
executed 17 times by 1 test: last_command_exit_value = 1;
Executed by:
  • Self test
17
109 if (subshell_environment)
subshell_environmentDescription
TRUEnever evaluated
FALSEevaluated 388 times by 1 test
Evaluated by:
  • Self test
0-388
110 {-
111 current_command = (COMMAND *)NULL;-
112 EOF_Reached = EOF;-
113 goto exec_done;
never executed: goto exec_done;
0
114 }-
115 /* Obstack free command elements, etc. */-
116 if (current_command)
current_commandDescription
TRUEevaluated 371 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 17 times by 1 test
Evaluated by:
  • Self test
17-371
117 {-
118 dispose_command (current_command);-
119 current_command = (COMMAND *)NULL;-
120 }
executed 371 times by 1 test: end of block
Executed by:
  • Self test
371
121#if defined (HAVE_POSIX_SIGNALS)-
122 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);-
123#endif-
124 break;
executed 388 times by 1 test: break;
Executed by:
  • Self test
388
125-
126 default:
never executed: default:
0
127 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);-
128 }
never executed: end of block
0
129 }-
130-
131 executing = 0;-
132 if (temporary_env)
temporary_envDescription
TRUEnever evaluated
FALSEevaluated 176405 times by 1 test
Evaluated by:
  • Self test
0-176405
133 dispose_used_env_vars ();
never executed: dispose_used_env_vars ();
0
134-
135#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)-
136 /* Attempt to reclaim memory allocated with alloca (). */-
137 (void) alloca (0);-
138#endif-
139-
140 if (read_command () == 0)
read_command () == 0Description
TRUEevaluated 176381 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
6-176381
141 {-
142 if (interactive_shell == 0 && read_but_dont_execute)
interactive_shell == 0Description
TRUEevaluated 176379 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
read_but_dont_executeDescription
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 176372 times by 1 test
Evaluated by:
  • Self test
2-176379
143 {-
144 last_command_exit_value = EXECUTION_SUCCESS;-
145 dispose_command (global_command);-
146 global_command = (COMMAND *)NULL;-
147 }
executed 7 times by 1 test: end of block
Executed by:
  • Self test
7
148 else if (current_command = global_command)
current_comman...global_commandDescription
TRUEevaluated 96408 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 79966 times by 1 test
Evaluated by:
  • Self test
79966-96408
149 {-
150 global_command = (COMMAND *)NULL;-
151-
152 /* If the shell is interactive, expand and display $PS0 after reading a-
153 command (possibly a list or pipeline) and before executing it. */-
154 if (interactive && ps0_prompt)
interactiveDescription
TRUEnever evaluated
FALSEevaluated 96408 times by 1 test
Evaluated by:
  • Self test
ps0_promptDescription
TRUEnever evaluated
FALSEnever evaluated
0-96408
155 {-
156 char *ps0_string;-
157-
158 ps0_string = decode_prompt_string (ps0_prompt);-
159 if (ps0_string && *ps0_string)
ps0_stringDescription
TRUEnever evaluated
FALSEnever evaluated
*ps0_stringDescription
TRUEnever evaluated
FALSEnever evaluated
0
160 {-
161 fprintf (stderr, "%s", ps0_string);-
162 fflush (stderr);-
163 }
never executed: end of block
0
164 free (ps0_string);-
165 }
never executed: end of block
0
166-
167 current_command_number++;-
168-
169 executing = 1;-
170 stdin_redir = 0;-
171-
172 execute_command (current_command);-
173-
174 exec_done:
code before this statement executed 91108 times by 1 test: exec_done:
Executed by:
  • Self test
91108
175 QUIT;
never executed: termsig_handler (terminating_signal);
never executed: throw_to_top_level ();
terminating_signalDescription
TRUEnever evaluated
FALSEevaluated 91171 times by 1 test
Evaluated by:
  • Self test
interrupt_stateDescription
TRUEnever evaluated
FALSEevaluated 91171 times by 1 test
Evaluated by:
  • Self test
0-91171
176-
177 if (current_command)
current_commandDescription
TRUEevaluated 91108 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 63 times by 1 test
Evaluated by:
  • Self test
63-91108
178 {-
179 dispose_command (current_command);-
180 current_command = (COMMAND *)NULL;-
181 }
executed 91108 times by 1 test: end of block
Executed by:
  • Self test
91108
182 }
executed 91171 times by 1 test: end of block
Executed by:
  • Self test
91171
183 }
executed 171144 times by 1 test: end of block
Executed by:
  • Self test
171144
184 else-
185 {-
186 /* Parse error, maybe discard rest of stream if not interactive. */-
187 if (interactive == 0)
interactive == 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-6
188 EOF_Reached = EOF;
executed 6 times by 1 test: EOF_Reached = (-1) ;
Executed by:
  • Self test
6
189 }
executed 6 times by 1 test: end of block
Executed by:
  • Self test
6
190 if (just_one_command)
just_one_commandDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 171149 times by 1 test
Evaluated by:
  • Self test
1-171149
191 EOF_Reached = EOF;
executed 1 time by 1 test: EOF_Reached = (-1) ;
Executed by:
  • Self test
1
192 }
executed 171150 times by 1 test: end of block
Executed by:
  • Self test
171150
193 indirection_level--;-
194 return (last_command_exit_value);
executed 399 times by 1 test: return (last_command_exit_value);
Executed by:
  • Self test
399
195}-
196-
197/* Pretty print shell scripts */-
198int-
199pretty_print_loop ()-
200{-
201 COMMAND *current_command;-
202 char *command_to_print;-
203 int code;-
204 int global_posix_mode, last_was_newline;-
205-
206 global_posix_mode = posixly_correct;-
207 last_was_newline = 0;-
208 while (EOF_Reached == 0)
EOF_Reached == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
209 {-
210 code = setjmp_nosigs (top_level);-
211 if (code)
codeDescription
TRUEnever evaluated
FALSEnever evaluated
0
212 return (EXECUTION_FAILURE);
never executed: return (1);
0
213 if (read_command() == 0)
read_command() == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
214 {-
215 current_command = global_command;-
216 global_command = 0;-
217 posixly_correct = 1; /* print posix-conformant */-
218 if (current_command && (command_to_print = make_command_string (current_command)))
current_commandDescription
TRUEnever evaluated
FALSEnever evaluated
(command_to_pr...rent_command))Description
TRUEnever evaluated
FALSEnever evaluated
0
219 {-
220 printf ("%s\n", command_to_print); /* for now */-
221 last_was_newline = 0;-
222 }
never executed: end of block
0
223 else if (last_was_newline == 0)
last_was_newline == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
224 {-
225 printf ("\n");-
226 last_was_newline = 1;-
227 }
never executed: end of block
0
228 posixly_correct = global_posix_mode;-
229 dispose_command (current_command);-
230 }
never executed: end of block
0
231 else-
232 return (EXECUTION_FAILURE);
never executed: return (1);
0
233 }-
234 -
235 return (EXECUTION_SUCCESS);
never executed: return (0);
0
236}-
237-
238static sighandler-
239alrm_catcher(i)-
240 int i;-
241{-
242 printf (_("\007timed out waiting for input: auto-logout\n"));-
243 fflush (stdout);-
244 bash_logout (); /* run ~/.bash_logout if this is a login shell */-
245 jump_to_top_level (EXITPROG);-
246 SIGRETURN (0);
never executed: return;
0
247}-
248-
249/* Send an escape sequence to emacs term mode to tell it the-
250 current working directory. */-
251static void-
252send_pwd_to_eterm ()-
253{-
254 char *pwd, *f;-
255-
256 f = 0;-
257 pwd = get_string_value ("PWD");-
258 if (pwd == 0)
pwd == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
259 f = pwd = get_working_directory ("eterm");
never executed: f = pwd = get_working_directory ("eterm");
0
260 fprintf (stderr, "\032/%s\n", pwd);-
261 free (f);-
262}
never executed: end of block
0
263-
264static void-
265execute_prompt_command ()-
266{-
267 char *command_to_execute;-
268-
269 command_to_execute = get_string_value ("PROMPT_COMMAND");-
270 if (command_to_execute)
command_to_executeDescription
TRUEnever evaluated
FALSEnever evaluated
0
271 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
never executed: execute_variable_command (command_to_execute, "PROMPT_COMMAND");
0
272}
never executed: end of block
0
273/* Call the YACC-generated parser and return the status of the parse.-
274 Input is read from the current input stream (bash_input). yyparse-
275 leaves the parsed command in the global variable GLOBAL_COMMAND.-
276 This is where PROMPT_COMMAND is executed. */-
277int-
278parse_command ()-
279{-
280 int r;-
281-
282 need_here_doc = 0;-
283 run_pending_traps ();-
284-
285 /* Allow the execution of a random command just before the printing-
286 of each primary prompt. If the shell variable PROMPT_COMMAND-
287 is set then the value of it is the command to execute. */-
288 /* The tests are a combination of SHOULD_PROMPT() and prompt_again() -
289 from parse.y, which are the conditions under which the prompt is-
290 actually printed. */-
291 if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0)
interactiveDescription
TRUEnever evaluated
FALSEevaluated 907954 times by 1 test
Evaluated by:
  • Self test
bash_input.type != st_stringDescription
TRUEnever evaluated
FALSEnever evaluated
parser_expanding_alias() == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-907954
292 {-
293 execute_prompt_command ();-
294-
295 if (running_under_emacs == 2)
running_under_emacs == 2Description
TRUEnever evaluated
FALSEnever evaluated
0
296 send_pwd_to_eterm (); /* Yuck */
never executed: send_pwd_to_eterm ();
0
297 }
never executed: end of block
0
298-
299 current_command_line_count = 0;-
300 r = yyparse ();-
301-
302 if (need_here_doc)
need_here_docDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 907934 times by 1 test
Evaluated by:
  • Self test
2-907934
303 gather_here_documents ();
executed 2 times by 1 test: gather_here_documents ();
Executed by:
  • Self test
2
304-
305 return (r);
executed 907936 times by 1 test: return (r);
Executed by:
  • Self test
907936
306}-
307-
308/* Read and parse a command, returning the status of the parse. The command-
309 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.-
310 This is where the shell timeout code is executed. */-
311int-
312read_command ()-
313{-
314 SHELL_VAR *tmout_var;-
315 int tmout_len, result;-
316 SigHandler *old_alrm;-
317-
318 set_current_prompt_level (1);-
319 global_command = (COMMAND *)NULL;-
320-
321 /* Only do timeouts if interactive. */-
322 tmout_var = (SHELL_VAR *)NULL;-
323 tmout_len = 0;-
324 old_alrm = (SigHandler *)NULL;-
325-
326 if (interactive)
interactiveDescription
TRUEnever evaluated
FALSEevaluated 176405 times by 1 test
Evaluated by:
  • Self test
0-176405
327 {-
328 tmout_var = find_variable ("TMOUT");-
329-
330 if (tmout_var && var_isset (tmout_var))
tmout_varDescription
TRUEnever evaluated
FALSEnever evaluated
((tmout_var)->value != 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
331 {-
332 tmout_len = atoi (value_cell (tmout_var));-
333 if (tmout_len > 0)
tmout_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
334 {-
335 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);-
336 alarm (tmout_len);-
337 }
never executed: end of block
0
338 }
never executed: end of block
0
339 }
never executed: end of block
0
340-
341 QUIT;
never executed: termsig_handler (terminating_signal);
never executed: throw_to_top_level ();
terminating_signalDescription
TRUEnever evaluated
FALSEevaluated 176405 times by 1 test
Evaluated by:
  • Self test
interrupt_stateDescription
TRUEnever evaluated
FALSEevaluated 176405 times by 1 test
Evaluated by:
  • Self test
0-176405
342-
343 current_command_line_count = 0;-
344 result = parse_command ();-
345-
346 if (interactive && tmout_var && (tmout_len > 0))
interactiveDescription
TRUEnever evaluated
FALSEevaluated 176387 times by 1 test
Evaluated by:
  • Self test
tmout_varDescription
TRUEnever evaluated
FALSEnever evaluated
(tmout_len > 0)Description
TRUEnever evaluated
FALSEnever evaluated
0-176387
347 {-
348 alarm(0);-
349 set_signal_handler (SIGALRM, old_alrm);-
350 }
never executed: end of block
0
351-
352 return (result);
executed 176387 times by 1 test: return (result);
Executed by:
  • Self test
176387
353}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2