1 /* flags.c -- Everything about flags except the `set' command. That
4 /* Copyright (C) 1987-2015 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
23 #if defined (HAVE_UNISTD_H)
28 #include "execute_cmd.h"
31 #if defined (BANG_HISTORY)
32 # include "bashhist.h"
35 #if defined (JOB_CONTROL)
36 extern int set_job_control
__P((int));
39 /* **************************************************************** */
41 /* The Standard sh Flags. */
43 /* **************************************************************** */
45 /* Non-zero means automatically mark variables which are modified or created
46 as auto export variables. */
47 int mark_modified_vars
= 0;
49 /* Non-zero causes asynchronous job notification. Otherwise, job state
50 notification only takes place just before a primary prompt is printed. */
51 int asynchronous_notification
= 0;
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. */
57 int exit_immediately_on_error
= 0;
59 /* Non-zero means disable filename globbing. */
60 int disallow_filename_globbing
= 0;
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
65 int place_keywords_in_env
= 0;
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
70 int read_but_dont_execute
= 0;
72 /* Non-zero means end of file is after one command. */
73 int just_one_command
= 0;
75 /* Non-zero means don't overwrite existing files while doing redirections. */
78 /* Non-zero means trying to get the value of $i where $i is undefined
79 causes an error, instead of a null substitution. */
80 int unbound_vars_is_error
= 0;
82 /* Non-zero means type out input lines after you read them. */
83 int echo_input_at_read
= 0;
86 /* Non-zero means type out the command definition after reading, but
88 int echo_command_at_execute
= 0;
90 /* Non-zero means turn on the job control features. */
93 /* Non-zero means this shell is interactive, even if running under a
95 int forced_interactive
= 0;
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
102 int no_symbolic_links
= 0;
104 /* **************************************************************** */
106 /* Non-Standard Flags Follow Here. */
108 /* **************************************************************** */
111 /* Non-zero means do lexical scoping in the body of a FOR command. */
112 int lexical_scoping
= 0;
115 /* Non-zero means no such thing as invisible variables. */
116 int no_invisible_vars
= 0;
118 /* Non-zero means look up and remember command names in a hash table, */
119 int hashing_enabled
= 1;
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. */
124 int history_expansion
= HISTEXPAND_DEFAULT
;
125 int histexp_flag
= 0;
126 #endif /* BANG_HISTORY */
128 /* Non-zero means that we allow comments to appear in interactive commands. */
129 int interactive_comments
= 1;
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. */
136 int restricted
= 0; /* currently restricted */
137 int restricted_shell
= 0; /* shell was started in restricted mode. */
138 #endif /* RESTRICTED_SHELL */
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. */
144 int privileged_mode
= 0;
146 #if defined (BRACE_EXPANSION)
147 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
148 int brace_expansion
= 1;
151 /* Non-zero means that shell functions inherit the DEBUG trap. */
152 int function_trace_mode
= 0;
154 /* Non-zero means that shell functions inherit the ERR trap. */
155 int error_trace_mode
= 0;
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. */
160 int pipefail_opt
= 0;
162 /* **************************************************************** */
164 /* The Flags ALIST. */
166 /* **************************************************************** */
168 const 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
},
192 /* New flags that control non-standard things. */
194 { 'l', &lexical_scoping
},
196 #if defined (BRACE_EXPANSION)
197 { 'B', &brace_expansion
},
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
},
210 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
212 char optflags
[NUM_SHELL_FLAGS
+4] = { '+' };
219 for (i
= 0; shell_flags
[i
].name
; i
++)
221 if (shell_flags
[i
].name
== name
)
222 return (shell_flags
[i
].value
);
224 return (FLAG_UNKNOWN
);
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. */
231 change_flag (flag
, on_or_off
)
235 int *value
, old_value
;
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
)
241 #endif /* RESTRICTED_SHELL */
243 value
= find_flag (flag
);
245 if ((value
== (int *)FLAG_UNKNOWN
) || (on_or_off
!= FLAG_ON
&& on_or_off
!= FLAG_OFF
))
249 *value
= (on_or_off
== FLAG_ON
) ? 1 : 0;
251 /* Special cases for a few flags. */
254 #if defined (BANG_HISTORY)
256 history_expansion
= histexp_flag
;
257 if (on_or_off
== FLAG_ON
)
258 bash_initialize_history ();
262 #if defined (JOB_CONTROL)
264 set_job_control (on_or_off
== FLAG_ON
);
266 #endif /* JOB_CONTROL */
269 if (builtin_ignoring_errexit
== 0)
270 exit_immediately_on_error
= errexit_flag
;
274 if (interactive_shell
)
275 read_but_dont_execute
= 0;
279 if (on_or_off
== FLAG_OFF
)
280 disable_priv_mode ();
283 #if defined (RESTRICTED_SHELL)
285 if (on_or_off
== FLAG_ON
&& shell_initialized
)
286 maybe_make_restricted (shell_name
);
291 echo_input_at_read
= verbose_flag
;
298 /* Return a string which is the names of all the currently
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
++)
308 if (*(shell_flags
[i
].value
))
309 temp
[string_index
++] = shell_flags
[i
].name
;
311 if (want_pending_command
)
312 temp
[string_index
++] = 'c';
314 temp
[string_index
++] = 's';
316 temp
[string_index
] = '\0';
326 temp
= (char *)xmalloc (1 + NUM_SHELL_FLAGS
);
327 for (i
= 0; shell_flags
[i
].name
; i
++)
328 temp
[i
] = *(shell_flags
[i
].value
);
334 set_current_flags (bitmap
)
341 for (i
= 0; shell_flags
[i
].name
; i
++)
342 *(shell_flags
[i
].value
) = bitmap
[i
];
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;
355 error_trace_mode
= function_trace_mode
= 0;
357 exit_immediately_on_error
= errexit_flag
= 0;
358 echo_input_at_read
= verbose_flag
= 0;
360 hashing_enabled
= interactive_comments
= 1;
362 #if defined (JOB_CONTROL)
363 asynchronous_notification
= 0;
366 #if defined (BANG_HISTORY)
367 # if defined (STRICT_POSIX)
368 history_expansion
= 0;
370 history_expansion
= 1;
371 # endif /* STRICT_POSIX */
374 #if defined (BRACE_EXPANSION)
378 #if defined (RESTRICTED_SHELL)
388 for (i
= 0; shell_flags
[i
].name
; i
++)
389 optflags
[i
+1] = shell_flags
[i
].name
;
392 optflags
[i
+1] = '\0';