]> git.ipfire.org Git - thirdparty/bash.git/blob - flags.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / flags.c
1 /* flags.c -- Everything about flags except the `set' command. That
2 is in builtins.c */
3
4 /* Copyright (C) 1987,1989 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 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
11 version.
12
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
16 for more details.
17
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. */
21
22 /* Flags hacking. */
23 #include "config.h"
24 #if defined (HAVE_UNISTD_H)
25 # include <unistd.h>
26 #endif
27
28 #include "shell.h"
29 #include "flags.h"
30
31 #if defined (BANG_HISTORY)
32 # include "bashhist.h"
33 #endif
34
35 #if defined (JOB_CONTROL)
36 extern int set_job_control __P((int));
37 #endif
38
39 #if defined (RESTRICTED_SHELL)
40 extern char *shell_name;
41 #endif
42
43 /* -c, -s invocation options -- not really flags, but they show up in $- */
44 extern int want_pending_command, read_from_stdin;
45
46 /* **************************************************************** */
47 /* */
48 /* The Standard Sh Flags. */
49 /* */
50 /* **************************************************************** */
51
52 /* Non-zero means automatically mark variables which are modified or created
53 as auto export variables. */
54 int mark_modified_vars = 0;
55
56 /* Non-zero causes asynchronous job notification. Otherwise, job state
57 notification only takes place just before a primary prompt is printed. */
58 int asynchronous_notification = 0;
59
60 /* Non-zero means exit immediately if a command exits with a non-zero
61 exit status. */
62 int exit_immediately_on_error = 0;
63
64 /* Non-zero means disable filename globbing. */
65 int disallow_filename_globbing = 0;
66
67 /* Non-zero means that all keyword arguments are placed into the environment
68 for a command, not just those that appear on the line before the command
69 name. */
70 int place_keywords_in_env = 0;
71
72 /* Non-zero means read commands, but don't execute them. This is useful
73 for debugging shell scripts that should do something hairy and possibly
74 destructive. */
75 int read_but_dont_execute = 0;
76
77 /* Non-zero means end of file is after one command. */
78 int just_one_command = 0;
79
80 /* Non-zero means don't overwrite existing files while doing redirections. */
81 int noclobber = 0;
82
83 /* Non-zero means trying to get the value of $i where $i is undefined
84 causes an error, instead of a null substitution. */
85 int unbound_vars_is_error = 0;
86
87 /* Non-zero means type out input lines after you read them. */
88 int echo_input_at_read = 0;
89
90 /* Non-zero means type out the command definition after reading, but
91 before executing. */
92 int echo_command_at_execute = 0;
93
94 /* Non-zero means turn on the job control features. */
95 int jobs_m_flag = 0;
96
97 /* Non-zero means this shell is interactive, even if running under a
98 pipe. */
99 int forced_interactive = 0;
100
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;
107
108 /* **************************************************************** */
109 /* */
110 /* Non-Standard Flags Follow Here. */
111 /* */
112 /* **************************************************************** */
113
114 #if 0
115 /* Non-zero means do lexical scoping in the body of a FOR command. */
116 int lexical_scoping = 0;
117 #endif
118
119 /* Non-zero means no such thing as invisible variables. */
120 int no_invisible_vars = 0;
121
122 /* Non-zero means look up and remember command names in a hash table, */
123 int hashing_enabled = 1;
124
125 #if defined (BANG_HISTORY)
126 /* Non-zero means that we are doing history expansion. The default.
127 This means !22 gets the 22nd line of history. */
128 int history_expansion = 1;
129 #endif /* BANG_HISTORY */
130
131 /* Non-zero means that we allow comments to appear in interactive commands. */
132 int interactive_comments = 1;
133
134 #if defined (RESTRICTED_SHELL)
135 /* Non-zero means that this shell is `restricted'. A restricted shell
136 disallows: changing directories, command or path names containing `/',
137 unsetting or resetting the values of $PATH and $SHELL, and any type of
138 output redirection. */
139 int restricted = 0; /* currently restricted */
140 int restricted_shell = 0; /* shell was started in restricted mode. */
141 #endif /* RESTRICTED_SHELL */
142
143 /* Non-zero means that this shell is running in `privileged' mode. This
144 is required if the shell is to run setuid. If the `-p' option is
145 not supplied at startup, and the real and effective uids or gids
146 differ, disable_priv_mode is called to relinquish setuid status. */
147 int privileged_mode = 0;
148
149 #if defined (BRACE_EXPANSION)
150 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
151 int brace_expansion = 1;
152 #endif
153
154 /* **************************************************************** */
155 /* */
156 /* The Flags ALIST. */
157 /* */
158 /* **************************************************************** */
159
160 struct flags_alist shell_flags[] = {
161 /* Standard sh flags. */
162 { 'a', &mark_modified_vars },
163 #if defined (JOB_CONTROL)
164 { 'b', &asynchronous_notification },
165 #endif /* JOB_CONTROL */
166 { 'e', &exit_immediately_on_error },
167 { 'f', &disallow_filename_globbing },
168 { 'h', &hashing_enabled },
169 { 'i', &forced_interactive },
170 { 'k', &place_keywords_in_env },
171 #if defined (JOB_CONTROL)
172 { 'm', &jobs_m_flag },
173 #endif /* JOB_CONTROL */
174 { 'n', &read_but_dont_execute },
175 { 'p', &privileged_mode },
176 #if defined (RESTRICTED_SHELL)
177 { 'r', &restricted },
178 #endif /* RESTRICTED_SHELL */
179 { 't', &just_one_command },
180 { 'u', &unbound_vars_is_error },
181 { 'v', &echo_input_at_read },
182 { 'x', &echo_command_at_execute },
183 { 'C', &noclobber },
184
185 /* New flags that control non-standard things. */
186 #if 0
187 { 'l', &lexical_scoping },
188 #endif
189 { 'I', &no_invisible_vars },
190
191 { 'P', &no_symbolic_links },
192
193 #if defined (BRACE_EXPANSION)
194 { 'B', &brace_expansion },
195 #endif
196
197 #if defined (BANG_HISTORY)
198 { 'H', &history_expansion },
199 #endif /* BANG_HISTORY */
200
201 {0, (int *)NULL}
202 };
203
204 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
205
206 char optflags[NUM_SHELL_FLAGS+4] = { '+' };
207
208 int *
209 find_flag (name)
210 int name;
211 {
212 int i;
213 for (i = 0; shell_flags[i].name; i++)
214 {
215 if (shell_flags[i].name == name)
216 return (shell_flags[i].value);
217 }
218 return (FLAG_UNKNOWN);
219 }
220
221 /* Change the state of a flag, and return it's original value, or return
222 FLAG_ERROR if there is no flag FLAG. ON_OR_OFF must be either
223 FLAG_ON or FLAG_OFF. */
224 int
225 change_flag (flag, on_or_off)
226 int flag;
227 int on_or_off;
228 {
229 int *value, old_value;
230
231 #if defined (RESTRICTED_SHELL)
232 /* Don't allow "set +r" in a shell which is `restricted'. */
233 if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
234 return (FLAG_ERROR);
235 #endif /* RESTRICTED_SHELL */
236
237 value = find_flag (flag);
238
239 if ((value == (int *)FLAG_UNKNOWN) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
240 return (FLAG_ERROR);
241
242 old_value = *value;
243
244 *value = (on_or_off == FLAG_ON) ? 1 : 0;
245
246 /* Special cases for a few flags. */
247 switch (flag)
248 {
249 #if defined (BANG_HISTORY)
250 case 'H':
251 if (on_or_off == FLAG_ON)
252 bash_initialize_history ();
253 break;
254 #endif
255
256 #if defined (JOB_CONTROL)
257 case 'm':
258 set_job_control (on_or_off == FLAG_ON);
259 break;
260 #endif /* JOB_CONTROL */
261
262 case 'n':
263 if (interactive_shell)
264 read_but_dont_execute = 0;
265 break;
266
267 case 'p':
268 if (on_or_off == FLAG_OFF)
269 disable_priv_mode ();
270 break;
271
272 #if defined (RESTRICTED_SHELL)
273 case 'r':
274 if (on_or_off == FLAG_ON)
275 maybe_make_restricted (shell_name);
276 break;
277 #endif
278
279 }
280
281 return (old_value);
282 }
283
284 /* Return a string which is the names of all the currently
285 set shell flags. */
286 char *
287 which_set_flags ()
288 {
289 char *temp;
290 int i, string_index;
291
292 temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
293 for (i = string_index = 0; shell_flags[i].name; i++)
294 if (*(shell_flags[i].value))
295 temp[string_index++] = shell_flags[i].name;
296
297 if (want_pending_command)
298 temp[string_index++] = 'c';
299 if (read_from_stdin)
300 temp[string_index++] = 's';
301
302 temp[string_index] = '\0';
303 return (temp);
304 }
305
306 void
307 reset_shell_flags ()
308 {
309 mark_modified_vars = exit_immediately_on_error = disallow_filename_globbing = 0;
310 place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
311 noclobber = unbound_vars_is_error = echo_input_at_read = 0;
312 echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
313 no_symbolic_links = no_invisible_vars = privileged_mode = 0;
314
315 hashing_enabled = interactive_comments = 1;
316
317 #if defined (JOB_CONTROL)
318 asynchronous_notification = 0;
319 #endif
320
321 #if defined (BANG_HISTORY)
322 history_expansion = 1;
323 #endif
324
325 #if defined (BRACE_EXPANSION)
326 brace_expansion = 1;
327 #endif
328
329 #if defined (RESTRICTED_SHELL)
330 restricted = 0;
331 #endif
332 }
333
334 void
335 initialize_flags ()
336 {
337 register int i;
338
339 for (i = 0; shell_flags[i].name; i++)
340 optflags[i+1] = shell_flags[i].name;
341 optflags[++i] = 'o';
342 optflags[++i] = ';';
343 optflags[i+1] = '\0';
344 }