]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shopt.def
Imported from ../bash-2.05.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
bb70624e 10Software Foundation; either version 2, or (at your option) any later
ccc6cda3
JA
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
bb70624e 20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
ccc6cda3
JA
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;
bb70624e 67extern int xpg_echo;
cce855bc
JA
68
69#if defined (EXTENDED_GLOB)
70extern int extended_glob;
71#endif
ccc6cda3
JA
72
73#if defined (HISTORY)
d166f048 74extern int literal_history, command_oriented_history;
ccc6cda3
JA
75extern int force_append_history;
76#endif
77
78#if defined (READLINE)
d166f048 79extern int hist_verify, history_reediting, perform_hostname_completion;
bb70624e 80extern int no_empty_command_completion;
ccc6cda3
JA
81extern void enable_hostname_completion ();
82#endif
83
bb70624e
JA
84#if defined (PROGRAMMABLE_COMPLETION)
85extern int prog_completion_enabled;
86#endif
87
b72432fd
JA
88#if defined (RESTRICTED_SHELL)
89extern int restricted_shell;
90extern char *shell_name;
91#endif
92
d166f048
JA
93extern void set_shellopts ();
94
ccc6cda3
JA
95static int set_interactive_comments ();
96
b72432fd
JA
97#if defined (RESTRICTED_SHELL)
98static int set_restricted_shell ();
99#endif
100
ccc6cda3
JA
101static struct {
102 char *name;
103 int *value;
104 Function *set_func;
105} shopt_vars[] = {
106 { "cdable_vars", &cdable_vars, (Function *)NULL },
107 { "cdspell", &cdspelling, (Function *)NULL },
108 { "checkhash", &check_hashed_filenames, (Function *)NULL },
109 { "checkwinsize", &check_window_size, (Function *)NULL },
110#if defined (HISTORY)
111 { "cmdhist", &command_oriented_history, (Function *)NULL },
112#endif
113 { "dotglob", &glob_dot_filenames, (Function *)NULL },
114 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
115 { "expand_aliases", &expand_aliases, (Function *)NULL },
cce855bc
JA
116#if defined (EXTENDED_GLOB)
117 { "extglob", &extended_glob, (Function *)NULL },
118#endif
ccc6cda3
JA
119#if defined (READLINE)
120 { "histreedit", &history_reediting, (Function *)NULL },
121#endif
122#if defined (HISTORY)
123 { "histappend", &force_append_history, (Function *)NULL },
ccc6cda3
JA
124#endif
125#if defined (READLINE)
d166f048 126 { "histverify", &hist_verify, (Function *)NULL },
ccc6cda3
JA
127 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
128#endif
cce855bc 129 { "huponexit", &hup_on_exit, (Function *)NULL },
ccc6cda3
JA
130 { "interactive_comments", &interactive_comments, set_interactive_comments },
131#if defined (HISTORY)
132 { "lithist", &literal_history, (Function *)NULL },
133#endif
134 { "mailwarn", &mail_warning, (Function *)NULL },
bb70624e
JA
135#if defined (READLINE)
136 { "no_empty_cmd_completion", &no_empty_command_completion, (Function *)NULL },
137#endif
cce855bc 138 { "nocaseglob", &glob_ignore_case, (Function *)NULL },
ccc6cda3 139 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
bb70624e
JA
140#if defined (PROGRAMMABLE_COMPLETION)
141 { "progcomp", &prog_completion_enabled, (Function *)NULL },
142#endif
ccc6cda3 143 { "promptvars", &promptvars, (Function *)NULL },
b72432fd
JA
144#if defined (RESTRICTED_SHELL)
145 { "restricted_shell", &restricted_shell, set_restricted_shell },
146#endif
ccc6cda3
JA
147 { "shift_verbose", &print_shift_error, (Function *)NULL },
148 { "sourcepath", &source_uses_path, (Function *)NULL },
bb70624e 149 { "xpg_echo", &xpg_echo, (Function *)NULL },
ccc6cda3
JA
150 { (char *)0, (int *)0, (Function *)NULL }
151};
152
153static char *on = "on";
154static char *off = "off";
155
156static int list_shopt_o_options ();
157static int list_some_o_options (), list_some_shopts ();
158static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
159
160#define SFLAG 0x01
161#define UFLAG 0x02
162#define QFLAG 0x04
163#define OFLAG 0x08
164#define PFLAG 0x10
165
166int
167shopt_builtin (list)
168 WORD_LIST *list;
169{
170 int opt, flags, rval;
171
172 flags = 0;
173 reset_internal_getopt ();
174 while ((opt = internal_getopt (list, "psuoq")) != -1)
175 {
176 switch (opt)
177 {
178 case 's':
179 flags |= SFLAG;
180 break;
181 case 'u':
182 flags |= UFLAG;
183 break;
184 case 'q':
185 flags |= QFLAG;
186 break;
187 case 'o':
188 flags |= OFLAG;
189 break;
190 case 'p':
191 flags |= PFLAG;
192 break;
193 default:
194 builtin_usage ();
195 return (EX_USAGE);
196 }
197 }
198 list = loptend;
199
200 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
201 {
202 builtin_error ("cannot set and unset shell options simultaneously");
203 return (EXECUTION_FAILURE);
204 }
205
206 rval = EXECUTION_SUCCESS;
207 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
cce855bc 208 rval = list_shopt_o_options (list, flags);
ccc6cda3
JA
209 else if (list && (flags & OFLAG)) /* shopt -so args */
210 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
211 else if (flags & OFLAG) /* shopt -so */
cce855bc 212 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
ccc6cda3
JA
213 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
214 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
215 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
cce855bc 216 rval = list_shopts (list, flags);
ccc6cda3 217 else /* shopt -su */
cce855bc 218 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
ccc6cda3
JA
219 return (rval);
220}
221
d166f048
JA
222/* Reset the options managed by `shopt' to the values they would have at
223 shell startup. */
224void
225reset_shopt_options ()
226{
227 allow_null_glob_expansion = glob_dot_filenames = 0;
228 cdable_vars = mail_warning = 0;
229 no_exit_on_failed_exec = print_shift_error = 0;
230 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
231
232 source_uses_path = promptvars = 1;
233
cce855bc
JA
234#if defined (EXTENDED_GLOB)
235 extended_glob = 0;
236#endif
237
d166f048
JA
238#if defined (HISTORY)
239 literal_history = force_append_history = 0;
240 command_oriented_history = 1;
241#endif
242
243#if defined (READLINE)
244 hist_verify = history_reediting = 0;
245 perform_hostname_completion = 1;
246#endif
247}
248
ccc6cda3
JA
249static int
250find_shopt (name)
251 char *name;
252{
253 int i;
254
255 for (i = 0; shopt_vars[i].name; i++)
256 if (STREQ (name, shopt_vars[i].name))
257 return i;
258 return -1;
259}
260
261#define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
262
263static int
264toggle_shopts (mode, list, quiet)
265 int mode;
266 WORD_LIST *list;
267 int quiet;
268{
269 WORD_LIST *l;
270 int ind, rval;
271
272 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
273 {
274 ind = find_shopt (l->word->word);
275 if (ind < 0)
276 {
277 SHOPT_ERROR (l->word->word);
278 rval = EXECUTION_FAILURE;
279 }
280 else
281 {
282 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
283 if (shopt_vars[ind].set_func)
284 (*shopt_vars[ind].set_func) (mode);
285 }
286 }
287 return (rval);
288}
289
b72432fd 290static void
cce855bc
JA
291print_shopt (name, val, flags)
292 char *name;
293 int val, flags;
294{
295 if (flags & PFLAG)
296 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
297 else
298 printf (OPTFMT, name, val ? on : off);
299}
300
ccc6cda3
JA
301/* List the values of all or any of the `shopt' options. Returns 0 if
302 all were listed or all variables queried were on; 1 otherwise. */
303static int
cce855bc 304list_shopts (list, flags)
ccc6cda3 305 WORD_LIST *list;
cce855bc 306 int flags;
ccc6cda3
JA
307{
308 WORD_LIST *l;
309 int i, val, rval;
310
311 if (list == 0)
312 {
313 for (i = 0; shopt_vars[i].name; i++)
314 {
315 val = *shopt_vars[i].value;
cce855bc
JA
316 if ((flags & QFLAG) == 0)
317 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
318 }
319 return (EXECUTION_SUCCESS);
320 }
321
322 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
323 {
324 i = find_shopt (l->word->word);
325 if (i < 0)
326 {
327 SHOPT_ERROR (l->word->word);
328 rval = EXECUTION_FAILURE;
329 continue;
330 }
331 val = *shopt_vars[i].value;
332 if (val == 0)
333 rval = EXECUTION_FAILURE;
cce855bc 334 if ((flags & QFLAG) == 0)
28ef6c31 335 print_shopt (l->word->word, val, flags);
ccc6cda3 336 }
28ef6c31 337
ccc6cda3
JA
338 return (rval);
339}
340
341static int
cce855bc
JA
342list_some_shopts (mode, flags)
343 int mode, flags;
ccc6cda3
JA
344{
345 int val, i;
346
347 for (i = 0; shopt_vars[i].name; i++)
348 {
349 val = *shopt_vars[i].value;
cce855bc
JA
350 if (((flags & QFLAG) == 0) && mode == val)
351 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
352 }
353 return (EXECUTION_SUCCESS);
354}
355
356static int
cce855bc 357list_shopt_o_options (list, flags)
ccc6cda3 358 WORD_LIST *list;
cce855bc 359 int flags;
ccc6cda3
JA
360{
361 WORD_LIST *l;
362 int val, rval;
363
364 if (list == 0)
365 {
cce855bc
JA
366 if ((flags & QFLAG) == 0)
367 list_minus_o_opts (-1, (flags & PFLAG));
ccc6cda3
JA
368 return (EXECUTION_SUCCESS);
369 }
370
371 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
372 {
373 val = minus_o_option_value (l->word->word);
374 if (val == -1)
375 {
376 builtin_error ("%s: unknown option name", l->word->word);
377 rval = EXECUTION_FAILURE;
378 continue;
379 }
380 if (val == 0)
381 rval = EXECUTION_FAILURE;
cce855bc
JA
382 if ((flags & QFLAG) == 0)
383 {
384 if (flags & PFLAG)
385 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
386 else
387 printf (OPTFMT, l->word->word, val ? on : off);
388 }
ccc6cda3
JA
389 }
390 return (rval);
391}
392
393static int
cce855bc
JA
394list_some_o_options (mode, flags)
395 int mode, flags;
ccc6cda3 396{
cce855bc
JA
397 if ((flags & QFLAG) == 0)
398 list_minus_o_opts (mode, (flags & PFLAG));
ccc6cda3
JA
399 return (EXECUTION_SUCCESS);
400}
401
402static int
403set_shopt_o_options (mode, list, quiet)
404 int mode;
405 WORD_LIST *list;
406 int quiet;
407{
408 WORD_LIST *l;
409 int rval;
410
411 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
412 {
413 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
414 rval = EXECUTION_FAILURE;
415 }
416 set_shellopts ();
417 return rval;
418}
419
420/* If we set or unset interactive_comments with shopt, make sure the
421 change is reflected in $SHELLOPTS. */
422static int
423set_interactive_comments (mode)
424 int mode;
425{
426 set_shellopts ();
427 return (0);
428}
b72432fd
JA
429
430#if defined (RESTRICTED_SHELL)
431/* Don't allow the value of restricted_shell to be modified. */
432
433static int
434set_restricted_shell (mode)
435 int mode;
436{
437 static int save_restricted = -1;
438
439 if (save_restricted == -1)
440 save_restricted = shell_is_restricted (shell_name);
441
442 restricted_shell = save_restricted;
443 return (0);
444}
445#endif /* RESTRICTED_SHELL */
bb70624e
JA
446
447char **
448get_shopt_options ()
449{
450 char **ret;
451 int n, i;
452
453 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
454 ret = alloc_array (n + 1);
455 for (i = 0; shopt_vars[i].name; i++)
456 ret[i] = savestring (shopt_vars[i].name);
457 ret[i] = (char *)NULL;
458 return ret;
459}