1 /* flags.c -- Everything about flags except the `set' command. That
4 /* Copyright (C) 1987-2009 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)
30 #if defined (BANG_HISTORY)
31 # include "bashhist.h"
34 #if defined (JOB_CONTROL)
35 extern int set_job_control __P((int));
38 #if defined (RESTRICTED_SHELL)
39 extern char *shell_name;
42 extern int shell_initialized;
43 extern int builtin_ignoring_errexit;
45 /* -c, -s invocation options -- not really flags, but they show up in $- */
46 extern int want_pending_command, read_from_stdin;
48 /* **************************************************************** */
50 /* The Standard sh Flags. */
52 /* **************************************************************** */
54 /* Non-zero means automatically mark variables which are modified or created
55 as auto export variables. */
56 int mark_modified_vars = 0;
58 /* Non-zero causes asynchronous job notification. Otherwise, job state
59 notification only takes place just before a primary prompt is printed. */
60 int asynchronous_notification = 0;
62 /* Non-zero means exit immediately if a command exits with a non-zero
63 exit status. The first is what controls set -e; the second is what
64 bash uses internally. */
66 int exit_immediately_on_error = 0;
68 /* Non-zero means disable filename globbing. */
69 int disallow_filename_globbing = 0;
71 /* Non-zero means that all keyword arguments are placed into the environment
72 for a command, not just those that appear on the line before the command
74 int place_keywords_in_env = 0;
76 /* Non-zero means read commands, but don't execute them. This is useful
77 for debugging shell scripts that should do something hairy and possibly
79 int read_but_dont_execute = 0;
81 /* Non-zero means end of file is after one command. */
82 int just_one_command = 0;
84 /* Non-zero means don't overwrite existing files while doing redirections. */
87 /* Non-zero means trying to get the value of $i where $i is undefined
88 causes an error, instead of a null substitution. */
89 int unbound_vars_is_error = 0;
91 /* Non-zero means type out input lines after you read them. */
92 int echo_input_at_read = 0;
94 /* Non-zero means type out the command definition after reading, but
96 int echo_command_at_execute = 0;
98 /* Non-zero means turn on the job control features. */
101 /* Non-zero means this shell is interactive, even if running under a
103 int forced_interactive = 0;
105 /* By default, follow the symbolic links as if they were real directories
106 while hacking the `cd' command. This means that `cd ..' moves up in
107 the string of symbolic links that make up the current directory, instead
108 of the absolute directory. The shell variable `nolinks' also controls
110 int no_symbolic_links = 0;
112 /* **************************************************************** */
114 /* Non-Standard Flags Follow Here. */
116 /* **************************************************************** */
119 /* Non-zero means do lexical scoping in the body of a FOR command. */
120 int lexical_scoping = 0;
123 /* Non-zero means no such thing as invisible variables. */
124 int no_invisible_vars = 0;
126 /* Non-zero means look up and remember command names in a hash table, */
127 int hashing_enabled = 1;
129 #if defined (BANG_HISTORY)
130 /* Non-zero means that we are doing history expansion. The default.
131 This means !22 gets the 22nd line of history. */
132 # if defined (STRICT_POSIX)
133 int history_expansion = 0;
135 int history_expansion = 1;
137 #endif /* BANG_HISTORY */
139 /* Non-zero means that we allow comments to appear in interactive commands. */
140 int interactive_comments = 1;
142 #if defined (RESTRICTED_SHELL)
143 /* Non-zero means that this shell is `restricted'. A restricted shell
144 disallows: changing directories, command or path names containing `/',
145 unsetting or resetting the values of $PATH and $SHELL, and any type of
146 output redirection. */
147 int restricted = 0; /* currently restricted */
148 int restricted_shell = 0; /* shell was started in restricted mode. */
149 #endif /* RESTRICTED_SHELL */
151 /* Non-zero means that this shell is running in `privileged' mode. This
152 is required if the shell is to run setuid. If the `-p' option is
153 not supplied at startup, and the real and effective uids or gids
154 differ, disable_priv_mode is called to relinquish setuid status. */
155 int privileged_mode = 0;
157 #if defined (BRACE_EXPANSION)
158 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
159 int brace_expansion = 1;
162 /* Non-zero means that shell functions inherit the DEBUG trap. */
163 int function_trace_mode = 0;
165 /* Non-zero means that shell functions inherit the ERR trap. */
166 int error_trace_mode = 0;
168 /* Non-zero means that the rightmost non-zero exit status in a pipeline
169 is the exit status of the entire pipeline. If each processes exits
170 with a 0 status, the status of the pipeline is 0. */
171 int pipefail_opt = 0;
173 /* **************************************************************** */
175 /* The Flags ALIST. */
177 /* **************************************************************** */
179 const struct flags_alist shell_flags[] = {
180 /* Standard sh flags. */
181 { 'a', &mark_modified_vars },
182 #if defined (JOB_CONTROL)
183 { 'b', &asynchronous_notification },
184 #endif /* JOB_CONTROL */
185 { 'e', &errexit_flag },
186 { 'f', &disallow_filename_globbing },
187 { 'h', &hashing_enabled },
188 { 'i', &forced_interactive },
189 { 'k', &place_keywords_in_env },
190 #if defined (JOB_CONTROL)
191 { 'm', &jobs_m_flag },
192 #endif /* JOB_CONTROL */
193 { 'n', &read_but_dont_execute },
194 { 'p', &privileged_mode },
195 #if defined (RESTRICTED_SHELL)
196 { 'r', &restricted },
197 #endif /* RESTRICTED_SHELL */
198 { 't', &just_one_command },
199 { 'u', &unbound_vars_is_error },
200 { 'v', &echo_input_at_read },
201 { 'x', &echo_command_at_execute },
203 /* New flags that control non-standard things. */
205 { 'l', &lexical_scoping },
207 #if defined (BRACE_EXPANSION)
208 { 'B', &brace_expansion },
211 { 'E', &error_trace_mode },
212 #if defined (BANG_HISTORY)
213 { 'H', &history_expansion },
214 #endif /* BANG_HISTORY */
215 { 'I', &no_invisible_vars },
216 { 'P', &no_symbolic_links },
217 { 'T', &function_trace_mode },
221 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
223 char optflags[NUM_SHELL_FLAGS+4] = { '+' };
230 for (i = 0; shell_flags[i].name; i++)
232 if (shell_flags[i].name == name)
233 return (shell_flags[i].value);
235 return (FLAG_UNKNOWN);
238 /* Change the state of a flag, and return it's original value, or return
239 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
240 FLAG_ON or FLAG_OFF. */
242 change_flag (flag, on_or_off)
246 int *value, old_value;
248 #if defined (RESTRICTED_SHELL)
249 /* Don't allow "set +r" in a shell which is `restricted'. */
250 if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
252 #endif /* RESTRICTED_SHELL */
254 value = find_flag (flag);
256 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
260 *value = (on_or_off == FLAG_ON) ? 1 : 0;
262 /* Special cases for a few flags. */
265 #if defined (BANG_HISTORY)
267 if (on_or_off == FLAG_ON)
268 bash_initialize_history ();
272 #if defined (JOB_CONTROL)
274 set_job_control (on_or_off == FLAG_ON);
276 #endif /* JOB_CONTROL */
279 if (builtin_ignoring_errexit == 0)
280 exit_immediately_on_error = errexit_flag;
284 if (interactive_shell)
285 read_but_dont_execute = 0;
289 if (on_or_off == FLAG_OFF)
290 disable_priv_mode ();
293 #if defined (RESTRICTED_SHELL)
295 if (on_or_off == FLAG_ON && shell_initialized)
296 maybe_make_restricted (shell_name);
305 /* Return a string which is the names of all the currently
313 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
314 for (i = string_index = 0; shell_flags[i].name; i++)
315 if (*(shell_flags[i].value))
316 temp[string_index++] = shell_flags[i].name;
318 if (want_pending_command)
319 temp[string_index++] = 'c';
321 temp[string_index++] = 's';
323 temp[string_index] = '\0';
330 mark_modified_vars = exit_immediately_on_error = disallow_filename_globbing = 0;
331 place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
332 noclobber = unbound_vars_is_error = echo_input_at_read = 0;
333 echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
334 no_symbolic_links = no_invisible_vars = privileged_mode = pipefail_opt = 0;
336 hashing_enabled = interactive_comments = 1;
338 #if defined (JOB_CONTROL)
339 asynchronous_notification = 0;
342 #if defined (BANG_HISTORY)
343 history_expansion = 1;
346 #if defined (BRACE_EXPANSION)
350 #if defined (RESTRICTED_SHELL)
360 for (i = 0; shell_flags[i].name; i++)
361 optflags[i+1] = shell_flags[i].name;
364 optflags[i+1] = '\0';