From d9b3d4f234b660d325a4d01479590bced47c9ec4 Mon Sep 17 00:00:00 2001 From: czjstmax Date: Thu, 19 Mar 2026 17:47:36 +0100 Subject: using GNU readline for history+arrow key handling because i hate `` Signed-off-by: czjstmax --- Makefile | 2 +- nm.c | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 88c9e3a..8140274 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = gcc IN = nm.c OUT = nshm -LIBS = +LIBS = -lreadline OPTS = -O0 -g -Wall -Wextra -pedantic all: diff --git a/nm.c b/nm.c index 7d1cf78..47a8215 100644 --- a/nm.c +++ b/nm.c @@ -14,32 +14,44 @@ #include #include #include +#include +#include void handlecc(int sig); -void FreeAll(int n); +void FreeAll(); char *line = NULL; -size_t len = 0; wordexp_t p; int main(void) { signal(SIGINT, handlecc); + char *USER = getenv("USER"); while (true) { - printf("$ "); - getline(&line, &len, stdin); + char prompt[strlen(USER) + strlen("$ ") + 1]; + snprintf(prompt, sizeof(prompt), "%s$ ", USER); - line[strcspn(line, "\n")] = '\0'; + line = readline(prompt); - wordexp(line, &p, 0); + if (!line) + break; + if (*line) + add_history(line); - if (strlen(line) == 0) { - FreeAll(1); + if (wordexp(line, &p, 0) != 0) { + fprintf(stderr, "nsh+! parse error\n"); + free(line); + continue; + } + + // built-ins + if (p.we_wordc == 0) { + FreeAll(); continue; } if (strcmp(p.we_wordv[0], ":q") == 0 || strcmp(p.we_wordv[0], "exit") == 0) { - FreeAll(0); + FreeAll(); return 0; } else if (strcmp(p.we_wordv[0], "echo") == 0) { @@ -54,7 +66,7 @@ int main(void) else if (strcmp(p.we_wordv[0], "cd") == 0) { if (p.we_wordc <= 1) { printf("nsh+! cd: no arguments were given.\n"); - FreeAll(1); + FreeAll(); continue; } if (chdir(p.we_wordv[1])) @@ -84,7 +96,7 @@ int main(void) if (pid == 0) { execvp(p.we_wordv[0], argv); perror("nsh+! ERROR: failed to execvp()"); - FreeAll(0); + FreeAll(); _exit(1); } else if (pid > 0) { @@ -101,13 +113,14 @@ int main(void) void handlecc(int sig) { printf("\nnsh+! %d caught.\nfreeing stuff before quitting.\n", sig); - FreeAll(0); + FreeAll(); exit(0); } -void FreeAll(int n) +void FreeAll() { wordfree(&p); - if (n == 0) - free(line); + free(line); + line = NULL; } + -- cgit v1.3.1