]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shopt.def
Imported from ../bash-2.03.tar.gz.
[thirdparty/bash.git] / builtins / shopt.def
CommitLineData
ccc6cda3
JA
1This file is shopt.def, from which is created shopt.c.
2It implements the Bash `shopt' builtin.
3
4Copyright (C) 1994 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$PRODUCES shopt.c
23
24$BUILTIN shopt
25$DOCNAME shopt_builtin
26$FUNCTION shopt_builtin
27$SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
28Toggle the values of variables controlling optional behavior.
29The -s flag means to enable (set) each OPTNAME; the -u flag
30unsets each OPTNAME. The -q flag suppresses output; the exit
31status indicates whether each OPTNAME is set or unset. The -o
32option restricts the OPTNAMEs to those defined for use with
33`set -o'. With no options, or with the -p option, a list of all
34settable options is displayed, with an indication of whether or
35not each is set.
36$END
37
38#include <config.h>
39
40#if defined (HAVE_UNISTD_H)
cce855bc
JA
41# ifdef _MINIX
42# include <sys/types.h>
43# endif
ccc6cda3
JA
44# include <unistd.h>
45#endif
46
47#include <stdio.h>
48
49#include "../shell.h"
50#include "../flags.h"
51#include "common.h"
52#include "bashgetopt.h"
53
54#define UNSETOPT 0
55#define SETOPT 1
56
57#define OPTFMT "%-15s\t%s\n"
58
59extern int allow_null_glob_expansion, glob_dot_filenames;
60extern int cdable_vars, mail_warning, source_uses_path;
61extern int no_exit_on_failed_exec, print_shift_error;
62extern int check_hashed_filenames, promptvars, interactive_comments;
63extern int cdspelling, expand_aliases;
64extern int check_window_size;
cce855bc
JA
65extern int glob_ignore_case;
66extern int hup_on_exit;
67
68#if defined (EXTENDED_GLOB)
69extern int extended_glob;
70#endif
ccc6cda3
JA
71
72#if defined (HISTORY)
d166f048 73extern int literal_history, command_oriented_history;
ccc6cda3
JA
74extern int force_append_history;
75#endif
76
77#if defined (READLINE)
d166f048 78extern int hist_verify, history_reediting, perform_hostname_completion;
ccc6cda3
JA
79extern void enable_hostname_completion ();
80#endif
81
b72432fd
JA
82#if defined (RESTRICTED_SHELL)
83extern int restricted_shell;
84extern char *shell_name;
85#endif
86
d166f048
JA
87extern void set_shellopts ();
88
ccc6cda3
JA
89static int set_interactive_comments ();
90
b72432fd
JA
91#if defined (RESTRICTED_SHELL)
92static int set_restricted_shell ();
93#endif
94
ccc6cda3
JA
95static struct {
96 char *name;
97 int *value;
98 Function *set_func;
99} shopt_vars[] = {
100 { "cdable_vars", &cdable_vars, (Function *)NULL },
101 { "cdspell", &cdspelling, (Function *)NULL },
102 { "checkhash", &check_hashed_filenames, (Function *)NULL },
103 { "checkwinsize", &check_window_size, (Function *)NULL },
104#if defined (HISTORY)
105 { "cmdhist", &command_oriented_history, (Function *)NULL },
106#endif
107 { "dotglob", &glob_dot_filenames, (Function *)NULL },
108 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
109 { "expand_aliases", &expand_aliases, (Function *)NULL },
cce855bc
JA
110#if defined (EXTENDED_GLOB)
111 { "extglob", &extended_glob, (Function *)NULL },
112#endif
ccc6cda3
JA
113#if defined (READLINE)
114 { "histreedit", &history_reediting, (Function *)NULL },
115#endif
116#if defined (HISTORY)
117 { "histappend", &force_append_history, (Function *)NULL },
ccc6cda3
JA
118#endif
119#if defined (READLINE)
d166f048 120 { "histverify", &hist_verify, (Function *)NULL },
ccc6cda3
JA
121 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
122#endif
cce855bc 123 { "huponexit", &hup_on_exit, (Function *)NULL },
ccc6cda3
JA
124 { "interactive_comments", &interactive_comments, set_interactive_comments },
125#if defined (HISTORY)
126 { "lithist", &literal_history, (Function *)NULL },
127#endif
128 { "mailwarn", &mail_warning, (Function *)NULL },
cce855bc 129 { "nocaseglob", &glob_ignore_case, (Function *)NULL },
ccc6cda3
JA
130 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
131 { "promptvars", &promptvars, (Function *)NULL },
b72432fd
JA
132#if defined (RESTRICTED_SHELL)
133 { "restricted_shell", &restricted_shell, set_restricted_shell },
134#endif
ccc6cda3
JA
135 { "shift_verbose", &print_shift_error, (Function *)NULL },
136 { "sourcepath", &source_uses_path, (Function *)NULL },
137 { (char *)0, (int *)0, (Function *)NULL }
138};
139
140static char *on = "on";
141static char *off = "off";
142
143static int list_shopt_o_options ();
144static int list_some_o_options (), list_some_shopts ();
145static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
146
147#define SFLAG 0x01
148#define UFLAG 0x02
149#define QFLAG 0x04
150#define OFLAG 0x08
151#define PFLAG 0x10
152
153int
154shopt_builtin (list)
155 WORD_LIST *list;
156{
157 int opt, flags, rval;
158
159 flags = 0;
160 reset_internal_getopt ();
161 while ((opt = internal_getopt (list, "psuoq")) != -1)
162 {
163 switch (opt)
164 {
165 case 's':
166 flags |= SFLAG;
167 break;
168 case 'u':
169 flags |= UFLAG;
170 break;
171 case 'q':
172 flags |= QFLAG;
173 break;
174 case 'o':
175 flags |= OFLAG;
176 break;
177 case 'p':
178 flags |= PFLAG;
179 break;
180 default:
181 builtin_usage ();
182 return (EX_USAGE);
183 }
184 }
185 list = loptend;
186
187 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
188 {
189 builtin_error ("cannot set and unset shell options simultaneously");
190 return (EXECUTION_FAILURE);
191 }
192
193 rval = EXECUTION_SUCCESS;
194 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
cce855bc 195 rval = list_shopt_o_options (list, flags);
ccc6cda3
JA
196 else if (list && (flags & OFLAG)) /* shopt -so args */
197 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
198 else if (flags & OFLAG) /* shopt -so */
cce855bc 199 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
ccc6cda3
JA
200 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
201 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
202 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
cce855bc 203 rval = list_shopts (list, flags);
ccc6cda3 204 else /* shopt -su */
cce855bc 205 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
ccc6cda3
JA
206 return (rval);
207}
208
d166f048
JA
209/* Reset the options managed by `shopt' to the values they would have at
210 shell startup. */
211void
212reset_shopt_options ()
213{
214 allow_null_glob_expansion = glob_dot_filenames = 0;
215 cdable_vars = mail_warning = 0;
216 no_exit_on_failed_exec = print_shift_error = 0;
217 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
218
219 source_uses_path = promptvars = 1;
220
cce855bc
JA
221#if defined (EXTENDED_GLOB)
222 extended_glob = 0;
223#endif
224
d166f048
JA
225#if defined (HISTORY)
226 literal_history = force_append_history = 0;
227 command_oriented_history = 1;
228#endif
229
230#if defined (READLINE)
231 hist_verify = history_reediting = 0;
232 perform_hostname_completion = 1;
233#endif
234}
235
ccc6cda3
JA
236static int
237find_shopt (name)
238 char *name;
239{
240 int i;
241
242 for (i = 0; shopt_vars[i].name; i++)
243 if (STREQ (name, shopt_vars[i].name))
244 return i;
245 return -1;
246}
247
248#define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
249
250static int
251toggle_shopts (mode, list, quiet)
252 int mode;
253 WORD_LIST *list;
254 int quiet;
255{
256 WORD_LIST *l;
257 int ind, rval;
258
259 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
260 {
261 ind = find_shopt (l->word->word);
262 if (ind < 0)
263 {
264 SHOPT_ERROR (l->word->word);
265 rval = EXECUTION_FAILURE;
266 }
267 else
268 {
269 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
270 if (shopt_vars[ind].set_func)
271 (*shopt_vars[ind].set_func) (mode);
272 }
273 }
274 return (rval);
275}
276
b72432fd 277static void
cce855bc
JA
278print_shopt (name, val, flags)
279 char *name;
280 int val, flags;
281{
282 if (flags & PFLAG)
283 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
284 else
285 printf (OPTFMT, name, val ? on : off);
286}
287
ccc6cda3
JA
288/* List the values of all or any of the `shopt' options. Returns 0 if
289 all were listed or all variables queried were on; 1 otherwise. */
290static int
cce855bc 291list_shopts (list, flags)
ccc6cda3 292 WORD_LIST *list;
cce855bc 293 int flags;
ccc6cda3
JA
294{
295 WORD_LIST *l;
296 int i, val, rval;
297
298 if (list == 0)
299 {
300 for (i = 0; shopt_vars[i].name; i++)
301 {
302 val = *shopt_vars[i].value;
cce855bc
JA
303 if ((flags & QFLAG) == 0)
304 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
305 }
306 return (EXECUTION_SUCCESS);
307 }
308
309 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
310 {
311 i = find_shopt (l->word->word);
312 if (i < 0)
313 {
314 SHOPT_ERROR (l->word->word);
315 rval = EXECUTION_FAILURE;
316 continue;
317 }
318 val = *shopt_vars[i].value;
319 if (val == 0)
320 rval = EXECUTION_FAILURE;
cce855bc
JA
321 if ((flags & QFLAG) == 0)
322 print_shopt (l->word->word, val, flags);
ccc6cda3
JA
323 }
324 return (rval);
325}
326
327static int
cce855bc
JA
328list_some_shopts (mode, flags)
329 int mode, flags;
ccc6cda3
JA
330{
331 int val, i;
332
333 for (i = 0; shopt_vars[i].name; i++)
334 {
335 val = *shopt_vars[i].value;
cce855bc
JA
336 if (((flags & QFLAG) == 0) && mode == val)
337 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
338 }
339 return (EXECUTION_SUCCESS);
340}
341
342static int
cce855bc 343list_shopt_o_options (list, flags)
ccc6cda3 344 WORD_LIST *list;
cce855bc 345 int flags;
ccc6cda3
JA
346{
347 WORD_LIST *l;
348 int val, rval;
349
350 if (list == 0)
351 {
cce855bc
JA
352 if ((flags & QFLAG) == 0)
353 list_minus_o_opts (-1, (flags & PFLAG));
ccc6cda3
JA
354 return (EXECUTION_SUCCESS);
355 }
356
357 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
358 {
359 val = minus_o_option_value (l->word->word);
360 if (val == -1)
361 {
362 builtin_error ("%s: unknown option name", l->word->word);
363 rval = EXECUTION_FAILURE;
364 continue;
365 }
366 if (val == 0)
367 rval = EXECUTION_FAILURE;
cce855bc
JA
368 if ((flags & QFLAG) == 0)
369 {
370 if (flags & PFLAG)
371 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
372 else
373 printf (OPTFMT, l->word->word, val ? on : off);
374 }
ccc6cda3
JA
375 }
376 return (rval);
377}
378
379static int
cce855bc
JA
380list_some_o_options (mode, flags)
381 int mode, flags;
ccc6cda3 382{
cce855bc
JA
383 if ((flags & QFLAG) == 0)
384 list_minus_o_opts (mode, (flags & PFLAG));
ccc6cda3
JA
385 return (EXECUTION_SUCCESS);
386}
387
388static int
389set_shopt_o_options (mode, list, quiet)
390 int mode;
391 WORD_LIST *list;
392 int quiet;
393{
394 WORD_LIST *l;
395 int rval;
396
397 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
398 {
399 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
400 rval = EXECUTION_FAILURE;
401 }
402 set_shellopts ();
403 return rval;
404}
405
406/* If we set or unset interactive_comments with shopt, make sure the
407 change is reflected in $SHELLOPTS. */
408static int
409set_interactive_comments (mode)
410 int mode;
411{
412 set_shellopts ();
413 return (0);
414}
b72432fd
JA
415
416#if defined (RESTRICTED_SHELL)
417/* Don't allow the value of restricted_shell to be modified. */
418
419static int
420set_restricted_shell (mode)
421 int mode;
422{
423 static int save_restricted = -1;
424
425 if (save_restricted == -1)
426 save_restricted = shell_is_restricted (shell_name);
427
428 restricted_shell = save_restricted;
429 return (0);
430}
431#endif /* RESTRICTED_SHELL */