Add the hexed command line hex editor. See http://apps.venomdev.net/hexed/

This commit is contained in:
Gregory Nutt 2015-11-21 18:32:04 -06:00
parent 146f126ead
commit 2ff6775ac9
22 changed files with 3100 additions and 3 deletions

View File

@ -1448,4 +1448,6 @@
* apps/nshlib/nsh_ddcmd.c: Now that NuttX automatically converts
block devices to character devices, all of the BCH stuff in NSH
can be removed (2015-11-21).
* apps/system/hexed: Port the hexed command line hexadeciamal
editor to Nuttx. See http://apps.venomdev.net/hexed/.
Initial port is not functional (2015-11-21).

View File

@ -13,6 +13,7 @@ source "$APPSDIR/system/flash_eraseall/Kconfig"
source "$APPSDIR/system/hex2bin/Kconfig"
source "$APPSDIR/system/i2c/Kconfig"
source "$APPSDIR/system/inifile/Kconfig"
source "$APPSDIR/system/hexed/Kconfig"
source "$APPSDIR/system/netdb/Kconfig"
source "$APPSDIR/system/nxplayer/Kconfig"
source "$APPSDIR/system/ramtest/Kconfig"

View File

@ -54,6 +54,10 @@ ifeq ($(CONFIG_SYSTEM_HEX2BIN),y)
CONFIGURED_APPS += system/hex2bin
endif
ifeq ($(CONFIG_SYSTEM_HEXED),y)
CONFIGURED_APPS += system/hexed
endif
ifeq ($(CONFIG_SYSTEM_I2CTOOL),y)
CONFIGURED_APPS += system/i2c
endif

View File

@ -38,8 +38,9 @@
# Sub-directories containing system tasks/libraries
SUBDIRS = cdcacm cle composite cu flash_eraseall free i2c hex2bin inifile
SUBDIRS += install lm75 mdio netdb nxplayer ramtest ramtron readline sdcard
SUBDIRS += stackmonitor sudoku symtab usbmonitor usbmsc vi zmodem zoneinfo
SUBDIRS += install hexed lm75 mdio netdb nxplayer ramtest ramtron readline
SUBDIRS += sdcard stackmonitor sudoku symtab usbmonitor usbmsc vi zmodem
SUBDIRS += zoneinfo
# Create the list of installed runtime modules (INSTALLED_DIRS)

11
system/hexed/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src
/*.obj

13
system/hexed/Kconfig Normal file
View File

@ -0,0 +1,13 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menuconfig SYSTEM_HEXED
bool "Hex editor"
default n
---help---
Enable support for the hexed command line hexadecial file editor
if SYSTEM_HEXED
endif # SYSTEM_HEXED

145
system/hexed/Makefile Normal file
View File

@ -0,0 +1,145 @@
############################################################################
# apps/system/hexed/Makefile
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
ifeq ($(WINTOOL),y)
INCDIROPT = -w
endif
# hexed Application
APPNAME = hexed
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
ASRCS =
CSRCS = bfile.c cmdargs.c hexcopy.c hexdump.c hexenter.c hexhelp.c
CSRCS += hexinsert.c hexmove.c hexremove.c hexword.c
MAINSRC = hexed.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
OBJS = $(AOBJS) $(COBJS)
ifneq ($(CONFIG_BUILD_KERNEL),y)
OBJS += $(MAINOBJ)
endif
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
BIN = ..\..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
BIN = ..\\..\\libapps$(LIBEXT)
else
BIN = ../../libapps$(LIBEXT)
endif
endif
ifeq ($(WINTOOL),y)
INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
else
INSTALL_DIR = $(BIN_DIR)
endif
CONFIG_XYZ_PROGNAME ?= install$(EXEEXT)
PROGNAME = $(CONFIG_XYZ_PROGNAME)
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" include}
ROOTDEPPATH = --dep-path src
# Common build
VPATH = src
all: .built
.PHONY: context depend clean distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
$(call ARCHIVE, $(BIN), $(OBJS))
$(Q) touch .built
ifeq ($(CONFIG_BUILD_KERNEL),y)
$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ)
@echo "LD: $(PROGNAME)"
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS)
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME)
install: $(BIN_DIR)$(DELIM)$(PROGNAME)
else
install:
endif
# Register application
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
else
context:
endif
# Create dependencies
.depend: Makefile $(SRCS)
$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
$(Q) touch $@
depend: .depend
clean:
$(call DELFILE, .built)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
-include Make.dep

View File

@ -0,0 +1,28 @@
Copyright (c) 2010, B.ZaaR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
The names of contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,90 @@
/****************************************************************************
* apps/system/hexed/inlcude/bfile.h
* Buffered file control header
*
* Copyright (c) 2010, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_SYSTEM_HEXED_INCLUDE_BFILE_H
#define __APPS_SYSTEM_HEXED_INCLUDE_BFILE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Buffer size/alignment */
#define BFILE_BUF_MIN 0x1000
#define BFILE_BUF_ALIGN(sz) \
((sz + BFILE_BUF_MIN - 1) / BFILE_BUF_MIN * BFILE_BUF_MIN)
/* Buffered File flags */
#define BFILE_FL_DIRTY 0x00000001
/****************************************************************************
* Public Types
****************************************************************************/
/* Buffered File */
struct bfile_s
{
FAR FILE *fp;
FAR char *name;
long size;
int flags;
long bufsz;
FAR char *buf;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
long fsize(FAR FILE *fp);
int bfflush(FAR struct bfile_s *bf);
struct bfile_s *bfopen(FAR char *name, FAR char *mode);
int bfclose(FAR struct bfile_s *bf);
long bfclip(FAR struct bfile_s *bf, long off, long sz);
long bfcopy(FAR struct bfile_s *bf, long dest, long src, long sz);
long bfinsert(FAR struct bfile_s *bf, long off, FAR void *mem, long sz);
long bfmove(FAR struct bfile_s *bf, long dest, long src, long sz);
long bfread(FAR struct bfile_s *bf);
long bftruncate(FAR struct bfile_s *bf, long sz);
long bfwrite(FAR struct bfile_s *bf, long off, FAR void *mem, long sz);
#endif /* __APPS_SYSTEM_HEXED_INCLUDE_BFILE_H */

View File

@ -0,0 +1,100 @@
/****************************************************************************
* apps/system/hexed/include/cmdargs.h
* Command line argument parsing header
*
* Copyright (c) 2010, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_SYSTEM_HEXED_INCLUDE_CMDARGS_H
#define __APPS_SYSTEM_HEXED_INCLUDE_CMDARGS_H
/****************************************************************************
* Included Files
****************************************************************************/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Command arguments flags */
/* Flags set by the parser */
#define CMDARGS_FL_SETARG 0x00000001 /* Argument set with '-' */
#define CMDARGS_FL_LONGARG 0x00000002 /* long argument set with '--' */
#define CMDARGS_FL_SINGLE 0x00000004 /* Single stepping args */
/* flags to keep set between single stepping args */
#define CMDARGS_FL_RESET (CMDARGS_FL_SINGLE | CMDARGS_FL_SETARG)
/* Flags set by the caller */
#define CMDARGS_FL_OPT 0x00010000 /* options accepted */
#define CMDARGS_FL_OPTREQ (0x00020000 | CMDARGS_FL_OPT) /* opts */
/* required */
#define CMDARGS_FL_CALLMASK(fl) (fl & 0xffff0000)
/****************************************************************************
* Public Types
****************************************************************************/
/* Argument list table */
struct arglist_s
{
FAR char *name;
int flags;
};
/* Command argument details */
struct cmdargs_s
{
int idx; /* last argument parsed */
int flags; /* argument flags */
int argid; /* argument id in struct arglist_s */
FAR char *arg; /* argument string */
FAR char *opt; /* options string */
};
/****************************************************************************
* Public Data
****************************************************************************/
extern FAR struct cmdargs_s *cmdargs;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist);
#endif /* __APPS_SYSTEM_HEXED_INCLUDE_CMDARGS_H */

