[352] | 1 | /**
|
---|
| 2 | * @file usrcmd.c
|
---|
| 3 | * @author CuBeatSystems
|
---|
| 4 | * @author Shinichiro Nakamura
|
---|
| 5 | * @copyright
|
---|
| 6 | * ===============================================================
|
---|
| 7 | * Natural Tiny Shell (NT-Shell) Version 0.3.1
|
---|
| 8 | * ===============================================================
|
---|
| 9 | * Copyright (c) 2010-2016 Shinichiro Nakamura
|
---|
| 10 | *
|
---|
| 11 | * Permission is hereby granted, free of charge, to any person
|
---|
| 12 | * obtaining a copy of this software and associated documentation
|
---|
| 13 | * files (the "Software"), to deal in the Software without
|
---|
| 14 | * restriction, including without limitation the rights to use,
|
---|
| 15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
| 16 | * sell copies of the Software, and to permit persons to whom the
|
---|
| 17 | * Software is furnished to do so, subject to the following
|
---|
| 18 | * conditions:
|
---|
| 19 | *
|
---|
| 20 | * The above copyright notice and this permission notice shall be
|
---|
| 21 | * included in all copies or substantial portions of the Software.
|
---|
| 22 | *
|
---|
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include "shellif.h"
|
---|
| 34 | #include <kernel.h>
|
---|
| 35 | #include <string.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <t_syslog.h>
|
---|
| 38 | #include <t_stdlib.h>
|
---|
| 39 | #include <sil.h>
|
---|
| 40 | #include "syssvc/syslog.h"
|
---|
| 41 | #include "target_syssvc.h"
|
---|
| 42 | #include "kernel_cfg.h"
|
---|
| 43 | #include "main.h"
|
---|
| 44 | #include "ffarch.h"
|
---|
| 45 | #include "ff.h"
|
---|
| 46 | #include "usrcmd.h"
|
---|
| 47 | #include "core/ntshell.h"
|
---|
| 48 | #include "core/ntlibc.h"
|
---|
[374] | 49 | #include <stdio.h>
|
---|
| 50 | #include <getopt.h>
|
---|
[352] | 51 |
|
---|
| 52 | extern int ntshell_exit;
|
---|
| 53 |
|
---|
| 54 | void put_rc(const char *func, FRESULT rc)
|
---|
| 55 | {
|
---|
| 56 | const char *p =
|
---|
| 57 | "OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0"
|
---|
| 58 | "DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0"
|
---|
| 59 | "NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0"
|
---|
| 60 | "NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0";
|
---|
| 61 | FRESULT i;
|
---|
| 62 |
|
---|
| 63 | for (i = 0; i != rc && *p; i++) {
|
---|
| 64 | while (*p++);
|
---|
| 65 | }
|
---|
[374] | 66 | printf("%s() =>%u FR_%s\n", func, (UINT)rc, p);
|
---|
[352] | 67 | }
|
---|
| 68 |
|
---|
| 69 | void put_drc(const char *func, DRESULT rc)
|
---|
| 70 | {
|
---|
| 71 | const char *p =
|
---|
| 72 | "Successful\0R/W Error\0Write Protected\0Not Ready\0Invalid Parameter\0";
|
---|
| 73 | DRESULT i;
|
---|
| 74 |
|
---|
| 75 | for (i = 0; i != rc && *p; i++) {
|
---|
| 76 | while (*p++);
|
---|
| 77 | }
|
---|
[374] | 78 | printf("%s() =>%u %s\n", func, (UINT)rc, p);
|
---|
[352] | 79 | }
|
---|
| 80 |
|
---|
| 81 | char *basename(char *s)
|
---|
| 82 | {
|
---|
| 83 | size_t i, j;
|
---|
| 84 | if (!s || !*s) return ".";
|
---|
| 85 | i = strlen(s) - 1;
|
---|
| 86 | for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
|
---|
| 87 | for (; i&&s[i] == '/'; i--) s[i] = 0;
|
---|
| 88 | for (; i&&s[i - 1] != '/'; i--);
|
---|
| 89 | return s + i;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | char *dirname(char *_s)
|
---|
| 93 | {
|
---|
| 94 | char *s = _s;
|
---|
| 95 | size_t i, j;
|
---|
| 96 | if (!s || !*s) return ".";
|
---|
| 97 | i = strlen(s) - 1;
|
---|
| 98 | for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
|
---|
| 99 | for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
|
---|
| 100 | for (; s[i] != '/'; i--) if (!i) return (s == _s) ? "." : _s;
|
---|
| 101 | for (; s[i] == '/'; i--) if (!i) { if (s == _s) return "/"; else break; }
|
---|
| 102 | s[i + 1] = 0;
|
---|
| 103 | return _s;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int usrcmd_cd(int argc, char **argv)
|
---|
| 107 | {
|
---|
| 108 | FRESULT res;
|
---|
| 109 |
|
---|
| 110 | if (argc == 2) {
|
---|
| 111 | if ((res = f_chdir(argv[1])) != FR_OK) {
|
---|
| 112 | put_rc("f_chdir", res);
|
---|
| 113 | return 0;
|
---|
| 114 | }
|
---|
| 115 | if ((res = f_chdrive(argv[1]) != FR_OK)) {
|
---|
| 116 | put_rc("f_chdrive", res);
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | char path[256];
|
---|
| 122 | if ((res = f_getcwd(path, sizeof(path))) != FR_OK) {
|
---|
| 123 | put_rc("f_getcwd", res);
|
---|
| 124 | return 0;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | ntlibc_strlcat(path, "\n", sizeof(path));
|
---|
[374] | 128 | printf(path);
|
---|
[352] | 129 |
|
---|
| 130 | return 0;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | #define LS_ALL 0x01
|
---|
| 134 | #define LS_LONG 0x02
|
---|
| 135 | /* lsコマンド 1行表示 */
|
---|
| 136 | void print_one_list(FILINFO *fno, BYTE list_option)
|
---|
| 137 | {
|
---|
| 138 | char *fn;
|
---|
| 139 |
|
---|
[374] | 140 | #if FF_USE_LFN
|
---|
[352] | 141 | fn = *fno->lfname ? fno->lfname : fno->fname;
|
---|
| 142 | #else
|
---|
| 143 | fn = fno->fname;
|
---|
| 144 | #endif
|
---|
| 145 | if (!(list_option & LS_ALL)) {
|
---|
| 146 | if ((fno->fattrib & AM_HID) || (fn[0] == '.'))
|
---|
| 147 | return;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | if (list_option & LS_LONG) {
|
---|
[374] | 151 | printf("%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",
|
---|
[352] | 152 | (fno->fattrib & AM_DIR) ? 'd' : '-',
|
---|
| 153 | (fno->fattrib & AM_RDO) ? 'r' : '-',
|
---|
| 154 | (fno->fattrib & AM_HID) ? 'h' : '-',
|
---|
| 155 | (fno->fattrib & AM_SYS) ? 's' : '-',
|
---|
| 156 | (fno->fattrib & AM_ARC) ? 'a' : '-',
|
---|
| 157 | ((fno->fdate & 0xFE00) >> 9) + 1980,
|
---|
| 158 | ((fno->fdate & 0x01E0) >> 5),
|
---|
| 159 | ( fno->fdate & 0x001F),
|
---|
| 160 | ((fno->ftime & 0xF800) >> 11),
|
---|
| 161 | ((fno->ftime & 0x07E0) >> 5),
|
---|
| 162 | ( fno->ftime & 0x001F));
|
---|
| 163 |
|
---|
| 164 | if (fno->fattrib & AM_DIR) { /* It is a directory */
|
---|
[374] | 165 | printf("%10s ", " ");
|
---|
[352] | 166 | }
|
---|
| 167 | else {
|
---|
[374] | 168 | printf("%10lu ", fno->fsize);
|
---|
[352] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (fno->fattrib & AM_DIR) { /* It is a directory */
|
---|
[374] | 173 | printf("\x1B[32m%s\x1B[0m\n", fn);
|
---|
[352] | 174 | }
|
---|
| 175 | else {
|
---|
[374] | 176 | printf("%s\n", fn);
|
---|
[352] | 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[374] | 180 | #define LFN_BUF_SIZE (FF_MAX_LFN + 1)
|
---|
[352] | 181 | /* lsコマンド dir内 表示 */
|
---|
| 182 | void print_ls(char *path_p, char *pattern_p, BYTE list_option)
|
---|
| 183 | {
|
---|
| 184 | FRESULT res;
|
---|
| 185 | FILINFO fno;
|
---|
[374] | 186 | FATFS_DIR dir;
|
---|
[352] | 187 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
| 188 |
|
---|
| 189 | if ((pattern_p != NULL) && (pattern_p[0] == '\0'))
|
---|
| 190 | pattern_p = NULL;
|
---|
| 191 |
|
---|
| 192 | char *path_backup = NULL;
|
---|
| 193 | path_backup = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 194 | if (path_backup == NULL) {
|
---|
[374] | 195 | printf("ff_memalloc err.\n");
|
---|
[352] | 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[374] | 199 | #if FF_USE_LFN
|
---|
[352] | 200 | char *lfn = NULL;
|
---|
| 201 | lfn = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 202 | if (lfn == NULL) {
|
---|
[374] | 203 | printf("ff_memalloc err.\n");
|
---|
[352] | 204 | ff_memfree(path_backup);
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
| 207 | fno.lfname = lfn;
|
---|
| 208 | fno.lfsize = LFN_BUF_SIZE;
|
---|
| 209 | #endif
|
---|
| 210 |
|
---|
| 211 | if ((path_p != NULL) && (pattern_p == NULL)) {
|
---|
| 212 | if ((res = f_opendir(&dir, path_p)) != FR_OK)
|
---|
| 213 | put_rc("f_opendir", res);
|
---|
| 214 | res = f_readdir(&dir, &fno);
|
---|
| 215 | }
|
---|
| 216 | else {
|
---|
| 217 | res = f_findfirst(&dir, &fno, path_p, pattern_p);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | while ((res == FR_OK) && (fno.fname[0] != 0)) {
|
---|
[374] | 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
|
---|
[352] | 223 | fn = *fno.lfname ? fno.lfname : fno.fname;
|
---|
| 224 | #else
|
---|
| 225 | fn = fno->fname;
|
---|
| 226 | #endif
|
---|
| 227 | if ((res = f_getcwd(path_backup, LFN_BUF_SIZE)) != FR_OK) {
|
---|
| 228 | put_rc("f_getcwd", res);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | if ((res = f_chdrive(path_p)) != FR_OK) {
|
---|
| 232 | put_rc("f_chdrive", res);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | if ((res = f_chdir(path_p)) != FR_OK) {
|
---|
| 236 | put_rc("f_chdir", res);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[374] | 239 | printf("\n%s/%s:\n", path_p, fn);
|
---|
[352] | 240 |
|
---|
| 241 | print_ls(fn, NULL, list_option);
|
---|
| 242 |
|
---|
[374] | 243 | printf("\n");
|
---|
[352] | 244 |
|
---|
| 245 | if ((res = f_chdrive(path_backup)) != FR_OK) {
|
---|
| 246 | put_rc("f_chdrive", res);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if ((res = f_chdir(path_backup)) != FR_OK) {
|
---|
| 250 | put_rc("f_chdir", res);
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | else {
|
---|
| 254 | print_one_list(&fno, list_option);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | if (pattern_p == NULL)
|
---|
| 258 | res = f_readdir(&dir, &fno); /* all */
|
---|
| 259 | else
|
---|
| 260 | res = f_findnext(&dir, &fno); /* pattern matching */
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | f_closedir(&dir);
|
---|
| 264 |
|
---|
| 265 | if (lfn != NULL) ff_memfree(lfn);
|
---|
| 266 | if (path_backup != NULL) ff_memfree(path_backup);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | int usrcmd_ls(int argc, char **argv)
|
---|
| 270 | {
|
---|
| 271 | char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
|
---|
[374] | 272 | char default_pattern[FF_MAX_LFN] = "";
|
---|
[352] | 273 | int c;
|
---|
| 274 | BYTE list_option = 0;
|
---|
| 275 |
|
---|
[374] | 276 | while ((c = getopt(argc, argv, "al")) != -1) {
|
---|
[352] | 277 | switch (c) {
|
---|
| 278 | case 'a':
|
---|
| 279 | list_option |= LS_ALL;
|
---|
| 280 | break;
|
---|
| 281 | case 'l':
|
---|
| 282 | list_option |= LS_LONG;
|
---|
| 283 | break;
|
---|
| 284 | default:
|
---|
| 285 | break;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 | pattern_p = *(argv + optind);
|
---|
| 289 |
|
---|
| 290 | if (pattern_p != NULL)
|
---|
| 291 | ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
|
---|
| 292 | basename_p = basename(pattern_p);
|
---|
| 293 | dirname_p = dirname(default_pattern);
|
---|
| 294 | if (((dirname_p[0] == '/') && (basename_p[0] == '/')) ||
|
---|
| 295 | (!ntlibc_strncmp(dirname_p, ".", strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", strlen(basename_p))))
|
---|
| 296 | {
|
---|
| 297 | basename_p = NULL;
|
---|
| 298 | }
|
---|
| 299 | print_ls(dirname_p, basename_p, list_option);
|
---|
| 300 |
|
---|
| 301 | return 0;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | int usrcmd_cp(int argc, char **argv)
|
---|
| 305 | {
|
---|
| 306 | char *src_str_p = NULL, *dst_str_p = NULL;
|
---|
| 307 | unsigned char i;
|
---|
| 308 | FRESULT res;
|
---|
| 309 | FILINFO fno;
|
---|
| 310 | FIL src_fp, dst_fp;
|
---|
| 311 | size_t read_size, write_size;
|
---|
| 312 | char *lfn = NULL, *local_buff = NULL, *dst_mod_str_p = NULL;
|
---|
| 313 | char *src_basename_p;
|
---|
| 314 |
|
---|
| 315 | if (argc < 2)
|
---|
| 316 | return 0;
|
---|
| 317 |
|
---|
| 318 | /* 引数チェック */
|
---|
| 319 | for (i = 1; i < argc; i++) {
|
---|
| 320 | if (argv[i][0] == '-')
|
---|
| 321 | continue;
|
---|
| 322 | if (argv[i][0] != '\0') {
|
---|
| 323 | if (src_str_p == NULL)
|
---|
| 324 | src_str_p = &argv[i][0];
|
---|
| 325 | else {
|
---|
| 326 | dst_str_p = &argv[i][0];
|
---|
| 327 | break;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | if ((src_str_p == NULL) || (dst_str_p == NULL))
|
---|
| 332 | return 0;
|
---|
| 333 |
|
---|
[374] | 334 | #if FF_USE_LFN
|
---|
[352] | 335 | /* LFN buffer alloc */
|
---|
| 336 | lfn = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 337 | if (lfn == NULL) {
|
---|
[374] | 338 | printf("alloc err.\n");
|
---|
[352] | 339 | goto cp_end;
|
---|
| 340 | }
|
---|
| 341 | fno.lfname = lfn;
|
---|
| 342 | fno.lfsize = LFN_BUF_SIZE;
|
---|
| 343 | #endif
|
---|
| 344 |
|
---|
| 345 | /* copy buffer alloc */
|
---|
| 346 | local_buff = ff_memalloc(64);
|
---|
| 347 | if (local_buff == NULL) {
|
---|
[374] | 348 | printf("alloc err.\n");
|
---|
[352] | 349 | goto cp_end;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /*************/
|
---|
| 353 | /* src check */
|
---|
| 354 | /*************/
|
---|
| 355 | res = f_stat(src_str_p, &fno);
|
---|
| 356 | if (res != FR_OK) {
|
---|
| 357 | if (res == FR_NO_FILE)
|
---|
[374] | 358 | printf("src no file.\n");
|
---|
[352] | 359 | else
|
---|
[374] | 360 | printf("src stat err(%d).\n", res);
|
---|
[352] | 361 | goto cp_end;
|
---|
| 362 | }
|
---|
| 363 | if (fno.fattrib & AM_DIR) { /* src is dir */
|
---|
| 364 | /*******************************************************/ /* from dir */ /* 未実装 */
|
---|
| 365 | }
|
---|
| 366 | else { /* src is file */
|
---|
| 367 | res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
|
---|
| 368 | if (res != FR_OK) {
|
---|
[374] | 369 | printf("src open err(%d).\n", res);
|
---|
[352] | 370 | goto cp_end;
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /*************/
|
---|
| 375 | /* dst check */
|
---|
| 376 | /*************/
|
---|
| 377 | res = f_stat(dst_str_p, &fno);
|
---|
| 378 | if (res != FR_NO_FILE) {
|
---|
| 379 | if (res == FR_OK) {
|
---|
| 380 | if (fno.fattrib & AM_DIR) { /* dst is dir */
|
---|
| 381 | src_basename_p = basename(src_str_p);
|
---|
| 382 | dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 383 | if (dst_mod_str_p == NULL) {
|
---|
[374] | 384 | printf("alloc err.\n");
|
---|
[352] | 385 | goto cp_end;
|
---|
| 386 | }
|
---|
[374] | 387 | snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);
|
---|
[352] | 388 | dst_str_p = dst_mod_str_p;
|
---|
| 389 | }
|
---|
| 390 | else {
|
---|
[374] | 391 | printf("dst file exists.\n");
|
---|
[352] | 392 | goto cp_end_1;
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 | else {
|
---|
[374] | 396 | printf("src stat err(%d).\n", res);
|
---|
[352] | 397 | goto cp_end_1;
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 | res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
|
---|
| 401 | if (res != FR_OK) {
|
---|
[374] | 402 | printf("dst open err(%d).\n", res);
|
---|
[352] | 403 | goto cp_end_1;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /********/
|
---|
| 407 | /* copy */
|
---|
| 408 | /********/
|
---|
| 409 | do {
|
---|
| 410 | /* read from src */
|
---|
| 411 | res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
|
---|
| 412 | if (res != FR_OK) {
|
---|
[374] | 413 | printf("src read err(%d).\n", res);
|
---|
[352] | 414 | goto cp_end_2;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | /* write to dst */
|
---|
| 418 | res = f_write(&dst_fp, local_buff, read_size, &write_size);
|
---|
| 419 | if (res != FR_OK) {
|
---|
[374] | 420 | printf("dst write err(%d).\n", res);
|
---|
[352] | 421 | goto cp_end_2;
|
---|
| 422 | }
|
---|
| 423 | if (read_size != write_size) {
|
---|
[374] | 424 | printf("dst write err(disk full).\n");
|
---|
[352] | 425 | goto cp_end_2;
|
---|
| 426 | }
|
---|
| 427 | } while (read_size == sizeof(local_buff));
|
---|
| 428 |
|
---|
| 429 | cp_end_2:
|
---|
| 430 | f_close(&dst_fp);
|
---|
| 431 | cp_end_1:
|
---|
| 432 | f_close(&src_fp);
|
---|
| 433 | cp_end:
|
---|
| 434 | if(dst_mod_str_p != NULL) ff_memfree(dst_mod_str_p);
|
---|
| 435 | if(local_buff != NULL) ff_memfree(local_buff);
|
---|
| 436 | if(lfn != NULL) ff_memfree(lfn);
|
---|
| 437 |
|
---|
| 438 | return 0;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | int usrcmd_rm(int argc, char **argv)
|
---|
| 442 | {
|
---|
| 443 | FRESULT res;
|
---|
| 444 |
|
---|
| 445 | if (argc == 2) {
|
---|
| 446 | if ((res = f_unlink(argv[1])) != FR_OK) {
|
---|
| 447 | put_rc("f_unlink", res);
|
---|
| 448 | return 0;
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | return 0;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | int usrcmd_mv(int argc, char **argv)
|
---|
| 456 | {
|
---|
| 457 | FRESULT res;
|
---|
| 458 |
|
---|
| 459 | if (argc == 3) {
|
---|
| 460 | if ((res = f_rename(argv[1], argv[2])) != FR_OK) {
|
---|
| 461 | put_rc("f_rename", res);
|
---|
| 462 | return 0;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | return 0;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | int usrcmd_mkdir(int argc, char **argv)
|
---|
| 470 | {
|
---|
| 471 | FRESULT res;
|
---|
| 472 |
|
---|
| 473 | if (argc == 2) {
|
---|
| 474 | if ((res = f_mkdir(argv[1])) != FR_OK) {
|
---|
| 475 | put_rc("f_mkdir", res);
|
---|
| 476 | return 0;
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | return 0;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | #define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */
|
---|
| 484 | #define CCOLOR_RESET 0
|
---|
| 485 | #define CCOLOR_BLACK 30
|
---|
| 486 | #define CCOLOR_RED 31
|
---|
| 487 | #define CCOLOR_GREEN 32
|
---|
| 488 | #define CCOLOR_YELLOW 33
|
---|
| 489 | #define CCOLOR_BLUE 34
|
---|
| 490 | #define CCOLOR_MAGENTA 35
|
---|
| 491 | #define CCOLOR_CYAN 36
|
---|
| 492 | #define CCOLOR_WHITE 37
|
---|
| 493 | enum {
|
---|
| 494 | HEXDUMP_OPT_DEFAULT = 1 << 0,
|
---|
| 495 | HEXDUMP_OPT_UTF8 = 1 << 1,
|
---|
| 496 | };
|
---|
| 497 |
|
---|
| 498 | int usrcmd_hexdump(int argc, char **argv)
|
---|
| 499 | {
|
---|
| 500 | FRESULT res;
|
---|
| 501 | FIL fsrc;
|
---|
| 502 | char data[16 + 1 + HEXDUMP_EXTRA_FOR_UTF8];
|
---|
| 503 | 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];
|
---|
| 504 | 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)];
|
---|
| 505 | TCHAR *filename_p = NULL;
|
---|
| 506 | int op, option_flag = HEXDUMP_OPT_DEFAULT;
|
---|
| 507 | char *option_ptr, *option_endptr, ccolor, utf8_done_flg, utf8_odd_bytes = 0;
|
---|
| 508 | unsigned int op_offset = 0, op_size = 0, op_end = 0;
|
---|
| 509 |
|
---|
[374] | 510 | while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) {
|
---|
[352] | 511 | switch (op) {
|
---|
| 512 | case 'h': /* help */
|
---|
[374] | 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");
|
---|
[352] | 519 | break;
|
---|
| 520 | case 'd': /* print one byte character [in character area] (default) */
|
---|
| 521 | option_flag |= HEXDUMP_OPT_DEFAULT;
|
---|
| 522 | break;
|
---|
| 523 | case 'u': /* try print UTF-8 code [in character area] */
|
---|
| 524 | option_flag |= HEXDUMP_OPT_UTF8;
|
---|
| 525 | break;
|
---|
| 526 | case 'o': /* print start offset address from top */
|
---|
| 527 | option_ptr = *(argv + optind);
|
---|
| 528 | op_offset = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
|
---|
| 529 | break;
|
---|
| 530 | case 's': /* print size */
|
---|
| 531 | option_ptr = *(argv + optind);
|
---|
| 532 | op_size = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
|
---|
| 533 | break;
|
---|
| 534 | default:
|
---|
| 535 | break;
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 | filename_p = *(argv + optind);
|
---|
| 539 | if (filename_p == NULL)
|
---|
| 540 | return 0;
|
---|
| 541 |
|
---|
| 542 | if ((res = f_open(&fsrc, filename_p, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
|
---|
| 543 | put_rc("f_open", res);
|
---|
| 544 | return 0;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* position adjusting */
|
---|
| 548 | if (op_offset >= fsrc.fsize) {
|
---|
[374] | 549 | printf("error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize);
|
---|
[352] | 550 | return 0;
|
---|
| 551 | }
|
---|
| 552 | op_end = op_offset + op_size;
|
---|
| 553 | if ((op_size == 0) || (op_end >= fsrc.fsize))
|
---|
| 554 | op_end = fsrc.fsize;
|
---|
| 555 | f_lseek(&fsrc, op_offset);
|
---|
| 556 |
|
---|
| 557 | for (int i = op_offset; i < op_end; i += 16) {
|
---|
| 558 | ascii[0] = '\0';
|
---|
| 559 | line[0] = '\0';
|
---|
| 560 | char *pos = line;
|
---|
| 561 | int rst = sizeof(line);
|
---|
[374] | 562 | int len = snprintf(pos, rst, "%08X: ", i);
|
---|
[352] | 563 | pos += len;
|
---|
| 564 | rst -= len;
|
---|
| 565 |
|
---|
| 566 | UINT br = 0;
|
---|
| 567 | if ((res = f_read(&fsrc, data, 16 + HEXDUMP_EXTRA_FOR_UTF8, &br)) != FR_OK) {
|
---|
| 568 | put_rc("f_read", res);
|
---|
| 569 | f_close(&fsrc);
|
---|
| 570 | return 0;
|
---|
| 571 | }
|
---|
| 572 | data[br] = '\0';
|
---|
| 573 | f_lseek(&fsrc, i + 16);
|
---|
| 574 | if (br > 16) br = 16;
|
---|
| 575 |
|
---|
| 576 | char *apos = ascii;
|
---|
| 577 | int arst = sizeof(ascii);
|
---|
| 578 | for (int j = 0; j < br; j++) {
|
---|
| 579 | char c = data[j];
|
---|
| 580 | if (j != 7)
|
---|
[374] | 581 | len = snprintf(pos, rst, "%02X ", c);
|
---|
[352] | 582 | else
|
---|
[374] | 583 | len = snprintf(pos, rst, "%02X-", c);
|
---|
[352] | 584 | pos += len;
|
---|
| 585 | rst -= len;
|
---|
| 586 |
|
---|
| 587 | len = 0;
|
---|
| 588 | utf8_done_flg = 0;
|
---|
| 589 | ccolor = CCOLOR_RESET;
|
---|
| 590 |
|
---|
| 591 | if (c < 0x20) {
|
---|
| 592 | ccolor = CCOLOR_RED;
|
---|
| 593 | c = c + 0x40;
|
---|
| 594 | }
|
---|
| 595 | else {
|
---|
| 596 | if (option_flag & HEXDUMP_OPT_UTF8) { /* try UTF-8 */
|
---|
| 597 | /* check character code */
|
---|
| 598 | int bf_utf8_bytes, af_utf8_bytes;
|
---|
| 599 | unsigned char utf8_code[4];
|
---|
| 600 | WCHAR utf16_code;
|
---|
| 601 |
|
---|
| 602 | utf16_code = Utf8_to_Utf16(&data[j], &bf_utf8_bytes); /* try UTF-8 -> UTF-16 */
|
---|
| 603 | Utf16_to_Utf8(utf8_code, &af_utf8_bytes, (UINT)utf16_code); /* try UTF-16 -> UTF-8 */
|
---|
| 604 | if ((af_utf8_bytes <= 3 && bf_utf8_bytes == af_utf8_bytes) &&
|
---|
| 605 | !memcmp(&data[j], utf8_code, af_utf8_bytes)) { /* size & code match */
|
---|
| 606 | utf8_done_flg = 1;
|
---|
| 607 | for (int k = 1; k < af_utf8_bytes; k++) { /* pos */
|
---|
| 608 | if (j + k >= 16)
|
---|
| 609 | break;
|
---|
| 610 | if (j + k != 7)
|
---|
[374] | 611 | len = snprintf(pos, rst, "%02X ", data[j + k]);
|
---|
[352] | 612 | else
|
---|
[374] | 613 | len = snprintf(pos, rst, "%02X-", data[j + k]);
|
---|
[352] | 614 | pos += len;
|
---|
| 615 | rst -= len;
|
---|
| 616 | }
|
---|
| 617 | memcpy(apos, &data[j], af_utf8_bytes); /* apos */
|
---|
| 618 | apos[af_utf8_bytes] = 0;
|
---|
| 619 | len = af_utf8_bytes;
|
---|
| 620 | j += af_utf8_bytes - 1;
|
---|
| 621 | if (af_utf8_bytes > 1) {
|
---|
| 622 | utf8_odd_bytes = j < 15 ? af_utf8_bytes - 2 : af_utf8_bytes - 1;
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | if (utf8_odd_bytes > 0 && j < 15) {
|
---|
| 629 | apos += len;
|
---|
| 630 | arst -= len;
|
---|
[374] | 631 | len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');
|
---|
[352] | 632 | utf8_odd_bytes--;
|
---|
| 633 | }
|
---|
| 634 | else if (utf8_done_flg == 0) {
|
---|
| 635 | if (c >= 0x80 && c < 0x9F) {
|
---|
| 636 | ccolor = CCOLOR_RED;
|
---|
| 637 | c = c - 0x60;
|
---|
| 638 | }
|
---|
| 639 | else if (c >= 0xA0 && c < 0xDF) {
|
---|
| 640 | ccolor = CCOLOR_GREEN;
|
---|
| 641 | c = c - 0x60;
|
---|
| 642 | }
|
---|
| 643 | else if (c >= 0xE0 && c < 0xFF) {
|
---|
| 644 | ccolor = CCOLOR_GREEN;
|
---|
| 645 | c = c - 0xC0;
|
---|
| 646 | }
|
---|
| 647 | else if (c == 0x7F || c == 0x9F || c == 0xDF || c == 0xFF) {
|
---|
| 648 | ccolor = CCOLOR_CYAN;
|
---|
| 649 | c = '?';
|
---|
| 650 | }
|
---|
[374] | 651 | len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);
|
---|
[352] | 652 | }
|
---|
| 653 |
|
---|
| 654 | apos += len;
|
---|
| 655 | arst -= len;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | for (int j = br; j < 16; j++) {
|
---|
| 659 | if (j != 7)
|
---|
[374] | 660 | len = snprintf(pos, rst, " ");
|
---|
[352] | 661 | else
|
---|
[374] | 662 | len = snprintf(pos, rst, " -");
|
---|
[352] | 663 | pos += len;
|
---|
| 664 | rst -= len;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[374] | 667 | len = snprintf(pos, rst, ": %s\n", ascii);
|
---|
[352] | 668 | pos += len;
|
---|
| 669 | rst -= len;
|
---|
| 670 |
|
---|
[374] | 671 | puts(line);
|
---|
[352] | 672 | }
|
---|
| 673 |
|
---|
| 674 | f_close(&fsrc);
|
---|
| 675 | return 0;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | int usrcmd_date(int argc, char **argv)
|
---|
| 679 | {
|
---|
| 680 | int ret;
|
---|
| 681 | struct timespec tp;
|
---|
| 682 | char buf[30];
|
---|
| 683 |
|
---|
[374] | 684 | ret = clock_gettime(CLOCK_REALTIME, &tp);
|
---|
[352] | 685 | if (ret != 0) {
|
---|
[374] | 686 | printf("clock_gettime error %d", ret);
|
---|
[352] | 687 | return 0;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | memset(buf, 0, sizeof(buf));
|
---|
| 691 | if (ctime_r(&tp.tv_sec, buf) == NULL) {
|
---|
[374] | 692 | printf("ctime_r error");
|
---|
[352] | 693 | return 0;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | /* 改行コードの削除 */
|
---|
| 697 | ret = ntlibc_strlen(buf);
|
---|
| 698 | buf[ret - 1] = '\0';
|
---|
| 699 |
|
---|
[374] | 700 | printf("%s .%09ld\n", buf, tp.tv_nsec);
|
---|
[352] | 701 | return 0;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | int usrcmd_info(int argc, char **argv)
|
---|
| 705 | {
|
---|
| 706 | if (argc != 2) {
|
---|
[374] | 707 | printf("info sys\n");
|
---|
| 708 | printf("info ver\n");
|
---|
[352] | 709 | return 0;
|
---|
| 710 | }
|
---|
| 711 | if (strcmp(argv[1], "sys") == 0) {
|
---|
[374] | 712 | printf(TARGET_NAME" Monitor\n");
|
---|
[352] | 713 | return 0;
|
---|
| 714 | }
|
---|
| 715 | if (strcmp(argv[1], "ver") == 0) {
|
---|
| 716 | int mj, mn, bd;
|
---|
| 717 | ntshell_version(&mj, &mn, &bd);
|
---|
[374] | 718 | printf("Version %d.%d.%d\n", mj, mn, bd);
|
---|
[352] | 719 | return 0;
|
---|
| 720 | }
|
---|
[374] | 721 | printf("Unknown sub command found\n");
|
---|
[352] | 722 | return -1;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | int usrcmd_exit(int argc, char **argv)
|
---|
| 726 | {
|
---|
| 727 | ntshell_exit = 1;
|
---|
| 728 |
|
---|
| 729 | return 0;
|
---|
| 730 | }
|
---|