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.
16fbbd9655SWarner Losh * 3. 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
33aa9caaf6SPeter Wemm #include <stdlib.h>
344b88c807SRodney W. Grimes #include "shell.h"
354b88c807SRodney W. Grimes #include "output.h"
364b88c807SRodney W. Grimes #include "error.h"
374b88c807SRodney W. Grimes #include "memalloc.h"
384b88c807SRodney W. Grimes #include "mystring.h"
394b88c807SRodney W. Grimes #include "alias.h"
4005e626c3SJilles Tjoelker #include "options.h"
41454a02b3SJilles Tjoelker #include "builtins.h"
424b88c807SRodney W. Grimes
434b88c807SRodney W. Grimes #define ATABSIZE 39
444b88c807SRodney W. Grimes
45aa7b6f82SDavid E. O'Brien static struct alias *atab[ATABSIZE];
46aa7b6f82SDavid E. O'Brien static int aliases;
474b88c807SRodney W. Grimes
4888328642SDavid E. O'Brien static void setalias(const char *, const char *);
4988328642SDavid E. O'Brien static int unalias(const char *);
50*2fc4a84eSPiotr Pawel Stefaniak static size_t hashalias(const char *);
514b88c807SRodney W. Grimes
5288328642SDavid E. O'Brien static
53aa9caaf6SPeter Wemm void
setalias(const char * name,const char * val)542cac6e36SJilles Tjoelker setalias(const char *name, const char *val)
554b88c807SRodney W. Grimes {
564b88c807SRodney W. Grimes struct alias *ap, **app;
574b88c807SRodney W. Grimes
58026dfd4aSJilles Tjoelker unalias(name);
59*2fc4a84eSPiotr Pawel Stefaniak app = &atab[hashalias(name)];
604b88c807SRodney W. Grimes INTOFF;
614b88c807SRodney W. Grimes ap = ckmalloc(sizeof (struct alias));
624b88c807SRodney W. Grimes ap->name = savestr(name);
634b88c807SRodney W. Grimes ap->val = savestr(val);
64410fb0acSSADA Kenji ap->flag = 0;
654b88c807SRodney W. Grimes ap->next = *app;
664b88c807SRodney W. Grimes *app = ap;
67d4b1e374SStefan Farfeleder aliases++;
684b88c807SRodney W. Grimes INTON;
694b88c807SRodney W. Grimes }
704b88c807SRodney W. Grimes
71773e27aeSJilles Tjoelker static void
freealias(struct alias * ap)72773e27aeSJilles Tjoelker freealias(struct alias *ap)
73773e27aeSJilles Tjoelker {
74773e27aeSJilles Tjoelker ckfree(ap->name);
75773e27aeSJilles Tjoelker ckfree(ap->val);
76773e27aeSJilles Tjoelker ckfree(ap);
77773e27aeSJilles Tjoelker }
78773e27aeSJilles Tjoelker
7988328642SDavid E. O'Brien static int
unalias(const char * name)80a65d88d1SRalf S. Engelschall unalias(const char *name)
814b88c807SRodney W. Grimes {
824b88c807SRodney W. Grimes struct alias *ap, **app;
834b88c807SRodney W. Grimes
84*2fc4a84eSPiotr Pawel Stefaniak app = &atab[hashalias(name)];
854b88c807SRodney W. Grimes
864b88c807SRodney W. Grimes for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
874b88c807SRodney W. Grimes if (equal(name, ap->name)) {
884b88c807SRodney W. Grimes /*
894b88c807SRodney W. Grimes * if the alias is currently in use (i.e. its
904b88c807SRodney W. Grimes * buffer is being used by the input routine) we
914b88c807SRodney W. Grimes * just null out the name instead of freeing it.
924b88c807SRodney W. Grimes * We could clear it out later, but this situation
934b88c807SRodney W. Grimes * is so rare that it hardly seems worth it.
944b88c807SRodney W. Grimes */
954b88c807SRodney W. Grimes if (ap->flag & ALIASINUSE)
964b88c807SRodney W. Grimes *ap->name = '\0';
974b88c807SRodney W. Grimes else {
984b88c807SRodney W. Grimes INTOFF;
994b88c807SRodney W. Grimes *app = ap->next;
100773e27aeSJilles Tjoelker freealias(ap);
1014b88c807SRodney W. Grimes INTON;
1024b88c807SRodney W. Grimes }
103d4b1e374SStefan Farfeleder aliases--;
1044b88c807SRodney W. Grimes return (0);
1054b88c807SRodney W. Grimes }
1064b88c807SRodney W. Grimes }
1074b88c807SRodney W. Grimes
1084b88c807SRodney W. Grimes return (1);
1094b88c807SRodney W. Grimes }
1104b88c807SRodney W. Grimes
1113835f47cSJilles Tjoelker static void
rmaliases(void)1125134c3f7SWarner Losh rmaliases(void)
1135134c3f7SWarner Losh {
114773e27aeSJilles Tjoelker struct alias *ap, **app;
1154b88c807SRodney W. Grimes int i;
1164b88c807SRodney W. Grimes
1174b88c807SRodney W. Grimes INTOFF;
1184b88c807SRodney W. Grimes for (i = 0; i < ATABSIZE; i++) {
119773e27aeSJilles Tjoelker app = &atab[i];
120773e27aeSJilles Tjoelker while (*app) {
121773e27aeSJilles Tjoelker ap = *app;
122773e27aeSJilles Tjoelker if (ap->flag & ALIASINUSE) {
123773e27aeSJilles Tjoelker *ap->name = '\0';
124773e27aeSJilles Tjoelker app = &(*app)->next;
125773e27aeSJilles Tjoelker } else {
126773e27aeSJilles Tjoelker *app = ap->next;
127773e27aeSJilles Tjoelker freealias(ap);
128773e27aeSJilles Tjoelker }
1294b88c807SRodney W. Grimes }
1304b88c807SRodney W. Grimes }
131d4b1e374SStefan Farfeleder aliases = 0;
1324b88c807SRodney W. Grimes INTON;
1334b88c807SRodney W. Grimes }
1344b88c807SRodney W. Grimes
1354b88c807SRodney W. Grimes struct alias *
lookupalias(const char * name,int check)1362cac6e36SJilles Tjoelker lookupalias(const char *name, int check)
1374b88c807SRodney W. Grimes {
1382ece3386SJilles Tjoelker struct alias *ap;
1394b88c807SRodney W. Grimes
1402ece3386SJilles Tjoelker if (aliases == 0)
1412ece3386SJilles Tjoelker return (NULL);
142*2fc4a84eSPiotr Pawel Stefaniak for (ap = atab[hashalias(name)]; ap; ap = ap->next) {
1434b88c807SRodney W. Grimes if (equal(name, ap->name)) {
1444b88c807SRodney W. Grimes if (check && (ap->flag & ALIASINUSE))
1454b88c807SRodney W. Grimes return (NULL);
1464b88c807SRodney W. Grimes return (ap);
1474b88c807SRodney W. Grimes }
1484b88c807SRodney W. Grimes }
1494b88c807SRodney W. Grimes
1504b88c807SRodney W. Grimes return (NULL);
1514b88c807SRodney W. Grimes }
1524b88c807SRodney W. Grimes
15388328642SDavid E. O'Brien static int
comparealiases(const void * p1,const void * p2)154d4b1e374SStefan Farfeleder comparealiases(const void *p1, const void *p2)
155d4b1e374SStefan Farfeleder {
156d4b1e374SStefan Farfeleder const struct alias *const *a1 = p1;
157d4b1e374SStefan Farfeleder const struct alias *const *a2 = p2;
158d4b1e374SStefan Farfeleder
159d4b1e374SStefan Farfeleder return strcmp((*a1)->name, (*a2)->name);
160d4b1e374SStefan Farfeleder }
161d4b1e374SStefan Farfeleder
16288328642SDavid E. O'Brien static void
printalias(const struct alias * a)163d4b1e374SStefan Farfeleder printalias(const struct alias *a)
164d4b1e374SStefan Farfeleder {
165d4b1e374SStefan Farfeleder out1fmt("%s=", a->name);
166d4b1e374SStefan Farfeleder out1qstr(a->val);
167d4b1e374SStefan Farfeleder out1c('\n');
168d4b1e374SStefan Farfeleder }
169d4b1e374SStefan Farfeleder
17088328642SDavid E. O'Brien static void
printaliases(void)171d4b1e374SStefan Farfeleder printaliases(void)
172d4b1e374SStefan Farfeleder {
173d4b1e374SStefan Farfeleder int i, j;
174d4b1e374SStefan Farfeleder struct alias **sorted, *ap;
175d4b1e374SStefan Farfeleder
1761632bf1aSJilles Tjoelker INTOFF;
177d4b1e374SStefan Farfeleder sorted = ckmalloc(aliases * sizeof(*sorted));
178d4b1e374SStefan Farfeleder j = 0;
179d4b1e374SStefan Farfeleder for (i = 0; i < ATABSIZE; i++)
180d4b1e374SStefan Farfeleder for (ap = atab[i]; ap; ap = ap->next)
181d4b1e374SStefan Farfeleder if (*ap->name != '\0')
182d4b1e374SStefan Farfeleder sorted[j++] = ap;
183d4b1e374SStefan Farfeleder qsort(sorted, aliases, sizeof(*sorted), comparealiases);
1841632bf1aSJilles Tjoelker for (i = 0; i < aliases; i++) {
185d4b1e374SStefan Farfeleder printalias(sorted[i]);
1861632bf1aSJilles Tjoelker if (int_pending())
1871632bf1aSJilles Tjoelker break;
1881632bf1aSJilles Tjoelker }
189d4b1e374SStefan Farfeleder ckfree(sorted);
1901632bf1aSJilles Tjoelker INTON;
191d4b1e374SStefan Farfeleder }
192d4b1e374SStefan Farfeleder
193aa9caaf6SPeter Wemm int
aliascmd(int argc __unused,char ** argv __unused)19471828da5SJilles Tjoelker aliascmd(int argc __unused, char **argv __unused)
1954b88c807SRodney W. Grimes {
1964b88c807SRodney W. Grimes char *n, *v;
1974b88c807SRodney W. Grimes int ret = 0;
1984b88c807SRodney W. Grimes struct alias *ap;
1994b88c807SRodney W. Grimes
20071828da5SJilles Tjoelker nextopt("");
20171828da5SJilles Tjoelker
20271828da5SJilles Tjoelker if (*argptr == NULL) {
203d4b1e374SStefan Farfeleder printaliases();
2044b88c807SRodney W. Grimes return (0);
2054b88c807SRodney W. Grimes }
20671828da5SJilles Tjoelker while ((n = *argptr++) != NULL) {
2074b88c807SRodney W. Grimes if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
2084b88c807SRodney W. Grimes if ((ap = lookupalias(n, 0)) == NULL) {
20947a5ab29SJilles Tjoelker warning("%s: not found", n);
2104b88c807SRodney W. Grimes ret = 1;
211d4b1e374SStefan Farfeleder } else
212d4b1e374SStefan Farfeleder printalias(ap);
2134b88c807SRodney W. Grimes else {
2144b88c807SRodney W. Grimes *v++ = '\0';
2154b88c807SRodney W. Grimes setalias(n, v);
2164b88c807SRodney W. Grimes }
2174b88c807SRodney W. Grimes }
2184b88c807SRodney W. Grimes
2194b88c807SRodney W. Grimes return (ret);
2204b88c807SRodney W. Grimes }
2214b88c807SRodney W. Grimes
222aa9caaf6SPeter Wemm int
unaliascmd(int argc __unused,char ** argv __unused)2235134c3f7SWarner Losh unaliascmd(int argc __unused, char **argv __unused)
2244b88c807SRodney W. Grimes {
2254b88c807SRodney W. Grimes int i;
2264b88c807SRodney W. Grimes
2274b88c807SRodney W. Grimes while ((i = nextopt("a")) != '\0') {
2284b88c807SRodney W. Grimes if (i == 'a') {
2294b88c807SRodney W. Grimes rmaliases();
2304b88c807SRodney W. Grimes return (0);
2314b88c807SRodney W. Grimes }
2324b88c807SRodney W. Grimes }
2334b88c807SRodney W. Grimes for (i = 0; *argptr; argptr++)
234dbf2e1c5SStefan Farfeleder i |= unalias(*argptr);
2354b88c807SRodney W. Grimes
2364b88c807SRodney W. Grimes return (i);
2374b88c807SRodney W. Grimes }
2384b88c807SRodney W. Grimes
239*2fc4a84eSPiotr Pawel Stefaniak static size_t
hashalias(const char * p)240a65d88d1SRalf S. Engelschall hashalias(const char *p)
2414b88c807SRodney W. Grimes {
2424b88c807SRodney W. Grimes unsigned int hashval;
2434b88c807SRodney W. Grimes
244c3c85727SJilles Tjoelker hashval = (unsigned char)*p << 4;
2454b88c807SRodney W. Grimes while (*p)
2464b88c807SRodney W. Grimes hashval+= *p++;
247*2fc4a84eSPiotr Pawel Stefaniak return (hashval % ATABSIZE);
248*2fc4a84eSPiotr Pawel Stefaniak }
249*2fc4a84eSPiotr Pawel Stefaniak
250*2fc4a84eSPiotr Pawel Stefaniak const struct alias *
iteralias(const struct alias * index)251*2fc4a84eSPiotr Pawel Stefaniak iteralias(const struct alias *index)
252*2fc4a84eSPiotr Pawel Stefaniak {
253*2fc4a84eSPiotr Pawel Stefaniak size_t i = 0;
254*2fc4a84eSPiotr Pawel Stefaniak
255*2fc4a84eSPiotr Pawel Stefaniak if (index != NULL) {
256*2fc4a84eSPiotr Pawel Stefaniak if (index->next != NULL)
257*2fc4a84eSPiotr Pawel Stefaniak return (index->next);
258*2fc4a84eSPiotr Pawel Stefaniak i = hashalias(index->name) + 1;
259*2fc4a84eSPiotr Pawel Stefaniak }
260*2fc4a84eSPiotr Pawel Stefaniak for (; i < ATABSIZE; i++)
261*2fc4a84eSPiotr Pawel Stefaniak if (atab[i] != NULL)
262*2fc4a84eSPiotr Pawel Stefaniak return (atab[i]);
263*2fc4a84eSPiotr Pawel Stefaniak
264*2fc4a84eSPiotr Pawel Stefaniak return (NULL);
2654b88c807SRodney W. Grimes }
266