View File

@ -0,0 +1,155 @@
/****************************************************************************
* apps/system/hexed/include/hexed.h
* Command line HEXadecimal file EDitor header
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_SYSTEM_HEXED_INCLUDE_HEXED_H
#define __APPS_SYSTEM_HEXED_INCLUDE_HEXED_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include "bfile.h"
#include "cmdargs.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Version number */
#define VER_MAJOR 0
#define VER_MINOR 2
#define VER_REVISION 2
/* hexed constants */
#define PNAME "hexed" /* Program name */
#define TEMP_FILE "~hexed.tmp" /* Temp file name */
#define CMD_MAX_CNT 0x10 /* Command table max count */
#define OPT_BUF_SZ 0x40 /* Option buffer size */
/* Word sizes */
#define WORD_64 0x08
#define WORD_32 0x04
#define WORD_16 0x02
#define WORD_8 0x01
/* Command flags */
#define CMD_FL_CMDLINE 0x00000001 /* Command set on command line */
#define CMD_FL_QUIT 0x00000002 /* Quit after command line */
#define CMD_FL_OVERFLOW 0x00000003 /* Command overflow */
#define RETURN_ERR(err) \
{ \
set_errno(err); \
return -err; \
}
/****************************************************************************
* Public Types
****************************************************************************/
/* Command list */
enum
{
/* Command line options */
CMD_HELP = 1,
CMD_COPY_OVER,
CMD_COPY,
CMD_DUMP,
CMD_ENTER,
CMD_FIND,
CMD_INSERT,
CMD_MOVE_OVER,
CMD_MOVE,
CMD_REMOVE,
CMD_WORD,
/* Internal options */
CMD_QUIT
};
/* Command options */
struct cmdoptions_s
{
int word;
long dest;
long src;
long len;
long cnt;
long bytes;
int64_t buf[OPT_BUF_SZ];
};
/* Command details */
struct command_s
{
FAR char *cmd;
int id;
int flags;
struct cmdoptions_s opts;
};
/****************************************************************************
* Public Data
****************************************************************************/
extern int g_wordsize;
extern FAR struct bfile_s *g_hexfile;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
void hexed_error(int eno, 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);
int hexhelp(FAR struct command_s *cmd, int optc, FAR char *opt);
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);
void printhex(uint64_t i, int size);
#endif /* __APPS_SYSTEM_HEXED_INCLUDE_HEXED_H */

524
system/hexed/src/bfile.c Normal file
View File

