mtree: no more /lib and /lib/i386.
[minix.git] / commands / ash / alias.c
blob9f197ed6e83437719c7047bb128c5780e8777623
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 * 4. 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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/alias.c,v 1.18 2004/04/06 20:06:51 markm Exp $");
43 #include <stdlib.h>
44 #include "shell.h"
45 #include "input.h"
46 #include "output.h"
47 #include "error.h"
48 #include "memalloc.h"
49 #include "mystring.h"
50 #include "alias.h"
51 #include "options.h" /* XXX for argptr (should remove?) */
52 #include "builtins.h"
54 #define ATABSIZE 39
56 STATIC struct alias *atab[ATABSIZE];
58 STATIC void setalias(const char *, const char *);
59 STATIC int unalias(const char *);
60 STATIC struct alias **hashalias(const char *);
62 STATIC
63 void
64 setalias(const char *name, const char *val)
66 struct alias *ap, **app;
68 app = hashalias(name);
69 for (ap = *app; ap; ap = ap->next) {
70 if (equal(name, ap->name)) {
71 INTOFF;
72 ckfree(ap->val);
73 ap->val = savestr(val);
74 INTON;
75 return;
78 /* not found */
79 INTOFF;
80 ap = ckmalloc(sizeof (struct alias));
81 ap->name = savestr(name);
83 * XXX - HACK: in order that the parser will not finish reading the
84 * alias value off the input before processing the next alias, we
85 * dummy up an extra space at the end of the alias. This is a crock
86 * and should be re-thought. The idea (if you feel inclined to help)
87 * is to avoid alias recursions. The mechanism used is: when
88 * expanding an alias, the value of the alias is pushed back on the
89 * input as a string and a pointer to the alias is stored with the
90 * string. The alias is marked as being in use. When the input
91 * routine finishes reading the string, it marks the alias not
92 * in use. The problem is synchronization with the parser. Since
93 * it reads ahead, the alias is marked not in use before the
94 * resulting token(s) is next checked for further alias sub. The
95 * H A C K is that we add a little fluff after the alias value
96 * so that the string will not be exhausted. This is a good
97 * idea ------- ***NOT***
99 #ifdef notyet
100 ap->val = savestr(val);
101 #else /* hack */
103 int len = strlen(val);
104 ap->val = ckmalloc(len + 2);
105 memcpy(ap->val, val, len);
106 ap->val[len] = ' '; /* fluff */
107 ap->val[len+1] = '\0';
109 #endif
110 ap->flag = 0;
111 ap->next = *app;
112 *app = ap;
113 INTON;
116 STATIC int
117 unalias(const char *name)
119 struct alias *ap, **app;
121 app = hashalias(name);
123 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
124 if (equal(name, ap->name)) {
126 * if the alias is currently in use (i.e. its
127 * buffer is being used by the input routine) we
128 * just null out the name instead of freeing it.
129 * We could clear it out later, but this situation
130 * is so rare that it hardly seems worth it.
132 if (ap->flag & ALIASINUSE)
133 *ap->name = '\0';
134 else {
135 INTOFF;
136 *app = ap->next;
137 ckfree(ap->name);
138 ckfree(ap->val);
139 ckfree(ap);
140 INTON;
142 return (0);
146 return (1);
149 #ifdef mkinit
150 INCLUDE "alias.h"
151 SHELLPROC {
152 rmaliases();
154 #endif
156 void
157 rmaliases(void)
159 struct alias *ap, *tmp;
160 int i;
162 INTOFF;
163 for (i = 0; i < ATABSIZE; i++) {
164 ap = atab[i];
165 atab[i] = NULL;
166 while (ap) {
167 ckfree(ap->name);
168 ckfree(ap->val);
169 tmp = ap;
170 ap = ap->next;
171 ckfree(tmp);
174 INTON;
177 struct alias *
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))
185 return (NULL);
186 return (ap);
190 return (NULL);
194 * TODO - sort output
197 aliascmd(int argc, char **argv)
199 char *n, *v;
200 int ret = 0;
201 struct alias *ap;
203 if (argc == 1) {
204 int i;
206 for (i = 0; i < ATABSIZE; i++)
207 for (ap = atab[i]; ap; ap = ap->next) {
208 if (*ap->name != '\0') {
209 out1fmt("alias %s=", ap->name);
210 out1qstr(ap->val);
211 out1c('\n');
214 return (0);
216 while ((n = *++argv) != NULL) {
217 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
218 if ((ap = lookupalias(n, 0)) == NULL) {
219 outfmt(out2, "alias: %s not found\n", n);
220 ret = 1;
221 } else {
222 out1fmt("alias %s=", n);
223 out1qstr(ap->val);
224 out1c('\n');
226 else {
227 *v++ = '\0';
228 setalias(n, v);
232 return (ret);
236 unaliascmd(int argc __unused, char **argv __unused)
238 int i;
240 while ((i = nextopt("a")) != '\0') {
241 if (i == 'a') {
242 rmaliases();
243 return (0);
246 for (i = 0; *argptr; argptr++)
247 i = unalias(*argptr);
249 return (i);
252 STATIC struct alias **
253 hashalias(const char *p)
255 unsigned int hashval;
257 hashval = *p << 4;
258 while (*p)
259 hashval+= *p++;
260 return &atab[hashval % ATABSIZE];
264 * $PchId: alias.c,v 1.5 2006/05/22 12:41:12 philip Exp $