]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shopt.def
Imported from ../bash-3.0.16.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
b80f6443 4Copyright (C) 1994-2003 Free Software Foundation, Inc.
ccc6cda3
JA
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
ccc6cda3
JA
25$FUNCTION shopt_builtin
26$SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
27Toggle the values of variables controlling optional behavior.
28The -s flag means to enable (set) each OPTNAME; the -u flag
29unsets each OPTNAME. The -q flag suppresses output; the exit
30status indicates whether each OPTNAME is set or unset. The -o
31option restricts the OPTNAMEs to those defined for use with
32`set -o'. With no options, or with the -p option, a list of all
33settable options is displayed, with an indication of whether or
34not each is set.
35$END
36
37#include <config.h>
38
39#if defined (HAVE_UNISTD_H)
cce855bc
JA
40# ifdef _MINIX
41# include <sys/types.h>
42# endif
ccc6cda3
JA
43# include <unistd.h>
44#endif
45
46#include <stdio.h>
47
b80f6443
JA
48#include "../bashintl.h"
49
ccc6cda3
JA
50#include "../shell.h"
51#include "../flags.h"
52#include "common.h"
53#include "bashgetopt.h"
54
55#define UNSETOPT 0
56#define SETOPT 1
57
58#define OPTFMT "%-15s\t%s\n"
59
b80f6443 60extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames;
ccc6cda3
JA
61extern int cdable_vars, mail_warning, source_uses_path;
62extern int no_exit_on_failed_exec, print_shift_error;
7117c2d2 63extern int check_hashed_filenames, promptvars;
ccc6cda3 64extern int cdspelling, expand_aliases;
b80f6443 65extern int extended_quote;
ccc6cda3 66extern int check_window_size;
cce855bc
JA
67extern int glob_ignore_case;
68extern int hup_on_exit;
bb70624e 69extern int xpg_echo;
b80f6443 70extern int gnu_error_format;
cce855bc
JA
71
72#if defined (EXTENDED_GLOB)
73extern int extended_glob;
74#endif
ccc6cda3
JA
75
76#if defined (HISTORY)
d166f048 77extern int literal_history, command_oriented_history;
ccc6cda3
JA
78extern int force_append_history;
79#endif
80
81#if defined (READLINE)
d166f048 82extern int hist_verify, history_reediting, perform_hostname_completion;
bb70624e 83extern int no_empty_command_completion;
b80f6443 84extern int force_fignore;
f73dda09 85extern int enable_hostname_completion __P((int));
ccc6cda3
JA
86#endif
87
bb70624e
JA
88#if defined (PROGRAMMABLE_COMPLETION)
89extern int prog_completion_enabled;
90#endif
91
b72432fd 92#if defined (RESTRICTED_SHELL)
b72432fd
JA
93extern char *shell_name;
94#endif
95
b80f6443
JA
96#if defined (DEBUGGER)
97extern int debugging_mode;
98#endif
99
7117c2d2
JA
100static void shopt_error __P((char *));
101
b80f6443 102static int set_shellopts_after_change __P((int));
ccc6cda3 103
b72432fd 104#if defined (RESTRICTED_SHELL)
f73dda09 105static int set_restricted_shell __P((int));
b72432fd
JA
106#endif
107
f73dda09
JA
108static int shopt_login_shell;
109
110typedef int shopt_set_func_t __P((int));
111
ccc6cda3
JA
112static struct {
113 char *name;
114 int *value;
f73dda09 115 shopt_set_func_t *set_func;
ccc6cda3 116} shopt_vars[] = {
f73dda09
JA
117 { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
118 { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
119 { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
120 { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
ccc6cda3 121#if defined (HISTORY)
f73dda09 122 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
ccc6cda3 123#endif
f73dda09
JA
124 { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
125 { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
126 { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL },
b80f6443
JA
127#if defined (DEBUGGER)
128 { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL },
129#endif
cce855bc 130#if defined (EXTENDED_GLOB)
f73dda09 131 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
cce855bc 132#endif
b80f6443
JA
133 { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
134 { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
ccc6cda3 135#if defined (READLINE)
b80f6443
JA
136 { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
137 { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
f73dda09 138 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
ccc6cda3
JA
139#endif
140#if defined (HISTORY)
f73dda09 141 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
ccc6cda3
JA
142#endif
143#if defined (READLINE)
f73dda09
JA
144 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
145 { "hostcomplete", &perform_hostname_completion, enable_hostname_completion },
ccc6cda3 146#endif
f73dda09 147 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
b80f6443 148 { "interactive_comments", &interactive_comments, set_shellopts_after_change },
ccc6cda3 149#if defined (HISTORY)
f73dda09 150 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
ccc6cda3 151#endif
f73dda09
JA
152 { "login_shell", &shopt_login_shell, set_login_shell },
153 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
bb70624e 154#if defined (READLINE)
f73dda09 155 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
bb70624e 156#endif
f73dda09
JA
157 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
158 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
bb70624e 159#if defined (PROGRAMMABLE_COMPLETION)
f73dda09 160 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
bb70624e 161#endif
f73dda09 162 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
b72432fd
JA
163#if defined (RESTRICTED_SHELL)
164 { "restricted_shell", &restricted_shell, set_restricted_shell },
165#endif
f73dda09
JA
166 { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL },
167 { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL },
168 { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL },
169 { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
ccc6cda3
JA
170};
171
172static char *on = "on";
173static char *off = "off";
174
f73dda09
JA
175static int find_shopt __P((char *));
176static int toggle_shopts __P((int, WORD_LIST *, int));
177static void print_shopt __P((char *, int, int));
178static int list_shopts __P((WORD_LIST *, int));
179static int list_some_shopts __P((int, int));
180static int list_shopt_o_options __P((WORD_LIST *, int));
181static int list_some_o_options __P((int, int));
182static int set_shopt_o_options __P((int, WORD_LIST *, int));
ccc6cda3
JA
183
184#define SFLAG 0x01
185#define UFLAG 0x02
186#define QFLAG 0x04
187#define OFLAG 0x08
188#define PFLAG 0x10
189
190int
191shopt_builtin (list)
192 WORD_LIST *list;
193{
194 int opt, flags, rval;
195
196 flags = 0;
197 reset_internal_getopt ();
198 while ((opt = internal_getopt (list, "psuoq")) != -1)
199 {
200 switch (opt)
201 {
202 case 's':
203 flags |= SFLAG;
204 break;
205 case 'u':
206 flags |= UFLAG;
207 break;
208 case 'q':
209 flags |= QFLAG;
210 break;
211 case 'o':
212 flags |= OFLAG;
213 break;
214 case 'p':
215 flags |= PFLAG;
216 break;
217 default:
218 builtin_usage ();
219 return (EX_USAGE);
220 }
221 }
222 list = loptend;
223
224 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
225 {
b80f6443 226 builtin_error (_("cannot set and unset shell options simultaneously"));
ccc6cda3
JA
227 return (EXECUTION_FAILURE);
228 }
229
230 rval = EXECUTION_SUCCESS;
231 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
cce855bc 232 rval = list_shopt_o_options (list, flags);
ccc6cda3
JA
233 else if (list && (flags & OFLAG)) /* shopt -so args */
234 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
235 else if (flags & OFLAG) /* shopt -so */
cce855bc 236 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
ccc6cda3
JA
237 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
238 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
239 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
cce855bc 240 rval = list_shopts (list, flags);
ccc6cda3 241 else /* shopt -su */
cce855bc 242 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
ccc6cda3
JA
243 return (rval);
244}
245
d166f048
JA
246/* Reset the options managed by `shopt' to the values they would have at
247 shell startup. */
248void
249reset_shopt_options ()
250{
251 allow_null_glob_expansion = glob_dot_filenames = 0;
252 cdable_vars = mail_warning = 0;
253 no_exit_on_failed_exec = print_shift_error = 0;
254 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
255
256 source_uses_path = promptvars = 1;
257
cce855bc
JA
258#if defined (EXTENDED_GLOB)
259 extended_glob = 0;
260#endif
261
d166f048
JA
262#if defined (HISTORY)
263 literal_history = force_append_history = 0;
264 command_oriented_history = 1;
265#endif
266
267#if defined (READLINE)
268 hist_verify = history_reediting = 0;
269 perform_hostname_completion = 1;
270#endif
f73dda09
JA
271
272 shopt_login_shell = login_shell;
d166f048
JA
273}
274
ccc6cda3
JA
275static int
276find_shopt (name)
277 char *name;
278{
279 int i;
280
281 for (i = 0; shopt_vars[i].name; i++)
282 if (STREQ (name, shopt_vars[i].name))
283 return i;
284 return -1;
285}
286
7117c2d2
JA
287static void
288shopt_error (s)
289 char *s;
290{
b80f6443 291 builtin_error (_("%s: invalid shell option name"), s);
7117c2d2 292}
ccc6cda3
JA
293
294static int
295toggle_shopts (mode, list, quiet)
296 int mode;
297 WORD_LIST *list;
298 int quiet;
299{
300 WORD_LIST *l;
301 int ind, rval;
302
303 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
304 {
305 ind = find_shopt (l->word->word);
306 if (ind < 0)
307 {
7117c2d2 308 shopt_error (l->word->word);
ccc6cda3
JA
309 rval = EXECUTION_FAILURE;
310 }
311 else
312 {
313 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
314 if (shopt_vars[ind].set_func)
315 (*shopt_vars[ind].set_func) (mode);
316 }
317 }
318 return (rval);
319}
320
b72432fd 321static void
cce855bc
JA
322print_shopt (name, val, flags)
323 char *name;
324 int val, flags;
325{
326 if (flags & PFLAG)
327 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
328 else
329 printf (OPTFMT, name, val ? on : off);
330}
331
ccc6cda3
JA
332/* List the values of all or any of the `shopt' options. Returns 0 if
333 all were listed or all variables queried were on; 1 otherwise. */
334static int
cce855bc 335list_shopts (list, flags)
ccc6cda3 336 WORD_LIST *list;
cce855bc 337 int flags;
ccc6cda3
JA
338{
339 WORD_LIST *l;
340 int i, val, rval;
341
342 if (list == 0)
343 {
344 for (i = 0; shopt_vars[i].name; i++)
345 {
346 val = *shopt_vars[i].value;
cce855bc
JA
347 if ((flags & QFLAG) == 0)
348 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
349 }
350 return (EXECUTION_SUCCESS);
351 }
352
353 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
354 {
355 i = find_shopt (l->word->word);
356 if (i < 0)
357 {
7117c2d2 358 shopt_error (l->word->word);
ccc6cda3
JA
359 rval = EXECUTION_FAILURE;
360 continue;
361 }
362 val = *shopt_vars[i].value;
363 if (val == 0)
364 rval = EXECUTION_FAILURE;
cce855bc 365 if ((flags & QFLAG) == 0)
28ef6c31 366 print_shopt (l->word->word, val, flags);
ccc6cda3 367 }
28ef6c31 368
ccc6cda3
JA
369 return (rval);
370}
371
372static int
cce855bc
JA
373list_some_shopts (mode, flags)
374 int mode, flags;
ccc6cda3
JA
375{
376 int val, i;
377
378 for (i = 0; shopt_vars[i].name; i++)
379 {
380 val = *shopt_vars[i].value;
cce855bc
JA
381 if (((flags & QFLAG) == 0) && mode == val)
382 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
383 }
384 return (EXECUTION_SUCCESS);
385}
386
387static int
cce855bc 388list_shopt_o_options (list, flags)
ccc6cda3 389 WORD_LIST *list;
cce855bc 390 int flags;
ccc6cda3
JA
391{
392 WORD_LIST *l;
393 int val, rval;
394
395 if (list == 0)
396 {
cce855bc
JA
397 if ((flags & QFLAG) == 0)
398 list_minus_o_opts (-1, (flags & PFLAG));
ccc6cda3
JA
399 return (EXECUTION_SUCCESS);
400 }
401
402 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
403 {
404 val = minus_o_option_value (l->word->word);
405 if (val == -1)
406 {
7117c2d2 407 sh_invalidoptname (l->word->word);
ccc6cda3
JA
408 rval = EXECUTION_FAILURE;
409 continue;
410 }
411 if (val == 0)
412 rval = EXECUTION_FAILURE;
cce855bc
JA
413 if ((flags & QFLAG) == 0)
414 {
415 if (flags & PFLAG)
416 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
417 else
418 printf (OPTFMT, l->word->word, val ? on : off);
419 }
ccc6cda3
JA
420 }
421 return (rval);
422}
423
424static int
cce855bc
JA
425list_some_o_options (mode, flags)
426 int mode, flags;
ccc6cda3 427{
cce855bc
JA
428 if ((flags & QFLAG) == 0)
429 list_minus_o_opts (mode, (flags & PFLAG));
ccc6cda3
JA
430 return (EXECUTION_SUCCESS);
431}
432
433static int
434set_shopt_o_options (mode, list, quiet)
435 int mode;
436 WORD_LIST *list;
437 int quiet;
438{
439 WORD_LIST *l;
440 int rval;
441
442 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
443 {
444 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
445 rval = EXECUTION_FAILURE;
446 }
447 set_shellopts ();
448 return rval;
449}
450
451/* If we set or unset interactive_comments with shopt, make sure the
452 change is reflected in $SHELLOPTS. */
453static int
b80f6443 454set_shellopts_after_change (mode)
ccc6cda3
JA
455 int mode;
456{
457 set_shellopts ();
458 return (0);
459}
b72432fd
JA
460
461#if defined (RESTRICTED_SHELL)
462/* Don't allow the value of restricted_shell to be modified. */
463
464static int
465set_restricted_shell (mode)
466 int mode;
467{
468 static int save_restricted = -1;
469
470 if (save_restricted == -1)
471 save_restricted = shell_is_restricted (shell_name);
472
473 restricted_shell = save_restricted;
474 return (0);
475}
476#endif /* RESTRICTED_SHELL */
bb70624e 477
f73dda09
JA
478/* Not static so shell.c can call it to initialize shopt_login_shell */
479int
480set_login_shell (mode)
481 int mode;
482{
483 shopt_login_shell = login_shell != 0;
484 return (0);
485}
486
bb70624e
JA
487char **
488get_shopt_options ()
489{
490 char **ret;
491 int n, i;
492
493 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
7117c2d2 494 ret = strvec_create (n + 1);
bb70624e
JA
495 for (i = 0; shopt_vars[i].name; i++)
496 ret[i] = savestring (shopt_vars[i].name);
497 ret[i] = (char *)NULL;
498 return ret;
499}
f73dda09
JA
500
501/*
502 * External interface for other parts of the shell. NAME is a string option;
503 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
504 * REUSABLE is 1 if we want to print output in a form that may be reused.
505 */
506int
507shopt_setopt (name, mode)
508 char *name;
509 int mode;
510{
511 WORD_LIST *wl;
512 int r;
513
514 wl = add_string_to_list (name, (WORD_LIST *)NULL);
515 r = toggle_shopts (mode, wl, 0);
516 dispose_words (wl);
517 return r;
518}
519
520int
521shopt_listopt (name, reusable)
522 char *name;
523 int reusable;
524{
525 int i;
526
527 if (name == 0)
528 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
529
530 i = find_shopt (name);
531 if (i < 0)
532 {
7117c2d2 533 shopt_error (name);
f73dda09
JA
534 return (EXECUTION_FAILURE);
535 }
536
537 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
538 return (EXECUTION_SUCCESS);
539}