diff --git a/makefile b/makefile index d65d41f..574e90d 100644 --- a/makefile +++ b/makefile @@ -81,7 +81,7 @@ umka_fuse.o: umka_fuse.c umka.h $(CC) $(CFLAGS_32) `pkg-config fuse3 --cflags` -c $< umka_os.o: umka_os.c umka.h - $(CC) $(CFLAGS_32) -c $< -D_DEFAULT_SOURCE + $(CC) $(CFLAGS_32) -c $< -D_XOPEN_SOURCE=600 umka_ping.o: umka_ping.c umka.h $(CC) $(CFLAGS_32) -D_DEFAULT_SOURCE -c $< diff --git a/shell.c b/shell.c index 7589f3f..7613d34 100644 --- a/shell.c +++ b/shell.c @@ -31,7 +31,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -60,6 +62,11 @@ bool cur_dir_changed = true; char cmd_buf[FGETS_BUF_LEN]; +typedef struct { + char *name; + void (*func) (int, char **); +} func_table_t; + const char *f70_status_name[] = { "success", "disk_base", @@ -76,7 +83,8 @@ const char *f70_status_name[] = { "out_of_memory" }; -const char *get_f70_status_name(int s) { +static const char * +get_f70_status_name(int s) { switch (s) { case ERROR_SUCCESS: // return ""; @@ -98,7 +106,8 @@ const char *get_f70_status_name(int s) { } } -void convert_f70_file_attr(uint32_t attr, char s[KF_ATTR_CNT+1]) { +static void +convert_f70_file_attr(uint32_t attr, char s[KF_ATTR_CNT+1]) { s[0] = (attr & KF_READONLY) ? 'r' : '-'; s[1] = (attr & KF_HIDDEN) ? 'h' : '-'; s[2] = (attr & KF_SYSTEM) ? 's' : '-'; @@ -107,21 +116,24 @@ void convert_f70_file_attr(uint32_t attr, char s[KF_ATTR_CNT+1]) { s[5] = '\0'; } -void print_f70_status(f7080ret_t *r, int use_ebx) { +static void +print_f70_status(f7080ret_t *r, int use_ebx) { fprintf(fout, "status = %d %s", r->status, get_f70_status_name(r->status)); if (use_ebx && (r->status == ERROR_SUCCESS || r->status == ERROR_END_OF_FILE)) fprintf(fout, ", count = %d", r->count); fputc('\n', fout); } -bool parse_uintmax(const char *str, uintmax_t *res) { +static bool +parse_uintmax(const char *str, uintmax_t *res) { char *endptr; *res = strtoumax(str, &endptr, 0); bool ok = (str != endptr) && (*endptr == '\0'); return ok; } -bool parse_uint32(const char *str, uint32_t *res) { +static bool +parse_uint32(const char *str, uint32_t *res) { uintmax_t x; if (parse_uintmax(str, &x) && x <= UINT32_MAX) { *res = (uint32_t)x; @@ -132,7 +144,8 @@ bool parse_uint32(const char *str, uint32_t *res) { } } -bool parse_uint64(const char *str, uint64_t *res) { +static bool +parse_uint64(const char *str, uint64_t *res) { uintmax_t x; if (parse_uintmax(str, &x) && x <= UINT64_MAX) { *res = x; @@ -143,7 +156,8 @@ bool parse_uint64(const char *str, uint64_t *res) { } } -void print_bytes(uint8_t *x, size_t len) { +static void +print_bytes(uint8_t *x, size_t len) { for (size_t i = 0; i < len; i++) { if (i % PRINT_BYTES_PER_LINE == 0 && i != 0) { fputc('\n', fout); @@ -153,7 +167,8 @@ void print_bytes(uint8_t *x, size_t len) { fputc('\n', fout); } -void print_hash(uint8_t *x, size_t len) { +static void +print_hash(uint8_t *x, size_t len) { hash_context ctx; hash_oneshot(&ctx, x, len); for (size_t i = 0; i < HASH_SIZE; i++) { @@ -162,7 +177,8 @@ void print_hash(uint8_t *x, size_t len) { fputc('\n', fout); } -const char *get_last_dir(const char *path) { +static const char * +get_last_dir(const char *path) { const char *last = strrchr(path, '/'); if (!last) { last = path; @@ -172,7 +188,8 @@ const char *get_last_dir(const char *path) { return last; } -void prompt() { +static void +prompt() { if (cur_dir_changed) { COVERAGE_ON(); umka_sys_get_cwd(cur_dir, PATH_MAX); @@ -184,20 +201,8 @@ void prompt() { fflush(fout); } -/* -# define __FD_ZERO(fdsp) \ - do { \ - int __d0, __d1; \ - __asm__ __volatile__ ("cld; rep; stosd" \ - : "=c" (__d0), "=D" (__d1) \ - : "a" (0), "0" (sizeof (fd_set) \ - / sizeof (__fd_mask)), \ - "1" (&__FDS_BITS (fdsp)[0]) \ - : "memory"); \ - } while (0) -*/ - -int next_line(int is_tty, int block) { +static int +next_line(int is_tty, int block) { if (is_tty) { prompt(); } @@ -222,17 +227,19 @@ int next_line(int is_tty, int block) { } } -int split_args(char *s, char **argv) { +static int +split_args(char *s, char **argv) { int argc = -1; - for (; (argv[++argc] = strtok(s, " \t\n")) != NULL; s = NULL); + for (; (argv[++argc] = strtok(s, " \t\n\r")) != NULL; s = NULL); return argc; } -void shell_i40(int argc, char **argv) { +static void +shell_i40(int argc, char **argv) { const char *usage = \ "usage: i40 [ebx [ecx [edx [esi [edi [ebp]]]]]]...\n" " see '/kernel/docs/sysfuncs.txt' for details"; - if (argc == 1 || argc > 8) { + if (argc < 2 || argc > 8) { fputs(usage, fout); return; } @@ -253,7 +260,8 @@ void shell_i40(int argc, char **argv) { regs.ebx, regs.ebx, (int32_t)regs.ebx); } -void shell_disk_list_partitions(disk_t *d) { +static void +disk_list_partitions(disk_t *d) { for (size_t i = 0; i < d->num_partitions; i++) { fprintf(fout, "/%s/%d: ", d->name, i+1); if (d->partitions[i]->fs_user_functions == xfs_user_functions) { @@ -270,7 +278,8 @@ void shell_disk_list_partitions(disk_t *d) { } } -void shell_ramdisk_init(int argc, char **argv) { +static void +shell_ramdisk_init(int argc, char **argv) { const char *usage = \ "usage: ramdisk_init \n" " absolute or relative path"; @@ -297,10 +306,11 @@ void shell_ramdisk_init(int argc, char **argv) { COVERAGE_ON(); void *ramdisk = kos_ramdisk_init(); COVERAGE_OFF(); - shell_disk_list_partitions(ramdisk); + disk_list_partitions(ramdisk); } -void shell_disk_add(int argc, char **argv) { +static void +shell_disk_add(int argc, char **argv) { const char *usage = \ "usage: disk_add [option]...\n" " absolute or relative path\n" @@ -337,7 +347,7 @@ void shell_disk_add(int argc, char **argv) { COVERAGE_ON(); disk_media_changed(vdisk, 1); COVERAGE_OFF(); - shell_disk_list_partitions(vdisk); + disk_list_partitions(vdisk); return; } } @@ -346,7 +356,8 @@ void shell_disk_add(int argc, char **argv) { return; } -void shell_disk_del_by_name(const char *name) { +static void +disk_del_by_name(const char *name) { for(disk_t *d = disk_list.next; d != &disk_list; d = d->next) { if (!strcmp(d->name, name)) { COVERAGE_ON(); @@ -358,15 +369,28 @@ void shell_disk_del_by_name(const char *name) { fprintf(fout, "umka: can't find disk '%s'\n", name); } -void shell_disk_del(int argc, char **argv) { - (void)argc; +static void +shell_disk_del(int argc, char **argv) { + const char *usage = \ + "usage: disk_del \n" + " name disk name, i.e. rd or hd0"; + if (argc != 2) { + fputs(usage, fout); + return; + } const char *name = argv[1]; - shell_disk_del_by_name(name); + disk_del_by_name(name); return; } -void shell_pwd(int argc, char **argv) { - (void)argc; +static void +shell_pwd(int argc, char **argv) { + const char *usage = \ + "usage: pwd"; + if (argc != 1) { + fputs(usage, fout); + return; + } (void)argv; bool quoted = false; const char *quote = quoted ? "'" : ""; @@ -376,7 +400,18 @@ void shell_pwd(int argc, char **argv) { fprintf(fout, "%s%s%s\n", quote, cur_dir, quote); } -void shell_set_pixel(int argc, char **argv) { +static void +shell_set_pixel(int argc, char **argv) { + const char *usage = \ + "usage: set_pixel [-i]\n" + " x x window coordinate\n" + " y y window coordinate\n" + " color argb in hex\n" + " -i inverted color"; + if (argc < 4) { + fputs(usage, fout); + return; + } size_t x = strtoul(argv[1], NULL, 0); size_t y = strtoul(argv[2], NULL, 0); uint32_t color = strtoul(argv[3], NULL, 16); @@ -386,8 +421,26 @@ void shell_set_pixel(int argc, char **argv) { COVERAGE_OFF(); } -void shell_write_text(int argc, char **argv) { - (void)argc; +static void +shell_write_text(int argc, char **argv) { + const char *usage = \ + "usage: write_text " + " " + " \n" + " x x window coordinate\n" + " y y window coordinate\n" + " color argb in hex\n" + " string escape spaces\n" + " asciiz 1 if the string is zero-terminated\n" + " fill_bg fill text background with specified color\n" + " font_and_enc font size and string encoding\n" + " draw_to_buf draw to the buffer pointed to by the next param\n" + " length length of the string if it is non-asciiz\n" + " bg_color_or_buf argb or pointer"; + if (argc != 12) { + fputs(usage, fout); + return; + } size_t x = strtoul(argv[1], NULL, 0); size_t y = strtoul(argv[2], NULL, 0); uint32_t color = strtoul(argv[3], NULL, 16); @@ -400,11 +453,21 @@ void shell_write_text(int argc, char **argv) { int length = strtoul(argv[10], NULL, 0); int background_color_or_buffer = strtoul(argv[11], NULL, 0); COVERAGE_ON(); - umka_sys_write_text(x, y, color, asciiz, fill_background, font_and_encoding, draw_to_buffer, scale_factor, string, length, background_color_or_buffer); + umka_sys_write_text(x, y, color, asciiz, fill_background, font_and_encoding, + draw_to_buffer, scale_factor, string, length, + background_color_or_buffer); COVERAGE_OFF(); } -void shell_dump_win_stack(int argc, char **argv) { +static void +shell_dump_win_stack(int argc, char **argv) { + const char *usage = \ + "usage: dump_win_stack [count]\n" + " count how many items to dump"; + if (argc < 1) { + fputs(usage, fout); + return; + } int depth = 5; if (argc > 1) { depth = strtol(argv[1], NULL, 0); @@ -414,7 +477,15 @@ void shell_dump_win_stack(int argc, char **argv) { } } -void shell_dump_win_pos(int argc, char **argv) { +static void +shell_dump_win_pos(int argc, char **argv) { + const char *usage = \ + "usage: dump_win_pos [count]\n" + " count how many items to dump"; + if (argc < 1) { + fputs(usage, fout); + return; + } int depth = 5; if (argc > 1) { depth = strtol(argv[1], NULL, 0); @@ -424,13 +495,18 @@ void shell_dump_win_pos(int argc, char **argv) { } } -void shell_dump_appdata(int argc, char **argv) { - // TODO: usage - int idx; - int show_pointers = 0; - if (argc > 1) { - idx = strtol(argv[1], NULL, 0); +static void +shell_dump_appdata(int argc, char **argv) { + const char *usage = \ + "usage: dump_appdata [-p]\n" + " index index into appdata array to dump\n" + " -p print fields that are pointers"; + if (argc < 2) { + fputs(usage, fout); + return; } + int show_pointers = 0; + int idx = strtol(argv[1], NULL, 0); if (argc > 2 && !strcmp(argv[2], "-p")) { show_pointers = 1; } @@ -480,12 +556,16 @@ void shell_dump_appdata(int argc, char **argv) { (appdata_t*)a->in_schedule.next - kos_slot_base); } -void shell_dump_taskdata(int argc, char **argv) { - // TODO: usage - int idx; - if (argc > 1) { - idx = strtol(argv[1], NULL, 0); +static void +shell_dump_taskdata(int argc, char **argv) { + const char *usage = \ + "usage: dump_taskdata \n" + " index index into taskdata array to dump"; + if (argc < 2) { + fputs(usage, fout); + return; } + int idx = strtol(argv[1], NULL, 0); taskdata_t *t = kos_task_data + idx; fprintf(fout, "event_mask: %" PRIx32 "\n", t->event_mask); fprintf(fout, "pid: %" PRId32 "\n", t->pid); @@ -496,7 +576,8 @@ void shell_dump_taskdata(int argc, char **argv) { fprintf(fout, "cpu_usage: %" PRIu32 "\n", t->cpu_usage); } -void shell_mouse_move(int argc, char **argv) { +static void +shell_mouse_move(int argc, char **argv) { const char *usage = \ "usage: mouse_move [-l] [-m] [-r] [-x {+|-|=}]" "[-y {+|-|=}] [-h {+|-}] [-v {+|-}]\n" @@ -507,7 +588,10 @@ void shell_mouse_move(int argc, char **argv) { " -y increase, decrease or set y coordinate\n" " -h scroll horizontally\n" " -v scroll vertically\n"; - + if (!argc) { + fputs(usage, fout); + return; + } int lbheld = 0, mbheld = 0, rbheld = 0, xabs = 0, yabs = 0; int32_t xmoving = 0, ymoving = 0, hscroll = 0, vscroll = 0; int opt; @@ -580,8 +664,15 @@ void shell_mouse_move(int argc, char **argv) { COVERAGE_OFF(); } -void shell_process_info(int argc, char **argv) { - (void)argc; +static void +shell_process_info(int argc, char **argv) { + const char *usage = \ + "usage: process_info \n" + " pid process id to dump, -1 for self"; + if (argc != 2) { + fputs(usage, fout); + return; + } process_information_t info; int32_t pid = strtol(argv[1], NULL, 0); COVERAGE_ON(); @@ -592,16 +683,41 @@ void shell_process_info(int argc, char **argv) { fprintf(fout, "window_stack_value: %u\n", info.window_stack_value); fprintf(fout, "process_name: %s\n", info.process_name); fprintf(fout, "memory_start: 0x%.8" PRIx32 "\n", info.memory_start); - fprintf(fout, "used_memory: %u (0x%x)\n", info.used_memory, info.used_memory); + fprintf(fout, "used_memory: %u (0x%x)\n", info.used_memory, + info.used_memory); fprintf(fout, "pid: %u\n", info.pid); - fprintf(fout, "box: %u %u %u %u\n", info.box.left, info.box.top, info.box.width, info.box.height); + fprintf(fout, "box: %u %u %u %u\n", info.box.left, info.box.top, + info.box.width, info.box.height); fprintf(fout, "slot_state: %u\n", info.slot_state); - fprintf(fout, "client_box: %u %u %u %u\n", info.client_box.left, info.client_box.top, info.client_box.width, info.client_box.height); + fprintf(fout, "client_box: %u %u %u %u\n", info.client_box.left, + info.client_box.top, info.client_box.width, info.client_box.height); fprintf(fout, "wnd_state: 0x%.2" PRIx8 "\n", info.wnd_state); } -void shell_display_number(int argc, char **argv) { - (void)argc; +static void +shell_display_number(int argc, char **argv) { + const char *usage = \ + "usage: display_number " + " " + " \n" + " is_pointer if num_or_ptr argument is a pointer\n" + " base 0 - dec, 1 - hex, 2 - bin\n" + " num_digits how many digits to print\n" + " is_qword if 1, is_pointer = 1 and num_or_ptr is pointer\n" + " show_lead_zeros 0/1\n" + " num_or_ptr number itself or a pointer to it\n" + " x x window coord\n" + " y y window coord\n" + " color argb in hex\n" + " fill_bg 0/1\n" + " font 0 = 6x9, 1 = 8x16\n" + " draw_to_buf 0/1\n" + " scale_factor 0 = x1, ..., 7 = x8\n" + " bg_color_or_buf depending on flags fill_bg and draw_to_buf"; + if (argc != 15) { + fputs(usage, fout); + return; + } int is_pointer = strtoul(argv[1], NULL, 0); int base = strtoul(argv[2], NULL, 0); if (base == 10) base = 0; @@ -621,13 +737,22 @@ void shell_display_number(int argc, char **argv) { int scale_factor = strtoul(argv[13], NULL, 0); uintptr_t background_color_or_buffer = strtoul(argv[14], NULL, 16); COVERAGE_ON(); - umka_sys_display_number(is_pointer, base, digits_to_display, is_qword, show_leading_zeros, number_or_pointer, x, y, color, fill_background, font, draw_to_buffer, scale_factor, background_color_or_buffer); + umka_sys_display_number(is_pointer, base, digits_to_display, is_qword, + show_leading_zeros, number_or_pointer, x, y, color, + fill_background, font, draw_to_buffer, scale_factor, + background_color_or_buffer); COVERAGE_OFF(); } -void shell_set_window_colors(int argc, char **argv) { +static void +shell_set_window_colors(int argc, char **argv) { + const char *usage = \ + "usage: set_window_colors " + " " + " \n" + " * all colors are in hex"; if (argc != (1 + sizeof(system_colors_t)/4)) { - fprintf(fout, "10 colors required\n"); + fputs(usage, fout); return; } system_colors_t colors; @@ -646,8 +771,14 @@ void shell_set_window_colors(int argc, char **argv) { COVERAGE_OFF(); } -void shell_get_window_colors(int argc, char **argv) { - (void)argc; +static void +shell_get_window_colors(int argc, char **argv) { + const char *usage = \ + "usage: get_window_colors"; + if (argc != 1) { + fputs(usage, fout); + return; + } (void)argv; system_colors_t colors; COVERAGE_ON(); @@ -665,8 +796,14 @@ void shell_get_window_colors(int argc, char **argv) { fprintf(fout, "0x%.8" PRIx32 " work_graph\n", colors.work_graph); } -void shell_get_skin_height(int argc, char **argv) { - (void)argc; +static void +shell_get_skin_height(int argc, char **argv) { + const char *usage = \ + "usage: get_skin_height"; + if (argc != 1) { + fputs(usage, fout); + return; + } (void)argv; COVERAGE_ON(); uint32_t skin_height = umka_sys_get_skin_height(); @@ -674,8 +811,14 @@ void shell_get_skin_height(int argc, char **argv) { fprintf(fout, "%" PRIu32 "\n", skin_height); } -void shell_get_screen_area(int argc, char **argv) { - (void)argc; +static void +shell_get_screen_area(int argc, char **argv) { + const char *usage = \ + "usage: get_screen_area"; + if (argc != 1) { + fputs(usage, fout); + return; + } (void)argv; rect_t wa; COVERAGE_ON(); @@ -687,9 +830,16 @@ void shell_get_screen_area(int argc, char **argv) { fprintf(fout, "%" PRIu32 " bottom\n", wa.bottom); } -void shell_set_screen_area(int argc, char **argv) { +static void +shell_set_screen_area(int argc, char **argv) { + const char *usage = \ + "usage: set_screen_area \n" + " left left x coord\n" + " top top y coord\n" + " right right x coord (not length!)\n" + " bottom bottom y coord"; if (argc != 5) { - fprintf(fout, "left top right bottom\n"); + fputs(usage, fout); return; } rect_t wa; @@ -702,8 +852,14 @@ void shell_set_screen_area(int argc, char **argv) { COVERAGE_OFF(); } -void shell_get_skin_margins(int argc, char **argv) { - (void)argc; +static void +shell_get_skin_margins(int argc, char **argv) { + const char *usage = \ + "usage: get_skin_margins"; + if (argc != 1) { + fputs(usage, fout); + return; + } (void)argv; rect_t wa; COVERAGE_ON(); @@ -715,16 +871,30 @@ void shell_get_skin_margins(int argc, char **argv) { fprintf(fout, "%" PRIu32 " bottom\n", wa.bottom); } -void shell_set_button_style(int argc, char **argv) { - (void)argc; +static void +shell_set_button_style(int argc, char **argv) { + const char *usage = \ + "usage: set_button_style