]> Trent Huber's Code - thus.git/commitdiff
Introduce unalias builtin
authorTrent Huber <trentmhuber@gmail.com>
Wed, 5 Nov 2025 06:24:54 +0000 (01:24 -0500)
committerTrent Huber <trentmhuber@gmail.com>
Wed, 5 Nov 2025 06:24:54 +0000 (01:24 -0500)
src/builtins/alias.c
src/builtins/alias.h
src/builtins/unalias.c [new file with mode: 0644]
src/builtins/which.c

index 4bc3f49ea5872a78367dfa9f3bce39ffc5b260f7..2eb2692ec440e090c0d73c2225a0139523afd1ea 100644 (file)
 #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:
index fc6edcbd8402e92014760fa9dd61271fc97a1a24..7f2411f3f030716ab4b695d29caa1eb52db803b6 100644 (file)
@@ -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 (file)
index 0000000..3024cbc
--- /dev/null
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+
+#include "alias.h"
+#include "builtin.h"
+
+BUILTIN(unalias) {
+   if (argc != 2) return usage(argv[0], "lhs");
+
+   return removealias(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
index 493c75f111c5995917b2efc6f2c15fb6c9af41f7..80a774be2bb5da539ff4fa1bfec29254548c96c1 100644 (file)
@@ -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;
        }