160701Sbostic /*-
260701Sbostic * Copyright (c) 1993
360701Sbostic * The Regents of the University of California. All rights reserved.
460701Sbostic *
560701Sbostic * This code is derived from software contributed to Berkeley by
660701Sbostic * Kenneth Almquist.
760701Sbostic *
860701Sbostic * %sccs.include.redist.c%
960701Sbostic */
1060701Sbostic
1160701Sbostic #ifndef lint
12*69272Schristos static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 05/04/95";
1360701Sbostic #endif /* not lint */
1460701Sbostic
15*69272Schristos #include <stdlib.h>
1653651Smarc #include "shell.h"
1753651Smarc #include "input.h"
1853651Smarc #include "output.h"
1953651Smarc #include "error.h"
2053651Smarc #include "memalloc.h"
2153651Smarc #include "mystring.h"
2253651Smarc #include "alias.h"
2353651Smarc #include "options.h" /* XXX for argptr (should remove?) */
2453651Smarc
2553651Smarc #define ATABSIZE 39
2653651Smarc
2753651Smarc struct alias *atab[ATABSIZE];
2853651Smarc
29*69272Schristos STATIC void setalias __P((char *, char *));
30*69272Schristos STATIC int unalias __P((char *));
3153651Smarc STATIC struct alias **hashalias __P((char *));
3253651Smarc
3353651Smarc STATIC
34*69272Schristos void
setalias(name,val)3553651Smarc setalias(name, val)
3653651Smarc char *name, *val;
37*69272Schristos {
3853651Smarc struct alias *ap, **app;
3953651Smarc
4053651Smarc app = hashalias(name);
4153651Smarc for (ap = *app; ap; ap = ap->next) {
4253651Smarc if (equal(name, ap->name)) {
4353651Smarc INTOFF;
4453651Smarc ckfree(ap->val);
4553651Smarc ap->val = savestr(val);
4653651Smarc INTON;
4753651Smarc return;
4853651Smarc }
4953651Smarc }
5053651Smarc /* not found */
5153651Smarc INTOFF;
5253651Smarc ap = ckmalloc(sizeof (struct alias));
5353651Smarc ap->name = savestr(name);
5453651Smarc /*
5553651Smarc * XXX - HACK: in order that the parser will not finish reading the
5653651Smarc * alias value off the input before processing the next alias, we
5753651Smarc * dummy up an extra space at the end of the alias. This is a crock
5853651Smarc * and should be re-thought. The idea (if you feel inclined to help)
5953651Smarc * is to avoid alias recursions. The mechanism used is: when
6053651Smarc * expanding an alias, the value of the alias is pushed back on the
6153651Smarc * input as a string and a pointer to the alias is stored with the
6253651Smarc * string. The alias is marked as being in use. When the input
6353651Smarc * routine finishes reading the string, it markes the alias not
6453651Smarc * in use. The problem is synchronization with the parser. Since
6553651Smarc * it reads ahead, the alias is marked not in use before the
6653651Smarc * resulting token(s) is next checked for further alias sub. The
6753651Smarc * H A C K is that we add a little fluff after the alias value
6853651Smarc * so that the string will not be exhausted. This is a good
6953651Smarc * idea ------- ***NOT***
7053651Smarc */
7153651Smarc #ifdef notyet
7253651Smarc ap->val = savestr(val);
7353651Smarc #else /* hack */
7453651Smarc {
7553651Smarc int len = strlen(val);
7653651Smarc ap->val = ckmalloc(len + 2);
77*69272Schristos memcpy(ap->val, val, len);
7853651Smarc ap->val[len] = ' '; /* fluff */
7953651Smarc ap->val[len+1] = '\0';
8053651Smarc }
8153651Smarc #endif
8253651Smarc ap->next = *app;
8353651Smarc *app = ap;
8453651Smarc INTON;
8553651Smarc }
8653651Smarc
8753651Smarc STATIC int
unalias(name)8853651Smarc unalias(name)
8953651Smarc char *name;
9053651Smarc {
9153651Smarc struct alias *ap, **app;
9253651Smarc
9353651Smarc app = hashalias(name);
9453651Smarc
9553651Smarc for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
9653651Smarc if (equal(name, ap->name)) {
9753651Smarc /*
9853651Smarc * if the alias is currently in use (i.e. its
9953651Smarc * buffer is being used by the input routine) we
10053651Smarc * just null out the name instead of freeing it.
10153651Smarc * We could clear it out later, but this situation
10253651Smarc * is so rare that it hardly seems worth it.
10353651Smarc */
10453651Smarc if (ap->flag & ALIASINUSE)
10553651Smarc *ap->name = '\0';
10653651Smarc else {
10753651Smarc INTOFF;
10853651Smarc *app = ap->next;
10953651Smarc ckfree(ap->name);
11053651Smarc ckfree(ap->val);
11153651Smarc ckfree(ap);
11253651Smarc INTON;
11353651Smarc }
11453651Smarc return (0);
11553651Smarc }
11653651Smarc }
11753651Smarc
11853651Smarc return (1);
11953651Smarc }
12053651Smarc
12153651Smarc #ifdef mkinit
12253651Smarc MKINIT void rmaliases();
12353651Smarc
12453651Smarc SHELLPROC {
12553651Smarc rmaliases();
12653651Smarc }
12753651Smarc #endif
12853651Smarc
12953651Smarc void
rmaliases()13053651Smarc rmaliases() {
13153651Smarc struct alias *ap, *tmp;
13253651Smarc int i;
13353651Smarc
13453651Smarc INTOFF;
13553651Smarc for (i = 0; i < ATABSIZE; i++) {
13653651Smarc ap = atab[i];
13753651Smarc atab[i] = NULL;
13853651Smarc while (ap) {
13953651Smarc ckfree(ap->name);
14053651Smarc ckfree(ap->val);
14153651Smarc tmp = ap;
14253651Smarc ap = ap->next;
14353651Smarc ckfree(tmp);
14453651Smarc }
14553651Smarc }
14653651Smarc INTON;
14753651Smarc }
14853651Smarc
14953651Smarc struct alias *
lookupalias(name,check)15053651Smarc lookupalias(name, check)
15153651Smarc char *name;
152*69272Schristos int check;
153*69272Schristos {
15453651Smarc struct alias *ap = *hashalias(name);
15553651Smarc
15653651Smarc for (; ap; ap = ap->next) {
15753651Smarc if (equal(name, ap->name)) {
15853651Smarc if (check && (ap->flag & ALIASINUSE))
15953651Smarc return (NULL);
16053651Smarc return (ap);
16153651Smarc }
16253651Smarc }
16353651Smarc
16453651Smarc return (NULL);
16553651Smarc }
16653651Smarc
16753651Smarc /*
16853651Smarc * TODO - sort output
16953651Smarc */
170*69272Schristos int
aliascmd(argc,argv)17153651Smarc aliascmd(argc, argv)
172*69272Schristos int argc;
17353651Smarc char **argv;
174*69272Schristos {
17553651Smarc char *n, *v;
17653651Smarc int ret = 0;
17753651Smarc struct alias *ap;
17853651Smarc
17953651Smarc if (argc == 1) {
18053651Smarc int i;
18153651Smarc
18253651Smarc for (i = 0; i < ATABSIZE; i++)
18353651Smarc for (ap = atab[i]; ap; ap = ap->next) {
18453651Smarc if (*ap->name != '\0')
18553651Smarc out1fmt("alias %s=%s\n", ap->name, ap->val);
18653651Smarc }
18753651Smarc return (0);
18853651Smarc }
189*69272Schristos while ((n = *++argv) != NULL) {
19053651Smarc if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
19153651Smarc if ((ap = lookupalias(n, 0)) == NULL) {
19253651Smarc outfmt(out2, "alias: %s not found\n", n);
19353651Smarc ret = 1;
19453651Smarc } else
19553651Smarc out1fmt("alias %s=%s\n", n, ap->val);
19653651Smarc else {
19753651Smarc *v++ = '\0';
19853651Smarc setalias(n, v);
19953651Smarc }
20053651Smarc }
20153651Smarc
20253651Smarc return (ret);
20353651Smarc }
20453651Smarc
205*69272Schristos int
unaliascmd(argc,argv)20653651Smarc unaliascmd(argc, argv)
207*69272Schristos int argc;
20853651Smarc char **argv;
209*69272Schristos {
21053651Smarc int i;
21153651Smarc
21253651Smarc while ((i = nextopt("a")) != '\0') {
21353651Smarc if (i == 'a') {
21453651Smarc rmaliases();
21553651Smarc return (0);
21653651Smarc }
21753651Smarc }
21853651Smarc for (i = 0; *argptr; argptr++)
21953651Smarc i = unalias(*argptr);
22053651Smarc
22153651Smarc return (i);
22253651Smarc }
22353651Smarc
22453651Smarc STATIC struct alias **
hashalias(p)22553651Smarc hashalias(p)
22653651Smarc register char *p;
22753651Smarc {
22853651Smarc unsigned int hashval;
22953651Smarc
23053651Smarc hashval = *p << 4;
23153651Smarc while (*p)
23253651Smarc hashval+= *p++;
23353651Smarc return &atab[hashval % ATABSIZE];
23453651Smarc }
235