Ignore:
Timestamp:
Apr 5, 2019, 9:26:53 PM (5 years ago)
Author:
coas-nagasima
Message:

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_arm/trunk/ntshell/ntshell/usrcmd.c

    r352 r374  
    4747#include "core/ntshell.h"
    4848#include "core/ntlibc.h"
    49 #include "util/ntstdio.h"
    50 
    51 extern ntstdio_t ntstdio;
     49#include <stdio.h>
     50#include <getopt.h>
     51
    5252extern 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 }
    16753
    16854void put_rc(const char *func, FRESULT rc)
     
    17864                while (*p++);
    17965        }
    180         ntstdio_printf(&ntstdio, "%s() =>%u FR_%s\n", func, (UINT)rc, p);
     66        printf("%s() =>%u FR_%s\n", func, (UINT)rc, p);
    18167}
    18268
     
    19076                while (*p++);
    19177        }
    192         ntstdio_printf(&ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);
     78        printf("%s() =>%u %s\n", func, (UINT)rc, p);
    19379}
    19480
     
    240126
    241127        ntlibc_strlcat(path, "\n", sizeof(path));
    242         ntstdio_printf(&ntstdio, path);
     128        printf(path);
    243129
    244130        return 0;
     
    252138        char *fn;
    253139
    254 #if _USE_LFN
     140#if FF_USE_LFN
    255141        fn = *fno->lfname ? fno->lfname : fno->fname;
    256142#else
     
    263149
    264150        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 ",
    266152                        (fno->fattrib & AM_DIR) ? 'd' : '-',
    267153                        (fno->fattrib & AM_RDO) ? 'r' : '-',
     
    277163
    278164                if (fno->fattrib & AM_DIR) {                    /* It is a directory */
    279                         ntstdio_printf(&ntstdio, "%10S ", " ");
     165                        printf("%10s ", " ");
    280166                }
    281167                else {
    282                         ntstdio_printf(&ntstdio, "%10d ", fno->fsize);
     168                        printf("%10lu ", fno->fsize);
    283169                }
    284170        }
    285171
    286172        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);
    288174        }
    289175        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)
    295181/* lsコマンド dir内 表示 */
    296182void print_ls(char *path_p, char *pattern_p, BYTE list_option)
     
    298184        FRESULT res;
    299185        FILINFO fno;
    300         DIR dir;
     186        FATFS_DIR dir;
    301187        char *fn;   /* This function assumes non-Unicode configuration */
    302188
     
    307193        path_backup = ff_memalloc(LFN_BUF_SIZE);
    308194        if (path_backup == NULL) {
    309                 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
     195                printf("ff_memalloc err.\n");
    310196                return;
    311197        }
    312198
    313 #if _USE_LFN
     199#if FF_USE_LFN
    314200        char *lfn = NULL;
    315201        lfn = ff_memalloc(LFN_BUF_SIZE);
    316202        if (lfn == NULL) {
    317                 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
     203                printf("ff_memalloc err.\n");
    318204                ff_memfree(path_backup);
    319205                return;
     
    333219
    334220        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
     221                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
    337223                        fn = *fno.lfname ? fno.lfname : fno.fname;
    338224#else
     
    351237                        }
    352238
    353                         ntstdio_printf(&ntstdio, "\n%s/%s:\n", path_p, fn);
     239                        printf("\n%s/%s:\n", path_p, fn);
    354240
    355241                        print_ls(fn, NULL, list_option);
    356242
    357                         ntstdio_printf(&ntstdio, "\n");
     243                        printf("\n");
    358244
    359245                        if ((res = f_chdrive(path_backup)) != FR_OK) {
     
    384270{
    385271        char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
    386         char default_pattern[_MAX_LFN] = "";
     272        char default_pattern[FF_MAX_LFN] = "";
    387273        int c;
    388274        BYTE list_option = 0;
    389275
    390         while ((c = musl_getopt(argc, argv, "al")) != -1) {
     276        while ((c = getopt(argc, argv, "al")) != -1) {
    391277                switch (c) {
    392278                case 'a':
     
    446332                return 0;
    447333
    448 #if _USE_LFN
     334#if FF_USE_LFN
    449335        /* LFN buffer alloc */
    450336        lfn = ff_memalloc(LFN_BUF_SIZE);
    451337        if (lfn == NULL) {
    452                 ntstdio_printf(&ntstdio, "alloc err.\n");
     338                printf("alloc err.\n");
    453339                goto cp_end;
    454340        }
     
    460346        local_buff = ff_memalloc(64);
    461347        if (local_buff == NULL) {
    462                 ntstdio_printf(&ntstdio, "alloc err.\n");
     348                printf("alloc err.\n");
    463349                goto cp_end;
    464350        }
     
    470356        if (res != FR_OK) {
    471357                if (res == FR_NO_FILE)
    472                         ntstdio_printf(&ntstdio, "src no file.\n", res);
     358                        printf("src no file.\n");
    473359                else
    474                         ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
     360                        printf("src stat err(%d).\n", res);
    475361                goto cp_end;
    476362        }
     
    481367                res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
    482368                if (res != FR_OK) {
    483                         ntstdio_printf(&ntstdio, "src open err(%d).\n", res);
     369                        printf("src open err(%d).\n", res);
    484370                        goto cp_end;
    485371                }
     
    496382                                dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
    497383                                if (dst_mod_str_p == NULL) {
    498                                         ntstdio_printf(&ntstdio, "alloc err.\n");
     384                                        printf("alloc err.\n");
    499385                                        goto cp_end;
    500386                                }
    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);
    502388                                dst_str_p = dst_mod_str_p;
    503389                        }
    504390                        else {
    505                                 ntstdio_printf(&ntstdio, "dst file exists.\n");
     391                                printf("dst file exists.\n");
    506392                                goto cp_end_1;
    507393                        }
    508394                }
    509395                else {
    510                         ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
     396                        printf("src stat err(%d).\n", res);
    511397                        goto cp_end_1;
    512398                }
     
    514400        res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
    515401        if (res != FR_OK) {
    516                 ntstdio_printf(&ntstdio, "dst open err(%d).\n", res);
     402                printf("dst open err(%d).\n", res);
    517403                goto cp_end_1;
    518404        }
     
    525411                res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
    526412                if (res != FR_OK) {
    527                         ntstdio_printf(&ntstdio, "src read err(%d).\n", res);
     413                        printf("src read err(%d).\n", res);
    528414                        goto cp_end_2;
    529415                }
     
    532418                res = f_write(&dst_fp, local_buff, read_size, &write_size);
    533419                if (res != FR_OK) {
    534                         ntstdio_printf(&ntstdio, "dst write err(%d).\n", res);
     420                        printf("dst write err(%d).\n", res);
    535421                        goto cp_end_2;
    536422                }
    537423                if (read_size != write_size) {
    538                         ntstdio_printf(&ntstdio, "dst write err(disk full).\n", res);
     424                        printf("dst write err(disk full).\n");
    539425                        goto cp_end_2;
    540426                }
     
    622508        unsigned int op_offset = 0, op_size = 0, op_end = 0;
    623509
    624         while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {
     510        while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) {
    625511                switch (op) {
    626512                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");
    633519                        break;
    634520                case 'd': /* print one byte character [in character area] (default) */
     
    661547        /* position adjusting */
    662548        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);
    664550                return 0;
    665551        }
     
    674560                char *pos = line;
    675561                int rst = sizeof(line);
    676                 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);
     562                int len = snprintf(pos, rst, "%08X: ", i);
    677563                pos += len;
    678564                rst -= len;
     
    693579                        char c = data[j];
    694580                        if (j != 7)
    695                                 len = ntstdio_snprintf(pos, rst, "%02X ", c);
     581                                len = snprintf(pos, rst, "%02X ", c);
    696582                        else
    697                                 len = ntstdio_snprintf(pos, rst, "%02X-", c);
     583                                len = snprintf(pos, rst, "%02X-", c);
    698584                        pos += len;
    699585                        rst -= len;
     
    723609                                                                break;
    724610                                                        if (j + k != 7)
    725                                                                 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);
     611                                                                len = snprintf(pos, rst, "%02X ", data[j + k]);
    726612                                                        else
    727                                                                 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);
     613                                                                len = snprintf(pos, rst, "%02X-", data[j + k]);
    728614                                                        pos += len;
    729615                                                        rst -= len;
     
    743629                                apos += len;
    744630                                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, ' ');
    746632                                utf8_odd_bytes--;
    747633                        }
     
    763649                                        c = '?';
    764650                                }
    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);
    766652                        }
    767653
     
    772658                for (int j = br; j < 16; j++) {
    773659                        if (j != 7)
    774                                 len = ntstdio_snprintf(pos, rst, "   ");
     660                                len = snprintf(pos, rst, "   ");
    775661                        else
    776                                 len = ntstdio_snprintf(pos, rst, "  -");
     662                                len = snprintf(pos, rst, "  -");
    777663                        pos += len;
    778664                        rst -= len;
    779665                }
    780666
    781                 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);
     667                len = snprintf(pos, rst, ": %s\n", ascii);
    782668                pos += len;
    783669                rst -= len;
    784670
    785                 ntstdio_puts(&ntstdio, line);
     671                puts(line);
    786672        }
    787673
     
    796682        char buf[30];
    797683
    798         ret = shell_clock_gettime(CLOCK_REALTIME, &tp);
     684        ret = clock_gettime(CLOCK_REALTIME, &tp);
    799685        if (ret != 0) {
    800                 ntstdio_printf(&ntstdio, "clock_gettime error %d", ret);
     686                printf("clock_gettime error %d", ret);
    801687                return 0;
    802688        }
     
    804690        memset(buf, 0, sizeof(buf));
    805691        if (ctime_r(&tp.tv_sec, buf) == NULL) {
    806                 ntstdio_printf(&ntstdio, "ctime_r error");
     692                printf("ctime_r error");
    807693                return 0;
    808694        }
     
    812698        buf[ret - 1] = '\0';
    813699
    814         ntstdio_printf(&ntstdio, "%s .%09u\n", buf, tp.tv_nsec);
     700        printf("%s .%09ld\n", buf, tp.tv_nsec);
    815701        return 0;
    816702}
     
    819705{
    820706        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");
    823709                return 0;
    824710        }
    825711        if (strcmp(argv[1], "sys") == 0) {
    826                 ntstdio_printf(&ntstdio, TARGET_NAME" Monitor\n");
     712                printf(TARGET_NAME" Monitor\n");
    827713                return 0;
    828714        }
     
    830716                int mj, mn, bd;
    831717                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");
    836722        return -1;
    837723}
Note: See TracChangeset for help on using the changeset viewer.