alias.c revision 1.19 1 1.19 kre /* $NetBSD: alias.c,v 1.19 2018/12/02 10:27:58 kre Exp $ */
2 1.5 cgd
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * This code is derived from software contributed to Berkeley by
8 1.1 jtc * Kenneth Almquist.
9 1.1 jtc *
10 1.1 jtc * Redistribution and use in source and binary forms, with or without
11 1.1 jtc * modification, are permitted provided that the following conditions
12 1.1 jtc * are met:
13 1.1 jtc * 1. Redistributions of source code must retain the above copyright
14 1.1 jtc * notice, this list of conditions and the following disclaimer.
15 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jtc * notice, this list of conditions and the following disclaimer in the
17 1.1 jtc * documentation and/or other materials provided with the distribution.
18 1.12 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 jtc * may be used to endorse or promote products derived from this software
20 1.1 jtc * without specific prior written permission.
21 1.1 jtc *
22 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 jtc * SUCH DAMAGE.
33 1.1 jtc */
34 1.1 jtc
35 1.9 christos #include <sys/cdefs.h>
36 1.1 jtc #ifndef lint
37 1.5 cgd #if 0
38 1.6 christos static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
39 1.5 cgd #else
40 1.19 kre __RCSID("$NetBSD: alias.c,v 1.19 2018/12/02 10:27:58 kre Exp $");
41 1.5 cgd #endif
42 1.1 jtc #endif /* not lint */
43 1.1 jtc
44 1.6 christos #include <stdlib.h>
45 1.1 jtc #include "shell.h"
46 1.1 jtc #include "input.h"
47 1.1 jtc #include "output.h"
48 1.1 jtc #include "error.h"
49 1.1 jtc #include "memalloc.h"
50 1.1 jtc #include "mystring.h"
51 1.1 jtc #include "alias.h"
52 1.1 jtc #include "options.h" /* XXX for argptr (should remove?) */
53 1.14 christos #include "builtins.h"
54 1.11 christos #include "var.h"
55 1.1 jtc
56 1.1 jtc #define ATABSIZE 39
57 1.1 jtc
58 1.1 jtc struct alias *atab[ATABSIZE];
59 1.1 jtc
60 1.11 christos STATIC void setalias(char *, char *);
61 1.17 rillig STATIC int by_name(const void *, const void *);
62 1.17 rillig STATIC void list_aliases(void);
63 1.11 christos STATIC int unalias(char *);
64 1.15 christos STATIC struct alias **hashalias(const char *);
65 1.1 jtc
66 1.1 jtc STATIC
67 1.4 cgd void
68 1.11 christos setalias(char *name, char *val)
69 1.4 cgd {
70 1.1 jtc struct alias *ap, **app;
71 1.1 jtc
72 1.19 kre (void) unalias(name); /* old one (if any) is now gone */
73 1.1 jtc app = hashalias(name);
74 1.19 kre
75 1.1 jtc INTOFF;
76 1.1 jtc ap = ckmalloc(sizeof (struct alias));
77 1.1 jtc ap->name = savestr(name);
78 1.13 stacktic ap->flag = 0;
79 1.1 jtc /*
80 1.1 jtc * XXX - HACK: in order that the parser will not finish reading the
81 1.1 jtc * alias value off the input before processing the next alias, we
82 1.1 jtc * dummy up an extra space at the end of the alias. This is a crock
83 1.1 jtc * and should be re-thought. The idea (if you feel inclined to help)
84 1.1 jtc * is to avoid alias recursions. The mechanism used is: when
85 1.1 jtc * expanding an alias, the value of the alias is pushed back on the
86 1.1 jtc * input as a string and a pointer to the alias is stored with the
87 1.1 jtc * string. The alias is marked as being in use. When the input
88 1.1 jtc * routine finishes reading the string, it markes the alias not
89 1.1 jtc * in use. The problem is synchronization with the parser. Since
90 1.1 jtc * it reads ahead, the alias is marked not in use before the
91 1.1 jtc * resulting token(s) is next checked for further alias sub. The
92 1.1 jtc * H A C K is that we add a little fluff after the alias value
93 1.1 jtc * so that the string will not be exhausted. This is a good
94 1.1 jtc * idea ------- ***NOT***
95 1.1 jtc */
96 1.1 jtc #ifdef notyet
97 1.1 jtc ap->val = savestr(val);
98 1.1 jtc #else /* hack */
99 1.1 jtc {
100 1.1 jtc int len = strlen(val);
101 1.1 jtc ap->val = ckmalloc(len + 2);
102 1.3 mycroft memcpy(ap->val, val, len);
103 1.1 jtc ap->val[len] = ' '; /* fluff */
104 1.1 jtc ap->val[len+1] = '\0';
105 1.1 jtc }
106 1.1 jtc #endif
107 1.1 jtc ap->next = *app;
108 1.1 jtc *app = ap;
109 1.1 jtc INTON;
110 1.1 jtc }
111 1.1 jtc
112 1.1 jtc STATIC int
113 1.11 christos unalias(char *name)
114 1.11 christos {
115 1.1 jtc struct alias *ap, **app;
116 1.1 jtc
117 1.1 jtc app = hashalias(name);
118 1.1 jtc
119 1.1 jtc for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
120 1.1 jtc if (equal(name, ap->name)) {
121 1.1 jtc /*
122 1.1 jtc * if the alias is currently in use (i.e. its
123 1.1 jtc * buffer is being used by the input routine) we
124 1.1 jtc * just null out the name instead of freeing it.
125 1.1 jtc * We could clear it out later, but this situation
126 1.1 jtc * is so rare that it hardly seems worth it.
127 1.1 jtc */
128 1.1 jtc if (ap->flag & ALIASINUSE)
129 1.1 jtc *ap->name = '\0';
130 1.1 jtc else {
131 1.1 jtc INTOFF;
132 1.1 jtc *app = ap->next;
133 1.1 jtc ckfree(ap->name);
134 1.1 jtc ckfree(ap->val);
135 1.1 jtc ckfree(ap);
136 1.1 jtc INTON;
137 1.1 jtc }
138 1.18 kre return 0;
139 1.1 jtc }
140 1.1 jtc }
141 1.1 jtc
142 1.18 kre return 1;
143 1.1 jtc }
144 1.1 jtc
145 1.1 jtc #ifdef mkinit
146 1.11 christos MKINIT void rmaliases(void);
147 1.1 jtc
148 1.1 jtc SHELLPROC {
149 1.1 jtc rmaliases();
150 1.1 jtc }
151 1.1 jtc #endif
152 1.1 jtc
153 1.1 jtc void
154 1.11 christos rmaliases(void)
155 1.11 christos {
156 1.1 jtc struct alias *ap, *tmp;
157 1.1 jtc int i;
158 1.1 jtc
159 1.1 jtc INTOFF;
160 1.1 jtc for (i = 0; i < ATABSIZE; i++) {
161 1.1 jtc ap = atab[i];
162 1.1 jtc atab[i] = NULL;
163 1.1 jtc while (ap) {
164 1.1 jtc ckfree(ap->name);
165 1.1 jtc ckfree(ap->val);
166 1.1 jtc tmp = ap;
167 1.1 jtc ap = ap->next;
168 1.1 jtc ckfree(tmp);
169 1.1 jtc }
170 1.1 jtc }
171 1.1 jtc INTON;
172 1.1 jtc }
173 1.1 jtc
174 1.1 jtc struct alias *
175 1.15 christos lookupalias(const char *name, int check)
176 1.4 cgd {
177 1.1 jtc struct alias *ap = *hashalias(name);
178 1.1 jtc
179 1.1 jtc for (; ap; ap = ap->next) {
180 1.1 jtc if (equal(name, ap->name)) {
181 1.1 jtc if (check && (ap->flag & ALIASINUSE))
182 1.18 kre return NULL;
183 1.18 kre return ap;
184 1.1 jtc }
185 1.1 jtc }
186 1.1 jtc
187 1.18 kre return NULL;
188 1.1 jtc }
189 1.1 jtc
190 1.15 christos const char *
191 1.15 christos alias_text(void *dummy __unused, const char *name)
192 1.11 christos {
193 1.11 christos struct alias *ap;
194 1.11 christos
195 1.11 christos ap = lookupalias(name, 0);
196 1.11 christos if (ap == NULL)
197 1.11 christos return NULL;
198 1.11 christos return ap->val;
199 1.11 christos }
200 1.11 christos
201 1.17 rillig STATIC int
202 1.17 rillig by_name(const void *a, const void *b)
203 1.17 rillig {
204 1.17 rillig
205 1.17 rillig return strcmp(
206 1.17 rillig (*(const struct alias * const *)a)->name,
207 1.17 rillig (*(const struct alias * const *)b)->name);
208 1.17 rillig }
209 1.17 rillig
210 1.17 rillig STATIC void
211 1.17 rillig list_aliases(void)
212 1.17 rillig {
213 1.17 rillig size_t i, j, n;
214 1.17 rillig const struct alias **aliases;
215 1.17 rillig const struct alias *ap;
216 1.17 rillig
217 1.17 rillig n = 0;
218 1.17 rillig for (i = 0; i < ATABSIZE; i++)
219 1.17 rillig for (ap = atab[i]; ap != NULL; ap = ap->next)
220 1.17 rillig if (ap->name[0] != '\0')
221 1.17 rillig n++;
222 1.17 rillig
223 1.17 rillig aliases = ckmalloc(n * sizeof aliases[0]);
224 1.17 rillig
225 1.17 rillig j = 0;
226 1.17 rillig for (i = 0; i < ATABSIZE; i++)
227 1.17 rillig for (ap = atab[i]; ap != NULL; ap = ap->next)
228 1.17 rillig if (ap->name[0] != '\0')
229 1.17 rillig aliases[j++] = ap;
230 1.17 rillig
231 1.17 rillig qsort(aliases, n, sizeof aliases[0], by_name);
232 1.17 rillig
233 1.17 rillig for (i = 0; i < n; i++) {
234 1.17 rillig out1fmt("alias %s=", aliases[i]->name);
235 1.17 rillig print_quoted(aliases[i]->val);
236 1.17 rillig out1c('\n');
237 1.17 rillig }
238 1.17 rillig
239 1.17 rillig ckfree(aliases);
240 1.17 rillig }
241 1.17 rillig
242 1.4 cgd int
243 1.11 christos aliascmd(int argc, char **argv)
244 1.4 cgd {
245 1.1 jtc char *n, *v;
246 1.1 jtc int ret = 0;
247 1.1 jtc struct alias *ap;
248 1.1 jtc
249 1.1 jtc if (argc == 1) {
250 1.17 rillig list_aliases();
251 1.18 kre return 0;
252 1.1 jtc }
253 1.17 rillig
254 1.6 christos while ((n = *++argv) != NULL) {
255 1.10 christos if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
256 1.1 jtc if ((ap = lookupalias(n, 0)) == NULL) {
257 1.1 jtc outfmt(out2, "alias: %s not found\n", n);
258 1.1 jtc ret = 1;
259 1.11 christos } else {
260 1.11 christos out1fmt("alias %s=", n);
261 1.11 christos print_quoted(ap->val);
262 1.11 christos out1c('\n');
263 1.11 christos }
264 1.11 christos } else {
265 1.1 jtc *v++ = '\0';
266 1.1 jtc setalias(n, v);
267 1.1 jtc }
268 1.1 jtc }
269 1.1 jtc
270 1.18 kre return ret;
271 1.1 jtc }
272 1.1 jtc
273 1.4 cgd int
274 1.11 christos unaliascmd(int argc, char **argv)
275 1.4 cgd {
276 1.1 jtc int i;
277 1.7 christos
278 1.1 jtc while ((i = nextopt("a")) != '\0') {
279 1.1 jtc if (i == 'a') {
280 1.1 jtc rmaliases();
281 1.18 kre return 0;
282 1.1 jtc }
283 1.1 jtc }
284 1.1 jtc for (i = 0; *argptr; argptr++)
285 1.1 jtc i = unalias(*argptr);
286 1.1 jtc
287 1.18 kre return i;
288 1.1 jtc }
289 1.1 jtc
290 1.1 jtc STATIC struct alias **
291 1.15 christos hashalias(const char *p)
292 1.11 christos {
293 1.1 jtc unsigned int hashval;
294 1.1 jtc
295 1.16 kre hashval = *(const unsigned char *)p << 4;
296 1.1 jtc while (*p)
297 1.16 kre hashval += *p++;
298 1.1 jtc return &atab[hashval % ATABSIZE];
299 1.1 jtc }
300