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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <stdlib.h> 42 #include "shell.h" 43 #include "output.h" 44 #include "error.h" 45 #include "memalloc.h" 46 #include "mystring.h" 47 #include "alias.h" 48 #include "options.h" /* XXX for argptr (should remove?) */ 49 50 #define ATABSIZE 39 51 52 STATIC struct alias *atab[ATABSIZE]; 53 54 STATIC void setalias(char *, char *); 55 STATIC int unalias(const char *); 56 STATIC struct alias **hashalias(const char *); 57 58 STATIC 59 void 60 setalias(char *name, char *val) 61 { 62 struct alias *ap, **app; 63 64 app = hashalias(name); 65 for (ap = *app; ap; ap = ap->next) { 66 if (equal(name, ap->name)) { 67 INTOFF; 68 ckfree(ap->val); 69 ap->val = savestr(val); 70 INTON; 71 return; 72 } 73 } 74 /* not found */ 75 INTOFF; 76 ap = ckmalloc(sizeof (struct alias)); 77 ap->name = savestr(name); 78 /* 79 * XXX - HACK: in order that the parser will not finish reading the 80 * alias value off the input before processing the next alias, we 81 * dummy up an extra space at the end of the alias. This is a crock 82 * and should be re-thought. The idea (if you feel inclined to help) 83 * is to avoid alias recursions. The mechanism used is: when 84 * expanding an alias, the value of the alias is pushed back on the 85 * input as a string and a pointer to the alias is stored with the 86 * string. The alias is marked as being in use. When the input 87 * routine finishes reading the string, it marks the alias not 88 * in use. The problem is synchronization with the parser. Since 89 * it reads ahead, the alias is marked not in use before the 90 * resulting token(s) is next checked for further alias sub. The 91 * H A C K is that we add a little fluff after the alias value 92 * so that the string will not be exhausted. This is a good 93 * idea ------- ***NOT*** 94 */ 95 #ifdef notyet 96 ap->val = savestr(val); 97 #else /* hack */ 98 { 99 int len = strlen(val); 100 ap->val = ckmalloc(len + 2); 101 memcpy(ap->val, val, len); 102 ap->val[len] = ' '; /* fluff */ 103 ap->val[len+1] = '\0'; 104 } 105 #endif 106 ap->flag = 0; 107 ap->next = *app; 108 *app = ap; 109 INTON; 110 } 111 112 STATIC int 113 unalias(const 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(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 /* 191 * TODO - sort output 192 */ 193 int 194 aliascmd(int argc, char **argv) 195 { 196 char *n, *v; 197 int ret = 0; 198 struct alias *ap; 199 200 if (argc == 1) { 201 int i; 202 203 for (i = 0; i < ATABSIZE; i++) 204 for (ap = atab[i]; ap; ap = ap->next) { 205 if (*ap->name != '\0') { 206 out1fmt("alias %s=", ap->name); 207 out1qstr(ap->val); 208 out1c('\n'); 209 } 210 } 211 return (0); 212 } 213 while ((n = *++argv) != NULL) { 214 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */ 215 if ((ap = lookupalias(n, 0)) == NULL) { 216 outfmt(out2, "alias: %s not found\n", n); 217 ret = 1; 218 } else { 219 out1fmt("alias %s=", n); 220 out1qstr(ap->val); 221 out1c('\n'); 222 } 223 else { 224 *v++ = '\0'; 225 setalias(n, v); 226 } 227 } 228 229 return (ret); 230 } 231 232 int 233 unaliascmd(int argc __unused, char **argv __unused) 234 { 235 int i; 236 237 while ((i = nextopt("a")) != '\0') { 238 if (i == 'a') { 239 rmaliases(); 240 return (0); 241 } 242 } 243 for (i = 0; *argptr; argptr++) 244 i |= unalias(*argptr); 245 246 return (i); 247 } 248 249 STATIC struct alias ** 250 hashalias(const char *p) 251 { 252 unsigned int hashval; 253 254 hashval = *p << 4; 255 while (*p) 256 hashval+= *p++; 257 return &atab[hashval % ATABSIZE]; 258 } 259