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