[388] | 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"
|
---|
| 49 | #include <stdio.h>
|
---|
| 50 | #include <getopt.h>
|
---|
| 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 | }
|
---|
| 66 | printf("%s() =>%u FR_%s\n", func, (UINT)rc, p);
|
---|
| 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 | }
|
---|
| 78 | printf("%s() =>%u %s\n", func, (UINT)rc, p);
|
---|
| 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));
|
---|
| 128 | printf(path);
|
---|
| 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 |
|
---|
| 140 | #if FF_USE_LFN
|
---|
| 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) {
|
---|
| 151 | printf("%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",
|
---|
| 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 */
|
---|
| 165 | printf("%10s ", " ");
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | printf("%10lu ", fno->fsize);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (fno->fattrib & AM_DIR) { /* It is a directory */
|
---|
| 173 | printf("\x1B[32m%s\x1B[0m\n", fn);
|
---|
| 174 | }
|
---|
| 175 | else {
|
---|
| 176 | printf("%s\n", fn);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | #define LFN_BUF_SIZE (FF_MAX_LFN + 1)
|
---|
| 181 | /* lsã³ãã³ã dirå
|
---|
| 182 | 表示 */
|
---|
| 183 | void print_ls(char *path_p, char *pattern_p, BYTE list_option)
|
---|
| 184 | {
|
---|
| 185 | FRESULT res;
|
---|
| 186 | FILINFO fno;
|
---|
| 187 | FATFS_DIR dir;
|
---|
| 188 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
| 189 |
|
---|
| 190 | if ((pattern_p != NULL) && (pattern_p[0] == '\0'))
|
---|
| 191 | pattern_p = NULL;
|
---|
| 192 |
|
---|
| 193 | char *path_backup = NULL;
|
---|
| 194 | path_backup = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 195 | if (path_backup == NULL) {
|
---|
| 196 | printf("ff_memalloc err.\n");
|
---|
| 197 | return;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | #if FF_USE_LFN
|
---|
| 201 | char *lfn = NULL;
|
---|
| 202 | lfn = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 203 | if (lfn == NULL) {
|
---|
| 204 | printf("ff_memalloc err.\n");
|
---|
| 205 | ff_memfree(path_backup);
|
---|
| 206 | return;
|
---|
| 207 | }
|
---|
| 208 | fno.lfname = lfn;
|
---|
| 209 | fno.lfsize = LFN_BUF_SIZE;
|
---|
| 210 | #endif
|
---|
| 211 |
|
---|
| 212 | if ((path_p != NULL) && (pattern_p == NULL)) {
|
---|
| 213 | if ((res = f_opendir(&dir, path_p)) != FR_OK)
|
---|
| 214 | put_rc("f_opendir", res);
|
---|
| 215 | res = f_readdir(&dir, &fno);
|
---|
| 216 | }
|
---|
| 217 | else {
|
---|
| 218 | res = f_findfirst(&dir, &fno, path_p, pattern_p);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | while ((res == FR_OK) && (fno.fname[0] != 0)) {
|
---|
| 222 | if (pattern_p != NULL && (fno.fattrib & AM_DIR) && ((fno.fname[0] == '.') ? (pattern_p[0] == '.') : 1)) {/* FATFS_DIR ã¨ãã¿ã¼ã³ããããã¦ããå ´å㯠FATFS_DIR å
|
---|
| 223 | é¨ã ls ãã */
|
---|
| 224 | #if FF_USE_LFN
|
---|
| 225 | fn = *fno.lfname ? fno.lfname : fno.fname;
|
---|
| 226 | #else
|
---|
| 227 | fn = fno->fname;
|
---|
| 228 | #endif
|
---|
| 229 | if ((res = f_getcwd(path_backup, LFN_BUF_SIZE)) != FR_OK) {
|
---|
| 230 | put_rc("f_getcwd", res);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | if ((res = f_chdrive(path_p)) != FR_OK) {
|
---|
| 234 | put_rc("f_chdrive", res);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | if ((res = f_chdir(path_p)) != FR_OK) {
|
---|
| 238 | put_rc("f_chdir", res);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | printf("\n%s/%s:\n", path_p, fn);
|
---|
| 242 |
|
---|
| 243 | print_ls(fn, NULL, list_option);
|
---|
| 244 |
|
---|
| 245 | printf("\n");
|
---|
| 246 |
|
---|
| 247 | if ((res = f_chdrive(path_backup)) != FR_OK) {
|
---|
| 248 | put_rc("f_chdrive", res);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if ((res = f_chdir(path_backup)) != FR_OK) {
|
---|
| 252 | put_rc("f_chdir", res);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | else {
|
---|
| 256 | print_one_list(&fno, list_option);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (pattern_p == NULL)
|
---|
| 260 | res = f_readdir(&dir, &fno); /* all */
|
---|
| 261 | else
|
---|
| 262 | res = f_findnext(&dir, &fno); /* pattern matching */
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | f_closedir(&dir);
|
---|
| 266 |
|
---|
| 267 | if (lfn != NULL) ff_memfree(lfn);
|
---|
| 268 | if (path_backup != NULL) ff_memfree(path_backup);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | int usrcmd_ls(int argc, char **argv)
|
---|
| 272 | {
|
---|
| 273 | char *pattern_p = NULL, *basename_p = NULL, *dirname_p = NULL;
|
---|
| 274 | char default_pattern[FF_MAX_LFN] = "";
|
---|
| 275 | int c;
|
---|
| 276 | BYTE list_option = 0;
|
---|
| 277 |
|
---|
| 278 | while ((c = getopt(argc, argv, "al")) != -1) {
|
---|
| 279 | switch (c) {
|
---|
| 280 | case 'a':
|
---|
| 281 | list_option |= LS_ALL;
|
---|
| 282 | break;
|
---|
| 283 | case 'l':
|
---|
| 284 | list_option |= LS_LONG;
|
---|
| 285 | break;
|
---|
| 286 | default:
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | pattern_p = *(argv + optind);
|
---|
| 291 |
|
---|
| 292 | if (pattern_p != NULL)
|
---|
| 293 | ntlibc_strlcpy(default_pattern, pattern_p, sizeof(default_pattern));
|
---|
| 294 | basename_p = basename(pattern_p);
|
---|
| 295 | dirname_p = dirname(default_pattern);
|
---|
| 296 | if (((dirname_p[0] == '/') && (basename_p[0] == '/')) ||
|
---|
| 297 | (!ntlibc_strncmp(dirname_p, ".", strlen(dirname_p)) && !ntlibc_strncmp(basename_p, ".", strlen(basename_p))))
|
---|
| 298 | {
|
---|
| 299 | basename_p = NULL;
|
---|
| 300 | }
|
---|
| 301 | print_ls(dirname_p, basename_p, list_option);
|
---|
| 302 |
|
---|
| 303 | return 0;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | int usrcmd_cp(int argc, char **argv)
|
---|
| 307 | {
|
---|
| 308 | char *src_str_p = NULL, *dst_str_p = NULL;
|
---|
| 309 | unsigned char i;
|
---|
| 310 | FRESULT res;
|
---|
| 311 | FILINFO fno;
|
---|
| 312 | FIL src_fp, dst_fp;
|
---|
| 313 | size_t read_size, write_size;
|
---|
| 314 | char *lfn = NULL, *local_buff = NULL, *dst_mod_str_p = NULL;
|
---|
| 315 | char *src_basename_p;
|
---|
| 316 |
|
---|
| 317 | if (argc < 2)
|
---|
| 318 | return 0;
|
---|
| 319 |
|
---|
| 320 | /* å¼æ°ãã§ã㯠*/
|
---|
| 321 | for (i = 1; i < argc; i++) {
|
---|
| 322 | if (argv[i][0] == '-')
|
---|
| 323 | continue;
|
---|
| 324 | if (argv[i][0] != '\0') {
|
---|
| 325 | if (src_str_p == NULL)
|
---|
| 326 | src_str_p = &argv[i][0];
|
---|
| 327 | else {
|
---|
| 328 | dst_str_p = &argv[i][0];
|
---|
| 329 | break;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | if ((src_str_p == NULL) || (dst_str_p == NULL))
|
---|
| 334 | return 0;
|
---|
| 335 |
|
---|
| 336 | #if FF_USE_LFN
|
---|
| 337 | /* LFN buffer alloc */
|
---|
| 338 | lfn = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 339 | if (lfn == NULL) {
|
---|
| 340 | printf("alloc err.\n");
|
---|
| 341 | goto cp_end;
|
---|
| 342 | }
|
---|
| 343 | fno.lfname = lfn;
|
---|
| 344 | fno.lfsize = LFN_BUF_SIZE;
|
---|
| 345 | #endif
|
---|
| 346 |
|
---|
| 347 | /* copy buffer alloc */
|
---|
| 348 | local_buff = ff_memalloc(64);
|
---|
| 349 | if (local_buff == NULL) {
|
---|
| 350 | printf("alloc err.\n");
|
---|
| 351 | goto cp_end;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /*************/
|
---|
| 355 | /* src check */
|
---|
| 356 | /*************/
|
---|
| 357 | res = f_stat(src_str_p, &fno);
|
---|
| 358 | if (res != FR_OK) {
|
---|
| 359 | if (res == FR_NO_FILE)
|
---|
| 360 | printf("src no file.\n");
|
---|
| 361 | else
|
---|
| 362 | printf("src stat err(%d).\n", res);
|
---|
| 363 | goto cp_end;
|
---|
| 364 | }
|
---|
| 365 | if (fno.fattrib & AM_DIR) { /* src is dir */
|
---|
| 366 | /*******************************************************/ /* from dir */ /* æªå®è£
|
---|
| 367 | */
|
---|
| 368 | }
|
---|
| 369 | else { /* src is file */
|
---|
| 370 | res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
|
---|
| 371 | if (res != FR_OK) {
|
---|
| 372 | printf("src open err(%d).\n", res);
|
---|
| 373 | goto cp_end;
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /*************/
|
---|
| 378 | /* dst check */
|
---|
| 379 | /*************/
|
---|
| 380 | res = f_stat(dst_str_p, &fno);
|
---|
| 381 | if (res != FR_NO_FILE) {
|
---|
| 382 | if (res == FR_OK) {
|
---|
| 383 | if (fno.fattrib & AM_DIR) { /* dst is dir */
|
---|
| 384 | src_basename_p = basename(src_str_p);
|
---|
| 385 | dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
|
---|
| 386 | if (dst_mod_str_p == NULL) {
|
---|
| 387 | printf("alloc err.\n");
|
---|
| 388 | goto cp_end;
|
---|
| 389 | }
|
---|
| 390 | snprintf(dst_mod_str_p, LFN_BUF_SIZE, "%s/%s\0", dst_str_p, src_basename_p);
|
---|
| 391 | dst_str_p = dst_mod_str_p;
|
---|
| 392 | }
|
---|
| 393 | else {
|
---|
| 394 | printf("dst file exists.\n");
|
---|
| 395 | goto cp_end_1;
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 | else {
|
---|
| 399 | printf("src stat err(%d).\n", res);
|
---|
| 400 | goto cp_end_1;
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
|
---|
| 404 | if (res != FR_OK) {
|
---|
| 405 | printf("dst open err(%d).\n", res);
|
---|
| 406 | goto cp_end_1;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /********/
|
---|
| 410 | /* copy */
|
---|
| 411 | /********/
|
---|
| 412 | do {
|
---|
| 413 | /* read from src */
|
---|
| 414 | res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
|
---|
| 415 | if (res != FR_OK) {
|
---|
| 416 | printf("src read err(%d).\n", res);
|
---|
| 417 | goto cp_end_2;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | /* write to dst */
|
---|
| 421 | res = f_write(&dst_fp, local_buff, read_size, &write_size);
|
---|
| 422 | if (res != FR_OK) {
|
---|
| 423 | printf("dst write err(%d).\n", res);
|
---|
| 424 | goto cp_end_2;
|
---|
| 425 | }
|
---|
| 426 | if (read_size != write_size) {
|
---|
| 427 | printf("dst write err(disk full).\n");
|
---|
| 428 | goto cp_end_2;
|
---|
| 429 | }
|
---|
| 430 | } while (read_size == sizeof(local_buff));
|
---|
| 431 |
|
---|
| 432 | cp_end_2:
|
---|
| 433 | f_close(&dst_fp);
|
---|
| 434 | cp_end_1:
|
---|
| 435 | f_close(&src_fp);
|
---|
| 436 | cp_end:
|
---|
| 437 | if(dst_mod_str_p != NULL) ff_memfree(dst_mod_str_p);
|
---|
| 438 | if(local_buff != NULL) ff_memfree(local_buff);
|
---|
| 439 | if(lfn != NULL) ff_memfree(lfn);
|
---|
| 440 |
|
---|
| 441 | return 0;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | int usrcmd_rm(int argc, char **argv)
|
---|
| 445 | {
|
---|
| 446 | FRESULT res;
|
---|
| 447 |
|
---|
| 448 | if (argc == 2) {
|
---|
| 449 | if ((res = f_unlink(argv[1])) != FR_OK) {
|
---|
| 450 | put_rc("f_unlink", res);
|
---|
| 451 | return 0;
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | return 0;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | int usrcmd_mv(int argc, char **argv)
|
---|
| 459 | {
|
---|
| 460 | FRESULT res;
|
---|
| 461 |
|
---|
| 462 | if (argc == 3) {
|
---|
| 463 | if ((res = f_rename(argv[1], argv[2])) != FR_OK) {
|
---|
| 464 | put_rc("f_rename", res);
|
---|
| 465 | return 0;
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | return 0;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | int usrcmd_mkdir(int argc, char **argv)
|
---|
| 473 | {
|
---|
| 474 | FRESULT res;
|
---|
| 475 |
|
---|
| 476 | if (argc == 2) {
|
---|
| 477 | if ((res = f_mkdir(argv[1])) != FR_OK) {
|
---|
| 478 | put_rc("f_mkdir", res);
|
---|
| 479 | return 0;
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | return 0;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | #define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */
|
---|
| 487 | #define CCOLOR_RESET 0
|
---|
| 488 | #define CCOLOR_BLACK 30
|
---|
| 489 | #define CCOLOR_RED 31
|
---|
| 490 | #define CCOLOR_GREEN 32
|
---|
| 491 | #define CCOLOR_YELLOW 33
|
---|
| 492 | #define CCOLOR_BLUE 34
|
---|
| 493 | #define CCOLOR_MAGENTA 35
|
---|
| 494 | #define CCOLOR_CYAN 36
|
---|
| 495 | #define CCOLOR_WHITE 37
|
---|
| 496 | enum {
|
---|
| 497 | HEXDUMP_OPT_DEFAULT = 1 << 0,
|
---|
| 498 | HEXDUMP_OPT_UTF8 = 1 << 1,
|
---|
| 499 | };
|
---|
| 500 |
|
---|
| 501 | int usrcmd_hexdump(int argc, char **argv)
|
---|
| 502 | {
|
---|
| 503 | FRESULT res;
|
---|
| 504 | FIL fsrc;
|
---|
| 505 | char data[16 + 1 + HEXDUMP_EXTRA_FOR_UTF8];
|
---|
| 506 | 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];
|
---|
| 507 | 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 | TCHAR *filename_p = NULL;
|
---|
| 509 | int op, option_flag = HEXDUMP_OPT_DEFAULT;
|
---|
| 510 | char *option_ptr, *option_endptr, ccolor, utf8_done_flg, utf8_odd_bytes = 0;
|
---|
| 511 | unsigned int op_offset = 0, op_size = 0, op_end = 0;
|
---|
| 512 |
|
---|
| 513 | while ((op = getopt(argc, argv, "hduos0123456789xX")) != -1) {
|
---|
| 514 | switch (op) {
|
---|
| 515 | case 'h': /* help */
|
---|
| 516 | printf(" hexdump [OPTION] file\n");
|
---|
| 517 | printf(" -h : help\n");
|
---|
| 518 | printf(" -d : print all byte with convert and color [in character area] (default)\n");
|
---|
| 519 | printf(" -u : try print UTF-8 code [in character area]\n");
|
---|
| 520 | printf(" -oOFFSET : print start offset address from top\n");
|
---|
| 521 | printf(" -sSIZE : print size\n");
|
---|
| 522 | break;
|
---|
| 523 | case 'd': /* print one byte character [in character area] (default) */
|
---|
| 524 | option_flag |= HEXDUMP_OPT_DEFAULT;
|
---|
| 525 | break;
|
---|
| 526 | case 'u': /* try print UTF-8 code [in character area] */
|
---|
| 527 | option_flag |= HEXDUMP_OPT_UTF8;
|
---|
| 528 | break;
|
---|
| 529 | case 'o': /* print start offset address from top */
|
---|
| 530 | option_ptr = *(argv + optind);
|
---|
| 531 | op_offset = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
|
---|
| 532 | break;
|
---|
| 533 | case 's': /* print size */
|
---|
| 534 | option_ptr = *(argv + optind);
|
---|
| 535 | op_size = ntlibc_strtoul(&option_ptr[2], &option_endptr, 0);
|
---|
| 536 | break;
|
---|
| 537 | default:
|
---|
| 538 | break;
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 | filename_p = *(argv + optind);
|
---|
| 542 | if (filename_p == NULL)
|
---|
| 543 | return 0;
|
---|
| 544 |
|
---|
| 545 | if ((res = f_open(&fsrc, filename_p, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
|
---|
| 546 | put_rc("f_open", res);
|
---|
| 547 | return 0;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | /* position adjusting */
|
---|
| 551 | if (op_offset >= fsrc.fsize) {
|
---|
| 552 | printf("error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize);
|
---|
| 553 | return 0;
|
---|
| 554 | }
|
---|
| 555 | op_end = op_offset + op_size;
|
---|
| 556 | if ((op_size == 0) || (op_end >= fsrc.fsize))
|
---|
| 557 | op_end = fsrc.fsize;
|
---|
| 558 | f_lseek(&fsrc, op_offset);
|
---|
| 559 |
|
---|
| 560 | for (int i = op_offset; i < op_end; i += 16) {
|
---|
| 561 | ascii[0] = '\0';
|
---|
| 562 | line[0] = '\0';
|
---|
| 563 | char *pos = line;
|
---|
| 564 | int rst = sizeof(line);
|
---|
| 565 | int len = snprintf(pos, rst, "%08X: ", i);
|
---|
| 566 | pos += len;
|
---|
| 567 | rst -= len;
|
---|
| 568 |
|
---|
| 569 | UINT br = 0;
|
---|
| 570 | if ((res = f_read(&fsrc, data, 16 + HEXDUMP_EXTRA_FOR_UTF8, &br)) != FR_OK) {
|
---|
| 571 | put_rc("f_read", res);
|
---|
| 572 | f_close(&fsrc);
|
---|
| 573 | return 0;
|
---|
| 574 | }
|
---|
| 575 | data[br] = '\0';
|
---|
| 576 | f_lseek(&fsrc, i + 16);
|
---|
| 577 | if (br > 16) br = 16;
|
---|
| 578 |
|
---|
| 579 | char *apos = ascii;
|
---|
| 580 | int arst = sizeof(ascii);
|
---|
| 581 | for (int j = 0; j < br; j++) {
|
---|
| 582 | char c = data[j];
|
---|
| 583 | if (j != 7)
|
---|
| 584 | len = snprintf(pos, rst, "%02X ", c);
|
---|
| 585 | else
|
---|
| 586 | len = snprintf(pos, rst, "%02X-", c);
|
---|
| 587 | pos += len;
|
---|
| 588 | rst -= len;
|
---|
| 589 |
|
---|
| 590 | len = 0;
|
---|
| 591 | utf8_done_flg = 0;
|
---|
| 592 | ccolor = CCOLOR_RESET;
|
---|
| 593 |
|
---|
| 594 | if (c < 0x20) {
|
---|
| 595 | ccolor = CCOLOR_RED;
|
---|
| 596 | c = c + 0x40;
|
---|
| 597 | }
|
---|
| 598 | else {
|
---|
| 599 | if (option_flag & HEXDUMP_OPT_UTF8) { /* try UTF-8 */
|
---|
| 600 | /* check character code */
|
---|
| 601 | int bf_utf8_bytes, af_utf8_bytes;
|
---|
| 602 | unsigned char utf8_code[4];
|
---|
| 603 | WCHAR utf16_code;
|
---|
| 604 |
|
---|
| 605 | utf16_code = Utf8_to_Utf16(&data[j], &bf_utf8_bytes); /* try UTF-8 -> UTF-16 */
|
---|
| 606 | Utf16_to_Utf8(utf8_code, &af_utf8_bytes, (UINT)utf16_code); /* try UTF-16 -> UTF-8 */
|
---|
| 607 | if ((af_utf8_bytes <= 3 && bf_utf8_bytes == af_utf8_bytes) &&
|
---|
| 608 | !memcmp(&data[j], utf8_code, af_utf8_bytes)) { /* size & code match */
|
---|
| 609 | utf8_done_flg = 1;
|
---|
| 610 | for (int k = 1; k < af_utf8_bytes; k++) { /* pos */
|
---|
| 611 | if (j + k >= 16)
|
---|
| 612 | break;
|
---|
| 613 | if (j + k != 7)
|
---|
| 614 | len = snprintf(pos, rst, "%02X ", data[j + k]);
|
---|
| 615 | else
|
---|
| 616 | len = snprintf(pos, rst, "%02X-", data[j + k]);
|
---|
| 617 | pos += len;
|
---|
| 618 | rst -= len;
|
---|
| 619 | }
|
---|
| 620 | memcpy(apos, &data[j], af_utf8_bytes); /* apos */
|
---|
| 621 | apos[af_utf8_bytes] = 0;
|
---|
| 622 | len = af_utf8_bytes;
|
---|
| 623 | j += af_utf8_bytes - 1;
|
---|
| 624 | if (af_utf8_bytes > 1) {
|
---|
| 625 | utf8_odd_bytes = j < 15 ? af_utf8_bytes - 2 : af_utf8_bytes - 1;
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | if (utf8_odd_bytes > 0 && j < 15) {
|
---|
| 632 | apos += len;
|
---|
| 633 | arst -= len;
|
---|
| 634 | len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", CCOLOR_RESET, ' ');
|
---|
| 635 | utf8_odd_bytes--;
|
---|
| 636 | }
|
---|
| 637 | else if (utf8_done_flg == 0) {
|
---|
| 638 | if (c >= 0x80 && c < 0x9F) {
|
---|
| 639 | ccolor = CCOLOR_RED;
|
---|
| 640 | c = c - 0x60;
|
---|
| 641 | }
|
---|
| 642 | else if (c >= 0xA0 && c < 0xDF) {
|
---|
| 643 | ccolor = CCOLOR_GREEN;
|
---|
| 644 | c = c - 0x60;
|
---|
| 645 | }
|
---|
| 646 | else if (c >= 0xE0 && c < 0xFF) {
|
---|
| 647 | ccolor = CCOLOR_GREEN;
|
---|
| 648 | c = c - 0xC0;
|
---|
| 649 | }
|
---|
| 650 | else if (c == 0x7F || c == 0x9F || c == 0xDF || c == 0xFF) {
|
---|
| 651 | ccolor = CCOLOR_CYAN;
|
---|
| 652 | c = '?';
|
---|
| 653 | }
|
---|
| 654 | len = snprintf(apos, arst, "\x1B[%dm%c\x1B[0m", ccolor, c);
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | apos += len;
|
---|
| 658 | arst -= len;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | for (int j = br; j < 16; j++) {
|
---|
| 662 | if (j != 7)
|
---|
| 663 | len = snprintf(pos, rst, " ");
|
---|
| 664 | else
|
---|
| 665 | len = snprintf(pos, rst, " -");
|
---|
| 666 | pos += len;
|
---|
| 667 | rst -= len;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | len = snprintf(pos, rst, ": %s\n", ascii);
|
---|
| 671 | pos += len;
|
---|
| 672 | rst -= len;
|
---|
| 673 |
|
---|
| 674 | puts(line);
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | f_close(&fsrc);
|
---|
| 678 | return 0;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | int usrcmd_date(int argc, char **argv)
|
---|
| 682 | {
|
---|
| 683 | int ret;
|
---|
| 684 | struct timespec tp;
|
---|
| 685 | char buf[30];
|
---|
| 686 |
|
---|
| 687 | ret = clock_gettime(CLOCK_REALTIME, &tp);
|
---|
| 688 | if (ret != 0) {
|
---|
| 689 | printf("clock_gettime error %d", ret);
|
---|
| 690 | return 0;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | memset(buf, 0, sizeof(buf));
|
---|
| 694 | if (ctime_r(&tp.tv_sec, buf) == NULL) {
|
---|
| 695 | printf("ctime_r error");
|
---|
| 696 | return 0;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /* æ¹è¡ã³ã¼ãã®åé¤ */
|
---|
| 700 | ret = ntlibc_strlen(buf);
|
---|
| 701 | buf[ret - 1] = '\0';
|
---|
| 702 |
|
---|
| 703 | printf("%s .%09ld\n", buf, tp.tv_nsec);
|
---|
| 704 | return 0;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | int usrcmd_info(int argc, char **argv)
|
---|
| 708 | {
|
---|
| 709 | if (argc != 2) {
|
---|
| 710 | printf("info sys\n");
|
---|
| 711 | printf("info ver\n");
|
---|
| 712 | return 0;
|
---|
| 713 | }
|
---|
| 714 | if (strcmp(argv[1], "sys") == 0) {
|
---|
| 715 | printf(TARGET_NAME" Monitor\n");
|
---|
| 716 | return 0;
|
---|
| 717 | }
|
---|
| 718 | if (strcmp(argv[1], "ver") == 0) {
|
---|
| 719 | int mj, mn, bd;
|
---|
| 720 | ntshell_version(&mj, &mn, &bd);
|
---|
| 721 | printf("Version %d.%d.%d\n", mj, mn, bd);
|
---|
| 722 | return 0;
|
---|
| 723 | }
|
---|
| 724 | printf("Unknown sub command found\n");
|
---|
| 725 | return -1;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | int usrcmd_exit(int argc, char **argv)
|
---|
| 729 | {
|
---|
| 730 | ntshell_exit = 1;
|
---|
| 731 |
|
---|
| 732 | return 0;
|
---|
| 733 | }
|
---|