Home | History | Annotate | Line # | Download | only in sh
alias.c revision 1.15.16.1
      1 /*	$NetBSD: alias.c,v 1.15.16.1 2018/12/07 13:14:42 martin 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.15.16.1 2018/12/07 13:14:42 martin 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 unalias(char *);
     62 STATIC struct alias **hashalias(const char *);
     63 
     64 STATIC
     65 void
     66 setalias(char *name, char *val)
     67 {
     68 	struct alias *ap, **app;
     69 
     70 	(void) unalias(name);	/* old one (if any) is now gone */
     71 	app = hashalias(name);
     72 
     73 	INTOFF;
     74 	ap = ckmalloc(sizeof (struct alias));
     75 	ap->name = savestr(name);
     76 	ap->flag = 0;
     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 	memcpy(ap->val, 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(char *name)
    112 {
    113 	struct alias *ap, **app;
    114 
    115 	app = hashalias(name);
    116 
    117 	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
    118 		if (equal(name, ap->name)) {
    119 			/*
    120 			 * if the alias is currently in use (i.e. its
    121 			 * buffer is being used by the input routine) we
    122 			 * just null out the name instead of freeing it.
    123 			 * We could clear it out later, but this situation
    124 			 * is so rare that it hardly seems worth it.
    125 			 */
    126 			if (ap->flag & ALIASINUSE)
    127 				*ap->name = '\0';
    128 			else {
    129 				INTOFF;
    130 				*app = ap->next;
    131 				ckfree(ap->name);
    132 				ckfree(ap->val);
    133 				ckfree(ap);
    134 				INTON;
    135 			}
    136 			return (0);
    137 		}
    138 	}
    139 
    140 	return (1);
    141 }
    142 
    143 #ifdef mkinit
    144 MKINIT void rmaliases(void);
    145 
    146 SHELLPROC {
    147 	rmaliases();
    148 }
    149 #endif
    150 
    151 void
    152 rmaliases(void)
    153 {
    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(const char *name, int check)
    174 {
    175 	struct alias *ap = *hashalias(name);
    176 
    177 	for (; ap; ap = ap->next) {
    178 		if (equal(name, ap->name)) {
    179 			if (check && (ap->flag & ALIASINUSE))
    180 				return (NULL);
    181 			return (ap);
    182 		}
    183 	}
    184 
    185 	return (NULL);
    186 }
    187 
    188 const char *
    189 alias_text(void *dummy __unused, const char *name)
    190 {
    191 	struct alias *ap;
    192 
    193 	ap = lookupalias(name, 0);
    194 	if (ap == NULL)
    195 		return NULL;
    196 	return ap->val;
    197 }
    198 
    199 /*
    200  * TODO - sort output
    201  */
    202 int
    203 aliascmd(int argc, char **argv)
    204 {
    205 	char *n, *v;
    206 	int ret = 0;
    207 	struct alias *ap;
    208 
    209 	if (argc == 1) {
    210 		int i;
    211 
    212 		for (i = 0; i < ATABSIZE; i++)
    213 			for (ap = atab[i]; ap; ap = ap->next) {
    214 				if (*ap->name != '\0') {
    215 					out1fmt("alias %s=", ap->name);
    216 					print_quoted(ap->val);
    217 					out1c('\n');
    218 				}
    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=", n);
    229 				print_quoted(ap->val);
    230 				out1c('\n');
    231 			}
    232 		} else {
    233 			*v++ = '\0';
    234 			setalias(n, v);
    235 		}
    236 	}
    237 
    238 	return (ret);
    239 }
    240 
    241 int
    242 unaliascmd(int argc, 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(const char *p)
    260 {
    261 	unsigned int hashval;
    262 
    263 	hashval = *p << 4;
    264 	while (*p)
    265 		hashval+= *p++;
    266 	return &atab[hashval % ATABSIZE];
    267 }
    268