summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2019-10-06 14:36:17 +0100
committerFreeArtMan <dos21h@gmail.com>2019-10-06 14:36:17 +0100
commit786bd1e76c6806a4c9272814f9ffe7d901998e4e (patch)
tree8b1e38e48a766c5a8dbad7f00ffb63d09376426c
parent8a8e0941dd2ab5f089662362a30c91e16de18ce0 (diff)
downloadihe-786bd1e76c6806a4c9272814f9ffe7d901998e4e.tar.gz
ihe-786bd1e76c6806a4c9272814f9ffe7d901998e4e.zip
Fix seek command, tested/works relative seek, and hex positioning support
-rw-r--r--cmd/cmd_blk.c3
-rw-r--r--cmd/cmd_dumpx.c2
-rw-r--r--cmd/cmd_seek.c71
-rw-r--r--core.c4
-rw-r--r--ihe.c1
-rw-r--r--libbuf/buf.c1
-rw-r--r--libcmd/cmd_parse.c57
-rw-r--r--libcmd/cmd_parse.ragel2
8 files changed, 103 insertions, 38 deletions
diff --git a/cmd/cmd_blk.c b/cmd/cmd_blk.c
index a568f98..b849026 100644
--- a/cmd/cmd_blk.c
+++ b/cmd/cmd_blk.c
@@ -24,7 +24,10 @@ int c_blk( cmd_arg_t *arg )
} else if ( argc == 1 )
{
if ((type[0] == CMDT_INT) || (type[0] == CMDT_HEX))
+ {
g_file->blk_size = atoi( argv[0] );
+ g_buf->realloc(g_file->blk_size);
+ }
} else
{
return -1;
diff --git a/cmd/cmd_dumpx.c b/cmd/cmd_dumpx.c
index 1181200..f6448f5 100644
--- a/cmd/cmd_dumpx.c
+++ b/cmd/cmd_dumpx.c
@@ -28,7 +28,7 @@ int c_dumpx( cmd_arg_t *arg )
if ( j<g_buf->cursize() )
{
char c;
- g_buf->getc(j,&c);
+ g_buf->getc(j, &c);
printf("%02x ",(unsigned char)c);
} else
{
diff --git a/cmd/cmd_seek.c b/cmd/cmd_seek.c
index d8bb768..2aa0310 100644
--- a/cmd/cmd_seek.c
+++ b/cmd/cmd_seek.c
@@ -8,11 +8,39 @@ extern file_t *g_file;
extern Buf *g_buf;
extern int g_flags;
+int if_decimal(char *s, int sz)
+{
+ int i=0;
+ while (i<sz)
+ {
+ //printf("%d\n",i);
+ if (!isdigit(s[i]))
+ return 0;
+ i++;
+ }
+ return 1;
+}
+
+int if_hex(char *s, int sz)
+{
+ int i=0;
+ while (i<sz)
+ {
+ //printf("%d\n",i);
+ if (!isxdigit(s[i]))
+ return 0;
+ i++;
+ }
+ return 1;
+}
+
int c_seek( cmd_arg_t *arg )
{
int fret;
int argc = arg->argc;
char **argv = arg->argv;
+ int *type = arg->type;
+ int checktype;
off_t offset;
int off_type = 0; //-1 seek down, 0 set pos, +1 seek up
@@ -28,7 +56,6 @@ int c_seek( cmd_arg_t *arg )
return -1;
}
- //set seek type
switch( argv[0][0] )
{
case '+':
@@ -41,14 +68,54 @@ int c_seek( cmd_arg_t *arg )
off_type = 0;
}
- offset = atoi( argv[0] ); //!fix that to strtol at least
+ //printf("Type %d\n",type[0]);
+ checktype = type[0];
+ if (checktype == CMDT_WORD)
+ {
+ if (if_decimal(argv[0]+1,strlen(argv[0])-1))
+ {
+ checktype = CMDT_INT;
+ } else if (if_hex(argv[0]+3,strlen(argv[0])-3))
+ {
+ checktype = CMDT_HEX;
+ }
+ }
+
+
+ //check argument type and convert to offset
+ if (checktype == CMDT_INT)
+ {
+ if (off_type != 0)
+ {
+ offset = atoi( argv[0] ); //!fix that to strtol at least
+ } else
+ {
+ offset = atoi( argv[0] );
+ }
+ } else if (checktype == CMDT_HEX)
+ {
+ if (off_type != 0)
+ {
+ offset = strtol( argv[0], NULL, 16);
+ } else
+ {
+ offset = strtol( argv[0]+3, NULL, 16);
+ }
+ } else
+ {
+ //unsupported argument do nothing
+ return 0;
+ }
+
if (off_type == 0)
{
//g_file offset maybe wrong
+ //printf("seekp\n");
fret = file_seekp( g_file, offset );
} else
{
+ //printf("seek\n");
fret = file_seek( g_file, offset );
}
if ( fret < 0 )
diff --git a/core.c b/core.c
index 1fe2891..7d2600b 100644
--- a/core.c
+++ b/core.c
@@ -28,7 +28,7 @@ int fd_seek( int fd, off_t offset, int whence )
off_new = lseek( fd, offset, whence );
if ( errno != 0)
{
- printf("Cannot seek %s\n", strerror(errno));
+ printf("!Cannot seek %s\n", strerror(errno));
errno = 0; //why i need to reset it?
return -1;
}
@@ -145,7 +145,7 @@ int file_seek( file_t *ft, off_t offset )
ret = fd_seek( ft->fd, offset, FD_SEEK_CUR );
- if (ret == 0)
+ if (ret >= 0)
{
new_off = fd_pos( ft->fd );
if (new_off > ft->size)
diff --git a/ihe.c b/ihe.c
index a257c3a..a797a31 100644
--- a/ihe.c
+++ b/ihe.c
@@ -439,7 +439,6 @@ cmd_table_t tab[] = {
{"cd", c_cd, h_cd, NULL, NULL},
{"pageup", c_pageup, h_pageup, NULL, NULL},
{"pagedown", c_pagedown, h_pagedown, NULL, NULL},
- //{"goto", c_goto, h_goto, NULL, NULL},
//{"bwrite", c_bwrite, h_bwrite, NULL, NULL},
//{"bread", c_bread, h_bread, NULL, NULL},
//{"create", c_create, h_create, NULL, NULL},
diff --git a/libbuf/buf.c b/libbuf/buf.c
index b62eb13..f2eaee4 100644
--- a/libbuf/buf.c
+++ b/libbuf/buf.c
@@ -318,6 +318,7 @@ int Buf::realloc(int size)
ptr = (char *)::realloc(this->buf, size);
if (ptr != NULL)
{
+ free(this->buf);
this->buf = ptr;
this->buf_size = size;
}
diff --git a/libcmd/cmd_parse.c b/libcmd/cmd_parse.c
index 91e1e8b..9d831cf 100644
--- a/libcmd/cmd_parse.c
+++ b/libcmd/cmd_parse.c
@@ -39,50 +39,45 @@ static const char _cmd_actions[] = {
};
static const char _cmd_key_offsets[] = {
- 0, 0, 3, 6, 22, 29, 40, 53,
- 64, 75
+ 0, 0, 3, 6, 18, 21, 28, 38,
+ 46, 53
};
static const char _cmd_trans_keys[] = {
34, 39, 92, 32, 34, 92, 32, 34,
- 44, 46, 47, 48, 9, 10, 33, 43,
- 49, 57, 58, 90, 95, 126, 33, 35,
- 43, 46, 90, 95, 126, 33, 35, 43,
- 46, 47, 48, 57, 58, 90, 95, 126,
- 33, 46, 47, 98, 120, 35, 43, 48,
- 57, 58, 90, 95, 126, 33, 46, 47,
- 35, 43, 48, 57, 58, 90, 95, 126,
- 33, 35, 43, 46, 47, 48, 49, 50,
- 90, 95, 126, 33, 35, 43, 46, 47,
- 48, 57, 58, 64, 65, 70, 71, 90,
- 95, 96, 97, 102, 103, 126, 0
+ 46, 48, 9, 10, 33, 47, 49, 57,
+ 58, 126, 33, 35, 126, 33, 35, 47,
+ 48, 57, 58, 126, 33, 46, 98, 120,
+ 35, 47, 48, 57, 58, 126, 33, 46,
+ 35, 47, 48, 57, 58, 126, 33, 35,
+ 47, 48, 49, 50, 126, 33, 35, 47,
+ 48, 57, 58, 64, 65, 70, 71, 96,
+ 97, 102, 103, 126, 0
};
static const char _cmd_single_lengths[] = {
- 0, 3, 3, 6, 1, 1, 5, 3,
+ 0, 3, 3, 4, 1, 1, 4, 2,
1, 1
};
static const char _cmd_range_lengths[] = {
- 0, 0, 0, 5, 3, 5, 4, 4,
- 5, 9
+ 0, 0, 0, 4, 1, 3, 3, 3,
+ 3, 7
};
static const char _cmd_index_offsets[] = {
- 0, 0, 4, 8, 20, 25, 32, 42,
- 50, 57
+ 0, 0, 4, 8, 17, 20, 25, 33,
+ 39, 44
};
static const char _cmd_indicies[] = {
1, 2, 3, 0, 0, 0, 0, 2,
- 4, 0, 4, 6, 5, 7, 4, 5,
- 8, 5, 5, 2, 5, 5, 5, 5,
- 9, 5, 5, 5, 6, 5, 5, 9,
- 5, 6, 5, 11, 12, 5, 8, 5,
- 5, 10, 5, 6, 5, 5, 8, 5,
- 5, 10, 5, 5, 5, 14, 5, 5,
- 13, 5, 5, 5, 15, 5, 15, 5,
- 5, 15, 5, 13, 0
+ 4, 0, 6, 7, 4, 5, 8, 5,
+ 2, 5, 5, 9, 5, 5, 6, 5,
+ 9, 5, 6, 11, 12, 5, 8, 5,
+ 10, 5, 6, 5, 8, 5, 10, 5,
+ 5, 14, 5, 13, 5, 5, 15, 5,
+ 15, 5, 15, 5, 13, 0
};
static const char _cmd_trans_targs[] = {
@@ -139,7 +134,7 @@ int parse_cmd( cmd_tok_t *tl, const char *str, size_t str_size )
*/
-#line 143 "cmd_parse.c"
+#line 138 "cmd_parse.c"
{
cs = cmd_start;
ts = 0;
@@ -149,7 +144,7 @@ int parse_cmd( cmd_tok_t *tl, const char *str, size_t str_size )
#line 83 "cmd_parse.ragel"
-#line 153 "cmd_parse.c"
+#line 148 "cmd_parse.c"
{
int _klen;
unsigned int _trans;
@@ -170,7 +165,7 @@ _resume:
#line 1 "NONE"
{ts = p;}
break;
-#line 174 "cmd_parse.c"
+#line 169 "cmd_parse.c"
}
}
@@ -283,7 +278,7 @@ _eof_trans:
}
}
break;
-#line 287 "cmd_parse.c"
+#line 282 "cmd_parse.c"
}
}
@@ -296,7 +291,7 @@ _again:
#line 1 "NONE"
{ts = 0;}
break;
-#line 300 "cmd_parse.c"
+#line 295 "cmd_parse.c"
}
}
diff --git a/libcmd/cmd_parse.ragel b/libcmd/cmd_parse.ragel
index 8cd8717..7bae484 100644
--- a/libcmd/cmd_parse.ragel
+++ b/libcmd/cmd_parse.ragel
@@ -45,7 +45,7 @@ int print_token( char *s, char *e, int tok)
'"';
quote = '"' (any-'"')* '"';
#seperate = ';';
- word = ([a-zA-Z0-9!#$%&'()*+`./:;<=>?@{}|~_])*;
+ word = ([a-zA-Z0-9!#$%&'()*+-`./:;<=>?@{}|~_])*;
sp = ([' ','\t','\n']);
main := |*