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