| 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 | * @(#)alias.c 8.3 (Berkeley) 5/4/95 |
| 33 | * $FreeBSD: head/bin/sh/alias.c 242766 2012-11-08 13:33:48Z jilles $ |
| 34 | */ |
| 35 | |
| 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" /* XXX for argptr (should remove?) */ |
| 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 struct alias **hashalias(const char *); |
| 54 | |
| 55 | static |
| 56 | void |
| 57 | setalias(const char *name, const char *val) |
| 58 | { |
| 59 | struct alias *ap, **app; |
| 60 | |
| 61 | app = hashalias(name); |
| 62 | for (ap = *app; ap; ap = ap->next) { |
| 63 | if (equal(name, ap->name)) { |
| 64 | INTOFF; |
| 65 | ckfree(ap->val); |
| 66 | /* See HACK below. */ |
| 67 | #ifdef notyet |
| 68 | ap->val = savestr(val); |
| 69 | #else |
| 70 | { |
| 71 | size_t len = strlen(val); |
| 72 | ap->val = ckmalloc(len + 2); |
| 73 | memcpy(ap->val, val, len); |
| 74 | ap->val[len] = ' '; |
| 75 | ap->val[len+1] = '\0'; |
| 76 | } |
| 77 | #endif |
| 78 | INTON; |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | /* not found */ |
| 83 | INTOFF; |
| 84 | ap = ckmalloc(sizeof (struct alias)); |
| 85 | ap->name = savestr(name); |
| 86 | /* |
| 87 | * XXX - HACK: in order that the parser will not finish reading the |
| 88 | * alias value off the input before processing the next alias, we |
| 89 | * dummy up an extra space at the end of the alias. This is a crock |
| 90 | * and should be re-thought. The idea (if you feel inclined to help) |
| 91 | * is to avoid alias recursions. The mechanism used is: when |
| 92 | * expanding an alias, the value of the alias is pushed back on the |
| 93 | * input as a string and a pointer to the alias is stored with the |
| 94 | * string. The alias is marked as being in use. When the input |
| 95 | * routine finishes reading the string, it marks the alias not |
| 96 | * in use. The problem is synchronization with the parser. Since |
| 97 | * it reads ahead, the alias is marked not in use before the |
| 98 | * resulting token(s) is next checked for further alias sub. The |
| 99 | * H A C K is that we add a little fluff after the alias value |
| 100 | * so that the string will not be exhausted. This is a good |
| 101 | * idea ------- ***NOT*** |
| 102 | */ |
| 103 | #ifdef notyet |
| 104 | ap->val = savestr(val); |
| 105 | #else /* hack */ |
| 106 | { |
| 107 | size_t len = strlen(val); |
| 108 | ap->val = ckmalloc(len + 2); |
| 109 | memcpy(ap->val, val, len); |
| 110 | ap->val[len] = ' '; /* fluff */ |
| 111 | ap->val[len+1] = '\0'; |
| 112 | } |
| 113 | #endif |
| 114 | ap->flag = 0; |
| 115 | ap->next = *app; |
| 116 | *app = ap; |
| 117 | aliases++; |
| 118 | INTON; |
| 119 | } |
| 120 | |
| 121 | static int |
| 122 | unalias(const char *name) |
| 123 | { |
| 124 | struct alias *ap, **app; |
| 125 | |
| 126 | app = hashalias(name); |
| 127 | |
| 128 | for (ap = *app; ap; app = &(ap->next), ap = ap->next) { |
| 129 | if (equal(name, ap->name)) { |
| 130 | /* |
| 131 | * if the alias is currently in use (i.e. its |
| 132 | * buffer is being used by the input routine) we |
| 133 | * just null out the name instead of freeing it. |
| 134 | * We could clear it out later, but this situation |
| 135 | * is so rare that it hardly seems worth it. |
| 136 | */ |
| 137 | if (ap->flag & ALIASINUSE) |
| 138 | *ap->name = '\0'; |
| 139 | else { |
| 140 | INTOFF; |
| 141 | *app = ap->next; |
| 142 | ckfree(ap->name); |
| 143 | ckfree(ap->val); |
| 144 | ckfree(ap); |
| 145 | INTON; |
| 146 | } |
| 147 | aliases--; |
| 148 | return (0); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return (1); |
| 153 | } |
| 154 | |
| 155 | static void |
| 156 | rmaliases(void) |
| 157 | { |
| 158 | struct alias *ap, *tmp; |
| 159 | int i; |
| 160 | |
| 161 | INTOFF; |
| 162 | for (i = 0; i < ATABSIZE; i++) { |
| 163 | ap = atab[i]; |
| 164 | atab[i] = NULL; |
| 165 | while (ap) { |
| 166 | ckfree(ap->name); |
| 167 | ckfree(ap->val); |
| 168 | tmp = ap; |
| 169 | ap = ap->next; |
| 170 | ckfree(tmp); |
| 171 | } |
| 172 | } |
| 173 | aliases = 0; |
| 174 | INTON; |
| 175 | } |
| 176 | |
| 177 | struct alias * |
| 178 | lookupalias(const char *name, int check) |
| 179 | { |
| 180 | struct alias *ap = *hashalias(name); |
| 181 | |
| 182 | for (; ap; ap = ap->next) { |
| 183 | if (equal(name, ap->name)) { |
| 184 | if (check && (ap->flag & ALIASINUSE)) |
| 185 | return (NULL); |
| 186 | return (ap); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return (NULL); |
| 191 | } |
| 192 | |
| 193 | static int |
| 194 | comparealiases(const void *p1, const void *p2) |
| 195 | { |
| 196 | const struct alias *const *a1 = p1; |
| 197 | const struct alias *const *a2 = p2; |
| 198 | |
| 199 | return strcmp((*a1)->name, (*a2)->name); |
| 200 | } |
| 201 | |
| 202 | static void |
| 203 | printalias(const struct alias *a) |
| 204 | { |
| 205 | char *p; |
| 206 | |
| 207 | out1fmt("%s=", a->name); |
| 208 | /* Don't print the space added above. */ |
| 209 | p = a->val + strlen(a->val) - 1; |
| 210 | *p = '\0'; |
| 211 | out1qstr(a->val); |
| 212 | *p = ' '; |
| 213 | out1c('\n'); |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | printaliases(void) |
| 218 | { |
| 219 | int i, j; |
| 220 | struct alias **sorted, *ap; |
| 221 | |
| 222 | sorted = ckmalloc(aliases * sizeof(*sorted)); |
| 223 | j = 0; |
| 224 | for (i = 0; i < ATABSIZE; i++) |
| 225 | for (ap = atab[i]; ap; ap = ap->next) |
| 226 | if (*ap->name != '\0') |
| 227 | sorted[j++] = ap; |
| 228 | qsort(sorted, aliases, sizeof(*sorted), comparealiases); |
| 229 | for (i = 0; i < aliases; i++) |
| 230 | printalias(sorted[i]); |
| 231 | ckfree(sorted); |
| 232 | } |
| 233 | |
| 234 | int |
| 235 | aliascmd(int argc, char **argv) |
| 236 | { |
| 237 | char *n, *v; |
| 238 | int ret = 0; |
| 239 | struct alias *ap; |
| 240 | |
| 241 | if (argc == 1) { |
| 242 | printaliases(); |
| 243 | return (0); |
| 244 | } |
| 245 | while ((n = *++argv) != NULL) { |
| 246 | if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */ |
| 247 | if ((ap = lookupalias(n, 0)) == NULL) { |
| 248 | warning("%s: not found", n); |
| 249 | ret = 1; |
| 250 | } else |
| 251 | printalias(ap); |
| 252 | else { |
| 253 | *v++ = '\0'; |
| 254 | setalias(n, v); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return (ret); |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | unaliascmd(int argc __unused, char **argv __unused) |
| 263 | { |
| 264 | int i; |
| 265 | |
| 266 | while ((i = nextopt("a")) != '\0') { |
| 267 | if (i == 'a') { |
| 268 | rmaliases(); |
| 269 | return (0); |
| 270 | } |
| 271 | } |
| 272 | for (i = 0; *argptr; argptr++) |
| 273 | i |= unalias(*argptr); |
| 274 | |
| 275 | return (i); |
| 276 | } |
| 277 | |
| 278 | static struct alias ** |
| 279 | hashalias(const char *p) |
| 280 | { |
| 281 | unsigned int hashval; |
| 282 | |
| 283 | hashval = *p << 4; |
| 284 | while (*p) |
| 285 | hashval+= *p++; |
| 286 | return &atab[hashval % ATABSIZE]; |
| 287 | } |