2aeae39db1a7cda354e502962c296f6e51f65593
[platform/upstream/bash.git] / builtins / enable.def
1 This file is enable.def, from which is created enable.c.
2 It implements the builtin "enable" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 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 enable.c
23
24 $BUILTIN enable
25 $FUNCTION enable_builtin
26 $SHORT_DOC enable [-n] [name ...]
27 Enable and disable builtin shell commands.  This allows
28 you to use a disk command which has the same name as a shell
29 builtin.  If -n is used, the NAMEs become disabled.  Otherwise
30 NAMEs are enabled.  For example, to use the `test' found on your
31 path instead of the shell builtin version, you type `enable -n test'.
32 $END
33
34 #include "../shell.h"
35 #include "../builtins.h"
36 #include "common.h"
37
38 #define ENABLED  1
39 #define DISABLED 2
40
41 static int enable_shell_command ();
42 static void list_some_builtins ();
43
44 /* Enable/disable shell commands present in LIST.  If list is not specified,
45    then print out a list of shell commands showing which are enabled and
46    which are disabled. */
47 enable_builtin (list)
48      WORD_LIST *list;
49 {
50   int result = 0, any_failed = 0;
51   int disable_p, all_p;
52
53   disable_p = all_p = 0;
54
55   while (list && list->word->word && list->word->word[0] == '-')
56     {
57       char *arg = list->word->word;
58
59       list = list->next;
60
61       if (ISOPTION (arg, 'n'))
62         disable_p = 1;
63       else if (arg[1] == 'a' && (arg[2] == 0 || strcmp (arg + 2, "ll") == 0))
64         all_p = 1;
65       else if (ISOPTION (arg, '-'))
66         break;
67       else
68         {
69           bad_option (arg);
70           return (EXECUTION_FAILURE);
71         }
72     }
73
74   if (!list)
75     {
76       int filter;
77
78       if (all_p)
79         filter = ENABLED | DISABLED;
80       else if (disable_p)
81         filter = DISABLED;
82       else
83         filter = ENABLED;
84
85       list_some_builtins (filter);
86     }
87   else
88     {
89       while (list)
90         {
91           result = enable_shell_command (list->word->word, disable_p);
92
93           if (!result)
94             {
95               builtin_error ("%s: not a shell builtin", list->word->word);
96               any_failed++;
97             }
98           list = list->next;
99         }
100     }
101   return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
102 }
103
104 /* List some builtins.
105    FILTER is a mask with two slots: ENABLED and DISABLED. */
106 static void
107 list_some_builtins (filter)
108      int filter;
109 {
110   register int i;
111
112   for (i = 0; i < num_shell_builtins; i++)
113     {
114       if (!shell_builtins[i].function)
115         continue;
116
117       if ((filter & ENABLED) &&
118           (shell_builtins[i].flags & BUILTIN_ENABLED))
119         {
120           printf ("enable %s\n", shell_builtins[i].name);
121         }
122       else if ((filter & DISABLED) &&
123                ((shell_builtins[i].flags & BUILTIN_ENABLED) == 0))
124         {
125           printf ("enable -n %s\n", shell_builtins[i].name);
126         }
127     }
128 }
129
130 /* Enable the shell command NAME.  If DISABLE_P is non-zero, then
131    disable NAME instead. */
132 static int
133 enable_shell_command (name, disable_p)
134      char *name;
135      int disable_p;
136 {
137   register int i;
138   int found = 0;
139
140   for (i = 0; i < num_shell_builtins; i++)
141     {
142       if (!shell_builtins[i].function)
143         continue;
144
145       if (STREQ (name, shell_builtins[i].name))
146         {
147           found++;
148
149           if (disable_p)
150             shell_builtins[i].flags &= ~BUILTIN_ENABLED;
151           else
152             shell_builtins[i].flags |= BUILTIN_ENABLED;
153         }
154     }
155   return (found);
156 }