summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorczjstmax <jstmaxlol@disroot.org>2026-04-05 04:01:52 +0200
committerczjstmax <jstmaxlol@disroot.org>2026-04-05 04:01:52 +0200
commitde10ed1d94e9d6d80fdb61394612ef83767c3910 (patch)
treefb806e22ee504a711ddfc3a6bfd85c3adcb04db0
parentdab9c9f247de874c153ceaebe21c0932792ebbb8 (diff)
added simple `alias`ing system for commands
Signed-off-by: czjstmax <jstmaxlol@disroot.org>
-rw-r--r--README.md1
-rw-r--r--nm.c80
2 files changed, 77 insertions, 4 deletions
diff --git a/README.md b/README.md
index 94c81ae..071cde5 100644
--- a/README.md
+++ b/README.md
@@ -9,3 +9,4 @@ an extended version of the [nosh minimal POSIX C shell](https://github.com/jstma
- doesn't quit on `^C`
- it also fixes some minor memory leaks and stuff from nosh.
+
diff --git a/nm.c b/nm.c
index ffe2605..2e694e8 100644
--- a/nm.c
+++ b/nm.c
@@ -17,8 +17,20 @@
#include <readline/readline.h>
#include <readline/history.h>
+#define MAX_ALIASES 256
+
+typedef struct {
+ char *dst;
+ char *src;
+} Alias;
+
+Alias aliases[MAX_ALIASES];
+int alias_count = 0;
+
void handlecc(int sig);
+Alias CreateAlias(char *dst, char *src);
void FreeAll();
+void FreeAliases();
char *line = NULL;
wordexp_t p;
@@ -28,7 +40,7 @@ int main(void)
signal(SIGINT, handlecc);
char *USER = getenv("USER");
if (!USER)
- USER = "god";
+ USER = "anon";
char *HOME = getenv("HOME");
if (!HOME)
HOME = ".";
@@ -45,7 +57,7 @@ int main(void)
add_history(line);
if (wordexp(line, &p, 0) != 0) {
- fprintf(stderr, "nsh+! parse error\n");
+ fprintf(stderr, "nsh+! ERROR while parsing\n");
free(line);
continue;
}
@@ -57,6 +69,7 @@ int main(void)
}
if (strcmp(p.we_wordv[0], ":q") == 0 || strcmp(p.we_wordv[0], "exit") == 0) {
FreeAll();
+ FreeAliases();
return 0;
}
else if (strcmp(p.we_wordv[0], "echo") == 0) {
@@ -68,6 +81,26 @@ int main(void)
else continue;
}
}
+ else if (strcmp(p.we_wordv[0], "alias") == 0) {
+ if (p.we_wordc <= 2) {
+ fprintf(stderr, "nsh+! ERROR: expected 2 args\n");
+ } else {
+ // overwrite if exists
+ bool found = false;
+ for (int i = 0; i < alias_count; i++) {
+ if (strcmp(aliases[i].dst, p.we_wordv[1]) == 0) {
+ free(aliases[i].src);
+ aliases[i].src = strdup(p.we_wordv[2]);
+ found = true;
+ break;
+ }
+ }
+
+ if (!found && alias_count < MAX_ALIASES) {
+ aliases[alias_count++] = CreateAlias(p.we_wordv[1], p.we_wordv[2]);
+ }
+ }
+ }
else if (strcmp(p.we_wordv[0], "cd") == 0) {
if (p.we_wordc <= 1) {
if (chdir(HOME))
@@ -84,19 +117,41 @@ int main(void)
}
else if (strcmp(p.we_wordv[0], "help") == 0) {
printf(
- "noshmore\n"
+ "noshmore\tALIASES(%d)\n"
"an extended version of nosh, the minimal POSIX C shell.\n"
"\n"
"built-ins:\n"
"echo -> outputs a string of text, doesn't need \" enclosure.\n"
" supports '-c' for output without '\\n'"
"clear -> clears screen (and scrollback)\n"
+ "alias -> creates non-persistent command aliases\n"
+ " (i.e. alias v vim)\n"
"cd -> change working directory\n"
"help -> prints this screen\n"
"\ngithub repo: github.com/jstmaxlol/noshmore\n"
+ , MAX_ALIASES
);
}
else {
+ for (int i = 0; i < alias_count; i++) {
+ if (strcmp(p.we_wordv[0], aliases[i].dst) == 0) {
+ char new_line[81920] = {0};
+ strcat(new_line, aliases[i].src);
+
+ for (size_t j = 1; j < p.we_wordc; j++) {
+ strcat(new_line, " ");
+ strcat(new_line, p.we_wordv[j]);
+ }
+
+ wordfree(&p);
+ if (wordexp(new_line, &p, 0) != 0) {
+ fprintf(stderr, "nsh+! ERROR: alias expansion failed\n");
+ break;
+ }
+
+ break;
+ }
+ }
pid_t pid = fork();
char **argv = p.we_wordv; // pray with me now
if (pid == 0) {
@@ -110,7 +165,7 @@ int main(void)
waitpid(pid, &status, 0);
}
else {
- fprintf(stderr, "nsh+! ERROR: couldn't execute %s\n", p.we_wordv[0]);
+ fprintf(stderr, "nsh+! ERROR: %s not found\n", p.we_wordv[0]);
}
}
}
@@ -125,6 +180,14 @@ void handlecc(int sig)
rl_redisplay();
}
+Alias CreateAlias(char *dst, char *src)
+{
+ Alias alias;
+ alias.dst = strdup(dst);
+ alias.src = strdup(src);
+ return alias;
+}
+
void FreeAll()
{
wordfree(&p);
@@ -132,3 +195,12 @@ void FreeAll()
line = NULL;
}
+void FreeAliases()
+{
+ for (int i = 0; i < alias_count; i++) {
+ free(aliases[i].dst);
+ free(aliases[i].src);
+ }
+
+}
+