improve of cmpl.
[bush.git] / src / flags.c
blob4d72134a6d6050c5cb79e4b5b2ccc27f44bf0197
1 /* flags.c -- Everything about flags except the `set' command. That
2 is in builtins.c */
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/>.
22 #include "config.h"
23 #if defined (HAVE_UNISTD_H)
24 # include <unistd.h>
25 #endif
27 #include "shell.h"
28 #include "runner/execute_cmd.h"
29 #include "flags.h"
31 #if defined (BANG_HISTORY)
32 # include "bushhist.h"
33 #endif
35 #if defined (JOB_CONTROL)
36 extern int set_job_control PARAMS((int));
37 #endif
39 /* **************************************************************** */
40 /* */
41 /* The Standard sh Flags. */
42 /* */
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. */
55 int debug_info = 0;
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. */
60 int errexit_flag = 0;
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
68 name. */
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
73 destructive. */
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. */
80 int noclobber = 0;
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;
88 int verbose_flag = 0;
90 /* Non-zero means type out the command definition after reading, but
91 before executing. */
92 int echo_command_at_execute = 0;
94 /* Non-zero means turn on the job control features. */
95 int jobs_m_flag = 0;
97 /* Non-zero means this shell is interactive, even if running under a
98 pipe. */
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
105 this flag. */
106 int no_symbolic_links = 0;
108 /* **************************************************************** */
109 /* */
110 /* Non-Standard Flags Follow Here. */
111 /* */
112 /* **************************************************************** */
114 #if 0
115 /* Non-zero means do lexical scoping in the body of a FOR command. */
116 int lexical_scoping = 0;
117 #endif
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;
150 #endif
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 /* **************************************************************** */
164 /* */
165 /* The Flags ALIST. */
166 /* */
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. */
195 #if 0
196 { 'l', &lexical_scoping },
197 #endif
198 #if defined (BRACE_EXPANSION)
199 { 'B', &brace_expansion },
200 #endif
201 { 'C', &noclobber },
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 },
208 {0, (int *)NULL}
211 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
213 char optflags[NUM_SHELL_FLAGS+4] = { '+' };
215 int *
216 find_flag (name)
217 int name;
219 int i;
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)
233 int flag;
234 int 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)
241 return (FLAG_ERROR);
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))
247 return (FLAG_ERROR);
249 old_value = *value;
250 *value = (on_or_off == FLAG_ON) ? 1 : 0;
252 /* Special cases for a few flags. */
253 switch (flag)
255 #if defined (BANG_HISTORY)
256 case 'H':
257 history_expansion = histexp_flag;
258 if (on_or_off == FLAG_ON)
259 bush_initialize_history ();
260 break;
261 #endif
263 #if defined (JOB_CONTROL)
264 case 'm':
265 set_job_control (on_or_off == FLAG_ON);
266 break;
267 #endif /* JOB_CONTROL */
269 case 'e':
270 if (builtin_ignoring_errexit == 0)
271 exit_immediately_on_error = errexit_flag;
272 break;
274 case 'n':
275 if (interactive_shell)
276 read_but_dont_execute = 0;
277 break;
279 case 'p':
280 if (on_or_off == FLAG_OFF)
281 disable_priv_mode ();
282 break;
284 #if defined (RESTRICTED_SHELL)
285 case 'r':
286 if (on_or_off == FLAG_ON && shell_initialized)
287 maybe_make_restricted (shell_name);
288 break;
289 #endif
291 case 'v':
292 echo_input_at_read = verbose_flag;
293 break;
296 return (old_value);
299 /* Return a string which is the names of all the currently
300 set shell flags. */
301 char *
302 which_set_flags ()
304 char *temp;
305 int i, string_index;
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';
314 if (read_from_stdin)
315 temp[string_index++] = 's';
317 temp[string_index] = '\0';
318 return (temp);
321 char *
322 get_current_flags ()
324 char *temp;
325 int i;
327 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS);
328 for (i = 0; shell_flags[i].name; i++)
329 temp[i] = *(shell_flags[i].value);
330 temp[i] = '\0';
331 return (temp);
334 void
335 set_current_flags (bitmap)
336 const char *bitmap;
338 int i;
340 if (bitmap == 0)
341 return;
342 for (i = 0; shell_flags[i].name; i++)
343 *(shell_flags[i].value) = bitmap[i];
346 void
347 reset_shell_flags ()
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;
365 #endif
367 #if defined (BANG_HISTORY)
368 histexp_flag = 0;
369 #endif
371 #if defined (BRACE_EXPANSION)
372 brace_expansion = 1;
373 #endif
375 #if defined (RESTRICTED_SHELL)
376 restricted = 0;
377 #endif
380 void
381 initialize_flags ()
383 register int i;
385 for (i = 0; shell_flags[i].name; i++)
386 optflags[i+1] = shell_flags[i].name;
387 optflags[++i] = 'o';
388 optflags[++i] = ';';
389 optflags[i+1] = '\0';