diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/src/main.c b/src/main.c index c01ee8a..9f81a83 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "headers/nstdo.h" + extern long sys_read(int fd, void *buf, size_t cnt); extern long sys_write(int fd, const void *buf, size_t cnt); extern long sys_fork(void); @@ -9,7 +10,8 @@ extern void sys_exit(int status); static int streq(const char *a, const char *b) { while (*a && *b) { if (*a != *b) return 0; - a++; b++; + a++; + b++; } return *a == *b; } @@ -17,30 +19,91 @@ static int streq(const char *a, const char *b) { static long str_len(const char *s) { long len = 0; while (s[len]) len++; - return len; + return len + 1; // Include the null terminator in the length +} + +static void fancyMessage(void) { + const char *fM1 = "+-------------------------------------------------+\n"; + const char *fM2 = "| |\n"; + const char *fM3 = "| Welcome to the Mini Shell! |\n"; + const char *fM4 = "| |\n"; + const char *fM5 = "| Type 'help' to view all 16 commands. |\n"; + const char *fM6 = "| Type 'exit' to leave the shell. |\n"; + const char *fM7 = "| |\n"; + const char *fM8 = "| Have a productive coding session! |\n"; + const char *fM9 = "| |\n"; + const char *fM0 = "+-------------------------------------------------+\n"; + + sys_write(1, fM1, str_len(fM1)); + sys_write(1, fM2, str_len(fM2)); + sys_write(1, fM3, str_len(fM3)); + sys_write(1, fM4, str_len(fM4)); + sys_write(1, fM5, str_len(fM5)); + sys_write(1, fM6, str_len(fM6)); + sys_write(1, fM7, str_len(fM7)); + sys_write(1, fM8, str_len(fM8)); + sys_write(1, fM9, str_len(fM9)); + sys_write(1, fM0, str_len(fM0)); + sys_write(1, "\n", 1); + sys_write(1, "\n", 1); +} + +static void helpCommand(void) { + const char *helpText[] = { + "Available commands:\n\n", + "\tcd \t\tChange the current directory to \n", + "\tls [-l] [dir]\t\tList the contents of the directory [dir] (default: current directory), with an optional long format (-l)\n", + "\thistory\t\t\tDisplay the command history\n", + "\thelp\t\t\tShow this help message\n", + "\tpwd\t\t\tPrint the current working directory\n", + "\tclear\t\t\tClear the terminal screen\n", + "\ttouch \tCreate an empty file named \n", + "\tcat \t\tDisplay the contents of \n", + "\trm \t\tRemove the specified file\n", + "\tmkdir \t\tCreate a directory named \n", + "\trmdir \t\tRemove a directory named \n", + "\techo \t\tDisplay the text \n", + "\tman \t\tDisplay help for the specified command (e.g., 'man ls')\n", + "\tcp \tCopy a file from to \n", + "\tmv \tMove or rename a file from to \n", + "\thead \t\tDisplay the first 10 lines of \n", + "\tnano \t\tEdit in a simple text editor\n", + "\nFor more detailed information on a specific command, use 'man '.\n" + }; + + for (size_t i = 0; i < sizeof(helpText) / sizeof(helpText[0]); i++) { + sys_write(1, helpText[i], str_len(helpText[i])); + } } int main(int argc, char *argv[], char *envp[]) { - (void)argc; (void)argv; - const char prompt[] = "$ "; + (void)argc; + (void)argv; + + const char prompt[] = "lala@mini-shell> "; char buf[512]; + + fancyMessage(); + while (1) { - sys_write(1, prompt, sizeof(prompt)-1); - long n = sys_read(0, buf, sizeof(buf)-1); + sys_write(1, prompt, sizeof(prompt) - 1); + long n = sys_read(0, buf, sizeof(buf) - 1); if (n <= 0) break; - buf[n-1] = '\0'; + + buf[n - 1] = '\0'; + if (streq(buf, "help")) { - const char *h1 = " help - display this help message\n"; - const char *h2 = " exit - exit the shell\n"; - sys_write(1, h1, str_len(h1)); - sys_write(1, h2, str_len(h2)); + helpCommand(); continue; } + if (streq(buf, "exit")) { break; } + char *args[] = { buf, NULL }; long pid = sys_fork(); + if (pid == 0) { sys_execve(buf, args, envp); sys_exit(1); @@ -48,5 +111,6 @@ int main(int argc, char *argv[], char *envp[]) { sys_wait4(pid, 0, 0, 0); } } + return 0; }