Imported from ../bash-2.0.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 /* **************************************************************** */
36 /*                                                                  */
37 /*                      The Standard Sh Flags.                      */
38 /*                                                                  */
39 /* **************************************************************** */
40
41 /* Non-zero means automatically mark variables which are modified or created
42    as auto export variables. */
43 int mark_modified_vars = 0;
44
45 /* Non-zero causes asynchronous job notification.  Otherwise, job state
46    notification only takes place just before a primary prompt is printed. */
47 int asynchronous_notification = 0;
48
49 /* Non-zero means exit immediately if a command exits with a non-zero
50    exit status. */
51 int exit_immediately_on_error = 0;
52
53 /* Non-zero means disable filename globbing. */
54 int disallow_filename_globbing = 0;
55
56 /* Non-zero means that all keyword arguments are placed into the environment
57    for a command, not just those that appear on the line before the command
58    name. */
59 int place_keywords_in_env = 0;
60
61 /* Non-zero means read commands, but don't execute them.  This is useful
62    for debugging shell scripts that should do something hairy and possibly
63    desctructive. */
64 int read_but_dont_execute = 0;
65
66 /* Non-zero means end of file is after one command. */
67 int just_one_command = 0;
68
69 /* Non-zero means don't overwrite existing files while doing redirections. */
70 int noclobber = 0;
71
72 /* Non-zero means trying to get the value of $i where $i is undefined
73    causes an error, instead of a null substitution. */
74 int unbound_vars_is_error = 0;
75
76 /* Non-zero means type out input lines after you read them. */
77 int echo_input_at_read = 0;
78
79 /* Non-zero means type out the command definition after reading, but
80    before executing. */
81 int echo_command_at_execute = 0;
82
83 /* Non-zero means turn on the job control features. */
84 int jobs_m_flag = 0;
85
86 /* Non-zero means this shell is interactive, even if running under a
87    pipe. */
88 int forced_interactive = 0;
89
90 /* By default, follow the symbolic links as if they were real directories
91    while hacking the `cd' command.  This means that `cd ..' moves up in
92    the string of symbolic links that make up the current directory, instead
93    of the absolute directory.  The shell variable `nolinks' also controls
94    this flag. */
95 int no_symbolic_links = 0;
96
97 /* **************************************************************** */
98 /*                                                                  */
99 /*                   Non-Standard Flags Follow Here.                */
100 /*                                                                  */
101 /* **************************************************************** */
102
103 #if 0
104 /* Non-zero means do lexical scoping in the body of a FOR command. */
105 int lexical_scoping = 0;
106 #endif
107
108 /* Non-zero means no such thing as invisible variables. */
109 int no_invisible_vars = 0;
110
111 /* Non-zero means look up and remember command names in a hash table, */
112 int hashing_enabled = 1;
113
114 #if defined (BANG_HISTORY)
115 /* Non-zero means that we are doing history expansion.  The default.
116    This means !22 gets the 22nd line of history. */
117 int history_expansion = 1;
118 #endif /* BANG_HISTORY */
119
120 /* Non-zero means that we allow comments to appear in interactive commands. */
121 int interactive_comments = 1;
122
123 #if defined (RESTRICTED_SHELL)
124 /* Non-zero means that this shell is `restricted'.  A restricted shell
125    disallows: changing directories, command or path names containing `/',
126    unsetting or resetting the values of $PATH and $SHELL, and any type of
127    output redirection. */
128 int restricted = 0;
129 #endif /* RESTRICTED_SHELL */
130
131 /* Non-zero means that this shell is running in `privileged' mode.  This
132    mode is entered on startup if the real and effective uids or gids
133    differ. */
134 int privileged_mode = 0;
135
136 #if defined (BRACE_EXPANSION)
137 /* Zero means to disable brace expansion: foo{a,b} -> fooa foob */
138 int brace_expansion = 1;
139 #endif
140
141 /* **************************************************************** */
142 /*                                                                  */
143 /*                      The Flags ALIST.                            */
144 /*                                                                  */
145 /* **************************************************************** */
146
147 struct flags_alist shell_flags[] = {
148   /* Standard sh flags. */
149   { 'a', &mark_modified_vars },
150 #if defined (JOB_CONTROL)
151   { 'b', &asynchronous_notification },
152 #endif /* JOB_CONTROL */
153   { 'e', &exit_immediately_on_error },
154   { 'f', &disallow_filename_globbing },
155   { 'h', &hashing_enabled },
156   { 'i', &forced_interactive },
157   { 'k', &place_keywords_in_env },
158 #if defined (JOB_CONTROL)
159   { 'm', &jobs_m_flag },
160 #endif /* JOB_CONTROL */
161   { 'n', &read_but_dont_execute },
162   { 'p', &privileged_mode },
163 #if defined (RESTRICTED_SHELL)
164   { 'r', &restricted },
165 #endif /* RESTRICTED_SHELL */
166   { 't', &just_one_command },
167   { 'u', &unbound_vars_is_error },
168   { 'v', &echo_input_at_read },
169   { 'x', &echo_command_at_execute },
170   { 'C', &noclobber },
171
172   /* New flags that control non-standard things. */
173 #if 0
174   { 'l', &lexical_scoping },
175 #endif
176   { 'I', &no_invisible_vars },
177
178   { 'P', &no_symbolic_links },
179
180 #if defined (BRACE_EXPANSION)
181   { 'B', &brace_expansion },
182 #endif
183
184 #if defined (BANG_HISTORY)
185   { 'H', &history_expansion },
186 #endif /* BANG_HISTORY */
187
188   {0, (int *)NULL}
189 };
190
191 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
192
193 int *
194 find_flag (name)
195      int name;
196 {
197   int i;
198   for (i = 0; shell_flags[i].name; i++)
199     {
200       if (shell_flags[i].name == name)
201         return (shell_flags[i].value);
202     }
203   return (FLAG_UNKNOWN);
204 }
205
206 /* Change the state of a flag, and return it's original value, or return
207    FLAG_ERROR if there is no flag called NAME.  ON_OR_OFF should be one
208    of FLAG_ON or FLAG_OFF. */
209 int
210 change_flag (flag, on_or_off)
211   int flag;
212   int on_or_off;
213 {
214   int *value, old_value;
215
216   value = find_flag (flag);
217
218 #if defined (RESTRICTED_SHELL)
219   /* Don't allow "set +r" in a shell which is `restricted'. */
220   if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
221     return (FLAG_ERROR);
222 #endif /* RESTRICTED_SHELL */
223
224   if (value == (int *)FLAG_UNKNOWN)
225     return (FLAG_ERROR);
226
227   old_value = *value;
228
229   if (on_or_off == FLAG_ON)
230     *value = 1;
231   else if (on_or_off == FLAG_OFF)
232     *value = 0;
233   else
234     return (FLAG_ERROR);
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 == '-');
242       break;
243 #endif /* JOB_CONTROL */
244
245     case 'p':
246       if (on_or_off == '+')
247         disable_priv_mode ();
248
249       break;
250     }
251
252   return (old_value);
253 }
254
255 /* Return a string which is the names of all the currently
256    set shell flags. */
257 char *
258 which_set_flags ()
259 {
260   char *temp;
261   int i, string_index;
262
263   temp = xmalloc (1 + NUM_SHELL_FLAGS);
264   for (i = string_index = 0; shell_flags[i].name; i++)
265     if (*(shell_flags[i].value))
266       temp[string_index++] = shell_flags[i].name;
267
268   temp[string_index] = '\0';
269   return (temp);
270 }