Imported from ../bash-2.05.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 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 void enable_hostname_completion ();
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 extern void set_shellopts ();
94
95 static int set_interactive_comments ();
96
97 #if defined (RESTRICTED_SHELL)
98 static int set_restricted_shell ();
99 #endif
100
101 static 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 },
116 #if defined (EXTENDED_GLOB)
117   { "extglob", &extended_glob, (Function *)NULL },
118 #endif
119 #if defined (READLINE)
120   { "histreedit", &history_reediting, (Function *)NULL },
121 #endif
122 #if defined (HISTORY)
123   { "histappend", &force_append_history, (Function *)NULL },
124 #endif
125 #if defined (READLINE)
126   { "histverify", &hist_verify, (Function *)NULL },
127   { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
128 #endif
129   { "huponexit", &hup_on_exit, (Function *)NULL },
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 },
135 #if defined (READLINE)
136   { "no_empty_cmd_completion", &no_empty_command_completion, (Function *)NULL },
137 #endif
138   { "nocaseglob", &glob_ignore_case, (Function *)NULL },
139   { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
140 #if defined (PROGRAMMABLE_COMPLETION)
141   { "progcomp", &prog_completion_enabled, (Function *)NULL },
142 #endif
143   { "promptvars", &promptvars, (Function *)NULL },
144 #if defined (RESTRICTED_SHELL)
145   { "restricted_shell", &restricted_shell, set_restricted_shell },
146 #endif
147   { "shift_verbose", &print_shift_error, (Function *)NULL },
148   { "sourcepath", &source_uses_path, (Function *)NULL },
149   { "xpg_echo", &xpg_echo, (Function *)NULL },
150   { (char *)0, (int *)0, (Function *)NULL }
151 };
152
153 static char *on = "on";
154 static char *off = "off";
155
156 static int list_shopt_o_options ();
157 static int list_some_o_options (), list_some_shopts ();
158 static 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
166 int
167 shopt_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 */
208     rval = list_shopt_o_options (list, flags);
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 */
212     rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
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] */
216     rval = list_shopts (list, flags);
217   else                                          /* shopt -su */
218     rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
219   return (rval);
220 }
221
222 /* Reset the options managed by `shopt' to the values they would have at
223    shell startup. */
224 void
225 reset_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
234 #if defined (EXTENDED_GLOB)
235   extended_glob = 0;
236 #endif
237
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
249 static int
250 find_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
263 static int
264 toggle_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
290 static void
291 print_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
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. */
303 static int
304 list_shopts (list, flags)
305      WORD_LIST *list;
306      int flags;
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;
316           if ((flags & QFLAG) == 0)
317             print_shopt (shopt_vars[i].name, val, flags);
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;
334       if ((flags & QFLAG) == 0)
335         print_shopt (l->word->word, val, flags);
336     }
337
338   return (rval);
339 }
340
341 static int
342 list_some_shopts (mode, flags)
343      int mode, flags;
344 {
345   int val, i;
346
347   for (i = 0; shopt_vars[i].name; i++)
348     {
349       val = *shopt_vars[i].value;
350       if (((flags & QFLAG) == 0) && mode == val)
351         print_shopt (shopt_vars[i].name, val, flags);
352     }
353   return (EXECUTION_SUCCESS);
354 }
355
356 static int
357 list_shopt_o_options (list, flags)
358      WORD_LIST *list;
359      int flags;
360 {
361   WORD_LIST *l;
362   int val, rval;
363
364   if (list == 0)
365     {
366       if ((flags & QFLAG) == 0)
367         list_minus_o_opts (-1, (flags & PFLAG));
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;
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         }
389     }
390   return (rval);
391 }
392
393 static int
394 list_some_o_options (mode, flags)
395      int mode, flags;
396 {
397   if ((flags & QFLAG) == 0)
398     list_minus_o_opts (mode, (flags & PFLAG));
399   return (EXECUTION_SUCCESS);
400 }
401
402 static int
403 set_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. */
422 static int
423 set_interactive_comments (mode)
424      int mode;
425 {
426   set_shellopts ();
427   return (0);
428 }
429
430 #if defined (RESTRICTED_SHELL)
431 /* Don't allow the value of restricted_shell to be modified. */
432
433 static int
434 set_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 */
446
447 char **
448 get_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 }