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