762bb420044b4875798672dddad740069a1275ac
[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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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
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 void enable_hostname_completion ();
80 #endif
81
82 extern void set_shellopts ();
83
84 static int set_interactive_comments ();
85
86 static struct {
87   char *name;
88   int  *value;
89   Function *set_func;
90 } shopt_vars[] = {
91   { "cdable_vars", &cdable_vars, (Function *)NULL },
92   { "cdspell", &cdspelling, (Function *)NULL },
93   { "checkhash", &check_hashed_filenames, (Function *)NULL },
94   { "checkwinsize", &check_window_size, (Function *)NULL },
95 #if defined (HISTORY)
96   { "cmdhist", &command_oriented_history, (Function *)NULL },
97 #endif
98   { "dotglob", &glob_dot_filenames, (Function *)NULL },
99   { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
100   { "expand_aliases", &expand_aliases, (Function *)NULL },
101 #if defined (EXTENDED_GLOB)
102   { "extglob", &extended_glob, (Function *)NULL },
103 #endif
104 #if defined (READLINE)
105   { "histreedit", &history_reediting, (Function *)NULL },
106 #endif
107 #if defined (HISTORY)
108   { "histappend", &force_append_history, (Function *)NULL },
109 #endif
110 #if defined (READLINE)
111   { "histverify", &hist_verify, (Function *)NULL },
112   { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
113 #endif
114   { "huponexit", &hup_on_exit, (Function *)NULL },
115   { "interactive_comments", &interactive_comments, set_interactive_comments },
116 #if defined (HISTORY)
117   { "lithist", &literal_history, (Function *)NULL },
118 #endif
119   { "mailwarn", &mail_warning, (Function *)NULL },
120   { "nocaseglob", &glob_ignore_case, (Function *)NULL },
121   { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
122   { "promptvars", &promptvars, (Function *)NULL },
123   { "shift_verbose", &print_shift_error, (Function *)NULL },
124   { "sourcepath", &source_uses_path, (Function *)NULL },
125   { (char *)0, (int *)0, (Function *)NULL }
126 };
127
128 static char *on = "on";
129 static char *off = "off";
130
131 static int list_shopt_o_options ();
132 static int list_some_o_options (), list_some_shopts ();
133 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
134
135 #define SFLAG   0x01
136 #define UFLAG   0x02
137 #define QFLAG   0x04
138 #define OFLAG   0x08
139 #define PFLAG   0x10
140
141 int
142 shopt_builtin (list)
143      WORD_LIST *list;
144 {
145   int opt, flags, rval;
146
147   flags = 0;
148   reset_internal_getopt ();
149   while ((opt = internal_getopt (list, "psuoq")) != -1)
150     {
151       switch (opt)
152         {
153         case 's':
154           flags |= SFLAG;
155           break;
156         case 'u':
157           flags |= UFLAG;
158           break;
159         case 'q':
160           flags |= QFLAG;
161           break;
162         case 'o':
163           flags |= OFLAG;
164           break;
165         case 'p':
166           flags |= PFLAG;
167           break;
168         default:
169           builtin_usage ();
170           return (EX_USAGE);
171         }
172     }
173   list = loptend;
174
175   if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
176     {
177       builtin_error ("cannot set and unset shell options simultaneously");
178       return (EXECUTION_FAILURE);
179     }
180
181   rval = EXECUTION_SUCCESS;
182   if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0))        /* shopt -o */
183     rval = list_shopt_o_options (list, flags);
184   else if (list && (flags & OFLAG))             /* shopt -so args */
185     rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
186   else if (flags & OFLAG)       /* shopt -so */
187     rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
188   else if (list && (flags & (SFLAG|UFLAG)))     /* shopt -su args */
189     rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
190   else if ((flags & (SFLAG|UFLAG)) == 0)        /* shopt [args] */
191     rval = list_shopts (list, flags);
192   else                                          /* shopt -su */
193     rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
194   return (rval);
195 }
196
197 /* Reset the options managed by `shopt' to the values they would have at
198    shell startup. */
199 void
200 reset_shopt_options ()
201 {
202   allow_null_glob_expansion = glob_dot_filenames = 0;
203   cdable_vars = mail_warning = 0;
204   no_exit_on_failed_exec = print_shift_error = 0;
205   check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
206
207   source_uses_path = promptvars = 1;
208
209 #if defined (EXTENDED_GLOB)
210   extended_glob = 0;
211 #endif
212
213 #if defined (HISTORY)
214   literal_history = force_append_history = 0;
215   command_oriented_history = 1;
216 #endif
217
218 #if defined (READLINE)
219   hist_verify = history_reediting = 0;
220   perform_hostname_completion = 1;
221 #endif
222 }
223
224 static int
225 find_shopt (name)
226      char *name;
227 {
228   int i;
229
230   for (i = 0; shopt_vars[i].name; i++)
231     if (STREQ (name, shopt_vars[i].name))
232       return i;
233   return -1;
234 }
235
236 #define SHOPT_ERROR(str)        builtin_error ("%s: unknown shell option name", str)
237
238 static int
239 toggle_shopts (mode, list, quiet)
240      int mode;
241      WORD_LIST *list;
242      int quiet;
243 {
244   WORD_LIST *l;
245   int ind, rval;
246
247   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
248     {
249       ind = find_shopt (l->word->word);
250       if (ind < 0)
251         {
252           SHOPT_ERROR (l->word->word);
253           rval = EXECUTION_FAILURE;
254         }
255       else
256         {
257           *shopt_vars[ind].value = mode;        /* 1 for set, 0 for unset */
258           if (shopt_vars[ind].set_func)
259             (*shopt_vars[ind].set_func) (mode);
260         }
261     }
262   return (rval);
263 }
264
265 static int
266 print_shopt (name, val, flags)
267      char *name;
268      int val, flags;
269 {
270   if (flags & PFLAG)
271     printf ("shopt %s %s\n", val ? "-s" : "-u", name);
272   else
273     printf (OPTFMT, name, val ? on : off);
274 }
275
276 /* List the values of all or any of the `shopt' options.  Returns 0 if
277    all were listed or all variables queried were on; 1 otherwise. */
278 static int
279 list_shopts (list, flags)
280      WORD_LIST *list;
281      int flags;
282 {
283   WORD_LIST *l;
284   int i, val, rval;
285
286   if (list == 0)
287     {
288       for (i = 0; shopt_vars[i].name; i++)
289         {
290           val = *shopt_vars[i].value;
291           if ((flags & QFLAG) == 0)
292             print_shopt (shopt_vars[i].name, val, flags);
293         }
294       return (EXECUTION_SUCCESS);
295     }
296
297   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
298     {
299       i = find_shopt (l->word->word);
300       if (i < 0)
301         {
302           SHOPT_ERROR (l->word->word);
303           rval = EXECUTION_FAILURE;
304           continue;
305         }
306       val = *shopt_vars[i].value;
307       if (val == 0)
308         rval = EXECUTION_FAILURE;
309       if ((flags & QFLAG) == 0)
310         print_shopt (l->word->word, val, flags);
311     }
312   return (rval);
313 }
314
315 static int
316 list_some_shopts (mode, flags)
317      int mode, flags;
318 {
319   int val, i;
320
321   for (i = 0; shopt_vars[i].name; i++)
322     {
323       val = *shopt_vars[i].value;
324       if (((flags & QFLAG) == 0) && mode == val)
325         print_shopt (shopt_vars[i].name, val, flags);
326     }
327   return (EXECUTION_SUCCESS);
328 }
329
330 static int
331 list_shopt_o_options (list, flags)
332      WORD_LIST *list;
333      int flags;
334 {
335   WORD_LIST *l;
336   int val, rval;
337
338   if (list == 0)
339     {
340       if ((flags & QFLAG) == 0)
341         list_minus_o_opts (-1, (flags & PFLAG));
342       return (EXECUTION_SUCCESS);
343     }
344
345   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
346     {
347       val = minus_o_option_value (l->word->word);
348       if (val == -1)
349         {
350           builtin_error ("%s: unknown option name", l->word->word);
351           rval = EXECUTION_FAILURE;
352           continue;
353         }
354       if (val == 0)
355         rval = EXECUTION_FAILURE;
356       if ((flags & QFLAG) == 0)
357         {
358           if (flags & PFLAG)
359             printf ("set %co %s\n", val ? '-' : '+', l->word->word);
360           else
361             printf (OPTFMT, l->word->word, val ? on : off);
362         }
363     }
364   return (rval);
365 }
366
367 static int
368 list_some_o_options (mode, flags)
369      int mode, flags;
370 {
371   if ((flags & QFLAG) == 0)
372     list_minus_o_opts (mode, (flags & PFLAG));
373   return (EXECUTION_SUCCESS);
374 }
375
376 static int
377 set_shopt_o_options (mode, list, quiet)
378      int mode;
379      WORD_LIST *list;
380      int quiet;
381 {
382   WORD_LIST *l;
383   int rval;
384
385   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
386     {
387       if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
388         rval = EXECUTION_FAILURE;
389     }
390   set_shellopts ();
391   return rval;
392 }
393
394 /* If we set or unset interactive_comments with shopt, make sure the
395    change is reflected in $SHELLOPTS. */
396 static int
397 set_interactive_comments (mode)
398      int mode;
399 {
400   set_shellopts ();
401   return (0);
402 }