Imported from ../bash-2.05a.tar.gz.
[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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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 #  ifdef _MINIX
41 #    include <sys/types.h>
42 #  endif
43 #  include <unistd.h>
44 #endif
45
46 #  include "../bashansi.h"
47
48 #  include <stdio.h>
49 #  include "../shell.h"
50 #  include "../alias.h"
51 #  include "common.h"
52 #  include "bashgetopt.h"
53
54 static void print_alias __P((alias_t *));
55
56 /* Hack the alias command in a Korn shell way. */
57 int
58 alias_builtin (list)
59      WORD_LIST *list;
60 {
61   int any_failed, offset, pflag;
62   alias_t **alias_list, *t;
63   char *name, *value;
64
65   pflag = 0;
66   reset_internal_getopt ();
67   while ((offset = internal_getopt (list, "p")) != -1)
68     {
69       switch (offset)
70         {
71         case 'p':
72           pflag = 1;
73           break;
74         default:
75           builtin_usage ();
76           return (EX_USAGE);
77         }
78     }
79
80   list = loptend;
81
82   if (list == 0 || pflag)
83     {
84       if (aliases == 0)
85         return (EXECUTION_SUCCESS);
86
87       alias_list = all_aliases ();
88
89       if (alias_list == 0)
90         return (EXECUTION_SUCCESS);
91
92       for (offset = 0; alias_list[offset]; offset++)
93         print_alias (alias_list[offset]);
94
95       free (alias_list);        /* XXX - Do not free the strings. */
96
97       if (list == 0)
98         return (EXECUTION_SUCCESS);
99     }
100
101   any_failed = 0;
102   while (list)
103     {
104       name = list->word->word;
105
106       for (offset = 0; name[offset] && name[offset] != '='; offset++)
107         ;
108
109       if (offset && name[offset] == '=')
110         {
111           name[offset] = '\0';
112           value = name + offset + 1;
113
114           add_alias (name, value);
115         }
116       else
117         {
118           t = find_alias (name);
119           if (t)
120             print_alias (t);
121           else
122             {
123               builtin_error ("`%s' not found", name);
124               any_failed++;
125             }
126         }
127       list = list->next;
128     }
129
130   return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
131 }
132 #endif /* ALIAS */
133
134 $BUILTIN unalias
135 $FUNCTION unalias_builtin
136 $DEPENDS_ON ALIAS
137 $SHORT_DOC unalias [-a] [name ...]
138 Remove NAMEs from the list of defined aliases.  If the -a option is given,
139 then remove all alias definitions.
140 $END
141
142 #if defined (ALIAS)
143 /* Remove aliases named in LIST from the aliases database. */
144 int
145 unalias_builtin (list)
146      register WORD_LIST *list;
147 {
148   register alias_t *alias;
149   int opt, aflag;
150
151   aflag = 0;
152   reset_internal_getopt ();
153   while ((opt = internal_getopt (list, "a")) != -1)
154     {
155       switch (opt)
156         {
157         case 'a':
158           aflag = 1;
159           break;
160         default:
161           builtin_usage ();
162           return (EX_USAGE);
163         }
164     }
165
166   list = loptend;
167
168   if (aflag)
169     {
170       delete_all_aliases ();
171       return (EXECUTION_SUCCESS);
172     }
173
174   aflag = 0;
175   while (list)
176     {
177       alias = find_alias (list->word->word);
178
179       if (alias)
180         remove_alias (alias->name);
181       else
182         {
183           builtin_error ("`%s': not an alias", list->word->word);
184           aflag++;
185         }
186
187       list = list->next;
188     }
189
190   return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
191 }
192
193 /* Output ALIAS in such a way as to allow it to be read back in. */
194 static void
195 print_alias (alias)
196      alias_t *alias;
197 {
198   char *value;
199
200   value = sh_single_quote (alias->value);
201   printf ("alias %s=%s\n", alias->name, value);
202   free (value);
203
204   fflush (stdout);
205 }
206 #endif /* ALIAS */