1 /* $NetBSD: alias.c,v 1.11 2002/11/24 22:35:38 christos 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95"; 43 #else 44 __RCSID("$NetBSD: alias.c,v 1.11 2002/11/24 22:35:38 christos Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <stdlib.h> 49 #include "shell.h" 50 #include "input.h" 51 #include "output.h" 52 #include "error.h" 53 #include "memalloc.h" 54 #include "mystring.h" 55 #include "alias.h" 56 #include "options.h" /* XXX for argptr (should remove?) */ 57 #include "var.h" 58 59 #define ATABSIZE 39 60 61 struct alias *atab[ATABSIZE]; 62 63 STATIC void setalias(char *, char *); 64 STATIC int unalias(char *); 65 STATIC struct alias **hashalias(char *); 66 67 STATIC 68 void 69 setalias(char *name, char *val) 70 { 71 struct alias *ap, **app; 72 73 app = hashalias(name); 74 for (ap = *app; ap; ap = ap->next) { 75 if (equal(name, ap->name)) { 76 INTOFF; 77 ckfree(ap->val); 78 ap->val = savestr(val); 79 INTON; 80 return; 81 } 82 } 83 /* not found */ 84 INTOFF; 85 ap = ckmalloc(sizeof (struct alias)); 86 ap->name = savestr(name); 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(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 char * 199 get_alias_text(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 /* 210 * TODO - sort output 211 */ 212 int 213 aliascmd(int argc, char **argv) 214 { 215 char *n, *v; 216 int ret = 0; 217 struct alias *ap; 218 219 if (argc == 1) { 220 int i; 221 222 for (i = 0; i < ATABSIZE; i++) 223 for (ap = atab[i]; ap; ap = ap->next) { 224 if (*ap->name != '\0') { 225 out1fmt("alias %s=", ap->name); 226 print_quoted(ap->val); 227 out1c('\n'); 228 } 229 } 230 return (0); 231 } 232 while ((n = *++argv) != NULL) { 233 if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ 234 if ((ap = lookupalias(n, 0)) == NULL) { 235 outfmt(out2, "alias: %s not found\n", n); 236 ret = 1; 237 } else { 238 out1fmt("alias %s=", n); 239 print_quoted(ap->val); 240 out1c('\n'); 241 } 242 } else { 243 *v++ = '\0'; 244 setalias(n, v); 245 } 246 } 247 248 return (ret); 249 } 250 251 int 252 unaliascmd(int argc, char **argv) 253 { 254 int i; 255 256 while ((i = nextopt("a")) != '\0') { 257 if (i == 'a') { 258 rmaliases(); 259 return (0); 260 } 261 } 262 for (i = 0; *argptr; argptr++) 263 i = unalias(*argptr); 264 265 return (i); 266 } 267 268 STATIC struct alias ** 269 hashalias(char *p) 270 { 271 unsigned int hashval; 272 273 hashval = *p << 4; 274 while (*p) 275 hashval+= *p++; 276 return &atab[hashval % ATABSIZE]; 277 } 278