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