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