Imported from ../bash-2.02.tar.gz.
[platform/upstream/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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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 (JOB_CONTROL)
32 extern int set_job_control ();
33 #endif
34
35 #if defined (RESTRICTED_SHELL)
36 extern char *shell_name;
37 #endif
38
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. */
47 int 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. */
51 int asynchronous_notification = 0;
52
53 /* Non-zero means exit immediately if a command exits with a non-zero
54    exit status. */
55 int exit_immediately_on_error = 0;
56
57 /* Non-zero means disable filename globbing. */
58 int disallow_filename_globbing = 0;
59
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. */
63 int place_keywords_in_env = 0;
64
65 /* Non-zero means read commands, but don't execute them.  This is useful
66    for debugging shell scripts that should do something hairy and possibly
67    desctructive. */
68 int read_but_dont_execute = 0;
69
70 /* Non-zero means end of file is after one command. */
71 int just_one_command = 0;
72
73 /* Non-zero means don't overwrite existing files while doing redirections. */
74 int 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. */
78 int unbound_vars_is_error = 0;
79
80 /* Non-zero means type out input lines after you read them. */
81 int echo_input_at_read = 0;
82
83 /* Non-zero means type out the command definition after reading, but
84    before executing. */
85 int echo_command_at_execute = 0;
86
87 /* Non-zero means turn on the job control features. */
88 int jobs_m_flag = 0;
89
90 /* Non-zero means this shell is interactive, even if running under a
91    pipe. */
92 int 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. */
99 int no_symbolic_links = 0;
100
101 /* **************************************************************** */
102 /*                                                                  */
103 /*                   Non-Standard Flags Follow Here.                */
104 /*                                                                  */
105 /* **************************************************************** */
106
107 #if 0
108 /* Non-zero means do lexical scoping in the body of a FOR command. */
109 int lexical_scoping = 0;
110 #endif
111
112 /* Non-zero means no such thing as invisible variables. */
113 int no_invisible_vars = 0;
114
115 /* Non-zero means look up and remember command names in a hash table, */
116 int hashing_enabled = 1;
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. */
121 int history_expansion = 1;
122 #endif /* BANG_HISTORY */
123
124 /* Non-zero means that we allow comments to appear in interactive commands. */
125 int interactive_comments = 1;
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. */
132 int restricted = 0;
133 #endif /* RESTRICTED_SHELL */
134
135 /* Non-zero means that this shell is running in `privileged' mode.  This
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. */
139 int privileged_mode = 0;
140
141 #if defined (BRACE_EXPANSION)
142 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
143 int brace_expansion = 1;
144 #endif
145
146 /* **************************************************************** */
147 /*                                                                  */
148 /*                      The Flags ALIST.                            */
149 /*                                                                  */
150 /* **************************************************************** */
151
152 struct 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 },
160   { 'h', &hashing_enabled },
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. */
178 #if 0
179   { 'l', &lexical_scoping },
180 #endif
181   { 'I', &no_invisible_vars },
182
183   { 'P', &no_symbolic_links },
184
185 #if defined (BRACE_EXPANSION)
186   { 'B', &brace_expansion },
187 #endif
188
189 #if defined (BANG_HISTORY)
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
198 int *
199 find_flag (name)
200      int name;
201 {
202   int i;
203   for (i = 0; shell_flags[i].name; i++)
204     {
205       if (shell_flags[i].name == name)
206         return (shell_flags[i].value);
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 FLAG.  ON_OR_OFF must be either
213    FLAG_ON or FLAG_OFF. */
214 int
215 change_flag (flag, on_or_off)
216   int flag;
217   int on_or_off;
218 {
219   int *value, old_value;
220
221   value = find_flag (flag);
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) || (on_or_off != FLAG_ON && on_or_off != FLAG_OFF))
230     return (FLAG_ERROR);
231
232   old_value = *value;
233
234   *value = (on_or_off == FLAG_ON) ? 1 : 0;
235
236   /* Special cases for a few flags. */
237   switch (flag)
238     {
239 #if defined (JOB_CONTROL)
240     case 'm':
241       set_job_control (on_or_off == FLAG_ON);
242       break;
243 #endif /* JOB_CONTROL */
244
245 #if defined (RESTRICTED_SHELL)
246     case 'r':
247       if (on_or_off == FLAG_ON)
248         maybe_make_restricted (shell_name);
249       break;
250 #endif
251
252 #if defined (BANG_HISTORY)
253     case 'H':
254       if (on_or_off == FLAG_ON)
255         bash_initialize_history ();
256       break;
257 #endif
258
259     case 'p':
260       if (on_or_off == FLAG_OFF)
261         disable_priv_mode ();
262
263       break;
264     }
265
266   return (old_value);
267 }
268
269 /* Return a string which is the names of all the currently
270    set shell flags. */
271 char *
272 which_set_flags ()
273 {
274   char *temp;
275   int i, string_index;
276
277   temp = xmalloc (1 + NUM_SHELL_FLAGS);
278   for (i = string_index = 0; shell_flags[i].name; i++)
279     if (*(shell_flags[i].value))
280       temp[string_index++] = shell_flags[i].name;
281
282   temp[string_index] = '\0';
283   return (temp);
284 }
285
286 void
287 reset_shell_flags ()
288 {
289   mark_modified_vars = exit_immediately_on_error = disallow_filename_globbing = 0;
290   place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
291   noclobber = unbound_vars_is_error = echo_input_at_read = 0;
292   echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
293   no_symbolic_links = no_invisible_vars = privileged_mode = 0;
294
295   hashing_enabled = interactive_comments = 1;
296
297 #if defined (JOB_CONTROL)
298   asynchronous_notification = 0;
299 #endif
300
301 #if defined (BANG_HISTORY)
302   history_expansion = 1;
303 #endif
304
305 #if defined (BRACE_EXPANSION)
306   brace_expansion = 1;
307 #endif
308
309 #if defined (RESTRICTED_SHELL)
310   restricted = 0;
311 #endif
312 }