lala
This commit is contained in:
parent
cca667ebac
commit
31d1a64bba
2 changed files with 76 additions and 11 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
build/
|
||||||
86
src/main.c
86
src/main.c
|
|
@ -1,4 +1,5 @@
|
||||||
#include "headers/nstdo.h"
|
#include "headers/nstdo.h"
|
||||||
|
|
||||||
extern long sys_read(int fd, void *buf, size_t cnt);
|
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_write(int fd, const void *buf, size_t cnt);
|
||||||
extern long sys_fork(void);
|
extern long sys_fork(void);
|
||||||
|
|
@ -9,7 +10,8 @@ extern void sys_exit(int status);
|
||||||
static int streq(const char *a, const char *b) {
|
static int streq(const char *a, const char *b) {
|
||||||
while (*a && *b) {
|
while (*a && *b) {
|
||||||
if (*a != *b) return 0;
|
if (*a != *b) return 0;
|
||||||
a++; b++;
|
a++;
|
||||||
|
b++;
|
||||||
}
|
}
|
||||||
return *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) {
|
static long str_len(const char *s) {
|
||||||
long len = 0;
|
long len = 0;
|
||||||
while (s[len]) len++;
|
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 <path>\t\tChange the current directory to <path>\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 <filename>\tCreate an empty file named <filename>\n",
|
||||||
|
"\tcat <filename>\t\tDisplay the contents of <filename>\n",
|
||||||
|
"\trm <filename>\t\tRemove the specified file\n",
|
||||||
|
"\tmkdir <dir>\t\tCreate a directory named <dir>\n",
|
||||||
|
"\trmdir <dir>\t\tRemove a directory named <dir>\n",
|
||||||
|
"\techo <text>\t\tDisplay the text <text>\n",
|
||||||
|
"\tman <command>\t\tDisplay help for the specified command (e.g., 'man ls')\n",
|
||||||
|
"\tcp <source> <dest>\tCopy a file from <source> to <dest>\n",
|
||||||
|
"\tmv <source> <dest>\tMove or rename a file from <source> to <dest>\n",
|
||||||
|
"\thead <filename>\t\tDisplay the first 10 lines of <filename>\n",
|
||||||
|
"\tnano <filename>\t\tEdit <filename> in a simple text editor\n",
|
||||||
|
"\nFor more detailed information on a specific command, use 'man <command>'.\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[]) {
|
int main(int argc, char *argv[], char *envp[]) {
|
||||||
(void)argc; (void)argv;
|
(void)argc;
|
||||||
const char prompt[] = "$ ";
|
(void)argv;
|
||||||
|
|
||||||
|
const char prompt[] = "lala@mini-shell> ";
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
||||||
|
fancyMessage();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sys_write(1, prompt, sizeof(prompt)-1);
|
sys_write(1, prompt, sizeof(prompt) - 1);
|
||||||
long n = sys_read(0, buf, sizeof(buf)-1);
|
long n = sys_read(0, buf, sizeof(buf) - 1);
|
||||||
if (n <= 0) break;
|
if (n <= 0) break;
|
||||||
buf[n-1] = '\0';
|
|
||||||
|
buf[n - 1] = '\0';
|
||||||
|
|
||||||
if (streq(buf, "help")) {
|
if (streq(buf, "help")) {
|
||||||
const char *h1 = " help - display this help message\n";
|
helpCommand();
|
||||||
const char *h2 = " exit - exit the shell\n";
|
|
||||||
sys_write(1, h1, str_len(h1));
|
|
||||||
sys_write(1, h2, str_len(h2));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (streq(buf, "exit")) {
|
if (streq(buf, "exit")) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *args[] = { buf, NULL };
|
char *args[] = { buf, NULL };
|
||||||
long pid = sys_fork();
|
long pid = sys_fork();
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
sys_execve(buf, args, envp);
|
sys_execve(buf, args, envp);
|
||||||
sys_exit(1);
|
sys_exit(1);
|
||||||
|
|
@ -48,5 +111,6 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||||
sys_wait4(pid, 0, 0, 0);
|
sys_wait4(pid, 0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue