Ignore:
Timestamp:
Jan 21, 2018, 12:10:09 AM (6 years ago)
Author:
coas-nagasima
Message:

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

File:
1 edited

Legend:

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

    r321 r331  
    3131 */
    3232
     33#include "shellif.h"
    3334#include <kernel.h>
     35#include <string.h>
    3436#include <t_syslog.h>
    3537#include <t_stdlib.h>
    3638#include <sil.h>
    37 #include <stdlib.h>
    38 #include <string.h>
    39 #include <stdio.h>
    40 #include <setjmp.h>
    4139#include "syssvc/syslog.h"
    4240#include "kernel_cfg.h"
     
    4644#include "ff.h"
    4745#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"
    5049
    5150extern ntstdio_t ntstdio;
    5251extern int ntshell_exit;
     52
     53/* musl_getopt from msul */
     54char *optarg;
     55int /*optind=1, opterr=1, */optopt, optpos, optreset=0;
     56extern int optind, opterr;
     57
     58static 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
     65static 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
     73static 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}
    53148
    54149int shell_kill(int pid, int sig)
     
    99194        size_t i, j;
    100195        if (!s || !*s) return ".";
    101         i = strlen(s) - 1;
     196        i = ntlibc_strlen(s) - 1;
    102197        for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
    103198        for (; i&&s[i] == '/'; i--) s[i] = 0;
     
    111206        size_t i, j;
    112207        if (!s || !*s) return ".";
    113         i = strlen(s) - 1;
     208        i = ntlibc_strlen(s) - 1;
    114209        for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
    115210        for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
     
    141236        }
    142237
    143         strlcat(path, "\n", sizeof(path));
     238        ntlibc_strlcat(path, "\n", sizeof(path));
    144239        ntstdio_printf(&ntstdio, path);
    145240
     
    287382        char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
    288383        char default_pattern[_MAX_LFN] = "";
    289         int i;
     384        int c;
    290385        BYTE list_option = 0;
    291386
    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;
    304391                        break;
    305                 }
    306         }
     392                case 'l':
     393                        list_option |= LS_LONG;
     394                        break;
     395                default:
     396                        break;
     397                }
     398        }
     399        pattern_p = *(argv + optind);
    307400
    308401        if (pattern_p != NULL)
    309                 strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
     402                ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
    310403        basename_p = basename(pattern_p);
    311404        dirname_p = dirname(default_pattern);
    312405        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))))
    314407        {
    315408                basename_p = NULL;
     
    499592}
    500593
     594extern WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */
     595extern 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
     606enum {
     607        HEXDUMP_OPT_DEFAULT = 1 << 0,
     608        HEXDUMP_OPT_UTF8 = 1 << 1,
     609};
     610
    501611int usrcmd_hexdump(int argc, char **argv)
    502612{
    503613        FRESULT res;
    504614        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];
    507617        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) {
    514656                put_rc("f_open", res);
    515657                return 0;
    516658        }
    517659
    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) {
    519671                ascii[0] = '\0';
    520672                line[0] = '\0';
     
    526678
    527679                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) {
    529681                        put_rc("f_read", res);
    530682                        f_close(&fsrc);
    531683                        return 0;
    532684                }
    533                 data[16] = '\0';
     685                data[br] = '\0';
     686                f_lseek(&fsrc, i + 16);
     687                if (br > 16) br = 16;
    534688
    535689                char *apos = ascii;
     
    544698                        rst -= len;
    545699
     700                        len = 0;
     701                        utf8_done_flg = 0;
     702                        ccolor = CCOLOR_RESET;
     703
    546704                        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;
    551707                        }
    552708                        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
    555767                        apos += len;
    556768                        arst -= len;
     
    570782                rst -= len;
    571783
    572                 ntstdio_printf(&ntstdio, line);
     784                ntstdio_puts(&ntstdio, line);
    573785        }
    574786
     
    584796                return 0;
    585797        }
    586         if (strcmp(argv[1], "sys") == 0) {
     798        if (ntlibc_strcmp(argv[1], "sys") == 0) {
    587799                ntstdio_printf(&ntstdio, "GR-PEACH Monitor\n");
    588800                return 0;
    589801        }
    590         if (strcmp(argv[1], "ver") == 0) {
     802        if (ntlibc_strcmp(argv[1], "ver") == 0) {
    591803                int mj, mn, bd;
    592804                ntshell_version(&mj, &mn, &bd);
Note: See TracChangeset for help on using the changeset viewer.