]> git.ipfire.org Git - thirdparty/bash.git/blame_incremental - flags.c
fix for SIGINT in sourced script
[thirdparty/bash.git] / flags.c
... / ...
CommitLineData
1/* flags.c -- Everything about flags except the `set' command. That
2 is in builtins.c */
3
4/* Copyright (C) 1987-2015 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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.
12
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.
17
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*/
21
22#include "config.h"
23#if defined (HAVE_UNISTD_H)
24# include <unistd.h>
25#endif
26
27#include "shell.h"
28#include "flags.h"
29
30#if defined (BANG_HISTORY)
31# include "bashhist.h"
32#endif
33
34#if defined (JOB_CONTROL)
35extern int set_job_control __P((int));
36#endif
37
38#if defined (RESTRICTED_SHELL)
39extern char *shell_name;
40#endif
41
42extern int shell_initialized;
43extern int builtin_ignoring_errexit;
44
45/* -c, -s invocation options -- not really flags, but they show up in $- */
46extern int want_pending_command, read_from_stdin;
47
48/* **************************************************************** */
49/* */
50/* The Standard sh Flags. */
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
63 exit status. The first is what controls set -e; the second is what
64 bash uses internally. */
65int errexit_flag = 0;
66int exit_immediately_on_error = 0;
67
68/* Non-zero means disable filename globbing. */
69int disallow_filename_globbing = 0;
70
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
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
78 destructive. */
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;
93int verbose_flag = 0;
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
119#if 0
120/* Non-zero means do lexical scoping in the body of a FOR command. */
121int lexical_scoping = 0;
122#endif
123
124/* Non-zero means no such thing as invisible variables. */
125int no_invisible_vars = 0;
126
127/* Non-zero means look up and remember command names in a hash table, */
128int hashing_enabled = 1;
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. */
133# if defined (STRICT_POSIX)
134int history_expansion = 0;
135# else
136int history_expansion = 1;
137# endif
138#endif /* BANG_HISTORY */
139
140/* Non-zero means that we allow comments to appear in interactive commands. */
141int interactive_comments = 1;
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. */
148int restricted = 0; /* currently restricted */
149int restricted_shell = 0; /* shell was started in restricted mode. */
150#endif /* RESTRICTED_SHELL */
151
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. */
156int privileged_mode = 0;
157
158#if defined (BRACE_EXPANSION)
159/* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
160int brace_expansion = 1;
161#endif
162
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
174/* **************************************************************** */
175/* */
176/* The Flags ALIST. */
177/* */
178/* **************************************************************** */
179
180const 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 },
203
204 /* New flags that control non-standard things. */
205#if 0
206 { 'l', &lexical_scoping },
207#endif
208#if defined (BRACE_EXPANSION)
209 { 'B', &brace_expansion },
210#endif
211 { 'C', &noclobber },
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 },
219 {0, (int *)NULL}
220};
221
222#define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
223
224char optflags[NUM_SHELL_FLAGS+4] = { '+' };
225
226int *
227find_flag (name)
228 int name;
229{
230 int i;
231 for (i = 0; shell_flags[i].name; i++)
232 {
233 if (shell_flags[i].name == name)
234 return (shell_flags[i].value);
235 }
236 return (FLAG_UNKNOWN);
237}
238
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. */
242int
243change_flag (flag, on_or_off)
244 int flag;
245 int on_or_off;
246{
247 int *value, old_value;
248
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
255 value = find_flag (flag);
256
257 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
258 return (FLAG_ERROR);
259
260 old_value = *value;
261 *value = (on_or_off == FLAG_ON) ? 1 : 0;
262
263 /* Special cases for a few flags. */
264 switch (flag)
265 {
266#if defined (BANG_HISTORY)
267 case 'H':
268 if (on_or_off == FLAG_ON)
269 bash_initialize_history ();
270 break;
271#endif
272
273#if defined (JOB_CONTROL)
274 case 'm':
275 set_job_control (on_or_off == FLAG_ON);
276 break;
277#endif /* JOB_CONTROL */
278
279 case 'e':
280 if (builtin_ignoring_errexit == 0)
281 exit_immediately_on_error = errexit_flag;
282 break;
283
284 case 'n':
285 if (interactive_shell)
286 read_but_dont_execute = 0;
287 break;
288
289 case 'p':
290 if (on_or_off == FLAG_OFF)
291 disable_priv_mode ();
292 break;
293
294#if defined (RESTRICTED_SHELL)
295 case 'r':
296 if (on_or_off == FLAG_ON && shell_initialized)
297 maybe_make_restricted (shell_name);
298 break;
299#endif
300
301 case 'v':
302 echo_input_at_read = verbose_flag;
303 break;
304 }
305
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{
314 char *temp;
315 int i, string_index;
316
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;
321
322 if (want_pending_command)
323 temp[string_index++] = 'c';
324 if (read_from_stdin)
325 temp[string_index++] = 's';
326
327 temp[string_index] = '\0';
328 return (temp);
329}
330
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
356void
357reset_shell_flags ()
358{
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;
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;
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)
378# if defined (STRICT_POSIX)
379 history_expansion = 0;
380# else
381 history_expansion = 1;
382# endif /* STRICT_POSIX */
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}
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}