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
45 static struct alias *atab[ATABSIZE];
48 static void setalias(const char *, const char *);
49 static int unalias(const char *);
50 static size_t hashalias(const char *);
54 setalias(const char *name, const char *val)
56 struct alias *ap, **app;
59 app = &atab[hashalias(name)];
61 ap = ckmalloc(sizeof (struct alias));
62 ap->name = savestr(name);
63 ap->val = savestr(val);
72 freealias(struct alias *ap)
80 unalias(const char *name)
82 struct alias *ap, **app;
84 app = &atab[hashalias(name)];
86 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
87 if (equal(name, ap->name)) {
89 * if the alias is currently in use (i.e. its
90 * buffer is being used by the input routine) we
91 * just null out the name instead of freeing it.
92 * We could clear it out later, but this situation
93 * is so rare that it hardly seems worth it.
95 if (ap->flag & ALIASINUSE)
114 struct alias *ap, **app;
118 for (i = 0; i < ATABSIZE; i++) {
122 if (ap->flag & ALIASINUSE) {
136 lookupalias(const char *name, int check)
142 for (ap = atab[hashalias(name)]; ap; ap = ap->next) {
143 if (equal(name, ap->name)) {
144 if (check && (ap->flag & ALIASINUSE))
154 comparealiases(const void *p1, const void *p2)
156 const struct alias *const *a1 = p1;
157 const struct alias *const *a2 = p2;
159 return strcmp((*a1)->name, (*a2)->name);
163 printalias(const struct alias *a)
165 out1fmt("%s=", a->name);
174 struct alias **sorted, *ap;
177 sorted = ckmalloc(aliases * sizeof(*sorted));
179 for (i = 0; i < ATABSIZE; i++)
180 for (ap = atab[i]; ap; ap = ap->next)
181 if (*ap->name != '\0')
183 qsort(sorted, aliases, sizeof(*sorted), comparealiases);
184 for (i = 0; i < aliases; i++) {
185 printalias(sorted[i]);
194 aliascmd(int argc __unused, char **argv __unused)
202 if (*argptr == NULL) {
206 while ((n = *argptr++) != NULL) {
207 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
208 if ((ap = lookupalias(n, 0)) == NULL) {
209 warning("%s: not found", n);
223 unaliascmd(int argc __unused, char **argv __unused)
227 while ((i = nextopt("a")) != '\0') {
233 for (i = 0; *argptr; argptr++)
234 i |= unalias(*argptr);
240 hashalias(const char *p)
242 unsigned int hashval;
244 hashval = (unsigned char)*p << 4;
247 return (hashval % ATABSIZE);
251 iteralias(const struct alias *index)
256 if (index->next != NULL)
257 return (index->next);
258 i = hashalias(index->name) + 1;
260 for (; i < ATABSIZE; i++)