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