Imported from ../bash-1.14.7.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 [ name[=value] ... ]
27 `alias' with no arguments prints the list of aliases in the form
28 NAME=VALUE on standard output.  An alias is defined for each NAME
29 whose VALUE is given.  A trailing space in VALUE causes the next
30 word to be checked for alias substitution.  Alias returns true
31 unless a NAME is given for which no alias has been defined.
32 $END
33
34 #include "../config.h"
35
36 #if defined (ALIAS)
37 #  include <stdio.h>
38 #  include "../shell.h"
39 #  include "../alias.h"
40 #  include "common.h"
41
42 extern int interactive;
43 static void print_alias ();
44
45 /* Hack the alias command in a Korn shell way. */
46 alias_builtin (list)
47      WORD_LIST *list;
48 {
49   int any_failed = 0;
50
51   if (!list)
52     {
53       register int i;
54       ASSOC **alias_list;
55
56       if (!aliases)
57         return (EXECUTION_FAILURE);
58
59       alias_list = all_aliases ();
60
61       if (!alias_list)
62         return (EXECUTION_FAILURE);
63
64       for (i = 0; alias_list[i]; i++)
65         print_alias (alias_list[i]);
66
67       free (alias_list);        /* XXX - Do not free the strings. */
68     }
69   else
70     {
71       while (list)
72         {
73           register char *value, *name = list->word->word;
74           register int offset;
75
76           for (offset = 0; name[offset] && name[offset] != '='; offset++)
77             ;
78
79           if (offset && name[offset] == '=')
80             {
81               name[offset] = '\0';
82               value = name + offset + 1;
83
84               add_alias (name, value);
85             }
86           else
87             {
88               ASSOC *t = find_alias (name);
89               if (t)
90                 print_alias (t);
91               else
92                 {
93                   if (interactive)
94                     builtin_error ("`%s' not found", name);
95                   any_failed++;
96                 }
97             }
98           list = list->next;
99         }
100     }
101   if (any_failed)
102     return (EXECUTION_FAILURE);
103   else
104     return (EXECUTION_SUCCESS);
105 }
106 #endif /* ALIAS */
107
108 $BUILTIN unalias
109 $FUNCTION unalias_builtin
110 $DEPENDS_ON ALIAS
111 $SHORT_DOC unalias [-a] [name ...]
112 Remove NAMEs from the list of defined aliases.  If the -a option is given,
113 then remove all alias definitions.
114 $END
115
116 #if defined (ALIAS)
117 /* Remove aliases named in LIST from the aliases database. */
118 unalias_builtin (list)
119      register WORD_LIST *list;
120 {
121   register ASSOC *alias;
122   int any_failed = 0;
123
124   while (list && *list->word->word == '-')
125     {
126       register char *word = list->word->word;
127
128       if (ISOPTION (word, 'a'))
129         {
130           delete_all_aliases ();
131           list = list->next;
132         }
133       else if (ISOPTION (word, '-'))
134         {
135           list = list->next;
136           break;
137         }
138       else
139         {
140           bad_option (word);
141           return (EXECUTION_FAILURE);
142         }
143     }
144
145   while (list)
146     {
147       alias = find_alias (list->word->word);
148
149       if (alias)
150         remove_alias (alias->name);
151       else
152         {
153           if (interactive)
154             builtin_error ("`%s' not an alias", list->word->word);
155
156           any_failed++;
157         }
158
159       list = list->next;
160     }
161
162   if (any_failed)
163     return (EXECUTION_FAILURE);
164   else
165     return (EXECUTION_SUCCESS);
166 }
167
168 /* Output ALIAS in such a way as to allow it to be read back in. */
169 static void
170 print_alias (alias)
171      ASSOC *alias;
172 {
173   char *value = single_quote (alias->value);
174
175   printf ("alias %s=%s\n", alias->name, value);
176   free (value);
177
178   fflush (stdout);
179 }
180 #endif /* ALIAS */