Home | History | Annotate | Line # | Download | only in sh
alias.c revision 1.17
      1 /*	$NetBSD: alias.c,v 1.17 2018/10/07 23:17:52 rillig 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.17 2018/10/07 23:17:52 rillig 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 	app = hashalias(name);
     73 	for (ap = *app; ap; ap = ap->next) {
     74 		if (equal(name, ap->name)) {
     75 			INTOFF;
     76 			ckfree(ap->val);
     77 			ap->val	= savestr(val);
     78 			INTON;
     79 			return;
     80 		}
     81 	}
     82 	/* not found */
     83 	INTOFF;
     84 	ap = ckmalloc(sizeof (struct alias));
     85 	ap->name = savestr(name);
     86 	ap->flag = 0;
     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(char *name)
    122 {
    123 	struct alias *ap, **app;
    124 
    125 	app = hashalias(name);
    126 
    127 	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
    128 		if (equal(name, ap->name)) {
    129 			/*
    130 			 * if the alias is currently in use (i.e. its
    131 			 * buffer is being used by the input routine) we
    132 			 * just null out the name instead of freeing it.
    133 			 * We could clear it out later, but this situation
    134 			 * is so rare that it hardly seems worth it.
    135 			 */
    136 			if (ap->flag & ALIASINUSE)
    137 				*ap->name = '\0';
    138 			else {
    139 				INTOFF;
    140 				*app = ap->next;
    141 				ckfree(ap->name);
    142 				ckfree(ap->val);
    143 				ckfree(ap);
    144 				INTON;
    145 			}
    146 			return (0);
    147 		}
    148 	}
    149 
    150 	return (1);
    151 }
    152 
    153 #ifdef mkinit
    154 MKINIT void rmaliases(void);
    155 
    156 SHELLPROC {
    157 	rmaliases();
    158 }
    159 #endif
    160 
    161 void
    162 rmaliases(void)
    163 {
    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(const char *name, int check)
    184 {
    185 	struct alias *ap = *hashalias(name);
    186 
    187 	for (; ap; ap = ap->next) {
    188 		if (equal(name, ap->name)) {
    189 			if (check && (ap->flag & ALIASINUSE))
    190 				return (NULL);
    191 			return (ap);
    192 		}
    193 	}
    194 
    195 	return (NULL);
    196 }
    197 
    198 const char *
    199 alias_text(void *dummy __unused, const char *name)
    200 {
    201 	struct alias *ap;
    202 
    203 	ap = lookupalias(name, 0);
    204 	if (ap == NULL)
    205 		return NULL;
    206 	return ap->val;
    207 }
    208 
    209 STATIC int
    210 by_name(const void *a, const void *b)
    211 {
    212 
    213 	return strcmp(
    214 		(*(const struct alias * const *)a)->name,
    215 		(*(const struct alias * const *)b)->name);
    216 }
    217 
    218 STATIC void
    219 list_aliases(void)
    220 {
    221 	size_t i, j, n;
    222 	const struct alias **aliases;
    223 	const struct alias *ap;
    224 
    225 	n = 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 				n++;
    230 
    231 	aliases = ckmalloc(n * sizeof aliases[0]);
    232 
    233 	j = 0;
    234 	for (i = 0; i < ATABSIZE; i++)
    235 		for (ap = atab[i]; ap != NULL; ap = ap->next)
    236 			if (ap->name[0] != '\0')
    237 				aliases[j++] = ap;
    238 
    239 	qsort(aliases, n, sizeof aliases[0], by_name);
    240 
    241 	for (i = 0; i < n; i++) {
    242 		out1fmt("alias %s=", aliases[i]->name);
    243 		print_quoted(aliases[i]->val);
    244 		out1c('\n');
    245 	}
    246 
    247 	ckfree(aliases);
    248 }
    249 
    250 int
    251 aliascmd(int argc, char **argv)
    252 {
    253 	char *n, *v;
    254 	int ret = 0;
    255 	struct alias *ap;
    256 
    257 	if (argc == 1) {
    258 		list_aliases();
    259 		return (0);
    260 	}
    261 
    262 	while ((n = *++argv) != NULL) {
    263 		if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
    264 			if ((ap = lookupalias(n, 0)) == NULL) {
    265 				outfmt(out2, "alias: %s not found\n", n);
    266 				ret = 1;
    267 			} else {
    268 				out1fmt("alias %s=", n);
    269 				print_quoted(ap->val);
    270 				out1c('\n');
    271 			}
    272 		} else {
    273 			*v++ = '\0';
    274 			setalias(n, v);
    275 		}
    276 	}
    277 
    278 	return (ret);
    279 }
    280 
    281 int
    282 unaliascmd(int argc, char **argv)
    283 {
    284 	int i;
    285 
    286 	while ((i = nextopt("a")) != '\0') {
    287 		if (i == 'a') {
    288 			rmaliases();
    289 			return (0);
    290 		}
    291 	}
    292 	for (i = 0; *argptr; argptr++)
    293 		i = unalias(*argptr);
    294 
    295 	return (i);
    296 }
    297 
    298 STATIC struct alias **
    299 hashalias(const char *p)
    300 {
    301 	unsigned int hashval;
    302 
    303 	hashval = *(const unsigned char *)p << 4;
    304 	while (*p)
    305 		hashval += *p++;
    306 	return &atab[hashval % ATABSIZE];
    307 }
    308