]> git.ipfire.org Git - thirdparty/bash.git/blame - flags.c
fix for SIGINT in sourced script
[thirdparty/bash.git] / flags.c
CommitLineData
726f6388
JA
1/* flags.c -- Everything about flags except the `set' command. That
2 is in builtins.c */
3
a0c0a00f 4/* Copyright (C) 1987-2015 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"
28#include "flags.h"
29
f73dda09
JA
30#if defined (BANG_HISTORY)
31# include "bashhist.h"
32#endif
33
ccc6cda3 34#if defined (JOB_CONTROL)
f73dda09 35extern int set_job_control __P((int));
ccc6cda3
JA
36#endif
37
d166f048
JA
38#if defined (RESTRICTED_SHELL)
39extern char *shell_name;
40#endif
41
b80f6443 42extern int shell_initialized;
ac50fbac 43extern int builtin_ignoring_errexit;
b80f6443 44
bb70624e
JA
45/* -c, -s invocation options -- not really flags, but they show up in $- */
46extern int want_pending_command, read_from_stdin;
47
726f6388
JA
48/* **************************************************************** */
49/* */
3185942a 50/* The Standard sh Flags. */
726f6388
JA
51/* */
52/* **************************************************************** */
53
54/* Non-zero means automatically mark variables which are modified or created
55 as auto export variables. */
56int mark_modified_vars = 0;
57
58/* Non-zero causes asynchronous job notification. Otherwise, job state
59 notification only takes place just before a primary prompt is printed. */
60int asynchronous_notification = 0;
61
62/* Non-zero means exit immediately if a command exits with a non-zero
ac50fbac
CR
63 exit status. The first is what controls set -e; the second is what
64 bash uses internally. */
65int errexit_flag = 0;
726f6388
JA
66int exit_immediately_on_error = 0;
67
68/* Non-zero means disable filename globbing. */
69int disallow_filename_globbing = 0;
70
726f6388
JA
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
73 name. */
74int place_keywords_in_env = 0;
75
ccc6cda3 76/* Non-zero means read commands, but don't execute them. This is useful
726f6388 77 for debugging shell scripts that should do something hairy and possibly
f73dda09 78 destructive. */
726f6388
JA
79int read_but_dont_execute = 0;
80
81/* Non-zero means end of file is after one command. */
82int just_one_command = 0;
83
84/* Non-zero means don't overwrite existing files while doing redirections. */
85int noclobber = 0;
86
87/* Non-zero means trying to get the value of $i where $i is undefined
88 causes an error, instead of a null substitution. */
89int unbound_vars_is_error = 0;
90
91/* Non-zero means type out input lines after you read them. */
92int echo_input_at_read = 0;
a0c0a00f 93int verbose_flag = 0;
726f6388
JA
94
95/* Non-zero means type out the command definition after reading, but
96 before executing. */
97int echo_command_at_execute = 0;
98
99/* Non-zero means turn on the job control features. */
100int jobs_m_flag = 0;
101
102/* Non-zero means this shell is interactive, even if running under a
103 pipe. */
104int forced_interactive = 0;
105
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
110 this flag. */
111int no_symbolic_links = 0;
112
113/* **************************************************************** */
114/* */
115/* Non-Standard Flags Follow Here. */
116/* */
117/* **************************************************************** */
118
ccc6cda3 119#if 0
726f6388
JA
120/* Non-zero means do lexical scoping in the body of a FOR command. */
121int lexical_scoping = 0;
ccc6cda3 122#endif
726f6388
JA
123
124/* Non-zero means no such thing as invisible variables. */
125int no_invisible_vars = 0;
126
ccc6cda3
JA
127/* Non-zero means look up and remember command names in a hash table, */
128int hashing_enabled = 1;
726f6388
JA
129
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. */
ac50fbac
CR
133# if defined (STRICT_POSIX)
134int history_expansion = 0;
135# else
726f6388 136int history_expansion = 1;
ac50fbac 137# endif
726f6388
JA
138#endif /* BANG_HISTORY */
139
140/* Non-zero means that we allow comments to appear in interactive commands. */
726f6388 141int interactive_comments = 1;
726f6388
JA
142
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. */
b72432fd
JA
148int restricted = 0; /* currently restricted */
149int restricted_shell = 0; /* shell was started in restricted mode. */
726f6388
JA
150#endif /* RESTRICTED_SHELL */
151
152/* Non-zero means that this shell is running in `privileged' mode. This
e8ce775d
JA
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. */
726f6388
JA
156int privileged_mode = 0;
157
ccc6cda3
JA
158#if defined (BRACE_EXPANSION)
159/* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
160int brace_expansion = 1;
161#endif
162
b80f6443
JA
163/* Non-zero means that shell functions inherit the DEBUG trap. */
164int function_trace_mode = 0;
165
166/* Non-zero means that shell functions inherit the ERR trap. */
167int error_trace_mode = 0;
168
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. */
172int pipefail_opt = 0;
173
726f6388
JA
174/* **************************************************************** */
175/* */
176/* The Flags ALIST. */
177/* */
178/* **************************************************************** */
179
3185942a 180const struct flags_alist shell_flags[] = {
726f6388
JA
181 /* Standard sh flags. */
182 { 'a', &mark_modified_vars },
183#if defined (JOB_CONTROL)
184 { 'b', &asynchronous_notification },
185#endif /* JOB_CONTROL */
ac50fbac 186 { 'e', &errexit_flag },
726f6388 187 { 'f', &disallow_filename_globbing },
ccc6cda3 188 { 'h', &hashing_enabled },
726f6388
JA
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 },
a0c0a00f 201 { 'v', &verbose_flag },
726f6388 202 { 'x', &echo_command_at_execute },
726f6388
JA
203
204 /* New flags that control non-standard things. */
ccc6cda3 205#if 0
726f6388 206 { 'l', &lexical_scoping },
ccc6cda3 207#endif
ccc6cda3
JA
208#if defined (BRACE_EXPANSION)
209 { 'B', &brace_expansion },
210#endif
b80f6443
JA
211 { 'C', &noclobber },
212 { 'E', &error_trace_mode },
726f6388 213#if defined (BANG_HISTORY)
726f6388
JA
214 { 'H', &history_expansion },
215#endif /* BANG_HISTORY */
b80f6443
JA
216 { 'I', &no_invisible_vars },
217 { 'P', &no_symbolic_links },
218 { 'T', &function_trace_mode },
726f6388
JA
219 {0, (int *)NULL}
220};
221
222#define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
223
7117c2d2
JA
224char optflags[NUM_SHELL_FLAGS+4] = { '+' };
225
726f6388
JA
226int *
227find_flag (name)
228 int name;
229{
ccc6cda3
JA
230 int i;
231 for (i = 0; shell_flags[i].name; i++)
726f6388
JA
232 {
233 if (shell_flags[i].name == name)
234 return (shell_flags[i].value);
726f6388
JA
235 }
236 return (FLAG_UNKNOWN);
237}
238
239/* Change the state of a flag, and return it's original value, or return
cce855bc
JA
240 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
241 FLAG_ON or FLAG_OFF. */
726f6388
JA
242int
243change_flag (flag, on_or_off)
244 int flag;
245 int on_or_off;
246{
ccc6cda3
JA
247 int *value, old_value;
248
726f6388
JA
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)
252 return (FLAG_ERROR);
253#endif /* RESTRICTED_SHELL */
254
bb70624e
JA
255 value = find_flag (flag);
256
cce855bc 257 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
726f6388 258 return (FLAG_ERROR);
ccc6cda3
JA
259
260 old_value = *value;
cce855bc 261 *value = (on_or_off == FLAG_ON) ? 1 : 0;
726f6388
JA
262
263 /* Special cases for a few flags. */
264 switch (flag)
265 {
f73dda09
JA
266#if defined (BANG_HISTORY)
267 case 'H':
268 if (on_or_off == FLAG_ON)
269 bash_initialize_history ();
270 break;
271#endif
272
726f6388
JA
273#if defined (JOB_CONTROL)
274 case 'm':
d166f048 275 set_job_control (on_or_off == FLAG_ON);
726f6388
JA
276 break;
277#endif /* JOB_CONTROL */
278
ac50fbac
CR
279 case 'e':
280 if (builtin_ignoring_errexit == 0)
281 exit_immediately_on_error = errexit_flag;
282 break;
283
f73dda09
JA
284 case 'n':
285 if (interactive_shell)
286 read_but_dont_execute = 0;
d166f048 287 break;
d166f048 288
726f6388 289 case 'p':
d166f048 290 if (on_or_off == FLAG_OFF)
ccc6cda3 291 disable_priv_mode ();
f73dda09 292 break;
ccc6cda3 293
f73dda09
JA
294#if defined (RESTRICTED_SHELL)
295 case 'r':
b80f6443 296 if (on_or_off == FLAG_ON && shell_initialized)
f73dda09 297 maybe_make_restricted (shell_name);
726f6388 298 break;
f73dda09
JA
299#endif
300
a0c0a00f
CR
301 case 'v':
302 echo_input_at_read = verbose_flag;
303 break;
726f6388 304 }
ccc6cda3 305
726f6388
JA
306 return (old_value);
307}
308
309/* Return a string which is the names of all the currently
310 set shell flags. */
311char *
312which_set_flags ()
313{
ccc6cda3
JA
314 char *temp;
315 int i, string_index;
726f6388 316
f73dda09 317 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
ccc6cda3 318 for (i = string_index = 0; shell_flags[i].name; i++)
726f6388
JA
319 if (*(shell_flags[i].value))
320 temp[string_index++] = shell_flags[i].name;
321
bb70624e
JA
322 if (want_pending_command)
323 temp[string_index++] = 'c';
324 if (read_from_stdin)
325 temp[string_index++] = 's';
326
726f6388
JA
327 temp[string_index] = '\0';
328 return (temp);
329}
d166f048 330
a0c0a00f
CR
331char *
332get_current_flags ()
333{
334 char *temp;
335 int i;
336
337 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS);
338 for (i = 0; shell_flags[i].name; i++)
339 temp[i] = *(shell_flags[i].value);
340 temp[i] = '\0';
341 return (temp);
342}
343
344void
345set_current_flags (bitmap)
346 const char *bitmap;
347{
348 int i;
349
350 if (bitmap == 0)
351 return;
352 for (i = 0; shell_flags[i].name; i++)
353 *(shell_flags[i].value) = bitmap[i];
354}
355
d166f048
JA
356void
357reset_shell_flags ()
358{
a0c0a00f 359 mark_modified_vars = disallow_filename_globbing = 0;
d166f048 360 place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
a0c0a00f 361 noclobber = unbound_vars_is_error = 0;
d166f048 362 echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
a0c0a00f
CR
363 no_symbolic_links = no_invisible_vars = 0;
364 privileged_mode = pipefail_opt = 0;
365
366 error_trace_mode = function_trace_mode = 0;
367
368 exit_immediately_on_error = errexit_flag = 0;
369 echo_input_at_read = verbose_flag = 0;
d166f048
JA
370
371 hashing_enabled = interactive_comments = 1;
372
373#if defined (JOB_CONTROL)
374 asynchronous_notification = 0;
375#endif
376
377#if defined (BANG_HISTORY)
a0c0a00f
CR
378# if defined (STRICT_POSIX)
379 history_expansion = 0;
380# else
d166f048 381 history_expansion = 1;
a0c0a00f 382# endif /* STRICT_POSIX */
d166f048
JA
383#endif
384
385#if defined (BRACE_EXPANSION)
386 brace_expansion = 1;
387#endif
388
389#if defined (RESTRICTED_SHELL)
390 restricted = 0;
391#endif
392}
7117c2d2
JA
393
394void
395initialize_flags ()
396{
397 register int i;
398
399 for (i = 0; shell_flags[i].name; i++)
400 optflags[i+1] = shell_flags[i].name;
401 optflags[++i] = 'o';
402 optflags[++i] = ';';
403 optflags[i+1] = '\0';
404}