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