]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/alias.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / bin / sh / alias.c
1 /*-
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
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.
19  *
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.
31  */
32
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>
39 __FBSDID("$FreeBSD$");
40
41 #include <stdlib.h>
42 #include "shell.h"
43 #include "output.h"
44 #include "error.h"
45 #include "memalloc.h"
46 #include "mystring.h"
47 #include "alias.h"
48 #include "options.h"    /* XXX for argptr (should remove?) */
49 #include "builtins.h"
50
51 #define ATABSIZE 39
52
53 static struct alias *atab[ATABSIZE];
54 static int aliases;
55
56 static void setalias(const char *, const char *);
57 static int unalias(const char *);
58 static struct alias **hashalias(const char *);
59
60 static
61 void
62 setalias(const char *name, const char *val)
63 {
64         struct alias *ap, **app;
65
66         app = hashalias(name);
67         for (ap = *app; ap; ap = ap->next) {
68                 if (equal(name, ap->name)) {
69                         INTOFF;
70                         ckfree(ap->val);
71                         ap->val = savestr(val);
72                         INTON;
73                         return;
74                 }
75         }
76         /* not found */
77         INTOFF;
78         ap = ckmalloc(sizeof (struct alias));
79         ap->name = savestr(name);
80         ap->val = savestr(val);
81         ap->flag = 0;
82         ap->next = *app;
83         *app = ap;
84         aliases++;
85         INTON;
86 }
87
88 static int
89 unalias(const char *name)
90 {
91         struct alias *ap, **app;
92
93         app = hashalias(name);
94
95         for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
96                 if (equal(name, ap->name)) {
97                         /*
98                          * if the alias is currently in use (i.e. its
99                          * buffer is being used by the input routine) we
100                          * just null out the name instead of freeing it.
101                          * We could clear it out later, but this situation
102                          * is so rare that it hardly seems worth it.
103                          */
104                         if (ap->flag & ALIASINUSE)
105                                 *ap->name = '\0';
106                         else {
107                                 INTOFF;
108                                 *app = ap->next;
109                                 ckfree(ap->name);
110                                 ckfree(ap->val);
111                                 ckfree(ap);
112                                 INTON;
113                         }
114                         aliases--;
115                         return (0);
116                 }
117         }
118
119         return (1);
120 }
121
122 static void
123 rmaliases(void)
124 {
125         struct alias *ap, *tmp;
126         int i;
127
128         INTOFF;
129         for (i = 0; i < ATABSIZE; i++) {
130                 ap = atab[i];
131                 atab[i] = NULL;
132                 while (ap) {
133                         ckfree(ap->name);
134                         ckfree(ap->val);
135                         tmp = ap;
136                         ap = ap->next;
137                         ckfree(tmp);
138                 }
139         }
140         aliases = 0;
141         INTON;
142 }
143
144 struct alias *
145 lookupalias(const char *name, int check)
146 {
147         struct alias *ap;
148
149         if (aliases == 0)
150                 return (NULL);
151         for (ap = *hashalias(name); ap; ap = ap->next) {
152                 if (equal(name, ap->name)) {
153                         if (check && (ap->flag & ALIASINUSE))
154                                 return (NULL);
155                         return (ap);
156                 }
157         }
158
159         return (NULL);
160 }
161
162 static int
163 comparealiases(const void *p1, const void *p2)
164 {
165         const struct alias *const *a1 = p1;
166         const struct alias *const *a2 = p2;
167
168         return strcmp((*a1)->name, (*a2)->name);
169 }
170
171 static void
172 printalias(const struct alias *a)
173 {
174         out1fmt("%s=", a->name);
175         out1qstr(a->val);
176         out1c('\n');
177 }
178
179 static void
180 printaliases(void)
181 {
182         int i, j;
183         struct alias **sorted, *ap;
184
185         INTOFF;
186         sorted = ckmalloc(aliases * sizeof(*sorted));
187         j = 0;
188         for (i = 0; i < ATABSIZE; i++)
189                 for (ap = atab[i]; ap; ap = ap->next)
190                         if (*ap->name != '\0')
191                                 sorted[j++] = ap;
192         qsort(sorted, aliases, sizeof(*sorted), comparealiases);
193         for (i = 0; i < aliases; i++) {
194                 printalias(sorted[i]);
195                 if (int_pending())
196                         break;
197         }
198         ckfree(sorted);
199         INTON;
200 }
201
202 int
203 aliascmd(int argc __unused, char **argv __unused)
204 {
205         char *n, *v;
206         int ret = 0;
207         struct alias *ap;
208
209         nextopt("");
210
211         if (*argptr == NULL) {
212                 printaliases();
213                 return (0);
214         }
215         while ((n = *argptr++) != NULL) {
216                 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
217                         if ((ap = lookupalias(n, 0)) == NULL) {
218                                 warning("%s: not found", n);
219                                 ret = 1;
220                         } else
221                                 printalias(ap);
222                 else {
223                         *v++ = '\0';
224                         setalias(n, v);
225                 }
226         }
227
228         return (ret);
229 }
230
231 int
232 unaliascmd(int argc __unused, char **argv __unused)
233 {
234         int i;
235
236         while ((i = nextopt("a")) != '\0') {
237                 if (i == 'a') {
238                         rmaliases();
239                         return (0);
240                 }
241         }
242         for (i = 0; *argptr; argptr++)
243                 i |= unalias(*argptr);
244
245         return (i);
246 }
247
248 static struct alias **
249 hashalias(const char *p)
250 {
251         unsigned int hashval;
252
253         hashval = (unsigned char)*p << 4;
254         while (*p)
255                 hashval+= *p++;
256         return &atab[hashval % ATABSIZE];
257 }