@ -0,0 +1,524 @@
/****************************************************************************
* apps/system/hexed/src/bfile.c
* Buffered file control
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bfile.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Allocate file buffer */
static void *bfallocbuf(FAR struct bfile_s *bf, long sz)
{
if (bf == NULL)
{
errno = EBADF;
return NULL;
}
/* Allocate buffer */
sz = BFILE_BUF_ALIGN(sz);
if ((bf->buf = realloc(bf->buf, sz)) == NULL)
{
return NULL;
}
/* Clear new memory */
if (sz > bf->bufsz)
{
memset(bf->buf + bf->bufsz, 0, sz - bf->bufsz);
}
bf->bufsz = sz;
return bf->buf;
}
/* Free a buffered file */
static int bffree(FAR struct bfile_s *bf)
{
if (bf == NULL)
{
errno = EBADF;
return -EBADF;
}
/* Free buffer */
if (bf->buf != NULL)
{
free(bf->buf);
}
/* Free file name */
if (bf->name != NULL)
{
free(bf->name);
}
free(bf);
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Get file size */
long fsize(FILE * fp)
{
long off, sz;
if (fp == NULL)
{
errno = EBADF;
return 0;
}
off = ftell(fp);
fseek(fp, 0, SEEK_END);
sz = ftell(fp);
fseek(fp, off, SEEK_SET);
return sz;
}
long bftruncate(FAR struct bfile_s *bf, long sz)
{
if (bf == NULL)
{
errno = EBADF;
return -EBADF;
}
/* Reopen file with truncate */
bf->fp = freopen(bf->name, "w+", bf->fp);
return bfflush(bf);
}
/* Flush buffer data to the file */
int bfflush(FAR struct bfile_s *bf)
{
if (bf == NULL)
{
errno = EBADF;
return -EBADF;
}
/* Check for file changes */
if (!(bf->flags & BFILE_FL_DIRTY))
{
return 0;
}
/* Write file */
set_errno(0);
fseek(bf->fp, 0, SEEK_SET);
if (fwrite(bf->buf, 1, bf->size, bf->fp) == bf->size)
{
bf->flags &= ~BFILE_FL_DIRTY;
}
fflush(bf->fp);
return errno;
}
/* Opens a Buffered File */
FAR struct bfile_s *bfopen(char *name, char *mode)
{
FAR struct bfile_s *bf;
/* NULL file name */
if (name == NULL)
{
errno = EINVAL;
return NULL;
}
/* Allocate a buffered file structure */
if ((bf = malloc(sizeof(struct bfile_s))) == NULL)
{
return NULL;
}
memset(bf, 0, sizeof(struct bfile_s));
/* Set file name */
if ((bf->name = malloc(strlen(name) + 1)) == NULL)
{
bffree(bf);
return NULL;
}
strcpy(bf->name, name);
/* Open file */
if ((bf->fp = fopen(bf->name, mode)) == NULL)
{
bffree(bf);
return NULL;
}
/* Set file buffer */
bf->buf = NULL;
bf->size = fsize(bf->fp);
if (bfallocbuf(bf, bf->size) == NULL)
{
return NULL;
}
return bf;
}
/* Closes a Buffered File */
int bfclose(FAR struct bfile_s *bf)
{
int r;
if (bf == NULL)
{
errno = EBADF;
return -EBADF;
}
bfflush(bf);
/* Close file */
r = fclose(bf->fp);
bffree(bf);
return r;
}
/* Remove bytes from the Buffered File
*
* Moves the data from the end of the buffer, then shrinks the file
* size.
*/
long bfclip(FAR struct bfile_s *bf, long off, long sz)
{
long cnt;
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Negative error */
if (off < 0 || sz <= 0)
{
return EOF;
}
/* Offset past EOF */
if (off > bf->size)
{
return EOF;
}
/* Size past EOF */
if ((off + sz) > bf->size)
{
sz = bf->size - off;
}
/* Remove from file */
cnt = bf->size - (off + sz);
memmove(bf->buf + off, bf->buf + off + sz, cnt);
bf->size -= sz;
bf->flags |= BFILE_FL_DIRTY;
memset(bf->buf + bf->size, 0, sz);
return cnt;
}
/* Insert bytes into the Buffered File
*
* Increases the file size, moves the current data and inserts the
* new data.
*/
long bfinsert(FAR struct bfile_s *bf, long off, void *mem, long sz)
{
long cnt;
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Negative error */
if (off < 0 || sz <= 0)
{
return EOF;
}
/* Increase buffer size */
if (bf->bufsz < (bf->size + off + sz))
{
bfallocbuf(bf, bf->size + off + sz);
}
/* Move data */
if (off < bf->size)
{
cnt = bf->size - off;
memmove(bf->buf + off + sz, bf->buf + off, cnt);
}
memmove(bf->buf + off, mem, sz);
/* Increase file size */
if (off > bf->size)
{
bf->size += off - bf->size;
}
bf->size += sz;
bf->flags |= BFILE_FL_DIRTY;
return sz;
}
/* Copies bytes from src to off */
long bfcopy(FAR struct bfile_s *bf, long off, long src, long sz)
{
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Error: Source past EOF */
if (src > bf->size)
{
return EOF;
}
/* Adjust sz to EOF */
if ((src > off) && (src + sz > bf->size))
{
sz = bf->size - src;
}
/* Adjust source/length for insert */
if (src >= off)
{
if (src + sz > bf->size)
{
sz = bf->size - src;
}
src += sz;
}
return bfinsert(bf, off, bf->buf + src, sz);
}
/* Moves bytes from src to off
*
* Moves the data from src to off then removes the data from the original src
* Moves data in chunks to save memory allocation
*/
long bfmove(FAR struct bfile_s *bf, long off, long src, long sz)
{
long adj;
long cnt;
long len;
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Error: source past EOF */
if (src > bf->size)
{
return EOF;
}
/* Edjust sz to EOF */
if ((src > off) && (src + sz > bf->size))
{
sz = bf->size - src;
}
/* Adjust source/offset */
len = BFILE_BUF_MIN > sz ? sz : BFILE_BUF_MIN;
if (src > off)
{
src += len;
adj = len;
}
else
{
off += sz;
adj = 0;
}
/* Move data in chunks */
for (cnt = sz; cnt; cnt -= len)
{
/* End of count? */
if (cnt < len)
{
len = cnt;
}
/* Move data */
bfinsert(bf, off, bf->buf + src, len);
bfclip(bf, src, len);
off += adj;
}
return sz;
}
/* Read Buffered File
*
* Reads an entire file to the buffer.
*/
long bfread(FAR struct bfile_s *bf)
{
long r;
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Check buffer size */
bf->size = fsize(bf->fp);
if (bf->size > bf->bufsz)
{
if (bfallocbuf(bf, bf->size) == NULL)
{
return EOF;
}
}
/* Read whole file into the buffer */
if ((r = fread(bf->buf, 1, bf->size, bf->fp)) == bf->size)
{
bf->flags &= ~BFILE_FL_DIRTY;
}
return r;
}
/* Write Buffered File
*
* Writes data to the buffer, the buffer still needs to be flushed
* to the file before closing or reading.
*/
long bfwrite(FAR struct bfile_s *bf, long off, void *mem, long sz)
{
long r;
if (bf == NULL)
{
errno = EBADF;
return EOF;
}
/* Increase buffer size */
if (bf->bufsz < off + sz)
{
bfallocbuf(bf, off + sz);
}
memmove(bf->buf + off, mem, sz);
/* Increase file size */
if (bf->size < off + sz)
{
bf->size = off + sz;
}
bf->flags |= BFILE_FL_DIRTY;
return sz;
}

210
system/hexed/src/cmdargs.c Normal file
View File

@ -0,0 +1,210 @@
/****************************************************************************
* apps/system/hexed/src/cmdargs.c
* Command line argument parsing
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "cmdargs.h"
/****************************************************************************
* Public Data
****************************************************************************/
FAR struct cmdargs_s *cmdargs = NULL;
/****************************************************************************
* Private Data
****************************************************************************/
static struct cmdargs_s _cmdargs;
/****************************************************************************
* Public Functions
****************************************************************************/
/* Parse the command line arguments */
int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist)
{
int i;
char *arg;
/* Set cmdargs */
if (cmdargs != &_cmdargs)
{
cmdargs = &_cmdargs;
memset(cmdargs, 0, sizeof(struct cmdargs_s));
}
cmdargs->argid = 0;
/* Get next arg */
if ((cmdargs->flags & CMDARGS_FL_SINGLE) == 0)
{
cmdargs->idx++;
arg = argv[cmdargs->idx];
cmdargs->flags = 0;
/* Single step through this arg */
}
else
{
arg = cmdargs->arg + 1;
cmdargs->flags &= CMDARGS_FL_RESET;
}
/* Error or end of command line */
if (argv == NULL || arglist == NULL || argv[cmdargs->idx] == NULL)
{
memset(cmdargs, 0, sizeof(struct cmdargs_s));
return -1;
}
/* Check for argument */
cmdargs->arg = arg;
if (*arg == '-')
{
cmdargs->flags |= CMDARGS_FL_SETARG;
arg++;
if (*arg == '-')
{
arg++;
cmdargs->flags |= CMDARGS_FL_LONGARG;
}
}
/* Scan arglist */
if ((cmdargs->flags & CMDARGS_FL_SETARG) != 0)
{
for (i = 1; arglist->name != NULL; i++, arglist++)
{
/* Skip long args in single step mode */
if ((cmdargs->flags & CMDARGS_FL_SINGLE) != 0
&& strlen(arglist->name) > 1)
{
continue;
}
/* Check arg for match in arglist */
if (strncmp(arg, arglist->name, strlen(arglist->name)) == 0)
{
/* The long arg flag is set but we've found a single arg match */
if (strlen(arglist->name) == 1 &&
(cmdargs->flags & CMDARGS_FL_LONGARG) != 0)
{
break;
}
cmdargs->argid = i;
break;
}
}
}
/* Found argument */
if (cmdargs->argid > 0)
{
cmdargs->arg = arg;
cmdargs->opt = NULL;
cmdargs->flags |= CMDARGS_FL_CALLMASK(arglist->flags);
arg += strlen(arglist->name);
/* Argument accepts options */
if ((cmdargs->flags & CMDARGS_FL_OPT) != 0)
{
/* Option in same arg */
if (*arg != '\0')
{
/* Option set after ":=" */
if (strchr(":=", *arg))
{
arg++;
cmdargs->opt = arg;
/* Error setting arg/option */
}
else
{
cmdargs->opt = NULL;
}
/* Option in next arg */
}
else if (argv[cmdargs->idx + 1] != NULL &&
*argv[cmdargs->idx + 1] != '\0' &&
*argv[cmdargs->idx + 1] != '-')
{
cmdargs->idx++;
cmdargs->opt = argv[cmdargs->idx];
}
}
/* Single step through this arg */
if (strlen(arglist->name) == 1 && *arg != '\0' &&
(cmdargs->flags & CMDARGS_FL_OPTREQ) != CMDARGS_FL_OPTREQ &&
cmdargs->opt == NULL)
{
cmdargs->flags |= CMDARGS_FL_SINGLE;
}
else
{
cmdargs->flags &= ~CMDARGS_FL_SINGLE;
}
/* No valid argument found */
}
else
{
cmdargs->flags = 0;
cmdargs->opt = NULL;
}
return cmdargs->argid;
}

