]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/alias.def
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / builtins / alias.def
CommitLineData
726f6388
JA
1This file is alias.def, from which is created alias.c
2It implements the builtins "alias" and "unalias" in Bash.
3
4Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
bb70624e 10Software Foundation; either version 2, or (at your option) any later
726f6388
JA
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
bb70624e 20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
726f6388
JA
21
22$BUILTIN alias
23$FUNCTION alias_builtin
24$DEPENDS_ON ALIAS
25$PRODUCES alias.c
ccc6cda3
JA
26$SHORT_DOC alias [-p] [name[=value] ... ]
27`alias' with no arguments or with the -p option prints the list
28of aliases in the form alias NAME=VALUE on standard output.
29Otherwise, an alias is defined for each NAME whose VALUE is given.
30A trailing space in VALUE causes the next word to be checked for
31alias substitution when the alias is expanded. Alias returns
32true unless a NAME is given for which no alias has been defined.
726f6388
JA
33$END
34
ccc6cda3 35#include <config.h>
726f6388
JA
36
37#if defined (ALIAS)
ccc6cda3
JA
38
39#if defined (HAVE_UNISTD_H)
cce855bc
JA
40# ifdef _MINIX
41# include <sys/types.h>
42# endif
ccc6cda3
JA
43# include <unistd.h>
44#endif
45
d166f048
JA
46# include "../bashansi.h"
47
726f6388
JA
48# include <stdio.h>
49# include "../shell.h"
50# include "../alias.h"
51# include "common.h"
ccc6cda3 52# include "bashgetopt.h"
726f6388
JA
53
54extern int interactive;
55static void print_alias ();
56
57/* Hack the alias command in a Korn shell way. */
ccc6cda3 58int
726f6388
JA
59alias_builtin (list)
60 WORD_LIST *list;
61{
ccc6cda3
JA
62 int any_failed, offset, pflag;
63 alias_t **alias_list, *t;
64 char *name, *value;
726f6388 65
ccc6cda3
JA
66 pflag = 0;
67 reset_internal_getopt ();
68 while ((offset = internal_getopt (list, "p")) != -1)
726f6388 69 {
ccc6cda3
JA
70 switch (offset)
71 {
72 case 'p':
73 pflag = 1;
74 break;
75 default:
76 builtin_usage ();
77 return (EX_USAGE);
78 }
79 }
726f6388 80
ccc6cda3
JA
81 list = loptend;
82
83 if (list == 0 || pflag)
84 {
85 if (aliases == 0)
cce855bc 86 return (EXECUTION_SUCCESS);
726f6388
JA
87
88 alias_list = all_aliases ();
89
ccc6cda3 90 if (alias_list == 0)
cce855bc 91 return (EXECUTION_SUCCESS);
726f6388 92
ccc6cda3
JA
93 for (offset = 0; alias_list[offset]; offset++)
94 print_alias (alias_list[offset]);
726f6388
JA
95
96 free (alias_list); /* XXX - Do not free the strings. */
ccc6cda3
JA
97
98 if (list == 0)
99 return (EXECUTION_SUCCESS);
726f6388 100 }
ccc6cda3
JA
101
102 any_failed = 0;
103 while (list)
726f6388 104 {
ccc6cda3 105 name = list->word->word;
726f6388 106
ccc6cda3
JA
107 for (offset = 0; name[offset] && name[offset] != '='; offset++)
108 ;
726f6388 109
ccc6cda3
JA
110 if (offset && name[offset] == '=')
111 {
112 name[offset] = '\0';
113 value = name + offset + 1;
726f6388 114
ccc6cda3
JA
115 add_alias (name, value);
116 }
117 else
118 {
119 t = find_alias (name);
120 if (t)
121 print_alias (t);
726f6388
JA
122 else
123 {
cce855bc 124 builtin_error ("`%s' not found", name);
ccc6cda3 125 any_failed++;
726f6388 126 }
726f6388 127 }
ccc6cda3 128 list = list->next;
726f6388 129 }
ccc6cda3
JA
130
131 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
726f6388
JA
132}
133#endif /* ALIAS */
134
135$BUILTIN unalias
136$FUNCTION unalias_builtin
137$DEPENDS_ON ALIAS
138$SHORT_DOC unalias [-a] [name ...]
139Remove NAMEs from the list of defined aliases. If the -a option is given,
140then remove all alias definitions.
141$END
142
143#if defined (ALIAS)
144/* Remove aliases named in LIST from the aliases database. */
ccc6cda3 145int
726f6388
JA
146unalias_builtin (list)
147 register WORD_LIST *list;
148{
ccc6cda3
JA
149 register alias_t *alias;
150 int opt, aflag;
726f6388 151
ccc6cda3
JA
152 aflag = 0;
153 reset_internal_getopt ();
154 while ((opt = internal_getopt (list, "a")) != -1)
726f6388 155 {
ccc6cda3 156 switch (opt)
726f6388 157 {
ccc6cda3
JA
158 case 'a':
159 aflag = 1;
726f6388 160 break;
ccc6cda3
JA
161 default:
162 builtin_usage ();
163 return (EX_USAGE);
726f6388 164 }
726f6388
JA
165 }
166
ccc6cda3
JA
167 list = loptend;
168
169 if (aflag)
170 {
171 delete_all_aliases ();
172 return (EXECUTION_SUCCESS);
173 }
174
175 aflag = 0;
726f6388
JA
176 while (list)
177 {
178 alias = find_alias (list->word->word);
179
180 if (alias)
181 remove_alias (alias->name);
182 else
183 {
cce855bc 184 builtin_error ("`%s': not an alias", list->word->word);
ccc6cda3 185 aflag++;
726f6388
JA
186 }
187
188 list = list->next;
189 }
190
ccc6cda3 191 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
726f6388
JA
192}
193
194/* Output ALIAS in such a way as to allow it to be read back in. */
195static void
196print_alias (alias)
ccc6cda3 197 alias_t *alias;
726f6388 198{
ccc6cda3 199 char *value;
726f6388 200
28ef6c31 201 value = sh_single_quote (alias->value);
726f6388
JA
202 printf ("alias %s=%s\n", alias->name, value);
203 free (value);
204
205 fflush (stdout);
206}
207#endif /* ALIAS */