alias.c revision 3044
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$
37 */
38
39#ifndef lint
40static char sccsid[] = "@(#)alias.c	8.1 (Berkeley) 5/31/93";
41#endif /* not lint */
42
43#include "shell.h"
44#include "input.h"
45#include "output.h"
46#include "error.h"
47#include "memalloc.h"
48#include "mystring.h"
49#include "alias.h"
50#include "options.h"	/* XXX for argptr (should remove?) */
51
52#define ATABSIZE 39
53
54struct alias *atab[ATABSIZE];
55
56STATIC struct alias **hashalias __P((char *));
57
58STATIC
59setalias(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	bcopy(val, ap->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
111STATIC int
112unalias(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
146MKINIT void rmaliases();
147
148SHELLPROC {
149	rmaliases();
150}
151#endif
152
153void
154rmaliases() {
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
173struct alias *
174lookupalias(name, check)
175	char *name;
176	{
177	struct alias *ap = *hashalias(name);
178
179	for (; ap; ap = ap->next) {
180		if (equal(name, ap->name)) {
181			if (check && (ap->flag & ALIASINUSE))
182				return (NULL);
183			return (ap);
184		}
185	}
186
187	return (NULL);
188}
189
190/*
191 * TODO - sort output
192 */
193aliascmd(argc, argv)
194	char **argv;
195	{
196	char *n, *v;
197	int ret = 0;
198	struct alias *ap;
199
200	if (argc == 1) {
201		int i;
202
203		for (i = 0; i < ATABSIZE; i++)
204			for (ap = atab[i]; ap; ap = ap->next) {
205				if (*ap->name != '\0')
206				    out1fmt("alias %s=%s\n", ap->name, ap->val);
207			}
208		return (0);
209	}
210	while (n = *++argv) {
211		if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
212			if ((ap = lookupalias(n, 0)) == NULL) {
213				outfmt(out2, "alias: %s not found\n", n);
214				ret = 1;
215			} else
216				out1fmt("alias %s=%s\n", n, ap->val);
217		else {
218			*v++ = '\0';
219			setalias(n, v);
220		}
221	}
222
223	return (ret);
224}
225
226unaliascmd(argc, argv)
227	char **argv;
228	{
229	int i;
230
231	while ((i = nextopt("a")) != '\0') {
232		if (i == 'a') {
233			rmaliases();
234			return (0);
235		}
236	}
237	for (i = 0; *argptr; argptr++)
238		i = unalias(*argptr);
239
240	return (i);
241}
242
243STATIC struct alias **
244hashalias(p)
245	register char *p;
246	{
247	unsigned int hashval;
248
249	hashval = *p << 4;
250	while (*p)
251		hashval+= *p++;
252	return &atab[hashval % ATABSIZE];
253}
254