192
system/hexed/src/hexcopy.c Normal file
View File

@ -0,0 +1,192 @@
/****************************************************************************
* apps/system/hexed/src/hexcopy.c - hexed copy command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Copy len words from src to dest */
static int copy(FAR struct command_s *cmd)
{
bfcopy(g_hexfile, cmd->opts.dest, cmd->opts.src, cmd->opts.bytes);
return 0;
}
/* Copy overwriting len words from src to dest */
static int copyover(FAR struct command_s *cmd)
{
/* Adjust length for EOF */
if ((cmd->opts.src >= cmd->opts.dest) &&
(cmd->opts.src + cmd->opts.bytes > g_hexfile->size))
{
cmd->opts.bytes = g_hexfile->size - cmd->opts.src;
}
/* Copy overwrite */
bfwrite(g_hexfile, cmd->opts.dest, g_hexfile->buf + cmd->opts.src,
cmd->opts.bytes);
return 0;
}
/* Run the copy command */
static int runcopy(FAR struct command_s *cmd)
{
/* Error: Source past EOF */
if (cmd->opts.src > g_hexfile->size)
{
RETURN_ERR(EINVAL);
}
switch (cmd->id)
{
case CMD_COPY:
return copy(cmd);
case CMD_COPY_OVER:
return copyover(cmd);
default:
return -1;
}
}
/* Set the copy command */
static int setcopy(FAR struct command_s *cmd, int optc, char *opt)
{
FAR char *s;
uint64_t v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
}
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EFAULT);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set options */
switch (optc)
{
case 0:
/* Set source */
cmd->opts.src = v;
optc++;
break;
case 1:
/* Set destination */
cmd->opts.dest = v;
optc++;
break;
case 2:
/* Set length */
cmd->opts.len = v;
cmd->opts.bytes = cmd->opts.len * cmd->opts.word;
optc = 0;
break;
default:
/* Too many options specified */
RETURN_ERR(E2BIG);
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Copy command */
int hexcopy(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || (cmd->id != CMD_COPY && cmd->id != CMD_COPY_OVER))
{
RETURN_ERR(EINVAL);
}
/* Set/run copy */
if (optc >= 0)
{
optc = setcopy(cmd, optc, opt);
}
else
{
optc = runcopy(cmd);
}
return optc;
}

