Add skip= and count= options to the NSH hexdump command. From Ken Pettit

This commit is contained in:
Gregory Nutt 2013-11-01 07:15:14 -06:00
parent 716ca60638
commit 74b0933088
4 changed files with 109 additions and 25 deletions

View File

@ -716,4 +716,6 @@
(2013-10-30). (2013-10-30).
* apps/platform/mikroe-stm32f4: Now supports storage of configuration * apps/platform/mikroe-stm32f4: Now supports storage of configuration
data. From Ken Pettit (2013-10-30). data. From Ken Pettit (2013-10-30).
* apps/nshlib/nsh_dbgcmds.c and others: Add skip= and count=
options to the hexdump command. From Ken Pettit (2013-11-1).

View File

@ -238,6 +238,10 @@ config NSH_CODECS_BUFSIZE
int "File buffer size used by CODEC commands" int "File buffer size used by CODEC commands"
default 128 default 128
config NSH_CMDOPT_HEXDUMP
bool "hexdump: Enable 'skip' and 'count' parameters"
default n
endmenu endmenu
config NSH_FILEIOSIZE config NSH_FILEIOSIZE

View File

@ -369,9 +369,15 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
uint8_t buffer[IOBUFFERSIZE]; uint8_t buffer[IOBUFFERSIZE];
char msg[32]; char msg[32];
int position; off_t position;
int fd; int fd;
int ret = OK; int ret = OK;
#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
off_t skip = 0;
off_t count = 0xfffffff;
off_t dumpbytes;
int x;
#endif
/* Open the file for reading */ /* Open the file for reading */
@ -382,31 +388,99 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
return ERROR; return ERROR;
} }
#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
for (x = 2; x < argc; x++)
{
if (strncmp(argv[x], "skip=", 5) == 0)
{
skip = atoi(&argv[x][5]);
}
else if (strncmp(argv[x], "count=", 6) == 0)
{
count = atoi(&argv[x][6]);
}
}
#endif
position = 0; position = 0;
for (;;) for (;;)
{ {
int nbytesread = read(fd, buffer, IOBUFFERSIZE); int nbytesread = read(fd, buffer, IOBUFFERSIZE);
/* Check for read errors */ /* Check for read errors */
if (nbytesread < 0) if (nbytesread < 0)
{ {
int errval = errno; int errval = errno;
nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "read", NSH_ERRNO_OF(errval)); nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "read",
ret = ERROR; NSH_ERRNO_OF(errval));
break; ret = ERROR;
} break;
else if (nbytesread > 0) }
{ else if (nbytesread > 0)
snprintf(msg, sizeof(msg), "%s at %08x", argv[1], position); {
nsh_dumpbuffer(vtbl, msg, buffer, nbytesread); #ifdef CONFIG_NSH_CMDOPT_HEXDUMP
position += nbytesread; if (position < skip)
} {
else /* Skip bytes until we reach the skip point */
{
break; // EOF position += nbytesread;
} if (position > skip)
} {
dumpbytes = position - skip;
if (dumpbytes > count)
{
dumpbytes = count;
}
snprintf(msg, sizeof(msg), "%s at %08x", argv[1], skip);
nsh_dumpbuffer(vtbl, msg,
&buffer[nbytesread - (position-skip)],
dumpbytes);
if (count > dumpbytes)
{
count -= dumpbytes;
}
else
{
break;
}
}
/* Don't print if we are in skip mode */
continue;
}
/* Limit dumpbuffer to count if less than a full buffer needed */
if (nbytesread > count)
{
nbytesread = count;
}
#endif
snprintf(msg, sizeof(msg), "%s at %08x", argv[1], position);
nsh_dumpbuffer(vtbl, msg, buffer, nbytesread);
position += nbytesread;
#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
if (count > nbytesread)
{
count -= nbytesread;
}
else
{
break;
}
#endif
}
else
{
break; // EOF
}
}
(void)close(fd); (void)close(fd);
return ret; return ret;

View File

@ -245,7 +245,11 @@ static const struct cmdmap_s g_cmdmap[] =
#if CONFIG_NFILE_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_NSH_DISABLE_HEXDUMP #ifndef CONFIG_NSH_DISABLE_HEXDUMP
#ifndef CONFIG_NSH_CMDOPT_HEXDUMP
{ "hexdump", cmd_hexdump, 2, 2, "<file or device>" }, { "hexdump", cmd_hexdump, 2, 2, "<file or device>" },
#else
{ "hexdump", cmd_hexdump, 2, 4, "<file or device> [skip=<bytes>] [count=<bytes>]" },
#endif
#endif #endif
#endif #endif