]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/shopt.def
Imported from ../bash-2.0.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 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 # include <unistd.h>
42 #endif
43
44 #include <stdio.h>
45
46 #include "../shell.h"
47 #include "../flags.h"
48 #include "common.h"
49 #include "bashgetopt.h"
50
51 #define UNSETOPT 0
52 #define SETOPT 1
53
54 #define OPTFMT "%-15s\t%s\n"
55
56 extern int allow_null_glob_expansion, glob_dot_filenames;
57 extern int cdable_vars, mail_warning, source_uses_path;
58 extern int no_exit_on_failed_exec, print_shift_error;
59 extern int check_hashed_filenames, promptvars, interactive_comments;
60 extern int cdspelling, expand_aliases;
61 extern int check_window_size;
62
63 #if defined (HISTORY)
64 extern int hist_verify, literal_history, command_oriented_history;
65 extern int force_append_history;
66 #endif
67
68 #if defined (READLINE)
69 extern int history_reediting, perform_hostname_completion;
70 extern void enable_hostname_completion ();
71 #endif
72
73 static int set_interactive_comments ();
74
75 static struct {
76 char *name;
77 int *value;
78 Function *set_func;
79 } shopt_vars[] = {
80 { "cdable_vars", &cdable_vars, (Function *)NULL },
81 { "cdspell", &cdspelling, (Function *)NULL },
82 { "checkhash", &check_hashed_filenames, (Function *)NULL },
83 { "checkwinsize", &check_window_size, (Function *)NULL },
84 #if defined (HISTORY)
85 { "cmdhist", &command_oriented_history, (Function *)NULL },
86 #endif
87 { "dotglob", &glob_dot_filenames, (Function *)NULL },
88 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
89 { "expand_aliases", &expand_aliases, (Function *)NULL },
90 #if defined (READLINE)
91 { "histreedit", &history_reediting, (Function *)NULL },
92 #endif
93 #if defined (HISTORY)
94 { "histappend", &force_append_history, (Function *)NULL },
95 { "histverify", &hist_verify, (Function *)NULL },
96 #endif
97 #if defined (READLINE)
98 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
99 #endif
100 { "interactive_comments", &interactive_comments, set_interactive_comments },
101 #if defined (HISTORY)
102 { "lithist", &literal_history, (Function *)NULL },
103 #endif
104 { "mailwarn", &mail_warning, (Function *)NULL },
105 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
106 { "promptvars", &promptvars, (Function *)NULL },
107 { "shift_verbose", &print_shift_error, (Function *)NULL },
108 { "sourcepath", &source_uses_path, (Function *)NULL },
109 { (char *)0, (int *)0, (Function *)NULL }
110 };
111
112 static char *on = "on";
113 static char *off = "off";
114
115 static int list_shopt_o_options ();
116 static int list_some_o_options (), list_some_shopts ();
117 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
118
119 #define SFLAG 0x01
120 #define UFLAG 0x02
121 #define QFLAG 0x04
122 #define OFLAG 0x08
123 #define PFLAG 0x10
124
125 int
126 shopt_builtin (list)
127 WORD_LIST *list;
128 {
129 int opt, flags, rval;
130
131 flags = 0;
132 reset_internal_getopt ();
133 while ((opt = internal_getopt (list, "psuoq")) != -1)
134 {
135 switch (opt)
136 {
137 case 's':
138 flags |= SFLAG;
139 break;
140 case 'u':
141 flags |= UFLAG;
142 break;
143 case 'q':
144 flags |= QFLAG;
145 break;
146 case 'o':
147 flags |= OFLAG;
148 break;
149 case 'p':
150 flags |= PFLAG;
151 break;
152 default:
153 builtin_usage ();
154 return (EX_USAGE);
155 }
156 }
157 list = loptend;
158
159 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
160 {
161 builtin_error ("cannot set and unset shell options simultaneously");
162 return (EXECUTION_FAILURE);
163 }
164
165 rval = EXECUTION_SUCCESS;
166 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
167 rval = list_shopt_o_options (list, flags & QFLAG);
168 else if (list && (flags & OFLAG)) /* shopt -so args */
169 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
170 else if (flags & OFLAG) /* shopt -so */
171 rval = list_some_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, flags & QFLAG);
172 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
173 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
174 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
175 rval = list_shopts (list, flags & QFLAG);
176 else /* shopt -su */
177 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags & QFLAG);
178 return (rval);
179 }
180
181 static int
182 find_shopt (name)
183 char *name;
184 {
185 int i;
186
187 for (i = 0; shopt_vars[i].name; i++)
188 if (STREQ (name, shopt_vars[i].name))
189 return i;
190 return -1;
191 }
192
193 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
194
195 static int
196 toggle_shopts (mode, list, quiet)
197 int mode;
198 WORD_LIST *list;
199 int quiet;
200 {
201 WORD_LIST *l;
202 int ind, rval;
203
204 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
205 {
206 ind = find_shopt (l->word->word);
207 if (ind < 0)
208 {
209 SHOPT_ERROR (l->word->word);
210 rval = EXECUTION_FAILURE;
211 }
212 else
213 {
214 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
215 if (shopt_vars[ind].set_func)
216 (*shopt_vars[ind].set_func) (mode);
217 }
218 }
219 return (rval);
220 }
221
222 /* List the values of all or any of the `shopt' options. Returns 0 if
223 all were listed or all variables queried were on; 1 otherwise. */
224 static int
225 list_shopts (list, quiet)
226 WORD_LIST *list;
227 int quiet;
228 {
229 WORD_LIST *l;
230 int i, val, rval;
231
232 if (list == 0)
233 {
234 for (i = 0; shopt_vars[i].name; i++)
235 {
236 val = *shopt_vars[i].value;
237 if (quiet == 0)
238 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
239 }
240 return (EXECUTION_SUCCESS);
241 }
242
243 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
244 {
245 i = find_shopt (l->word->word);
246 if (i < 0)
247 {
248 SHOPT_ERROR (l->word->word);
249 rval = EXECUTION_FAILURE;
250 continue;
251 }
252 val = *shopt_vars[i].value;
253 if (val == 0)
254 rval = EXECUTION_FAILURE;
255 if (quiet == 0)
256 printf (OPTFMT, l->word->word, val ? on : off);
257 }
258 return (rval);
259 }
260
261 static int
262 list_some_shopts (mode, quiet)
263 int mode, quiet;
264 {
265 int val, i;
266
267 for (i = 0; shopt_vars[i].name; i++)
268 {
269 val = *shopt_vars[i].value;
270 if (quiet == 0 && mode == val)
271 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
272 }
273 return (EXECUTION_SUCCESS);
274 }
275
276 static int
277 list_shopt_o_options (list, quiet)
278 WORD_LIST *list;
279 int quiet;
280 {
281 WORD_LIST *l;
282 int val, rval;
283
284 if (list == 0)
285 {
286 if (quiet == 0)
287 list_minus_o_opts (-1);
288 return (EXECUTION_SUCCESS);
289 }
290
291 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
292 {
293 val = minus_o_option_value (l->word->word);
294 if (val == -1)
295 {
296 builtin_error ("%s: unknown option name", l->word->word);
297 rval = EXECUTION_FAILURE;
298 continue;
299 }
300 if (val == 0)
301 rval = EXECUTION_FAILURE;
302 if (quiet == 0)
303 printf (OPTFMT, l->word->word, val ? "on" : "off");
304 }
305 return (rval);
306 }
307
308 static int
309 list_some_o_options (mode, quiet)
310 int mode, quiet;
311 {
312 if (quiet == 0)
313 list_minus_o_opts (mode);
314 return (EXECUTION_SUCCESS);
315 }
316
317 static int
318 set_shopt_o_options (mode, list, quiet)
319 int mode;
320 WORD_LIST *list;
321 int quiet;
322 {
323 WORD_LIST *l;
324 int rval;
325
326 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
327 {
328 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
329 rval = EXECUTION_FAILURE;
330 }
331 set_shellopts ();
332 return rval;
333 }
334
335 /* If we set or unset interactive_comments with shopt, make sure the
336 change is reflected in $SHELLOPTS. */
337 static int
338 set_interactive_comments (mode)
339 int mode;
340 {
341 set_shellopts ();
342 return (0);
343 }