]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* flags.c -- Everything about flags except the `set' command. That |
2 | is in builtins.c */ | |
3 | ||
74091dd4 | 4 | /* Copyright (C) 1987-2021 Free Software Foundation, Inc. |
726f6388 | 5 | |
3185942a | 6 | This file is part of GNU Bash, the Bourne Again SHell. |
726f6388 | 7 | |
3185942a JA |
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. | |
726f6388 | 12 | |
3185942a JA |
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. | |
726f6388 | 17 | |
3185942a JA |
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 | */ | |
726f6388 | 21 | |
ccc6cda3 JA |
22 | #include "config.h" |
23 | #if defined (HAVE_UNISTD_H) | |
24 | # include <unistd.h> | |
25 | #endif | |
726f6388 JA |
26 | |
27 | #include "shell.h" | |
d233b485 | 28 | #include "execute_cmd.h" |
726f6388 JA |
29 | #include "flags.h" |
30 | ||
f73dda09 JA |
31 | #if defined (BANG_HISTORY) |
32 | # include "bashhist.h" | |
33 | #endif | |
34 | ||
ccc6cda3 | 35 | #if defined (JOB_CONTROL) |
8868edaf | 36 | extern int set_job_control PARAMS((int)); |
ccc6cda3 JA |
37 | #endif |
38 | ||
726f6388 JA |
39 | /* **************************************************************** */ |
40 | /* */ | |
3185942a | 41 | /* The Standard sh Flags. */ |
726f6388 JA |
42 | /* */ |
43 | /* **************************************************************** */ | |
44 | ||
45 | /* Non-zero means automatically mark variables which are modified or created | |
46 | as auto export variables. */ | |
47 | int 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. */ | |
51 | int asynchronous_notification = 0; | |
52 | ||
53 | /* Non-zero means exit immediately if a command exits with a non-zero | |
ac50fbac CR |
54 | exit status. The first is what controls set -e; the second is what |
55 | bash uses internally. */ | |
56 | int errexit_flag = 0; | |
726f6388 JA |
57 | int exit_immediately_on_error = 0; |
58 | ||
59 | /* Non-zero means disable filename globbing. */ | |
60 | int disallow_filename_globbing = 0; | |
61 | ||
726f6388 JA |
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. */ | |
65 | int place_keywords_in_env = 0; | |
66 | ||
ccc6cda3 | 67 | /* Non-zero means read commands, but don't execute them. This is useful |
726f6388 | 68 | for debugging shell scripts that should do something hairy and possibly |
f73dda09 | 69 | destructive. */ |
726f6388 JA |
70 | int read_but_dont_execute = 0; |
71 | ||
72 | /* Non-zero means end of file is after one command. */ | |
73 | int just_one_command = 0; | |
74 | ||
75 | /* Non-zero means don't overwrite existing files while doing redirections. */ | |
76 | int 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. */ | |
80 | int unbound_vars_is_error = 0; | |
81 | ||
82 | /* Non-zero means type out input lines after you read them. */ | |
83 | int echo_input_at_read = 0; | |
a0c0a00f | 84 | int verbose_flag = 0; |
726f6388 JA |
85 | |
86 | /* Non-zero means type out the command definition after reading, but | |
87 | before executing. */ | |
88 | int echo_command_at_execute = 0; | |
89 | ||
90 | /* Non-zero means turn on the job control features. */ | |
91 | int jobs_m_flag = 0; | |
92 | ||
93 | /* Non-zero means this shell is interactive, even if running under a | |
94 | pipe. */ | |
95 | int 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. */ | |
102 | int no_symbolic_links = 0; | |
103 | ||
104 | /* **************************************************************** */ | |
105 | /* */ | |
106 | /* Non-Standard Flags Follow Here. */ | |
107 | /* */ | |
108 | /* **************************************************************** */ | |
109 | ||
ccc6cda3 | 110 | #if 0 |
726f6388 JA |
111 | /* Non-zero means do lexical scoping in the body of a FOR command. */ |
112 | int lexical_scoping = 0; | |
ccc6cda3 | 113 | #endif |
726f6388 | 114 | |
ccc6cda3 JA |
115 | /* Non-zero means look up and remember command names in a hash table, */ |
116 | int hashing_enabled = 1; | |
726f6388 JA |
117 | |
118 | #if defined (BANG_HISTORY) | |
119 | /* Non-zero means that we are doing history expansion. The default. | |
120 | This means !22 gets the 22nd line of history. */ | |
d233b485 CR |
121 | int history_expansion = HISTEXPAND_DEFAULT; |
122 | int histexp_flag = 0; | |
726f6388 JA |
123 | #endif /* BANG_HISTORY */ |
124 | ||
125 | /* Non-zero means that we allow comments to appear in interactive commands. */ | |
726f6388 | 126 | int interactive_comments = 1; |
726f6388 JA |
127 | |
128 | #if defined (RESTRICTED_SHELL) | |
129 | /* Non-zero means that this shell is `restricted'. A restricted shell | |
130 | disallows: changing directories, command or path names containing `/', | |
131 | unsetting or resetting the values of $PATH and $SHELL, and any type of | |
132 | output redirection. */ | |
b72432fd JA |
133 | int restricted = 0; /* currently restricted */ |
134 | int restricted_shell = 0; /* shell was started in restricted mode. */ | |
726f6388 JA |
135 | #endif /* RESTRICTED_SHELL */ |
136 | ||
137 | /* Non-zero means that this shell is running in `privileged' mode. This | |
e8ce775d JA |
138 | is required if the shell is to run setuid. If the `-p' option is |
139 | not supplied at startup, and the real and effective uids or gids | |
140 | differ, disable_priv_mode is called to relinquish setuid status. */ | |
726f6388 JA |
141 | int privileged_mode = 0; |
142 | ||
ccc6cda3 JA |
143 | #if defined (BRACE_EXPANSION) |
144 | /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */ | |
145 | int brace_expansion = 1; | |
146 | #endif | |
147 | ||
b80f6443 JA |
148 | /* Non-zero means that shell functions inherit the DEBUG trap. */ |
149 | int function_trace_mode = 0; | |
150 | ||
151 | /* Non-zero means that shell functions inherit the ERR trap. */ | |
152 | int error_trace_mode = 0; | |
153 | ||
154 | /* Non-zero means that the rightmost non-zero exit status in a pipeline | |
155 | is the exit status of the entire pipeline. If each processes exits | |
156 | with a 0 status, the status of the pipeline is 0. */ | |
157 | int pipefail_opt = 0; | |
158 | ||
726f6388 JA |
159 | /* **************************************************************** */ |
160 | /* */ | |
161 | /* The Flags ALIST. */ | |
162 | /* */ | |
163 | /* **************************************************************** */ | |
164 | ||
3185942a | 165 | const struct flags_alist shell_flags[] = { |
726f6388 JA |
166 | /* Standard sh flags. */ |
167 | { 'a', &mark_modified_vars }, | |
168 | #if defined (JOB_CONTROL) | |
169 | { 'b', &asynchronous_notification }, | |
170 | #endif /* JOB_CONTROL */ | |
ac50fbac | 171 | { 'e', &errexit_flag }, |
726f6388 | 172 | { 'f', &disallow_filename_globbing }, |
ccc6cda3 | 173 | { 'h', &hashing_enabled }, |
726f6388 JA |
174 | { 'i', &forced_interactive }, |
175 | { 'k', &place_keywords_in_env }, | |
176 | #if defined (JOB_CONTROL) | |
177 | { 'm', &jobs_m_flag }, | |
178 | #endif /* JOB_CONTROL */ | |
179 | { 'n', &read_but_dont_execute }, | |
180 | { 'p', &privileged_mode }, | |
181 | #if defined (RESTRICTED_SHELL) | |
182 | { 'r', &restricted }, | |
183 | #endif /* RESTRICTED_SHELL */ | |
184 | { 't', &just_one_command }, | |
185 | { 'u', &unbound_vars_is_error }, | |
a0c0a00f | 186 | { 'v', &verbose_flag }, |
726f6388 | 187 | { 'x', &echo_command_at_execute }, |
726f6388 JA |
188 | |
189 | /* New flags that control non-standard things. */ | |
ccc6cda3 | 190 | #if 0 |
726f6388 | 191 | { 'l', &lexical_scoping }, |
ccc6cda3 | 192 | #endif |
ccc6cda3 JA |
193 | #if defined (BRACE_EXPANSION) |
194 | { 'B', &brace_expansion }, | |
195 | #endif | |
b80f6443 JA |
196 | { 'C', &noclobber }, |
197 | { 'E', &error_trace_mode }, | |
726f6388 | 198 | #if defined (BANG_HISTORY) |
d233b485 | 199 | { 'H', &histexp_flag }, |
726f6388 | 200 | #endif /* BANG_HISTORY */ |
b80f6443 JA |
201 | { 'P', &no_symbolic_links }, |
202 | { 'T', &function_trace_mode }, | |
726f6388 JA |
203 | {0, (int *)NULL} |
204 | }; | |
205 | ||
206 | #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist)) | |
207 | ||
7117c2d2 JA |
208 | char optflags[NUM_SHELL_FLAGS+4] = { '+' }; |
209 | ||
726f6388 JA |
210 | int * |
211 | find_flag (name) | |
212 | int name; | |
213 | { | |
ccc6cda3 JA |
214 | int i; |
215 | for (i = 0; shell_flags[i].name; i++) | |
726f6388 JA |
216 | { |
217 | if (shell_flags[i].name == name) | |
218 | return (shell_flags[i].value); | |
726f6388 JA |
219 | } |
220 | return (FLAG_UNKNOWN); | |
221 | } | |
222 | ||
223 | /* Change the state of a flag, and return it's original value, or return | |
cce855bc JA |
224 | FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either |
225 | FLAG_ON or FLAG_OFF. */ | |
726f6388 JA |
226 | int |
227 | change_flag (flag, on_or_off) | |
228 | int flag; | |
229 | int on_or_off; | |
230 | { | |
ccc6cda3 JA |
231 | int *value, old_value; |
232 | ||
726f6388 JA |
233 | #if defined (RESTRICTED_SHELL) |
234 | /* Don't allow "set +r" in a shell which is `restricted'. */ | |
235 | if (restricted && flag == 'r' && on_or_off == FLAG_OFF) | |
236 | return (FLAG_ERROR); | |
237 | #endif /* RESTRICTED_SHELL */ | |
238 | ||
bb70624e JA |
239 | value = find_flag (flag); |
240 | ||
cce855bc | 241 | if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF)) |
726f6388 | 242 | return (FLAG_ERROR); |
ccc6cda3 JA |
243 | |
244 | old_value = *value; | |
cce855bc | 245 | *value = (on_or_off == FLAG_ON) ? 1 : 0; |
726f6388 JA |
246 | |
247 | /* Special cases for a few flags. */ | |
248 | switch (flag) | |
249 | { | |
f73dda09 JA |
250 | #if defined (BANG_HISTORY) |
251 | case 'H': | |
d233b485 | 252 | history_expansion = histexp_flag; |
f73dda09 JA |
253 | if (on_or_off == FLAG_ON) |
254 | bash_initialize_history (); | |
255 | break; | |
256 | #endif | |
257 | ||
726f6388 JA |
258 | #if defined (JOB_CONTROL) |
259 | case 'm': | |
d166f048 | 260 | set_job_control (on_or_off == FLAG_ON); |
726f6388 JA |
261 | break; |
262 | #endif /* JOB_CONTROL */ | |
263 | ||
ac50fbac CR |
264 | case 'e': |
265 | if (builtin_ignoring_errexit == 0) | |
266 | exit_immediately_on_error = errexit_flag; | |
267 | break; | |
268 | ||
f73dda09 JA |
269 | case 'n': |
270 | if (interactive_shell) | |
271 | read_but_dont_execute = 0; | |
d166f048 | 272 | break; |
d166f048 | 273 | |
726f6388 | 274 | case 'p': |
d166f048 | 275 | if (on_or_off == FLAG_OFF) |
ccc6cda3 | 276 | disable_priv_mode (); |
f73dda09 | 277 | break; |
ccc6cda3 | 278 | |
f73dda09 JA |
279 | #if defined (RESTRICTED_SHELL) |
280 | case 'r': | |
b80f6443 | 281 | if (on_or_off == FLAG_ON && shell_initialized) |
f73dda09 | 282 | maybe_make_restricted (shell_name); |
726f6388 | 283 | break; |
f73dda09 JA |
284 | #endif |
285 | ||
a0c0a00f CR |
286 | case 'v': |
287 | echo_input_at_read = verbose_flag; | |
288 | break; | |
726f6388 | 289 | } |
ccc6cda3 | 290 | |
726f6388 JA |
291 | return (old_value); |
292 | } | |
293 | ||
294 | /* Return a string which is the names of all the currently | |
295 | set shell flags. */ | |
296 | char * | |
297 | which_set_flags () | |
298 | { | |
ccc6cda3 JA |
299 | char *temp; |
300 | int i, string_index; | |
726f6388 | 301 | |
f73dda09 | 302 | temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command); |
ccc6cda3 | 303 | for (i = string_index = 0; shell_flags[i].name; i++) |
726f6388 JA |
304 | if (*(shell_flags[i].value)) |
305 | temp[string_index++] = shell_flags[i].name; | |
306 | ||
bb70624e JA |
307 | if (want_pending_command) |
308 | temp[string_index++] = 'c'; | |
309 | if (read_from_stdin) | |
310 | temp[string_index++] = 's'; | |
311 | ||
726f6388 JA |
312 | temp[string_index] = '\0'; |
313 | return (temp); | |
314 | } | |
d166f048 | 315 | |
a0c0a00f CR |
316 | char * |
317 | get_current_flags () | |
318 | { | |
319 | char *temp; | |
320 | int i; | |
321 | ||
322 | temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS); | |
323 | for (i = 0; shell_flags[i].name; i++) | |
324 | temp[i] = *(shell_flags[i].value); | |
325 | temp[i] = '\0'; | |
326 | return (temp); | |
327 | } | |
328 | ||
329 | void | |
330 | set_current_flags (bitmap) | |
331 | const char *bitmap; | |
332 | { | |
333 | int i; | |
334 | ||
335 | if (bitmap == 0) | |
336 | return; | |
337 | for (i = 0; shell_flags[i].name; i++) | |
338 | *(shell_flags[i].value) = bitmap[i]; | |
339 | } | |
340 | ||
d166f048 JA |
341 | void |
342 | reset_shell_flags () | |
343 | { | |
a0c0a00f | 344 | mark_modified_vars = disallow_filename_globbing = 0; |
d166f048 | 345 | place_keywords_in_env = read_but_dont_execute = just_one_command = 0; |
a0c0a00f | 346 | noclobber = unbound_vars_is_error = 0; |
d166f048 | 347 | echo_command_at_execute = jobs_m_flag = forced_interactive = 0; |
8868edaf | 348 | no_symbolic_links = 0; |
a0c0a00f CR |
349 | privileged_mode = pipefail_opt = 0; |
350 | ||
351 | error_trace_mode = function_trace_mode = 0; | |
352 | ||
353 | exit_immediately_on_error = errexit_flag = 0; | |
354 | echo_input_at_read = verbose_flag = 0; | |
d166f048 JA |
355 | |
356 | hashing_enabled = interactive_comments = 1; | |
357 | ||
358 | #if defined (JOB_CONTROL) | |
359 | asynchronous_notification = 0; | |
360 | #endif | |
361 | ||
362 | #if defined (BANG_HISTORY) | |
8868edaf | 363 | histexp_flag = 0; |
d166f048 JA |
364 | #endif |
365 | ||
366 | #if defined (BRACE_EXPANSION) | |
367 | brace_expansion = 1; | |
368 | #endif | |
369 | ||
370 | #if defined (RESTRICTED_SHELL) | |
371 | restricted = 0; | |
372 | #endif | |
373 | } | |
7117c2d2 JA |
374 | |
375 | void | |
376 | initialize_flags () | |
377 | { | |
378 | register int i; | |
379 | ||
380 | for (i = 0; shell_flags[i].name; i++) | |
381 | optflags[i+1] = shell_flags[i].name; | |
382 | optflags[++i] = 'o'; | |
383 | optflags[++i] = ';'; | |
384 | optflags[i+1] = '\0'; | |
385 | } |