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