trace(1): resolve all level-5 LLVM warnings
[minix3.git] / bin / sh / alias.c
blob669a5de0f4acf93598a43c9ad2ca0db73239e556
1 /* $NetBSD: alias.c,v 1.15 2014/06/18 18:17:30 christos Exp $ */
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: alias.c,v 1.15 2014/06/18 18:17:30 christos Exp $");
41 #endif
42 #endif /* not lint */
44 #include <stdlib.h>
45 #include "shell.h"
46 #include "input.h"
47 #include "output.h"
48 #include "error.h"
49 #include "memalloc.h"
50 #include "mystring.h"
51 #include "alias.h"
52 #include "options.h" /* XXX for argptr (should remove?) */
53 #include "builtins.h"
54 #include "var.h"
56 #define ATABSIZE 39
58 struct alias *atab[ATABSIZE];
60 STATIC void setalias(char *, char *);
61 STATIC int unalias(char *);
62 STATIC struct alias **hashalias(const char *);
64 STATIC
65 void
66 setalias(char *name, char *val)
68 struct alias *ap, **app;
70 app = hashalias(name);
71 for (ap = *app; ap; ap = ap->next) {
72 if (equal(name, ap->name)) {
73 INTOFF;
74 ckfree(ap->val);
75 ap->val = savestr(val);
76 INTON;
77 return;
80 /* not found */
81 INTOFF;
82 ap = ckmalloc(sizeof (struct alias));
83 ap->name = savestr(name);
84 ap->flag = 0;
86 * XXX - HACK: in order that the parser will not finish reading the
87 * alias value off the input before processing the next alias, we
88 * dummy up an extra space at the end of the alias. This is a crock
89 * and should be re-thought. The idea (if you feel inclined to help)
90 * is to avoid alias recursions. The mechanism used is: when
91 * expanding an alias, the value of the alias is pushed back on the
92 * input as a string and a pointer to the alias is stored with the
93 * string. The alias is marked as being in use. When the input
94 * routine finishes reading the string, it markes the alias not
95 * in use. The problem is synchronization with the parser. Since
96 * it reads ahead, the alias is marked not in use before the
97 * resulting token(s) is next checked for further alias sub. The
98 * H A C K is that we add a little fluff after the alias value
99 * so that the string will not be exhausted. This is a good
100 * idea ------- ***NOT***
102 #ifdef notyet
103 ap->val = savestr(val);
104 #else /* hack */
106 int len = strlen(val);
107 ap->val = ckmalloc(len + 2);
108 memcpy(ap->val, val, len);
109 ap->val[len] = ' '; /* fluff */
110 ap->val[len+1] = '\0';
112 #endif
113 ap->next = *app;
114 *app = ap;
115 INTON;
118 STATIC int
119 unalias(char *name)
121 struct alias *ap, **app;
123 app = hashalias(name);
125 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
126 if (equal(name, ap->name)) {
128 * if the alias is currently in use (i.e. its
129 * buffer is being used by the input routine) we
130 * just null out the name instead of freeing it.
131 * We could clear it out later, but this situation
132 * is so rare that it hardly seems worth it.
134 if (ap->flag & ALIASINUSE)
135 *ap->name = '\0';
136 else {
137 INTOFF;
138 *app = ap->next;
139 ckfree(ap->name);
140 ckfree(ap->val);
141 ckfree(ap);
142 INTON;
144 return (0);
148 return (1);
151 #ifdef mkinit
152 MKINIT void rmaliases(void);
154 SHELLPROC {
155 rmaliases();
157 #endif
159 void
160 rmaliases(void)
162 struct alias *ap, *tmp;
163 int i;
165 INTOFF;
166 for (i = 0; i < ATABSIZE; i++) {
167 ap = atab[i];
168 atab[i] = NULL;
169 while (ap) {
170 ckfree(ap->name);
171 ckfree(ap->val);
172 tmp = ap;
173 ap = ap->next;
174 ckfree(tmp);
177 INTON;
180 struct alias *
181 lookupalias(const char *name, int check)
183 struct alias *ap = *hashalias(name);
185 for (; ap; ap = ap->next) {
186 if (equal(name, ap->name)) {
187 if (check && (ap->flag & ALIASINUSE))
188 return (NULL);
189 return (ap);
193 return (NULL);
196 const char *
197 alias_text(void *dummy __unused, const char *name)
199 struct alias *ap;
201 ap = lookupalias(name, 0);
202 if (ap == NULL)
203 return NULL;
204 return ap->val;
208 * TODO - sort output
211 aliascmd(int argc, char **argv)
213 char *n, *v;
214 int ret = 0;
215 struct alias *ap;
217 if (argc == 1) {
218 int i;
220 for (i = 0; i < ATABSIZE; i++)
221 for (ap = atab[i]; ap; ap = ap->next) {
222 if (*ap->name != '\0') {
223 out1fmt("alias %s=", ap->name);
224 print_quoted(ap->val);
225 out1c('\n');
228 return (0);
230 while ((n = *++argv) != NULL) {
231 if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
232 if ((ap = lookupalias(n, 0)) == NULL) {
233 outfmt(out2, "alias: %s not found\n", n);
234 ret = 1;
235 } else {
236 out1fmt("alias %s=", n);
237 print_quoted(ap->val);
238 out1c('\n');
240 } else {
241 *v++ = '\0';
242 setalias(n, v);
246 return (ret);
250 unaliascmd(int argc, char **argv)
252 int i;
254 while ((i = nextopt("a")) != '\0') {
255 if (i == 'a') {
256 rmaliases();
257 return (0);
260 for (i = 0; *argptr; argptr++)
261 i = unalias(*argptr);
263 return (i);
266 STATIC struct alias **
267 hashalias(const char *p)
269 unsigned int hashval;
271 hashval = *p << 4;
272 while (*p)
273 hashval+= *p++;
274 return &atab[hashval % ATABSIZE];