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 * 3. 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 #endif /* not lint */ 35 #include <sys/cdefs.h> 36 #include <stdlib.h> 37 #include "shell.h" 38 #include "output.h" 39 #include "error.h" 40 #include "memalloc.h" 41 #include "mystring.h" 42 #include "alias.h" 43 #include "options.h" 44 #include "builtins.h" 45 46 #define ATABSIZE 39 47 48 static struct alias *atab[ATABSIZE]; 49 static int aliases; 50 51 static void setalias(const char *, const char *); 52 static int unalias(const char *); 53 static size_t hashalias(const char *); 54 55 static 56 void 57 setalias(const char *name, const char *val) 58 { 59 struct alias *ap, **app; 60 61 unalias(name); 62 app = &atab[hashalias(name)]; 63 INTOFF; 64 ap = ckmalloc(sizeof (struct alias)); 65 ap->name = savestr(name); 66 ap->val = savestr(val); 67 ap->flag = 0; 68 ap->next = *app; 69 *app = ap; 70 aliases++; 71 INTON; 72 } 73 74 static void 75 freealias(struct alias *ap) 76 { 77 ckfree(ap->name); 78 ckfree(ap->val); 79 ckfree(ap); 80 } 81 82 static int 83 unalias(const char *name) 84 { 85 struct alias *ap, **app; 86 87 app = &atab[hashalias(name)]; 88 89 for (ap = *app; ap; app = &(ap->next), ap = ap->next) { 90 if (equal(name, ap->name)) { 91 /* 92 * if the alias is currently in use (i.e. its 93 * buffer is being used by the input routine) we 94 * just null out the name instead of freeing it. 95 * We could clear it out later, but this situation 96 * is so rare that it hardly seems worth it. 97 */ 98 if (ap->flag & ALIASINUSE) 99 *ap->name = '\0'; 100 else { 101 INTOFF; 102 *app = ap->next; 103 freealias(ap); 104 INTON; 105 } 106 aliases--; 107 return (0); 108 } 109 } 110 111 return (1); 112 } 113 114 static void 115 rmaliases(void) 116 { 117 struct alias *ap, **app; 118 int i; 119 120 INTOFF; 121 for (i = 0; i < ATABSIZE; i++) { 122 app = &atab[i]; 123 while (*app) { 124 ap = *app; 125 if (ap->flag & ALIASINUSE) { 126 *ap->name = '\0'; 127 app = &(*app)->next; 128 } else { 129 *app = ap->next; 130 freealias(ap); 131 } 132 } 133 } 134 aliases = 0; 135 INTON; 136 } 137 138 struct alias * 139 lookupalias(const char *name, int check) 140 { 141 struct alias *ap; 142 143 if (aliases == 0) 144 return (NULL); 145 for (ap = atab[hashalias(name)]; ap; ap = ap->next) { 146 if (equal(name, ap->name)) { 147 if (check && (ap->flag & ALIASINUSE)) 148 return (NULL); 149 return (ap); 150 } 151 } 152 153 return (NULL); 154 } 155 156 static int 157 comparealiases(const void *p1, const void *p2) 158 { 159 const struct alias *const *a1 = p1; 160 const struct alias *const *a2 = p2; 161 162 return strcmp((*a1)->name, (*a2)->name); 163 } 164 165 static void 166 printalias(const struct alias *a) 167 { 168 out1fmt("%s=", a->name); 169 out1qstr(a->val); 170 out1c('\n'); 171 } 172 173 static void 174 printaliases(void) 175 { 176 int i, j; 177 struct alias **sorted, *ap; 178 179 INTOFF; 180 sorted = ckmalloc(aliases * sizeof(*sorted)); 181 j = 0; 182 for (i = 0; i < ATABSIZE; i++) 183 for (ap = atab[i]; ap; ap = ap->next) 184 if (*ap->name != '\0') 185 sorted[j++] = ap; 186 qsort(sorted, aliases, sizeof(*sorted), comparealiases); 187 for (i = 0; i < aliases; i++) { 188 printalias(sorted[i]); 189 if (int_pending()) 190 break; 191 } 192 ckfree(sorted); 193 INTON; 194 } 195 196 int 197 aliascmd(int argc __unused, char **argv __unused) 198 { 199 char *n, *v; 200 int ret = 0; 201 struct alias *ap; 202 203 nextopt(""); 204 205 if (*argptr == NULL) { 206 printaliases(); 207 return (0); 208 } 209 while ((n = *argptr++) != NULL) { 210 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */ 211 if ((ap = lookupalias(n, 0)) == NULL) { 212 warning("%s: not found", n); 213 ret = 1; 214 } else 215 printalias(ap); 216 else { 217 *v++ = '\0'; 218 setalias(n, v); 219 } 220 } 221 222 return (ret); 223 } 224 225 int 226 unaliascmd(int argc __unused, char **argv __unused) 227 { 228 int i; 229 230 while ((i = nextopt("a")) != '\0') { 231 if (i == 'a') { 232 rmaliases(); 233 return (0); 234 } 235 } 236 for (i = 0; *argptr; argptr++) 237 i |= unalias(*argptr); 238 239 return (i); 240 } 241 242 static size_t 243 hashalias(const char *p) 244 { 245 unsigned int hashval; 246 247 hashval = (unsigned char)*p << 4; 248 while (*p) 249 hashval+= *p++; 250 return (hashval % ATABSIZE); 251 } 252 253 const struct alias * 254 iteralias(const struct alias *index) 255 { 256 size_t i = 0; 257 258 if (index != NULL) { 259 if (index->next != NULL) 260 return (index->next); 261 i = hashalias(index->name) + 1; 262 } 263 for (; i < ATABSIZE; i++) 264 if (atab[i] != NULL) 265 return (atab[i]); 266 267 return (NULL); 268 } 269