xref: /freebsd/bin/sh/alias.c (revision 5134c3f7990c353d2442ddd8a9a1d30b80099e43)
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  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <stdlib.h>
464b88c807SRodney W. Grimes #include "shell.h"
474b88c807SRodney W. Grimes #include "input.h"
484b88c807SRodney W. Grimes #include "output.h"
494b88c807SRodney W. Grimes #include "error.h"
504b88c807SRodney W. Grimes #include "memalloc.h"
514b88c807SRodney W. Grimes #include "mystring.h"
524b88c807SRodney W. Grimes #include "alias.h"
534b88c807SRodney W. Grimes #include "options.h"	/* XXX for argptr (should remove?) */
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes #define ATABSIZE 39
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes struct alias *atab[ATABSIZE];
584b88c807SRodney W. Grimes 
595134c3f7SWarner Losh STATIC void setalias(char *, char *);
605134c3f7SWarner Losh STATIC int unalias(char *);
615134c3f7SWarner Losh STATIC struct alias **hashalias(char *);
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes STATIC
64aa9caaf6SPeter Wemm void
655134c3f7SWarner Losh setalias(char *name, char *val)
664b88c807SRodney W. Grimes {
674b88c807SRodney W. Grimes 	struct alias *ap, **app;
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes 	app = hashalias(name);
704b88c807SRodney W. Grimes 	for (ap = *app; ap; ap = ap->next) {
714b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
724b88c807SRodney W. Grimes 			INTOFF;
734b88c807SRodney W. Grimes 			ckfree(ap->val);
744b88c807SRodney W. Grimes 			ap->val	= savestr(val);
754b88c807SRodney W. Grimes 			INTON;
764b88c807SRodney W. Grimes 			return;
774b88c807SRodney W. Grimes 		}
784b88c807SRodney W. Grimes 	}
794b88c807SRodney W. Grimes 	/* not found */
804b88c807SRodney W. Grimes 	INTOFF;
814b88c807SRodney W. Grimes 	ap = ckmalloc(sizeof (struct alias));
824b88c807SRodney W. Grimes 	ap->name = savestr(name);
834b88c807SRodney W. Grimes 	/*
844b88c807SRodney W. Grimes 	 * XXX - HACK: in order that the parser will not finish reading the
854b88c807SRodney W. Grimes 	 * alias value off the input before processing the next alias, we
864b88c807SRodney W. Grimes 	 * dummy up an extra space at the end of the alias.  This is a crock
874b88c807SRodney W. Grimes 	 * and should be re-thought.  The idea (if you feel inclined to help)
884b88c807SRodney W. Grimes 	 * is to avoid alias recursions.  The mechanism used is: when
894b88c807SRodney W. Grimes 	 * expanding an alias, the value of the alias is pushed back on the
904b88c807SRodney W. Grimes 	 * input as a string and a pointer to the alias is stored with the
914b88c807SRodney W. Grimes 	 * string.  The alias is marked as being in use.  When the input
9246be34b9SKris Kennaway 	 * routine finishes reading the string, it marks the alias not
934b88c807SRodney W. Grimes 	 * in use.  The problem is synchronization with the parser.  Since
944b88c807SRodney W. Grimes 	 * it reads ahead, the alias is marked not in use before the
954b88c807SRodney W. Grimes 	 * resulting token(s) is next checked for further alias sub.  The
964b88c807SRodney W. Grimes 	 * H A C K is that we add a little fluff after the alias value
974b88c807SRodney W. Grimes 	 * so that the string will not be exhausted.  This is a good
984b88c807SRodney W. Grimes 	 * idea ------- ***NOT***
994b88c807SRodney W. Grimes 	 */
1004b88c807SRodney W. Grimes #ifdef notyet
1014b88c807SRodney W. Grimes 	ap->val = savestr(val);
1024b88c807SRodney W. Grimes #else /* hack */
1034b88c807SRodney W. Grimes 	{
1044b88c807SRodney W. Grimes 	int len = strlen(val);
1054b88c807SRodney W. Grimes 	ap->val = ckmalloc(len + 2);
106aa9caaf6SPeter Wemm 	memcpy(ap->val, val, len);
1074b88c807SRodney W. Grimes 	ap->val[len] = ' ';	/* fluff */
1084b88c807SRodney W. Grimes 	ap->val[len+1] = '\0';
1094b88c807SRodney W. Grimes 	}
1104b88c807SRodney W. Grimes #endif
111410fb0acSSADA Kenji 	ap->flag = 0;
1124b88c807SRodney W. Grimes 	ap->next = *app;
1134b88c807SRodney W. Grimes 	*app = ap;
1144b88c807SRodney W. Grimes 	INTON;
1154b88c807SRodney W. Grimes }
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes STATIC int
1185134c3f7SWarner Losh unalias(char *name)
1194b88c807SRodney W. Grimes {
1204b88c807SRodney W. Grimes 	struct alias *ap, **app;
1214b88c807SRodney W. Grimes 
1224b88c807SRodney W. Grimes 	app = hashalias(name);
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes 	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
1254b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
1264b88c807SRodney W. Grimes 			/*
1274b88c807SRodney W. Grimes 			 * if the alias is currently in use (i.e. its
1284b88c807SRodney W. Grimes 			 * buffer is being used by the input routine) we
1294b88c807SRodney W. Grimes 			 * just null out the name instead of freeing it.
1304b88c807SRodney W. Grimes 			 * We could clear it out later, but this situation
1314b88c807SRodney W. Grimes 			 * is so rare that it hardly seems worth it.
1324b88c807SRodney W. Grimes 			 */
1334b88c807SRodney W. Grimes 			if (ap->flag & ALIASINUSE)
1344b88c807SRodney W. Grimes 				*ap->name = '\0';
1354b88c807SRodney W. Grimes 			else {
1364b88c807SRodney W. Grimes 				INTOFF;
1374b88c807SRodney W. Grimes 				*app = ap->next;
1384b88c807SRodney W. Grimes 				ckfree(ap->name);
1394b88c807SRodney W. Grimes 				ckfree(ap->val);
1404b88c807SRodney W. Grimes 				ckfree(ap);
1414b88c807SRodney W. Grimes 				INTON;
1424b88c807SRodney W. Grimes 			}
1434b88c807SRodney W. Grimes 			return (0);
1444b88c807SRodney W. Grimes 		}
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes 
1474b88c807SRodney W. Grimes 	return (1);
1484b88c807SRodney W. Grimes }
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes #ifdef mkinit
1514b88c807SRodney W. Grimes MKINIT void rmaliases();
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes SHELLPROC {
1544b88c807SRodney W. Grimes 	rmaliases();
1554b88c807SRodney W. Grimes }
1564b88c807SRodney W. Grimes #endif
1574b88c807SRodney W. Grimes 
1584b88c807SRodney W. Grimes void
1595134c3f7SWarner Losh rmaliases(void)
1605134c3f7SWarner Losh {
1614b88c807SRodney W. Grimes 	struct alias *ap, *tmp;
1624b88c807SRodney W. Grimes 	int i;
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 	INTOFF;
1654b88c807SRodney W. Grimes 	for (i = 0; i < ATABSIZE; i++) {
1664b88c807SRodney W. Grimes 		ap = atab[i];
1674b88c807SRodney W. Grimes 		atab[i] = NULL;
1684b88c807SRodney W. Grimes 		while (ap) {
1694b88c807SRodney W. Grimes 			ckfree(ap->name);
1704b88c807SRodney W. Grimes 			ckfree(ap->val);
1714b88c807SRodney W. Grimes 			tmp = ap;
1724b88c807SRodney W. Grimes 			ap = ap->next;
1734b88c807SRodney W. Grimes 			ckfree(tmp);
1744b88c807SRodney W. Grimes 		}
1754b88c807SRodney W. Grimes 	}
1764b88c807SRodney W. Grimes 	INTON;
1774b88c807SRodney W. Grimes }
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes struct alias *
1805134c3f7SWarner Losh lookupalias(char *name, int check)
1814b88c807SRodney W. Grimes {
1824b88c807SRodney W. Grimes 	struct alias *ap = *hashalias(name);
1834b88c807SRodney W. Grimes 
1844b88c807SRodney W. Grimes 	for (; ap; ap = ap->next) {
1854b88c807SRodney W. Grimes 		if (equal(name, ap->name)) {
1864b88c807SRodney W. Grimes 			if (check && (ap->flag & ALIASINUSE))
1874b88c807SRodney W. Grimes 				return (NULL);
1884b88c807SRodney W. Grimes 			return (ap);
1894b88c807SRodney W. Grimes 		}
1904b88c807SRodney W. Grimes 	}
1914b88c807SRodney W. Grimes 
1924b88c807SRodney W. Grimes 	return (NULL);
1934b88c807SRodney W. Grimes }
1944b88c807SRodney W. Grimes 
1954b88c807SRodney W. Grimes /*
1964b88c807SRodney W. Grimes  * TODO - sort output
1974b88c807SRodney W. Grimes  */
198aa9caaf6SPeter Wemm int
1995134c3f7SWarner Losh aliascmd(int argc, char **argv)
2004b88c807SRodney W. Grimes {
2014b88c807SRodney W. Grimes 	char *n, *v;
2024b88c807SRodney W. Grimes 	int ret = 0;
2034b88c807SRodney W. Grimes 	struct alias *ap;
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes 	if (argc == 1) {
2064b88c807SRodney W. Grimes 		int i;
2074b88c807SRodney W. Grimes 
2084b88c807SRodney W. Grimes 		for (i = 0; i < ATABSIZE; i++)
2094b88c807SRodney W. Grimes 			for (ap = atab[i]; ap; ap = ap->next) {
2104b88c807SRodney W. Grimes 				if (*ap->name != '\0')
2114b88c807SRodney W. Grimes 				    out1fmt("alias %s=%s\n", ap->name, ap->val);
2124b88c807SRodney W. Grimes 			}
2134b88c807SRodney W. Grimes 		return (0);
2144b88c807SRodney W. Grimes 	}
215aa9caaf6SPeter Wemm 	while ((n = *++argv) != NULL) {
2164b88c807SRodney W. Grimes 		if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
2174b88c807SRodney W. Grimes 			if ((ap = lookupalias(n, 0)) == NULL) {
2184b88c807SRodney W. Grimes 				outfmt(out2, "alias: %s not found\n", n);
2194b88c807SRodney W. Grimes 				ret = 1;
2204b88c807SRodney W. Grimes 			} else
2214b88c807SRodney W. Grimes 				out1fmt("alias %s=%s\n", n, ap->val);
2224b88c807SRodney W. Grimes 		else {
2234b88c807SRodney W. Grimes 			*v++ = '\0';
2244b88c807SRodney W. Grimes 			setalias(n, v);
2254b88c807SRodney W. Grimes 		}
2264b88c807SRodney W. Grimes 	}
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 	return (ret);
2294b88c807SRodney W. Grimes }
2304b88c807SRodney W. Grimes 
231aa9caaf6SPeter Wemm int
2325134c3f7SWarner Losh unaliascmd(int argc __unused, char **argv __unused)
2334b88c807SRodney W. Grimes {
2344b88c807SRodney W. Grimes 	int i;
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes 	while ((i = nextopt("a")) != '\0') {
2374b88c807SRodney W. Grimes 		if (i == 'a') {
2384b88c807SRodney W. Grimes 			rmaliases();
2394b88c807SRodney W. Grimes 			return (0);
2404b88c807SRodney W. Grimes 		}
2414b88c807SRodney W. Grimes 	}
2424b88c807SRodney W. Grimes 	for (i = 0; *argptr; argptr++)
2434b88c807SRodney W. Grimes 		i = unalias(*argptr);
2444b88c807SRodney W. Grimes 
2454b88c807SRodney W. Grimes 	return (i);
2464b88c807SRodney W. Grimes }
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes STATIC struct alias **
2495134c3f7SWarner Losh hashalias(char *p)
2504b88c807SRodney W. Grimes {
2514b88c807SRodney W. Grimes 	unsigned int hashval;
2524b88c807SRodney W. Grimes 
2534b88c807SRodney W. Grimes 	hashval = *p << 4;
2544b88c807SRodney W. Grimes 	while (*p)
2554b88c807SRodney W. Grimes 		hashval+= *p++;
2564b88c807SRodney W. Grimes 	return &atab[hashval % ATABSIZE];
2574b88c807SRodney W. Grimes }
258