229
system/hexed/src/hexdump.c Normal file
View File

@ -0,0 +1,229 @@
/****************************************************************************
* apps/system/hexed/src/hexdump.c
* hexed dump command
*
* Copyright (c) 2010, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Dump the hex values from dest for len words */
static int rundump(FAR struct command_s *cmd)
{
unsigned char *cur;
int x, i;
long off, last;
/* Error - start is past EOF */
if (cmd->opts.src > g_hexfile->size)
{
return EOF;
}
/* Show to EOF */
if (cmd->opts.bytes == 0)
{
cmd->opts.bytes = g_hexfile->size;
}
/* Trim length to EOF */
if ((cmd->opts.src + cmd->opts.bytes) > g_hexfile->size)
{
cmd->opts.bytes = g_hexfile->size - cmd->opts.src;
}
/* Show file */
cur = (unsigned char *)(g_hexfile->buf + cmd->opts.src);
off = cmd->opts.src;
last = cmd->opts.src + cmd->opts.bytes;
for (; off < last; off += 0x10, cur += 0x10)
{
printf("%08lx ", off);
/* Print hex values */
for (x = 0; x < 0x10; x += cmd->opts.word)
{
if (x % 8 == 0)
{
printf(" ");
}
if (off + x < last)
{
printhex(*(uint64_t *) (cur + x), cmd->opts.word);
}
else
{
/* Print space */
for (i = 0; i < cmd->opts.word; i++)
{
printf(" ");
}
}
printf(" ");
}
printf(" ");
/* Print character data */
for (x = 0; x < 0x10; x++)
{
if (off + x < last)
{
if (*(cur + x) >= 0x20 && *(cur + x) < 0x80)
{
printf("%c", *(cur + x));
}
else
{
printf(".");
}
}
else
{
printf(" ");
}
}
printf("\n");
}
printf("\n");
return 0;
}
/* Set the dump command */
static int setdump(FAR struct command_s *cmd, int optc, char *opt)
{
FAR char *s;
long v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
cmd->opts.src = 0;
cmd->opts.len = 0;
cmd->opts.bytes = 0;
}
/* NULL option */
if (opt == NULL)
{
return 0;
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
return -1;
}
/* Set options */
switch (optc)
{
case 0:
/* Set offset */
cmd->opts.src = v;
optc++;
break;
case 1:
/* Set length */
cmd->opts.len = v;
cmd->opts.bytes = cmd->opts.len * cmd->opts.word;
default:
optc = 0;
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Dump command */
int hexdump(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_DUMP)
{
return -1;
}
/* Set/run dump */
if (optc >= 0)
{
optc = setdump(cmd, optc, opt);
}
else
{
optc = rundump(cmd);
}
return optc;
}

399
system/hexed/src/hexed.c Normal file
View File

