unstack, sort: cleanup and improvement
[minix.git] / commands / ash / alias.c
blobe584ebcf3ab1e4b76698837e75d6b27c1e14404f
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 */
38 #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?) */
53 #define ATABSIZE 39
55 STATIC struct alias *atab[ATABSIZE];
57 STATIC void setalias(const char *, const char *);
58 STATIC int unalias(const char *);
59 STATIC struct alias **hashalias(const char *);
61 STATIC
62 void
63 setalias(const char *name, const char *val)
65 struct alias *ap, **app;
67 app = hashalias(name);
68 for (ap = *app; ap; ap = ap->next) {
69 if (equal(name, ap->name)) {
70 INTOFF;
71 ckfree(ap->val);
72 ap->val = savestr(val);
73 INTON;
74 return;
77 /* not found */
78 INTOFF;
79 ap = ckmalloc(sizeof (struct alias));
80 ap->name = savestr(name);
82 * XXX - HACK: in order that the parser will not finish reading the
83 * alias value off the input before processing the next alias, we
84 * dummy up an extra space at the end of the alias. This is a crock
85 * and should be re-thought. The idea (if you feel inclined to help)
86 * is to avoid alias recursions. The mechanism used is: when
87 * expanding an alias, the value of the alias is pushed back on the
88 * input as a string and a pointer to the alias is stored with the
89 * string. The alias is marked as being in use. When the input
90 * routine finishes reading the string, it marks the alias not
91 * in use. The problem is synchronization with the parser. Since
92 * it reads ahead, the alias is marked not in use before the
93 * resulting token(s) is next checked for further alias sub. The
94 * H A C K is that we add a little fluff after the alias value
95 * so that the string will not be exhausted. This is a good
96 * idea ------- ***NOT***
98 #ifdef notyet
99 ap->val = savestr(val);
100 #else /* hack */
102 int len = strlen(val);
103 ap->val = ckmalloc(len + 2);
104 memcpy(ap->val, val, len);
105 ap->val[len] = ' '; /* fluff */
106 ap->val[len+1] = '\0';
108 #endif
109 ap->flag = 0;
110 ap->next = *app;
111 *app = ap;
112 INTON;
115 STATIC int
116 unalias(const char *name)
118 struct alias *ap, **app;
120 app = hashalias(name);
122 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
123 if (equal(name, ap->name)) {
125 * if the alias is currently in use (i.e. its
126 * buffer is being used by the input routine) we
127 * just null out the name instead of freeing it.
128 * We could clear it out later, but this situation
129 * is so rare that it hardly seems worth it.
131 if (ap->flag & ALIASINUSE)
132 *ap->name = '\0';
133 else {
134 INTOFF;
135 *app = ap->next;
136 ckfree(ap->name);
137 ckfree(ap->val);
138 ckfree(ap);
139 INTON;
141 return (0);
145 return (1);
148 #ifdef mkinit
149 INCLUDE "alias.h"
150 SHELLPROC {
151 rmaliases();
153 #endif
155 void
156 rmaliases(void)
158 struct alias *ap, *tmp;
159 int i;
161 INTOFF;
162 for (i = 0; i < ATABSIZE; i++) {
163 ap = atab[i];
164 atab[i] = NULL;
165 while (ap) {
166 ckfree(ap->name);
167 ckfree(ap->val);
168 tmp = ap;
169 ap = ap->next;
170 ckfree(tmp);
173 INTON;
176 struct alias *
177 lookupalias(const char *name, int check)
179 struct alias *ap = *hashalias(name);
181 for (; ap; ap = ap->next) {
182 if (equal(name, ap->name)) {
183 if (check && (ap->flag & ALIASINUSE))
184 return (NULL);
185 return (ap);
189 return (NULL);
193 * TODO - sort output
196 aliascmd(int argc, char **argv)
198 char *n, *v;
199 int ret = 0;
200 struct alias *ap;
202 if (argc == 1) {
203 int i;
205 for (i = 0; i < ATABSIZE; i++)
206 for (ap = atab[i]; ap; ap = ap->next) {
207 if (*ap->name != '\0') {
208 out1fmt("alias %s=", ap->name);
209 out1qstr(ap->val);
210 out1c('\n');
213 return (0);
215 while ((n = *++argv) != NULL) {
216 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
217 if ((ap = lookupalias(n, 0)) == NULL) {
218 outfmt(out2, "alias: %s not found\n", n);
219 ret = 1;
220 } else {
221 out1fmt("alias %s=", n);
222 out1qstr(ap->val);
223 out1c('\n');
225 else {
226 *v++ = '\0';
227 setalias(n, v);
231 return (ret);
235 unaliascmd(int argc __unused, char **argv __unused)
237 int i;
239 while ((i = nextopt("a")) != '\0') {
240 if (i == 'a') {
241 rmaliases();
242 return (0);
245 for (i = 0; *argptr; argptr++)
246 i = unalias(*argptr);
248 return (i);
251 STATIC struct alias **
252 hashalias(const char *p)
254 unsigned int hashval;
256 hashval = *p << 4;
257 while (*p)
258 hashval+= *p++;
259 return &atab[hashval % ATABSIZE];
263 * $PchId: alias.c,v 1.5 2006/05/22 12:41:12 philip Exp $