hw2 done
This commit is contained in:
52
lib/shell.c
52
lib/shell.c
@@ -1,4 +1,5 @@
|
||||
#include <stddef.h>
|
||||
#include <utils.h>
|
||||
#include <string.h>
|
||||
#include <uart.h>
|
||||
#include <mbox.h>
|
||||
@@ -17,6 +18,8 @@ void help (void)
|
||||
"help : print this help menu" ENDL
|
||||
"hello : print Hello World!" ENDL
|
||||
"hwinfo: print hardware info" ENDL
|
||||
"ls : list directory contents" ENDL
|
||||
"cat : concatenate files and print" ENDL
|
||||
"reboot: reboot the device" ENDL
|
||||
);
|
||||
}
|
||||
@@ -46,6 +49,27 @@ void hwinfo (void)
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
void ls_initrd_callback(file_node_t *tr)
|
||||
{
|
||||
uart_puts(tr->filename);
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
void ls(file_node_t *root)
|
||||
{
|
||||
initrd_traverse(root, ls_initrd_callback);
|
||||
}
|
||||
|
||||
void cat(file_node_t *root, const char *filename)
|
||||
{
|
||||
file_node_t *tr = initrd_get(root, filename);
|
||||
if (tr) {
|
||||
uart_puts((char *)tr->filecontent);
|
||||
} else {
|
||||
uart_puts("FILE NOT EXIST!" ENDL);
|
||||
}
|
||||
}
|
||||
|
||||
void set(long addr, unsigned int value) {
|
||||
volatile unsigned int* point = (unsigned int*)addr;
|
||||
*point = value;
|
||||
@@ -61,7 +85,7 @@ void reboot(void)
|
||||
reset(1 << 16);
|
||||
}
|
||||
|
||||
int shell (void)
|
||||
int shell(file_node_t *initrd_root)
|
||||
{
|
||||
uart_puts("# ");
|
||||
|
||||
@@ -74,13 +98,31 @@ int shell (void)
|
||||
uart_puts(ENDL);
|
||||
buf[sz] = '\0';
|
||||
|
||||
if (strcmp(buf, "help") == 0) {
|
||||
const char *i = buf;
|
||||
|
||||
char bin[INPUT_BUFLEN], *j;
|
||||
for (j = bin; *i != '\0' && !isspace(*i); ++i, ++j)
|
||||
*j = *i;
|
||||
*j = '\0';
|
||||
|
||||
for (; *i != '\0' && isspace(*i); ++i);
|
||||
|
||||
char param[INPUT_BUFLEN];
|
||||
for (j = param; *i != '\0' && !isspace(*i); ++i, ++j)
|
||||
*j = *i;
|
||||
*j = '\0';
|
||||
|
||||
if (!strcmp(bin, "help")) {
|
||||
help();
|
||||
} else if (strcmp(buf, "hello") == 0) {
|
||||
} else if (!strcmp(bin, "hello")) {
|
||||
hello();
|
||||
} else if (strcmp(buf, "hwinfo") == 0) {
|
||||
} else if (!strcmp(bin, "hwinfo")) {
|
||||
hwinfo();
|
||||
} else if (strcmp(buf, "reboot") == 0) {
|
||||
} else if (!strcmp(bin, "ls")) {
|
||||
ls(initrd_root);
|
||||
} else if (!strcmp(bin, "cat")) {
|
||||
cat(initrd_root, param);
|
||||
} else if (!strcmp(bin, "reboot")) {
|
||||
reboot();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user