@ -0,0 +1,399 @@
/****************************************************************************
* apps/system/hexed/src/hexed.c
* Command line HEXadecimal file EDitor
*
* Copyright (c) 2010, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bfile.h"
#include "cmdargs.h"
#include "hexed.h"
/****************************************************************************
* Public Data
****************************************************************************/
int g_wordsize;
FAR struct bfile_s *g_hexfile = NULL;
/****************************************************************************
* Private Data
****************************************************************************/
static FAR struct command_s g_cmdtbl[CMD_MAX_CNT];
static FAR struct command_s *g_cmd;
/* Valid args */
static struct arglist_s g_arglist[] =
{
{"?", 0},
{"co", CMDARGS_FL_OPTREQ},
{"c", CMDARGS_FL_OPTREQ},
{"d", CMDARGS_FL_OPTREQ},
{"e", CMDARGS_FL_OPTREQ},
{"f", CMDARGS_FL_OPTREQ},
{"i", CMDARGS_FL_OPTREQ},
{"mo", CMDARGS_FL_OPTREQ},
{"m", CMDARGS_FL_OPTREQ},
{"r", CMDARGS_FL_OPTREQ},
{"w", CMDARGS_FL_OPTREQ},
{NULL, 0}
};
/****************************************************************************
* Public Functions
****************************************************************************/
/* Print hexed_error msg and exit */
void hexed_error(int eno, 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;
}
}
/* Load a Buffered File hexfile */
int loadfile(char *name)
{
/* No file name */
if (name == NULL)
{
errno = ENOENT;
return -errno;
}
/* Open file */
g_hexfile = bfopen(name, "r+b");
if (g_hexfile == NULL && errno == ENOENT)
{
/* Create file */
g_hexfile = bfopen(name, "w+b");
}
/* File didn't open */
if (g_hexfile == NULL)
{
return -errno;
}
bfread(g_hexfile);
return 0;
}
/* Save a buffered file hexfile */
int savefile(FAR struct bfile_s *file)
{
if (file == NULL || file->fp == NULL)
{
errno = EBADF;
return -errno;
}
bftruncate(file, file->size);
return bfflush(file);
}
/* Set or run a command */
int hexcmd(FAR struct command_s *cmd, int optc, char *opt)
{
switch (cmd->id)
{
case CMD_COPY:
optc = hexcopy(cmd, optc, opt);
break;
case CMD_COPY_OVER:
optc = hexcopy(cmd, optc, opt);
break;
case CMD_DUMP:
optc = hexdump(cmd, optc, opt);
break;
case CMD_ENTER:
optc = hexenter(cmd, optc, opt);
break;
case CMD_FIND:
optc = 0;
break;
case CMD_HELP:
optc = hexhelp(cmd, optc, opt);
break;
case CMD_INSERT:
optc = hexinsert(cmd, optc, opt);
break;
case CMD_MOVE:
optc = hexmove(cmd, optc, opt);
break;
case CMD_MOVE_OVER:
optc = hexmove(cmd, optc, opt);
break;
case CMD_REMOVE:
optc = hexremove(cmd, optc, opt);
break;
case CMD_WORD:
optc = hexword(cmd, optc, opt);
break;
default:
RETURN_ERR(EINVAL);
}
return optc;
}
/* New command found in the args */
FAR struct command_s *newcmd(int cmdno)
{
FAR struct command_s *cmd;
/* Command buffer overflow */
if (cmdno >= CMD_MAX_CNT)
{
errno = E2BIG;
perror(PNAME);
exit(-errno);
}
/* Set defaults */
cmd = &g_cmdtbl[cmdno];
memset(cmd, sizeof(struct command_s), 0);
cmd->id = cmdargs->argid;
cmd->cmd = cmdargs->arg;
cmd->flags = CMD_FL_CMDLINE;
cmd->opts.word = g_wordsize;
return cmd;
}
/* Parse the command line arguments */
int parseargs(char *argv[])
{
FAR char *fname;
FAR char *opt;
int cmdcnt;
int optc;
fname = NULL;
g_cmd = g_cmdtbl; /* 1st command is reserved */
cmdcnt = 0;
memset(g_cmd, sizeof(struct command_s), 0);
while (parsecmdargs(argv, g_arglist) != -1)
{
/* Set new command */
if (cmdargs->argid > 0)
{
cmdcnt++;
optc = 0;
opt = cmdargs->opt;
g_cmd = newcmd(cmdcnt);
/* Unknown arg */
}
else
{
opt = cmdargs->arg;
}
/* Set command options */
if ((optc = hexcmd(g_cmd, optc, opt)) < 0)
{
/* End of range option? */
if (*opt == '-')
{
/* Unknown option */
if (strlen(opt) > 1 && *(opt + 1) != '-')
{
hexed_error(EINVAL, "Unknown option: %s\n", opt);
}
/* Might be file name */
}
else if (fname == NULL)
{
fname = opt;
loadfile(fname);
/* Unexpected option */
}
else
{
hexed_error(EINVAL, "Unexpected option: %s\n", opt);
}
}
/* End of command options */
if (optc <= 0)
{
g_cmd = g_cmdtbl;
optc = 0;
}
}
return 0;
}
/* Run arguments from command line */
int runargs(void)
{
int quit;
int i;
/* Scan for help command */
quit = 0;
g_cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */
for (i = 1; i < CMD_MAX_CNT && g_cmd->id > 0; i++, g_cmd++)
{
if (g_cmd->id == CMD_HELP)
{
hexcmd(g_cmd, -1, NULL);
quit = CMD_QUIT;
break;
}
}
/* Loop through command list */
if (quit != CMD_QUIT)
{
g_cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */
for (i = 1; i < CMD_MAX_CNT && g_cmd->id > 0; i++, g_cmd++)
{
hexcmd(g_cmd, -1, NULL);
if ((g_cmd->flags & CMD_FL_QUIT) != 0)
{
quit = CMD_QUIT;
}
}
}
/* Save file */
if (quit == CMD_QUIT && g_hexfile->flags == BFILE_FL_DIRTY)
{
savefile(g_hexfile);
}
return quit;
}
/* hexed - Hexadecimal File Editor */
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int hexed_main(int argc, char *argv[])
#endif
{
g_wordsize = WORD_8;
/* Check command line arguments */
parseargs(argv);
/* Open file */
if (g_hexfile == NULL)
{
loadfile(TEMP_FILE);
}
/* Run commands */
runargs();
bfclose(g_hexfile);
(void)unlink(TEMP_FILE);
return 0;
}

165
system/hexed/src/hexenter.c Normal file
View File

