044b44dc9383b150a645be8ffa7d510361f8f92b
[platform/upstream/bash.git] / builtins / alias.def
1 This file is alias.def, from which is created alias.c
2 It implements the builtins "alias" and "unalias" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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 1, or (at your option) any later
11 version.
12
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
16 for more details.
17
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, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $BUILTIN alias
23 $FUNCTION alias_builtin
24 $DEPENDS_ON ALIAS
25 $PRODUCES alias.c
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.
33 $END
34
35 #include <config.h>
36
37 #if defined (ALIAS)
38
39 #if defined (HAVE_UNISTD_H)
40 #  include <unistd.h>
41 #endif
42
43 #  include "../bashansi.h"
44
45 #  include <stdio.h>
46 #  include "../shell.h"
47 #  include "../alias.h"
48 #  include "common.h"
49 #  include "bashgetopt.h"
50
51 extern int interactive;
52 static void print_alias ();
53
54 /* Hack the alias command in a Korn shell way. */
55 int
56 alias_builtin (list)
57      WORD_LIST *list;
58 {
59   int any_failed, offset, pflag;
60   alias_t **alias_list, *t;
61   char *name, *value;
62
63   pflag = 0;
64   reset_internal_getopt ();
65   while ((offset = internal_getopt (list, "p")) != -1)
66     {
67       switch (offset)
68         {
69         case 'p':
70           pflag = 1;
71           break;
72         default:
73           builtin_usage ();
74           return (EX_USAGE);
75         }
76     }
77
78   list = loptend;
79
80   if (list == 0 || pflag)
81     {
82       if (aliases == 0)
83         return (EXECUTION_FAILURE);
84
85       alias_list = all_aliases ();
86
87       if (alias_list == 0)
88         return (EXECUTION_FAILURE);
89
90       for (offset = 0; alias_list[offset]; offset++)
91         print_alias (alias_list[offset]);
92
93       free (alias_list);        /* XXX - Do not free the strings. */
94
95       if (list == 0)
96         return (EXECUTION_SUCCESS);
97     }
98
99   any_failed = 0;
100   while (list)
101     {
102       name = list->word->word;
103
104       for (offset = 0; name[offset] && name[offset] != '='; offset++)
105         ;
106
107       if (offset && name[offset] == '=')
108         {
109           name[offset] = '\0';
110           value = name + offset + 1;
111
112           add_alias (name, value);
113         }
114       else
115         {
116           t = find_alias (name);
117           if (t)
118             print_alias (t);
119           else
120             {
121               if (interactive)
122                 builtin_error ("`%s' not found", name);
123               any_failed++;
124             }
125         }
126       list = list->next;
127     }
128
129   return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
130 }
131 #endif /* ALIAS */
132
133 $BUILTIN unalias
134 $FUNCTION unalias_builtin
135 $DEPENDS_ON ALIAS
136 $SHORT_DOC unalias [-a] [name ...]
137 Remove NAMEs from the list of defined aliases.  If the -a option is given,
138 then remove all alias definitions.
139 $END
140
141 #if defined (ALIAS)
142 /* Remove aliases named in LIST from the aliases database. */
143 int
144 unalias_builtin (list)
145      register WORD_LIST *list;
146 {
147   register alias_t *alias;
148   int opt, aflag;
149
150   aflag = 0;
151   reset_internal_getopt ();
152   while ((opt = internal_getopt (list, "a")) != -1)
153     {
154       switch (opt)
155         {
156         case 'a':
157           aflag = 1;
158           break;
159         default:
160           builtin_usage ();
161           return (EX_USAGE);
162         }
163     }
164
165   list = loptend;
166
167   if (aflag)
168     {
169       delete_all_aliases ();
170       return (EXECUTION_SUCCESS);
171     }
172
173   aflag = 0;
174   while (list)
175     {
176       alias = find_alias (list->word->word);
177
178       if (alias)
179         remove_alias (alias->name);
180       else
181         {
182           if (interactive)
183             builtin_error ("`%s': not an alias", list->word->word);
184
185           aflag++;
186         }
187
188       list = list->next;
189     }
190
191   return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
192 }
193
194 /* Output ALIAS in such a way as to allow it to be read back in. */
195 static void
196 print_alias (alias)
197      alias_t *alias;
198 {
199   char *value;
200
201   value = single_quote (alias->value);
202   printf ("alias %s=%s\n", alias->name, value);
203   free (value);
204
205   fflush (stdout);
206 }
207 #endif /* ALIAS */