From: Trent Huber Date: Wed, 5 Nov 2025 06:24:54 +0000 (-0500) Subject: Introduce unalias builtin X-Git-Url: https://trenthuber.com/code?a=commitdiff_plain;h=964fb0ce0badae9d97da38e56c93c619a26d0169;p=thus.git Introduce unalias builtin --- diff --git a/src/builtins/alias.c b/src/builtins/alias.c index 4bc3f49..2eb2692 100644 --- a/src/builtins/alias.c +++ b/src/builtins/alias.c @@ -10,27 +10,34 @@ #define MAXALIAS 25 static struct { - struct { + struct entry { char lhs[MAXCHARS - 5], *rhs; } entries[MAXALIAS + 1]; size_t size; } aliases; -char *getrawalias(char *token) { +static size_t getindex(char *lhs) { size_t i; for (i = 0; i < aliases.size; ++i) - if (strcmp(token, aliases.entries[i].lhs) == 0) return aliases.entries[i].rhs; + if (strcmp(aliases.entries[i].lhs, lhs) == 0) break; - return NULL; + return i; } -char **getalias(char *token) { +char *getaliasrhs(char *lhs) { + size_t i; + + if ((i = getindex(lhs)) == aliases.size) return NULL; + return aliases.entries[i].rhs; +} + +char **getalias(char *lhs) { char *rhs; size_t l; static struct context context; - if (!(rhs = getrawalias(token))) return NULL; + if (!(rhs = getaliasrhs(lhs))) return NULL; while (*rhs == ' ') ++rhs; strcpy(context.buffer, rhs); @@ -46,9 +53,22 @@ char **getalias(char *token) { return context.tokens; } +int removealias(char *lhs) { + size_t i; + struct entry *entry; + + if ((i = getindex(lhs)) == aliases.size) return 0; + entry = &aliases.entries[i]; + memmove(entry, entry + 1, (--aliases.size - i) * sizeof(*entry)); + for (; i < aliases.size; ++i, ++entry) + entry->rhs = (void *)entry->rhs - sizeof(*entry); + + return 1; +} + BUILTIN(alias) { size_t i; - char *lhs; + struct entry *entry; switch (argc) { case 1: @@ -60,15 +80,13 @@ BUILTIN(alias) { note("Unable to add alias `%s', maximum reached (%d)", argv[1], MAXALIAS); return EXIT_FAILURE; } - for (i = 0; i < aliases.size; ++i) - if (strcmp(aliases.entries[i].lhs, argv[1]) == 0) break; - lhs = aliases.entries[i].lhs; + entry = &aliases.entries[i = getindex(argv[1])]; if (i == aliases.size) { - strcpy(lhs, argv[1]); + strcpy(entry->lhs, argv[1]); ++aliases.size; } - strcpy(aliases.entries[i].rhs = lhs + strlen(lhs) + 1, argv[2]); + strcpy(entry->rhs = entry->lhs + strlen(entry->lhs) + 1, argv[2]); break; default: diff --git a/src/builtins/alias.h b/src/builtins/alias.h index fc6edcb..7f2411f 100644 --- a/src/builtins/alias.h +++ b/src/builtins/alias.h @@ -1,2 +1,3 @@ -char *getrawalias(char *token); -char **getalias(char *token); +char *getaliasrhs(char *lhs); +char **getalias(char *lhs); +int removealias(char *lhs); diff --git a/src/builtins/unalias.c b/src/builtins/unalias.c new file mode 100644 index 0000000..3024cbc --- /dev/null +++ b/src/builtins/unalias.c @@ -0,0 +1,10 @@ +#include + +#include "alias.h" +#include "builtin.h" + +BUILTIN(unalias) { + if (argc != 2) return usage(argv[0], "lhs"); + + return removealias(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/builtins/which.c b/src/builtins/which.c index 493c75f..80a774b 100644 --- a/src/builtins/which.c +++ b/src/builtins/which.c @@ -39,7 +39,7 @@ char *getpath(char *file) { type = PATH; if (!(slash = strchr(file, '/'))) { - if ((entry = getrawalias(file))) { + if ((entry = getaliasrhs(file))) { type = ALIAS; return entry; }