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