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