alias.c revision 99110
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#if 0
39static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
40#endif
41#endif /* not lint */
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/bin/sh/alias.c 99110 2002-06-30 05:15:05Z obrien $");
44
45#include <stdlib.h>
46#include "shell.h"
47#include "input.h"
48#include "output.h"
49#include "error.h"
50#include "memalloc.h"
51#include "mystring.h"
52#include "alias.h"
53#include "options.h"	/* XXX for argptr (should remove?) */
54
55#define ATABSIZE 39
56
57struct alias *atab[ATABSIZE];
58
59STATIC void setalias(char *, char *);
60STATIC int unalias(char *);
61STATIC struct alias **hashalias(char *);
62
63STATIC
64void
65setalias(char *name, char *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 marks 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->flag = 0;
112	ap->next = *app;
113	*app = ap;
114	INTON;
115}
116
117STATIC int
118unalias(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
151MKINIT void rmaliases();
152
153SHELLPROC {
154	rmaliases();
155}
156#endif
157
158void
159rmaliases(void)
160{
161	struct alias *ap, *tmp;
162	int i;
163
164	INTOFF;
165	for (i = 0; i < ATABSIZE; i++) {
166		ap = atab[i];
167		atab[i] = NULL;
168		while (ap) {
169			ckfree(ap->name);
170			ckfree(ap->val);
171			tmp = ap;
172			ap = ap->next;
173			ckfree(tmp);
174		}
175	}
176	INTON;
177}
178
179struct alias *
180lookupalias(char *name, 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(int argc, char **argv)
200{
201	char *n, *v;
202	int ret = 0;
203	struct alias *ap;
204
205	if (argc == 1) {
206		int i;
207
208		for (i = 0; i < ATABSIZE; i++)
209			for (ap = atab[i]; ap; ap = ap->next) {
210				if (*ap->name != '\0') {
211					out1fmt("alias %s=", ap->name);
212					out1qstr(ap->val);
213					out1c('\n');
214				}
215			}
216		return (0);
217	}
218	while ((n = *++argv) != NULL) {
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=", n);
225				out1qstr(ap->val);
226				out1c('\n');
227			}
228		else {
229			*v++ = '\0';
230			setalias(n, v);
231		}
232	}
233
234	return (ret);
235}
236
237int
238unaliascmd(int argc __unused, char **argv __unused)
239{
240	int i;
241
242	while ((i = nextopt("a")) != '\0') {
243		if (i == 'a') {
244			rmaliases();
245			return (0);
246		}
247	}
248	for (i = 0; *argptr; argptr++)
249		i = unalias(*argptr);
250
251	return (i);
252}
253
254STATIC struct alias **
255hashalias(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