xref: /freebsd/bin/sh/alias.c (revision 1632bf1a88ce60fd335ccb030d1868db6f2f279f)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <stdlib.h>
424b88c807SRodney W. Grimes #include "shell.h"
434b88c807SRodney W. Grimes #include "output.h"
444b88c807SRodney W. Grimes #include "error.h"
454b88c807SRodney W. Grimes #include "memalloc.h"
464b88c807SRodney W. Grimes #include "mystring.h"
474b88c807SRodney W. Grimes #include "alias.h"
484b88c807SRodney W. Grimes #include "options.h"	/* XXX for argptr (should remove?) */
49454a02b3SJilles Tjoelker #include "builtins.h"
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes #define ATABSIZE 39
524b88c807SRodney W. Grimes 
53aa7b6f82SDavid E. O'Brien static struct alias *atab[ATABSIZE];
54aa7b6f82SDavid E. O'Brien static int aliases;
554b88c807SRodney W. Grimes 
5688328642SDavid E. O'Brien static void setalias(const char *, const char *);
5788328642SDavid E. O'Brien static int unalias(const char *);
5888328642SDavid E. O'Brien static struct alias **hashalias(const char *);
594b88c807SRodney W. Grimes 
6088328642SDavid E. O'Brien static
61aa9caaf6SPeter Wemm void
622cac6e36SJilles Tjoelker setalias(const char *name, const char *val)
634b88c807SRodney W. Grimes {
644b88c807SRodney W. Grimes 	struct alias *ap, **app;
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes 	app = hashalias(name);
674b88c807SRodney W. Grimes 	for (ap = *app; ap; ap = ap->next) {
684b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
694b88c807SRodney W. Grimes 			INTOFF;
704b88c807SRodney W. Grimes 			ckfree(ap->val);
714b88c807SRodney W. Grimes 			ap->val	= savestr(val);
724b88c807SRodney W. Grimes 			INTON;
734b88c807SRodney W. Grimes 			return;
744b88c807SRodney W. Grimes 		}
754b88c807SRodney W. Grimes 	}
764b88c807SRodney W. Grimes 	/* not found */
774b88c807SRodney W. Grimes 	INTOFF;
784b88c807SRodney W. Grimes 	ap = ckmalloc(sizeof (struct alias));
794b88c807SRodney W. Grimes 	ap->name = savestr(name);
804b88c807SRodney W. Grimes 	ap->val = savestr(val);
81410fb0acSSADA Kenji 	ap->flag = 0;
824b88c807SRodney W. Grimes 	ap->next = *app;
834b88c807SRodney W. Grimes 	*app = ap;
84d4b1e374SStefan Farfeleder 	aliases++;
854b88c807SRodney W. Grimes 	INTON;
864b88c807SRodney W. Grimes }
874b88c807SRodney W. Grimes 
8888328642SDavid E. O'Brien static int
89a65d88d1SRalf S. Engelschall unalias(const char *name)
904b88c807SRodney W. Grimes {
914b88c807SRodney W. Grimes 	struct alias *ap, **app;
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes 	app = hashalias(name);
944b88c807SRodney W. Grimes 
954b88c807SRodney W. Grimes 	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
964b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
974b88c807SRodney W. Grimes 			/*
984b88c807SRodney W. Grimes 			 * if the alias is currently in use (i.e. its
994b88c807SRodney W. Grimes 			 * buffer is being used by the input routine) we
1004b88c807SRodney W. Grimes 			 * just null out the name instead of freeing it.
1014b88c807SRodney W. Grimes 			 * We could clear it out later, but this situation
1024b88c807SRodney W. Grimes 			 * is so rare that it hardly seems worth it.
1034b88c807SRodney W. Grimes 			 */
1044b88c807SRodney W. Grimes 			if (ap->flag & ALIASINUSE)
1054b88c807SRodney W. Grimes 				*ap->name = '\0';
1064b88c807SRodney W. Grimes 			else {
1074b88c807SRodney W. Grimes 				INTOFF;
1084b88c807SRodney W. Grimes 				*app = ap->next;
1094b88c807SRodney W. Grimes 				ckfree(ap->name);
1104b88c807SRodney W. Grimes 				ckfree(ap->val);
1114b88c807SRodney W. Grimes 				ckfree(ap);
1124b88c807SRodney W. Grimes 				INTON;
1134b88c807SRodney W. Grimes 			}
114d4b1e374SStefan Farfeleder 			aliases--;
1154b88c807SRodney W. Grimes 			return (0);
1164b88c807SRodney W. Grimes 		}
1174b88c807SRodney W. Grimes 	}
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes 	return (1);
1204b88c807SRodney W. Grimes }
1214b88c807SRodney W. Grimes 
1223835f47cSJilles Tjoelker static void
1235134c3f7SWarner Losh rmaliases(void)
1245134c3f7SWarner Losh {
1254b88c807SRodney W. Grimes 	struct alias *ap, *tmp;
1264b88c807SRodney W. Grimes 	int i;
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 	INTOFF;
1294b88c807SRodney W. Grimes 	for (i = 0; i < ATABSIZE; i++) {
1304b88c807SRodney W. Grimes 		ap = atab[i];
1314b88c807SRodney W. Grimes 		atab[i] = NULL;
1324b88c807SRodney W. Grimes 		while (ap) {
1334b88c807SRodney W. Grimes 			ckfree(ap->name);
1344b88c807SRodney W. Grimes 			ckfree(ap->val);
1354b88c807SRodney W. Grimes 			tmp = ap;
1364b88c807SRodney W. Grimes 			ap = ap->next;
1374b88c807SRodney W. Grimes 			ckfree(tmp);
1384b88c807SRodney W. Grimes 		}
1394b88c807SRodney W. Grimes 	}
140d4b1e374SStefan Farfeleder 	aliases = 0;
1414b88c807SRodney W. Grimes 	INTON;
1424b88c807SRodney W. Grimes }
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes struct alias *
1452cac6e36SJilles Tjoelker lookupalias(const char *name, int check)
1464b88c807SRodney W. Grimes {
1474b88c807SRodney W. Grimes 	struct alias *ap = *hashalias(name);
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 	for (; ap; ap = ap->next) {
1504b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
1514b88c807SRodney W. Grimes 			if (check && (ap->flag & ALIASINUSE))
1524b88c807SRodney W. Grimes 				return (NULL);
1534b88c807SRodney W. Grimes 			return (ap);
1544b88c807SRodney W. Grimes 		}
1554b88c807SRodney W. Grimes 	}
1564b88c807SRodney W. Grimes 
1574b88c807SRodney W. Grimes 	return (NULL);
1584b88c807SRodney W. Grimes }
1594b88c807SRodney W. Grimes 
16088328642SDavid E. O'Brien static int
161d4b1e374SStefan Farfeleder comparealiases(const void *p1, const void *p2)
162d4b1e374SStefan Farfeleder {
163d4b1e374SStefan Farfeleder 	const struct alias *const *a1 = p1;
164d4b1e374SStefan Farfeleder 	const struct alias *const *a2 = p2;
165d4b1e374SStefan Farfeleder 
166d4b1e374SStefan Farfeleder 	return strcmp((*a1)->name, (*a2)->name);
167d4b1e374SStefan Farfeleder }
168d4b1e374SStefan Farfeleder 
16988328642SDavid E. O'Brien static void
170d4b1e374SStefan Farfeleder printalias(const struct alias *a)
171d4b1e374SStefan Farfeleder {
172d4b1e374SStefan Farfeleder 	out1fmt("%s=", a->name);
173d4b1e374SStefan Farfeleder 	out1qstr(a->val);
174d4b1e374SStefan Farfeleder 	out1c('\n');
175d4b1e374SStefan Farfeleder }
176d4b1e374SStefan Farfeleder 
17788328642SDavid E. O'Brien static void
178d4b1e374SStefan Farfeleder printaliases(void)
179d4b1e374SStefan Farfeleder {
180d4b1e374SStefan Farfeleder 	int i, j;
181d4b1e374SStefan Farfeleder 	struct alias **sorted, *ap;
182d4b1e374SStefan Farfeleder 
183*1632bf1aSJilles Tjoelker 	INTOFF;
184d4b1e374SStefan Farfeleder 	sorted = ckmalloc(aliases * sizeof(*sorted));
185d4b1e374SStefan Farfeleder 	j = 0;
186d4b1e374SStefan Farfeleder 	for (i = 0; i < ATABSIZE; i++)
187d4b1e374SStefan Farfeleder 		for (ap = atab[i]; ap; ap = ap->next)
188d4b1e374SStefan Farfeleder 			if (*ap->name != '\0')
189d4b1e374SStefan Farfeleder 				sorted[j++] = ap;
190d4b1e374SStefan Farfeleder 	qsort(sorted, aliases, sizeof(*sorted), comparealiases);
191*1632bf1aSJilles Tjoelker 	for (i = 0; i < aliases; i++) {
192d4b1e374SStefan Farfeleder 		printalias(sorted[i]);
193*1632bf1aSJilles Tjoelker 		if (int_pending())
194*1632bf1aSJilles Tjoelker 			break;
195*1632bf1aSJilles Tjoelker 	}
196d4b1e374SStefan Farfeleder 	ckfree(sorted);
197*1632bf1aSJilles Tjoelker 	INTON;
198d4b1e374SStefan Farfeleder }
199d4b1e374SStefan Farfeleder 
200aa9caaf6SPeter Wemm int
20171828da5SJilles Tjoelker aliascmd(int argc __unused, char **argv __unused)
2024b88c807SRodney W. Grimes {
2034b88c807SRodney W. Grimes 	char *n, *v;
2044b88c807SRodney W. Grimes 	int ret = 0;
2054b88c807SRodney W. Grimes 	struct alias *ap;
2064b88c807SRodney W. Grimes 
20771828da5SJilles Tjoelker 	nextopt("");
20871828da5SJilles Tjoelker 
20971828da5SJilles Tjoelker 	if (*argptr == NULL) {
210d4b1e374SStefan Farfeleder 		printaliases();
2114b88c807SRodney W. Grimes 		return (0);
2124b88c807SRodney W. Grimes 	}
21371828da5SJilles Tjoelker 	while ((n = *argptr++) != NULL) {
2144b88c807SRodney W. Grimes 		if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
2154b88c807SRodney W. Grimes 			if ((ap = lookupalias(n, 0)) == NULL) {
21647a5ab29SJilles Tjoelker 				warning("%s: not found", n);
2174b88c807SRodney W. Grimes 				ret = 1;
218d4b1e374SStefan Farfeleder 			} else
219d4b1e374SStefan Farfeleder 				printalias(ap);
2204b88c807SRodney W. Grimes 		else {
2214b88c807SRodney W. Grimes 			*v++ = '\0';
2224b88c807SRodney W. Grimes 			setalias(n, v);
2234b88c807SRodney W. Grimes 		}
2244b88c807SRodney W. Grimes 	}
2254b88c807SRodney W. Grimes 
2264b88c807SRodney W. Grimes 	return (ret);
2274b88c807SRodney W. Grimes }
2284b88c807SRodney W. Grimes 
229aa9caaf6SPeter Wemm int
2305134c3f7SWarner Losh unaliascmd(int argc __unused, char **argv __unused)
2314b88c807SRodney W. Grimes {
2324b88c807SRodney W. Grimes 	int i;
2334b88c807SRodney W. Grimes 
2344b88c807SRodney W. Grimes 	while ((i = nextopt("a")) != '\0') {
2354b88c807SRodney W. Grimes 		if (i == 'a') {
2364b88c807SRodney W. Grimes 			rmaliases();
2374b88c807SRodney W. Grimes 			return (0);
2384b88c807SRodney W. Grimes 		}
2394b88c807SRodney W. Grimes 	}
2404b88c807SRodney W. Grimes 	for (i = 0; *argptr; argptr++)
241dbf2e1c5SStefan Farfeleder 		i |= unalias(*argptr);
2424b88c807SRodney W. Grimes 
2434b88c807SRodney W. Grimes 	return (i);
2444b88c807SRodney W. Grimes }
2454b88c807SRodney W. Grimes 
24688328642SDavid E. O'Brien static struct alias **
247a65d88d1SRalf S. Engelschall hashalias(const char *p)
2484b88c807SRodney W. Grimes {
2494b88c807SRodney W. Grimes 	unsigned int hashval;
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes 	hashval = *p << 4;
2524b88c807SRodney W. Grimes 	while (*p)
2534b88c807SRodney W. Grimes 		hashval+= *p++;
2544b88c807SRodney W. Grimes 	return &atab[hashval % ATABSIZE];
2554b88c807SRodney W. Grimes }
256