]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
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 | |
bb70624e | 10 | Software Foundation; either version 2, or (at your option) any later |
ccc6cda3 JA |
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 | |
bb70624e | 20 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
ccc6cda3 JA |
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) | |
cce855bc JA |
41 | # ifdef _MINIX |
42 | # include <sys/types.h> | |
43 | # endif | |
ccc6cda3 JA |
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; | |
cce855bc JA |
65 | extern int glob_ignore_case; |
66 | extern int hup_on_exit; | |
bb70624e | 67 | extern int xpg_echo; |
cce855bc JA |
68 | |
69 | #if defined (EXTENDED_GLOB) | |
70 | extern int extended_glob; | |
71 | #endif | |
ccc6cda3 JA |
72 | |
73 | #if defined (HISTORY) | |
d166f048 | 74 | extern int literal_history, command_oriented_history; |
ccc6cda3 JA |
75 | extern int force_append_history; |
76 | #endif | |
77 | ||
78 | #if defined (READLINE) | |
d166f048 | 79 | extern int hist_verify, history_reediting, perform_hostname_completion; |
bb70624e | 80 | extern int no_empty_command_completion; |
ccc6cda3 JA |
81 | extern void enable_hostname_completion (); |
82 | #endif | |
83 | ||
bb70624e JA |
84 | #if defined (PROGRAMMABLE_COMPLETION) |
85 | extern int prog_completion_enabled; | |
86 | #endif | |
87 | ||
b72432fd JA |
88 | #if defined (RESTRICTED_SHELL) |
89 | extern int restricted_shell; | |
90 | extern char *shell_name; | |
91 | #endif | |
92 | ||
d166f048 JA |
93 | extern void set_shellopts (); |
94 | ||
ccc6cda3 JA |
95 | static int set_interactive_comments (); |
96 | ||
b72432fd JA |
97 | #if defined (RESTRICTED_SHELL) |
98 | static int set_restricted_shell (); | |
99 | #endif | |
100 | ||
ccc6cda3 JA |
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 }, | |
cce855bc JA |
116 | #if defined (EXTENDED_GLOB) |
117 | { "extglob", &extended_glob, (Function *)NULL }, | |
118 | #endif | |
ccc6cda3 JA |
119 | #if defined (READLINE) |
120 | { "histreedit", &history_reediting, (Function *)NULL }, | |
121 | #endif | |
122 | #if defined (HISTORY) | |
123 | { "histappend", &force_append_history, (Function *)NULL }, | |
ccc6cda3 JA |
124 | #endif |
125 | #if defined (READLINE) | |
d166f048 | 126 | { "histverify", &hist_verify, (Function *)NULL }, |
ccc6cda3 JA |
127 | { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion }, |
128 | #endif | |
cce855bc | 129 | { "huponexit", &hup_on_exit, (Function *)NULL }, |
ccc6cda3 JA |
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 }, | |
bb70624e JA |
135 | #if defined (READLINE) |
136 | { "no_empty_cmd_completion", &no_empty_command_completion, (Function *)NULL }, | |
137 | #endif | |
cce855bc | 138 | { "nocaseglob", &glob_ignore_case, (Function *)NULL }, |
ccc6cda3 | 139 | { "nullglob", &allow_null_glob_expansion, (Function *)NULL }, |
bb70624e JA |
140 | #if defined (PROGRAMMABLE_COMPLETION) |
141 | { "progcomp", &prog_completion_enabled, (Function *)NULL }, | |
142 | #endif | |
ccc6cda3 | 143 | { "promptvars", &promptvars, (Function *)NULL }, |
b72432fd JA |
144 | #if defined (RESTRICTED_SHELL) |
145 | { "restricted_shell", &restricted_shell, set_restricted_shell }, | |
146 | #endif | |
ccc6cda3 JA |
147 | { "shift_verbose", &print_shift_error, (Function *)NULL }, |
148 | { "sourcepath", &source_uses_path, (Function *)NULL }, | |
bb70624e | 149 | { "xpg_echo", &xpg_echo, (Function *)NULL }, |
ccc6cda3 JA |
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 */ | |
cce855bc | 208 | rval = list_shopt_o_options (list, flags); |
ccc6cda3 JA |
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 */ | |
cce855bc | 212 | rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags); |
ccc6cda3 JA |
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] */ | |
cce855bc | 216 | rval = list_shopts (list, flags); |
ccc6cda3 | 217 | else /* shopt -su */ |
cce855bc | 218 | rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags); |
ccc6cda3 JA |
219 | return (rval); |
220 | } | |
221 | ||
d166f048 JA |
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 | ||
cce855bc JA |
234 | #if defined (EXTENDED_GLOB) |
235 | extended_glob = 0; | |
236 | #endif | |
237 | ||
d166f048 JA |
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 | ||
ccc6cda3 JA |
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 | ||
b72432fd | 290 | static void |
cce855bc JA |
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 | ||
ccc6cda3 JA |
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 | |
cce855bc | 304 | list_shopts (list, flags) |
ccc6cda3 | 305 | WORD_LIST *list; |
cce855bc | 306 | int flags; |
ccc6cda3 JA |
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; | |
cce855bc JA |
316 | if ((flags & QFLAG) == 0) |
317 | print_shopt (shopt_vars[i].name, val, flags); | |
ccc6cda3 JA |
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; | |
cce855bc JA |
334 | if ((flags & QFLAG) == 0) |
335 | print_shopt (l->word->word, val, flags); | |
ccc6cda3 JA |
336 | } |
337 | return (rval); | |
338 | } | |
339 | ||
340 | static int | |
cce855bc JA |
341 | list_some_shopts (mode, flags) |
342 | int mode, flags; | |
ccc6cda3 JA |
343 | { |
344 | int val, i; | |
345 | ||
346 | for (i = 0; shopt_vars[i].name; i++) | |
347 | { | |
348 | val = *shopt_vars[i].value; | |
cce855bc JA |
349 | if (((flags & QFLAG) == 0) && mode == val) |
350 | print_shopt (shopt_vars[i].name, val, flags); | |
ccc6cda3 JA |
351 | } |
352 | return (EXECUTION_SUCCESS); | |
353 | } | |
354 | ||
355 | static int | |
cce855bc | 356 | list_shopt_o_options (list, flags) |
ccc6cda3 | 357 | WORD_LIST *list; |
cce855bc | 358 | int flags; |
ccc6cda3 JA |
359 | { |
360 | WORD_LIST *l; | |
361 | int val, rval; | |
362 | ||
363 | if (list == 0) | |
364 | { | |
cce855bc JA |
365 | if ((flags & QFLAG) == 0) |
366 | list_minus_o_opts (-1, (flags & PFLAG)); | |
ccc6cda3 JA |
367 | return (EXECUTION_SUCCESS); |
368 | } | |
369 | ||
370 | for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) | |
371 | { | |
372 | val = minus_o_option_value (l->word->word); | |
373 | if (val == -1) | |
374 | { | |
375 | builtin_error ("%s: unknown option name", l->word->word); | |
376 | rval = EXECUTION_FAILURE; | |
377 | continue; | |
378 | } | |
379 | if (val == 0) | |
380 | rval = EXECUTION_FAILURE; | |
cce855bc JA |
381 | if ((flags & QFLAG) == 0) |
382 | { | |
383 | if (flags & PFLAG) | |
384 | printf ("set %co %s\n", val ? '-' : '+', l->word->word); | |
385 | else | |
386 | printf (OPTFMT, l->word->word, val ? on : off); | |
387 | } | |
ccc6cda3 JA |
388 | } |
389 | return (rval); | |
390 | } | |
391 | ||
392 | static int | |
cce855bc JA |
393 | list_some_o_options (mode, flags) |
394 | int mode, flags; | |
ccc6cda3 | 395 | { |
cce855bc JA |
396 | if ((flags & QFLAG) == 0) |
397 | list_minus_o_opts (mode, (flags & PFLAG)); | |
ccc6cda3 JA |
398 | return (EXECUTION_SUCCESS); |
399 | } | |
400 | ||
401 | static int | |
402 | set_shopt_o_options (mode, list, quiet) | |
403 | int mode; | |
404 | WORD_LIST *list; | |
405 | int quiet; | |
406 | { | |
407 | WORD_LIST *l; | |
408 | int rval; | |
409 | ||
410 | for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) | |
411 | { | |
412 | if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE) | |
413 | rval = EXECUTION_FAILURE; | |
414 | } | |
415 | set_shellopts (); | |
416 | return rval; | |
417 | } | |
418 | ||
419 | /* If we set or unset interactive_comments with shopt, make sure the | |
420 | change is reflected in $SHELLOPTS. */ | |
421 | static int | |
422 | set_interactive_comments (mode) | |
423 | int mode; | |
424 | { | |
425 | set_shellopts (); | |
426 | return (0); | |
427 | } | |
b72432fd JA |
428 | |
429 | #if defined (RESTRICTED_SHELL) | |
430 | /* Don't allow the value of restricted_shell to be modified. */ | |
431 | ||
432 | static int | |
433 | set_restricted_shell (mode) | |
434 | int mode; | |
435 | { | |
436 | static int save_restricted = -1; | |
437 | ||
438 | if (save_restricted == -1) | |
439 | save_restricted = shell_is_restricted (shell_name); | |
440 | ||
441 | restricted_shell = save_restricted; | |
442 | return (0); | |
443 | } | |
444 | #endif /* RESTRICTED_SHELL */ | |
bb70624e JA |
445 | |
446 | char ** | |
447 | get_shopt_options () | |
448 | { | |
449 | char **ret; | |
450 | int n, i; | |
451 | ||
452 | n = sizeof (shopt_vars) / sizeof (shopt_vars[0]); | |
453 | ret = alloc_array (n + 1); | |
454 | for (i = 0; shopt_vars[i].name; i++) | |
455 | ret[i] = savestring (shopt_vars[i].name); | |
456 | ret[i] = (char *)NULL; | |
457 | return ret; | |
458 | } |