From 5314ef2cae16816b87377decf3ba6c99b6f06703 Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Tue, 31 May 2022 17:43:41 +0400 Subject: [PATCH] Implement basic variables (to hide pointers from logs) --- shell.c | 700 ++++++++++++++++++----------- shell.h | 16 + test/051_#draw_#cursor_all.ref.log | 4 + test/051_#draw_#cursor_all.ref.png | Bin 0 -> 1653 bytes test/051_#draw_#cursor_all.t | 3 + umka.asm | 32 +- umka.h | 2 +- umka.ld | 14 +- umka_shell.c | 14 +- 9 files changed, 505 insertions(+), 280 deletions(-) create mode 100644 test/051_#draw_#cursor_all.ref.png diff --git a/shell.c b/shell.c index 734d973..7537685 100644 --- a/shell.c +++ b/shell.c @@ -154,6 +154,134 @@ parse_uint64(struct shell_ctx *ctx, const char *str, uint64_t *res) { } } +static struct shell_var * +shell_var_get(struct shell_ctx *ctx, const char *name) { + for (struct shell_var *var = ctx->var; var; var = var->next) { + if (!strcmp(var->name, name)) { + return var; + } + } + return NULL; +} + +static bool +shell_parse_sint(struct shell_ctx *ctx, ssize_t *value, const char *s) { + if (s[0] == '$') { + struct shell_var *var = shell_var_get(ctx, s); + if (var) { + *value = var->value.sint; + return true; + } + } else { + *value = strtol(s, NULL, 0); + return true; + } + return false; +} + +static bool +shell_parse_uint(struct shell_ctx *ctx, size_t *value, const char *s) { + if (s[0] == '$') { + struct shell_var *var = shell_var_get(ctx, s); + if (var) { + *value = var->value.uint; + return true; + } + } else { + *value = strtoul(s, NULL, 0); + return true; + } + return false; +} + +static bool +shell_parse_ptr(struct shell_ctx *ctx, void **value, const char *s) { + if (s[0] == '$') { + struct shell_var *var = shell_var_get(ctx, s); + if (var) { + *value = var->value.ptr; + return true; + } + } else { + *value = (void*)strtoul(s, NULL, 0); + return true; + } + return false; +} + +static bool +shell_var_name(struct shell_ctx *ctx, const char *name) { + struct shell_var *var = ctx->var; + if (!var || var->name[0] != '\0') { + return false; + } + if (name[0] != '$' || strlen(name) >= SHELL_VAR_NAME_LEN) { + return false; + } + strcpy(var->name, name); + return true; +} + +struct shell_var * +shell_var_new() { + struct shell_var *var = (struct shell_var*)malloc(sizeof(struct shell_var)); + var->next = NULL; + var->name[0] = '\0'; + return var; +} + +static struct shell_var * +shell_var_add(struct shell_ctx *ctx) { + struct shell_var *var = ctx->var; + struct shell_var *new_var; + if (!var) { + new_var = shell_var_new(); + ctx->var = new_var; + } else { + if (var->name[0] == '\0') { + new_var = var; + } else { + new_var = shell_var_new(); + new_var->next = var; + ctx->var = new_var; + } + } + return new_var; +} + +static bool +shell_var_add_sint(struct shell_ctx *ctx, ssize_t value) { + struct shell_var *var = shell_var_add(ctx); + if (!var) { + return false; + } + var->type = SHELL_VAR_SINT; + var->value.sint = value; + return true; +} + +static bool +shell_var_add_uint(struct shell_ctx *ctx, size_t value) { + struct shell_var *var = shell_var_add(ctx); + if (!var) { + return false; + } + var->type = SHELL_VAR_UINT; + var->value.uint = value; + return true; +} + +static bool +shell_var_add_ptr(struct shell_ctx *ctx, void *value) { + struct shell_var *var = shell_var_add(ctx); + if (!var) { + return false; + } + var->type = SHELL_VAR_POINTER; + var->value.ptr = value; + return true; +} + static void print_bytes(struct shell_ctx *ctx, uint8_t *x, size_t len) { for (size_t i = 0; i < len; i++) { @@ -241,11 +369,25 @@ split_args(char *s, char **argv) { } static void -shell_send_scancode(struct shell_ctx *ctx, int argc, char **argv) { +cmd_var(struct shell_ctx *ctx, int argc, char **argv) { + const char *usage = \ + "usage: var <$name>\n" + " $name variable name, must start with $ sign\n"; + if (argc != 2) { + fputs(usage, ctx->fout); + return; + } + bool status = shell_var_name(ctx, argv[1]); + if (!status) { + fprintf(ctx->fout, "fail\n"); + } +} + +static void +cmd_send_scancode(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: send_scancode ...\n" " code dec or hex number\n"; - if (argc < 2) { fputs(usage, ctx->fout); return; @@ -269,7 +411,7 @@ shell_send_scancode(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_umka_init(struct shell_ctx *ctx, int argc, char **argv) { +cmd_umka_init(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: umka_init\n"; (void)argv; @@ -283,7 +425,7 @@ shell_umka_init(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_umka_set_boot_params(struct shell_ctx *ctx, int argc, char **argv) { +cmd_umka_set_boot_params(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: umka_set_boot_params [--x_res ] [--y_res ]\n" " --x_res screen width\n" @@ -314,7 +456,7 @@ shell_umka_set_boot_params(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_i40(struct shell_ctx *ctx, int argc, char **argv) { +cmd_i40(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: i40 [ebx [ecx [edx [esi [edi [ebp]]]]]]...\n" " see '/kernel/docs/sysfuncs.txt' for details\n"; @@ -360,7 +502,7 @@ disk_list_partitions(struct shell_ctx *ctx, disk_t *d) { } static void -shell_ramdisk_init(struct shell_ctx *ctx, int argc, char **argv) { +cmd_ramdisk_init(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: ramdisk_init \n" " absolute or relative path\n"; @@ -391,7 +533,7 @@ shell_ramdisk_init(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_disk_add(struct shell_ctx *ctx, int argc, char **argv) { +cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: disk_add [option]...\n" " absolute or relative path\n" @@ -451,7 +593,7 @@ disk_del_by_name(struct shell_ctx *ctx, const char *name) { } static void -shell_disk_del(struct shell_ctx *ctx, int argc, char **argv) { +cmd_disk_del(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: disk_del \n" " name disk name, i.e. rd or hd0\n"; @@ -465,7 +607,7 @@ shell_disk_del(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_pwd(struct shell_ctx *ctx, int argc, char **argv) { +cmd_pwd(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: pwd\n"; if (argc != 1) { @@ -482,7 +624,7 @@ shell_pwd(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_set_pixel(struct shell_ctx *ctx, int argc, char **argv) { +cmd_set_pixel(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set_pixel [-i]\n" " x x window coordinate\n" @@ -503,7 +645,7 @@ shell_set_pixel(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_write_text(struct shell_ctx *ctx, int argc, char **argv) { +cmd_write_text(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: write_text " " " @@ -541,7 +683,7 @@ shell_write_text(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get_key(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get_key(struct shell_ctx *ctx, int argc, char **argv) { (void)argv; const char *usage = \ "usage: get_key\n"; @@ -553,7 +695,7 @@ shell_get_key(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_dump_key_buff(struct shell_ctx *ctx, int argc, char **argv) { +cmd_dump_key_buff(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: dump_key_buff [count]\n" " count how many items to dump (all by default)\n"; @@ -572,7 +714,7 @@ shell_dump_key_buff(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_dump_win_stack(struct shell_ctx *ctx, int argc, char **argv) { +cmd_dump_win_stack(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: dump_win_stack [count]\n" " count how many items to dump\n"; @@ -590,7 +732,7 @@ shell_dump_win_stack(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_dump_win_pos(struct shell_ctx *ctx, int argc, char **argv) { +cmd_dump_win_pos(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: dump_win_pos [count]\n" " count how many items to dump\n"; @@ -608,7 +750,7 @@ shell_dump_win_pos(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_dump_win_map(struct shell_ctx *ctx, int argc, char **argv) { +cmd_dump_win_map(struct shell_ctx *ctx, int argc, char **argv) { // TODO: area const char *usage = \ "usage: dump_win_map\n"; @@ -626,7 +768,7 @@ shell_dump_win_map(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_dump_appdata(struct shell_ctx *ctx, int argc, char **argv) { +cmd_dump_appdata(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: dump_appdata [-p]\n" " index index into appdata array to dump\n" @@ -689,7 +831,7 @@ shell_dump_appdata(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_switch_to_thread(struct shell_ctx *ctx, int argc, char **argv) { +cmd_switch_to_thread(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: switch_to_thread \n" " thread id to switch to\n"; @@ -703,7 +845,7 @@ shell_switch_to_thread(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: get \n" " variable to get\n"; @@ -730,7 +872,7 @@ shell_get(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_set(struct shell_ctx *ctx, int argc, char **argv) { +cmd_set(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set \n" " variable to set\n" @@ -763,7 +905,7 @@ shell_set(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_new_sys_thread(struct shell_ctx *ctx, int argc, char **argv) { +cmd_new_sys_thread(struct shell_ctx *ctx, int argc, char **argv) { // FIXME const char *usage = \ "usage: new_sys_thread\n"; @@ -777,7 +919,7 @@ shell_new_sys_thread(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_mouse_move(struct shell_ctx *ctx, int argc, char **argv) { +cmd_mouse_move(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: mouse_move [-l] [-m] [-r] [-x {+|-|=}]" "[-y {+|-|=}] [-h {+|-}] [-v {+|-}]\n" @@ -865,7 +1007,7 @@ shell_mouse_move(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_process_info(struct shell_ctx *ctx, int argc, char **argv) { +cmd_process_info(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: process_info \n" " pid process id to dump, -1 for self\n"; @@ -874,7 +1016,8 @@ shell_process_info(struct shell_ctx *ctx, int argc, char **argv) { return; } process_information_t info; - int32_t pid = strtol(argv[1], NULL, 0); + ssize_t pid; + shell_parse_sint(ctx, &pid, argv[1]); COVERAGE_ON(); umka_sys_process_info(pid, &info); COVERAGE_OFF(); @@ -895,7 +1038,7 @@ shell_process_info(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_display_number(struct shell_ctx *ctx, int argc, char **argv) { +cmd_display_number(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: display_number " " " @@ -945,7 +1088,7 @@ shell_display_number(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_set_window_colors(struct shell_ctx *ctx, int argc, char **argv) { +cmd_set_window_colors(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set_window_colors " " " @@ -972,7 +1115,7 @@ shell_set_window_colors(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get_window_colors(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get_window_colors(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: get_window_colors\n"; if (argc != 1) { @@ -999,7 +1142,7 @@ shell_get_window_colors(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get_skin_height(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get_skin_height(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: get_skin_height\n"; if (argc != 1) { @@ -1014,7 +1157,7 @@ shell_get_skin_height(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get_screen_area(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get_screen_area(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: get_screen_area\n"; if (argc != 1) { @@ -1033,7 +1176,7 @@ shell_get_screen_area(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_set_screen_area(struct shell_ctx *ctx, int argc, char **argv) { +cmd_set_screen_area(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set_screen_area \n" " left left x coord\n" @@ -1055,7 +1198,7 @@ shell_set_screen_area(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_get_skin_margins(struct shell_ctx *ctx, int argc, char **argv) { +cmd_get_skin_margins(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: get_skin_margins\n"; if (argc != 1) { @@ -1074,7 +1217,7 @@ shell_get_skin_margins(struct shell_ctx *ctx, int argc, char **argv) { } static void -shell_set_button_style(struct shell_ctx *ctx, int argc, char **argv) { +cmd_set_button_style(struct shell_ctx *ctx, int argc, char **argv) { const char *usage = \ "usage: set_button_style