Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Apr 16, 2022
1 parent 409215d commit ebbb7d6
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apps/system/sys_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static int app_read(int block_no, char* dst) {
}

static int app_spawn(struct proc_request *req) {
int bin_ino = dir_lookup(0, "bin");
int bin_ino = dir_lookup(0, "bin/");
if ((app_ino = dir_lookup(bin_ino, req->argv[0])) < 0) return -1;

app_pid = grass->proc_alloc();
Expand Down
25 changes: 7 additions & 18 deletions apps/system/sys_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,32 @@ void parse_request(char* buf, struct proc_request* req) {

int main() {
SUCCESS("Enter kernel process GPID_SHELL");

char work_dir[] = "/home/yunhao";
strcpy(grass->work_dir_name, "yunhao");

int home_ino = dir_lookup(0, "home");
grass->work_dir_ino = dir_lookup(home_ino, "yunhao");
INFO("sys_shell: %s has ino=%d", work_dir, grass->work_dir_ino);
CRITICAL("Welcome to egos-2000!");

/* Wait for shell commands */
char buf[256] = "cd"; /* Enter the home directory first */
while (1) {
printf("%s➜ %s%s%s ", "\x1B[1;32m", "\x1B[1;36m", grass->work_dir_name, "\x1B[1;0m");
char buf[256];
if (earth->tty_read(buf, 256) == 0) continue;

struct proc_request req;
struct proc_reply reply;

if (strcmp(buf, "pwd") == 0) {
printf("%s\r\n", work_dir);
} else if (strcmp(buf, "clear") == 0) {
if (strcmp(buf, "clear") == 0) {
printf("\e[1;1H\e[2J");
} else if (strcmp(buf, "killall") == 0) {
req.type = PROC_KILLALL;
grass->sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
} else {
int sender;
req.type = PROC_SPAWN;
parse_request(buf, &req);
grass->sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
grass->sys_recv(&sender, (void*)&reply, sizeof(reply));
grass->sys_recv(NULL, (void*)&reply, sizeof(reply));

if (reply.type != CMD_OK)
INFO("sys_shell: command causes an error");
else if (req.argv[req.argc - 1][0] != '&')
/* Wait for foreground process */
grass->sys_recv(&sender, (void*)&reply, sizeof(reply));
grass->sys_recv(NULL, (void*)&reply, sizeof(reply));
}

printf("%s➜ %s%s%s ", "\x1B[1;32m", "\x1B[1;36m", grass->workdir, "\x1B[1;0m");
while (earth->tty_read(buf, 256) == 0);
}
}
2 changes: 1 addition & 1 deletion apps/user/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char** argv) {
}

/* Get the inode number of the file */
int file_ino = dir_lookup(grass->work_dir_ino, argv[1]);
int file_ino = dir_lookup(grass->workdir_ino, argv[1]);
if (file_ino < 0) {
INFO("cat: file %s not found", argv[1]);
return -1;
Expand Down
41 changes: 41 additions & 0 deletions apps/user/cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* (C) 2022, Cornell University
* All rights reserved.
*/

/* Author: Yunhao Zhang
* Description: a simple cd
*/

#include "app.h"
#include <string.h>

int main(int argc, char** argv) {
if (argc == 1) {
int home_ino = dir_lookup(0, "home/");
grass->workdir_ino = dir_lookup(home_ino, "yunhao/");
strcpy(grass->workdir, "/home/yunhao");
return 0;
}

strcat(argv[1], "/");
int dir_ino = dir_lookup(grass->workdir_ino, argv[1]);
if (dir_ino == -1) {
INFO("cd: directory %s not found", argv[1]);
return -1;
}

grass->workdir_ino = dir_ino;
if (!strcmp(".", argv[1])) return 0;

int len = strlen(grass->workdir);
if (strcmp("..", argv[1])) {
if (len > 1) strcat(grass->workdir, "/");
strncat(grass->workdir, argv[1], strlen(argv[1]));
} else {
while (grass->workdir[len] != '/') grass->workdir[len--] = 0;
if (len) grass->workdir[len] = 0;
}

return 0;
}
2 changes: 1 addition & 1 deletion apps/user/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char** argv) {
}

char buf[BLOCK_SIZE];
file_read(grass->work_dir_ino, 0, buf);
file_read(grass->workdir_ino, 0, buf);

/* Remove the inode numbers from the string */
for (int i = 1; i < strlen(buf); i++)
Expand Down
15 changes: 15 additions & 0 deletions apps/user/pwd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* (C) 2022, Cornell University
* All rights reserved.
*/

/* Author: Yunhao Zhang
* Description: a simple pwd
*/

#include "app.h"

int main(int argc, char** argv) {
printf("%s\r\n", grass->workdir);
return 0;
}
2 changes: 1 addition & 1 deletion grass/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int sys_recv(int* sender, char* buf, int size) {
sc->type = SYS_RECV;
sys_invoke();
memcpy(buf, sc->msg.content, size);
*sender = sc->msg.sender;
if (sender) *sender = sc->msg.sender;
return sc->retval;
}

Expand Down
5 changes: 2 additions & 3 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct earth {
int (*tty_critical)(const char *format, ...);
};

#define ENV_DIR_LEN 32
struct grass {
/* Process control interface */
int (*proc_alloc)();
Expand All @@ -39,8 +38,8 @@ struct grass {
int (*sys_recv)(int* pid, char* buf, int size);

/* Shell environment variables */
int work_dir_ino;
char work_dir_name[ENV_DIR_LEN];
int workdir_ino;
char workdir[128];
};

extern struct earth *earth;
Expand Down
17 changes: 10 additions & 7 deletions tools/mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ char* kernel_processes[] = {
#0: / #1: /home #2: /home/yunhao
#3: /home/rvr #4: /home/yunhao/README #5: /bin
#6: /bin/echo #7: /bin/ls #8: /bin/cat
#9: /bin/clock #10: /bin/crash1 #11: /bin/crash2
#9: /bin/clock #10: /bin/cd #11:/bin/pwd
#12: /bin/crash1 #13: /bin/crash2
*/
#define NINODE 12
#define NINODE 14
char* contents[] = {
". 0 .. 0 home 1 bin 5 ",
". 1 .. 0 yunhao 2 rvr 3 ",
". 2 .. 1 README 4 ",
". 3 .. 1 ",
"./ 0 ../ 0 home/ 1 bin/ 5 ",
"./ 1 ../ 0 yunhao/ 2 rvr/ 3 ",
"./ 2 ../ 1 README 4 ",
"./ 3 ../ 1 ",
"With only 2K lines of code, egos-2000 implements boot loader, microSD driver, tty driver, memory paging, address translation, interrupt handling, process scheduling and messaging, system call, file system, shell, 7 user commands and the `mkfs/mkrom` tools.",
". 5 .. 0 echo 6 ls 7 cat 8 clock 9 crash1 10 crash2 11 ",
"./ 5 ../ 0 echo 6 ls 7 cat 8 clock 9 cd 10 pwd 11 crash1 12 crash2 13 ",
"#../build/release/echo.elf",
"#../build/release/ls.elf",
"#../build/release/cat.elf",
"#../build/release/clock.elf",
"#../build/release/cd.elf",
"#../build/release/pwd.elf",
"#../build/release/crash1.elf",
"#../build/release/crash2.elf",
};
Expand Down

0 comments on commit ebbb7d6

Please sign in to comment.