alias.c revision 17987
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 *	$Id: alias.c,v 1.3 1995/05/30 00:07:10 rgrimes Exp $
37 */
38
39#ifndef lint
40static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
41#endif /* not lint */
42
43#include <stdlib.h>
44#include "shell.h"
45#include "input.h"
46#include "output.h"
47#include "error.h"
48#include "memalloc.h"
49#include "mystring.h"
50#include "alias.h"
51#include "options.h"	/* XXX for argptr (should remove?) */
52
53#define ATABSIZE 39
54
55struct alias *atab[ATABSIZE];
56
57STATIC void setalias __P((char *, char *));
58STATIC int unalias __P((char *));
59STATIC struct alias **hashalias __P((char *));
60
61STATIC
62void
63setalias(name, val)
64	char *name, *val;
65{
66	struct alias *ap, **app;
67
68	app = hashalias(name);
69	for (ap = *app; ap; ap = ap->next) {
70		if (equal(name, ap->name)) {
71			INTOFF;
72			ckfree(ap->val);
73			ap->val	= savestr(val);
74			INTON;
75			return;
76		}
77	}
78	/* not found */
79	INTOFF;
80	ap = ckmalloc(sizeof (struct alias));
81	ap->name = savestr(name);
82	/*
83	 * XXX - HACK: in order that the parser will not finish reading the
84	 * alias value off the input before processing the next alias, we
85	 * dummy up an extra space at the end of the alias.  This is a crock
86	 * and should be re-thought.  The idea (if you feel inclined to help)
87	 * is to avoid alias recursions.  The mechanism used is: when
88	 * expanding an alias, the value of the alias is pushed back on the
89	 * input as a string and a pointer to the alias is stored with the
90	 * string.  The alias is marked as being in use.  When the input
91	 * routine finishes reading the string, it markes the alias not
92	 * in use.  The problem is synchronization with the parser.  Since
93	 * it reads ahead, the alias is marked not in use before the
94	 * resulting token(s) is next checked for further alias sub.  The
95	 * H A C K is that we add a little fluff after the alias value
96	 * so that the string will not be exhausted.  This is a good
97	 * idea ------- ***NOT***
98	 */
99#ifdef notyet
100	ap->val = savestr(val);
101#else /* hack */
102	{
103	int len = strlen(val);
104	ap->val = ckmalloc(len + 2);
105	memcpy(ap->val, val, len);
106	ap->val[len] = ' ';	/* fluff */
107	ap->val[len+1] = '\0';
108	}
109#endif
110	ap->next = *app;
111	*app = ap;
112	INTON;
113}
114
115STATIC int
116unalias(name)
117	char *name;
118	{
119	struct alias *ap, **app;
120
121	app = hashalias(name);
122
123	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
124		if (equal(name, ap->name)) {
125			/*
126			 * if the alias is currently in use (i.e. its
127			 * buffer is being used by the input routine) we
128			 * just null out the name instead of freeing it.
129			 * We could clear it out later, but this situation
130			 * is so rare that it hardly seems worth it.
131			 */
132			if (ap->flag & ALIASINUSE)
133				*ap->name = '\0';
134			else {
135				INTOFF;
136				*app = ap->next;
137				ckfree(ap->name);
138				ckfree(ap->val);
139				ckfree(ap);
140				INTON;
141			}
142			return (0);
143		}
144	}
145
146	return (1);
147}
148
149#ifdef mkinit
150MKINIT void rmaliases();
151
152SHELLPROC {
153	rmaliases();
154}
155#endif
156
157void
158rmaliases() {
159	struct alias *ap, *tmp;
160	int i;
161
162	INTOFF;
163	for (i = 0; i < ATABSIZE; i++) {
164		ap = atab[i];
165		atab[i] = NULL;
166		while (ap) {
167			ckfree(ap->name);
168			ckfree(ap->val);
169			tmp = ap;
170			ap = ap->next;
171			ckfree(tmp);
172		}
173	}
174	INTON;
175}
176
177struct alias *
178lookupalias(name, check)
179	char *name;
180	int check;
181{
182	struct alias *ap = *hashalias(name);
183
184	for (; ap; ap = ap->next) {
185		if (equal(name, ap->name)) {
186			if (check && (ap->flag & ALIASINUSE))
187				return (NULL);
188			return (ap);
189		}
190	}
191
192	return (NULL);
193}
194
195/*
196 * TODO - sort output
197 */
198int
199aliascmd(argc, argv)
200	int argc;
201	char **argv;
202{
203	char *n, *v;
204	int ret = 0;
205	struct alias *ap;
206
207	if (argc == 1) {
208		int i;
209
210		for (i = 0; i < ATABSIZE; i++)
211			for (ap = atab[i]; ap; ap = ap->next) {
212				if (*ap->name != '\0')
213				    out1fmt("alias %s=%s\n", ap->name, ap->val);
214			}
215		return (0);
216	}
217	while ((n = *++argv) != NULL) {
218		if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
219			if ((ap = lookupalias(n, 0)) == NULL) {
220				outfmt(out2, "alias: %s not found\n", n);
221				ret = 1;
222			} else
223				out1fmt("alias %s=%s\n", n, ap->val);
224		else {
225			*v++ = '\0';
226			setalias(n, v);
227		}
228	}
229
230	return (ret);
231}
232
233int
234unaliascmd(argc, argv)
235	int argc;
236	char **argv;
237{
238	int i;
239
240	while ((i = nextopt("a")) != '\0') {
241		if (i == 'a') {
242			rmaliases();
243			return (0);
244		}
245	}
246	for (i = 0; *argptr; argptr++)
247		i = unalias(*argptr);
248
249	return (i);
250}
251
252STATIC struct alias **
253hashalias(p)
254	register char *p;
255	{
256	unsigned int hashval;
257
258	hashval = *p << 4;
259	while (*p)
260		hashval+= *p++;
261	return &atab[hashval % ATABSIZE];
262}
263