]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shopt.def
Imported from ../bash-2.01.1.tar.gz.
[thirdparty/bash.git] / builtins / shopt.def
CommitLineData
ccc6cda3
JA
1This file is shopt.def, from which is created shopt.c.
2It implements the Bash `shopt' builtin.
3
4Copyright (C) 1994 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 1, or (at your option) any later
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
20Foundation, 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...]
28Toggle the values of variables controlling optional behavior.
29The -s flag means to enable (set) each OPTNAME; the -u flag
30unsets each OPTNAME. The -q flag suppresses output; the exit
31status indicates whether each OPTNAME is set or unset. The -o
32option restricts the OPTNAMEs to those defined for use with
33`set -o'. With no options, or with the -p option, a list of all
34settable options is displayed, with an indication of whether or
35not 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
56extern int allow_null_glob_expansion, glob_dot_filenames;
57extern int cdable_vars, mail_warning, source_uses_path;
58extern int no_exit_on_failed_exec, print_shift_error;
59extern int check_hashed_filenames, promptvars, interactive_comments;
60extern int cdspelling, expand_aliases;
61extern int check_window_size;
62
63#if defined (HISTORY)
d166f048 64extern int literal_history, command_oriented_history;
ccc6cda3
JA
65extern int force_append_history;
66#endif
67
68#if defined (READLINE)
d166f048 69extern int hist_verify, history_reediting, perform_hostname_completion;
ccc6cda3
JA
70extern void enable_hostname_completion ();
71#endif
72
d166f048
JA
73extern void set_shellopts ();
74
ccc6cda3
JA
75static int set_interactive_comments ();
76
77static struct {
78 char *name;
79 int *value;
80 Function *set_func;
81} shopt_vars[] = {
82 { "cdable_vars", &cdable_vars, (Function *)NULL },
83 { "cdspell", &cdspelling, (Function *)NULL },
84 { "checkhash", &check_hashed_filenames, (Function *)NULL },
85 { "checkwinsize", &check_window_size, (Function *)NULL },
86#if defined (HISTORY)
87 { "cmdhist", &command_oriented_history, (Function *)NULL },
88#endif
89 { "dotglob", &glob_dot_filenames, (Function *)NULL },
90 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
91 { "expand_aliases", &expand_aliases, (Function *)NULL },
92#if defined (READLINE)
93 { "histreedit", &history_reediting, (Function *)NULL },
94#endif
95#if defined (HISTORY)
96 { "histappend", &force_append_history, (Function *)NULL },
ccc6cda3
JA
97#endif
98#if defined (READLINE)
d166f048 99 { "histverify", &hist_verify, (Function *)NULL },
ccc6cda3
JA
100 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
101#endif
102 { "interactive_comments", &interactive_comments, set_interactive_comments },
103#if defined (HISTORY)
104 { "lithist", &literal_history, (Function *)NULL },
105#endif
106 { "mailwarn", &mail_warning, (Function *)NULL },
107 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
108 { "promptvars", &promptvars, (Function *)NULL },
109 { "shift_verbose", &print_shift_error, (Function *)NULL },
110 { "sourcepath", &source_uses_path, (Function *)NULL },
111 { (char *)0, (int *)0, (Function *)NULL }
112};
113
114static char *on = "on";
115static char *off = "off";
116
117static int list_shopt_o_options ();
118static int list_some_o_options (), list_some_shopts ();
119static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
120
121#define SFLAG 0x01
122#define UFLAG 0x02
123#define QFLAG 0x04
124#define OFLAG 0x08
125#define PFLAG 0x10
126
127int
128shopt_builtin (list)
129 WORD_LIST *list;
130{
131 int opt, flags, rval;
132
133 flags = 0;
134 reset_internal_getopt ();
135 while ((opt = internal_getopt (list, "psuoq")) != -1)
136 {
137 switch (opt)
138 {
139 case 's':
140 flags |= SFLAG;
141 break;
142 case 'u':
143 flags |= UFLAG;
144 break;
145 case 'q':
146 flags |= QFLAG;
147 break;
148 case 'o':
149 flags |= OFLAG;
150 break;
151 case 'p':
152 flags |= PFLAG;
153 break;
154 default:
155 builtin_usage ();
156 return (EX_USAGE);
157 }
158 }
159 list = loptend;
160
161 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
162 {
163 builtin_error ("cannot set and unset shell options simultaneously");
164 return (EXECUTION_FAILURE);
165 }
166
167 rval = EXECUTION_SUCCESS;
168 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
169 rval = list_shopt_o_options (list, flags & QFLAG);
170 else if (list && (flags & OFLAG)) /* shopt -so args */
171 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
172 else if (flags & OFLAG) /* shopt -so */
173 rval = list_some_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, flags & QFLAG);
174 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
175 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
176 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
177 rval = list_shopts (list, flags & QFLAG);
178 else /* shopt -su */
179 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags & QFLAG);
180 return (rval);
181}
182
d166f048
JA
183/* Reset the options managed by `shopt' to the values they would have at
184 shell startup. */
185void
186reset_shopt_options ()
187{
188 allow_null_glob_expansion = glob_dot_filenames = 0;
189 cdable_vars = mail_warning = 0;
190 no_exit_on_failed_exec = print_shift_error = 0;
191 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
192
193 source_uses_path = promptvars = 1;
194
195#if defined (HISTORY)
196 literal_history = force_append_history = 0;
197 command_oriented_history = 1;
198#endif
199
200#if defined (READLINE)
201 hist_verify = history_reediting = 0;
202 perform_hostname_completion = 1;
203#endif
204}
205
ccc6cda3
JA
206static int
207find_shopt (name)
208 char *name;
209{
210 int i;
211
212 for (i = 0; shopt_vars[i].name; i++)
213 if (STREQ (name, shopt_vars[i].name))
214 return i;
215 return -1;
216}
217
218#define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
219
220static int
221toggle_shopts (mode, list, quiet)
222 int mode;
223 WORD_LIST *list;
224 int quiet;
225{
226 WORD_LIST *l;
227 int ind, rval;
228
229 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
230 {
231 ind = find_shopt (l->word->word);
232 if (ind < 0)
233 {
234 SHOPT_ERROR (l->word->word);
235 rval = EXECUTION_FAILURE;
236 }
237 else
238 {
239 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
240 if (shopt_vars[ind].set_func)
241 (*shopt_vars[ind].set_func) (mode);
242 }
243 }
244 return (rval);
245}
246
247/* List the values of all or any of the `shopt' options. Returns 0 if
248 all were listed or all variables queried were on; 1 otherwise. */
249static int
250list_shopts (list, quiet)
251 WORD_LIST *list;
252 int quiet;
253{
254 WORD_LIST *l;
255 int i, val, rval;
256
257 if (list == 0)
258 {
259 for (i = 0; shopt_vars[i].name; i++)
260 {
261 val = *shopt_vars[i].value;
262 if (quiet == 0)
263 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
264 }
265 return (EXECUTION_SUCCESS);
266 }
267
268 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
269 {
270 i = find_shopt (l->word->word);
271 if (i < 0)
272 {
273 SHOPT_ERROR (l->word->word);
274 rval = EXECUTION_FAILURE;
275 continue;
276 }
277 val = *shopt_vars[i].value;
278 if (val == 0)
279 rval = EXECUTION_FAILURE;
280 if (quiet == 0)
281 printf (OPTFMT, l->word->word, val ? on : off);
282 }
283 return (rval);
284}
285
286static int
287list_some_shopts (mode, quiet)
288 int mode, quiet;
289{
290 int val, i;
291
292 for (i = 0; shopt_vars[i].name; i++)
293 {
294 val = *shopt_vars[i].value;
295 if (quiet == 0 && mode == val)
296 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
297 }
298 return (EXECUTION_SUCCESS);
299}
300
301static int
302list_shopt_o_options (list, quiet)
303 WORD_LIST *list;
304 int quiet;
305{
306 WORD_LIST *l;
307 int val, rval;
308
309 if (list == 0)
310 {
311 if (quiet == 0)
312 list_minus_o_opts (-1);
313 return (EXECUTION_SUCCESS);
314 }
315
316 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
317 {
318 val = minus_o_option_value (l->word->word);
319 if (val == -1)
320 {
321 builtin_error ("%s: unknown option name", l->word->word);
322 rval = EXECUTION_FAILURE;
323 continue;
324 }
325 if (val == 0)
326 rval = EXECUTION_FAILURE;
327 if (quiet == 0)
328 printf (OPTFMT, l->word->word, val ? "on" : "off");
329 }
330 return (rval);
331}
332
333static int
334list_some_o_options (mode, quiet)
335 int mode, quiet;
336{
337 if (quiet == 0)
338 list_minus_o_opts (mode);
339 return (EXECUTION_SUCCESS);
340}
341
342static int
343set_shopt_o_options (mode, list, quiet)
344 int mode;
345 WORD_LIST *list;
346 int quiet;
347{
348 WORD_LIST *l;
349 int rval;
350
351 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
352 {
353 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
354 rval = EXECUTION_FAILURE;
355 }
356 set_shellopts ();
357 return rval;
358}
359
360/* If we set or unset interactive_comments with shopt, make sure the
361 change is reflected in $SHELLOPTS. */
362static int
363set_interactive_comments (mode)
364 int mode;
365{
366 set_shellopts ();
367 return (0);
368}