1
/* $NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $ */
5
* The Regents of the University of California. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
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
36
#include <sys/cdefs.h>
43
static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
45
__RCSID("$NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $");
57
#include "options.h" /* XXX for argptr (should remove?) */
62
struct alias *atab[ATABSIZE];
64
STATIC void setalias(char *, char *);
65
STATIC int unalias(char *);
66
STATIC struct alias **hashalias(char *);
70
setalias(char *name, char *val)
72
struct alias *ap, **app;
74
app = hashalias(name);
75
for (ap = *app; ap; ap = ap->next) {
76
if (equal(name, ap->name)) {
79
ap->val = savestr(val);
86
ap = ckmalloc(sizeof (struct alias));
87
ap->name = savestr(name);
89
* XXX - HACK: in order that the parser will not finish reading the
90
* alias value off the input before processing the next alias, we
91
* dummy up an extra space at the end of the alias. This is a crock
92
* and should be re-thought. The idea (if you feel inclined to help)
93
* is to avoid alias recursions. The mechanism used is: when
94
* expanding an alias, the value of the alias is pushed back on the
95
* input as a string and a pointer to the alias is stored with the
96
* string. The alias is marked as being in use. When the input
97
* routine finishes reading the string, it markes the alias not
98
* in use. The problem is synchronization with the parser. Since
99
* it reads ahead, the alias is marked not in use before the
100
* resulting token(s) is next checked for further alias sub. The
101
* H A C K is that we add a little fluff after the alias value
102
* so that the string will not be exhausted. This is a good
103
* idea ------- ***NOT***
106
ap->val = savestr(val);
109
int len = strlen(val);
110
ap->val = ckmalloc(len + 2);
111
memcpy(ap->val, val, len);
112
ap->val[len] = ' '; /* fluff */
113
ap->val[len+1] = '\0';
124
struct alias *ap, **app;
126
app = hashalias(name);
128
for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
129
if (equal(name, ap->name)) {
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.
137
if (ap->flag & ALIASINUSE)
155
MKINIT void rmaliases(void);
165
struct alias *ap, *tmp;
169
for (i = 0; i < ATABSIZE; i++) {
184
lookupalias(char *name, int check)
186
struct alias *ap = *hashalias(name);
188
for (; ap; ap = ap->next) {
189
if (equal(name, ap->name)) {
190
if (check && (ap->flag & ALIASINUSE))
200
get_alias_text(char *name)
204
ap = lookupalias(name, 0);
214
aliascmd(int argc, char **argv)
223
for (i = 0; i < ATABSIZE; i++)
224
for (ap = atab[i]; ap; ap = ap->next) {
225
if (*ap->name != '\0') {
226
out1fmt("alias %s=", ap->name);
227
print_quoted(ap->val);
233
while ((n = *++argv) != NULL) {
234
if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
235
if ((ap = lookupalias(n, 0)) == NULL) {
236
outfmt(out2, "alias: %s not found\n", n);
239
out1fmt("alias %s=", n);
240
print_quoted(ap->val);
253
unaliascmd(int argc, char **argv)
257
while ((i = nextopt("a")) != '\0') {
263
for (i = 0; *argptr; argptr++)
264
i = unalias(*argptr);
269
STATIC struct alias **
272
unsigned int hashval;
277
return &atab[hashval % ATABSIZE];