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