1 This file is alias.def, from which is created alias.c
2 It implements the builtins "alias" and "unalias" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
22 $FUNCTION alias_builtin
25 $SHORT_DOC alias [-p] [name[=value] ... ]
26 Define or display aliases.
28 Without arguments, `alias' prints the list of aliases in the reusable
29 form `alias NAME=VALUE' on standard output.
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.
36 -p Print all defined aliases in a reusable format
39 alias returns true unless a NAME is supplied for which no alias has been
47 #if defined (HAVE_UNISTD_H)
49 # include <sys/types.h>
54 # include "../bashansi.h"
55 # include "../bashintl.h"
58 # include "../shell.h"
59 # include "../alias.h"
61 # include "bashgetopt.h"
63 /* Flags for print_alias */
64 #define AL_REUSABLE 0x01
66 static void print_alias __P((alias_t *, int));
68 extern int posixly_correct;
70 /* Hack the alias command in a Korn shell way. */
75 int any_failed, offset, pflag, dflags;
76 alias_t **alias_list, *t;
79 dflags = posixly_correct ? 0 : AL_REUSABLE;
81 reset_internal_getopt ();
82 while ((offset = internal_getopt (list, "p")) != -1)
88 dflags |= AL_REUSABLE;
98 if (list == 0 || pflag)
101 return (EXECUTION_SUCCESS);
103 alias_list = all_aliases ();
106 return (EXECUTION_SUCCESS);
108 for (offset = 0; alias_list[offset]; offset++)
109 print_alias (alias_list[offset], dflags);
111 free (alias_list); /* XXX - Do not free the strings. */
114 return (sh_chkwrite (EXECUTION_SUCCESS));
120 name = list->word->word;
122 for (offset = 0; name[offset] && name[offset] != '='; offset++)
125 if (offset && name[offset] == '=')
128 value = name + offset + 1;
130 if (legal_alias_name (name, 0) == 0)
132 builtin_error (_("`%s': invalid alias name"), name);
136 add_alias (name, value);
140 t = find_alias (name);
142 print_alias (t, dflags);
152 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
157 $FUNCTION unalias_builtin
159 $SHORT_DOC unalias [-a] name [name ...]
160 Remove each NAME from the list of defined aliases.
163 -a remove all alias definitions.
165 Return success unless a NAME is not an existing alias.
169 /* Remove aliases named in LIST from the aliases database. */
171 unalias_builtin (list)
172 register WORD_LIST *list;
174 register alias_t *alias;
178 reset_internal_getopt ();
179 while ((opt = internal_getopt (list, "a")) != -1)
196 delete_all_aliases ();
197 return (EXECUTION_SUCCESS);
209 alias = find_alias (list->word->word);
212 remove_alias (alias->name);
215 sh_notfound (list->word->word);
222 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
225 /* Output ALIAS in such a way as to allow it to be read back in. */
227 print_alias (alias, flags)
233 value = sh_single_quote (alias->value);
234 if (flags & AL_REUSABLE)
236 printf ("%s=%s\n", alias->name, value);