OpenCoverage

flags.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/flags.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* flags.c -- Everything about flags except the `set' command. That-
2 is in builtins.c */-
3-
4/* Copyright (C) 1987-2015 Free Software Foundation, Inc.-
5-
6 This file is part of GNU Bash, the Bourne Again SHell.-
7-
8 Bash is free software: you can redistribute it and/or modify-
9 it under the terms of the GNU General Public License as published by-
10 the Free Software Foundation, either version 3 of the License, or-
11 (at your option) any later version.-
12-
13 Bash is distributed in the hope that it will be useful,-
14 but WITHOUT ANY WARRANTY; without even the implied warranty of-
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
16 GNU General Public License for more details.-
17-
18 You should have received a copy of the GNU General Public License-
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.-
20*/-
21-
22#include "config.h"-
23#if defined (HAVE_UNISTD_H)-
24# include <unistd.h>-
25#endif-
26-
27#include "shell.h"-
28#include "execute_cmd.h"-
29#include "flags.h"-
30-
31#if defined (BANG_HISTORY)-
32# include "bashhist.h"-
33#endif-
34-
35#if defined (JOB_CONTROL)-
36extern int set_job_control __P((int));-
37#endif-
38-
39/* **************************************************************** */-
40/* */-
41/* The Standard sh Flags. */-
42/* */-
43/* **************************************************************** */-
44-
45/* Non-zero means automatically mark variables which are modified or created-
46 as auto export variables. */-
47int mark_modified_vars = 0;-
48-
49/* Non-zero causes asynchronous job notification. Otherwise, job state-
50 notification only takes place just before a primary prompt is printed. */-
51int asynchronous_notification = 0;-
52-
53/* Non-zero means exit immediately if a command exits with a non-zero-
54 exit status. The first is what controls set -e; the second is what-
55 bash uses internally. */-
56int errexit_flag = 0;-
57int exit_immediately_on_error = 0;-
58-
59/* Non-zero means disable filename globbing. */-
60int disallow_filename_globbing = 0;-
61-
62/* Non-zero means that all keyword arguments are placed into the environment-
63 for a command, not just those that appear on the line before the command-
64 name. */-
65int place_keywords_in_env = 0;-
66-
67/* Non-zero means read commands, but don't execute them. This is useful-
68 for debugging shell scripts that should do something hairy and possibly-
69 destructive. */-
70int read_but_dont_execute = 0;-
71-
72/* Non-zero means end of file is after one command. */-
73int just_one_command = 0;-
74-
75/* Non-zero means don't overwrite existing files while doing redirections. */-
76int noclobber = 0;-
77-
78/* Non-zero means trying to get the value of $i where $i is undefined-
79 causes an error, instead of a null substitution. */-
80int unbound_vars_is_error = 0;-
81-
82/* Non-zero means type out input lines after you read them. */-
83int echo_input_at_read = 0;-
84int verbose_flag = 0;-
85-
86/* Non-zero means type out the command definition after reading, but-
87 before executing. */-
88int echo_command_at_execute = 0;-
89-
90/* Non-zero means turn on the job control features. */-
91int jobs_m_flag = 0;-
92-
93/* Non-zero means this shell is interactive, even if running under a-
94 pipe. */-
95int forced_interactive = 0;-
96-
97/* By default, follow the symbolic links as if they were real directories-
98 while hacking the `cd' command. This means that `cd ..' moves up in-
99 the string of symbolic links that make up the current directory, instead-
100 of the absolute directory. The shell variable `nolinks' also controls-
101 this flag. */-
102int no_symbolic_links = 0;-
103-
104/* **************************************************************** */-
105/* */-
106/* Non-Standard Flags Follow Here. */-
107/* */-
108/* **************************************************************** */-
109-
110#if 0-
111/* Non-zero means do lexical scoping in the body of a FOR command. */-
112int lexical_scoping = 0;-
113#endif-
114-
115/* Non-zero means no such thing as invisible variables. */-
116int no_invisible_vars = 0;-
117-
118/* Non-zero means look up and remember command names in a hash table, */-
119int hashing_enabled = 1;-
120-
121#if defined (BANG_HISTORY)-
122/* Non-zero means that we are doing history expansion. The default.-
123 This means !22 gets the 22nd line of history. */-
124int history_expansion = HISTEXPAND_DEFAULT;-
125int histexp_flag = 0;-
126#endif /* BANG_HISTORY */-
127-
128/* Non-zero means that we allow comments to appear in interactive commands. */-
129int interactive_comments = 1;-
130-
131#if defined (RESTRICTED_SHELL)-
132/* Non-zero means that this shell is `restricted'. A restricted shell-
133 disallows: changing directories, command or path names containing `/',-
134 unsetting or resetting the values of $PATH and $SHELL, and any type of-
135 output redirection. */-
136int restricted = 0; /* currently restricted */-
137int restricted_shell = 0; /* shell was started in restricted mode. */-
138#endif /* RESTRICTED_SHELL */-
139-
140/* Non-zero means that this shell is running in `privileged' mode. This-
141 is required if the shell is to run setuid. If the `-p' option is-
142 not supplied at startup, and the real and effective uids or gids-
143 differ, disable_priv_mode is called to relinquish setuid status. */-
144int privileged_mode = 0;-
145-
146#if defined (BRACE_EXPANSION)-
147/* Zero means to disable brace expansion: foo{a,b} -> fooa foob */-
148int brace_expansion = 1;-
149#endif-
150-
151/* Non-zero means that shell functions inherit the DEBUG trap. */-
152int function_trace_mode = 0;-
153-
154/* Non-zero means that shell functions inherit the ERR trap. */-
155int error_trace_mode = 0;-
156-
157/* Non-zero means that the rightmost non-zero exit status in a pipeline-
158 is the exit status of the entire pipeline. If each processes exits-
159 with a 0 status, the status of the pipeline is 0. */-
160int pipefail_opt = 0;-
161-
162/* **************************************************************** */-
163/* */-
164/* The Flags ALIST. */-
165/* */-
166/* **************************************************************** */-
167-
168const struct flags_alist shell_flags[] = {-
169 /* Standard sh flags. */-
170 { 'a', &mark_modified_vars },-
171#if defined (JOB_CONTROL)-
172 { 'b', &asynchronous_notification },-
173#endif /* JOB_CONTROL */-
174 { 'e', &errexit_flag },-
175 { 'f', &disallow_filename_globbing },-
176 { 'h', &hashing_enabled },-
177 { 'i', &forced_interactive },-
178 { 'k', &place_keywords_in_env },-
179#if defined (JOB_CONTROL)-
180 { 'm', &jobs_m_flag },-
181#endif /* JOB_CONTROL */-
182 { 'n', &read_but_dont_execute },-
183 { 'p', &privileged_mode },-
184#if defined (RESTRICTED_SHELL)-
185 { 'r', &restricted },-
186#endif /* RESTRICTED_SHELL */-
187 { 't', &just_one_command },-
188 { 'u', &unbound_vars_is_error },-
189 { 'v', &verbose_flag },-
190 { 'x', &echo_command_at_execute },-
191-
192 /* New flags that control non-standard things. */-
193#if 0-
194 { 'l', &lexical_scoping },-
195#endif-
196#if defined (BRACE_EXPANSION)-
197 { 'B', &brace_expansion },-
198#endif-
199 { 'C', &noclobber },-
200 { 'E', &error_trace_mode },-
201#if defined (BANG_HISTORY)-
202 { 'H', &histexp_flag },-
203#endif /* BANG_HISTORY */-
204 { 'I', &no_invisible_vars },-
205 { 'P', &no_symbolic_links },-
206 { 'T', &function_trace_mode },-
207 {0, (int *)NULL}-
208};-
209-
210#define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))-
211-
212char optflags[NUM_SHELL_FLAGS+4] = { '+' };-
213-
214int *-
215find_flag (name)-
216 int name;-
217{-
218 int i;-
219 for (i = 0; shell_flags[i].name; i++)
shell_flags[i].nameDescription
TRUEevaluated 2238050 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2238050
220 {-
221 if (shell_flags[i].name == name)
shell_flags[i].name == nameDescription
TRUEevaluated 199955 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2038095 times by 1 test
Evaluated by:
  • Self test
199955-2038095
222 return (shell_flags[i].value);
executed 199955 times by 1 test: return (shell_flags[i].value);
Executed by:
  • Self test
199955
223 }
executed 2038095 times by 1 test: end of block
Executed by:
  • Self test
2038095
224 return (FLAG_UNKNOWN);
never executed: return ((int *)0);
0
225}-
226-
227/* Change the state of a flag, and return it's original value, or return-
228 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either-
229 FLAG_ON or FLAG_OFF. */-
230int-
231change_flag (flag, on_or_off)-
232 int flag;-
233 int on_or_off;-
234{-
235 int *value, old_value;-
236-
237#if defined (RESTRICTED_SHELL)-
238 /* Don't allow "set +r" in a shell which is `restricted'. */-
239 if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
restrictedDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 13715 times by 1 test
Evaluated by:
  • Self test
