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)
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;
95 /* Non-zero means type out the command definition after reading, but
97 int echo_command_at_execute
= 0;
99 /* Non-zero means turn on the job control features. */
102 /* Non-zero means this shell is interactive, even if running under a
104 int forced_interactive
= 0;
106 /* By default, follow the symbolic links as if they were real directories
107 while hacking the `cd' command. This means that `cd ..' moves up in
108 the string of symbolic links that make up the current directory, instead
109 of the absolute directory. The shell variable `nolinks' also controls
111 int no_symbolic_links
= 0;
113 /* **************************************************************** */
115 /* Non-Standard Flags Follow Here. */
117 /* **************************************************************** */
120 /* Non-zero means do lexical scoping in the body of a FOR command. */
121 int lexical_scoping
= 0;
124 /* Non-zero means no such thing as invisible variables. */
125 int no_invisible_vars
= 0;
127 /* Non-zero means look up and remember command names in a hash table, */
128 int hashing_enabled
= 1;
130 #if defined (BANG_HISTORY)
131 /* Non-zero means that we are doing history expansion. The default.
132 This means !22 gets the 22nd line of history. */
133 # if defined (STRICT_POSIX)
134 int history_expansion
= 0;
136 int history_expansion
= 1;
138 #endif /* BANG_HISTORY */
140 /* Non-zero means that we allow comments to appear in interactive commands. */
141 int interactive_comments
= 1;
143 #if defined (RESTRICTED_SHELL)
144 /* Non-zero means that this shell is `restricted'. A restricted shell
145 disallows: changing directories, command or path names containing `/',
146 unsetting or resetting the values of $PATH and $SHELL, and any type of
147 output redirection. */
148 int restricted
= 0; /* currently restricted */
149 int restricted_shell
= 0; /* shell was started in restricted mode. */
150 #endif /* RESTRICTED_SHELL */
152 /* Non-zero means that this shell is running in `privileged' mode. This
153 is required if the shell is to run setuid. If the `-p' option is
154 not supplied at startup, and the real and effective uids or gids
155 differ, disable_priv_mode is called to relinquish setuid status. */
156 int privileged_mode
= 0;
158 #if defined (BRACE_EXPANSION)
159 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
160 int brace_expansion
= 1;
163 /* Non-zero means that shell functions inherit the DEBUG trap. */
164 int function_trace_mode
= 0;
166 /* Non-zero means that shell functions inherit the ERR trap. */
167 int error_trace_mode
= 0;
169 /* Non-zero means that the rightmost non-zero exit status in a pipeline
170 is the exit status of the entire pipeline. If each processes exits
171 with a 0 status, the status of the pipeline is 0. */
172 int pipefail_opt
= 0;
174 /* **************************************************************** */
176 /* The Flags ALIST. */
178 /* **************************************************************** */
180 const struct flags_alist shell_flags
[] = {
181 /* Standard sh flags. */
182 { 'a', &mark_modified_vars
},
183 #if defined (JOB_CONTROL)
184 { 'b', &asynchronous_notification
},
185 #endif /* JOB_CONTROL */
186 { 'e', &errexit_flag
},
187 { 'f', &disallow_filename_globbing
},
188 { 'h', &hashing_enabled
},
189 { 'i', &forced_interactive
},
190 { 'k', &place_keywords_in_env
},
191 #if defined (JOB_CONTROL)
192 { 'm', &jobs_m_flag
},
193 #endif /* JOB_CONTROL */
194 { 'n', &read_but_dont_execute
},
195 { 'p', &privileged_mode
},
196 #if defined (RESTRICTED_SHELL)
197 { 'r', &restricted
},
198 #endif /* RESTRICTED_SHELL */
199 { 't', &just_one_command
},
200 { 'u', &unbound_vars_is_error
},
201 { 'v', &verbose_flag
},
202 { 'x', &echo_command_at_execute
},
204 /* New flags that control non-standard things. */
206 { 'l', &lexical_scoping
},
208 #if defined (BRACE_EXPANSION)
209 { 'B', &brace_expansion
},
212 { 'E', &error_trace_mode
},
213 #if defined (BANG_HISTORY)
214 { 'H', &history_expansion
},
215 #endif /* BANG_HISTORY */
216 { 'I', &no_invisible_vars
},
217 { 'P', &no_symbolic_links
},
218 { 'T', &function_trace_mode
},
222 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
224 char optflags
[NUM_SHELL_FLAGS
+4] = { '+' };
231 for (i
= 0; shell_flags
[i
].name
; i
++)
233 if (shell_flags
[i
].name
== name
)
234 return (shell_flags
[i
].value
);
236 return (FLAG_UNKNOWN
);
239 /* Change the state of a flag, and return it's original value, or return
240 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
241 FLAG_ON or FLAG_OFF. */
243 change_flag (flag
, on_or_off
)
247 int *value
, old_value
;
249 #if defined (RESTRICTED_SHELL)
250 /* Don't allow "set +r" in a shell which is `restricted'. */
251 if (restricted
&& flag
== 'r' && on_or_off
== FLAG_OFF
)
253 #endif /* RESTRICTED_SHELL */
255 value
= find_flag (flag
);
257 if ((value
== (int *)FLAG_UNKNOWN
) || (on_or_off
!= FLAG_ON
&& on_or_off
!= FLAG_OFF
))
261 *value
= (on_or_off
== FLAG_ON
) ? 1 : 0;
263 /* Special cases for a few flags. */
266 #if defined (BANG_HISTORY)
268 if (on_or_off
== FLAG_ON
)
269 bash_initialize_history ();
273 #if defined (JOB_CONTROL)
275 set_job_control (on_or_off
== FLAG_ON
);
277 #endif /* JOB_CONTROL */
280 if (builtin_ignoring_errexit
== 0)
281 exit_immediately_on_error
= errexit_flag
;
285 if (interactive_shell
)
286 read_but_dont_execute
= 0;
290 if (on_or_off
== FLAG_OFF
)
291 disable_priv_mode ();
294 #if defined (RESTRICTED_SHELL)
296 if (on_or_off
== FLAG_ON
&& shell_initialized
)
297 maybe_make_restricted (shell_name
);
302 echo_input_at_read
= verbose_flag
;
309 /* Return a string which is the names of all the currently
317 temp
= (char *)xmalloc (1 + NUM_SHELL_FLAGS
+ read_from_stdin
+ want_pending_command
);
318 for (i
= string_index
= 0; shell_flags
[i
].name
; i
++)
319 if (*(shell_flags
[i
].value
))
320 temp
[string_index
++] = shell_flags
[i
].name
;
322 if (want_pending_command
)
323 temp
[string_index
++] = 'c';
325 temp
[string_index
++] = 's';
327 temp
[string_index
] = '\0';
337 temp
= (char *)xmalloc (1 + NUM_SHELL_FLAGS
);
338 for (i
= 0; shell_flags
[i
].name
; i
++)
339 temp
[i
] = *(shell_flags
[i
].value
);
345 set_current_flags (bitmap
)
352 for (i
= 0; shell_flags
[i
].name
; i
++)
353 *(shell_flags
[i
].value
) = bitmap
[i
];
359 mark_modified_vars
= disallow_filename_globbing
= 0;
360 place_keywords_in_env
= read_but_dont_execute
= just_one_command
= 0;
361 noclobber
= unbound_vars_is_error
= 0;
362 echo_command_at_execute
= jobs_m_flag
= forced_interactive
= 0;
363 no_symbolic_links
= no_invisible_vars
= 0;
364 privileged_mode
= pipefail_opt
= 0;
366 error_trace_mode
= function_trace_mode
= 0;
368 exit_immediately_on_error
= errexit_flag
= 0;
369 echo_input_at_read
= verbose_flag
= 0;
371 hashing_enabled
= interactive_comments
= 1;
373 #if defined (JOB_CONTROL)
374 asynchronous_notification
= 0;
377 #if defined (BANG_HISTORY)
378 # if defined (STRICT_POSIX)
379 history_expansion
= 0;
381 history_expansion
= 1;
382 # endif /* STRICT_POSIX */
385 #if defined (BRACE_EXPANSION)
389 #if defined (RESTRICTED_SHELL)
399 for (i
= 0; shell_flags
[i
].name
; i
++)
400 optflags
[i
+1] = shell_flags
[i
].name
;
403 optflags
[i
+1] = '\0';