Imported from ../bash-1.14.7.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
24 #include "shell.h"
25 #include "flags.h"
26
27 /* **************************************************************** */
28 /*                                                                  */
29 /*                      The Standard Sh Flags.                      */
30 /*                                                                  */
31 /* **************************************************************** */
32
33 /* Non-zero means automatically mark variables which are modified or created
34    as auto export variables. */
35 int mark_modified_vars = 0;
36
37 /* Non-zero causes asynchronous job notification.  Otherwise, job state
38    notification only takes place just before a primary prompt is printed. */
39 int asynchronous_notification = 0;
40
41 /* Non-zero means exit immediately if a command exits with a non-zero
42    exit status. */
43 int exit_immediately_on_error = 0;
44
45 /* Non-zero means disable filename globbing. */
46 int disallow_filename_globbing = 0;
47
48 /* Non-zero means to locate and remember function commands as functions are
49    defined.  Function commands are normally located when the function is
50    executed. */
51 int locate_commands_in_functions = 0;
52
53 /* Non-zero means that all keyword arguments are placed into the environment
54    for a command, not just those that appear on the line before the command
55    name. */
56 int place_keywords_in_env = 0;
57
58 /* Non-zero means read commands, but don't execute tham.  This is useful
59    for debugging shell scripts that should do something hairy and possibly
60    desctructive. */
61 int read_but_dont_execute = 0;
62
63 /* Non-zero means end of file is after one command. */
64 int just_one_command = 0;
65
66 /* Non-zero means don't overwrite existing files while doing redirections. */
67 int noclobber = 0;
68
69 /* Non-zero means trying to get the value of $i where $i is undefined
70    causes an error, instead of a null substitution. */
71 int unbound_vars_is_error = 0;
72
73 /* Non-zero means type out input lines after you read them. */
74 int echo_input_at_read = 0;
75
76 /* Non-zero means type out the command definition after reading, but
77    before executing. */
78 int echo_command_at_execute = 0;
79
80 /* Non-zero means turn on the job control features. */
81 int jobs_m_flag = 0;
82
83 /* Non-zero means this shell is interactive, even if running under a
84    pipe. */
85 int forced_interactive = 0;
86
87 /* By default, follow the symbolic links as if they were real directories
88    while hacking the `cd' command.  This means that `cd ..' moves up in
89    the string of symbolic links that make up the current directory, instead
90    of the absolute directory.  The shell variable `nolinks' also controls
91    this flag. */
92 int no_symbolic_links = 0;
93
94 /* **************************************************************** */
95 /*                                                                  */
96 /*                   Non-Standard Flags Follow Here.                */
97 /*                                                                  */
98 /* **************************************************************** */
99
100
101 /* Non-zero means do lexical scoping in the body of a FOR command. */
102 int lexical_scoping = 0;
103
104 /* Non-zero means no such thing as invisible variables. */
105 int no_invisible_vars = 0;
106
107 /* Non-zero means don't look up or remember command names in a hash table, */
108 int hashing_disabled = 0;
109
110 #if defined (BANG_HISTORY)
111 /* Non-zero means that we are doing history expansion.  The default.
112    This means !22 gets the 22nd line of history. */
113 int history_expansion = 1;
114 #endif /* BANG_HISTORY */
115
116 /* Non-zero means that we allow comments to appear in interactive commands. */
117 #if defined (INTERACTIVE_COMMENTS)
118 int interactive_comments = 1;
119 #else
120 int interactive_comments = 0;
121 #endif /* INTERACTIVE_COMMENTS */
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 /* **************************************************************** */
137 /*                                                                  */
138 /*                      The Flags ALIST.                            */
139 /*                                                                  */
140 /* **************************************************************** */
141
142 struct flags_alist shell_flags[] = {
143   /* Standard sh flags. */
144   { 'a', &mark_modified_vars },
145 #if defined (JOB_CONTROL)
146   { 'b', &asynchronous_notification },
147 #endif /* JOB_CONTROL */
148   { 'e', &exit_immediately_on_error },
149   { 'f', &disallow_filename_globbing },
150   { 'h', &locate_commands_in_functions }, /* Oh, yeah, good mnemonic. */
151   { 'i', &forced_interactive },
152   { 'k', &place_keywords_in_env },
153 #if defined (JOB_CONTROL)
154   { 'm', &jobs_m_flag },
155 #endif /* JOB_CONTROL */
156   { 'n', &read_but_dont_execute },
157   { 'p', &privileged_mode },
158 #if defined (RESTRICTED_SHELL)
159   { 'r', &restricted },
160 #endif /* RESTRICTED_SHELL */
161   { 't', &just_one_command },
162   { 'u', &unbound_vars_is_error },
163   { 'v', &echo_input_at_read },
164   { 'x', &echo_command_at_execute },
165   { 'C', &noclobber },
166
167   /* New flags that control non-standard things. */
168   { 'l', &lexical_scoping },
169   { 'I', &no_invisible_vars },
170
171   /* I want `h', but locate_commands_in_functions has it.  Great. */
172   { 'd', &hashing_disabled },
173
174   { 'P', &no_symbolic_links },
175
176 #if defined (BANG_HISTORY)
177   /* Once again, we don't have the right mnemonic. */
178   { 'H', &history_expansion },
179 #endif /* BANG_HISTORY */
180
181   {0, (int *)NULL}
182 };
183
184 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
185
186 int *
187 find_flag (name)
188      int name;
189 {
190   int i = 0;
191   while (shell_flags[i].name)
192     {
193       if (shell_flags[i].name == name)
194         return (shell_flags[i].value);
195       i++;
196     }
197   return (FLAG_UNKNOWN);
198 }
199
200 /* Change the state of a flag, and return it's original value, or return
201    FLAG_ERROR if there is no flag called NAME.  ON_OR_OFF should be one
202    of FLAG_ON or FLAG_OFF. */
203 int
204 change_flag (flag, on_or_off)
205   int flag;
206   int on_or_off;
207 {
208   int *value = find_flag (flag);
209   int old_value;
210
211 #if defined (RESTRICTED_SHELL)
212   /* Don't allow "set +r" in a shell which is `restricted'. */
213   if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
214     return (FLAG_ERROR);
215 #endif /* RESTRICTED_SHELL */
216
217   if (value == (int *)FLAG_UNKNOWN)
218     return (FLAG_ERROR);
219   else
220     old_value = *value;
221
222   if (on_or_off == FLAG_ON)
223     *value = 1;
224   else
225     {
226       if (on_or_off == FLAG_OFF)
227         *value = 0;
228       else
229         return (FLAG_ERROR);
230     }
231
232   /* Special cases for a few flags. */
233   switch (flag)
234     {
235 #if defined (JOB_CONTROL)
236     case 'm':
237       set_job_control (on_or_off == '-');
238       break;
239 #endif /* JOB_CONTROL */
240
241     case 'p':
242       if (on_or_off == '+')
243         {
244           setuid (current_user.uid);
245           setgid (current_user.gid);
246           current_user.euid = current_user.uid;
247           current_user.egid = current_user.gid;
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 = (char *)xmalloc (1 + NUM_SHELL_FLAGS);
261
262   int i, string_index = 0;
263
264   for (i = 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 }