1 /* flags.c -- Everything about flags except the `set' command. That
4 /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #if defined (HAVE_UNISTD_H)
31 #if defined (JOB_CONTROL)
32 extern int set_job_control ();
35 #if defined (RESTRICTED_SHELL)
36 extern char *shell_name;
39 /* -c, -s invocation options -- not really flags, but they show up in $- */
40 extern int want_pending_command, read_from_stdin;
42 /* **************************************************************** */
44 /* The Standard Sh Flags. */
46 /* **************************************************************** */
48 /* Non-zero means automatically mark variables which are modified or created
49 as auto export variables. */
50 int mark_modified_vars = 0;
52 /* Non-zero causes asynchronous job notification. Otherwise, job state
53 notification only takes place just before a primary prompt is printed. */
54 int asynchronous_notification = 0;
56 /* Non-zero means exit immediately if a command exits with a non-zero
58 int exit_immediately_on_error = 0;
60 /* Non-zero means disable filename globbing. */
61 int disallow_filename_globbing = 0;
63 /* Non-zero means that all keyword arguments are placed into the environment
64 for a command, not just those that appear on the line before the command
66 int place_keywords_in_env = 0;
68 /* Non-zero means read commands, but don't execute them. This is useful
69 for debugging shell scripts that should do something hairy and possibly
71 int read_but_dont_execute = 0;
73 /* Non-zero means end of file is after one command. */
74 int just_one_command = 0;
76 /* Non-zero means don't overwrite existing files while doing redirections. */
79 /* Non-zero means trying to get the value of $i where $i is undefined
80 causes an error, instead of a null substitution. */
81 int unbound_vars_is_error = 0;
83 /* Non-zero means type out input lines after you read them. */
84 int echo_input_at_read = 0;
86 /* Non-zero means type out the command definition after reading, but
88 int echo_command_at_execute = 0;
90 /* Non-zero means turn on the job control features. */
93 /* Non-zero means this shell is interactive, even if running under a
95 int forced_interactive = 0;
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
102 int no_symbolic_links = 0;
104 /* **************************************************************** */
106 /* Non-Standard Flags Follow Here. */
108 /* **************************************************************** */
111 /* Non-zero means do lexical scoping in the body of a FOR command. */
112 int lexical_scoping = 0;
115 /* Non-zero means no such thing as invisible variables. */
116 int no_invisible_vars = 0;
118 /* Non-zero means look up and remember command names in a hash table, */
119 int hashing_enabled = 1;
121 #if defined (BANG_HISTORY)
122 /* Non-zero means that we are doing history expansion. The default.
123 This means !22 gets the 22nd line of history. */
124 int history_expansion = 1;
125 #endif /* BANG_HISTORY */
127 /* Non-zero means that we allow comments to appear in interactive commands. */
128 int interactive_comments = 1;
130 #if defined (RESTRICTED_SHELL)
131 /* Non-zero means that this shell is `restricted'. A restricted shell
132 disallows: changing directories, command or path names containing `/',
133 unsetting or resetting the values of $PATH and $SHELL, and any type of
134 output redirection. */
135 int restricted = 0; /* currently restricted */
136 int restricted_shell = 0; /* shell was started in restricted mode. */
137 #endif /* RESTRICTED_SHELL */
139 /* Non-zero means that this shell is running in `privileged' mode. This
140 is required if the shell is to run setuid. If the `-p' option is
141 not supplied at startup, and the real and effective uids or gids
142 differ, disable_priv_mode is called to relinquish setuid status. */
143 int privileged_mode = 0;
145 #if defined (BRACE_EXPANSION)
146 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
147 int brace_expansion = 1;
150 /* **************************************************************** */
152 /* The Flags ALIST. */
154 /* **************************************************************** */
156 struct flags_alist shell_flags[] = {
157 /* Standard sh flags. */
158 { 'a', &mark_modified_vars },
159 #if defined (JOB_CONTROL)
160 { 'b', &asynchronous_notification },
161 #endif /* JOB_CONTROL */
162 { 'e', &exit_immediately_on_error },
163 { 'f', &disallow_filename_globbing },
164 { 'h', &hashing_enabled },
165 { 'i', &forced_interactive },
166 { 'k', &place_keywords_in_env },
167 #if defined (JOB_CONTROL)
168 { 'm', &jobs_m_flag },
169 #endif /* JOB_CONTROL */
170 { 'n', &read_but_dont_execute },
171 { 'p', &privileged_mode },
172 #if defined (RESTRICTED_SHELL)
173 { 'r', &restricted },
174 #endif /* RESTRICTED_SHELL */
175 { 't', &just_one_command },
176 { 'u', &unbound_vars_is_error },
177 { 'v', &echo_input_at_read },
178 { 'x', &echo_command_at_execute },
181 /* New flags that control non-standard things. */
183 { 'l', &lexical_scoping },
185 { 'I', &no_invisible_vars },
187 { 'P', &no_symbolic_links },
189 #if defined (BRACE_EXPANSION)
190 { 'B', &brace_expansion },
193 #if defined (BANG_HISTORY)
194 { 'H', &history_expansion },
195 #endif /* BANG_HISTORY */
200 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
207 for (i = 0; shell_flags[i].name; i++)
209 if (shell_flags[i].name == name)
210 return (shell_flags[i].value);
212 return (FLAG_UNKNOWN);
215 /* Change the state of a flag, and return it's original value, or return
216 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
217 FLAG_ON or FLAG_OFF. */
219 change_flag (flag, on_or_off)
223 int *value, old_value;
225 #if defined (RESTRICTED_SHELL)
226 /* Don't allow "set +r" in a shell which is `restricted'. */
227 if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
229 #endif /* RESTRICTED_SHELL */
231 value = find_flag (flag);
233 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
238 *value = (on_or_off == FLAG_ON) ? 1 : 0;
240 /* Special cases for a few flags. */
243 #if defined (JOB_CONTROL)
245 set_job_control (on_or_off == FLAG_ON);
247 #endif /* JOB_CONTROL */
249 #if defined (RESTRICTED_SHELL)
251 if (on_or_off == FLAG_ON)
252 maybe_make_restricted (shell_name);
256 #if defined (BANG_HISTORY)
258 if (on_or_off == FLAG_ON)
259 bash_initialize_history ();
264 if (on_or_off == FLAG_OFF)
265 disable_priv_mode ();
273 /* Return a string which is the names of all the currently
281 temp = xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
282 for (i = string_index = 0; shell_flags[i].name; i++)
283 if (*(shell_flags[i].value))
284 temp[string_index++] = shell_flags[i].name;
286 if (want_pending_command)
287 temp[string_index++] = 'c';
289 temp[string_index++] = 's';
291 temp[string_index] = '\0';
298 mark_modified_vars = exit_immediately_on_error = disallow_filename_globbing = 0;
299 place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
300 noclobber = unbound_vars_is_error = echo_input_at_read = 0;
301 echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
302 no_symbolic_links = no_invisible_vars = privileged_mode = 0;
304 hashing_enabled = interactive_comments = 1;
306 #if defined (JOB_CONTROL)
307 asynchronous_notification = 0;
310 #if defined (BANG_HISTORY)
311 history_expansion = 1;
314 #if defined (BRACE_EXPANSION)
318 #if defined (RESTRICTED_SHELL)