From b1ef1169b97cf47c1c0e59509c12f93a8ccbbc98 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 22 Nov 2015 12:00:20 -0600 Subject: [PATCH] hexed: Fix some misc. issues --- system/hexed/Kconfig | 13 +++++++++++ system/hexed/Makefile | 7 ++++-- system/hexed/include/hexed.h | 4 +--- system/hexed/src/hexcopy.c | 9 ++++++++ system/hexed/src/hexdump.c | 45 ++++++++++++++++++++++++++++++++++-- system/hexed/src/hexed.c | 34 ++++----------------------- system/hexed/src/hexenter.c | 12 ++++++---- system/hexed/src/hexinsert.c | 3 ++- system/hexed/src/hexmove.c | 9 ++++++++ system/hexed/src/hexremove.c | 9 ++++++++ 10 files changed, 103 insertions(+), 42 deletions(-) diff --git a/system/hexed/Kconfig b/system/hexed/Kconfig index c088cf303..15cbc4ff6 100644 --- a/system/hexed/Kconfig +++ b/system/hexed/Kconfig @@ -10,4 +10,17 @@ menuconfig SYSTEM_HEXED Enable support for the hexed command line hexadecial file editor if SYSTEM_HEXED + +config SYSTEM_HEXED_STACKSIZE + int "hexed stack size" + default 2048 + ---help--- + The size of stack allocated for the hexed task. + +config SYSTEM_HEXED_PRIORITY + int "hexed priority" + default 100 + ---help--- + The priority of the hexed task. + endif # SYSTEM_HEXED diff --git a/system/hexed/Makefile b/system/hexed/Makefile index bb11f1329..b4cec99c8 100644 --- a/system/hexed/Makefile +++ b/system/hexed/Makefile @@ -43,9 +43,12 @@ endif # hexed Application +CONFIG_SYSTEM_HEXED_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_SYSTEM_HEXED_STACKSIZE ?= 2048 + APPNAME = hexed -PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 2048 +PRIORITY = $(CONFIG_SYSTEM_HEXED_PRIORITY) +STACKSIZE = $(CONFIG_SYSTEM_HEXED_STACKSIZE) ASRCS = CSRCS = bfile.c cmdargs.c hexcopy.c hexdump.c hexenter.c hexhelp.c diff --git a/system/hexed/include/hexed.h b/system/hexed/include/hexed.h index 83801e6b3..64b2cdb4f 100644 --- a/system/hexed/include/hexed.h +++ b/system/hexed/include/hexed.h @@ -134,7 +134,7 @@ extern FAR struct bfile_s *g_hexfile; * Public Function Prototypes ****************************************************************************/ -void hexed_error(int eno, FAR const char *fmt, ...); +void hexed_fatal(FAR const char *fmt, ...); int hexcopy(FAR struct command_s *cmd, int optc, FAR char *opt); int hexdump(FAR struct command_s *cmd, int optc, FAR char *opt); int hexenter(FAR struct command_s *cmd, int optc, FAR char *opt); @@ -143,7 +143,5 @@ int hexinsert(FAR struct command_s *cmd, int optc, FAR char *opt); int hexmove(FAR struct command_s *cmd, int optc, FAR char *opt); int hexremove(FAR struct command_s *cmd, int optc, FAR char *opt); int hexword(FAR struct command_s *cmd, int optc, FAR char *opt); -int show_usage(void); -void printhex(uint64_t i, int size); #endif /* __APPS_SYSTEM_HEXED_INCLUDE_HEXED_H */ diff --git a/system/hexed/src/hexcopy.c b/system/hexed/src/hexcopy.c index 71ad9cae6..af41b6add 100644 --- a/system/hexed/src/hexcopy.c +++ b/system/hexed/src/hexcopy.c @@ -190,6 +190,15 @@ int hexcopy(FAR struct command_s *cmd, int optc, char *opt) } else { + /* We need to have a file name for this command */ + + if (g_hexfile == NULL) + { + fprintf(stderr, "ERROR: Copy command requires a filename\n"); + g_last_error = EINVAL; + return -EINVAL; + } + optc = runcopy(cmd); } diff --git a/system/hexed/src/hexdump.c b/system/hexed/src/hexdump.c index 96393ba09..6e7384ed5 100644 --- a/system/hexed/src/hexdump.c +++ b/system/hexed/src/hexdump.c @@ -97,7 +97,38 @@ static int rundump(FAR struct command_s *cmd) if (off + x < last) { - printhex(*(uint64_t *) (cur + x), cmd->opts.word); + /* Print 1-8 byte hexadecimal number */ + + switch (cmd->opts.word) + { + case WORD_64: + { + uint64_t data = *(uint64_t *)(cur + x); + printf("%016llx", (unsigned long long)data); + } + break; + + case WORD_32: + { + uint32_t data = *(uint32_t *)(cur + x); + printf("%08lx", (unsigned long)data); + } + break; + + case WORD_16: + { + uint16_t data = *(uint16_t *)(cur + x); + printf("%04x", (unsigned int)data); + } + break; + + case WORD_8: + { + uint8_t data = *(uint8_t *)(cur + x); + printf("%02x", (unsigned int)data); + } + break; + } } else { @@ -211,7 +242,8 @@ int hexdump(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_DUMP) { - return -1; + g_last_error = EINVAL; + return -EINVAL; } /* Set/run dump */ @@ -222,6 +254,15 @@ int hexdump(FAR struct command_s *cmd, int optc, char *opt) } else { + /* We need to have a file name for this command */ + + if (g_hexfile == NULL) + { + fprintf(stderr, "ERROR: Dump command requires a filename\n"); + g_last_error = EINVAL; + return -EINVAL; + } + optc = rundump(cmd); } diff --git a/system/hexed/src/hexed.c b/system/hexed/src/hexed.c index 211497c80..cd58759ce 100644 --- a/system/hexed/src/hexed.c +++ b/system/hexed/src/hexed.c @@ -83,40 +83,16 @@ static struct arglist_s g_arglist[] = * Public Functions ****************************************************************************/ -/* Print hexed_error msg and exit */ +/* Print error msg and exit */ -void hexed_error(int eno, FAR const char *fmt, ...) +void hexed_fatal(FAR const char *fmt, ...) { va_list va; va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); - exit(-eno); -} - -/* Print 1-8 byte hexadecimal number */ - -void printhex(uint64_t i, int size) -{ - switch (size) - { - case WORD_64: - printf("%016llx", (unsigned long long)i); - break; - - case WORD_32: - printf("%08lx", (unsigned long)i); - break; - - case WORD_16: - printf("%04x", (unsigned int)i); - break; - - case WORD_8: - printf("%02x", (unsigned int)i); - break; - } + exit(1); } /* Load a Buffered File hexfile */ @@ -296,7 +272,7 @@ int parseargs(FAR char *argv[]) if (strlen(opt) > 1 && *(opt + 1) != '-') { - hexed_error(EINVAL, "Unknown option: %s\n", opt); + hexed_fatal("ERROR: Unknown option: %s\n", opt); } } @@ -312,7 +288,7 @@ int parseargs(FAR char *argv[]) else { - hexed_error(EINVAL, "Unexpected option: %s\n", opt); + hexed_fatal("ERROR: Unexpected option: %s\n", opt); } } diff --git a/system/hexed/src/hexenter.c b/system/hexed/src/hexenter.c index 43ce88b3a..17f53aa37 100644 --- a/system/hexed/src/hexenter.c +++ b/system/hexed/src/hexenter.c @@ -63,7 +63,8 @@ static int runenter(FAR struct command_s *cmd) static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) { FAR char *s; - int64_t *hx, v; + FAR int64_t *hx; + int64_t v; /* Set defaults */ @@ -109,15 +110,15 @@ static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) break; case WORD_32: - *((int32_t *) hx + cmd->opts.cnt) = v; + *((int32_t *)hx + cmd->opts.cnt) = v; break; case WORD_16: - *((int16_t *) hx + cmd->opts.cnt) = v; + *((int16_t *)hx + cmd->opts.cnt) = v; break; case WORD_8: - *((int8_t *) hx + cmd->opts.cnt) = v; + *((int8_t *)hx + cmd->opts.cnt) = v; break; default: @@ -133,8 +134,9 @@ static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) else { - hexed_error(E2BIG, "Enter error: too many values set\n"); + fprintf(stderr, "ERROR: too many values set\n"); g_last_error = E2BIG; + return -E2BIG; } return optc; diff --git a/system/hexed/src/hexinsert.c b/system/hexed/src/hexinsert.c index 5e407244a..9d63b39a9 100644 --- a/system/hexed/src/hexinsert.c +++ b/system/hexed/src/hexinsert.c @@ -157,8 +157,9 @@ static int setinsert(FAR struct command_s *cmd, int optc, char *opt) } else { - hexed_error(E2BIG, "Insert error: too many values set\n"); + fprintf(stderr, "ERROR: Too many values set\n"); g_last_error = E2BIG; + return -E2BIG; } } diff --git a/system/hexed/src/hexmove.c b/system/hexed/src/hexmove.c index 6a450c5ac..241ca47b6 100644 --- a/system/hexed/src/hexmove.c +++ b/system/hexed/src/hexmove.c @@ -222,6 +222,15 @@ int hexmove(FAR struct command_s *cmd, int optc, char *opt) } else { + /* We need to have a file name for this command */ + + if (g_hexfile == NULL) + { + fprintf(stderr, "ERROR: Move command requires a filename\n"); + g_last_error = EINVAL; + return -EINVAL; + } + optc = runmove(cmd); } diff --git a/system/hexed/src/hexremove.c b/system/hexed/src/hexremove.c index d66b9b0b4..648d10bc8 100644 --- a/system/hexed/src/hexremove.c +++ b/system/hexed/src/hexremove.c @@ -142,6 +142,15 @@ int hexremove(FAR struct command_s *cmd, int optc, char *opt) } else { + /* We need to have a file name for this command */ + + if (g_hexfile == NULL) + { + fprintf(stderr, "ERROR: Remove command requires a filename\n"); + g_last_error = EINVAL; + return -EINVAL; + } + optc = runremove(cmd); }