14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 39aa9caaf6SPeter Wemm #include <stdlib.h> 404b88c807SRodney W. Grimes #include "shell.h" 414b88c807SRodney W. Grimes #include "output.h" 424b88c807SRodney W. Grimes #include "error.h" 434b88c807SRodney W. Grimes #include "memalloc.h" 444b88c807SRodney W. Grimes #include "mystring.h" 454b88c807SRodney W. Grimes #include "alias.h" 4605e626c3SJilles Tjoelker #include "options.h" 47454a02b3SJilles Tjoelker #include "builtins.h" 484b88c807SRodney W. Grimes 494b88c807SRodney W. Grimes #define ATABSIZE 39 504b88c807SRodney W. Grimes 51aa7b6f82SDavid E. O'Brien static struct alias *atab[ATABSIZE]; 52aa7b6f82SDavid E. O'Brien static int aliases; 534b88c807SRodney W. Grimes 5488328642SDavid E. O'Brien static void setalias(const char *, const char *); 5588328642SDavid E. O'Brien static int unalias(const char *); 56*2fc4a84eSPiotr Pawel Stefaniak static size_t hashalias(const char *); 574b88c807SRodney W. Grimes 5888328642SDavid E. O'Brien static 59aa9caaf6SPeter Wemm void 602cac6e36SJilles Tjoelker setalias(const char *name, const char *val) 614b88c807SRodney W. Grimes { 624b88c807SRodney W. Grimes struct alias *ap, **app; 634b88c807SRodney W. Grimes 64026dfd4aSJilles Tjoelker unalias(name); 65*2fc4a84eSPiotr Pawel Stefaniak app = &atab[hashalias(name)]; 664b88c807SRodney W. Grimes INTOFF; 674b88c807SRodney W. Grimes ap = ckmalloc(sizeof (struct alias)); 684b88c807SRodney W. Grimes ap->name = savestr(name); 694b88c807SRodney W. Grimes ap->val = savestr(val); 70410fb0acSSADA Kenji ap->flag = 0; 714b88c807SRodney W. Grimes ap->next = *app; 724b88c807SRodney W. Grimes *app = ap; 73d4b1e374SStefan Farfeleder aliases++; 744b88c807SRodney W. Grimes INTON; 754b88c807SRodney W. Grimes } 764b88c807SRodney W. Grimes 77773e27aeSJilles Tjoelker static void 78773e27aeSJilles Tjoelker freealias(struct alias *ap) 79773e27aeSJilles Tjoelker { 80773e27aeSJilles Tjoelker ckfree(ap->name); 81773e27aeSJilles Tjoelker ckfree(ap->val); 82773e27aeSJilles Tjoelker ckfree(ap); 83773e27aeSJilles Tjoelker } 84773e27aeSJilles Tjoelker 8588328642SDavid E. O'Brien static int 86a65d88d1SRalf S. Engelschall unalias(const char *name) 874b88c807SRodney W. Grimes { 884b88c807SRodney W. Grimes struct alias *ap, **app; 894b88c807SRodney W. Grimes 90*2fc4a84eSPiotr Pawel Stefaniak app = &atab[hashalias(name)]; 914b88c807SRodney W. Grimes 924b88c807SRodney W. Grimes for (ap = *app; ap; app = &(ap->next), ap = ap->next) { 934b88c807SRodney W. Grimes if (equal(name, ap->name)) { 944b88c807SRodney W. Grimes /* 954b88c807SRodney W. Grimes * if the alias is currently in use (i.e. its 964b88c807SRodney W. Grimes * buffer is being used by the input routine) we 974b88c807SRodney W. Grimes * just null out the name instead of freeing it. 984b88c807SRodney W. Grimes * We could clear it out later, but this situation 994b88c807SRodney W. Grimes * is so rare that it hardly seems worth it. 1004b88c807SRodney W. Grimes */ 1014b88c807SRodney W. Grimes if (ap->flag & ALIASINUSE) 1024b88c807SRodney W. Grimes *ap->name = '\0'; 1034b88c807SRodney W. Grimes else { 1044b88c807SRodney W. Grimes INTOFF; 1054b88c807SRodney W. Grimes *app = ap->next; 106773e27aeSJilles Tjoelker freealias(ap); 1074b88c807SRodney W. Grimes INTON; 1084b88c807SRodney W. Grimes } 109d4b1e374SStefan Farfeleder aliases--; 1104b88c807SRodney W. Grimes return (0); 1114b88c807SRodney W. Grimes } 1124b88c807SRodney W. Grimes } 1134b88c807SRodney W. Grimes 1144b88c807SRodney W. Grimes return (1); 1154b88c807SRodney W. Grimes } 1164b88c807SRodney W. Grimes 1173835f47cSJilles Tjoelker static void 1185134c3f7SWarner Losh rmaliases(void) 1195134c3f7SWarner Losh { 120773e27aeSJilles Tjoelker struct alias *ap, **app; 1214b88c807SRodney W. Grimes int i; 1224b88c807SRodney W. Grimes 1234b88c807SRodney W. Grimes INTOFF; 1244b88c807SRodney W. Grimes for (i = 0; i < ATABSIZE; i++) { 125773e27aeSJilles Tjoelker app = &atab[i]; 126773e27aeSJilles Tjoelker while (*app) { 127773e27aeSJilles Tjoelker ap = *app; 128773e27aeSJilles Tjoelker if (ap->flag & ALIASINUSE) { 129773e27aeSJilles Tjoelker *ap->name = '\0'; 130773e27aeSJilles Tjoelker app = &(*app)->next; 131773e27aeSJilles Tjoelker } else { 132773e27aeSJilles Tjoelker *app = ap->next; 133773e27aeSJilles Tjoelker freealias(ap); 134773e27aeSJilles Tjoelker } 1354b88c807SRodney W. Grimes } 1364b88c807SRodney W. Grimes } 137d4b1e374SStefan Farfeleder aliases = 0; 1384b88c807SRodney W. Grimes INTON; 1394b88c807SRodney W. Grimes } 1404b88c807SRodney W. Grimes 1414b88c807SRodney W. Grimes struct alias * 1422cac6e36SJilles Tjoelker lookupalias(const char *name, int check) 1434b88c807SRodney W. Grimes { 1442ece3386SJilles Tjoelker struct alias *ap; 1454b88c807SRodney W. Grimes 1462ece3386SJilles Tjoelker if (aliases == 0) 1472ece3386SJilles Tjoelker return (NULL); 148*2fc4a84eSPiotr Pawel Stefaniak for (ap = atab[hashalias(name)]; ap; ap = ap->next) { 1494b88c807SRodney W. Grimes if (equal(name, ap->name)) { 1504b88c807SRodney W. Grimes if (check && (ap->flag & ALIASINUSE)) 1514b88c807SRodney W. Grimes return (NULL); 1524b88c807SRodney W. Grimes return (ap); 1534b88c807SRodney W. Grimes } 1544b88c807SRodney W. Grimes } 1554b88c807SRodney W. Grimes 1564b88c807SRodney W. Grimes return (NULL); 1574b88c807SRodney W. Grimes } 1584b88c807SRodney W. Grimes 15988328642SDavid E. O'Brien static int 160d4b1e374SStefan Farfeleder comparealiases(const void *p1, const void *p2) 161d4b1e374SStefan Farfeleder { 162d4b1e374SStefan Farfeleder const struct alias *const *a1 = p1; 163d4b1e374SStefan Farfeleder const struct alias *const *a2 = p2; 164d4b1e374SStefan Farfeleder 165d4b1e374SStefan Farfeleder return strcmp((*a1)->name, (*a2)->name); 166d4b1e374SStefan Farfeleder } 167d4b1e374SStefan Farfeleder 16888328642SDavid E. O'Brien static void 169d4b1e374SStefan Farfeleder printalias(const struct alias *a) 170d4b1e374SStefan Farfeleder { 171d4b1e374SStefan Farfeleder out1fmt("%s=", a->name); 172d4b1e374SStefan Farfeleder out1qstr(a->val); 173d4b1e374SStefan Farfeleder out1c('\n'); 174d4b1e374SStefan Farfeleder } 175d4b1e374SStefan Farfeleder 17688328642SDavid E. O'Brien static void 177d4b1e374SStefan Farfeleder printaliases(void) 178d4b1e374SStefan Farfeleder { 179d4b1e374SStefan Farfeleder int i, j; 180d4b1e374SStefan Farfeleder struct alias **sorted, *ap; 181d4b1e374SStefan Farfeleder 1821632bf1aSJilles Tjoelker INTOFF; 183d4b1e374SStefan Farfeleder sorted = ckmalloc(aliases * sizeof(*sorted)); 184d4b1e374SStefan Farfeleder j = 0; 185d4b1e374SStefan Farfeleder for (i = 0; i < ATABSIZE; i++) 186d4b1e374SStefan Farfeleder for (ap = atab[i]; ap; ap = ap->next) 187d4b1e374SStefan Farfeleder if (*ap->name != '\0') 188d4b1e374SStefan Farfeleder sorted[j++] = ap; 189d4b1e374SStefan Farfeleder qsort(sorted, aliases, sizeof(*sorted), comparealiases); 1901632bf1aSJilles Tjoelker for (i = 0; i < aliases; i++) { 191d4b1e374SStefan Farfeleder printalias(sorted[i]); 1921632bf1aSJilles Tjoelker if (int_pending()) 1931632bf1aSJilles Tjoelker break; 1941632bf1aSJilles Tjoelker } 195d4b1e374SStefan Farfeleder ckfree(sorted); 1961632bf1aSJilles Tjoelker INTON; 197d4b1e374SStefan Farfeleder } 198d4b1e374SStefan Farfeleder 199aa9caaf6SPeter Wemm int 20071828da5SJilles Tjoelker aliascmd(int argc __unused, char **argv __unused) 2014b88c807SRodney W. Grimes { 2024b88c807SRodney W. Grimes char *n, *v; 2034b88c807SRodney W. Grimes int ret = 0; 2044b88c807SRodney W. Grimes struct alias *ap; 2054b88c807SRodney W. Grimes 20671828da5SJilles Tjoelker nextopt(""); 20771828da5SJilles Tjoelker 20871828da5SJilles Tjoelker if (*argptr == NULL) { 209d4b1e374SStefan Farfeleder printaliases(); 2104b88c807SRodney W. Grimes return (0); 2114b88c807SRodney W. Grimes } 21271828da5SJilles Tjoelker while ((n = *argptr++) != NULL) { 2134b88c807SRodney W. Grimes if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */ 2144b88c807SRodney W. Grimes if ((ap = lookupalias(n, 0)) == NULL) { 21547a5ab29SJilles Tjoelker warning("%s: not found", n); 2164b88c807SRodney W. Grimes ret = 1; 217d4b1e374SStefan Farfeleder } else 218d4b1e374SStefan Farfeleder printalias(ap); 2194b88c807SRodney W. Grimes else { 2204b88c807SRodney W. Grimes *v++ = '\0'; 2214b88c807SRodney W. Grimes setalias(n, v); 2224b88c807SRodney W. Grimes } 2234b88c807SRodney W. Grimes } 2244b88c807SRodney W. Grimes 2254b88c807SRodney W. Grimes return (ret); 2264b88c807SRodney W. Grimes } 2274b88c807SRodney W. Grimes 228aa9caaf6SPeter Wemm int 2295134c3f7SWarner Losh unaliascmd(int argc __unused, char **argv __unused) 2304b88c807SRodney W. Grimes { 2314b88c807SRodney W. Grimes int i; 2324b88c807SRodney W. Grimes 2334b88c807SRodney W. Grimes while ((i = nextopt("a")) != '\0') { 2344b88c807SRodney W. Grimes if (i == 'a') { 2354b88c807SRodney W. Grimes rmaliases(); 2364b88c807SRodney W. Grimes return (0); 2374b88c807SRodney W. Grimes } 2384b88c807SRodney W. Grimes } 2394b88c807SRodney W. Grimes for (i = 0; *argptr; argptr++) 240dbf2e1c5SStefan Farfeleder i |= unalias(*argptr); 2414b88c807SRodney W. Grimes 2424b88c807SRodney W. Grimes return (i); 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes 245*2fc4a84eSPiotr Pawel Stefaniak static size_t 246a65d88d1SRalf S. Engelschall hashalias(const char *p) 2474b88c807SRodney W. Grimes { 2484b88c807SRodney W. Grimes unsigned int hashval; 2494b88c807SRodney W. Grimes 250c3c85727SJilles Tjoelker hashval = (unsigned char)*p << 4; 2514b88c807SRodney W. Grimes while (*p) 2524b88c807SRodney W. Grimes hashval+= *p++; 253*2fc4a84eSPiotr Pawel Stefaniak return (hashval % ATABSIZE); 254*2fc4a84eSPiotr Pawel Stefaniak } 255*2fc4a84eSPiotr Pawel Stefaniak 256*2fc4a84eSPiotr Pawel Stefaniak const struct alias * 257*2fc4a84eSPiotr Pawel Stefaniak iteralias(const struct alias *index) 258*2fc4a84eSPiotr Pawel Stefaniak { 259*2fc4a84eSPiotr Pawel Stefaniak size_t i = 0; 260*2fc4a84eSPiotr Pawel Stefaniak 261*2fc4a84eSPiotr Pawel Stefaniak if (index != NULL) { 262*2fc4a84eSPiotr Pawel Stefaniak if (index->next != NULL) 263*2fc4a84eSPiotr Pawel Stefaniak return (index->next); 264*2fc4a84eSPiotr Pawel Stefaniak i = hashalias(index->name) + 1; 265*2fc4a84eSPiotr Pawel Stefaniak } 266*2fc4a84eSPiotr Pawel Stefaniak for (; i < ATABSIZE; i++) 267*2fc4a84eSPiotr Pawel Stefaniak if (atab[i] != NULL) 268*2fc4a84eSPiotr Pawel Stefaniak return (atab[i]); 269*2fc4a84eSPiotr Pawel Stefaniak 270*2fc4a84eSPiotr Pawel Stefaniak return (NULL); 2714b88c807SRodney W. Grimes } 272