Merge branch 'vendor/OPENSSL'
[dragonfly.git] / bin / sh / alias.c
CommitLineData
984263bc
MD
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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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.
1de703da
MD
31 *
32 * @(#)alias.c 8.3 (Berkeley) 5/4/95
f00eae14 33 * $FreeBSD: head/bin/sh/alias.c 242766 2012-11-08 13:33:48Z jilles $
984263bc
MD
34 */
35
984263bc
MD
36#include <stdlib.h>
37#include "shell.h"
984263bc
MD
38#include "output.h"
39#include "error.h"
40#include "memalloc.h"
41#include "mystring.h"
42#include "alias.h"
43#include "options.h" /* XXX for argptr (should remove?) */
c3e3bc7a 44#include "builtins.h"
984263bc
MD
45
46#define ATABSIZE 39
47
99512ac4
PA
48static struct alias *atab[ATABSIZE];
49static int aliases;
984263bc 50
99512ac4
PA
51static void setalias(const char *, const char *);
52static int unalias(const char *);
53static struct alias **hashalias(const char *);
984263bc 54
99512ac4 55static
984263bc 56void
99512ac4 57setalias(const char *name, const char *val)
984263bc
MD
58{
59 struct alias *ap, **app;
60
61 app = hashalias(name);
62 for (ap = *app; ap; ap = ap->next) {
63 if (equal(name, ap->name)) {
64 INTOFF;
65 ckfree(ap->val);
f00eae14
PA
66 /* See HACK below. */
67#ifdef notyet
984263bc 68 ap->val = savestr(val);
f00eae14
PA
69#else
70 {
71 size_t len = strlen(val);
72 ap->val = ckmalloc(len + 2);
73 memcpy(ap->val, val, len);
74 ap->val[len] = ' ';
75 ap->val[len+1] = '\0';
76 }
77#endif
984263bc
MD
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 marks 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 {
99512ac4 107 size_t len = strlen(val);
984263bc
MD
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->flag = 0;
115 ap->next = *app;
116 *app = ap;
99512ac4 117 aliases++;
984263bc
MD
118 INTON;
119}
120
99512ac4
PA
121static int
122unalias(const char *name)
984263bc
MD
123{
124 struct alias *ap, **app;
125
126 app = hashalias(name);
127
128 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
129 if (equal(name, ap->name)) {
130 /*
131 * if the alias is currently in use (i.e. its
132 * buffer is being used by the input routine) we
133 * just null out the name instead of freeing it.
134 * We could clear it out later, but this situation
135 * is so rare that it hardly seems worth it.
136 */
137 if (ap->flag & ALIASINUSE)
138 *ap->name = '\0';
139 else {
140 INTOFF;
141 *app = ap->next;
142 ckfree(ap->name);
143 ckfree(ap->val);
144 ckfree(ap);
145 INTON;
146 }
99512ac4 147 aliases--;
984263bc
MD
148 return (0);
149 }
150 }
151
152 return (1);
153}
154
99512ac4 155static void
984263bc
MD
156rmaliases(void)
157{
158 struct alias *ap, *tmp;
159 int i;
160
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);
171 }
172 }
99512ac4 173 aliases = 0;
984263bc
MD
174 INTON;
175}
176
177struct alias *
99512ac4 178lookupalias(const char *name, int check)
984263bc
MD
179{
180 struct alias *ap = *hashalias(name);
181
182 for (; ap; ap = ap->next) {
183 if (equal(name, ap->name)) {
184 if (check && (ap->flag & ALIASINUSE))
185 return (NULL);
186 return (ap);
187 }
188 }
189
190 return (NULL);
191}
192
99512ac4
PA
193static int
194comparealiases(const void *p1, const void *p2)
195{
196 const struct alias *const *a1 = p1;
197 const struct alias *const *a2 = p2;
198
199 return strcmp((*a1)->name, (*a2)->name);
200}
201
202static void
203printalias(const struct alias *a)
204{
205 char *p;
206
207 out1fmt("%s=", a->name);
208 /* Don't print the space added above. */
209 p = a->val + strlen(a->val) - 1;
210 *p = '\0';
211 out1qstr(a->val);
212 *p = ' ';
213 out1c('\n');
214}
215
216static void
217printaliases(void)
218{
219 int i, j;
220 struct alias **sorted, *ap;
221
222 sorted = ckmalloc(aliases * sizeof(*sorted));
223 j = 0;
224 for (i = 0; i < ATABSIZE; i++)
225 for (ap = atab[i]; ap; ap = ap->next)
226 if (*ap->name != '\0')
227 sorted[j++] = ap;
228 qsort(sorted, aliases, sizeof(*sorted), comparealiases);
229 for (i = 0; i < aliases; i++)
230 printalias(sorted[i]);
231 ckfree(sorted);
232}
233
984263bc
MD
234int
235aliascmd(int argc, char **argv)
236{
237 char *n, *v;
238 int ret = 0;
239 struct alias *ap;
240
241 if (argc == 1) {
99512ac4 242 printaliases();
984263bc
MD
243 return (0);
244 }
245 while ((n = *++argv) != NULL) {
246 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
247 if ((ap = lookupalias(n, 0)) == NULL) {
f893ef22 248 warning("%s: not found", n);
984263bc 249 ret = 1;
99512ac4
PA
250 } else
251 printalias(ap);
984263bc
MD
252 else {
253 *v++ = '\0';
254 setalias(n, v);
255 }
256 }
257
258 return (ret);
259}
260
261int
262unaliascmd(int argc __unused, char **argv __unused)
263{
264 int i;
265
266 while ((i = nextopt("a")) != '\0') {
267 if (i == 'a') {
268 rmaliases();
269 return (0);
270 }
271 }
272 for (i = 0; *argptr; argptr++)
7085f7b3 273 i |= unalias(*argptr);
984263bc
MD
274
275 return (i);
276}
277
99512ac4
PA
278static struct alias **
279hashalias(const char *p)
984263bc
MD
280{
281 unsigned int hashval;
282
283 hashval = *p << 4;
284 while (*p)
285 hashval+= *p++;
286 return &atab[hashval % ATABSIZE];
287}