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

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

dateコマンド追加
コードの整理

  • 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 _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 (_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 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 _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 _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 (lfn != NULL) ff_memfree(lfn);
377 if (path_backup != NULL) ff_memfree(path_backup);
378}
379
380int usrcmd_ls(int argc, char **argv)
381{
382 char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
383 char default_pattern[_MAX_LFN] = "";
384 int c;
385 BYTE list_option = 0;
386
387 while ((c = musl_getopt(argc, argv, "al")) != -1) {
388 switch (c) {
389 case 'a':
390 list_option |= LS_ALL;
391 break;
392 case 'l':
393 list_option |= LS_LONG;
394 break;
395 default:
396 break;
397 }
398 }
399 pattern_p = *(argv + optind);
400
401 if (pattern_p != NULL)
402 ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
403 basename_p = basename(pattern_p);
404 dirname_p = dirname(default_pattern);
405 if (((dirname_p[0] == '/') && (basename_p[0] == '/')) ||
406 (!ntlibc_strncmp(dirname_p, ".", ntlibc_strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", ntlibc_strlen(basename_p))))
407 {
408 basename_p = NULL;
409 }
410 print_ls(dirname_p, basename_p, list_option);
411
412 return 0;
413}
414
415int usrcmd_cp(int argc, char **argv)
416{
417 char *src_str_p = NULL, *dst_str_p = NULL;
418 unsigned char i;
419 FRESULT res;
420 FILINFO fno;
421 FIL src_fp, dst_fp;
422 size_t read_size, write_size;
423 char *lfn = NULL, *local_buff = NULL, *dst_mod_str_p = NULL;
424 char *src_basename_p;
425
426 if (argc < 2)
427 return 0;
428
429 /* 引数チェック */
430 for (i = 1; i < argc; i++) {
431 if (argv[i][0] == '-')
432 continue;
433 if (argv[i][0] != '\0') {
434 if (src_str_p == NULL)
435 src_str_p = &argv[i][0];
436 else {
437 dst_str_p = &argv[i][0];
438 break;
439 }
440 }
441 }
442 if ((src_str_p == NULL) || (dst_str_p == NULL))
443 return 0;
444
445#if _USE_LFN
446 /* LFN buffer alloc */
447 lfn = ff_memalloc(LFN_BUF_SIZE);
448 if (lfn == NULL) {
449 ntstdio_printf(&ntstdio, "alloc err.\n");
450 goto cp_end;
451 }
452 fno.lfname = lfn;
453 fno.lfsize = LFN_BUF_SIZE;
454#endif
455
456 /* copy buffer alloc */
457 local_buff = ff_memalloc(64);
458 if (local_buff == NULL) {
459 ntstdio_printf(&ntstdio, "alloc err.\n");
460 goto cp_end;
461 }
462
463 /*************/
464 /* src check */
465 /*************/
466 res = f_stat(src_str_p, &fno);
467 if (res != FR_OK) {
468 if (res == FR_NO_FILE)
469 ntstdio_printf(&ntstdio, "src no file.\n", res);
470 else
471 ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
472 goto cp_end;
473 }
474 if (fno.fattrib & AM_DIR) { /* src is dir */
475 /*******************************************************/ /* from dir */ /* 未実装 */
476 }
477 else { /* src is file */
478 res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
479 if (res != FR_OK) {
480 ntstdio_printf(&ntstdio, "src open err(%d).\n", res);
481 goto cp_end;
482 }
483 }
484
485 /*************/
486 /* dst check */
487 /*************/
488 res = f_stat(dst_str_p, &fno);
489 if (res != FR_NO_FILE) {
490 if (res == FR_OK) {
491 if (fno.fattrib & AM_DIR) { /* dst is dir */
492 src_basename_p = basename(src_str_p);
493 dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
494 if (dst_mod_str_p == NULL) {
495 ntstdio_printf(&ntstdio, "alloc err.\n");
496 goto cp_end;
497 }
498 ntstdio_snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);
499 dst_str_p = dst_mod_str_p;
500 }
501 else {
502 ntstdio_printf(&ntstdio, "dst file exists.\n");
503 goto cp_end_1;
504 }
505 }
506 else {
507 ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
508 goto cp_end_1;
509 }
510 }
511 res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
512 if (res != FR_OK) {
513 ntstdio_printf(&ntstdio, "dst open err(%d).\n", res);
514 goto cp_end_1;
515 }
516
517 /********/
518 /* copy */
519 /********/
520 do {
521 /* read from src */
522 res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
523 if (res != FR_OK) {
524 ntstdio_printf(&ntstdio, "src read err(%d).\n", res);
525 goto cp_end_2;
526 }
527
528 /* write to dst */
529 res = f_write(&dst_fp, local_buff, read_size, &write_size);
530 if (res != FR_OK) {
531 ntstdio_printf(&ntstdio, "dst write err(%d).\n", res);
532 goto cp_end_2;
533 }
534 if (read_size != write_size) {
535 ntstdio_printf(&ntstdio, "dst write err(disk full).\n", res);
536 goto cp_end_2;
537 }
538 } while (read_size == sizeof(local_buff));
539
540cp_end_2:
541 f_close(&dst_fp);
542cp_end_1:
543 f_close(&src_fp);
544cp_end:
545 if(dst_mod_str_p != NULL) ff_memfree(dst_mod_str_p);
546 if(local_buff != NULL) ff_memfree(local_buff);
547 if(lfn != NULL) ff_memfree(lfn);
548
549 return 0;
550}
551
552int usrcmd_rm(int argc, char **argv)
553{
554 FRESULT res;
555
556 if (argc == 2) {
557 if ((res = f_unlink(argv[1])) != FR_OK) {
558 put_rc("f_unlink", res);
559 return 0;
560 }
561 }
562
563 return 0;
564}
565
566int usrcmd_mv(int argc, char **argv)
567{
568 FRESULT res;
569
570 if (argc == 3) {
571 if ((res = f_rename(argv[1], argv[2])) != FR_OK) {
572 put_rc("f_rename", res);
573 return 0;
574 }
575 }
576
577 return 0;
578}
579
580int usrcmd_mkdir(int argc, char **argv)
581{
582 FRESULT res;
583
584 if (argc == 2) {
585 if ((res = f_mkdir(argv[1])) != FR_OK) {
586 put_rc("f_mkdir", res);
587 return 0;
588 }
589 }
590
591 return 0;
592}
593
594extern WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */
595extern void Utf16_to_Utf8(unsigned char *, int *, UINT); /* in ff.c */
596#define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */
597#define CCOLOR_RESET 0
598#define CCOLOR_BLACK 30
599#define CCOLOR_RED 31
600#define CCOLOR_GREEN 32
601#define CCOLOR_YELLOW 33
602#define CCOLOR_BLUE 34
603#define CCOLOR_MAGENTA 35
604#define CCOLOR_CYAN 36
605#define CCOLOR_WHITE 37
606enum {
607 HEXDUMP_OPT_DEFAULT = 1 << 0,
608 HEXDUMP_OPT_UTF8 = 1 << 1,
609};
610
611int usrcmd_hexdump(int argc, char **argv)
612{
613 FRESULT res;
614 FIL fsrc;
615 unsigned char data[16 + 1 + HEXDUMP_EXTRA_FOR_UTF8];
616 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];
617 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)];
618 TCHAR *filename_p = NULL;
619 int op, option_flag = HEXDUMP_OPT_DEFAULT;
620 char *option_ptr, *option_endptr, ccolor, utf8_done_flg, utf8_odd_bytes = 0;
621 unsigned int op_offset = 0, op_size = 0, op_end = 0;
622
623 while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {
624 switch (op) {
625 case 'h': /* help */
626 ntstdio_printf(&ntstdio, " hexdump [OPTION] file\n");
627 ntstdio_printf(&ntstdio, " -h : help\n");
628 ntstdio_printf(&ntstdio, " -d : print all byte with convert and color [in character area] (default)\n");
629 ntstdio_printf(&ntstdio, " -u : try print UTF-8 code [in character area]\n");
630 ntstdio_printf(&ntstdio, " -oOFFSET : print start offset address from top\n");
631 ntstdio_printf(&ntstdio, " -sSIZE : print size\n");
632 break;
633 case 'd': /* print one byte character [in character area] (default) */
634 option_flag |= HEXDUMP_OPT_DEFAULT;
635 break;
636 case 'u': /* try print UTF-8 code [in character area] */
637 option_flag |= HEXDUMP_OPT_UTF8;
638 break;
639 case 'o': /* print start offset address from top */
640 option_ptr = *(argv + optind);
641 op_offset = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
642 break;
643 case 's': /* print size */
644 option_ptr = *(argv + optind);
645 op_size = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
646 break;
647 default:
648 break;
649 }
650 }
651 filename_p = *(argv + optind);
652 if (filename_p == NULL)
653 return 0;
654
655 if ((res = f_open(&fsrc, filename_p, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
656 put_rc("f_open", res);
657 return 0;
658 }
659
660 /* position adjusting */
661 if (op_offset >= fsrc.fsize) {
662 ntstdio_printf(&ntstdio, "error : input offset is bigger than file size(0x%X).\n", fsrc.fsize);
663 return 0;
664 }
665 op_end = op_offset + op_size;
666 if ((op_size == 0) || (op_end >= fsrc.fsize))
667 op_end = fsrc.fsize;
668 f_lseek(&fsrc, op_offset);
669
670 for (int i = op_offset; i < op_end; i += 16) {
671 ascii[0] = '\0';
672 line[0] = '\0';
673 char *pos = line;
674 int rst = sizeof(line);
675 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);
676 pos += len;
677 rst -= len;
678
679 UINT br = 0;
680 if ((res = f_read(&fsrc, data, 16 + HEXDUMP_EXTRA_FOR_UTF8, &br)) != FR_OK) {
681 put_rc("f_read", res);
682 f_close(&fsrc);
683 return 0;
684 }
685 data[br] = '\0';
686 f_lseek(&fsrc, i + 16);
687 if (br > 16) br = 16;
688
689 char *apos = ascii;
690 int arst = sizeof(ascii);
691 for (int j = 0; j < br; j++) {
692 unsigned char c = data[j];
693 if (j != 7)
694 len = ntstdio_snprintf(pos, rst, "%02X ", c);
695 else
696 len = ntstdio_snprintf(pos, rst, "%02X-", c);
697 pos += len;
698 rst -= len;
699
700 len = 0;
701 utf8_done_flg = 0;
702 ccolor = CCOLOR_RESET;
703
704 if (c < 0x20) {
705 ccolor = CCOLOR_RED;
706 c = c + 0x40;
707 }
708 else {
709 if (option_flag & HEXDUMP_OPT_UTF8) { /* try UTF-8 */
710 /* check character code */
711 int bf_utf8_bytes, af_utf8_bytes;
712 unsigned char utf8_code[4];
713 WCHAR utf16_code;
714
715 utf16_code = Utf8_to_Utf16(&data[j], &bf_utf8_bytes); /* try UTF-8 -> UTF-16 */
716 Utf16_to_Utf8(utf8_code, &af_utf8_bytes, (UINT)utf16_code); /* try UTF-16 -> UTF-8 */
717 if ((af_utf8_bytes <= 3 && bf_utf8_bytes == af_utf8_bytes) &&
718 !memcmp(&data[j], utf8_code, af_utf8_bytes)) { /* size & code match */
719 utf8_done_flg = 1;
720 for (int k = 1; k < af_utf8_bytes; k++) { /* pos */
721 if (j + k >= 16)
722 break;
723 if (j + k != 7)
724 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);
725 else
726 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);
727 pos += len;
728 rst -= len;
729 }
730 memcpy(apos, &data[j], af_utf8_bytes); /* apos */
731 apos[af_utf8_bytes] = 0;
732 len = af_utf8_bytes;
733 j += af_utf8_bytes - 1;
734 if (af_utf8_bytes > 1) {
735 utf8_odd_bytes = j < 15 ? af_utf8_bytes - 2 : af_utf8_bytes - 1;
736 }
737 }
738 }
739 }
740
741 if (utf8_odd_bytes > 0 && j < 15) {
742 apos += len;
743 arst -= len;
744 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');
745 utf8_odd_bytes--;
746 }
747 else if (utf8_done_flg == 0) {
748 if (c >= 0x80 && c < 0x9F) {
749 ccolor = CCOLOR_RED;
750 c = c - 0x60;
751 }
752 else if (c >= 0xA0 && c < 0xDF) {
753 ccolor = CCOLOR_GREEN;
754 c = c - 0x60;
755 }
756 else if (c >= 0xE0 && c < 0xFF) {
757 ccolor = CCOLOR_GREEN;
758 c = c - 0xC0;
759 }
760 else if (c == 0x7F || c == 0x9F || c == 0xDF || c == 0xFF) {
761 ccolor = CCOLOR_CYAN;
762 c = '?';
763 }
764 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);
765 }
766
767 apos += len;
768 arst -= len;
769 }
770
771 for (int j = br; j < 16; j++) {
772 if (j != 7)
773 len = ntstdio_snprintf(pos, rst, " ");
774 else
775 len = ntstdio_snprintf(pos, rst, " -");
776 pos += len;
777 rst -= len;
778 }
779
780 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);
781 pos += len;
782 rst -= len;
783
784 ntstdio_puts(&ntstdio, line);
785 }
786
787 f_close(&fsrc);
788 return 0;
789}
790
791int usrcmd_date(int argc, char **argv)
792{
793 int ret;
794 struct timespec tp;
795 char buf[30];
796
797 ret = shell_clock_gettime(CLOCK_REALTIME, &tp);
798 if (ret != 0) {
799 ntstdio_printf(&ntstdio, "clock_gettime error %d", ret);
800 return 0;
801 }
802
803 memset(buf, 0, sizeof(buf));
804 if (ctime_r(&tp.tv_sec, buf) == NULL) {
805 ntstdio_printf(&ntstdio, "ctime_r error");
806 return 0;
807 }
808
809 /* 改行コードの削除 */
810 ret = ntlibc_strlen(buf);
811 buf[ret - 1] = '\0';
812
813 ntstdio_printf(&ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);
814 return 0;
815}
816
817int usrcmd_info(int argc, char **argv)
818{
819 if (argc != 2) {
820 ntstdio_printf(&ntstdio, "info sys\n");
821 ntstdio_printf(&ntstdio, "info ver\n");
822 return 0;
823 }
824 if (ntlibc_strcmp(argv[1], "sys") == 0) {
825 ntstdio_printf(&ntstdio, "GR-PEACH Monitor\n");
826 return 0;
827 }
828 if (ntlibc_strcmp(argv[1], "ver") == 0) {
829 int mj, mn, bd;
830 ntshell_version(&mj, &mn, &bd);
831 ntstdio_printf(&ntstdio, "Version %d.%d.%d\n", mj, mn, bd);
832 return 0;
833 }
834 ntstdio_printf(&ntstdio, "Unknown sub command found\n");
835 return -1;
836}
837
838int usrcmd_exit(int argc, char **argv)
839{
840 ntshell_exit = 1;
841
842 return 0;
843}
Note: See TracBrowser for help on using the repository browser.