@ -0,0 +1,165 @@
/****************************************************************************
* apps/system/hexed/src/hexenter.c
* hexed enter command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Enter bytes at dest */
static int runenter(FAR struct command_s *cmd)
{
/* Write to file */
bfwrite(g_hexfile, cmd->opts.dest, cmd->opts.buf, cmd->opts.bytes);
return 0;
}
/* Set the enter command */
static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt)
{
FAR char *s;
int64_t *hx, v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
}
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EINVAL);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set destination */
if (optc == 0)
{
cmd->opts.dest = v;
optc++;
/* Set values */
}
else if (optc > 0 && cmd->opts.cnt < OPT_BUF_SZ)
{
hx = cmd->opts.buf;
switch (cmd->opts.word)
{
case WORD_64:
*(hx + cmd->opts.cnt) = v;
break;
case WORD_32:
*((int32_t *) hx + cmd->opts.cnt) = v;
break;
case WORD_16:
*((int16_t *) hx + cmd->opts.cnt) = v;
break;
case WORD_8:
*((int8_t *) hx + cmd->opts.cnt) = v;
break;
default:
break;
}
cmd->opts.cnt++;
cmd->opts.bytes = cmd->opts.cnt * cmd->opts.word;
optc++;
/* Buffer overflow */
}
else
{
error(E2BIG, "Enter error: too many values set\n");
/* RETURN_ERR(E2BIG); */
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Enter command */
int hexenter(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_ENTER)
{
RETURN_ERR(EINVAL);
}
/* Set/run enter */
if (optc >= 0)
{
optc = setenter(cmd, optc, opt);
}
else
{
optc = runenter(cmd);
}
return optc;
}

126
system/hexed/src/hexhelp.c Normal file
View File

@ -0,0 +1,126 @@
/****************************************************************************
* apps/system/hexed/src/hexhelp.c
* hexed help command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Data
****************************************************************************/
static const char *helpmsg[] =
{
NULL,
" -? Shows this help screen\n",
" -co [src] [dest] [len] Copy data from src overwriting dest for len\n"
" words\n",
" -c [src] [dest] [len] Copy data from src to dest for len words\n",
" -d [src] [len] Display data from src for len words\n",
" -e [dest] [...] Enter hex values [...] at dest\n",
NULL,
" -i [dest] [cnt] [...] Insert hex values [...] at dest repeating cnt"
"\n times. Defaults to 0 for empty hex values.\n",
" -mo [src] [dest] [len] Move data from src overwriting dest for len\n"
" words\n",
" -m [src] [dest] [len] Move data from src to dest for len words\n",
" -r [src] [len] Remove data from src for len words\n",
" -w [bytes] Set the word size in bytes: 1 = 8 bits\n"
" 2 = 16 bits, 4 = 32 bits, 8 = 64 bits\n",
NULL,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* Display the help page */
static int runhelp(FAR struct command_s *cmd)
{
/* Show command line help */
printf("%s - Hexadecimal File Editor\n", PNAME);
printf(" v%d.%d.%d\n\n", VER_MAJOR, VER_MINOR, VER_REVISION);
printf("Usage:\n");
printf(" %s [options] [file]\n\n", PNAME);
printf("Options:\n");
printf("%s", helpmsg[CMD_HELP]);
printf("%s", helpmsg[CMD_COPY]);
printf("%s", helpmsg[CMD_COPY_OVER]);
printf("%s", helpmsg[CMD_DUMP]);
printf("%s", helpmsg[CMD_ENTER]);
printf("%s", helpmsg[CMD_INSERT]);
printf("%s", helpmsg[CMD_MOVE]);
printf("%s", helpmsg[CMD_MOVE_OVER]);
printf("%s", helpmsg[CMD_REMOVE]);
printf("%s", helpmsg[CMD_WORD]);
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Help command */
int hexhelp(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_HELP)
{
RETURN_ERR(EINVAL);
}
/* Run help */
if (optc >= 0)
{
optc = 0; /* Nothing to set */
}
else
{
cmd->flags |= CMD_FL_QUIT;
optc = runhelp(cmd);
}
return optc;
}

View File

@ -0,0 +1,193 @@
/****************************************************************************
* apps/system/hexed/src/hexinsert.c
* hexed insert command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Insert len words at dest */
static int runinsert(FAR struct command_s *cmd)
{
long dest, rpt;
/* Default to a 0 (zero) value for 1 count */
cmd->opts.bytes = cmd->opts.cnt * cmd->opts.word;
if (cmd->opts.bytes == 0)
{
cmd->opts.bytes = 1 * cmd->opts.word;
}
/* Insert bytes */
dest = cmd->opts.dest;
for (rpt = cmd->opts.len; rpt > 0; rpt--)
{
bfinsert(g_hexfile, dest, cmd->opts.buf, cmd->opts.bytes);
dest += cmd->opts.bytes;
}
return 0;
}
/* Set the insert command */
static int setinsert(FAR struct command_s *cmd, int optc, char *opt)
{
FAR char *s;
int64_t *hx, v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
}
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EINVAL);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set options */
switch (optc)
{
case 0:
/* Set destination */
cmd->opts.dest = v;
optc++;
break;
case 1:
/* Set count (len) */
cmd->opts.len = v;
optc++;
break;
default:
/* Set values */
if (optc > 1 && cmd->opts.cnt < OPT_BUF_SZ)
{
hx = cmd->opts.buf;
switch (cmd->opts.word)
{
case WORD_64:
*(hx + cmd->opts.cnt) = v;
break;
case WORD_32:
*((int32_t *) hx + cmd->opts.cnt) = v;
break;
case WORD_16:
*((int16_t *) hx + cmd->opts.cnt) = v;
break;
case WORD_8:
*((int8_t *) hx + cmd->opts.cnt) = v;
break;
default:
break;
}
cmd->opts.cnt++;
optc++;
/* Buffer overflow */
}
else
{
error(E2BIG, "Insert error: too many values set\n");
/* RETURN_ERR(E2BIG); */
}
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Insert command */
int hexinsert(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_INSERT)
{
RETURN_ERR(EINVAL);
}
/* Set/run insert */
if (optc >= 0)
{
optc = setinsert(cmd, optc, opt);
}
else
{
optc = runinsert(cmd);
}
return optc;
}

