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