Ignore:
Timestamp:
Jul 13, 2020, 8:07:55 PM (4 years ago)
Author:
coas-nagasima
Message:

ntshellアプリはnewlibを使うよう変更し、syscallの実装部分と区別がつくよう更新。

File:
1 edited

Legend:

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

    r441 r442  
    3232
    3333#include "shellif.h"
     34#include <getopt.h>
    3435#include <kernel.h>
    3536#include <t_syslog.h>
     
    4546#include "core/ntshell.h"
    4647#include "core/ntlibc.h"
    47 #include "util/ntstdio.h"
    48 
    49 extern ntstdio_t *ntstdio;
     48
    5049extern 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 }
    14750
    14851void put_rc(const char *func, FRESULT rc)
     
    15861                while (*p++);
    15962        }
    160         ntstdio_printf(ntstdio, "%s() =>%u FR_%s\n", func, (UINT)rc, p);
     63        printf("%s() =>%u FR_%s\n", func, (UINT)rc, p);
    16164}
    16265
     
    17073                while (*p++);
    17174        }
    172         ntstdio_printf(ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);
     75        printf("%s() =>%u %s\n", func, (UINT)rc, p);
    17376}
    17477
     
    17780        size_t i, j;
    17881        if (!s || !*s) return ".";
    179         i = ntlibc_strlen(s) - 1;
     82        i = strlen(s) - 1;
    18083        for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
    18184        for (; i&&s[i] == '/'; i--) s[i] = 0;
     
    18992        size_t i, j;
    19093        if (!s || !*s) return ".";
    191         i = ntlibc_strlen(s) - 1;
     94        i = strlen(s) - 1;
    19295        for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
    19396        for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
     
    219122        }
    220123
    221         ntlibc_strlcat(path, "\n", sizeof(path));
    222         ntstdio_printf(ntstdio, path);
     124        strlcat(path, "\n", sizeof(path));
     125        printf(path);
    223126
    224127        return 0;
     
    243146
    244147        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 ",
    246149                        (fno->fattrib & AM_DIR) ? 'd' : '-',
    247150                        (fno->fattrib & AM_RDO) ? 'r' : '-',
     
    257160
    258161                if (fno->fattrib & AM_DIR) {                    /* It is a directory */
    259                         ntstdio_printf(ntstdio, "%10s ", " ");
     162                        printf("%10s ", " ");
    260163                }
    261164                else {
    262                         ntstdio_printf(ntstdio, "%10lu ", fno->fsize);
     165                        printf("%10lu ", fno->fsize);
    263166                }
    264167        }
    265168
    266169        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);
    268171        }
    269172        else {
    270                 ntstdio_printf(ntstdio, "%s\n", fn);
     173                printf("%s\n", fn);
    271174        }
    272175}
     
    287190        path_backup = ff_memalloc(LFN_BUF_SIZE);
    288191        if (path_backup == NULL) {
    289                 ntstdio_printf(ntstdio, "ff_memalloc err.\n");
     192                printf("ff_memalloc err.\n");
    290193                return;
    291194        }
     
    295198        lfn = ff_memalloc(LFN_BUF_SIZE);
    296199        if (lfn == NULL) {
    297                 ntstdio_printf(ntstdio, "ff_memalloc err.\n");
     200                printf("ff_memalloc err.\n");
    298201                ff_memfree(path_backup);
    299202                return;
     
    317220                        fn = *fno.lfname ? fno.lfname : fno.fname;
    318221#else
    319                         fn = fno->fname;
     222                        fn = fno.fname;
    320223#endif
    321224                        if ((res = f_getcwd(path_backup, LFN_BUF_SIZE)) != FR_OK) {
     
    331234                        }
    332235
    333                         ntstdio_printf(ntstdio, "\n%s/%s:\n", path_p, fn);
     236                        printf("\n%s/%s:\n", path_p, fn);
    334237
    335238                        print_ls(fn, NULL, list_option);
    336239
    337                         ntstdio_printf(ntstdio, "\n");
     240                        printf("\n");
    338241
    339242                        if ((res = f_chdrive(path_backup)) != FR_OK) {
     
    370273        BYTE list_option = 0;
    371274
    372         while ((c = musl_getopt(argc, argv, "al")) != -1) {
     275        while ((c = getopt(argc, argv, "al")) != -1) {
    373276                switch (c) {
    374277                case 'a':
     
    385288
    386289        if (pattern_p != NULL)
    387                 ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
     290                strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
    388291        basename_p = basename(pattern_p);
    389292        dirname_p = dirname(default_pattern);
    390293        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))))
    392295        {
    393296                basename_p = NULL;
     
    432335        lfn = ff_memalloc(LFN_BUF_SIZE);
    433336        if (lfn == NULL) {
    434                 ntstdio_printf(ntstdio, "alloc err.\n");
     337                printf("alloc err.\n");
    435338                goto cp_end;
    436339        }
     
    442345        local_buff = ff_memalloc(64);
    443346        if (local_buff == NULL) {
    444                 ntstdio_printf(ntstdio, "alloc err.\n");
     347                printf("alloc err.\n");
    445348                goto cp_end;
    446349        }
     
    452355        if (res != FR_OK) {
    453356                if (res == FR_NO_FILE)
    454                         ntstdio_printf(ntstdio, "src no file.\n");
     357                        printf("src no file.\n");
    455358                else
    456                         ntstdio_printf(ntstdio, "src stat err(%d).\n", res);
     359                        printf("src stat err(%d).\n", res);
    457360                goto cp_end;
    458361        }
     
    463366                res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
    464367                if (res != FR_OK) {
    465                         ntstdio_printf(ntstdio, "src open err(%d).\n", res);
     368                        printf("src open err(%d).\n", res);
    466369                        goto cp_end;
    467370                }
     
    478381                                dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
    479382                                if (dst_mod_str_p == NULL) {
    480                                         ntstdio_printf(ntstdio, "alloc err.\n");
     383                                        printf("alloc err.\n");
    481384                                        goto cp_end;
    482385                                }
    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);
    484387                                dst_str_p = dst_mod_str_p;
    485388                        }
    486389                        else {
    487                                 ntstdio_printf(ntstdio, "dst file exists.\n");
     390                                printf("dst file exists.\n");
    488391                                goto cp_end_1;
    489392                        }
    490393                }
    491394                else {
    492                         ntstdio_printf(ntstdio, "src stat err(%d).\n", res);
     395                        printf("src stat err(%d).\n", res);
    493396                        goto cp_end_1;
    494397                }
     
    496399        res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
    497400        if (res != FR_OK) {
    498                 ntstdio_printf(ntstdio, "dst open err(%d).\n", res);
     401                printf("dst open err(%d).\n", res);
    499402                goto cp_end_1;
    500403        }
     
    507410                res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
    508411                if (res != FR_OK) {
    509                         ntstdio_printf(ntstdio, "src read err(%d).\n", res);
     412                        printf("src read err(%d).\n", res);
    510413                        goto cp_end_2;
    511414                }
     
    514417                res = f_write(&dst_fp, local_buff, read_size, &write_size);
    515418                if (res != FR_OK) {
    516                         ntstdio_printf(ntstdio, "dst write err(%d).\n", res);
     419                        printf("dst write err(%d).\n", res);
    517420                        goto cp_end_2;
    518421                }
    519422                if (read_size != write_size) {
    520                         ntstdio_printf(ntstdio, "dst write err(disk full).\n");
     423                        printf("dst write err(disk full).\n");
    521424                        goto cp_end_2;
    522425                }
     
    604507        unsigned int op_offset = 0, op_size = 0, op_end = 0;
    605508
    606         while ((op = musl_getopt(argc, argv, "hduos0123456789xX")) != -1) {
     509        while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) {
    607510                switch (op) {
    608511                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");
    615518                        break;
    616519                case 'd': /* print one byte character [in character area] (default) */
     
    622525                case 'o': /* print start offset address from top */
    623526                        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);
    625528                        break;
    626529                case 's': /* print size */
    627530                        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);
    629532                        break;
    630533                default:
     
    643546        /* position adjusting */
    644547        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);
    646549                return 0;
    647550        }
     
    656559                char *pos = line;
    657560                int rst = sizeof(line);
    658                 int len = ntstdio_snprintf(pos, rst, "%08X: ", i);
     561                int len = snprintf(pos, rst, "%08X: ", i);
    659562                pos += len;
    660563                rst -= len;
     
    675578                        unsigned char c = data[j];
    676579                        if (j != 7)
    677                                 len = ntstdio_snprintf(pos, rst, "%02X ", c);
     580                                len = snprintf(pos, rst, "%02X ", c);
    678581                        else
    679                                 len = ntstdio_snprintf(pos, rst, "%02X-", c);
     582                                len = snprintf(pos, rst, "%02X-", c);
    680583                        pos += len;
    681584                        rst -= len;
     
    705608                                                                break;
    706609                                                        if (j + k != 7)
    707                                                                 len = ntstdio_snprintf(pos, rst, "%02X ", data[j + k]);
     610                                                                len = snprintf(pos, rst, "%02X ", data[j + k]);
    708611                                                        else
    709                                                                 len = ntstdio_snprintf(pos, rst, "%02X-", data[j + k]);
     612                                                                len = snprintf(pos, rst, "%02X-", data[j + k]);
    710613                                                        pos += len;
    711614                                                        rst -= len;
     
    725628                                apos += len;
    726629                                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, ' ');
    728631                                utf8_odd_bytes--;
    729632                        }
     
    745648                                        c = '?';
    746649                                }
    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);
    748651                        }
    749652
     
    754657                for (int j = br; j < 16; j++) {
    755658                        if (j != 7)
    756                                 len = ntstdio_snprintf(pos, rst, "   ");
     659                                len = snprintf(pos, rst, "   ");
    757660                        else
    758                                 len = ntstdio_snprintf(pos, rst, "  -");
     661                                len = snprintf(pos, rst, "  -");
    759662                        pos += len;
    760663                        rst -= len;
    761664                }
    762665
    763                 len = ntstdio_snprintf(pos, rst, ": %s\n", ascii);
     666                len = snprintf(pos, rst, ": %s\n", ascii);
    764667                pos += len;
    765668                rst -= len;
    766669
    767                 ntstdio_puts(ntstdio, line);
     670                printf(line);
    768671        }
    769672
     
    780683        ret = shell_clock_gettime(CLOCK_REALTIME, &tp);
    781684        if (ret != 0) {
    782                 ntstdio_printf(ntstdio, "clock_gettime error %d", ret);
     685                printf("clock_gettime error %d", ret);
    783686                return 0;
    784687        }
     
    786689        memset(buf, 0, sizeof(buf));
    787690        if (ctime_r(&tp.tv_sec, buf) == NULL) {
    788                 ntstdio_printf(ntstdio, "ctime_r error");
     691                printf("ctime_r error");
    789692                return 0;
    790693        }
    791694
    792695        /* 改行コードの削除 */
    793         ret = ntlibc_strlen(buf);
     696        ret = strlen(buf);
    794697        buf[ret - 1] = '\0';
    795698
    796         ntstdio_printf(ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);
     699        printf("%s .%09ld\n", buf, tp.tv_nsec);
    797700        return 0;
    798701}
     
    801704{
    802705        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) {
    812715                int mj, mn, bd;
    813716                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");
    818721        return -1;
    819722}
Note: See TracChangeset for help on using the changeset viewer.