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-2004 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
---|
21 |
|
---|
22 | $BUILTIN alias
|
---|
23 | $FUNCTION alias_builtin
|
---|
24 | $DEPENDS_ON ALIAS
|
---|
25 | $PRODUCES alias.c
|
---|
26 | $SHORT_DOC alias [-p] [name[=value] ... ]
|
---|
27 | `alias' with no arguments or with the -p option prints the list
|
---|
28 | of aliases in the form alias NAME=VALUE on standard output.
|
---|
29 | Otherwise, an alias is defined for each NAME whose VALUE is given.
|
---|
30 | A trailing space in VALUE causes the next word to be checked for
|
---|
31 | alias substitution when the alias is expanded. Alias returns
|
---|
32 | true unless a NAME is given for which no alias has been defined.
|
---|
33 | $END
|
---|
34 |
|
---|
35 | #include <config.h>
|
---|
36 |
|
---|
37 | #if defined (ALIAS)
|
---|
38 |
|
---|
39 | #if defined (HAVE_UNISTD_H)
|
---|
40 | # ifdef _MINIX
|
---|
41 | # include <sys/types.h>
|
---|
42 | # endif
|
---|
43 | # include <unistd.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | # include "../bashansi.h"
|
---|
47 | # include "../bashintl.h"
|
---|
48 |
|
---|
49 | # include <stdio.h>
|
---|
50 | # include "../shell.h"
|
---|
51 | # include "../alias.h"
|
---|
52 | # include "common.h"
|
---|
53 | # include "bashgetopt.h"
|
---|
54 |
|
---|
55 | /* Flags for print_alias */
|
---|
56 | #define AL_REUSABLE 0x01
|
---|
57 |
|
---|
58 | static void print_alias __P((alias_t *, int));
|
---|
59 |
|
---|
60 | extern int posixly_correct;
|
---|
61 |
|
---|
62 | /* Hack the alias command in a Korn shell way. */
|
---|
63 | int
|
---|
64 | alias_builtin (list)
|
---|
65 | WORD_LIST *list;
|
---|
66 | {
|
---|
67 | int any_failed, offset, pflag, dflags;
|
---|
68 | alias_t **alias_list, *t;
|
---|
69 | char *name, *value;
|
---|
70 |
|
---|
71 | dflags = posixly_correct ? 0 : AL_REUSABLE;
|
---|
72 | pflag = 0;
|
---|
73 | reset_internal_getopt ();
|
---|
74 | while ((offset = internal_getopt (list, "p")) != -1)
|
---|
75 | {
|
---|
76 | switch (offset)
|
---|
77 | {
|
---|
78 | case 'p':
|
---|
79 | pflag = 1;
|
---|
80 | dflags |= AL_REUSABLE;
|
---|
81 | break;
|
---|
82 | default:
|
---|
83 | builtin_usage ();
|
---|
84 | return (EX_USAGE);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | list = loptend;
|
---|
89 |
|
---|
90 | if (list == 0 || pflag)
|
---|
91 | {
|
---|
92 | if (aliases == 0)
|
---|
93 | return (EXECUTION_SUCCESS);
|
---|
94 |
|
---|
95 | alias_list = all_aliases ();
|
---|
96 |
|
---|
97 | if (alias_list == 0)
|
---|
98 | return (EXECUTION_SUCCESS);
|
---|
99 |
|
---|
100 | for (offset = 0; alias_list[offset]; offset++)
|
---|
101 | print_alias (alias_list[offset], dflags);
|
---|
102 |
|
---|
103 | free (alias_list); /* XXX - Do not free the strings. */
|
---|
104 |
|
---|
105 | if (list == 0)
|
---|
106 | return (EXECUTION_SUCCESS);
|
---|
107 | }
|
---|
108 |
|
---|
109 | any_failed = 0;
|
---|
110 | while (list)
|
---|
111 | {
|
---|
112 | name = list->word->word;
|
---|
113 |
|
---|
114 | for (offset = 0; name[offset] && name[offset] != '='; offset++)
|
---|
115 | ;
|
---|
116 |
|
---|
117 | if (offset && name[offset] == '=')
|
---|
118 | {
|
---|
119 | name[offset] = '\0';
|
---|
120 | value = name + offset + 1;
|
---|
121 |
|
---|
122 | if (legal_alias_name (name, 0) == 0)
|
---|
123 | {
|
---|
124 | builtin_error (_("`%s': invalid alias name"), name);
|
---|
125 | any_failed++;
|
---|
126 | }
|
---|
127 | else
|
---|
128 | add_alias (name, value);
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | t = find_alias (name);
|
---|
133 | if (t)
|
---|
134 | print_alias (t, dflags);
|
---|
135 | else
|
---|
136 | {
|
---|
137 | sh_notfound (name);
|
---|
138 | any_failed++;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | list = list->next;
|
---|
142 | }
|
---|
143 |
|
---|
144 | return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
|
---|
145 | }
|
---|
146 | #endif /* ALIAS */
|
---|
147 |
|
---|
148 | $BUILTIN unalias
|
---|
149 | $FUNCTION unalias_builtin
|
---|
150 | $DEPENDS_ON ALIAS
|
---|
151 | $SHORT_DOC unalias [-a] name [name ...]
|
---|
152 | Remove NAMEs from the list of defined aliases. If the -a option is given,
|
---|
153 | then remove all alias definitions.
|
---|
154 | $END
|
---|
155 |
|
---|
156 | #if defined (ALIAS)
|
---|
157 | /* Remove aliases named in LIST from the aliases database. */
|
---|
158 | int
|
---|
159 | unalias_builtin (list)
|
---|
160 | register WORD_LIST *list;
|
---|
161 | {
|
---|
162 | register alias_t *alias;
|
---|
163 | int opt, aflag;
|
---|
164 |
|
---|
165 | aflag = 0;
|
---|
166 | reset_internal_getopt ();
|
---|
167 | while ((opt = internal_getopt (list, "a")) != -1)
|
---|
168 | {
|
---|
169 | switch (opt)
|
---|
170 | {
|
---|
171 | case 'a':
|
---|
172 | aflag = 1;
|
---|
173 | break;
|
---|
174 | default:
|
---|
175 | builtin_usage ();
|
---|
176 | return (EX_USAGE);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | list = loptend;
|
---|
181 |
|
---|
182 | if (aflag)
|
---|
183 | {
|
---|
184 | delete_all_aliases ();
|
---|
185 | return (EXECUTION_SUCCESS);
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (list == 0)
|
---|
189 | {
|
---|
190 | builtin_usage ();
|
---|
191 | return (EX_USAGE);
|
---|
192 | }
|
---|
193 |
|
---|
194 | aflag = 0;
|
---|
195 | while (list)
|
---|
196 | {
|
---|
197 | alias = find_alias (list->word->word);
|
---|
198 |
|
---|
199 | if (alias)
|
---|
200 | remove_alias (alias->name);
|
---|
201 | else
|
---|
202 | {
|
---|
203 | sh_notfound (list->word->word);
|
---|
204 | aflag++;
|
---|
205 | }
|
---|
206 |
|
---|
207 | list = list->next;
|
---|
208 | }
|
---|
209 |
|
---|
210 | return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* Output ALIAS in such a way as to allow it to be read back in. */
|
---|
214 | static void
|
---|
215 | print_alias (alias, flags)
|
---|
216 | alias_t *alias;
|
---|
217 | int flags;
|
---|
218 | {
|
---|
219 | char *value;
|
---|
220 |
|
---|
221 | value = sh_single_quote (alias->value);
|
---|
222 | if (flags & AL_REUSABLE)
|
---|
223 | printf ("alias ");
|
---|
224 | printf ("%s=%s\n", alias->name, value);
|
---|
225 | free (value);
|
---|
226 |
|
---|
227 | fflush (stdout);
|
---|
228 | }
|
---|
229 | #endif /* ALIAS */
|
---|