3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 3. 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.
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
35 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: head/bin/sh/alias.c 317039 2017-04-16 22:10:02Z jilles $");
53 static struct alias *atab[ATABSIZE];
56 static void setalias(const char *, const char *);
57 static int unalias(const char *);
58 static struct alias **hashalias(const char *);
62 setalias(const char *name, const char *val)
64 struct alias *ap, **app;
67 app = hashalias(name);
69 ap = ckmalloc(sizeof (struct alias));
70 ap->name = savestr(name);
71 ap->val = savestr(val);
80 freealias(struct alias *ap)
88 unalias(const char *name)
90 struct alias *ap, **app;
92 app = hashalias(name);
94 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
95 if (equal(name, ap->name)) {
97 * if the alias is currently in use (i.e. its
98 * buffer is being used by the input routine) we
99 * just null out the name instead of freeing it.
100 * We could clear it out later, but this situation
101 * is so rare that it hardly seems worth it.
103 if (ap->flag & ALIASINUSE)
122 struct alias *ap, **app;
126 for (i = 0; i < ATABSIZE; i++) {
130 if (ap->flag & ALIASINUSE) {
144 lookupalias(const char *name, int check)
150 for (ap = *hashalias(name); ap; ap = ap->next) {
151 if (equal(name, ap->name)) {
152 if (check && (ap->flag & ALIASINUSE))
162 comparealiases(const void *p1, const void *p2)
164 const struct alias *const *a1 = p1;
165 const struct alias *const *a2 = p2;
167 return strcmp((*a1)->name, (*a2)->name);
171 printalias(const struct alias *a)
173 out1fmt("%s=", a->name);
182 struct alias **sorted, *ap;
185 sorted = ckmalloc(aliases * sizeof(*sorted));
187 for (i = 0; i < ATABSIZE; i++)
188 for (ap = atab[i]; ap; ap = ap->next)
189 if (*ap->name != '\0')
191 qsort(sorted, aliases, sizeof(*sorted), comparealiases);
192 for (i = 0; i < aliases; i++) {
193 printalias(sorted[i]);
202 aliascmd(int argc __unused, char **argv __unused)
210 if (*argptr == NULL) {
214 while ((n = *argptr++) != NULL) {
215 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
216 if ((ap = lookupalias(n, 0)) == NULL) {
217 warning("%s: not found", n);
231 unaliascmd(int argc __unused, char **argv __unused)
235 while ((i = nextopt("a")) != '\0') {
241 for (i = 0; *argptr; argptr++)
242 i |= unalias(*argptr);
247 static struct alias **
248 hashalias(const char *p)
250 unsigned int hashval;
252 hashval = (unsigned char)*p << 4;
255 return &atab[hashval % ATABSIZE];