flag == 'r'Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
on_or_off == '+'Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-13715
240 return (FLAG_ERROR);
executed 1 time by 1 test: return (-1);
Executed by:
  • Self test
1
241#endif /* RESTRICTED_SHELL */-
242-
243 value = find_flag (flag);-
244-
245 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
(value == (int *)(int *)0)Description
TRUEnever evaluated
FALSEevaluated 13715 times by 1 test
Evaluated by:
  • Self test
on_or_off != '-'Description
TRUEevaluated 12963 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 752 times by 1 test
Evaluated by:
  • Self test
on_or_off != '+'Description
TRUEnever evaluated
FALSEevaluated 12963 times by 1 test
Evaluated by:
  • Self test
0-13715
246 return (FLAG_ERROR);
never executed: return (-1);
0
247-
248 old_value = *value;-
249 *value = (on_or_off == FLAG_ON) ? 1 : 0;
(on_or_off == '-')Description
TRUEevaluated 752 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12963 times by 1 test
Evaluated by:
  • Self test
752-12963
250-
251 /* Special cases for a few flags. */-
252 switch (flag)-
253 {-
254#if defined (BANG_HISTORY)-
255 case 'H':
executed 30 times by 1 test: case 'H':
Executed by:
  • Self test
30
256 history_expansion = histexp_flag;-
257 if (on_or_off == FLAG_ON)
on_or_off == '-'Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-30
258 bash_initialize_history ();
executed 30 times by 1 test: bash_initialize_history ();
Executed by:
  • Self test
30
259 break;
executed 30 times by 1 test: break;
Executed by:
  • Self test
30
260#endif-
261-
262#if defined (JOB_CONTROL)-
263 case 'm':
executed 5462 times by 1 test: case 'm':
Executed by:
  • Self test
5462
264 set_job_control (on_or_off == FLAG_ON);-
265 break;
executed 5462 times by 1 test: break;
Executed by:
  • Self test
5462
266#endif /* JOB_CONTROL */-
267-
268 case 'e':
executed 3347 times by 1 test: case 'e':
Executed by:
  • Self test
3347
269 if (builtin_ignoring_errexit == 0)
builtin_ignoring_errexit == 0Description
TRUEevaluated 3346 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-3346
270 exit_immediately_on_error = errexit_flag;
executed 3346 times by 1 test: exit_immediately_on_error = errexit_flag;
Executed by:
  • Self test
3346
271 break;
executed 3347 times by 1 test: break;
Executed by:
  • Self test
3347
272-
273 case 'n':
executed 2 times by 1 test: case 'n':
Executed by:
  • Self test
2
274 if (interactive_shell)
interactive_shellDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
0-2
275 read_but_dont_execute = 0;
never executed: read_but_dont_execute = 0;
0
276 break;
executed 2 times by 1 test: break;
Executed by:
  • Self test
2
277-
278 case 'p':
executed 11 times by 1 test: case 'p':
Executed by:
  • Self test
11
279 if (on_or_off == FLAG_OFF)
on_or_off == '+'Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-10
280 disable_priv_mode ();
executed 10 times by 1 test: disable_priv_mode ();
Executed by:
  • Self test
10
281 break;
executed 11 times by 1 test: break;
Executed by:
  • Self test
11
282-
283#if defined (RESTRICTED_SHELL)-
284 case 'r':
executed 1 time by 1 test: case 'r':
Executed by:
  • Self test
1
285 if (on_or_off == FLAG_ON && shell_initialized)
on_or_off == '-'Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
shell_initializedDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1
286 maybe_make_restricted (shell_name);
executed 1 time by 1 test: maybe_make_restricted (shell_name);
Executed by:
  • Self test
1
287 break;
executed 1 time by 1 test: break;
Executed by:
  • Self test
1
288#endif-
289-
290 case 'v':
executed 4340 times by 1 test: case 'v':
Executed by:
  • Self test
4340
291 echo_input_at_read = verbose_flag;-
292 break;
executed 4340 times by 1 test: break;
Executed by:
  • Self test
4340
293 }-
294-
295 return (old_value);
executed 13715 times by 1 test: return (old_value);
Executed by:
  • Self test
