source: asp3_tinet_ecnl_rx/trunk/ntshell/ntshell/usrcmd.c@ 340

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

NTPクライアント処理を追加

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