aboutsummaryrefslogtreecommitdiffstats
path: root/builtins/shopt.def
blob: 1ee5b5416cb908c1a5c52a769528a5834ae61656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
This file is shopt.def, from which is created shopt.c.
It implements the Bash `shopt' builtin.

Copyright (C) 1994 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any later
version.

Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

$PRODUCES shopt.c

$BUILTIN shopt
$DOCNAME shopt_builtin
$FUNCTION shopt_builtin
$SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
Toggle the values of variables controlling optional behavior.
The -s flag means to enable (set) each OPTNAME; the -u flag
unsets each OPTNAME.  The -q flag suppresses output; the exit
status indicates whether each OPTNAME is set or unset.  The -o
option restricts the OPTNAMEs to those defined for use with
`set -o'.  With no options, or with the -p option, a list of all
settable options is displayed, with an indication of whether or
not each is set.
$END

#include <config.h>

#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#    include <sys/types.h>
#  endif
#  include <unistd.h>
#endif

#include <stdio.h>

#include "../shell.h"
#include "../flags.h"
#include "common.h"
#include "bashgetopt.h"

#define UNSETOPT	0
#define SETOPT		1

#define OPTFMT		"%-15s\t%s\n"

extern int allow_null_glob_expansion, glob_dot_filenames;
extern int cdable_vars, mail_warning, source_uses_path;
extern int no_exit_on_failed_exec, print_shift_error;
extern int check_hashed_filenames, promptvars, interactive_comments;
extern int cdspelling, expand_aliases;
extern int check_window_size;
extern int glob_ignore_case;
extern int hup_on_exit;

#if defined (EXTENDED_GLOB)
extern int extended_glob;
#endif

#if defined (HISTORY)
extern int literal_history, command_oriented_history;
extern int force_append_history;
#endif

#if defined (READLINE)
extern int hist_verify, history_reediting, perform_hostname_completion;
extern void enable_hostname_completion ();
#endif

#if defined (RESTRICTED_SHELL)
extern int restricted_shell;
extern char *shell_name;
#endif

extern void set_shellopts ();

static int set_interactive_comments ();

#if defined (RESTRICTED_SHELL)
static int set_restricted_shell ();
#endif

static struct {
  char *name;
  int  *value;
  Function *set_func;
} shopt_vars[] = {
  { "cdable_vars", &cdable_vars, (Function *)NULL },
  { "cdspell", &cdspelling, (Function *)NULL },
  { "checkhash", &check_hashed_filenames, (Function *)NULL },
  { "checkwinsize", &check_window_size, (Function *)NULL },
#if defined (HISTORY)
  { "cmdhist", &command_oriented_history, (Function *)NULL },
#endif
  { "dotglob", &glob_dot_filenames, (Function *)NULL },
  { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
  { "expand_aliases", &expand_aliases, (Function *)NULL },
#if defined (EXTENDED_GLOB)
  { "extglob", &extended_glob, (Function *)NULL },
#endif
#if defined (READLINE)
  { "histreedit", &history_reediting, (Function *)NULL },
#endif
#if defined (HISTORY)
  { "histappend", &force_append_history, (Function *)NULL },
#endif
#if defined (READLINE)
  { "histverify", &hist_verify, (Function *)NULL },
  { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
#endif
  { "huponexit", &hup_on_exit, (Function *)NULL },
  { "interactive_comments", &interactive_comments, set_interactive_comments },
#if defined (HISTORY)
  { "lithist", &literal_history, (Function *)NULL },
#endif
  { "mailwarn", &mail_warning, (Function *)NULL },
  { "nocaseglob", &glob_ignore_case, (Function *)NULL },
  { "nullglob",	&allow_null_glob_expansion, (Function *)NULL },
  { "promptvars", &promptvars, (Function *)NULL },
#if defined (RESTRICTED_SHELL)
  { "restricted_shell", &restricted_shell, set_restricted_shell },
#endif
  { "shift_verbose", &print_shift_error, (Function *)NULL },
  { "sourcepath", &source_uses_path, (Function *)NULL },
  { (char *)0, (int *)0, (Function *)NULL }
};

static char *on = "on";
static char *off = "off";

static int list_shopt_o_options ();
static int list_some_o_options (), list_some_shopts ();
static int toggle_shopts (), list_shopts (), set_shopt_o_options ();

#define SFLAG	0x01
#define UFLAG	0x02
#define QFLAG	0x04
#define OFLAG	0x08
#define PFLAG	0x10

int
shopt_builtin (list)
     WORD_LIST *list;
{
  int opt, flags, rval;

  flags = 0;
  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "psuoq")) != -1)
    {
      switch (opt)
	{
	case 's':
	  flags |= SFLAG;
	  break;
	case 'u':
	  flags |= UFLAG;
	  break;
	case 'q':
	  flags |= QFLAG;
	  break;
	case 'o':
	  flags |= OFLAG;
	  break;
	case 'p':
	  flags |= PFLAG;
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
    {
      builtin_error ("cannot set and unset shell options simultaneously");
      return (EXECUTION_FAILURE);
    }

  rval = EXECUTION_SUCCESS;
  if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0))	/* shopt -o */
    rval = list_shopt_o_options (list, flags);
  else if (list && (flags & OFLAG))		/* shopt -so args */
    rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
  else if (flags & OFLAG)	/* shopt -so */
    rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
  else if (list && (flags & (SFLAG|UFLAG)))	/* shopt -su args */
    rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
  else if ((flags & (SFLAG|UFLAG)) == 0)	/* shopt [args] */
    rval = list_shopts (list, flags);
  else						/* shopt -su */
    rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
  return (rval);
}

