diff options
| author | czjstmax <jstmaxlol@disroot.org> | 2026-04-10 17:07:16 +0200 |
|---|---|---|
| committer | czjstmax <jstmaxlol@disroot.org> | 2026-04-10 17:07:16 +0200 |
| commit | accd3b2d959bfc0e8a8aa0f148ebef5871529f47 (patch) | |
| tree | 763f56809be1d8f1e5c29a0e18f338cb71b8a232 | |
| parent | dc391b4360aa8bda3df80a781a0e15cf1ca1d69f (diff) | |
release 2.2 -- fixes, 'betterifications' and reworked `echo`
Signed-off-by: czjstmax <jstmaxlol@disroot.org>
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | nm.c | 102 |
2 files changed, 67 insertions, 39 deletions
@@ -2,8 +2,8 @@ CC = gcc IN = nm.c OUT = nshm LIBS = -lreadline -OPTS = -O2 -Wall -Wextra -pedantic -OPTSDBG = -O0 -g -Wall -Wextra -pedantic +OPTS = -O2 -Wall -Wextra -pedantic -std=c99 +OPTSDBG = -O0 -g -Wall -Wextra -pedantic -std=c99 debug: $(CC) -o$(OUT) $(LIBS) $(OPTSDBG) $(IN) @@ -1,16 +1,23 @@ -/* noshmore +/* + * # noshmore * an extended version of nosh, - * the minimal POSIX C shell. + * the minimal POSIX C99 shell. * * # notable changes - * - less memory leaks (almost none) + * - less memory leaks * - more stable - * - cleaner code (ish) - * - GNU readline + * - cleaner code + * - uses GNU readline * - command history - * and more fixes and tweaks. + * and other fixes and tweaks. * - * von czjstmax, <jstmaxlol at disroot dot org> */ + * von czjstmax, <jstmaxlol at disroot dot org> + */ + +#define _POSIX_C_SOURCE 200809L + +#define NOSHMORE_VERSION 2.2 +#define MAX_ALIASES 256 #include <stdio.h> #include <stdlib.h> @@ -23,9 +30,6 @@ #include <readline/readline.h> #include <readline/history.h> -#define NOSHMORE_VERSION 2.1 -#define MAX_ALIASES 256 - typedef struct { char *dst; char *src; @@ -38,11 +42,11 @@ char *line = NULL; char *history_path; wordexp_t p; -const char *RED = "\x1b[31m"; -const char *WHITE = "\x1b[37m"; -const char *RESET = "\x1b[0m"; -const char *ITALIC = "\x1b[3m"; -const char *UNDERLINE = "\x1b[4m"; +static const char *RED = "\x1b[31m"; +static const char *WHITE = "\x1b[37m"; +static const char *ITALIC = "\x1b[3m"; +static const char *UNDERLINE = "\x1b[4m"; +static const char *RESET = "\x1b[0m"; void HandleCC (int sig); Alias CreateAlias (char *dst, char *src); @@ -53,19 +57,21 @@ void Cleanup (); void Greet() { printf( - "welcome to %s%snoshmore%s | spinning version %s%s%.1f%s\n" + "welcome to %s%snoshmore%s | spinning version %s%s%s%.1f%s\n" "type %s%shelp%s for a list of %sbuilt-in%s commands!\n" - , RED, ITALIC, RESET, RED, ITALIC, NOSHMORE_VERSION, RESET + , RED, ITALIC, RESET, UNDERLINE, RED, ITALIC, NOSHMORE_VERSION, RESET , RED, ITALIC, RESET, ITALIC, RESET ); } int main(void) { + // phase 0 - setup atexit(Cleanup); signal(SIGINT, HandleCC); using_history(); + // phase 1 - runtime initialization char *USER = getenv("USER"); if (!USER) USER = "anon"; @@ -85,13 +91,21 @@ int main(void) fprintf(stderr, "(%sfirst run? try the %swritehist%s%s command%s)\n", ITALIC, RED, RESET, ITALIC, RESET); } + char prompt[strlen(USER) + strlen("$ ") + 1]; + snprintf(prompt, sizeof(prompt), "%s$ ", USER); + + // phase 2 - shell aliases creation + //aliases[alias_count++] = CreateAlias("NOSH_HIST", history_path); + /* + * note: this is broken because the aliasing expansion system sucks. + * once ill have reworked the aliasing expansion system and probably + * made a tokenizer for it this will come back, for now, get //'d + */ + // initialization phase completed, now Greet() the USER Greet(); while (true) { - char prompt[strlen(USER) + strlen("$ ") + 1]; - snprintf(prompt, sizeof(prompt), "%s$ ", USER); - line = readline(prompt); if (!line) @@ -102,7 +116,7 @@ int main(void) } if (wordexp(line, &p, 0) != 0) { - perror("nsh+! ERROR while parsing input\n"); + fprintf(stderr, "nsh+! ERROR while parsing input\n"); FreeAll(); continue; } @@ -113,17 +127,28 @@ int main(void) continue; } if (strcmp(p.we_wordv[0], ":q") == 0 || strcmp(p.we_wordv[0], "exit") == 0) { - //Cleanup(); + //Cleanup(); -- taken care of by atexit() return 0; } - else if (strcmp(p.we_wordv[0], "echo") == 0) { - for (size_t i = 1; i < p.we_wordc; i++) - printf("%s ", p.we_wordv[i]); - if (p.we_wordc >= 2) { - if (strcmp(p.we_wordv[1], "-c") != 0) - printf("\n"); - else continue; + else if (strcmp(p.we_wordv[0], "echo") == 0 || strcmp(p.we_wordv[0], "@") == 0) { + size_t start = 1; + bool no_newline = false; + + if (p.we_wordc >= 2 && (strcmp(p.we_wordv[1], "-c") == 0 || strcmp(p.we_wordv[1], "--clean") == 0)) { + no_newline = true; + start = 2; } + + for (size_t i = start; i < p.we_wordc; ++i) { + printf("%s", p.we_wordv[i]); + if (i < p.we_wordc - 1) + printf(" "); + } + + if (!no_newline) + printf("\n"); + + FreeAll(); } else if (strcmp(p.we_wordv[0], "alias") == 0) { if (p.we_wordc <= 2) { @@ -180,6 +205,7 @@ int main(void) "%snoshmore%s\tVERSION(%.1f)\tALIASES(%d)\n" "an extended version of %snosh%s, the minimal POSIX C shell.\n" "history file path -> '%s%s%s%s'\n" + //"note: you can access this file by the %sNOSH_HIST%s alias\n" "\n" "## built-ins:\n" "echo -> outputs a string of text, doesn't need \" enclosure.\n" @@ -199,10 +225,10 @@ int main(void) , RED, RESET, NOSHMORE_VERSION, MAX_ALIASES , ITALIC, RESET , RED, ITALIC, history_path, RESET + //, RED, RESET , ITALIC, RESET /* make no mistakes: i know i can just #define RED_FMT(s) RED s RESET - and make this printf() 10x more readable and lovely, but i wont. - */ + and make this printf() 10x more readable and lovely, but i wont. */ ); FreeAll(); } @@ -226,11 +252,12 @@ int main(void) break; } } + pid_t pid = fork(); - char **argv = p.we_wordv; // pray with me now + //char **argv = p.we_wordv; // pray with me now if (pid == 0) { - execvp(p.we_wordv[0], argv); - perror("nsh+! ERROR: failed to execvp()"); + execvp(p.we_wordv[0], p.we_wordv); + perror("nsh+! ERROR"); // let errno do the work FreeAll(); free(history_path); _exit(1); @@ -240,7 +267,7 @@ int main(void) waitpid(pid, &status, 0); } else { - fprintf(stderr, "nsh+! ERROR: %s not found\n", p.we_wordv[0]); + fprintf(stderr, "nsh+! ERROR: %s not found\n", p.we_wordv[0]); // never reached (?) } } } @@ -275,8 +302,8 @@ void FreeAliases() for (int i = 0; i < alias_count; i++) { free(aliases[i].dst); free(aliases[i].src); - } - + } + alias_count = 0; } void Cleanup() @@ -285,3 +312,4 @@ void Cleanup() FreeAliases(); free(history_path); } + |