diff --git a/apps/filereader/main.c b/apps/filereader/main.c index f0826f73..b7aff88a 100644 --- a/apps/filereader/main.c +++ b/apps/filereader/main.c @@ -1,17 +1,42 @@ - #include #include + +static bool isprint(char c) { + return c >= 0x20 && c <= 0x7e; +} + int main(int argc, const char **argv) { if (argc != 2) { printf("Usage: %s filename\n", argv[0]); return 1; } size_t size = __syscall(SYSCALL_FILE_SIZE, argv[1]); - byte *buff = (byte *)xmalloc(size); - __syscall(SYSCALL_LOAD_FILE, argv[1], buff, size); - for (int i = 0; i < size; i++) { - printf("%c", buff[i]); + byte *buf = (byte *)xmalloc(size); + __syscall(SYSCALL_LOAD_FILE, argv[1], buf, size); + printf("\033[1;35mHEX DUMP\033[0m "); + for (usize j = 0; j < 16; j++) { + printf("\033[1;37m%02x\033[0m ", j); + } + printf(" \033[1;36mCHAR\033[0m\n"); + for (usize i = 0; i < size; i += 16) { + printf("\033[1;33m%08x\033[0m ", i); + for (usize j = 0; j < 16; j++) { + if (i + j < size) { + printf("%02x ", buf[i + j]); + } else { + printf(" "); + } + } + printf(" "); + for (usize j = 0; j < 16; j++) { + if (i + j >= size) break; + if (isprint(buf[i + j])) + printf("\033[1;32m%c\033[0m", buf[i + j]); + else + printf("\033[1;30m.\033[0m"); + } + printf("\n"); } - printf("\n"); + free(buf); return 0; } \ No newline at end of file diff --git a/src/kernel/io/logging/default.debug.h b/src/kernel/io/logging/default.debug.h index cb9bf546..6f6e7da8 100644 --- a/src/kernel/io/logging/default.debug.h +++ b/src/kernel/io/logging/default.debug.h @@ -315,19 +315,23 @@ int printf(cstr _rest fmt, ...) { int rets = vsprintf(print_buf, fmt, va); va_end(va); print(print_buf); +#if PLOS_LOGGING_PRINTS size_t len = strlen(print_buf); while (len > 0 && print_buf[len - 1] == '\n') { print_buf[len - 1] = '\0'; len--; } klogi("print: %s", print_buf); +#endif return rets; } void puts(cstr s) { print(s); print("\n"); +#if PLOS_LOGGING_PRINTS klogi("print: %s", s); +#endif } static void _debugger_tick() { diff --git a/src/kernel/io/logging/default.h b/src/kernel/io/logging/default.h index 8158c44d..d4ab633d 100644 --- a/src/kernel/io/logging/default.h +++ b/src/kernel/io/logging/default.h @@ -72,19 +72,23 @@ int printf(cstr _rest fmt, ...) { int rets = vsprintf(print_buf, fmt, va); va_end(va); print(print_buf); +#if PLOS_LOGGING_PRINTS size_t len = strlen(print_buf); while (len > 0 && print_buf[len - 1] == '\n') { print_buf[len - 1] = '\0'; len--; } klogi("print: %s", print_buf); +#endif return rets; } void puts(cstr s) { print(s); print("\n"); +#if PLOS_LOGGING_PRINTS klogi("print: %s", s); +#endif } void kenel_debugger_tick() { diff --git a/src/kernel/io/logging/ext.h b/src/kernel/io/logging/ext.h index 94eaea92..3b302ee7 100644 --- a/src/kernel/io/logging/ext.h +++ b/src/kernel/io/logging/ext.h @@ -142,17 +142,21 @@ int printf(cstr _rest fmt, ...) { int rets = vsprintf(print_buf, fmt, va); va_end(va); print(print_buf); +#if PLOS_LOGGING_PRINTS size_t len = strlen(print_buf); while (len > 0 && print_buf[len - 1] == '\n') { print_buf[len - 1] = '\0'; len--; } klogi("print: %s", print_buf); +#endif return rets; } void puts(cstr s) { print(s); print("\n"); +#if PLOS_LOGGING_PRINTS klogi("print: %s", s); +#endif } diff --git a/src/kernel/task/shell.c b/src/kernel/task/shell.c index 452ab3ca..ad83954a 100644 --- a/src/kernel/task/shell.c +++ b/src/kernel/task/shell.c @@ -109,6 +109,54 @@ extern void plac_player(cstr path); extern void qoa_player(cstr path); extern void mp3_player(cstr path); +static bool isprint(char c) { + return c >= 0x20 && c <= 0x7e; +} + +static int print_file(cstr path) { + vfs_node_t file = vfs_open(path); + if (file == null) { + printf("open %s failed\n", path); + return 1; + } + if (file->type != file_block) { + printf("not a file\n"); + return 1; + } + if (file->size > 1024 * 1024) { + printf("file too large\n"); + return 1; + } + byte *buf = malloc(file->size); + vfs_read(file, buf, 0, file->size); + printf("\033[1;35mHEX DUMP\033[0m "); + for (usize j = 0; j < 16; j++) { + printf("\033[1;37m%02x\033[0m ", j); + } + printf(" \033[1;36mCHAR\033[0m\n"); + for (usize i = 0; i < file->size; i += 16) { + printf("\033[1;33m%08x\033[0m ", i); + for (usize j = 0; j < 16; j++) { + if (i + j < file->size) { + printf("%02x ", buf[i + j]); + } else { + printf(" "); + } + } + printf(" "); + for (usize j = 0; j < 16; j++) { + if (i + j >= file->size) break; + if (isprint(buf[i + j])) + printf("\033[1;32m%c\033[0m", buf[i + j]); + else + printf("\033[1;30m.\033[0m"); + } + printf("\n"); + } + free(buf); + return 0; +} + int shell_exec(char *path, cstr comand) { if (!strlen(comand)) return 0; int retcode = 0; @@ -153,37 +201,14 @@ int shell_exec(char *path, cstr comand) { vfs_node_t stdout = vfs_open("/dev/stdout"); vfs_write(stdout, "Hello, world!\n", 0, 14); } else if (strneq(comand, "file ", 5)) { - cstr path = comand + 5; - cstr type = filetype_from_name(path); + cstr type = filetype_from_name(comand + 5); if (type) { printf("%s\n", type); } else { printf("unknown file\n"); } } else if (strneq(comand, "read ", 5)) { - char *s = comand + 5; - vfs_node_t p = vfs_open(s); - if (!p) { - printf("open %s failed\n", s); - return 1; - } - if (p->type == file_dir) { - printf("not a file\n"); - return 1; - } - size_t size = p->size; - byte *buf = malloc(1024); - size_t pos = 0; - while (1) { - size_t len = vfs_read(p, buf, pos, 1024); - if (len == 0) break; - for (int i = 0; i < len; i++) { - printf("%02x ", buf[i]); - if ((i + 1) % 32 == 0) printf("\n"); - } - pos += len; - } - free(buf); + return print_file(comand + 5); } else if (strneq(comand, "mkdir ", 6)) { vfs_mkdir(comand + 6); } else if (strneq(comand, "mount ", 6)) {