1 /* flags.c -- Everything about flags except the `set' command. That
4 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
28 #include "runner/execute_cmd.h"
31 #if defined (BANG_HISTORY)
32 # include "bushhist.h"
35 #if defined (JOB_CONTROL)
36 extern int set_job_control
PARAMS((int));
39 /* **************************************************************** */
41 /* The Standard sh Flags. */
43 /* **************************************************************** */
45 /* Non-zero means automatically mark variables which are modified or created
46 as auto export variables. */
47 int mark_modified_vars
= 0;
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;
53 /* Non-zero means type out the command definition after reading, just
54 in current function. */
57 /* Non-zero means exit immediately if a command exits with a non-zero
58 exit status. The first is what controls set -e; the second is what
59 bush uses internally. */
61 int exit_immediately_on_error
= 0;
63 /* Non-zero means disable filename globbing. */
64 int disallow_filename_globbing
= 0;
66 /* Non-zero means that all keyword arguments are placed into the environment
67 for a command, not just those that appear on the line before the command
69 int place_keywords_in_env
= 0;
71 /* Non-zero means read commands, but don't execute them. This is useful
72 for debugging shell scripts that should do something hairy and possibly
74 int read_but_dont_execute
= 0;
76 /* Non-zero means end of file is after one command. */
77 int just_one_command
= 0;
79 /* Non-zero means don't overwrite existing files while doing redirections. */
82 /* Non-zero means trying to get the value of $i where $i is undefined
83 causes an error, instead of a null substitution. */
84 int unbound_vars_is_error
= 0;
86 /* Non-zero means type out input lines after you read them. */
87 int echo_input_at_read
= 0;
90 /* Non-zero means type out the command definition after reading, but
92 int echo_command_at_execute
= 0;
94 /* Non-zero means turn on the job control features. */
97 /* Non-zero means this shell is interactive, even if running under a
99 int forced_interactive
= 0;
101 /* By default, follow the symbolic links as if they were real directories
102 while hacking the `cd' command. This means that `cd ..' moves up in
103 the string of symbolic links that make up the current directory, instead
104 of the absolute directory. The shell variable `nolinks' also controls
106 int no_symbolic_links
= 0;
108 /* **************************************************************** */
110 /* Non-Standard Flags Follow Here. */
112 /* **************************************************************** */
115 /* Non-zero means do lexical scoping in the body of a FOR command. */
116 int lexical_scoping
= 0;
119 /* Non-zero means look up and remember command names in a hash table, */
120 int hashing_enabled
= 1;
122 #if defined (BANG_HISTORY)
123 /* Non-zero means that we are doing history expansion. The default.
124 This means !22 gets the 22nd line of history. */
125 int history_expansion
= HISTEXPAND_DEFAULT
;
126 int histexp_flag
= 0;
127 #endif /* BANG_HISTORY */
129 /* Non-zero means that we allow comments to appear in interactive commands. */
130 int interactive_comments
= 1;
132 #if defined (RESTRICTED_SHELL)
133 /* Non-zero means that this shell is `restricted'. A restricted shell
134 disallows: changing directories, command or path names containing `/',
135 unsetting or resetting the values of $PATH and $SHELL, and any type of
136 output redirection. */
137 int restricted
= 0; /* currently restricted */
138 int restricted_shell
= 0; /* shell was started in restricted mode. */
139 #endif /* RESTRICTED_SHELL */
141 /* Non-zero means that this shell is running in `privileged' mode. This
142 is required if the shell is to run setuid. If the `-p' option is
143 not supplied at startup, and the real and effective uids or gids
144 differ, disable_priv_mode is called to relinquish setuid status. */
145 int privileged_mode
= 0;
147 #if defined (BRACE_EXPANSION)
148 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
149 int brace_expansion
= 1;
152 /* Non-zero means that shell functions inherit the DEBUG trap. */
153 int function_trace_mode
= 0;
155 /* Non-zero means that shell functions inherit the ERR trap. */
156 int error_trace_mode
= 0;
158 /* Non-zero means that the rightmost non-zero exit status in a pipeline
159 is the exit status of the entire pipeline. If each processes exits
160 with a 0 status, the status of the pipeline is 0. */
161 int pipefail_opt
= 0;
163 /* **************************************************************** */
165 /* The Flags ALIST. */
167 /* **************************************************************** */
169 const struct flags_alist shell_flags
[] = {
170 /* Standard sh flags. */
171 { 'a', &mark_modified_vars
},
172 #if defined (JOB_CONTROL)
173 { 'b', &asynchronous_notification
},
174 #endif /* JOB_CONTROL */
175 { 'd', &debug_info
},
176 { 'e', &errexit_flag
},
177 { 'f', &disallow_filename_globbing
},
178 { 'h', &hashing_enabled
},
179 { 'i', &forced_interactive
},
180 { 'k', &place_keywords_in_env
},
181 #if defined (JOB_CONTROL)
182 { 'm', &jobs_m_flag
},
183 #endif /* JOB_CONTROL */
184 { 'n', &read_but_dont_execute
},
185 { 'p', &privileged_mode
},
186 #if defined (RESTRICTED_SHELL)
187 { 'r', &restricted
},
188 #endif /* RESTRICTED_SHELL */
189 { 't', &just_one_command
},
190 { 'u', &unbound_vars_is_error
},
191 { 'v', &verbose_flag
},
192 { 'x', &echo_command_at_execute
},
194 /* New flags that control non-standard things. */
196 { 'l', &lexical_scoping
},
198 #if defined (BRACE_EXPANSION)
199 { 'B', &brace_expansion
},
202 { 'E', &error_trace_mode
},
203 #if defined (BANG_HISTORY)
204 { 'H', &histexp_flag
},
205 #endif /* BANG_HISTORY */
206 { 'P', &no_symbolic_links
},
207 { 'T', &function_trace_mode
},
211 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
213 char optflags
[NUM_SHELL_FLAGS
+4] = { '+' };
220 for (i
= 0; shell_flags
[i
].name
; i
++)
222 if (shell_flags
[i
].name
== name
)
223 return (shell_flags
[i
].value
);
225 return (FLAG_UNKNOWN
);
228 /* Change the state of a flag, and return it's original value, or return
229 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
230 FLAG_ON or FLAG_OFF. */
232 change_flag (flag
, on_or_off
)
236 int *value
, old_value
;
238 #if defined (RESTRICTED_SHELL)
239 /* Don't allow "set +r" in a shell which is `restricted'. */
240 if (restricted
&& flag
== 'r' && on_or_off
== FLAG_OFF
)
242 #endif /* RESTRICTED_SHELL */
244 value
= find_flag (flag
);
246 if ((value
== (int *)FLAG_UNKNOWN
) || (on_or_off
!= FLAG_ON
&& on_or_off
!= FLAG_OFF
))
250 *value
= (on_or_off
== FLAG_ON
) ? 1 : 0;
252 /* Special cases for a few flags. */
255 #if defined (BANG_HISTORY)
257 history_expansion
= histexp_flag
;
258 if (on_or_off
== FLAG_ON
)
259 bush_initialize_history ();
263 #if defined (JOB_CONTROL)
265 set_job_control (on_or_off
== FLAG_ON
);
267 #endif /* JOB_CONTROL */
270 if (builtin_ignoring_errexit
== 0)
271 exit_immediately_on_error
= errexit_flag
;
275 if (interactive_shell
)
276 read_but_dont_execute
= 0;
280 if (on_or_off
== FLAG_OFF
)
281 disable_priv_mode ();
284 #if defined (RESTRICTED_SHELL)
286 if (on_or_off
== FLAG_ON
&& shell_initialized
)
287 maybe_make_restricted (shell_name
);
292 echo_input_at_read
= verbose_flag
;
299 /* Return a string which is the names of all the currently
307 temp
= (char *)xmalloc (1 + NUM_SHELL_FLAGS
+ read_from_stdin
+ want_pending_command
);
308 for (i
= string_index
= 0; shell_flags
[i
].name
; i
++)
309 if (*(shell_flags
[i
].value
))
310 temp
[string_index
++] = shell_flags
[i
].name
;
312 if (want_pending_command
)
313 temp
[string_index
++] = 'c';
315 temp
[string_index
++] = 's';
317 temp
[string_index
] = '\0';
327 temp
= (char *)xmalloc (1 + NUM_SHELL_FLAGS
);
328 for (i
= 0; shell_flags
[i
].name
; i
++)
329 temp
[i
] = *(shell_flags
[i
].value
);
335 set_current_flags (bitmap
)
342 for (i
= 0; shell_flags
[i
].name
; i
++)
343 *(shell_flags
[i
].value
) = bitmap
[i
];
349 mark_modified_vars
= disallow_filename_globbing
= 0;
350 place_keywords_in_env
= read_but_dont_execute
= just_one_command
= 0;
351 noclobber
= unbound_vars_is_error
= 0;
352 debug_info
= echo_command_at_execute
= jobs_m_flag
= forced_interactive
= 0;
353 no_symbolic_links
= 0;
354 privileged_mode
= pipefail_opt
= 0;
356 error_trace_mode
= function_trace_mode
= 0;
358 exit_immediately_on_error
= errexit_flag
= 0;
359 echo_input_at_read
= verbose_flag
= 0;
361 hashing_enabled
= interactive_comments
= 1;
363 #if defined (JOB_CONTROL)
364 asynchronous_notification
= 0;
367 #if defined (BANG_HISTORY)
371 #if defined (BRACE_EXPANSION)
375 #if defined (RESTRICTED_SHELL)
385 for (i
= 0; shell_flags
[i
].name
; i
++)
386 optflags
[i
+1] = shell_flags
[i
].name
;
389 optflags
[i
+1] = '\0';