Changeset 374 for asp3_tinet_ecnl_rx/trunk/ntshell/ntshell/usrcmd.c
- Timestamp:
- Apr 5, 2019, 9:26:53 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_rx/trunk/ntshell/ntshell/usrcmd.c
r340 r374 47 47 #include "core/ntshell.h" 48 48 #include "core/ntlibc.h" 49 #include "util/ntstdio.h"50 51 extern ntstdio_t ntstdio; 49 #include <stdio.h> 50 #include <getopt.h> 51 52 52 extern int ntshell_exit; 53 54 extern int mbtowc(wchar_t *__restrict wc, const char *__restrict src, size_t n);55 56 /* musl_getopt from msul */57 char *optarg;58 int optind=1, opterr=1, optopt, optpos, optreset=0;59 extern int optind, opterr;60 61 static 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 68 static 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 76 static 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 152 int shell_kill(int pid, int sig)153 {154 DebugBreak();155 return -1;156 }157 158 int 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 53 168 54 void put_rc(const char *func, FRESULT rc) … … 178 64 while (*p++); 179 65 } 180 ntstdio_printf(&ntstdio,"%s() =>%u FR_%s\n", func, (UINT)rc, p);66 printf("%s() =>%u FR_%s\n", func, (UINT)rc, p); 181 67 } 182 68 … … 190 76 while (*p++); 191 77 } 192 ntstdio_printf(&ntstdio,"%s() =>%u %s\n", func, (UINT)rc, p);78 printf("%s() =>%u %s\n", func, (UINT)rc, p); 193 79 } 194 80 … … 240 126 241 127 ntlibc_strlcat(path, "\n", sizeof(path)); 242 ntstdio_printf(&ntstdio,path);128 printf(path); 243 129 244 130 return 0; … … 252 138 char *fn; 253 139 254 #if _USE_LFN140 #if FF_USE_LFN 255 141 fn = *fno->lfname ? fno->lfname : fno->fname; 256 142 #else … … 263 149 264 150 if (list_option & LS_LONG) { 265 ntstdio_printf(&ntstdio,"%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",151 printf("%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ", 266 152 (fno->fattrib & AM_DIR) ? 'd' : '-', 267 153 (fno->fattrib & AM_RDO) ? 'r' : '-', … … 277 163 278 164 if (fno->fattrib & AM_DIR) { /* It is a directory */ 279 ntstdio_printf(&ntstdio, "%10S", " ");165 printf("%10s ", " "); 280 166 } 281 167 else { 282 ntstdio_printf(&ntstdio, "%10d", fno->fsize);168 printf("%10lu ", fno->fsize); 283 169 } 284 170 } 285 171 286 172 if (fno->fattrib & AM_DIR) { /* It is a directory */ 287 ntstdio_printf(&ntstdio,"\x1B[32m%s\x1B[0m\n", fn);173 printf("\x1B[32m%s\x1B[0m\n", fn); 288 174 } 289 175 else { 290 ntstdio_printf(&ntstdio,"%s\n", fn);291 } 292 } 293 294 #define LFN_BUF_SIZE ( _MAX_LFN + 1)176 printf("%s\n", fn); 177 } 178 } 179 180 #define LFN_BUF_SIZE (FF_MAX_LFN + 1) 295 181 /* lsコマンド dir内 表示 */ 296 182 void print_ls(char *path_p, char *pattern_p, BYTE list_option) … … 298 184 FRESULT res; 299 185 FILINFO fno; 300 DIR dir;186 FATFS_DIR dir; 301 187 char *fn; /* This function assumes non-Unicode configuration */ 302 188 … … 307 193 path_backup = ff_memalloc(LFN_BUF_SIZE); 308 194 if (path_backup == NULL) { 309 ntstdio_printf(&ntstdio,"ff_memalloc err.\n");195 printf("ff_memalloc err.\n"); 310 196 return; 311 197 } 312 198 313 #if _USE_LFN199 #if FF_USE_LFN 314 200 char *lfn = NULL; 315 201 lfn = ff_memalloc(LFN_BUF_SIZE); 316 202 if (lfn == NULL) { 317 ntstdio_printf(&ntstdio,"ff_memalloc err.\n");203 printf("ff_memalloc err.\n"); 318 204 ff_memfree(path_backup); 319 205 return; … … 333 219 334 220 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_LFN221 if (pattern_p != NULL && (fno.fattrib & AM_DIR) && ((fno.fname[0] == '.') ? (pattern_p[0] == '.') : 1)) {/* FATFS_DIR とパターンマッチしている場合は FATFS_DIR 内部を ls する */ 222 #if FF_USE_LFN 337 223 fn = *fno.lfname ? fno.lfname : fno.fname; 338 224 #else … … 351 237 } 352 238 353 ntstdio_printf(&ntstdio,"\n%s/%s:\n", path_p, fn);239 printf("\n%s/%s:\n", path_p, fn); 354 240 355 241 print_ls(fn, NULL, list_option); 356 242 357 ntstdio_printf(&ntstdio,"\n");243 printf("\n"); 358 244 359 245 if ((res = f_chdrive(path_backup)) != FR_OK) { … … 384 270 { 385 271 char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL; 386 char default_pattern[ _MAX_LFN] = "";272 char default_pattern[FF_MAX_LFN] = ""; 387 273 int c; 388 274 BYTE list_option = 0; 389 275 390 while ((c = musl_getopt(argc, argv, "al")) != -1) {276 while ((c = getopt(argc, argv, "al")) != -1) { 391 277 switch (c) { 392 278 case 'a': … … 446 332 return 0; 447 333 448 #if _USE_LFN334 #if FF_USE_LFN 449 335 /* LFN buffer alloc */ 450 336 lfn = ff_memalloc(LFN_BUF_SIZE); 451 337 if (lfn == NULL) { 452 ntstdio_printf(&ntstdio,"alloc err.\n");338 printf("alloc err.\n"); 453 339 goto cp_end; 454 340 } … … 460 346 local_buff = ff_memalloc(64); 461 347 if (local_buff == NULL) { 462 ntstdio_printf(&ntstdio,"alloc err.\n");348 printf("alloc err.\n"); 463 349 goto cp_end; 464 350 } … … 470 356 if (res != FR_OK) { 471 357 if (res == FR_NO_FILE) 472 ntstdio_printf(&ntstdio, "src no file.\n", res);358 printf("src no file.\n"); 473 359 else 474 ntstdio_printf(&ntstdio,"src stat err(%d).\n", res);360 printf("src stat err(%d).\n", res); 475 361 goto cp_end; 476 362 } … … 481 367 res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ)); 482 368 if (res != FR_OK) { 483 ntstdio_printf(&ntstdio,"src open err(%d).\n", res);369 printf("src open err(%d).\n", res); 484 370 goto cp_end; 485 371 } … … 496 382 dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE); 497 383 if (dst_mod_str_p == NULL) { 498 ntstdio_printf(&ntstdio,"alloc err.\n");384 printf("alloc err.\n"); 499 385 goto cp_end; 500 386 } 501 ntstdio_snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);387 snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p); 502 388 dst_str_p = dst_mod_str_p; 503 389 } 504 390 else { 505 ntstdio_printf(&ntstdio,"dst file exists.\n");391 printf("dst file exists.\n"); 506 392 goto cp_end_1; 507 393 } 508 394 } 509 395 else { 510 ntstdio_printf(&ntstdio,"src stat err(%d).\n", res);396 printf("src stat err(%d).\n", res); 511 397 goto cp_end_1; 512 398 } … … 514 400 res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE)); 515 401 if (res != FR_OK) { 516 ntstdio_printf(&ntstdio,"dst open err(%d).\n", res);402 printf("dst open err(%d).\n", res); 517 403 goto cp_end_1; 518 404 } … … 525 411 res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size); 526 412 if (res != FR_OK) { 527 ntstdio_printf(&ntstdio,"src read err(%d).\n", res);413 printf("src read err(%d).\n", res); 528 414 goto cp_end_2; 529 415 } … … 532 418 res = f_write(&dst_fp, local_buff, read_size, &write_size); 533 419 if (res != FR_OK) { 534 ntstdio_printf(&ntstdio,"dst write err(%d).\n", res);420 printf("dst write err(%d).\n", res); 535 421 goto cp_end_2; 536 422 } 537 423 if (read_size != write_size) { 538 ntstdio_printf(&ntstdio, "dst write err(disk full).\n", res);424 printf("dst write err(disk full).\n"); 539 425 goto cp_end_2; 540 426 } … … 622 508 unsigned int op_offset = 0, op_size = 0, op_end = 0; 623 509 624 while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {510 while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) { 625 511 switch (op) { 626 512 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");513 printf(" hexdump [OPTION] file\n"); 514 printf(" -h : help\n"); 515 printf(" -d : print all byte with convert and color [in character area] (default)\n"); 516 printf(" -u : try print UTF-8 code [in character area]\n"); 517 printf(" -oOFFSET : print start offset address from top\n"); 518 printf(" -sSIZE : print size\n"); 633 519 break; 634 520 case 'd': /* print one byte character [in character area] (default) */ … … 661 547 /* position adjusting */ 662 548 if (op_offset >= fsrc.fsize) { 663 ntstdio_printf(&ntstdio, "error : input offset is bigger than file size(0x%X).\n", fsrc.fsize);549 printf("error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize); 664 550 return 0; 665 551 } … … 674 560 char *pos = line; 675 561 int rst = sizeof(line); 676 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);562 int len = snprintf(pos, rst, "%08X: ", i); 677 563 pos += len; 678 564 rst -= len; … … 693 579 char c = data[j]; 694 580 if (j != 7) 695 len = ntstdio_snprintf(pos, rst, "%02X ", c);581 len = snprintf(pos, rst, "%02X ", c); 696 582 else 697 len = ntstdio_snprintf(pos, rst, "%02X-", c);583 len = snprintf(pos, rst, "%02X-", c); 698 584 pos += len; 699 585 rst -= len; … … 723 609 break; 724 610 if (j + k != 7) 725 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);611 len = snprintf(pos, rst, "%02X ", data[j + k]); 726 612 else 727 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);613 len = snprintf(pos, rst, "%02X-", data[j + k]); 728 614 pos += len; 729 615 rst -= len; … … 743 629 apos += len; 744 630 arst -= len; 745 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');631 len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' '); 746 632 utf8_odd_bytes--; 747 633 } … … 763 649 c = '?'; 764 650 } 765 len = ntstdio_snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);651 len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c); 766 652 } 767 653 … … 772 658 for (int j = br; j < 16; j++) { 773 659 if (j != 7) 774 len = ntstdio_snprintf(pos, rst, " ");660 len = snprintf(pos, rst, " "); 775 661 else 776 len = ntstdio_snprintf(pos, rst, " -");662 len = snprintf(pos, rst, " -"); 777 663 pos += len; 778 664 rst -= len; 779 665 } 780 666 781 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);667 len = snprintf(pos, rst, ": %s\n", ascii); 782 668 pos += len; 783 669 rst -= len; 784 670 785 ntstdio_puts(&ntstdio,line);671 puts(line); 786 672 } 787 673 … … 796 682 char buf[30]; 797 683 798 ret = shell_clock_gettime(CLOCK_REALTIME, &tp);684 ret = clock_gettime(CLOCK_REALTIME, &tp); 799 685 if (ret != 0) { 800 ntstdio_printf(&ntstdio,"clock_gettime error %d", ret);686 printf("clock_gettime error %d", ret); 801 687 return 0; 802 688 } … … 804 690 memset(buf, 0, sizeof(buf)); 805 691 if (ctime_r(&tp.tv_sec, buf) == NULL) { 806 ntstdio_printf(&ntstdio,"ctime_r error");692 printf("ctime_r error"); 807 693 return 0; 808 694 } … … 812 698 buf[ret - 1] = '\0'; 813 699 814 ntstdio_printf(&ntstdio, "%s .%09u\n", buf, tp.tv_nsec);700 printf("%s .%09ld\n", buf, tp.tv_nsec); 815 701 return 0; 816 702 } … … 819 705 { 820 706 if (argc != 2) { 821 ntstdio_printf(&ntstdio,"info sys\n");822 ntstdio_printf(&ntstdio,"info ver\n");707 printf("info sys\n"); 708 printf("info ver\n"); 823 709 return 0; 824 710 } 825 711 if (strcmp(argv[1], "sys") == 0) { 826 ntstdio_printf(&ntstdio,TARGET_NAME" Monitor\n");712 printf(TARGET_NAME" Monitor\n"); 827 713 return 0; 828 714 } … … 830 716 int mj, mn, bd; 831 717 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");718 printf("Version %d.%d.%d\n", mj, mn, bd); 719 return 0; 720 } 721 printf("Unknown sub command found\n"); 836 722 return -1; 837 723 }
Note:
See TracChangeset
for help on using the changeset viewer.