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