-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell.c
4422 lines (4194 loc) · 136 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SPDX-License-Identifier: GPL-2.0-or-later
UMKa - User-Mode KolibriOS developer tools
umka_shell - the shell
Copyright (C) 2017-2023 Ivan Baravy <[email protected]>
Copyright (C) 2021 Magomed Kostoev <[email protected]>
*/
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// TODO: Cleanup
#ifndef _WIN32
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <io.h>
#endif
#include "shell.h"
#include "vdisk.h"
#include "vnet.h"
#include "umka.h"
#include "trace.h"
#include "pci.h"
#include "umkart.h"
#include "lodepng/lodepng.h"
#include "optparse/optparse.h"
#include "isocline/include/isocline.h"
#define MAX_COMMAND_ARGS 42
#define PRINT_BYTES_PER_LINE 32
#define MAX_DIRENTS_TO_READ 100
#define MAX_BYTES_TO_READ (1024*1024)
#define DEFAULT_READDIR_ENCODING UTF8
#define DEFAULT_PATH_ENCODING UTF8
#define SHELL_CMD_BUF_LEN 0x10
enum {
SHELL_CMD_STATUS_EMPTY,
SHELL_CMD_STATUS_READY,
SHELL_CMD_STATUS_DONE,
};
struct umka_cmd umka_cmd_buf[SHELL_CMD_BUF_LEN];
char prompt_line[PATH_MAX];
char cur_dir[PATH_MAX] = "/";
const char *last_dir = cur_dir;
bool cur_dir_changed = true;
typedef struct {
char *name;
void (*func) (struct shell_ctx *, int, char **);
} func_table_t;
static uint32_t
shell_run_cmd_wait_test(void /* struct appdata * with wait_param is in ebx */) {
appdata_t *app;
__asm__ __volatile__ __inline__ ("":"=b"(app)::);
struct umka_cmd *cmd = (struct umka_cmd*)app->wait_param;
return (uint32_t)(atomic_load_explicit(&cmd->status, memory_order_acquire) == SHELL_CMD_STATUS_READY);
}
static void
shell_run_cmd_sync(struct shell_ctx *ctx);
static void
thread_cmd_runner(void *arg) {
umka_sti();
struct shell_ctx *ctx = arg;
while (1) {
kos_wait_events(shell_run_cmd_wait_test, umka_cmd_buf);
shell_run_cmd_sync(ctx);
}
}
const char *f70_status_name[] = {
"success",
"disk_base",
"unsupported_fs",
"unknown_fs",
"partition",
"file_not_found",
"end_of_file",
"memory_pointer",
"disk_full",
"fs_fail",
"access_denied",
"device",
"out_of_memory"
};
static const char *
get_f70_status_name(int s) {
switch (s) {
case KOS_ERROR_SUCCESS:
// return "";
case KOS_ERROR_DISK_BASE:
case KOS_ERROR_UNSUPPORTED_FS:
case KOS_ERROR_UNKNOWN_FS:
case KOS_ERROR_PARTITION:
case KOS_ERROR_FILE_NOT_FOUND:
case KOS_ERROR_END_OF_FILE:
case KOS_ERROR_MEMORY_POINTER:
case KOS_ERROR_DISK_FULL:
case KOS_ERROR_FS_FAIL:
case KOS_ERROR_ACCESS_DENIED:
case KOS_ERROR_DEVICE:
case KOS_ERROR_OUT_OF_MEMORY:
return f70_status_name[s];
default:
return "unknown";
}
}
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' : '-';
s[3] = (attr & KF_LABEL) ? 'l' : '-';
s[4] = (attr & KF_FOLDER) ? 'f' : '-';
s[5] = '\0';
}
static void
print_f70_status(struct shell_ctx *ctx, f7080ret_t *r, int use_ebx) {
fprintf(ctx->fout, "status = %d %s", r->status,
get_f70_status_name(r->status));
if (use_ebx
&& (r->status == KOS_ERROR_SUCCESS || r->status == KOS_ERROR_END_OF_FILE)) {
fprintf(ctx->fout, ", count = %d", r->count);
}
fprintf(ctx->fout, "\n");
}
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;
}
static bool
parse_uint32(struct shell_ctx *ctx, const char *str, uint32_t *res) {
uintmax_t x;
if (parse_uintmax(str, &x) && x <= UINT32_MAX) {
*res = (uint32_t)x;
return true;
} else {
fprintf(ctx->fout, "invalid number: %s\n", str);
return false;
}
}
static bool
parse_uint64(struct shell_ctx *ctx, const char *str, uint64_t *res) {
uintmax_t x;
if (parse_uintmax(str, &x) && x <= UINT64_MAX) {
*res = x;
return true;
} else {
fprintf(ctx->fout, "invalid number: %s\n", str);
return false;
}
}
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(void) {
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++) {
if (i % PRINT_BYTES_PER_LINE == 0 && i != 0) {
fprintf(ctx->fout, "\n");
}
fprintf(ctx->fout, "%2.2x", x[i]);
}
fprintf(ctx->fout, "\n");
}
static void
print_hash(struct shell_ctx *ctx, uint8_t *x, size_t len) {
hash_context hash;
hash_oneshot(&hash, x, len);
for (size_t i = 0; i < HASH_SIZE; i++) {
fprintf(ctx->fout, "%2.2x", hash.hash[i]);
}
fprintf(ctx->fout, "\n");
}
void *host_load_file(const char *fname) {
FILE *f = fopen(fname, "rb");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
rewind(f);
void *fdata = malloc(fsize);
fread(fdata, 1, fsize, f);
fclose(f);
return fdata;
}
static const char *
get_last_dir(const char *path) {
const char *last = strrchr(path, '/');
if (!last) {
last = path;
} else if (last != path || last[1] != '\0') {
last++;
}
return last;
}
static void
prompt(struct shell_ctx *ctx) {
if (cur_dir_changed) {
if (ctx->umka->booted) {
COVERAGE_ON();
umka_sys_get_cwd(cur_dir, PATH_MAX);
COVERAGE_OFF();
}
last_dir = get_last_dir(cur_dir);
cur_dir_changed = false;
}
fprintf(ctx->fout, "%s> ", last_dir);
fflush(stdout);
}
static void
completer(ic_completion_env_t *cenv, const char *prefix) {
(void)cenv;
(void)prefix;
}
static void
highlighter(ic_highlight_env_t *henv, const char *input, void *arg) {
(void)henv;
(void)input;
(void)arg;
}
static int
split_args(char *s, char **argv) {
int argc = -1;
for (; (argv[++argc] = strtok(s, " \t\n\r")) != NULL; s = NULL);
return argc;
}
static void
cmd_var(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
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) {
(void)ctx;
const char *usage = \
"usage: send_scancode <code>...\n"
" code dec or hex number\n";
if (argc < 2) {
fputs(usage, ctx->fout);
return;
}
argc -= 1;
argv += 1;
struct umka_cmd *cmd = shell_get_cmd(ctx);
cmd->type = UMKA_CMD_SEND_SCANCODE;
struct cmd_send_scancode_arg *c = &cmd->send_scancode.arg;
while (argc) {
char *endptr;
size_t code = strtoul(argv[0], &endptr, 0);
if (*endptr == '\0') {
c->scancode = code;
shell_run_cmd(ctx);
shell_clear_cmd(cmd);
argc--;
argv++;
} else {
fprintf(ctx->fout, "not an integer: %s\n", argv[0]);
fputs(usage, ctx->fout);
return;
}
}
}
static void
cmd_umka_boot(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
(void)argv;
const char *usage = \
"usage: umka_boot\n";
if (argc != 1) {
fputs(usage, ctx->fout);
return;
}
COVERAGE_ON();
umka_boot();
COVERAGE_OFF();
if (*ctx->running != UMKA_RUNNING_NEVER) {
char *stack = malloc(UMKA_DEFAULT_THREAD_STACK_SIZE);
char *stack_top = stack + UMKA_DEFAULT_THREAD_STACK_SIZE;
size_t tid = umka_new_sys_threads(0, thread_cmd_runner, stack_top, ctx,
"cmd_runner");
(void)tid;
}
}
static void
cmd_umka_set_boot_params(struct shell_ctx *ctx, int argc, char **argv) {
const char *usage = \
"usage: umka_set_boot_params [--x_res <num>] [--y_res <num>]\n"
" --x_res <num> screen width\n"
" --y_res <num> screen height\n"
" --bpp <num> screen bits per pixel\n"
" --pitch <num> screen line length in bytes\n";
argc -= 1;
argv += 1;
while (argc) {
if (!strcmp(argv[0], "--x_res") && argc > 1) {
kos_boot.x_res = strtoul(argv[1], NULL, 0);
kos_boot.pitch = kos_boot.x_res * kos_boot.bpp/8;
argc -= 2;
argv += 2;
continue;
} else if (!strcmp(argv[0], "--y_res") && argc > 1) {
kos_boot.y_res = strtoul(argv[1], NULL, 0);
argc -= 2;
argv += 2;
continue;
} else if (!strcmp(argv[0], "--bpp") && argc > 1) {
kos_boot.bpp = strtoul(argv[1], NULL, 0);
kos_boot.pitch = kos_boot.x_res * kos_boot.bpp/8;
argc -= 2;
argv += 2;
continue;
} else if (!strcmp(argv[0], "--pitch") && argc > 1) {
kos_boot.pitch = strtoul(argv[1], NULL, 0);
argc -= 2;
argv += 2;
continue;
} else {
fprintf(ctx->fout, "bad option: %s\n", argv[0]);
fputs(usage, ctx->fout);
return;
}
}
}
static void
cmd_csleep(struct shell_ctx *ctx, int argc, char **argv) {
const char *usage = \
"usage: csleep\n";
if (argc != 2) {
fputs(usage, ctx->fout);
return;
}
struct umka_cmd *cmd = umka_cmd_buf;
struct cmd_sys_csleep_arg *c = &cmd->sys_csleep.arg;
cmd->type = UMKA_CMD_SYS_CSLEEP;
c->csec = strtoul(argv[1], NULL, 0);
shell_run_cmd(ctx);
atomic_store_explicit(&cmd->status, SHELL_CMD_STATUS_EMPTY,
memory_order_release);
}
static uint32_t
umka_wait_for_idle_test(void) {
return (uint32_t)(atomic_load_explicit(&idle_scheduled, memory_order_acquire));
}
static void
umka_wait_for_idle(void) {
atomic_store_explicit(&idle_scheduled, 0, memory_order_release);
kos_wait_events(umka_wait_for_idle_test, NULL);
}
static void
cmd_wait_for_idle(struct shell_ctx *ctx, int argc, char **argv) {
(void)argv;
const char *usage = \
"usage: wait_for_idle\n";
if (argc != 1) {
fputs(usage, ctx->fout);
return;
}
struct umka_cmd *cmd = umka_cmd_buf;
cmd->type = UMKA_CMD_WAIT_FOR_IDLE;
shell_run_cmd(ctx);
atomic_store_explicit(&cmd->status, SHELL_CMD_STATUS_EMPTY,
memory_order_release);
}
static uint32_t
umka_wait_for_os_test(void) {
return (uint32_t)(atomic_load_explicit(&os_scheduled, memory_order_acquire));
}
static void
umka_wait_for_os_idle(void) {
atomic_store_explicit(&os_scheduled, 0, memory_order_release);
kos_wait_events(umka_wait_for_os_test, NULL);
atomic_store_explicit(&idle_scheduled, 0, memory_order_release);
kos_wait_events(umka_wait_for_idle_test, NULL);
}
static void
cmd_wait_for_os_idle(struct shell_ctx *ctx, int argc, char **argv) {
(void)argv;
const char *usage = \
"usage: wait_for_os_idle\n";
if (argc != 1) {
fputs(usage, ctx->fout);
return;
}
struct umka_cmd *cmd = umka_cmd_buf;
cmd->type = UMKA_CMD_WAIT_FOR_OS_IDLE;
shell_run_cmd(ctx);
atomic_store_explicit(&cmd->status, SHELL_CMD_STATUS_EMPTY,
memory_order_release);
}
static uint32_t
umka_wait_for_window_test(void) {
appdata_t *app;
wdata_t *wdata;
__asm__ __volatile__ __inline__ ("":"=b"(app)::);
const char *wnd_title = (const char *)app->wait_param;
for (size_t i = 0; i < 256; i++) {
app = kos_slot_base + i;
wdata = kos_window_data + i;
if (app->state != KOS_TSTATE_FREE && wdata->caption
&& !strcmp(wdata->caption, wnd_title)) {
return 1;
}
}
return 0;
}
static void
umka_wait_for_window(char *wnd_title) {
kos_wait_events(umka_wait_for_window_test, wnd_title);
}
static void
cmd_wait_for_window(struct shell_ctx *ctx, int argc, char **argv) {
(void)argv;
const char *usage = \
"usage: wait_for_window <window_title>\n";
if (argc != 2) {
fputs(usage, ctx->fout);
return;
}
struct umka_cmd *cmd = umka_cmd_buf;
cmd->type = UMKA_CMD_WAIT_FOR_WINDOW;
struct cmd_wait_for_window_arg *c = &cmd->wait_for_window.arg;
c->wnd_title = argv[1];
shell_run_cmd(ctx);
atomic_store_explicit(&cmd->status, SHELL_CMD_STATUS_EMPTY,
memory_order_release);
}
static void
cmd_i40(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: i40 <eax> [ebx [ecx [edx [esi [edi [ebp]]]]]]...\n"
" see '/kernel/docs/sysfuncs.txt' for details\n";
if (argc < 2 || argc > 8) {
fputs(usage, ctx->fout);
return;
}
pushad_t regs = {0, 0, 0, 0, 0, 0, 0, 0};
if (argv[1]) regs.eax = strtoul(argv[1], NULL, 0);
if (argv[2]) regs.ebx = strtoul(argv[2], NULL, 0);
if (argv[3]) regs.ecx = strtoul(argv[3], NULL, 0);
if (argv[4]) regs.edx = strtoul(argv[4], NULL, 0);
if (argv[5]) regs.esi = strtoul(argv[5], NULL, 0);
if (argv[6]) regs.edi = strtoul(argv[6], NULL, 0);
if (argv[7]) regs.ebp = strtoul(argv[7], NULL, 0);
COVERAGE_ON();
umka_i40(®s);
COVERAGE_OFF();
fprintf(ctx->fout, "eax = %8.8x %" PRIu32 " %" PRIi32 "\n"
"ebx = %8.8x %" PRIu32 " %" PRIi32 "\n",
regs.eax, regs.eax, (int32_t)regs.eax,
regs.ebx, regs.ebx, (int32_t)regs.ebx);
}
static void
bytes_to_kmgtpe(uint64_t *bytes, const char **kmg) {
lldiv_t d;
*kmg = "B";
if (*bytes == 0) {
return;
}
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "kiB";
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "MiB";
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "GiB";
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "TiB";
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "PiB";
d = lldiv(*bytes, 1024);
if (d.rem != 0) {
return;
}
*bytes = d.quot;
*kmg = "EiB";
}
static void
disk_list_partitions(struct shell_ctx *ctx, disk_t *d) {
uint64_t kmgtpe_count = d->media_info.sector_size * d->media_info.capacity;
const char *kmgtpe = NULL;
bytes_to_kmgtpe(&kmgtpe_count, &kmgtpe);
fprintf(ctx->fout, "/%s: sector_size=%u, capacity=%" PRIu64 " (%" PRIu64
" %s), num_partitions=%u\n", d->name, d->media_info.sector_size,
d->media_info.capacity, kmgtpe_count, kmgtpe, d->num_partitions);
for (size_t i = 0; i < d->num_partitions; i++) {
partition_t *p = d->partitions[i];
const char *fsname;
if (p->fs_user_functions == xfs_user_functions) {
fsname = "xfs";
} else if (p->fs_user_functions == ext_user_functions) {
fsname = "ext";
} else if (p->fs_user_functions == fat_user_functions) {
fsname = "fat";
} else if (p->fs_user_functions == exfat_user_functions) {
fsname = "exfat";
} else if (p->fs_user_functions == ntfs_user_functions) {
fsname = "ntfs";
} else if (p->fs_user_functions == iso9660_user_functions) {
fsname = "iso9660";
} else {
fsname = "???";
}
kmgtpe_count = d->media_info.sector_size * p->first_sector;
bytes_to_kmgtpe(&kmgtpe_count, &kmgtpe);
fprintf(ctx->fout, "/%s/%d: fs=%s, start=%" PRIu64 " (%" PRIu64 " %s)",
d->name, i+1, fsname, p->first_sector, kmgtpe_count, kmgtpe);
kmgtpe_count = d->media_info.sector_size * p->length;
bytes_to_kmgtpe(&kmgtpe_count, &kmgtpe);
fprintf(ctx->fout, ", length=%" PRIu64 " (%" PRIu64 " %s)\n",
p->length, kmgtpe_count, kmgtpe);
}
}
static void
cmd_ramdisk_init(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: ramdisk_init <file>\n"
" <file> absolute or relative path\n";
if (argc != 2) {
fputs(usage, ctx->fout);
return;
}
const char *fname = argv[1];
FILE *f = fopen(fname, "rb");
if (!f) {
fprintf(ctx->fout, "[!] can't open file '%s': %s\n", fname, strerror(errno));
return;
}
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
if (fsize > RAMDISK_MAX_LEN) {
fprintf(ctx->fout, "[!] file '%s' is too big, max size is %d bytes\n",
fname, RAMDISK_MAX_LEN);
return;
}
rewind(f);
fread(kos_ramdisk, fsize, 1, f);
fclose(f);
COVERAGE_ON();
void *ramdisk = kos_ramdisk_init();
COVERAGE_OFF();
disk_list_partitions(ctx, ramdisk);
}
static void
cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) {
const char *usage = \
"usage: disk_add <file> <name> [option]...\n"
" <file> absolute or relative path\n"
" <name> disk name, e.g. hd0 or rd\n"
" -c cache size size of disk cache in bytes\n";
if (argc < 3) {
fputs(usage, ctx->fout);
return;
}
size_t cache_size = 0;
int adjust_cache_size = 0;
int opt;
optparse_init(&ctx->opts, argv);
const char *file_name = optparse_arg(&ctx->opts);
const char *disk_name = optparse_arg(&ctx->opts);
while ((opt = optparse(&ctx->opts, "c:")) != -1) {
switch (opt) {
case 'c':
cache_size = strtoul(ctx->opts.optarg, NULL, 0);
adjust_cache_size = 1;
break;
default:
fputs(usage, ctx->fout);
return;
}
}
struct vdisk *umka_disk = vdisk_init(file_name, adjust_cache_size,
cache_size, ctx->io);
if (umka_disk) {
COVERAGE_ON();
disk_t *disk = disk_add(&umka_disk->diskfunc, disk_name, umka_disk, 0);
COVERAGE_OFF();
if (disk) {
COVERAGE_ON();
disk_media_changed(disk, 1);
COVERAGE_OFF();
disk_list_partitions(ctx, disk);
return;
}
}
fprintf(ctx->fout, "umka: can't add file '%s' as disk '%s'\n", file_name, disk_name);
return;
}
static void
disk_del_by_name(struct shell_ctx *ctx, const char *name) {
for(disk_t *d = disk_list.next; d != &disk_list; d = d->next) {
if (!strcmp(d->name, name)) {
COVERAGE_ON();
disk_del(d);
COVERAGE_OFF();
return;
}
}
fprintf(ctx->fout, "umka: can't find disk '%s'\n", name);
}
static void
cmd_disk_del(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: disk_del <name>\n"
" name disk name, i.e. rd or hd0\n";
if (argc != 2) {
fputs(usage, ctx->fout);
return;
}
const char *name = argv[1];
disk_del_by_name(ctx, name);
return;
}
static void
cmd_pwd(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: pwd\n";
if (argc != 1) {
fputs(usage, ctx->fout);
return;
}
(void)argv;
bool quoted = false;
const char *quote = quoted ? "'" : "";
COVERAGE_ON();
umka_sys_get_cwd(cur_dir, PATH_MAX);
COVERAGE_OFF();
fprintf(ctx->fout, "%s%s%s\n", quote, cur_dir, quote);
}
static void
cmd_set_pixel(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: set_pixel <x> <y> <color> [-i]\n"
" x x window coordinate\n"
" y y window coordinate\n"
" color argb in hex\n"
" -i inverted color\n";
if (argc < 4) {
fputs(usage, ctx->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);
int invert = (argc == 5) && !strcmp(argv[4], "-i");
COVERAGE_ON();
umka_sys_set_pixel(x, y, color, invert);
COVERAGE_OFF();
}
static void
cmd_write_text(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: write_text <x> <y> <color> <string> <asciiz> <fill_bg>"
" <font_and_enc> <draw_to_buf> <scale_factor> <length>"
" <bg_color_or_buf>\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\n";
if (argc != 12) {
fputs(usage, ctx->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);
const char *string = argv[4];
int asciiz = strtoul(argv[5], NULL, 0);
int fill_background = strtoul(argv[6], NULL, 0);
int font_and_encoding = strtoul(argv[7], NULL, 0);
int draw_to_buffer = strtoul(argv[8], NULL, 0);
int scale_factor = strtoul(argv[9], NULL, 0);
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);
COVERAGE_OFF();
}
static void
cmd_get_key(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
(void)argv;
const char *usage = \
"usage: get_key\n";
if (argc > 1) {
fputs(usage, ctx->fout);
return;
}
fprintf(ctx->fout, "0x%8.8" PRIx32 "\n", umka_get_key());
}
static void
cmd_dump_key_buff(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: dump_key_buff [count]\n"
" count how many items to dump (all by default)\n";
if (argc > 2) {
fputs(usage, ctx->fout);
return;
}
int count = INT_MAX;
if (argc > 1) {
count = strtol(argv[1], NULL, 0);
}
for (int i = 0; i < count && i < kos_key_count; i++) {
fprintf(ctx->fout, "%3i 0x%2.2x 0x%2.2x\n", i, kos_key_buff[i],
kos_key_buff[120+2+i]);
}
}
static void
cmd_dump_win_stack(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: dump_win_stack [count]\n"
" count how many items to dump\n";
if (argc > 2) {
fputs(usage, ctx->fout);
return;
}
int depth = 5;
if (argc > 1) {
depth = strtol(argv[1], NULL, 0);
}
for (int i = 0; i < depth; i++) {
fprintf(ctx->fout, "%3i: %3u\n", i, kos_win_stack[i]);
}
}
static void
cmd_dump_win_pos(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
const char *usage = \
"usage: dump_win_pos [count]\n"
" count how many items to dump\n";
if (argc < 1) {
fputs(usage, ctx->fout);
return;
}
int depth = 5;
if (argc > 1) {
depth = strtol(argv[1], NULL, 0);
}
for (int i = 0; i < depth; i++) {
fprintf(ctx->fout, "%3i: %3u\n", i, kos_win_pos[i]);
}
}
static void
cmd_dump_win_map(struct shell_ctx *ctx, int argc, char **argv) {
(void)ctx;
(void)argv;
// TODO: area
const char *usage = \
"usage: dump_win_map\n";
if (argc < 0) {
fputs(usage, ctx->fout);
return;
}
for (size_t y = 0; y < kos_display.height; y++) {
for (size_t x = 0; x < kos_display.width; x++) {