13715
296}-
297-
298/* Return a string which is the names of all the currently-
299 set shell flags. */-
300char *-
301which_set_flags ()-
302{-
303 char *temp;-
304 int i, string_index;-
305-
306 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);-
307 for (i = string_index = 0; shell_flags[i].name; i++)
shell_flags[i].nameDescription
TRUEevaluated 1672 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 76 times by 1 test
Evaluated by:
  • Self test
76-1672
308 if (*(shell_flags[i].value))
*(shell_flags[i].value)Description
TRUEevaluated 161 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1511 times by 1 test
Evaluated by:
  • Self test
161-1511
309 temp[string_index++] = shell_flags[i].name;
executed 161 times by 1 test: temp[string_index++] = shell_flags[i].name;
Executed by:
  • Self test
161
310-
311 if (want_pending_command)
want_pending_commandDescription
TRUEnever evaluated
FALSEevaluated 76 times by 1 test
Evaluated by:
  • Self test
0-76
312 temp[string_index++] = 'c';
never executed: temp[string_index++] = 'c';
0
313 if (read_from_stdin)
read_from_stdinDescription
TRUEnever evaluated
FALSEevaluated 76 times by 1 test
Evaluated by:
  • Self test
0-76
314 temp[string_index++] = 's';
never executed: temp[string_index++] = 's';
0
315-
316 temp[string_index] = '\0';-
317 return (temp);
executed 76 times by 1 test: return (temp);
Executed by:
  • Self test
