blob: d760cebe07b9ebedef84a61139cb162a6495afba [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is alias.def, from which is created alias.c
2It implements the builtins "alias" and "unalias" in Bash.
3
Jari Aalto31859422009-01-12 13:36:28 +00004Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$BUILTIN alias
22$FUNCTION alias_builtin
23$DEPENDS_ON ALIAS
24$PRODUCES alias.c
Jari Aaltoccc6cda1996-12-23 17:02:34 +000025$SHORT_DOC alias [-p] [name[=value] ... ]
Jari Aalto31859422009-01-12 13:36:28 +000026Define or display aliases.
27
28Without arguments, `alias' prints the list of aliases in the reusable
29form `alias NAME=VALUE' on standard output.
30
Jari Aaltoccc6cda1996-12-23 17:02:34 +000031Otherwise, an alias is defined for each NAME whose VALUE is given.
32A trailing space in VALUE causes the next word to be checked for
Jari Aalto31859422009-01-12 13:36:28 +000033alias substitution when the alias is expanded.
34
35Options:
36 -p Print all defined aliases in a reusable format
37
38Exit Status:
39alias returns true unless a NAME is supplied for which no alias has been
40defined.
Jari Aalto726f6381996-08-26 18:22:31 +000041$END
42
Jari Aaltoccc6cda1996-12-23 17:02:34 +000043#include <config.h>
Jari Aalto726f6381996-08-26 18:22:31 +000044
45#if defined (ALIAS)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000046
47#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000048# ifdef _MINIX
49# include <sys/types.h>
50# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051# include <unistd.h>
52#endif
53
Jari Aaltod166f041997-06-05 14:59:13 +000054# include "../bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000055# include "../bashintl.h"
Jari Aaltod166f041997-06-05 14:59:13 +000056
Jari Aalto726f6381996-08-26 18:22:31 +000057# include <stdio.h>
58# include "../shell.h"
59# include "../alias.h"
60# include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000061# include "bashgetopt.h"
Jari Aalto726f6381996-08-26 18:22:31 +000062
Jari Aaltob80f6442004-07-27 13:29:18 +000063/* Flags for print_alias */
64#define AL_REUSABLE 0x01
65
66static void print_alias __P((alias_t *, int));
67
68extern int posixly_correct;
Jari Aalto726f6381996-08-26 18:22:31 +000069
70/* Hack the alias command in a Korn shell way. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000071int
Jari Aalto726f6381996-08-26 18:22:31 +000072alias_builtin (list)
73 WORD_LIST *list;
74{
Jari Aaltob80f6442004-07-27 13:29:18 +000075 int any_failed, offset, pflag, dflags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076 alias_t **alias_list, *t;
77 char *name, *value;
Jari Aalto726f6381996-08-26 18:22:31 +000078
Jari Aaltob80f6442004-07-27 13:29:18 +000079 dflags = posixly_correct ? 0 : AL_REUSABLE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080 pflag = 0;
81 reset_internal_getopt ();
82 while ((offset = internal_getopt (list, "p")) != -1)
Jari Aalto726f6381996-08-26 18:22:31 +000083 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +000084 switch (offset)
85 {
86 case 'p':
87 pflag = 1;
Jari Aaltob80f6442004-07-27 13:29:18 +000088 dflags |= AL_REUSABLE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089 break;
90 default:
91 builtin_usage ();
92 return (EX_USAGE);
93 }
94 }
Jari Aalto726f6381996-08-26 18:22:31 +000095
Jari Aaltoccc6cda1996-12-23 17:02:34 +000096 list = loptend;
97
98 if (list == 0 || pflag)
99 {
100 if (aliases == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +0000101 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000102
103 alias_list = all_aliases ();
104
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000105 if (alias_list == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +0000106 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000107
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108 for (offset = 0; alias_list[offset]; offset++)
Jari Aaltob80f6442004-07-27 13:29:18 +0000109 print_alias (alias_list[offset], dflags);
Jari Aalto726f6381996-08-26 18:22:31 +0000110
111 free (alias_list); /* XXX - Do not free the strings. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000112
113 if (list == 0)
Jari Aalto31859422009-01-12 13:36:28 +0000114 return (sh_chkwrite (EXECUTION_SUCCESS));
Jari Aalto726f6381996-08-26 18:22:31 +0000115 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000116
117 any_failed = 0;
118 while (list)
Jari Aalto726f6381996-08-26 18:22:31 +0000119 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000120 name = list->word->word;
121
122 for (offset = 0; name[offset] && name[offset] != '='; offset++)
123 ;
124
125 if (offset && name[offset] == '=')
Jari Aalto726f6381996-08-26 18:22:31 +0000126 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000127 name[offset] = '\0';
128 value = name + offset + 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000129
Jari Aaltob80f6442004-07-27 13:29:18 +0000130 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);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000137 }
138 else
139 {
140 t = find_alias (name);
141 if (t)
Jari Aaltob80f6442004-07-27 13:29:18 +0000142 print_alias (t, dflags);
Jari Aalto726f6381996-08-26 18:22:31 +0000143 else
144 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000145 sh_notfound (name);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000146 any_failed++;
Jari Aalto726f6381996-08-26 18:22:31 +0000147 }
Jari Aalto726f6381996-08-26 18:22:31 +0000148 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000149 list = list->next;
Jari Aalto726f6381996-08-26 18:22:31 +0000150 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000151
152 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000153}
154#endif /* ALIAS */
155
156$BUILTIN unalias
157$FUNCTION unalias_builtin
158$DEPENDS_ON ALIAS
Jari Aaltob80f6442004-07-27 13:29:18 +0000159$SHORT_DOC unalias [-a] name [name ...]
Jari Aalto31859422009-01-12 13:36:28 +0000160Remove each NAME from the list of defined aliases.
161
162Options:
163 -a remove all alias definitions.
164
165Return success unless a NAME is not an existing alias.
Jari Aalto726f6381996-08-26 18:22:31 +0000166$END
167
168#if defined (ALIAS)
169/* Remove aliases named in LIST from the aliases database. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000170int
Jari Aalto726f6381996-08-26 18:22:31 +0000171unalias_builtin (list)
172 register WORD_LIST *list;
173{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000174 register alias_t *alias;
175 int opt, aflag;
Jari Aalto726f6381996-08-26 18:22:31 +0000176
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000177 aflag = 0;
178 reset_internal_getopt ();
179 while ((opt = internal_getopt (list, "a")) != -1)
Jari Aalto726f6381996-08-26 18:22:31 +0000180 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000181 switch (opt)
Jari Aalto726f6381996-08-26 18:22:31 +0000182 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000183 case 'a':
184 aflag = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000185 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000186 default:
187 builtin_usage ();
188 return (EX_USAGE);
Jari Aalto726f6381996-08-26 18:22:31 +0000189 }
190 }
191
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000192 list = loptend;
193
194 if (aflag)
195 {
196 delete_all_aliases ();
197 return (EXECUTION_SUCCESS);
198 }
199
Jari Aaltob80f6442004-07-27 13:29:18 +0000200 if (list == 0)
201 {
202 builtin_usage ();
203 return (EX_USAGE);
204 }
205
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000206 aflag = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000207 while (list)
208 {
209 alias = find_alias (list->word->word);
210
211 if (alias)
212 remove_alias (alias->name);
213 else
214 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000215 sh_notfound (list->word->word);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000216 aflag++;
Jari Aalto726f6381996-08-26 18:22:31 +0000217 }
218
219 list = list->next;
220 }
221
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000222 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000223}
224
225/* Output ALIAS in such a way as to allow it to be read back in. */
226static void
Jari Aaltob80f6442004-07-27 13:29:18 +0000227print_alias (alias, flags)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000228 alias_t *alias;
Jari Aaltob80f6442004-07-27 13:29:18 +0000229 int flags;
Jari Aalto726f6381996-08-26 18:22:31 +0000230{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000231 char *value;
Jari Aalto726f6381996-08-26 18:22:31 +0000232
Jari Aalto28ef6c32001-04-06 19:14:31 +0000233 value = sh_single_quote (alias->value);
Jari Aaltob80f6442004-07-27 13:29:18 +0000234 if (flags & AL_REUSABLE)
235 printf ("alias ");
236 printf ("%s=%s\n", alias->name, value);
Jari Aalto726f6381996-08-26 18:22:31 +0000237 free (value);
238
239 fflush (stdout);
240}
241#endif /* ALIAS */