init version.
[bush.git] / builtins / alias.def
blob819369f004c1f63c8e585a9fdcaf661b04e99318
1 This file is alias.def, from which is created alias.c
2 It implements the builtins "alias" and "unalias" in Bush.
4 Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bush is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bush. If not, see <http://www.gnu.org/licenses/>.
21 $BUILTIN alias
22 $FUNCTION alias_builtin
23 $DEPENDS_ON ALIAS
24 $PRODUCES alias.c
25 $SHORT_DOC alias [-p] [name[=value] ... ]
26 Define or display aliases.
28 Without arguments, `alias' prints the list of aliases in the reusable
29 form `alias NAME=VALUE' on standard output.
31 Otherwise, an alias is defined for each NAME whose VALUE is given.
32 A trailing space in VALUE causes the next word to be checked for
33 alias substitution when the alias is expanded.
35 Options:
36 -p print all defined aliases in a reusable format
38 Exit Status:
39 alias returns true unless a NAME is supplied for which no alias has been
40 defined.
41 $END
43 #include <config.h>
45 #if defined (ALIAS)
47 #if defined (HAVE_UNISTD_H)
48 # ifdef _MINIX
49 # include <sys/types.h>
50 # endif
51 # include <unistd.h>
52 #endif
54 # include "../src/bushansi.h"
55 # include "../src/bushintl.h"
57 # include <stdio.h>
58 # include "../src/shell.h"
59 # include "../src/alias.h"
60 # include "common.h"
61 # include "bushgetopt.h"
63 /* Flags for print_alias */
64 #define AL_REUSABLE 0x01
66 static void print_alias PARAMS((alias_t *, int));
68 /* Hack the alias command in a Korn shell way. */
69 int
70 alias_builtin (list)
71 WORD_LIST *list;
73 int any_failed, offset, pflag, dflags;
74 alias_t **alias_list, *t;
75 char *name, *value;
77 dflags = posixly_correct ? 0 : AL_REUSABLE;
78 pflag = 0;
79 reset_internal_getopt ();
80 while ((offset = internal_getopt (list, "p")) != -1)
82 switch (offset)
84 case 'p':
85 pflag = 1;
86 dflags |= AL_REUSABLE;
87 break;
88 CASE_HELPOPT;
89 default:
90 builtin_usage ();
91 return (EX_USAGE);
95 list = loptend;
97 if (list == 0 || pflag)
99 if (aliases == 0)
100 return (EXECUTION_SUCCESS);
102 alias_list = all_aliases ();
104 if (alias_list == 0)
105 return (EXECUTION_SUCCESS);
107 for (offset = 0; alias_list[offset]; offset++)
108 print_alias (alias_list[offset], dflags);
110 free (alias_list); /* XXX - Do not free the strings. */
112 if (list == 0)
113 return (sh_chkwrite (EXECUTION_SUCCESS));
116 any_failed = 0;
117 while (list)
119 name = list->word->word;
121 for (offset = 0; name[offset] && name[offset] != '='; offset++)
124 if (offset && name[offset] == '=')
126 name[offset] = '\0';
127 value = name + offset + 1;
129 if (legal_alias_name (name, 0) == 0)
131 builtin_error (_("`%s': invalid alias name"), name);
132 any_failed++;
134 else
135 add_alias (name, value);
137 else
139 t = find_alias (name);
140 if (t)
141 print_alias (t, dflags);
142 else
144 sh_notfound (name);
145 any_failed++;
148 list = list->next;
151 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
153 #endif /* ALIAS */
155 $BUILTIN unalias
156 $FUNCTION unalias_builtin
157 $DEPENDS_ON ALIAS
158 $SHORT_DOC unalias [-a] name [name ...]
159 Remove each NAME from the list of defined aliases.
161 Options:
162 -a remove all alias definitions
164 Return success unless a NAME is not an existing alias.
165 $END
167 #if defined (ALIAS)
168 /* Remove aliases named in LIST from the aliases database. */
170 unalias_builtin (list)
171 register WORD_LIST *list;
173 register alias_t *alias;
174 int opt, aflag;
176 aflag = 0;
177 reset_internal_getopt ();
178 while ((opt = internal_getopt (list, "a")) != -1)
180 switch (opt)
182 case 'a':
183 aflag = 1;
184 break;
185 CASE_HELPOPT;
186 default:
187 builtin_usage ();
188 return (EX_USAGE);
192 list = loptend;
194 if (aflag)
196 delete_all_aliases ();
197 return (EXECUTION_SUCCESS);
200 if (list == 0)
202 builtin_usage ();
203 return (EX_USAGE);
206 aflag = 0;
207 while (list)
209 alias = find_alias (list->word->word);
211 if (alias)
212 remove_alias (alias->name);
213 else
215 sh_notfound (list->word->word);
216 aflag++;
219 list = list->next;
222 return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
225 /* Output ALIAS in such a way as to allow it to be read back in. */
226 static void
227 print_alias (alias, flags)
228 alias_t *alias;
229 int flags;
231 char *value;
233 value = sh_single_quote (alias->value);
234 if (flags & AL_REUSABLE)
235 printf ("alias %s", (alias->name && alias->name[0] == '-') ? "-- " : "");
236 printf ("%s=%s\n", alias->name, value);
237 free (value);
239 fflush (stdout);
241 #endif /* ALIAS */