76
318}-
319-
320char *-
321get_current_flags ()-
322{-
323 char *temp;-
324 int i;-
325-
326 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS);-
327 for (i = 0; shell_flags[i].name; i++)
shell_flags[i].nameDescription
TRUEnever evaluated
FALSEnever evaluated
0
328 temp[i] = *(shell_flags[i].value);
never executed: temp[i] = *(shell_flags[i].value);
0
329 temp[i] = '\0';-
330 return (temp);
never executed: return (temp);
0
331}-
332-
333void-
334set_current_flags (bitmap)-
335 const char *bitmap;-
336{-
337 int i;-
338-
339 if (bitmap == 0)
bitmap == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
340 return;
never executed: return;
0
341 for (i = 0; shell_flags[i].name; i++)
shell_flags[i].nameDescription
TRUEnever evaluated
FALSEnever evaluated
0
342 *(shell_flags[i].value) = bitmap[i];
never executed: *(shell_flags[i].value) = bitmap[i];
0
343}
never executed: end of block
0
344-
345void-
346reset_shell_flags ()-
347{-
348 mark_modified_vars = disallow_filename_globbing = 0;-
349 place_keywords_in_env = read_but_dont_execute = just_one_command = 0;-
350 noclobber = unbound_vars_is_error = 0;-
351 echo_command_at_execute = jobs_m_flag = forced_interactive = 0;-
352 no_symbolic_links = no_invisible_vars = 0;-
353 privileged_mode = pipefail_opt = 0;-
354-
355 error_trace_mode = function_trace_mode = 0;-
356-
357 exit_immediately_on_error = errexit_flag = 0;-
358 echo_input_at_read = verbose_flag = 0;-
359-
360 hashing_enabled = interactive_comments = 1;-
361-
362#if defined (JOB_CONTROL)-
363 asynchronous_notification = 0;-
364#endif-
365-
366#if defined (BANG_HISTORY)-
367# if defined (STRICT_POSIX)-
368 history_expansion = 0;-
369# else-
370 history_expansion = 1;-
371# endif /* STRICT_POSIX */-
372#endif-
373-
374#if defined (BRACE_EXPANSION)-
375 brace_expansion = 1;-
376#endif-
377-
378#if defined (RESTRICTED_SHELL)-
379 restricted = 0;-
380#endif-
381}
executed 15 times by 1 test: end of block
Executed by:
  • Self test
15
382-
383void-
384initialize_flags ()-
385{-
386 register int i;-
387-
388 for (i = 0; shell_flags[i].name; i++)
shell_flags[i].nameDescription
TRUEevaluated 119834 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5447 times by 1 test
Evaluated by:
  • Self test
5447-119834
389 optflags[i+1] = shell_flags[i].name;
executed 119834 times by 1 test: optflags[i+1] = shell_flags[i].name;
Executed by:
  • Self test
119834
390 optflags[++i] = 'o';-
391 optflags[++i] = ';';-
392 optflags[i+1] = '\0';-
393}
executed 5447 times by 1 test: end of block
Executed by:
  • Self test
5447
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2