Home | History | Annotate | Line # | Download | only in sh
alias.c revision 1.10
      1 /*	$NetBSD: alias.c,v 1.10 1998/05/20 00:27:56 christos 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: alias.c,v 1.10 1998/05/20 00:27:56 christos Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <stdlib.h>
     49 #include "shell.h"
     50 #include "input.h"
     51 #include "output.h"
     52 #include "error.h"
     53 #include "memalloc.h"
     54 #include "mystring.h"
     55 #include "alias.h"
     56 #include "options.h"	/* XXX for argptr (should remove?) */
     57 
     58 #define ATABSIZE 39
     59 
     60 struct alias *atab[ATABSIZE];
     61 
     62 STATIC void setalias __P((char *, char *));
     63 STATIC int unalias __P((char *));
     64 STATIC struct alias **hashalias __P((char *));
     65 
     66 STATIC
     67 void
     68 setalias(name, val)
     69 	char *name, *val;
     70 {
     71 	struct alias *ap, **app;
     72 
     73 	app = hashalias(name);
     74 	for (ap = *app; ap; ap = ap->next) {
     75 		if (equal(name, ap->name)) {
     76 			INTOFF;
     77 			ckfree(ap->val);
     78 			ap->val	= savestr(val);
     79 			INTON;
     80 			return;
     81 		}
     82 	}
     83 	/* not found */
     84 	INTOFF;
     85 	ap = ckmalloc(sizeof (struct alias));
     86 	ap->name = savestr(name);
     87 	/*
     88 	 * XXX - HACK: in order that the parser will not finish reading the
     89 	 * alias value off the input before processing the next alias, we
     90 	 * dummy up an extra space at the end of the alias.  This is a crock
     91 	 * and should be re-thought.  The idea (if you feel inclined to help)
     92 	 * is to avoid alias recursions.  The mechanism used is: when
     93 	 * expanding an alias, the value of the alias is pushed back on the
     94 	 * input as a string and a pointer to the alias is stored with the
     95 	 * string.  The alias is marked as being in use.  When the input
     96 	 * routine finishes reading the string, it markes the alias not
     97 	 * in use.  The problem is synchronization with the parser.  Since
     98 	 * it reads ahead, the alias is marked not in use before the
     99 	 * resulting token(s) is next checked for further alias sub.  The
    100 	 * H A C K is that we add a little fluff after the alias value
    101 	 * so that the string will not be exhausted.  This is a good
    102 	 * idea ------- ***NOT***
    103 	 */
    104 #ifdef notyet
    105 	ap->val = savestr(val);
    106 #else /* hack */
    107 	{
    108 	int len = strlen(val);
    109 	ap->val = ckmalloc(len + 2);
    110 	memcpy(ap->val, val, len);
    111 	ap->val[len] = ' ';	/* fluff */
    112 	ap->val[len+1] = '\0';
    113 	}
    114 #endif
    115 	ap->next = *app;
    116 	*app = ap;
    117 	INTON;
    118 }
    119 
    120 STATIC int
    121 unalias(name)
    122 	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 __P((void));
    156 
    157 SHELLPROC {
    158 	rmaliases();
    159 }
    160 #endif
    161 
    162 void
    163 rmaliases() {
    164 	struct alias *ap, *tmp;
    165 	int i;
    166 
    167 	INTOFF;
    168 	for (i = 0; i < ATABSIZE; i++) {
    169 		ap = atab[i];
    170 		atab[i] = NULL;
    171 		while (ap) {
    172 			ckfree(ap->name);
    173 			ckfree(ap->val);
    174 			tmp = ap;
    175 			ap = ap->next;
    176 			ckfree(tmp);
    177 		}
    178 	}
    179 	INTON;
    180 }
    181 
    182 struct alias *
    183 lookupalias(name, check)
    184 	char *name;
    185 	int check;
    186 {
    187 	struct alias *ap = *hashalias(name);
    188 
    189 	for (; ap; ap = ap->next) {
    190 		if (equal(name, ap->name)) {
    191 			if (check && (ap->flag & ALIASINUSE))
    192 				return (NULL);
    193 			return (ap);
    194 		}
    195 	}
    196 
    197 	return (NULL);
    198 }
    199 
    200 /*
    201  * TODO - sort output
    202  */
    203 int
    204 aliascmd(argc, argv)
    205 	int argc;
    206 	char **argv;
    207 {
    208 	char *n, *v;
    209 	int ret = 0;
    210 	struct alias *ap;
    211 
    212 	if (argc == 1) {
    213 		int i;
    214 
    215 		for (i = 0; i < ATABSIZE; i++)
    216 			for (ap = atab[i]; ap; ap = ap->next) {
    217 				if (*ap->name != '\0')
    218 				    out1fmt("alias %s=%s\n", ap->name, ap->val);
    219 			}
    220 		return (0);
    221 	}
    222 	while ((n = *++argv) != NULL) {
    223 		if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
    224 			if ((ap = lookupalias(n, 0)) == NULL) {
    225 				outfmt(out2, "alias: %s not found\n", n);
    226 				ret = 1;
    227 			} else
    228 				out1fmt("alias %s=%s\n", n, ap->val);
    229 		}
    230 		else {
    231 			*v++ = '\0';
    232 			setalias(n, v);
    233 		}
    234 	}
    235 
    236 	return (ret);
    237 }
    238 
    239 int
    240 unaliascmd(argc, argv)
    241 	int argc;
    242 	char **argv;
    243 {
    244 	int i;
    245 
    246 	while ((i = nextopt("a")) != '\0') {
    247 		if (i == 'a') {
    248 			rmaliases();
    249 			return (0);
    250 		}
    251 	}
    252 	for (i = 0; *argptr; argptr++)
    253 		i = unalias(*argptr);
    254 
    255 	return (i);
    256 }
    257 
    258 STATIC struct alias **
    259 hashalias(p)
    260 	char *p;
    261 	{
    262 	unsigned int hashval;
    263 
    264 	hashval = *p << 4;
    265 	while (*p)
    266 		hashval+= *p++;
    267 	return &atab[hashval % ATABSIZE];
    268 }
    269