]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/alias.def
Bash-4.3 patch 15
[thirdparty/bash.git] / builtins / alias.def
1 This file is alias.def, from which is created alias.c
2 It implements the builtins "alias" and "unalias" in Bash.
3
4 Copyright (C) 1987-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20
21 $BUILTIN alias
22 $FUNCTION alias_builtin
23 $DEPENDS_ON ALIAS
24 $PRODUCES alias.c
25 $SHORT_DOC alias [-p] [name[=value] ... ]
26 Define or display aliases.
27
28 Without arguments, `alias' prints the list of aliases in the reusable
29 form `alias NAME=VALUE' on standard output.
30
31 Otherwise, an alias is defined for each NAME whose VALUE is given.
32 A trailing space in VALUE causes the next word to be checked for
33 alias substitution when the alias is expanded.
34
35 Options:
36 -p Print all defined aliases in a reusable format
37
38 Exit Status:
39 alias returns true unless a NAME is supplied for which no alias has been
40 defined.
41 $END
42
43 #include <config.h>
44
45 #if defined (ALIAS)
46
47 #if defined (HAVE_UNISTD_H)
48 # ifdef _MINIX
49 # include <sys/types.h>
50 # endif
51 # include <unistd.h>
52 #endif
53
54 # include "../bashansi.h"
55 # include "../bashintl.h"
56
57 # include <stdio.h>
58 # include "../shell.h"
59 # include "../alias.h"
60 # include "common.h"
61 # include "bashgetopt.h"
62
63 /* Flags for print_alias */
64 #define AL_REUSABLE 0x01
65
66 static void print_alias __P((alias_t *, int));
67
68 extern int posixly_correct;
69
70 /* Hack the alias command in a Korn shell way. */
71 int
72 alias_builtin (list)
73 WORD_LIST *list;
74 {
75 int any_failed, offset, pflag, dflags;
76 alias_t **alias_list, *t;
77 char *name, *value;
78
79 dflags = posixly_correct ? 0 : AL_REUSABLE;
80 pflag = 0;
81 reset_internal_getopt ();
82 while ((offset = internal_getopt (list, "p")) != -1)
83 {
84 switch (offset)
85 {
86 case 'p':
87 pflag = 1;
88 dflags |= AL_REUSABLE;
89 break;
90 default:
91 builtin_usage ();
92 return (EX_USAGE);
93 }
94 }
95
96 list = loptend;
97
98 if (list == 0 || pflag)
99 {
100 if (aliases == 0)
101 return (EXECUTION_SUCCESS);
102
103 alias_list = all_aliases ();
104
105 if (alias_list == 0)
106 return (EXECUTION_SUCCESS);
107
108 for (offset = 0; alias_list[offset]; offset++)
109 print_alias (alias_list[offset], dflags);
110
111 free (alias_list); /* XXX - Do not free the strings. */
112
113 if (list == 0)
114 return (sh_chkwrite (EXECUTION_SUCCESS));
115 }
116
117 any_failed = 0;
118 while (list)
119 {
120 name = list->word->word;
121
122 for (offset = 0; name[offset] && name[offset] != '='; offset++)
123 ;
124
125 if (offset && name[offset] == '=')
126 {
127 name[offset] = '\0';
128 value = name + offset + 1;
129
130 if (legal_alias_name (name, 0) == 0)
131 {
132 builtin_error (_("`%s': invalid alias name"), name);
133 any_failed++;
134 }
135 else
136 add_alias (name, value);
137 }
138 else
139 {
140 t = find_alias (name);
141 if (t)
142 print_alias (t, dflags);
143 else
144 {
145 sh_notfound (name);
146 any_failed++;
147 }
148 }
149 list = list->next;
150 }
151
152 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
153 }
154 #endif /* ALIAS */
155
156 $BUILTIN unalias
157 $FUNCTION unalias_builtin
158 $DEPENDS_ON ALIAS
159 $SHORT_DOC unalias [-a] name [name ...]
160 Remove each NAME from the list of defined aliases.
161
162 Options:
163 -a remove all alias definitions.
164
165 Return success unless a NAME is not an existing alias.
166 $END
167
168 #if defined (ALIAS)
169 /* Remove aliases named in LIST from the aliases database. */
170 int
171 unalias_builtin (list)
172 register WORD_LIST *list;
173 {
174 register alias_t *alias;
175 int opt, aflag;
176
177 aflag = 0;
178 reset_internal_getopt ();
179 while ((opt = internal_getopt (list, "a")) != -1)
180 {
181 switch (opt)
182 {
183 case 'a':
184 aflag = 1;
185 break;
186 default:
187 builtin_usage ();
188 return (EX_USAGE);
189 }
190 }
191
192 list = loptend;
193
194 if (aflag)
195 {
196 delete_all_aliases ();
197 return (EXECUTION_SUCCESS);
198 }
199
200 if (list == 0)
201 {
202 builtin_usage ();
203 return (EX_USAGE);
204 }
205
206 aflag = 0;
207 while (list)
208 {
209 alias = find_alias (list->word->word);
210
211 if (alias)
212 remove_alias (alias->name);
213 else
214 {
215 sh_notfound (list->word->word);
216 aflag++;
217 }
218
219 list = list->next;
220 }
221
222 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
223 }
224
225 /* Output ALIAS in such a way as to allow it to be read back in. */
226 static void
227 print_alias (alias, flags)
228 alias_t *alias;
229 int flags;
230 {
231 char *value;
232
233 value = sh_single_quote (alias->value);
234 if (flags & AL_REUSABLE)
235 printf ("alias ");
236 printf ("%s=%s\n", alias->name, value);
237 free (value);
238
239 fflush (stdout);
240 }
241 #endif /* ALIAS */