]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/shopt.def
Imported from ../bash-2.05b.tar.gz.
[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-2002 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 $FUNCTION shopt_builtin
26 $SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
27 Toggle the values of variables controlling optional behavior.
28 The -s flag means to enable (set) each OPTNAME; the -u flag
29 unsets each OPTNAME. The -q flag suppresses output; the exit
30 status indicates whether each OPTNAME is set or unset. The -o
31 option restricts the OPTNAMEs to those defined for use with
32 `set -o'. With no options, or with the -p option, a list of all
33 settable options is displayed, with an indication of whether or
34 not each is set.
35 $END
36
37 #include <config.h>
38
39 #if defined (HAVE_UNISTD_H)
40 # ifdef _MINIX
41 # include <sys/types.h>
42 # endif
43 # include <unistd.h>
44 #endif
45
46 #include <stdio.h>
47
48 #include "../shell.h"
49 #include "../flags.h"
50 #include "common.h"
51 #include "bashgetopt.h"
52
53 #define UNSETOPT 0
54 #define SETOPT 1
55
56 #define OPTFMT "%-15s\t%s\n"
57
58 extern int allow_null_glob_expansion, glob_dot_filenames;
59 extern int cdable_vars, mail_warning, source_uses_path;
60 extern int no_exit_on_failed_exec, print_shift_error;
61 extern int check_hashed_filenames, promptvars;
62 extern int cdspelling, expand_aliases;
63 extern int check_window_size;
64 extern int glob_ignore_case;
65 extern int hup_on_exit;
66 extern int xpg_echo;
67
68 #if defined (EXTENDED_GLOB)
69 extern int extended_glob;
70 #endif
71
72 #if defined (HISTORY)
73 extern int literal_history, command_oriented_history;
74 extern int force_append_history;
75 #endif
76
77 #if defined (READLINE)
78 extern int hist_verify, history_reediting, perform_hostname_completion;
79 extern int no_empty_command_completion;
80 extern int enable_hostname_completion __P((int));
81 #endif
82
83 #if defined (PROGRAMMABLE_COMPLETION)
84 extern int prog_completion_enabled;
85 #endif
86
87 #if defined (RESTRICTED_SHELL)
88 extern char *shell_name;
89 #endif
90
91 static void shopt_error __P((char *));
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 static void
272 shopt_error (s)
273 char *s;
274 {
275 builtin_error ("%s: invalid shell option name", s);
276 }
277
278 static int
279 toggle_shopts (mode, list, quiet)
280 int mode;
281 WORD_LIST *list;
282 int quiet;
283 {
284 WORD_LIST *l;
285 int ind, rval;
286
287 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
288 {
289 ind = find_shopt (l->word->word);
290 if (ind < 0)
291 {
292 shopt_error (l->word->word);
293 rval = EXECUTION_FAILURE;
294 }
295 else
296 {
297 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
298 if (shopt_vars[ind].set_func)
299 (*shopt_vars[ind].set_func) (mode);
300 }
301 }
302 return (rval);
303 }
304
305 static void
306 print_shopt (name, val, flags)
307 char *name;
308 int val, flags;
309 {
310 if (flags & PFLAG)
311 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
312 else
313 printf (OPTFMT, name, val ? on : off);
314 }
315
316 /* List the values of all or any of the `shopt' options. Returns 0 if
317 all were listed or all variables queried were on; 1 otherwise. */
318 static int
319 list_shopts (list, flags)
320 WORD_LIST *list;
321 int flags;
322 {
323 WORD_LIST *l;
324 int i, val, rval;
325
326 if (list == 0)
327 {
328 for (i = 0; shopt_vars[i].name; i++)
329 {
330 val = *shopt_vars[i].value;
331 if ((flags & QFLAG) == 0)
332 print_shopt (shopt_vars[i].name, val, flags);
333 }
334 return (EXECUTION_SUCCESS);
335 }
336
337 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
338 {
339 i = find_shopt (l->word->word);
340 if (i < 0)
341 {
342 shopt_error (l->word->word);
343 rval = EXECUTION_FAILURE;
344 continue;
345 }
346 val = *shopt_vars[i].value;
347 if (val == 0)
348 rval = EXECUTION_FAILURE;
349 if ((flags & QFLAG) == 0)
350 print_shopt (l->word->word, val, flags);
351 }
352
353 return (rval);
354 }
355
356 static int
357 list_some_shopts (mode, flags)
358 int mode, flags;
359 {
360 int val, i;
361
362 for (i = 0; shopt_vars[i].name; i++)
363 {
364 val = *shopt_vars[i].value;
365 if (((flags & QFLAG) == 0) && mode == val)
366 print_shopt (shopt_vars[i].name, val, flags);
367 }
368 return (EXECUTION_SUCCESS);
369 }
370
371 static int
372 list_shopt_o_options (list, flags)
373 WORD_LIST *list;
374 int flags;
375 {
376 WORD_LIST *l;
377 int val, rval;
378
379 if (list == 0)
380 {
381 if ((flags & QFLAG) == 0)
382 list_minus_o_opts (-1, (flags & PFLAG));
383 return (EXECUTION_SUCCESS);
384 }
385
386 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
387 {
388 val = minus_o_option_value (l->word->word);
389 if (val == -1)
390 {
391 sh_invalidoptname (l->word->word);
392 rval = EXECUTION_FAILURE;
393 continue;
394 }
395 if (val == 0)
396 rval = EXECUTION_FAILURE;
397 if ((flags & QFLAG) == 0)
398 {
399 if (flags & PFLAG)
400 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
401 else
402 printf (OPTFMT, l->word->word, val ? on : off);
403 }
404 }
405 return (rval);
406 }
407
408 static int
409 list_some_o_options (mode, flags)
410 int mode, flags;
411 {
412 if ((flags & QFLAG) == 0)
413 list_minus_o_opts (mode, (flags & PFLAG));
414 return (EXECUTION_SUCCESS);
415 }
416
417 static int
418 set_shopt_o_options (mode, list, quiet)
419 int mode;
420 WORD_LIST *list;
421 int quiet;
422 {
423 WORD_LIST *l;
424 int rval;
425
426 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
427 {
428 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
429 rval = EXECUTION_FAILURE;
430 }
431 set_shellopts ();
432 return rval;
433 }
434
435 /* If we set or unset interactive_comments with shopt, make sure the
436 change is reflected in $SHELLOPTS. */
437 static int
438 set_interactive_comments (mode)
439 int mode;
440 {
441 set_shellopts ();
442 return (0);
443 }
444
445 #if defined (RESTRICTED_SHELL)
446 /* Don't allow the value of restricted_shell to be modified. */
447
448 static int
449 set_restricted_shell (mode)
450 int mode;
451 {
452 static int save_restricted = -1;
453
454 if (save_restricted == -1)
455 save_restricted = shell_is_restricted (shell_name);
456
457 restricted_shell = save_restricted;
458 return (0);
459 }
460 #endif /* RESTRICTED_SHELL */
461
462 /* Not static so shell.c can call it to initialize shopt_login_shell */
463 int
464 set_login_shell (mode)
465 int mode;
466 {
467 shopt_login_shell = login_shell != 0;
468 return (0);
469 }
470
471 char **
472 get_shopt_options ()
473 {
474 char **ret;
475 int n, i;
476
477 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
478 ret = strvec_create (n + 1);
479 for (i = 0; shopt_vars[i].name; i++)
480 ret[i] = savestring (shopt_vars[i].name);
481 ret[i] = (char *)NULL;
482 return ret;
483 }
484
485 /*
486 * External interface for other parts of the shell. NAME is a string option;
487 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
488 * REUSABLE is 1 if we want to print output in a form that may be reused.
489 */
490 int
491 shopt_setopt (name, mode)
492 char *name;
493 int mode;
494 {
495 WORD_LIST *wl;
496 int r;
497
498 wl = add_string_to_list (name, (WORD_LIST *)NULL);
499 r = toggle_shopts (mode, wl, 0);
500 dispose_words (wl);
501 return r;
502 }
503
504 int
505 shopt_listopt (name, reusable)
506 char *name;
507 int reusable;
508 {
509 int i;
510
511 if (name == 0)
512 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
513
514 i = find_shopt (name);
515 if (i < 0)
516 {
517 shopt_error (name);
518 return (EXECUTION_FAILURE);
519 }
520
521 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
522 return (EXECUTION_SUCCESS);
523 }