~ubuntu-branches/ubuntu/hardy/klibc/hardy-updates

« back to all changes in this revision

Viewing changes to ash/alias.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2006-01-04 20:24:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104202452-ec4v3n829rymukuv
Tags: 1.1.15-0ubuntu1
* New upstream version.

* Patch to fix compilation on parisc64 kernels.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifndef __KLIBC__
36
 
#include <sys/cdefs.h>
37
 
#endif
38
 
#ifndef __RCSID
39
 
#define __RCSID(arg)
40
 
#endif
41
 
#ifndef lint
42
 
#if 0
43
 
static char sccsid[] = "@(#)alias.c     8.3 (Berkeley) 5/4/95";
44
 
#else
45
 
__RCSID("$NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $");
46
 
#endif
47
 
#endif /* not lint */
48
 
 
49
 
#include <stdlib.h>
50
 
#include "shell.h"
51
 
#include "input.h"
52
 
#include "output.h"
53
 
#include "error.h"
54
 
#include "memalloc.h"
55
 
#include "mystring.h"
56
 
#include "alias.h"
57
 
#include "options.h"    /* XXX for argptr (should remove?) */
58
 
#include "var.h"
59
 
 
60
 
#define ATABSIZE 39
61
 
 
62
 
struct alias *atab[ATABSIZE];
63
 
 
64
 
STATIC void setalias(char *, char *);
65
 
STATIC int unalias(char *);
66
 
STATIC struct alias **hashalias(char *);
67
 
 
68
 
STATIC
69
 
void
70
 
setalias(char *name, char *val)
71
 
{
72
 
        struct alias *ap, **app;
73
 
 
74
 
        app = hashalias(name);
75
 
        for (ap = *app; ap; ap = ap->next) {
76
 
                if (equal(name, ap->name)) {
77
 
                        INTOFF;
78
 
                        ckfree(ap->val);
79
 
                        ap->val = savestr(val);
80
 
                        INTON;
81
 
                        return;
82
 
                }
83
 
        }
84
 
        /* not found */
85
 
        INTOFF;
86
 
        ap = ckmalloc(sizeof (struct alias));
87
 
        ap->name = savestr(name);
88
 
        /*
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***
104
 
         */
105
 
#ifdef notyet
106
 
        ap->val = savestr(val);
107
 
#else /* hack */
108
 
        {
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';
114
 
        }
115
 
#endif
116
 
        ap->next = *app;
117
 
        *app = ap;
118
 
        INTON;
119
 
}
120
 
 
121
 
STATIC int
122
 
unalias(char *name)
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
 
                        }
147
 
                        return (0);
148
 
                }
149
 
        }
150
 
 
151
 
        return (1);
152
 
}
153
 
 
154
 
#ifdef mkinit
155
 
MKINIT void rmaliases(void);
156
 
 
157
 
SHELLPROC {
158
 
        rmaliases();
159
 
}
160
 
#endif
161
 
 
162
 
void
163
 
rmaliases(void)
164
 
{
165
 
        struct alias *ap, *tmp;
166
 
        int i;
167
 
 
168
 
        INTOFF;
169
 
        for (i = 0; i < ATABSIZE; i++) {
170
 
                ap = atab[i];
171
 
                atab[i] = NULL;
172
 
                while (ap) {
173
 
                        ckfree(ap->name);
174
 
                        ckfree(ap->val);
175
 
                        tmp = ap;
176
 
                        ap = ap->next;
177
 
                        ckfree(tmp);
178
 
                }
179
 
        }
180
 
        INTON;
181
 
}
182
 
 
183
 
struct alias *
184
 
lookupalias(char *name, int check)
185
 
{
186
 
        struct alias *ap = *hashalias(name);
187
 
 
188
 
        for (; ap; ap = ap->next) {
189
 
                if (equal(name, ap->name)) {
190
 
                        if (check && (ap->flag & ALIASINUSE))
191
 
                                return (NULL);
192
 
                        return (ap);
193
 
                }
194
 
        }
195
 
 
196
 
        return (NULL);
197
 
}
198
 
 
199
 
char *
200
 
get_alias_text(char *name)
201
 
{
202
 
        struct alias *ap;
203
 
 
204
 
        ap = lookupalias(name, 0);
205
 
        if (ap == NULL)
206
 
                return NULL;
207
 
        return ap->val;
208
 
}
209
 
 
210
 
/*
211
 
 * TODO - sort output
212
 
 */
213
 
int
214
 
aliascmd(int argc, char **argv)
215
 
{
216
 
        char *n, *v;
217
 
        int ret = 0;
218
 
        struct alias *ap;
219
 
 
220
 
        if (argc == 1) {
221
 
                int i;
222
 
 
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);
228
 
                                        out1c('\n');
229
 
                                }
230
 
                        }
231
 
                return (0);
232
 
        }
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);
237
 
                                ret = 1;
238
 
                        } else {
239
 
                                out1fmt("alias %s=", n);
240
 
                                print_quoted(ap->val);
241
 
                                out1c('\n');
242
 
                        }
243
 
                } else {
244
 
                        *v++ = '\0';
245
 
                        setalias(n, v);
246
 
                }
247
 
        }
248
 
 
249
 
        return (ret);
250
 
}
251
 
 
252
 
int
253
 
unaliascmd(int argc, char **argv)
254
 
{
255
 
        int i;
256
 
 
257
 
        while ((i = nextopt("a")) != '\0') {
258
 
                if (i == 'a') {
259
 
                        rmaliases();
260
 
                        return (0);
261
 
                }
262
 
        }
263
 
        for (i = 0; *argptr; argptr++)
264
 
                i = unalias(*argptr);
265
 
 
266
 
        return (i);
267
 
}
268
 
 
269
 
STATIC struct alias **
270
 
hashalias(char *p)
271
 
{
272
 
        unsigned int hashval;
273
 
 
274
 
        hashval = *p << 4;
275
 
        while (*p)
276
 
                hashval+= *p++;
277
 
        return &atab[hashval % ATABSIZE];
278
 
}