Home | History | Annotate | Line # | Download | only in sh
alias.c revision 1.13
      1 /*	$NetBSD: alias.c,v 1.13 2010/10/29 17:04:48 stacktic 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 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: alias.c,v 1.13 2010/10/29 17:04:48 stacktic Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <stdlib.h>
     45 #include "shell.h"
     46 #include "input.h"
     47 #include "output.h"
     48 #include "error.h"
     49 #include "memalloc.h"
     50 #include "mystring.h"
     51 #include "alias.h"
     52 #include "options.h"	/* XXX for argptr (should remove?) */
     53 #include "var.h"
     54 
     55 #define ATABSIZE 39
     56 
     57 struct alias *atab[ATABSIZE];
     58 
     59 STATIC void setalias(char *, char *);
     60 STATIC int unalias(char *);
     61 STATIC struct alias **hashalias(char *);
     62 
     63 STATIC
     64 void
     65 setalias(char *name, char *val)
     66 {
     67 	struct alias *ap, **app;
     68 
     69 	app = hashalias(name);
     70 	for (ap = *app; ap; ap = ap->next) {
     71 		if (equal(name, ap->name)) {
     72 			INTOFF;
     73 			ckfree(ap->val);
     74 			ap->val	= savestr(val);
     75 			INTON;
     76 			return;
     77 		}
     78 	}
     79 	/* not found */
     80 	INTOFF;
     81 	ap = ckmalloc(sizeof (struct alias));
     82 	ap->name = savestr(name);
     83 	ap->flag = 0;
     84 	/*
     85 	 * XXX - HACK: in order that the parser will not finish reading the
     86 	 * alias value off the input before processing the next alias, we
     87 	 * dummy up an extra space at the end of the alias.  This is a crock
     88 	 * and should be re-thought.  The idea (if you feel inclined to help)
     89 	 * is to avoid alias recursions.  The mechanism used is: when
     90 	 * expanding an alias, the value of the alias is pushed back on the
     91 	 * input as a string and a pointer to the alias is stored with the
     92 	 * string.  The alias is marked as being in use.  When the input
     93 	 * routine finishes reading the string, it markes the alias not
     94 	 * in use.  The problem is synchronization with the parser.  Since
     95 	 * it reads ahead, the alias is marked not in use before the
     96 	 * resulting token(s) is next checked for further alias sub.  The
     97 	 * H A C K is that we add a little fluff after the alias value
     98 	 * so that the string will not be exhausted.  This is a good
     99 	 * idea ------- ***NOT***
    100 	 */
    101 #ifdef notyet
    102 	ap->val = savestr(val);
    103 #else /* hack */
    104 	{
    105 	int len = strlen(val);
    106 	ap->val = ckmalloc(len + 2);
    107 	memcpy(ap->val, val, len);
    108 	ap->val[len] = ' ';	/* fluff */
    109 	ap->val[len+1] = '\0';
    110 	}
    111 #endif
    112 	ap->next = *app;
    113 	*app = ap;
    114 	INTON;
    115 }
    116 
    117 STATIC int
    118 unalias(char *name)
    119 {
    120 	struct alias *ap, **app;
    121 
    122 	app = hashalias(name);
    123 
    124 	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
    125 		if (equal(name, ap->name)) {
    126 			/*
    127 			 * if the alias is currently in use (i.e. its
    128 			 * buffer is being used by the input routine) we
    129 			 * just null out the name instead of freeing it.
    130 			 * We could clear it out later, but this situation
    131 			 * is so rare that it hardly seems worth it.
    132 			 */
    133 			if (ap->flag & ALIASINUSE)
    134 				*ap->name = '\0';
    135 			else {
    136 				INTOFF;
    137 				*app = ap->next;
    138 				ckfree(ap->name);
    139 				ckfree(ap->val);
    140 				ckfree(ap);
    141 				INTON;
    142 			}
    143 			return (0);
    144 		}
    145 	}
    146 
    147 	return (1);
    148 }
    149 
    150 #ifdef mkinit
    151 MKINIT void rmaliases(void);
    152 
    153 SHELLPROC {
    154 	rmaliases();
    155 }
    156 #endif
    157 
    158 void
    159 rmaliases(void)
    160 {
    161 	struct alias *ap, *tmp;
    162 	int i;
    163 
    164 	INTOFF;
    165 	for (i = 0; i < ATABSIZE; i++) {
    166 		ap = atab[i];
    167 		atab[i] = NULL;
    168 		while (ap) {
    169 			ckfree(ap->name);
    170 			ckfree(ap->val);
    171 			tmp = ap;
    172 			ap = ap->next;
    173 			ckfree(tmp);
    174 		}
    175 	}
    176 	INTON;
    177 }
    178 
    179 struct alias *
    180 lookupalias(char *name, int check)
    181 {
    182 	struct alias *ap = *hashalias(name);
    183 
    184 	for (; ap; ap = ap->next) {
    185 		if (equal(name, ap->name)) {
    186 			if (check && (ap->flag & ALIASINUSE))
    187 				return (NULL);
    188 			return (ap);
    189 		}
    190 	}
    191 
    192 	return (NULL);
    193 }
    194 
    195 char *
    196 get_alias_text(char *name)
    197 {
    198 	struct alias *ap;
    199 
    200 	ap = lookupalias(name, 0);
    201 	if (ap == NULL)
    202 		return NULL;
    203 	return ap->val;
    204 }
    205 
    206 /*
    207  * TODO - sort output
    208  */
    209 int
    210 aliascmd(int argc, char **argv)
    211 {
    212 	char *n, *v;
    213 	int ret = 0;
    214 	struct alias *ap;
    215 
    216 	if (argc == 1) {
    217 		int i;
    218 
    219 		for (i = 0; i < ATABSIZE; i++)
    220 			for (ap = atab[i]; ap; ap = ap->next) {
    221 				if (*ap->name != '\0') {
    222 					out1fmt("alias %s=", ap->name);
    223 					print_quoted(ap->val);
    224 					out1c('\n');
    225 				}
    226 			}
    227 		return (0);
    228 	}
    229 	while ((n = *++argv) != NULL) {
    230 		if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
    231 			if ((ap = lookupalias(n, 0)) == NULL) {
    232 				outfmt(out2, "alias: %s not found\n", n);
    233 				ret = 1;
    234 			} else {
    235 				out1fmt("alias %s=", n);
    236 				print_quoted(ap->val);
    237 				out1c('\n');
    238 			}
    239 		} else {
    240 			*v++ = '\0';
    241 			setalias(n, v);
    242 		}
    243 	}
    244 
    245 	return (ret);
    246 }
    247 
    248 int
    249 unaliascmd(int argc, char **argv)
    250 {
    251 	int i;
    252 
    253 	while ((i = nextopt("a")) != '\0') {
    254 		if (i == 'a') {
    255 			rmaliases();
    256 			return (0);
    257 		}
    258 	}
    259 	for (i = 0; *argptr; argptr++)
    260 		i = unalias(*argptr);
    261 
    262 	return (i);
    263 }
    264 
    265 STATIC struct alias **
    266 hashalias(char *p)
    267 {
    268 	unsigned int hashval;
    269 
    270 	hashval = *p << 4;
    271 	while (*p)
    272 		hashval+= *p++;
    273 	return &atab[hashval % ATABSIZE];
    274 }
    275