source: EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c@ 435

Last change on this file since 435 was 435, checked in by coas-nagasima, 4 years ago

sdfsを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 20.2 KB
Line 
1/**
2 * @file usrcmd.c
3 * @author CuBeatSystems
4 * @author Shinichiro Nakamura
5 * @copyright
6 * ===============================================================
7 * Natural Tiny Shell (NT-Shell) Version 0.3.1
8 * ===============================================================
9 * Copyright (c) 2010-2016 Shinichiro Nakamura
10 *
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use,
15 * copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following
18 * conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33#include "shellif.h"
34#include <kernel.h>
35//#include <string.h>
36#include <t_syslog.h>
37#include <t_stdlib.h>
38#include <sil.h>
39#include "syssvc/syslog.h"
40#include "kernel_cfg.h"
41#include "main.h"
42#include "rza1.h"
43#include "ffarch.h"
44#include "ff.h"
45#include "usrcmd.h"
46#include "core/ntshell.h"
47#include "core/ntlibc.h"
48#include "util/ntstdio.h"
49
50extern ntstdio_t ntstdio;
51extern int ntshell_exit;
52
53/* musl_getopt from msul */
54char *optarg;
55int /*optind=1, opterr=1, */optopt, optpos, optreset=0;
56extern int optind, opterr;
57
58static void ntstdio_write(ntstdio_t *handle, const char *str, int l)
59{
60 for (; *str && l >= 0; l--) {
61 ntstdio_putc(handle, *str++);
62 }
63}
64
65static void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
66{
67 ntstdio_puts(&ntstdio, a);
68 ntstdio_write(&ntstdio, b, ntlibc_strlen(b));
69 ntstdio_write(&ntstdio, c, l);
70 ntstdio_putc(&ntstdio, '\n');
71}
72
73static int musl_getopt(int argc, char * const argv[], const char *optstring)
74{
75 int i;
76 wchar_t c, d;
77 int k, l;
78 char *optchar;
79
80 if (!optind || optreset) {
81 optreset = 0;
82 optpos = 0;
83 optind = 1;
84 }
85
86 if (optind >= argc || !argv[optind])
87 return -1;
88
89 if (argv[optind][0] != '-') {
90 if (optstring[0] == '-') {
91 optarg = argv[optind++];
92 return 1;
93 }
94 return -1;
95 }
96
97 if (!argv[optind][1])
98 return -1;
99
100 if (argv[optind][1] == '-' && !argv[optind][2])
101 return optind++, -1;
102
103 if (!optpos) optpos++;
104 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
105 k = 1;
106 c = 0xfffd; /* replacement char */
107 }
108 optchar = argv[optind]+optpos;
109 optopt = c;
110 optpos += k;
111
112 if (!argv[optind][optpos]) {
113 optind++;
114 optpos = 0;
115 }
116
117 if (optstring[0] == '-' || optstring[0] == '+')
118 optstring++;
119
120 i = 0;
121 d = 0;
122 do {
123 l = mbtowc(&d, optstring+i, MB_LEN_MAX);
124 if (l>0) i+=l; else i++;
125 } while (l && d != c);
126
127 if (d != c) {
128 if (optstring[0] != ':' && opterr)
129 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
130 return '?';
131 }
132 if (optstring[i] == ':') {
133 if (optstring[i+1] == ':') optarg = 0;
134 else if (optind >= argc) {
135 if (optstring[0] == ':') return ':';
136 if (opterr) __getopt_msg(argv[0],
137 ": option requires an argument: ",
138 optchar, k);
139 return '?';
140 }
141 if (optstring[i+1] != ':' || optpos) {
142 optarg = argv[optind++] + optpos;
143 optpos = 0;
144 }
145 }
146 return c;
147}
148
149int shell_kill(int pid, int sig)
150{
151 asm("bkpt #0");
152 return -1;
153}
154
155int shell_gettimeofday(struct timeval *tv, void *tzvp)
156{
157 SYSTIM time;
158 if (!tv) return 0;
159 get_tim(&time);
160 tv->tv_sec = time / 1000000;
161 tv->tv_usec = time - (tv->tv_sec * 1000000);
162 return 0;
163}
164
165void put_rc(const char *func, FRESULT rc)
166{
167 const char *p =
168 "OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0"
169 "DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0"
170 "NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0"
171 "NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0";
172 FRESULT i;
173
174 for (i = 0; i != rc && *p; i++) {
175 while (*p++);
176 }
177 ntstdio_printf(&ntstdio, "%s() =>%u FR_%s\n", func, (UINT)rc, p);
178}
179
180void put_drc(const char *func, DRESULT rc)
181{
182 const char *p =
183 "Successful\0R/W Error\0Write Protected\0Not Ready\0Invalid Parameter\0";
184 DRESULT i;
185
186 for (i = 0; i != rc && *p; i++) {
187 while (*p++);
188 }
189 ntstdio_printf(&ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);
190}
191
192char *basename(char *s)
193{
194 size_t i, j;
195 if (!s || !*s) return ".";
196 i = ntlibc_strlen(s) - 1;
197 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
198 for (; i&&s[i] == '/'; i--) s[i] = 0;
199 for (; i&&s[i - 1] != '/'; i--);
200 return s + i;
201}
202
203char *dirname(char *_s)
204{
205 char *s = _s;
206 size_t i, j;
207 if (!s || !*s) return ".";
208 i = ntlibc_strlen(s) - 1;
209 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
210 for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
211 for (; s[i] != '/'; i--) if (!i) return (s == _s) ? "." : _s;
212 for (; s[i] == '/'; i--) if (!i) if (s == _s) return "/"; else break;
213 s[i + 1] = 0;
214 return _s;
215}
216
217int usrcmd_cd(int argc, char **argv)
218{
219 FRESULT res;
220
221 if (argc == 2) {
222 if ((res = f_chdir(argv[1])) != FR_OK) {
223 put_rc("f_chdir", res);
224 return 0;
225 }
226 if ((res = f_chdrive(argv[1]) != FR_OK)) {
227 put_rc("f_chdrive", res);
228 return 0;
229 }
230 }
231
232 char path[256];
233 if ((res = f_getcwd(path, sizeof(path))) != FR_OK) {
234 put_rc("f_getcwd", res);
235 return 0;
236 }
237
238 ntlibc_strlcat(path, "\n", sizeof(path));
239 ntstdio_printf(&ntstdio, path);
240
241 return 0;
242}
243
244#define LS_ALL 0x01
245#define LS_LONG 0x02
246/* lsコマンド 1行表示 */
247void print_one_list(FILINFO *fno, BYTE list_option)
248{
249 char *fn;
250
251#if FF_USE_LFN
252 fn = *fno->lfname ? fno->lfname : fno->fname;
253#else
254 fn = fno->fname;
255#endif
256 if (!(list_option & LS_ALL)) {
257 if ((fno->fattrib & AM_HID) || (fn[0] == '.'))
258 return;
259 }
260
261 if (list_option & LS_LONG) {
262 ntstdio_printf(&ntstdio, "%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",
263 (fno->fattrib & AM_DIR) ? 'd' : '-',
264 (fno->fattrib & AM_RDO) ? 'r' : '-',
265 (fno->fattrib & AM_HID) ? 'h' : '-',
266 (fno->fattrib & AM_SYS) ? 's' : '-',
267 (fno->fattrib & AM_ARC) ? 'a' : '-',
268 ((fno->fdate & 0xFE00) >> 9) + 1980,
269 ((fno->fdate & 0x01E0) >> 5),
270 ( fno->fdate & 0x001F),
271 ((fno->ftime & 0xF800) >> 11),
272 ((fno->ftime & 0x07E0) >> 5),
273 ( fno->ftime & 0x001F));
274
275 if (fno->fattrib & AM_DIR) { /* It is a directory */
276 ntstdio_printf(&ntstdio, "%10S ", " ");
277 }
278 else {
279 ntstdio_printf(&ntstdio, "%10d ", fno->fsize);
280 }
281 }
282
283 if (fno->fattrib & AM_DIR) { /* It is a directory */
284 ntstdio_printf(&ntstdio, "\x1B[32m%s\x1B[0m\n", fn);
285 }
286 else {
287 ntstdio_printf(&ntstdio, "%s\n", fn);
288 }
289}
290
291#define LFN_BUF_SIZE (FF_MAX_LFN + 1)
292/* lsコマンド dir内 表示 */
293void print_ls(char *path_p, char *pattern_p, BYTE list_option)
294{
295 FRESULT res;
296 FILINFO fno;
297 FATFS_DIR dir;
298 char *fn; /* This function assumes non-Unicode configuration */
299
300 if ((pattern_p != NULL) && (pattern_p[0] == '\0'))
301 pattern_p = NULL;
302
303 char *path_backup = NULL;
304 path_backup = ff_memalloc(LFN_BUF_SIZE);
305 if (path_backup == NULL) {
306 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
307 return;
308 }
309
310#if FF_USE_LFN
311 char *lfn = NULL;
312 lfn = ff_memalloc(LFN_BUF_SIZE);
313 if (lfn == NULL) {
314 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
315 ff_memfree(path_backup);
316 return;
317 }
318 fno.lfname = lfn;
319 fno.lfsize = LFN_BUF_SIZE;
320#endif
321
322 if ((path_p != NULL) && (pattern_p == NULL)) {
323 if ((res = f_opendir(&dir, path_p)) != FR_OK)
324 put_rc("f_opendir", res);
325 res = f_readdir(&dir, &fno);
326 }
327 else {
328 res = f_findfirst(&dir, &fno, path_p, pattern_p);
329 }
330
331 while ((res == FR_OK) && (fno.fname[0] != 0)) {
332 if (pattern_p != NULL && (fno.fattrib & AM_DIR) && ((fno.fname[0] == '.') ? (pattern_p[0] == '.') : 1)) {/* DIR とパターンマッチしている場合は DIR 内部を ls する */
333#if FF_USE_LFN
334 fn = *fno.lfname ? fno.lfname : fno.fname;
335#else
336 fn = fno->fname;
337#endif
338 if ((res = f_getcwd(path_backup, LFN_BUF_SIZE)) != FR_OK) {
339 put_rc("f_getcwd", res);
340 }
341
342 if ((res = f_chdrive(path_p)) != RES_OK) {
343 put_rc("f_chdrive", res);
344 }
345
346 if ((res = f_chdir(path_p)) != RES_OK) {
347 put_rc("f_chdir", res);
348 }
349
350 ntstdio_printf(&ntstdio, "\n%s/%s:\n", path_p, fn);
351
352 print_ls(fn, NULL, list_option);
353
354 ntstdio_printf(&ntstdio, "\n");
355
356 if ((res = f_chdrive(path_backup)) != RES_OK) {
357 put_rc("f_chdrive", res);
358 }
359
360 if ((res = f_chdir(path_backup)) != RES_OK) {
361 put_rc("f_chdir", res);
362 }
363 }
364 else {
365 print_one_list(&fno, list_option);
366 }
367
368 if (pattern_p == NULL)
369 res = f_readdir(&dir, &fno); /* all */
370 else
371 res = f_findnext(&dir, &fno); /* pattern matching */
372 }
373
374 f_closedir(&dir);
375
376#if FF_USE_LFN
377 if (lfn != NULL) ff_memfree(lfn);
378#endif
379 if (path_backup != NULL) ff_memfree(path_backup);
380}
381
382int usrcmd_ls(int argc, char **argv)
383{
384 char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
385 char default_pattern[FF_MAX_LFN] = "";
386 int c;
387 BYTE list_option = 0;
388
389 while ((c = musl_getopt(argc, argv, "al")) != -1) {
390 switch (c) {
391 case 'a':
392 list_option |= LS_ALL;
393 break;
394 case 'l':
395 list_option |= LS_LONG;
396 break;
397 default:
398 break;
399 }
400 }
401 pattern_p = *(argv + optind);
402
403 if (pattern_p != NULL)
404 ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
405 basename_p = basename(pattern_p);
406 dirname_p = dirname(default_pattern);
407 if (((dirname_p[0] == '/') && (basename_p[0] == '/')) ||
408 (!ntlibc_strncmp(dirname_p, ".", ntlibc_strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", ntlibc_strlen(basename_p))))
409 {
410 basename_p = NULL;
411 }
412 print_ls(dirname_p, basename_p, list_option);
413
414 return 0;
415}
416
417int usrcmd_cp(int argc, char **argv)
418{
419 char *src_str_p = NULL, *dst_str_p = NULL;
420 unsigned char i;
421 FRESULT res;
422 FILINFO fno;
423 FIL src_fp, dst_fp;
424 size_t read_size, write_size;
425 char *lfn = NULL, *local_buff = NULL, *dst_mod_str_p = NULL;
426 char *src_basename_p;
427
428 if (argc < 2)
429 return 0;
430
431 /* 引数チェック */
432 for (i = 1; i < argc; i++) {
433 if (argv[i][0] == '-')
434 continue;
435 if (argv[i][0] != '\0') {
436 if (src_str_p == NULL)
437 src_str_p = &argv[i][0];
438 else {
439 dst_str_p = &argv[i][0];
440 break;
441 }
442 }
443 }
444 if ((src_str_p == NULL) || (dst_str_p == NULL))
445 return 0;
446
447#if FF_USE_LFN
448 /* LFN buffer alloc */
449 lfn = ff_memalloc(LFN_BUF_SIZE);
450 if (lfn == NULL) {
451 ntstdio_printf(&ntstdio, "alloc err.\n");
452 goto cp_end;
453 }
454 fno.lfname = lfn;
455 fno.lfsize = LFN_BUF_SIZE;
456#endif
457
458 /* copy buffer alloc */
459 local_buff = ff_memalloc(64);
460 if (local_buff == NULL) {
461 ntstdio_printf(&ntstdio, "alloc err.\n");
462 goto cp_end;
463 }
464
465 /*************/
466 /* src check */
467 /*************/
468 res = f_stat(src_str_p, &fno);
469 if (res != FR_OK) {
470 if (res == FR_NO_FILE)
471 ntstdio_printf(&ntstdio, "src no file.\n", res);
472 else
473 ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
474 goto cp_end;
475 }
476 if (fno.fattrib & AM_DIR) { /* src is dir */
477 /*******************************************************/ /* from dir */ /* 未実装 */
478 }
479 else { /* src is file */
480 res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
481 if (res != FR_OK) {
482 ntstdio_printf(&ntstdio, "src open err(%d).\n", res);
483 goto cp_end;
484 }
485 }
486
487 /*************/
488 /* dst check */
489 /*************/
490 res = f_stat(dst_str_p, &fno);
491 if (res != FR_NO_FILE) {
492 if (res == FR_OK) {
493 if (fno.fattrib & AM_DIR) { /* dst is dir */
494 src_basename_p = basename(src_str_p);
495 dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
496 if (dst_mod_str_p == NULL) {
497 ntstdio_printf(&ntstdio, "alloc err.\n");
498 goto cp_end;
499 }
500 ntstdio_snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);
501 dst_str_p = dst_mod_str_p;
502 }
503 else {
504 ntstdio_printf(&ntstdio, "dst file exists.\n");
505 goto cp_end_1;
506 }
507 }
508 else {
509 ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
510 goto cp_end_1;
511 }
512 }
513 res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
514 if (res != FR_OK) {
515 ntstdio_printf(&ntstdio, "dst open err(%d).\n", res);
516 goto cp_end_1;
517 }
518
519 /********/
520 /* copy */
521 /********/
522 do {
523 /* read from src */
524 res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
525 if (res != FR_OK) {
526 ntstdio_printf(&ntstdio, "src read err(%d).\n", res);
527 goto cp_end_2;
528 }
529
530 /* write to dst */
531 res = f_write(&dst_fp, local_buff, read_size, &write_size);
532 if (res != FR_OK) {
533 ntstdio_printf(&ntstdio, "dst write err(%d).\n", res);
534 goto cp_end_2;
535 }
536 if (read_size != write_size) {
537 ntstdio_printf(&ntstdio, "dst write err(disk full).\n", res);
538 goto cp_end_2;
539 }
540 } while (read_size == sizeof(local_buff));
541
542cp_end_2:
543 f_close(&dst_fp);
544cp_end_1:
545 f_close(&src_fp);
546cp_end:
547 if(dst_mod_str_p != NULL) ff_memfree(dst_mod_str_p);
548 if(local_buff != NULL) ff_memfree(local_buff);
549 if(lfn != NULL) ff_memfree(lfn);
550
551 return 0;
552}
553
554int usrcmd_rm(int argc, char **argv)
555{
556 FRESULT res;
557
558 if (argc == 2) {
559 if ((res = f_unlink(argv[1])) != FR_OK) {
560 put_rc("f_unlink", res);
561 return 0;
562 }
563 }
564
565 return 0;
566}
567
568int usrcmd_mv(int argc, char **argv)
569{
570 FRESULT res;
571
572 if (argc == 3) {
573 if ((res = f_rename(argv[1], argv[2])) != FR_OK) {
574 put_rc("f_rename", res);
575 return 0;
576 }
577 }
578
579 return 0;
580}
581
582int usrcmd_mkdir(int argc, char **argv)
583{
584 FRESULT res;
585
586 if (argc == 2) {
587 if ((res = f_mkdir(argv[1])) != FR_OK) {
588 put_rc("f_mkdir", res);
589 return 0;
590 }
591 }
592
593 return 0;
594}
595
596extern WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */
597extern void Utf16_to_Utf8(unsigned char *, int *, UINT); /* in ff.c */
598#define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */
599#define CCOLOR_RESET 0
600#define CCOLOR_BLACK 30
601#define CCOLOR_RED 31
602#define CCOLOR_GREEN 32
603#define CCOLOR_YELLOW 33
604#define CCOLOR_BLUE 34
605#define CCOLOR_MAGENTA 35
606#define CCOLOR_CYAN 36
607#define CCOLOR_WHITE 37
608enum {
609 HEXDUMP_OPT_DEFAULT = 1 << 0,
610 HEXDUMP_OPT_UTF8 = 1 << 1,
611};
612
613int usrcmd_hexdump(int argc, char **argv)
614{
615 FRESULT res;
616 FIL fsrc;
617 unsigned char data[16 + 1 + HEXDUMP_EXTRA_FOR_UTF8];
618 unsigned char ascii[sizeof("\x1B[31m0\x1B[0m\x1B[31m1\x1B[0m\x1B[31m2\x1B[0m\x1B[31m3\x1B[0m\x1B[31m4\x1B[0m\x1B[31m5\x1B[0m\x1B[31m6\x1B[0m\x1B[31m7\x1B[0m\x1B[31m8\x1B[0m\x1B[31m9\x1B[0m\x1B[31ma\x1B[0m\x1B[31mb\x1B[0m\x1B[31mc\x1B[0m\x1B[31md\x1B[0m\x1B[31me\x1B[0m\x1B[31mf\x1B[0m") + HEXDUMP_EXTRA_FOR_UTF8];
619 char line[sizeof("00000000: 00 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 00 : \n") + sizeof(ascii)];
620 TCHAR *filename_p = NULL;
621 int op, option_flag = HEXDUMP_OPT_DEFAULT;
622 char *option_ptr, *option_endptr, ccolor, utf8_done_flg, utf8_odd_bytes = 0;
623 unsigned int op_offset = 0, op_size = 0, op_end = 0;
624
625 while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {
626 switch (op) {
627 case 'h': /* help */
628 ntstdio_printf(&ntstdio, " hexdump [OPTION] file\n");
629 ntstdio_printf(&ntstdio, " -h : help\n");
630 ntstdio_printf(&ntstdio, " -d : print all byte with convert and color [in character area] (default)\n");
631 ntstdio_printf(&ntstdio, " -u : try print UTF-8 code [in character area]\n");
632 ntstdio_printf(&ntstdio, " -oOFFSET : print start offset address from top\n");
633 ntstdio_printf(&ntstdio, " -sSIZE : print size\n");
634 break;
635 case 'd': /* print one byte character [in character area] (default) */
636 option_flag |= HEXDUMP_OPT_DEFAULT;
637 break;
638 case 'u': /* try print UTF-8 code [in character area] */
639 option_flag |= HEXDUMP_OPT_UTF8;
640 break;
641 case 'o': /* print start offset address from top */
642 option_ptr = *(argv + optind);
643 op_offset = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
644 break;
645 case 's': /* print size */
646 option_ptr = *(argv + optind);
647 op_size = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
648 break;
649 default:
650 break;
651 }
652 }
653 filename_p = *(argv + optind);
654 if (filename_p == NULL)
655 return 0;
656
657 if ((res = f_open(&fsrc, filename_p, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
658 put_rc("f_open", res);
659 return 0;
660 }
661
662 /* position adjusting */
663 if (op_offset >= fsrc.fsize) {
664 ntstdio_printf(&ntstdio, "error : input offset is bigger than file size(0x%X).\n", fsrc.fsize);
665 return 0;
666 }
667 op_end = op_offset + op_size;
668 if ((op_size == 0) || (op_end >= fsrc.fsize))
669 op_end = fsrc.fsize;
670 f_lseek(&fsrc, op_offset);
671
672 for (int i = op_offset; i < op_end; i += 16) {
673 ascii[0] = '\0';
674 line[0] = '\0';
675 char *pos = line;
676 int rst = sizeof(line);
677 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);
678 pos += len;
679 rst -= len;
680
681 UINT br = 0;
682 if ((res = f_read(&fsrc, data, 16 + HEXDUMP_EXTRA_FOR_UTF8, &br)) != FR_OK) {
683 put_rc("f_read", res);
684 f_close(&fsrc);
685 return 0;
686 }
687 data[br] = '\0';
688 f_lseek(&fsrc, i + 16);
689 if (br > 16) br = 16;
690
691 char *apos = ascii;
692 int arst = sizeof(ascii);
693 for (int j = 0; j < br; j++) {
694 unsigned char c = data[j];
695 if (j != 7)
696 len = ntstdio_snprintf(pos, rst, "%02X ", c);
697 else
698 len = ntstdio_snprintf(pos, rst, "%02X-", c);
699 pos += len;
700 rst -= len;
701
702 len = 0;
703 utf8_done_flg = 0;
704 ccolor = CCOLOR_RESET;
705
706 if (c < 0x20) {
707 ccolor = CCOLOR_RED;
708 c = c + 0x40;
709 }
710 else {
711 if (option_flag & HEXDUMP_OPT_UTF8) { /* try UTF-8 */
712 /* check character code */
713 int bf_utf8_bytes, af_utf8_bytes;
714 unsigned char utf8_code[4];
715 WCHAR utf16_code;
716
717 utf16_code = Utf8_to_Utf16(&data[j], &bf_utf8_bytes); /* try UTF-8 -> UTF-16 */
718 Utf16_to_Utf8(utf8_code, &af_utf8_bytes, (UINT)utf16_code); /* try UTF-16 -> UTF-8 */
719 if ((af_utf8_bytes <= 3 && bf_utf8_bytes == af_utf8_bytes) &&
720 !memcmp(&data[j], utf8_code, af_utf8_bytes)) { /* size & code match */
721 utf8_done_flg = 1;
722 for (int k = 1; k < af_utf8_bytes; k++) { /* pos */
723 if (j + k >= 16)
724 break;
725 if (j + k != 7)
726 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);
727 else
728 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);
729 pos += len;
730 rst -= len;
731 }
732 memcpy(apos, &data[j], af_utf8_bytes); /* apos */
733 apos[af_utf8_bytes] = 0;
734 len = af_utf8_bytes;
735 j += af_utf8_bytes - 1;
736 if (af_utf8_bytes > 1) {
737 utf8_odd_bytes = j < 15 ? af_utf8_bytes - 2 : af_utf8_bytes - 1;
738 }
739 }
740 }
741 }
742
743 if (utf8_odd_bytes > 0 && j < 15) {
744 apos += len;
745 arst -= len;
746 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');
747 utf8_odd_bytes--;
748 }
749 else if (utf8_done_flg == 0) {
750 if (c >= 0x80 && c < 0x9F) {
751 ccolor = CCOLOR_RED;
752 c = c - 0x60;
753 }
754 else if (c >= 0xA0 && c < 0xDF) {
755 ccolor = CCOLOR_GREEN;
756 c = c - 0x60;
757 }
758 else if (c >= 0xE0 && c < 0xFF) {
759 ccolor = CCOLOR_GREEN;
760 c = c - 0xC0;
761 }
762 else if (c == 0x7F || c == 0x9F || c == 0xDF || c == 0xFF) {
763 ccolor = CCOLOR_CYAN;
764 c = '?';
765 }
766 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);
767 }
768
769 apos += len;
770 arst -= len;
771 }
772
773 for (int j = br; j < 16; j++) {
774 if (j != 7)
775 len = ntstdio_snprintf(pos, rst, " ");
776 else
777 len = ntstdio_snprintf(pos, rst, " -");
778 pos += len;
779 rst -= len;
780 }
781
782 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);
783 pos += len;
784 rst -= len;
785
786 ntstdio_puts(&ntstdio, line);
787 }
788
789 f_close(&fsrc);
790 return 0;
791}
792
793int usrcmd_date(int argc, char **argv)
794{
795 int ret;
796 struct timespec tp;
797 char buf[30];
798
799 ret = shell_clock_gettime(CLOCK_REALTIME, &tp);
800 if (ret != 0) {
801 ntstdio_printf(&ntstdio, "clock_gettime error %d", ret);
802 return 0;
803 }
804
805 memset(buf, 0, sizeof(buf));
806 if (ctime_r(&tp.tv_sec, buf) == NULL) {
807 ntstdio_printf(&ntstdio, "ctime_r error");
808 return 0;
809 }
810
811 /* 改行コードの削除 */
812 ret = ntlibc_strlen(buf);
813 buf[ret - 1] = '\0';
814
815 ntstdio_printf(&ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);
816 return 0;
817}
818
819int usrcmd_info(int argc, char **argv)
820{
821 if (argc != 2) {
822 ntstdio_printf(&ntstdio, "info sys\n");
823 ntstdio_printf(&ntstdio, "info ver\n");
824 return 0;
825 }
826 if (ntlibc_strcmp(argv[1], "sys") == 0) {
827 ntstdio_printf(&ntstdio, "GR-PEACH Monitor\n");
828 return 0;
829 }
830 if (ntlibc_strcmp(argv[1], "ver") == 0) {
831 int mj, mn, bd;
832 ntshell_version(&mj, &mn, &bd);
833 ntstdio_printf(&ntstdio, "Version %d.%d.%d\n", mj, mn, bd);
834 return 0;
835 }
836 ntstdio_printf(&ntstdio, "Unknown sub command found\n");
837 return -1;
838}
839
840int usrcmd_exit(int argc, char **argv)
841{
842 ntshell_exit = 1;
843
844 return 0;
845}
Note: See TracBrowser for help on using the repository browser.