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