Changeset 442 for EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
- Timestamp:
- Jul 13, 2020, 8:07:55 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
r441 r442 32 32 33 33 #include "shellif.h" 34 #include <getopt.h> 34 35 #include <kernel.h> 35 36 #include <t_syslog.h> … … 45 46 #include "core/ntshell.h" 46 47 #include "core/ntlibc.h" 47 #include "util/ntstdio.h" 48 49 extern ntstdio_t *ntstdio; 48 50 49 extern int ntshell_exit; 51 52 /* musl_getopt from msul */53 char *optarg;54 int /*optind=1, opterr=1, */optopt, optpos, optreset=0;55 extern int optind, opterr;56 57 static void ntstdio_write(ntstdio_t *handle, const char *str, int l)58 {59 for (; *str && l >= 0; l--) {60 ntstdio_putc(handle, *str++);61 }62 }63 64 static void __getopt_msg(const char *a, const char *b, const char *c, size_t l)65 {66 ntstdio_puts(&ntstdio, a);67 ntstdio_write(&ntstdio, b, ntlibc_strlen(b));68 ntstdio_write(&ntstdio, c, l);69 ntstdio_putc(&ntstdio, '\n');70 }71 72 static int musl_getopt(int argc, char * const argv[], const char *optstring)73 {74 int i;75 wchar_t c, d;76 int k, l;77 char *optchar;78 79 if (!optind || optreset) {80 optreset = 0;81 optpos = 0;82 optind = 1;83 }84 85 if (optind >= argc || !argv[optind])86 return -1;87 88 if (argv[optind][0] != '-') {89 if (optstring[0] == '-') {90 optarg = argv[optind++];91 return 1;92 }93 return -1;94 }95 96 if (!argv[optind][1])97 return -1;98 99 if (argv[optind][1] == '-' && !argv[optind][2])100 return optind++, -1;101 102 if (!optpos) optpos++;103 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {104 k = 1;105 c = 0xfffd; /* replacement char */106 }107 optchar = argv[optind]+optpos;108 optopt = c;109 optpos += k;110 111 if (!argv[optind][optpos]) {112 optind++;113 optpos = 0;114 }115 116 if (optstring[0] == '-' || optstring[0] == '+')117 optstring++;118 119 i = 0;120 d = 0;121 do {122 l = mbtowc(&d, optstring+i, MB_LEN_MAX);123 if (l>0) i+=l; else i++;124 } while (l && d != c);125 126 if (d != c) {127 if (optstring[0] != ':' && opterr)128 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);129 return '?';130 }131 if (optstring[i] == ':') {132 if (optstring[i+1] == ':') optarg = 0;133 else if (optind >= argc) {134 if (optstring[0] == ':') return ':';135 if (opterr) __getopt_msg(argv[0],136 ": option requires an argument: ",137 optchar, k);138 return '?';139 }140 if (optstring[i+1] != ':' || optpos) {141 optarg = argv[optind++] + optpos;142 optpos = 0;143 }144 }145 return c;146 }147 50 148 51 void put_rc(const char *func, FRESULT rc) … … 158 61 while (*p++); 159 62 } 160 ntstdio_printf(ntstdio,"%s() =>%u FR_%s\n", func, (UINT)rc, p);63 printf("%s() =>%u FR_%s\n", func, (UINT)rc, p); 161 64 } 162 65 … … 170 73 while (*p++); 171 74 } 172 ntstdio_printf(ntstdio,"%s() =>%u %s\n", func, (UINT)rc, p);75 printf("%s() =>%u %s\n", func, (UINT)rc, p); 173 76 } 174 77 … … 177 80 size_t i, j; 178 81 if (!s || !*s) return "."; 179 i = ntlibc_strlen(s) - 1;82 i = strlen(s) - 1; 180 83 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; } 181 84 for (; i&&s[i] == '/'; i--) s[i] = 0; … … 189 92 size_t i, j; 190 93 if (!s || !*s) return "."; 191 i = ntlibc_strlen(s) - 1;94 i = strlen(s) - 1; 192 95 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; } 193 96 for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s; … … 219 122 } 220 123 221 ntlibc_strlcat(path, "\n", sizeof(path));222 ntstdio_printf(ntstdio,path);124 strlcat(path, "\n", sizeof(path)); 125 printf(path); 223 126 224 127 return 0; … … 243 146 244 147 if (list_option & LS_LONG) { 245 ntstdio_printf(ntstdio,"%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",148 printf("%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ", 246 149 (fno->fattrib & AM_DIR) ? 'd' : '-', 247 150 (fno->fattrib & AM_RDO) ? 'r' : '-', … … 257 160 258 161 if (fno->fattrib & AM_DIR) { /* It is a directory */ 259 ntstdio_printf(ntstdio,"%10s ", " ");162 printf("%10s ", " "); 260 163 } 261 164 else { 262 ntstdio_printf(ntstdio,"%10lu ", fno->fsize);165 printf("%10lu ", fno->fsize); 263 166 } 264 167 } 265 168 266 169 if (fno->fattrib & AM_DIR) { /* It is a directory */ 267 ntstdio_printf(ntstdio,"\x1B[32m%s\x1B[0m\n", fn);170 printf("\x1B[32m%s\x1B[0m\n", fn); 268 171 } 269 172 else { 270 ntstdio_printf(ntstdio,"%s\n", fn);173 printf("%s\n", fn); 271 174 } 272 175 } … … 287 190 path_backup = ff_memalloc(LFN_BUF_SIZE); 288 191 if (path_backup == NULL) { 289 ntstdio_printf(ntstdio,"ff_memalloc err.\n");192 printf("ff_memalloc err.\n"); 290 193 return; 291 194 } … … 295 198 lfn = ff_memalloc(LFN_BUF_SIZE); 296 199 if (lfn == NULL) { 297 ntstdio_printf(ntstdio,"ff_memalloc err.\n");200 printf("ff_memalloc err.\n"); 298 201 ff_memfree(path_backup); 299 202 return; … … 317 220 fn = *fno.lfname ? fno.lfname : fno.fname; 318 221 #else 319 fn = fno ->fname;222 fn = fno.fname; 320 223 #endif 321 224 if ((res = f_getcwd(path_backup, LFN_BUF_SIZE)) != FR_OK) { … … 331 234 } 332 235 333 ntstdio_printf(ntstdio,"\n%s/%s:\n", path_p, fn);236 printf("\n%s/%s:\n", path_p, fn); 334 237 335 238 print_ls(fn, NULL, list_option); 336 239 337 ntstdio_printf(ntstdio,"\n");240 printf("\n"); 338 241 339 242 if ((res = f_chdrive(path_backup)) != FR_OK) { … … 370 273 BYTE list_option = 0; 371 274 372 while ((c = musl_getopt(argc, argv, "al")) != -1) {275 while ((c = getopt(argc, argv, "al")) != -1) { 373 276 switch (c) { 374 277 case 'a': … … 385 288 386 289 if (pattern_p != NULL) 387 ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));290 strlcpy(default_pattern, pattern_p, sizeof(default_pattern)); 388 291 basename_p = basename(pattern_p); 389 292 dirname_p = dirname(default_pattern); 390 293 if (((dirname_p[0] == '/') && (basename_p[0] == '/')) || 391 (! ntlibc_strncmp(dirname_p, ".", ntlibc_strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", ntlibc_strlen(basename_p))))294 (!strncmp(dirname_p, ".", strlen(dirname_p)) && !strncmp(basename_p, ".", strlen(basename_p)))) 392 295 { 393 296 basename_p = NULL; … … 432 335 lfn = ff_memalloc(LFN_BUF_SIZE); 433 336 if (lfn == NULL) { 434 ntstdio_printf(ntstdio,"alloc err.\n");337 printf("alloc err.\n"); 435 338 goto cp_end; 436 339 } … … 442 345 local_buff = ff_memalloc(64); 443 346 if (local_buff == NULL) { 444 ntstdio_printf(ntstdio,"alloc err.\n");347 printf("alloc err.\n"); 445 348 goto cp_end; 446 349 } … … 452 355 if (res != FR_OK) { 453 356 if (res == FR_NO_FILE) 454 ntstdio_printf(ntstdio,"src no file.\n");357 printf("src no file.\n"); 455 358 else 456 ntstdio_printf(ntstdio,"src stat err(%d).\n", res);359 printf("src stat err(%d).\n", res); 457 360 goto cp_end; 458 361 } … … 463 366 res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ)); 464 367 if (res != FR_OK) { 465 ntstdio_printf(ntstdio,"src open err(%d).\n", res);368 printf("src open err(%d).\n", res); 466 369 goto cp_end; 467 370 } … … 478 381 dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE); 479 382 if (dst_mod_str_p == NULL) { 480 ntstdio_printf(ntstdio,"alloc err.\n");383 printf("alloc err.\n"); 481 384 goto cp_end; 482 385 } 483 ntstdio_snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);386 snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p); 484 387 dst_str_p = dst_mod_str_p; 485 388 } 486 389 else { 487 ntstdio_printf(ntstdio,"dst file exists.\n");390 printf("dst file exists.\n"); 488 391 goto cp_end_1; 489 392 } 490 393 } 491 394 else { 492 ntstdio_printf(ntstdio,"src stat err(%d).\n", res);395 printf("src stat err(%d).\n", res); 493 396 goto cp_end_1; 494 397 } … … 496 399 res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE)); 497 400 if (res != FR_OK) { 498 ntstdio_printf(ntstdio,"dst open err(%d).\n", res);401 printf("dst open err(%d).\n", res); 499 402 goto cp_end_1; 500 403 } … … 507 410 res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size); 508 411 if (res != FR_OK) { 509 ntstdio_printf(ntstdio,"src read err(%d).\n", res);412 printf("src read err(%d).\n", res); 510 413 goto cp_end_2; 511 414 } … … 514 417 res = f_write(&dst_fp, local_buff, read_size, &write_size); 515 418 if (res != FR_OK) { 516 ntstdio_printf(ntstdio,"dst write err(%d).\n", res);419 printf("dst write err(%d).\n", res); 517 420 goto cp_end_2; 518 421 } 519 422 if (read_size != write_size) { 520 ntstdio_printf(ntstdio,"dst write err(disk full).\n");423 printf("dst write err(disk full).\n"); 521 424 goto cp_end_2; 522 425 } … … 604 507 unsigned int op_offset = 0, op_size = 0, op_end = 0; 605 508 606 while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {509 while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) { 607 510 switch (op) { 608 511 case 'h': /* help */ 609 ntstdio_printf(ntstdio," hexdump [OPTION] file\n");610 ntstdio_printf(ntstdio," -h : help\n");611 ntstdio_printf(ntstdio," -d : print all byte with convert and color [in character area] (default)\n");612 ntstdio_printf(ntstdio," -u : try print UTF-8 code [in character area]\n");613 ntstdio_printf(ntstdio," -oOFFSET : print start offset address from top\n");614 ntstdio_printf(ntstdio," -sSIZE : print size\n");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"); 615 518 break; 616 519 case 'd': /* print one byte character [in character area] (default) */ … … 622 525 case 'o': /* print start offset address from top */ 623 526 option_ptr = *(argv + optind); 624 op_offset = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);527 op_offset = strtoul(&option_ptr[2], &option_endptr, 0); 625 528 break; 626 529 case 's': /* print size */ 627 530 option_ptr = *(argv + optind); 628 op_size = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);531 op_size = strtoul(&option_ptr[2], &option_endptr, 0); 629 532 break; 630 533 default: … … 643 546 /* position adjusting */ 644 547 if (op_offset >= fsrc.fsize) { 645 ntstdio_printf(ntstdio,"error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize);548 printf("error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize); 646 549 return 0; 647 550 } … … 656 559 char *pos = line; 657 560 int rst = sizeof(line); 658 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);561 int len = snprintf(pos, rst, "%08X: ", i); 659 562 pos += len; 660 563 rst -= len; … … 675 578 unsigned char c = data[j]; 676 579 if (j != 7) 677 len = ntstdio_snprintf(pos, rst, "%02X ", c);580 len = snprintf(pos, rst, "%02X ", c); 678 581 else 679 len = ntstdio_snprintf(pos, rst, "%02X-", c);582 len = snprintf(pos, rst, "%02X-", c); 680 583 pos += len; 681 584 rst -= len; … … 705 608 break; 706 609 if (j + k != 7) 707 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);610 len = snprintf(pos, rst, "%02X ", data[j + k]); 708 611 else 709 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);612 len = snprintf(pos, rst, "%02X-", data[j + k]); 710 613 pos += len; 711 614 rst -= len; … … 725 628 apos += len; 726 629 arst -= len; 727 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');630 len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' '); 728 631 utf8_odd_bytes--; 729 632 } … … 745 648 c = '?'; 746 649 } 747 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);650 len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c); 748 651 } 749 652 … … 754 657 for (int j = br; j < 16; j++) { 755 658 if (j != 7) 756 len = ntstdio_snprintf(pos, rst, " ");659 len = snprintf(pos, rst, " "); 757 660 else 758 len = ntstdio_snprintf(pos, rst, " -");661 len = snprintf(pos, rst, " -"); 759 662 pos += len; 760 663 rst -= len; 761 664 } 762 665 763 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);666 len = snprintf(pos, rst, ": %s\n", ascii); 764 667 pos += len; 765 668 rst -= len; 766 669 767 ntstdio_puts(ntstdio,line);670 printf(line); 768 671 } 769 672 … … 780 683 ret = shell_clock_gettime(CLOCK_REALTIME, &tp); 781 684 if (ret != 0) { 782 ntstdio_printf(ntstdio,"clock_gettime error %d", ret);685 printf("clock_gettime error %d", ret); 783 686 return 0; 784 687 } … … 786 689 memset(buf, 0, sizeof(buf)); 787 690 if (ctime_r(&tp.tv_sec, buf) == NULL) { 788 ntstdio_printf(ntstdio,"ctime_r error");691 printf("ctime_r error"); 789 692 return 0; 790 693 } 791 694 792 695 /* 改行コードの削除 */ 793 ret = ntlibc_strlen(buf);696 ret = strlen(buf); 794 697 buf[ret - 1] = '\0'; 795 698 796 ntstdio_printf(ntstdio,"%s .%09ld\n", buf, tp.tv_nsec);699 printf("%s .%09ld\n", buf, tp.tv_nsec); 797 700 return 0; 798 701 } … … 801 704 { 802 705 if (argc != 2) { 803 ntstdio_printf(ntstdio,"info sys\n");804 ntstdio_printf(ntstdio,"info ver\n");805 return 0; 806 } 807 if ( ntlibc_strcmp(argv[1], "sys") == 0) {808 ntstdio_printf(ntstdio,TARGET_NAME" Monitor\n");809 return 0; 810 } 811 if ( ntlibc_strcmp(argv[1], "ver") == 0) {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) { 812 715 int mj, mn, bd; 813 716 ntshell_version(&mj, &mn, &bd); 814 ntstdio_printf(ntstdio,"Version %d.%d.%d\n", mj, mn, bd);815 return 0; 816 } 817 ntstdio_printf(ntstdio,"Unknown sub command found\n");717 printf("Version %d.%d.%d\n", mj, mn, bd); 718 return 0; 719 } 720 printf("Unknown sub command found\n"); 818 721 return -1; 819 722 }
Note:
See TracChangeset
for help on using the changeset viewer.