Changeset 331 for EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
r321 r331 31 31 */ 32 32 33 #include "shellif.h" 33 34 #include <kernel.h> 35 #include <string.h> 34 36 #include <t_syslog.h> 35 37 #include <t_stdlib.h> 36 38 #include <sil.h> 37 #include <stdlib.h>38 #include <string.h>39 #include <stdio.h>40 #include <setjmp.h>41 39 #include "syssvc/syslog.h" 42 40 #include "kernel_cfg.h" … … 46 44 #include "ff.h" 47 45 #include "usrcmd.h" 48 #include "ntstdio.h" 49 #include "ntshell.h" 46 #include "core/ntshell.h" 47 #include "core/ntlibc.h" 48 #include "util/ntstdio.h" 50 49 51 50 extern ntstdio_t ntstdio; 52 51 extern int ntshell_exit; 52 53 /* musl_getopt from msul */ 54 char *optarg; 55 int /*optind=1, opterr=1, */optopt, optpos, optreset=0; 56 extern int optind, opterr; 57 58 static 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 65 static 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 73 static 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 } 53 148 54 149 int shell_kill(int pid, int sig) … … 99 194 size_t i, j; 100 195 if (!s || !*s) return "."; 101 i = strlen(s) - 1;196 i = ntlibc_strlen(s) - 1; 102 197 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; } 103 198 for (; i&&s[i] == '/'; i--) s[i] = 0; … … 111 206 size_t i, j; 112 207 if (!s || !*s) return "."; 113 i = strlen(s) - 1;208 i = ntlibc_strlen(s) - 1; 114 209 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; } 115 210 for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s; … … 141 236 } 142 237 143 strlcat(path, "\n", sizeof(path));238 ntlibc_strlcat(path, "\n", sizeof(path)); 144 239 ntstdio_printf(&ntstdio, path); 145 240 … … 287 382 char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL; 288 383 char default_pattern[_MAX_LFN] = ""; 289 int i;384 int c; 290 385 BYTE list_option = 0; 291 386 292 for (i = 1; i < argc; i++) { 293 if (argv[i][0] == '-') { 294 if (argv[i][1] == 'a') { 295 list_option |= LS_ALL; 296 } 297 else if (argv[i][1] == 'l') { 298 list_option |= LS_LONG; 299 } 300 continue; 301 } 302 if (argv[i][0] != '\0') { /* pattern matching ? */ 303 pattern_p = &argv[i][0]; 387 while ((c = musl_getopt(argc, argv, "al")) != -1) { 388 switch (c) { 389 case 'a': 390 list_option |= LS_ALL; 304 391 break; 305 } 306 } 392 case 'l': 393 list_option |= LS_LONG; 394 break; 395 default: 396 break; 397 } 398 } 399 pattern_p = *(argv + optind); 307 400 308 401 if (pattern_p != NULL) 309 strlcpy(default_pattern, pattern_p, sizeof(default_pattern));402 ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern)); 310 403 basename_p = basename(pattern_p); 311 404 dirname_p = dirname(default_pattern); 312 405 if (((dirname_p[0] == '/') && (basename_p[0] == '/')) || 313 (! strncmp(dirname_p, ".", strlen(dirname_p)) && !strncmp(basename_p, ".",strlen(basename_p))))406 (!ntlibc_strncmp(dirname_p, ".", ntlibc_strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", ntlibc_strlen(basename_p)))) 314 407 { 315 408 basename_p = NULL; … … 499 592 } 500 593 594 extern WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */ 595 extern 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 606 enum { 607 HEXDUMP_OPT_DEFAULT = 1 << 0, 608 HEXDUMP_OPT_UTF8 = 1 << 1, 609 }; 610 501 611 int usrcmd_hexdump(int argc, char **argv) 502 612 { 503 613 FRESULT res; 504 614 FIL fsrc; 505 unsigned char data[16 + 1 ];506 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") ];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]; 507 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)]; 508 509 if (argc != 2) { 510 ntstdio_printf(&ntstdio, "hexdump file\n"); 511 } 512 513 if ((res = f_open(&fsrc, argv[1], FA_OPEN_EXISTING | FA_READ)) != FR_OK) { 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) { 514 656 put_rc("f_open", res); 515 657 return 0; 516 658 } 517 659 518 for (int i = 0; i < fsrc.fsize; i += 16) { 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) { 519 671 ascii[0] = '\0'; 520 672 line[0] = '\0'; … … 526 678 527 679 UINT br = 0; 528 if ((res = f_read(&fsrc, data, 16 , &br)) != FR_OK) {680 if ((res = f_read(&fsrc, data, 16 + HEXDUMP_EXTRA_FOR_UTF8, &br)) != FR_OK) { 529 681 put_rc("f_read", res); 530 682 f_close(&fsrc); 531 683 return 0; 532 684 } 533 data[16] = '\0'; 685 data[br] = '\0'; 686 f_lseek(&fsrc, i + 16); 687 if (br > 16) br = 16; 534 688 535 689 char *apos = ascii; … … 544 698 rst -= len; 545 699 700 len = 0; 701 utf8_done_flg = 0; 702 ccolor = CCOLOR_RESET; 703 546 704 if (c < 0x20) { 547 len = ntstdio_snprintf(apos, arst , "\x1B[31m%c\x1B[0m", c + 0x40); 548 } 549 else if (c == 0xFF) { 550 len = ntstdio_snprintf(apos, arst, "\x1B[32m%c\x1B[0m", 'E'); 705 ccolor = CCOLOR_RED; 706 c = c + 0x40; 551 707 } 552 708 else { 553 len = ntstdio_snprintf(apos, arst, "%c", c); 554 } 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 555 767 apos += len; 556 768 arst -= len; … … 570 782 rst -= len; 571 783 572 ntstdio_p rintf(&ntstdio, line);784 ntstdio_puts(&ntstdio, line); 573 785 } 574 786 … … 584 796 return 0; 585 797 } 586 if ( strcmp(argv[1], "sys") == 0) {798 if (ntlibc_strcmp(argv[1], "sys") == 0) { 587 799 ntstdio_printf(&ntstdio, "GR-PEACH Monitor\n"); 588 800 return 0; 589 801 } 590 if ( strcmp(argv[1], "ver") == 0) {802 if (ntlibc_strcmp(argv[1], "ver") == 0) { 591 803 int mj, mn, bd; 592 804 ntshell_version(&mj, &mn, &bd);
Note:
See TracChangeset
for help on using the changeset viewer.