]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | This file is alias.def, from which is created alias.c |
2 | It implements the builtins "alias" and "unalias" in Bash. | |
3 | ||
8868edaf | 4 | Copyright (C) 1987-2020 Free Software Foundation, Inc. |
726f6388 JA |
5 | |
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
3185942a JA |
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. | |
726f6388 | 12 | |
3185942a JA |
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. | |
726f6388 | 17 | |
3185942a JA |
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/>. | |
726f6388 JA |
20 | |
21 | $BUILTIN alias | |
22 | $FUNCTION alias_builtin | |
23 | $DEPENDS_ON ALIAS | |
24 | $PRODUCES alias.c | |
ccc6cda3 | 25 | $SHORT_DOC alias [-p] [name[=value] ... ] |
3185942a JA |
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 | ||
ccc6cda3 JA |
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 | |
3185942a JA |
33 | alias substitution when the alias is expanded. |
34 | ||
35 | Options: | |
a0c0a00f | 36 | -p print all defined aliases in a reusable format |
3185942a JA |
37 | |
38 | Exit Status: | |
39 | alias returns true unless a NAME is supplied for which no alias has been | |
40 | defined. | |
726f6388 JA |
41 | $END |
42 | ||
ccc6cda3 | 43 | #include <config.h> |
726f6388 JA |
44 | |
45 | #if defined (ALIAS) | |
ccc6cda3 JA |
46 | |
47 | #if defined (HAVE_UNISTD_H) | |
cce855bc JA |
48 | # ifdef _MINIX |
49 | # include <sys/types.h> | |
50 | # endif | |
ccc6cda3 JA |
51 | # include <unistd.h> |
52 | #endif | |
53 | ||
d166f048 | 54 | # include "../bashansi.h" |
b80f6443 | 55 | # include "../bashintl.h" |
d166f048 | 56 | |
726f6388 JA |
57 | # include <stdio.h> |
58 | # include "../shell.h" | |
59 | # include "../alias.h" | |
60 | # include "common.h" | |
ccc6cda3 | 61 | # include "bashgetopt.h" |
726f6388 | 62 | |
b80f6443 JA |
63 | /* Flags for print_alias */ |
64 | #define AL_REUSABLE 0x01 | |
65 | ||
8868edaf | 66 | static void print_alias PARAMS((alias_t *, int)); |
b80f6443 | 67 | |
726f6388 | 68 | /* Hack the alias command in a Korn shell way. */ |
ccc6cda3 | 69 | int |
726f6388 JA |
70 | alias_builtin (list) |
71 | WORD_LIST *list; | |
72 | { | |
b80f6443 | 73 | int any_failed, offset, pflag, dflags; |
ccc6cda3 JA |
74 | alias_t **alias_list, *t; |
75 | char *name, *value; | |
726f6388 | 76 | |
b80f6443 | 77 | dflags = posixly_correct ? 0 : AL_REUSABLE; |
ccc6cda3 JA |
78 | pflag = 0; |
79 | reset_internal_getopt (); | |
80 | while ((offset = internal_getopt (list, "p")) != -1) | |
726f6388 | 81 | { |
ccc6cda3 JA |
82 | switch (offset) |
83 | { | |
84 | case 'p': | |
85 | pflag = 1; | |
b80f6443 | 86 | dflags |= AL_REUSABLE; |
ccc6cda3 | 87 | break; |
a0c0a00f | 88 | CASE_HELPOPT; |
ccc6cda3 JA |
89 | default: |
90 | builtin_usage (); | |
91 | return (EX_USAGE); | |
92 | } | |
93 | } | |
726f6388 | 94 | |
ccc6cda3 JA |
95 | list = loptend; |
96 | ||
97 | if (list == 0 || pflag) | |
98 | { | |
99 | if (aliases == 0) | |
cce855bc | 100 | return (EXECUTION_SUCCESS); |
726f6388 JA |
101 | |
102 | alias_list = all_aliases (); | |
103 | ||
ccc6cda3 | 104 | if (alias_list == 0) |
cce855bc | 105 | return (EXECUTION_SUCCESS); |
726f6388 | 106 | |
ccc6cda3 | 107 | for (offset = 0; alias_list[offset]; offset++) |
b80f6443 | 108 | print_alias (alias_list[offset], dflags); |
726f6388 JA |
109 | |
110 | free (alias_list); /* XXX - Do not free the strings. */ | |
ccc6cda3 JA |
111 | |
112 | if (list == 0) | |
3185942a | 113 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
726f6388 | 114 | } |
ccc6cda3 JA |
115 | |
116 | any_failed = 0; | |
117 | while (list) | |
726f6388 | 118 | { |
ccc6cda3 | 119 | name = list->word->word; |
726f6388 | 120 | |
ccc6cda3 JA |
121 | for (offset = 0; name[offset] && name[offset] != '='; offset++) |
122 | ; | |
726f6388 | 123 | |
ccc6cda3 JA |
124 | if (offset && name[offset] == '=') |
125 | { | |
126 | name[offset] = '\0'; | |
127 | value = name + offset + 1; | |
726f6388 | 128 | |
b80f6443 JA |
129 | if (legal_alias_name (name, 0) == 0) |
130 | { | |
131 | builtin_error (_("`%s': invalid alias name"), name); | |
132 | any_failed++; | |
133 | } | |
134 | else | |
135 | add_alias (name, value); | |
ccc6cda3 JA |
136 | } |
137 | else | |
138 | { | |
139 | t = find_alias (name); | |
140 | if (t) | |
b80f6443 | 141 | print_alias (t, dflags); |
726f6388 JA |
142 | else |
143 | { | |
7117c2d2 | 144 | sh_notfound (name); |
ccc6cda3 | 145 | any_failed++; |
726f6388 | 146 | } |
726f6388 | 147 | } |
ccc6cda3 | 148 | list = list->next; |
726f6388 | 149 | } |
ccc6cda3 JA |
150 | |
151 | return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS); | |
726f6388 JA |
152 | } |
153 | #endif /* ALIAS */ | |
154 | ||
155 | $BUILTIN unalias | |
156 | $FUNCTION unalias_builtin | |
157 | $DEPENDS_ON ALIAS | |
b80f6443 | 158 | $SHORT_DOC unalias [-a] name [name ...] |
3185942a JA |
159 | Remove each NAME from the list of defined aliases. |
160 | ||
161 | Options: | |
a0c0a00f | 162 | -a remove all alias definitions |
3185942a JA |
163 | |
164 | Return success unless a NAME is not an existing alias. | |
726f6388 JA |
165 | $END |
166 | ||
167 | #if defined (ALIAS) | |
168 | /* Remove aliases named in LIST from the aliases database. */ | |
ccc6cda3 | 169 | int |
726f6388 JA |
170 | unalias_builtin (list) |
171 | register WORD_LIST *list; | |
172 | { | |
ccc6cda3 JA |
173 | register alias_t *alias; |
174 | int opt, aflag; | |
726f6388 | 175 | |
ccc6cda3 JA |
176 | aflag = 0; |
177 | reset_internal_getopt (); | |
178 | while ((opt = internal_getopt (list, "a")) != -1) | |
726f6388 | 179 | { |
ccc6cda3 | 180 | switch (opt) |
726f6388 | 181 | { |
ccc6cda3 JA |
182 | case 'a': |
183 | aflag = 1; | |
726f6388 | 184 | break; |
a0c0a00f | 185 | CASE_HELPOPT; |
ccc6cda3 JA |
186 | default: |
187 | builtin_usage (); | |
188 | return (EX_USAGE); | |
726f6388 | 189 | } |
726f6388 JA |
190 | } |
191 | ||
ccc6cda3 JA |
192 | list = loptend; |
193 | ||
194 | if (aflag) | |
195 | { | |
196 | delete_all_aliases (); | |
197 | return (EXECUTION_SUCCESS); | |
198 | } | |
199 | ||
b80f6443 JA |
200 | if (list == 0) |
201 | { | |
202 | builtin_usage (); | |
203 | return (EX_USAGE); | |
204 | } | |
205 | ||
ccc6cda3 | 206 | aflag = 0; |
726f6388 JA |
207 | while (list) |
208 | { | |
209 | alias = find_alias (list->word->word); | |
210 | ||
211 | if (alias) | |
212 | remove_alias (alias->name); | |
213 | else | |
214 | { | |
7117c2d2 | 215 | sh_notfound (list->word->word); |
ccc6cda3 | 216 | aflag++; |
726f6388 JA |
217 | } |
218 | ||
219 | list = list->next; | |
220 | } | |
221 | ||
ccc6cda3 | 222 | return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
726f6388 JA |
223 | } |
224 | ||
225 | /* Output ALIAS in such a way as to allow it to be read back in. */ | |
226 | static void | |
b80f6443 | 227 | print_alias (alias, flags) |
ccc6cda3 | 228 | alias_t *alias; |
b80f6443 | 229 | int flags; |
726f6388 | 230 | { |
ccc6cda3 | 231 | char *value; |
726f6388 | 232 | |
28ef6c31 | 233 | value = sh_single_quote (alias->value); |
b80f6443 | 234 | if (flags & AL_REUSABLE) |
a0c0a00f | 235 | printf ("alias %s", (alias->name && alias->name[0] == '-') ? "-- " : ""); |
b80f6443 | 236 | printf ("%s=%s\n", alias->name, value); |
726f6388 JA |
237 | free (value); |
238 | ||
239 | fflush (stdout); | |
240 | } | |
241 | #endif /* ALIAS */ |