224
system/hexed/src/hexmove.c Normal file
View File

@ -0,0 +1,224 @@
/****************************************************************************
* apps/system/hexed/src/hexmove.c
* hexed move command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Move len words from src to dest */
static int move(FAR struct command_s *cmd)
{
bfmove(g_hexfile, cmd->opts.dest, cmd->opts.src, cmd->opts.bytes);
return 0;
}
/* Move overwriting len words from src to dest */
static int moveover(FAR struct command_s *cmd)
{
long cnt, len, off;
bfmove(g_hexfile, cmd->opts.dest, cmd->opts.src, cmd->opts.bytes);
/* Insert zeroes in chunks (keeps dest set) */
if (cmd->opts.src < cmd->opts.dest)
{
memset(cmd->opts.buf, 0, OPT_BUF_SZ);
len = OPT_BUF_SZ;
cnt = cmd->opts.bytes;
off = cmd->opts.dest - cmd->opts.bytes;
if (off < cmd->opts.src)
{
cnt = cmd->opts.dest - cmd->opts.src;
off = cmd->opts.src;
}
for (; cnt; cnt -= len)
{
/* End of count? */
if (cnt < len)
{
len = cnt;
}
/* Insert zeroes */
bfwrite(g_hexfile, off, cmd->opts.buf, len);
off += len;
}
/* Clip data */
}
else
{
bfclip(g_hexfile, cmd->opts.dest + cmd->opts.len, cmd->opts.bytes);
}
return 0;
}
/* Run the move command */
static int runmove(FAR struct command_s *cmd)
{
/* Error: Source past EOF */
if (cmd->opts.src > g_hexfile->size)
{
RETURN_ERR(EINVAL);
}
switch (cmd->id)
{
case CMD_MOVE:
return move(cmd);
case CMD_MOVE_OVER:
return moveover(cmd);
default:
return -1;
}
}
/* set the move command */
static int setmove(FAR struct command_s *cmd, int optc, char *opt)
{
FAR char *s;
uint64_t v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
}
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EFAULT);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set options */
switch (optc)
{
case 0:
/* Set source */
cmd->opts.src = v;
optc++;
break;
case 1:
/* Set destination */
cmd->opts.dest = v;
optc++;
break;
case 2:
/* Set length */
cmd->opts.len = v;
cmd->opts.bytes = cmd->opts.len * cmd->opts.word;
optc = 0;
break;
default:
/* Too many options specified */
RETURN_ERR(E2BIG);
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Move command */
int hexmove(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || (cmd->id != CMD_MOVE && cmd->id != CMD_MOVE_OVER))
{
RETURN_ERR(EINVAL);
}
/* Ret/run move */
if (optc >= 0)
{
optc = setmove(cmd, optc, opt);
}
else
{
optc = runmove(cmd);
}
return optc;
}

View File

@ -0,0 +1,145 @@
/****************************************************************************
* apps/system/hexed/src/hexremove.c
* hexed remove command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/* Remove len words from src */
static int runremove(FAR struct command_s *cmd)
{
/* Remove from file */
bfclip(g_hexfile, cmd->opts.src, cmd->opts.bytes);
return 0;
}
/* Set the remove command */
static int setremove(FAR struct command_s *cmd, int optc, char *opt)
{
FAR char *s;
uint64_t v;
/* Set defaults */
if (optc == 0)
{
cmd->flags |= CMD_FL_QUIT;
}
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EINVAL);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set options */
switch (optc)
{
case 0:
/* Set source */
cmd->opts.src = v;
optc++;
break;
case 1:
/* Set length */
cmd->opts.len = v;
cmd->opts.bytes = cmd->opts.len * cmd->opts.word;
optc = 0;
break;
default:
/* Too many options specified */
RETURN_ERR(E2BIG);
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Remove command */
int hexremove(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_REMOVE)
{
RETURN_ERR(EINVAL);
}
/* Set/run remove */
if (optc >= 0)
{
optc = setremove(cmd, optc, opt);
}
else
{
optc = runremove(cmd);
}
return optc;
}

140
system/hexed/src/hexword.c Normal file
View File

@ -0,0 +1,140 @@
/****************************************************************************
* apps/system/hexed/src/hexword.c
* hexed word command
*
* Copyright (c) 2011, B.ZaaR, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "bfile.h"
#include "hexed.h"
/****************************************************************************
* Public Data
****************************************************************************/
int g_wordsize;
/****************************************************************************
* Private Functions
****************************************************************************/
/* Set the word size */
static int setword(FAR struct command_s *cmd, int optc, char *opt)
{
char *s;
size_t v;
/* NULL option */
if (opt == NULL)
{
RETURN_ERR(EFAULT);
}
v = strtoll(opt, &s, 0x10);
/* No value set in option */
if (s == opt)
{
RETURN_ERR(EINVAL);
}
/* Set global word size */
if (optc == 0)
{
if (v >= WORD_64)
{
g_wordsize = WORD_64;
}
else if (v >= WORD_32)
{
g_wordsize = WORD_32;
}
else if (v >= WORD_16)
{
g_wordsize = WORD_16;
}
else
{
g_wordsize = WORD_8;
}
optc = 0; /* no more options accepted */
}
else
{
/* Too many options specified */
RETURN_ERR(E2BIG);
}
return optc;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/* Word command */
int hexword(FAR struct command_s *cmd, int optc, char *opt)
{
/* Invalid command */
if (cmd == NULL || cmd->id != CMD_WORD)
{
RETURN_ERR(EINVAL);
}
/* Set word size */
if (optc >= 0)
{
optc = setword(cmd, optc, opt);
}
else
{
optc = 0;
}
return optc;
}