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
32 * @(#)alias.c 8.3 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/alias.c 242766 2012-11-08 13:33:48Z jilles $
43 #include "options.h" /* XXX for argptr (should remove?) */
48 static struct alias *atab[ATABSIZE];
51 static void setalias(const char *, const char *);
52 static int unalias(const char *);
53 static struct alias **hashalias(const char *);
57 setalias(const char *name, const char *val)
59 struct alias *ap, **app;
61 app = hashalias(name);
62 for (ap = *app; ap; ap = ap->next) {
63 if (equal(name, ap->name)) {
68 ap->val = savestr(val);
71 size_t len = strlen(val);
72 ap->val = ckmalloc(len + 2);
73 memcpy(ap->val, val, len);
75 ap->val[len+1] = '\0';
84 ap = ckmalloc(sizeof (struct alias));
85 ap->name = savestr(name);
87 * XXX - HACK: in order that the parser will not finish reading the
88 * alias value off the input before processing the next alias, we
89 * dummy up an extra space at the end of the alias. This is a crock
90 * and should be re-thought. The idea (if you feel inclined to help)
91 * is to avoid alias recursions. The mechanism used is: when
92 * expanding an alias, the value of the alias is pushed back on the
93 * input as a string and a pointer to the alias is stored with the
94 * string. The alias is marked as being in use. When the input
95 * routine finishes reading the string, it marks the alias not
96 * in use. The problem is synchronization with the parser. Since
97 * it reads ahead, the alias is marked not in use before the
98 * resulting token(s) is next checked for further alias sub. The
99 * H A C K is that we add a little fluff after the alias value
100 * so that the string will not be exhausted. This is a good
101 * idea ------- ***NOT***
104 ap->val = savestr(val);
107 size_t len = strlen(val);
108 ap->val = ckmalloc(len + 2);
109 memcpy(ap->val, val, len);
110 ap->val[len] = ' '; /* fluff */
111 ap->val[len+1] = '\0';
122 unalias(const char *name)
124 struct alias *ap, **app;
126 app = hashalias(name);
128 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
129 if (equal(name, ap->name)) {
131 * if the alias is currently in use (i.e. its
132 * buffer is being used by the input routine) we
133 * just null out the name instead of freeing it.
134 * We could clear it out later, but this situation
135 * is so rare that it hardly seems worth it.
137 if (ap->flag & ALIASINUSE)
158 struct alias *ap, *tmp;
162 for (i = 0; i < ATABSIZE; i++) {
178 lookupalias(const char *name, int check)
180 struct alias *ap = *hashalias(name);
182 for (; ap; ap = ap->next) {
183 if (equal(name, ap->name)) {
184 if (check && (ap->flag & ALIASINUSE))
194 comparealiases(const void *p1, const void *p2)
196 const struct alias *const *a1 = p1;
197 const struct alias *const *a2 = p2;
199 return strcmp((*a1)->name, (*a2)->name);
203 printalias(const struct alias *a)
207 out1fmt("%s=", a->name);
208 /* Don't print the space added above. */
209 p = a->val + strlen(a->val) - 1;
220 struct alias **sorted, *ap;
222 sorted = ckmalloc(aliases * sizeof(*sorted));
224 for (i = 0; i < ATABSIZE; i++)
225 for (ap = atab[i]; ap; ap = ap->next)
226 if (*ap->name != '\0')
228 qsort(sorted, aliases, sizeof(*sorted), comparealiases);
229 for (i = 0; i < aliases; i++)
230 printalias(sorted[i]);
235 aliascmd(int argc, char **argv)
245 while ((n = *++argv) != NULL) {
246 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
247 if ((ap = lookupalias(n, 0)) == NULL) {
248 warning("%s: not found", n);
262 unaliascmd(int argc __unused, char **argv __unused)
266 while ((i = nextopt("a")) != '\0') {
272 for (i = 0; *argptr; argptr++)
273 i |= unalias(*argptr);
278 static struct alias **
279 hashalias(const char *p)
281 unsigned int hashval;
286 return &atab[hashval % ATABSIZE];