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-2004 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 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
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
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.
23 $FUNCTION alias_builtin
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.
39 #if defined (HAVE_UNISTD_H)
41 # include <sys/types.h>
46 # include "../bashansi.h"
47 # include "../bashintl.h"
50 # include "../shell.h"
51 # include "../alias.h"
53 # include "bashgetopt.h"
55 /* Flags for print_alias */
56 #define AL_REUSABLE 0x01
58 static void print_alias __P((alias_t *, int));
60 extern int posixly_correct;
62 /* Hack the alias command in a Korn shell way. */
67 int any_failed, offset, pflag, dflags;
68 alias_t **alias_list, *t;
71 dflags = posixly_correct ? 0 : AL_REUSABLE;
73 reset_internal_getopt ();
74 while ((offset = internal_getopt (list, "p")) != -1)
80 dflags |= AL_REUSABLE;
90 if (list == 0 || pflag)
93 return (EXECUTION_SUCCESS);
95 alias_list = all_aliases ();
98 return (EXECUTION_SUCCESS);
100 for (offset = 0; alias_list[offset]; offset++)
101 print_alias (alias_list[offset], dflags);
103 free (alias_list); /* XXX - Do not free the strings. */
106 return (EXECUTION_SUCCESS);
112 name = list->word->word;
114 for (offset = 0; name[offset] && name[offset] != '='; offset++)
117 if (offset && name[offset] == '=')
120 value = name + offset + 1;
122 if (legal_alias_name (name, 0) == 0)
124 builtin_error (_("`%s': invalid alias name"), name);
128 add_alias (name, value);
132 t = find_alias (name);
134 print_alias (t, dflags);
144 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
149 $FUNCTION unalias_builtin
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.
157 /* Remove aliases named in LIST from the aliases database. */
159 unalias_builtin (list)
160 register WORD_LIST *list;
162 register alias_t *alias;
166 reset_internal_getopt ();
167 while ((opt = internal_getopt (list, "a")) != -1)
184 delete_all_aliases ();
185 return (EXECUTION_SUCCESS);
197 alias = find_alias (list->word->word);
200 remove_alias (alias->name);
203 sh_notfound (list->word->word);
210 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
213 /* Output ALIAS in such a way as to allow it to be read back in. */
215 print_alias (alias, flags)
221 value = sh_single_quote (alias->value);
222 if (flags & AL_REUSABLE)
224 printf ("%s=%s\n", alias->name, value);