/* Reset the options managed by `shopt' to the values they would have at
   shell startup. */
void
reset_shopt_options ()
{
  allow_null_glob_expansion = glob_dot_filenames = 0;
  cdable_vars = mail_warning = 0;
  no_exit_on_failed_exec = print_shift_error = 0;
  check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;

  source_uses_path = promptvars = 1;

#if defined (EXTENDED_GLOB)
  extended_glob = 0;
#endif

#if defined (HISTORY)
  literal_history = force_append_history = 0;
  command_oriented_history = 1;
#endif

#if defined (READLINE)
  hist_verify = history_reediting = 0;
  perform_hostname_completion = 1;
#endif
}

static int
find_shopt (name)
     char *name;
{
  int i;

  for (i = 0; shopt_vars[i].name; i++)
    if (STREQ (name, shopt_vars[i].name))
      return i;
  return -1;
}

#define SHOPT_ERROR(str)	builtin_error ("%s: unknown shell option name", str)

static int
toggle_shopts (mode, list, quiet)
     int mode;
     WORD_LIST *list;
     int quiet;
{
  WORD_LIST *l;
  int ind, rval;

  for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
    {
      ind = find_shopt (l->word->word);
      if (ind < 0)
	{
	  SHOPT_ERROR (l->word->word);
	  rval = EXECUTION_FAILURE;
	}
      else
	{
	  *shopt_vars[ind].value = mode;	/* 1 for set, 0 for unset */
	  if (shopt_vars[ind].set_func)
	    (*shopt_vars[ind].set_func) (mode);
	}
    }
  return (rval);
}

static void
print_shopt (name, val, flags)
     char *name;
     int val, flags;
{
  if (flags & PFLAG)
    printf ("shopt %s %s\n", val ? "-s" : "-u", name);
  else
    printf (OPTFMT, name, val ? on : off);
}

/* List the values of all or any of the `shopt' options.  Returns 0 if
   all were listed or all variables queried were on; 1 otherwise. */
static int
list_shopts (list, flags)
     WORD_LIST *list;
     int flags;
{
  WORD_LIST *l;
  int i, val, rval;

  if (list == 0)
    {
      for (i = 0; shopt_vars[i].name; i++)
	{
	  val = *shopt_vars[i].value;
	  if ((flags & QFLAG) == 0)
	    print_shopt (shopt_vars[i].name, val, flags);
	}
      return (EXECUTION_SUCCESS);
    }

  for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
    {
      i = find_shopt (l->word->word);
      if (i < 0)
	{
	  SHOPT_ERROR (l->word->word);
	  rval = EXECUTION_FAILURE;
	  continue;
	}
      val = *shopt_vars[i].value;
      if (val == 0)
	rval = EXECUTION_FAILURE;
      if ((flags & QFLAG) == 0)
        print_shopt (l->word->word, val, flags);
    }
  return (rval);
}

static int
list_some_shopts (mode, flags)
     int mode, flags;
{
  int val, i;

  for (i = 0; shopt_vars[i].name; i++)
    {
      val = *shopt_vars[i].value;
      if (((flags & QFLAG) == 0) && mode == val)
	print_shopt (shopt_vars[i].name, val, flags);
    }
  return (EXECUTION_SUCCESS);
}

static int
list_shopt_o_options (list, flags)
     WORD_LIST *list;
     int flags;
{
  WORD_LIST *l;
  int val, rval;

  if (list == 0)
    {
      if ((flags & QFLAG) == 0)
	list_minus_o_opts (-1, (flags & PFLAG));
      return (EXECUTION_SUCCESS);
    }

  for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
    {
      val = minus_o_option_value (l->word->word);
      if (val == -1)
	{
	  builtin_error ("%s: unknown option name", l->word->word);
	  rval = EXECUTION_FAILURE;
	  continue;
	}
      if (val == 0)
	rval = EXECUTION_FAILURE;
      if ((flags & QFLAG) == 0)
	{
	  if (flags & PFLAG)
	    printf ("set %co %s\n", val ? '-' : '+', l->word->word);
	  else
	    printf (OPTFMT, l->word->word, val ? on : off);
	}
    }
  return (rval);
}

static int
list_some_o_options (mode, flags)
     int mode, flags;
{
  if ((flags & QFLAG) == 0)
    list_minus_o_opts (mode, (flags & PFLAG));
  return (EXECUTION_SUCCESS);
}

static int
set_shopt_o_options (mode, list, quiet)
     int mode;
     WORD_LIST *list;
     int quiet;
{
  WORD_LIST *l;
  int rval;

  for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
    {
      if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
	rval = EXECUTION_FAILURE;
    }
  set_shellopts ();
  return rval;
}

/* If we set or unset interactive_comments with shopt, make sure the
   change is reflected in $SHELLOPTS. */
static int
set_interactive_comments (mode)
     int mode;
{
  set_shellopts ();
  return (0);
}

#if defined (RESTRICTED_SHELL)
/* Don't allow the value of restricted_shell to be modified. */

static int
set_restricted_shell (mode)
     int mode;
{
  static int save_restricted = -1;

  if (save_restricted == -1)
    save_restricted = shell_is_restricted (shell_name);

  restricted_shell = save_restricted;
  return (0);
}
#endif /* RESTRICTED_SHELL */