VirtualBox

source: kBuild/trunk/src/ash-messup/alias.c@ 882

Last change on this file since 882 was 882, checked in by bird, 18 years ago

hacking...

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1/* $NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $ */
2
3/*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $");
43#endif
44#endif /* not lint */
45
46#include <stdlib.h>
47#include "shell.h"
48#include "input.h"
49#include "output.h"
50#include "error.h"
51#include "memalloc.h"
52#include "mystring.h"
53#include "alias.h"
54#include "options.h" /* XXX for argptr (should remove?) */
55#include "var.h"
56#include "shinstance.h"
57
58/*#define ATABSIZE 39
59
60struct alias *atab[ATABSIZE];*/
61
62STATIC void setalias(shinstance *, char *, char *);
63STATIC int unalias(shinstance *, char *);
64STATIC struct alias **hashalias(shinstance *, char *);
65
66STATIC
67void
68setalias(shinstance *psh, char *name, char *val)
69{
70 struct alias *ap, **app;
71
72 app = hashalias(psh, name);
73 for (ap = *app; ap; ap = ap->next) {
74 if (equal(name, ap->name)) {
75 INTOFF;
76 ckfree(ap->val);
77 ap->val = savestr(val);
78 INTON;
79 return;
80 }
81 }
82 /* not found */
83 INTOFF;
84 ap = ckmalloc(sizeof (struct alias));
85 ap->name = savestr(name);
86 /*
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 markes 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***
102 */
103#ifdef notyet
104 ap->val = savestr(val);
105#else /* hack */
106 {
107 int 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';
112 }
113#endif
114 ap->next = *app;
115 *app = ap;
116 INTON;
117}
118
119STATIC int
120unalias(shinstance *psh, char *name)
121{
122 struct alias *ap, **app;
123
124 app = hashalias(psh, name);
125
126 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
127 if (equal(name, ap->name)) {
128 /*
129 * if the alias is currently in use (i.e. its
130 * buffer is being used by the input routine) we
131 * just null out the name instead of freeing it.
132 * We could clear it out later, but this situation
133 * is so rare that it hardly seems worth it.
134 */
135 if (ap->flag & ALIASINUSE)
136 *ap->name = '\0';
137 else {
138 INTOFF;
139 *app = ap->next;
140 ckfree(ap->name);
141 ckfree(ap->val);
142 ckfree(ap);
143 INTON;
144 }
145 return (0);
146 }
147 }
148
149 return (1);
150}
151
152#ifdef mkinit
153MKINIT void rmaliases(shinstance *psh);
154
155SHELLPROC {
156 rmaliases(psh);
157}
158#endif
159
160void
161rmaliases(shinstance *psh)
162{
163 struct alias *ap, *tmp;
164 int i;
165
166 INTOFF;
167 for (i = 0; i < ATABSIZE; i++) {
168 ap = psh->atab[i];
169 psh->atab[i] = NULL;
170 while (ap) {
171 ckfree(ap->name);
172 ckfree(ap->val);
173 tmp = ap;
174 ap = ap->next;
175 ckfree(tmp);
176 }
177 }
178 INTON;
179}
180
181struct alias *
182lookupalias(shinstance *psh, char *name, int check)
183{
184 struct alias *ap = *hashalias(psh, name);
185
186 for (; ap; ap = ap->next) {
187 if (equal(name, ap->name)) {
188 if (check && (ap->flag & ALIASINUSE))
189 return (NULL);
190 return (ap);
191 }
192 }
193
194 return (NULL);
195}
196
197char *
198get_alias_text(shinstance *psh, char *name)
199{
200 struct alias *ap;
201
202 ap = lookupalias(psh, name, 0);
203 if (ap == NULL)
204 return NULL;
205 return ap->val;
206}
207
208/*
209 * TODO - sort output
210 */
211int
212aliascmd(shinstance *psh, int argc, char **argv)
213{
214 char *n, *v;
215 int ret = 0;
216 struct alias *ap;
217
218 if (argc == 1) {
219 int i;
220
221 for (i = 0; i < ATABSIZE; i++)
222 for (ap = psh->atab[i]; ap; ap = ap->next) {
223 if (*ap->name != '\0') {
224 out1fmt(psh, "alias %s=", ap->name);
225 print_quoted(psh, ap->val);
226 out1c(psh, '\n');
227 }
228 }
229 return (0);
230 }
231 while ((n = *++argv) != NULL) {
232 if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
233 if ((ap = lookupalias(psh, n, 0)) == NULL) {
234 outfmt(psh->out2, "alias: %s not found\n", n);
235 ret = 1;
236 } else {
237 out1fmt(psh, "alias %s=", n);
238 print_quoted(psh, ap->val);
239 out1c(psh, '\n');
240 }
241 } else {
242 *v++ = '\0';
243 setalias(psh, n, v);
244 }
245 }
246
247 return (ret);
248}
249
250int
251unaliascmd(shinstance *psh, int argc, char **argv)
252{
253 int i;
254
255 while ((i = nextopt(psh, "a")) != '\0') {
256 if (i == 'a') {
257 rmaliases(psh);
258 return (0);
259 }
260 }
261 for (i = 0; *psh->argptr; psh->argptr++)
262 i = unalias(psh, *psh->argptr);
263
264 return (i);
265}
266
267STATIC struct alias **
268hashalias(shinstance *psh, char *p)
269{
270 unsigned int hashval;
271
272 hashval = *p << 4;
273 while (*p)
274 hashval+= *p++;
275 return &psh->atab[hashval % ATABSIZE];
276}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette