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