summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile10
-rw-r--r--cmd/cmd_blk.c17
-rw-r--r--cmd/cmd_cd.c32
-rw-r--r--cmd/cmd_close.c26
-rw-r--r--cmd/cmd_dump.c28
-rw-r--r--cmd/cmd_dumps.c36
-rw-r--r--cmd/cmd_dumpx.c55
-rw-r--r--cmd/cmd_flags.c44
-rw-r--r--cmd/cmd_info.c45
-rw-r--r--cmd/cmd_ls.c41
-rw-r--r--cmd/cmd_open.c40
-rw-r--r--cmd/cmd_pos.c25
-rw-r--r--cmd/cmd_pwd.c36
-rw-r--r--cmd/cmd_read.c29
-rw-r--r--cmd/cmd_seek.c61
-rw-r--r--cmd/cmd_size.c25
-rw-r--r--cmd/cmd_write.c91
-rw-r--r--cmd/cmd_writes.c46
-rw-r--r--ihe.c629
-rw-r--r--ihe.h20
21 files changed, 790 insertions, 548 deletions
diff --git a/.gitignore b/.gitignore
index 5f582e0..313f488 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,5 @@
ihe
TODO
test/*
+*.so
+*.a
diff --git a/Makefile b/Makefile
index 4a6b232..444323d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,21 @@
PROJECT=ihe
CC=gcc
-CFLAGS=
+CFLAGS=-I./
SOURCES=buf.c core.c
+SOURCES+=$(wildcard cmd/*.c)
OBJECTS=$(SOURCES:.c=.o)
LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o
all: clean $(OBJECTS) $(PROJECT)
$(PROJECT):
- $(CC) $(CFLAGS) $(OBJECTS) $(LIB_OBJECTS) $(PROJECT).c -o $(PROJECT)
+ $(CC) -c $(PROJECT).c
+ ld -r $(OBJECTS) $(PROJECT).o -o $(PROJECT)_.o
+
+ $(CC) $(CFLAGS) $(PROJECT)_.o $(LIB_OBJECTS) -o $(PROJECT)
%.o: %.c
- $(CC) $(CFLAGS) -c $<
+ $(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(PROJECT)
diff --git a/cmd/cmd_blk.c b/cmd/cmd_blk.c
new file mode 100644
index 0000000..db64f42
--- /dev/null
+++ b/cmd/cmd_blk.c
@@ -0,0 +1,17 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_blk( cmd_arg_t *arg )
+{
+ printf("FILE BLOCK SIZE %u\n", g_file->blk_size );
+ printf("BUFFER BLOCK SIZE %d (MAX %d)\n", g_buf->size, g_buf->buf_size );
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_cd.c b/cmd/cmd_cd.c
new file mode 100644
index 0000000..652217c
--- /dev/null
+++ b/cmd/cmd_cd.c
@@ -0,0 +1,32 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_cd( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ int fret = -1;
+
+ if ( argc != 1 )
+ {
+ printf("Only 1 argument needed\n");
+ return -1;
+ }
+
+ //printf("[%s]\n", argv[0]);
+ fret = chdir( argv[0] );
+ if ( fret == -1 )
+ {
+ printf("Cannot set dir to %s\n", argv[0]);
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_close.c b/cmd/cmd_close.c
new file mode 100644
index 0000000..d1d6fc7
--- /dev/null
+++ b/cmd/cmd_close.c
@@ -0,0 +1,26 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+/*
+CLOSE
+*/
+int c_close( cmd_arg_t *arg )
+{
+ int fret = 0;
+
+ fret = file_close( g_file );
+ if ( fret != 0 )
+ {
+ printf("Cannot close file\n");
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_dump.c b/cmd/cmd_dump.c
new file mode 100644
index 0000000..a11a2d0
--- /dev/null
+++ b/cmd/cmd_dump.c
@@ -0,0 +1,28 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_dump( cmd_arg_t *arg )
+{
+ int i;
+
+ if ( g_buf->buf == NULL)
+ {
+ printf("Buffer to print empty\n");
+ return -1;
+ }
+
+ for (i=0; i<g_buf->size; i++)
+ {
+ printf("%02x",(unsigned char)g_buf->buf[i]);
+ }
+ printf("\n");
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_dumps.c b/cmd/cmd_dumps.c
new file mode 100644
index 0000000..4f3461c
--- /dev/null
+++ b/cmd/cmd_dumps.c
@@ -0,0 +1,36 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_dumps( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ int i=0;
+
+ if ( argc != 0 )
+ {
+ printf("No arguments plz\n");
+ return -1;
+ }
+
+ for (i=0; i<g_buf->size; i++)
+ {
+ if (isprint(g_buf->buf[i]))
+ {
+ printf("%c", g_buf->buf[i]);
+ } else
+ {
+ printf("\e[7m.\e[0m");
+ }
+ }
+ printf("\n");
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_dumpx.c b/cmd/cmd_dumpx.c
new file mode 100644
index 0000000..d6228fd
--- /dev/null
+++ b/cmd/cmd_dumpx.c
@@ -0,0 +1,55 @@
+#include <ctype.h>
+#include <string.h>
+
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_dumpx( cmd_arg_t *arg )
+{
+ int i,j;
+
+ if ( g_buf->buf == NULL)
+ {
+ printf("Buffer to print empty\n");
+ return -1;
+ }
+
+ for (i=0; i<g_buf->size; i+=16)
+ {
+ for (j=i; j<i+16; j++)
+ {
+ if ( j<g_buf->size )
+ {
+ printf("%02x ",(unsigned char)g_buf->buf[j]);
+ } else
+ {
+ printf(" ");
+ }
+ }
+
+ for (j=i; j<i+16; j++)
+ {
+ if ( j<g_buf->size ) //wrong place move to cycle?
+ {
+ if ( isprint(g_buf->buf[j]) )
+ {
+ printf("%c",(unsigned char)g_buf->buf[j]);
+ } else
+ {
+ printf("\e[7m.\e[0m");
+ }
+ }
+ }
+ printf("\n");
+ }
+ printf("\n");
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_flags.c b/cmd/cmd_flags.c
new file mode 100644
index 0000000..0c4a8bd
--- /dev/null
+++ b/cmd/cmd_flags.c
@@ -0,0 +1,44 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_flags( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ char **argv = arg->argv;
+
+ if ( argc == 0 )
+ {
+ printf("FLAGS: 0x%08x\n", g_flags );
+ return 0;
+ }
+
+ if ( argc > 1 )
+ {
+ printf("Only one argument needed\n");
+ return -1;
+ }
+
+ if ( strncmp(argv[0],"R",2) == 0 )
+ {
+ g_flags = FD_RO;
+ } else if ( strncmp(argv[0],"W",2) == 0 )
+ {
+ g_flags = FD_WO;
+ } else if ( strncmp(argv[0],"RW",3) == 0 )
+ {
+ g_flags = FD_RW;
+ } else
+ {
+ printf("Unknown mode. Suported R/W/RW\n");
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_info.c b/cmd/cmd_info.c
new file mode 100644
index 0000000..b06f8bc
--- /dev/null
+++ b/cmd/cmd_info.c
@@ -0,0 +1,45 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+/*
+FILE
+*/
+int c_info( cmd_arg_t *arg )
+{
+
+ if ( g_file == NULL )
+ {
+ printf("no opened files\n");
+ } else
+ {
+ printf("FILE INFO:\n");
+ printf("NAME : %s\n", g_file->filename );
+ printf("FD : %d\n", g_file->fd );
+ printf("FLAGS : 0x%08x\n", g_file->flags );
+ printf("MODE : 0x%08x\n", g_file->mode );
+ printf("OFFSET : %zd\n", g_file->offset );
+ printf("POSITION: %ld\n", g_file->position );
+ printf("SIZE : %zd\n", g_file->size );
+ printf("BLOCK : %u\n", g_file->blk_size );
+ }
+
+ if ( g_buf == NULL )
+ {
+ printf("buffer not initialised\n");
+ } else
+ {
+ printf("BUF:\n");
+ printf("ADDR : %p\n", g_buf->buf);
+ printf("SIZE : %d\n", g_buf->size);
+ printf("BUFSIZE: %d\n", g_buf->buf_size);
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_ls.c b/cmd/cmd_ls.c
new file mode 100644
index 0000000..a3c642e
--- /dev/null
+++ b/cmd/cmd_ls.c
@@ -0,0 +1,41 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_ls( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ //char **argv = arg->argv;
+ uint8_t **f_ls = NULL;
+ int i=0;
+
+ if (argc > 0)
+ {
+ printf("Plz dont use arguments\n");
+ return -1;
+ }
+
+ f_ls = dir_list("./");
+ if ( f_ls == NULL )
+ {
+ printf("Cannot list current directory\n");
+ return -1;
+ }
+
+ i = 0;
+ while ( f_ls[i] != NULL )
+ {
+ printf("%s\n",f_ls[i]);
+ free( f_ls[i] );
+ i++;
+ }
+ free( f_ls );
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_open.c b/cmd/cmd_open.c
new file mode 100644
index 0000000..bbdfc48
--- /dev/null
+++ b/cmd/cmd_open.c
@@ -0,0 +1,40 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+/*
+OPEN <FILENAME>
+*/
+int c_open( cmd_arg_t *arg )
+{
+
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ char *fname = NULL;
+ int fret = 0;
+
+ if ( argc != 1 )
+ {
+ printf("Neeed one argument\n");
+ return -1;
+ }
+
+ fname = argv[0];
+
+ fret = file_open_fn( g_file, fname, g_flags ); //!if failure fields could be non empty inside struct
+ if ( fret < 0 )
+ {
+ printf("Cannot open file %s\n",fname);
+ return -1;
+ }
+
+
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_pos.c b/cmd/cmd_pos.c
new file mode 100644
index 0000000..845d69d
--- /dev/null
+++ b/cmd/cmd_pos.c
@@ -0,0 +1,25 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_pos( cmd_arg_t *arg )
+{
+ int fret = 0;
+
+ fret = file_pos( g_file );
+ if ( fret < 0)
+ {
+ printf("Cannot get file position\n");
+ return -1;
+ }
+
+ printf("POS:%d\n",fret);
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_pwd.c b/cmd/cmd_pwd.c
new file mode 100644
index 0000000..f14ece7
--- /dev/null
+++ b/cmd/cmd_pwd.c
@@ -0,0 +1,36 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_pwd( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ //char **argv = arg->argv;
+ char *cur_dir;
+
+ if ( argc > 0 )
+ {
+ printf("PLZ no arguments\n");
+ return -1;
+ }
+
+ cur_dir = get_current_dir_name();
+ if ( errno != 0 )
+ {
+ printf("Cannot get current dir\n");
+ free( cur_dir ); //on failure content unknown;
+ return -1;
+ }
+
+ printf("%s\n", cur_dir);
+ free( cur_dir );
+ cur_dir = NULL;
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_read.c b/cmd/cmd_read.c
new file mode 100644
index 0000000..965ae5a
--- /dev/null
+++ b/cmd/cmd_read.c
@@ -0,0 +1,29 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_read( cmd_arg_t *arg )
+{
+ int ret;
+
+ if ( g_buf->buf == NULL )
+ {
+ printf("Buffer mem not allocated\n");
+ return -1;
+ }
+
+ ret = file_read_blk( g_file, g_buf->buf );
+ printf("Readed %d bytes\n", ret);
+ if ( (ret >= 0) && (ret <= g_buf->buf_size) )
+ {
+ g_buf->size = ret;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_seek.c b/cmd/cmd_seek.c
new file mode 100644
index 0000000..b563231
--- /dev/null
+++ b/cmd/cmd_seek.c
@@ -0,0 +1,61 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_seek( cmd_arg_t *arg )
+{
+ int fret;
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ off_t offset;
+ int off_type = 0; //-1 seek down, 0 set pos, +1 seek up
+
+ if (argc != 1)
+ {
+ printf("One argument needed\n");
+ return -1;
+ }
+
+ if (g_file->fd == 0)
+ {
+ printf("File descriptor not set\n");
+ return -1;
+ }
+
+ //set seek type
+ switch( argv[0][0] )
+ {
+ case '+':
+ off_type = 1;
+ break;
+ case '-':
+ off_type = -1;
+ break;
+ default:
+ off_type = 0;
+ }
+
+ offset = atoi( argv[0] ); //!fix that to strtol at least
+
+ if (off_type == 0)
+ {
+ //g_file offset maybe wrong
+ fret = file_seekp( g_file, offset );
+ } else
+ {
+ fret = file_seek( g_file, offset );
+ }
+ if ( fret != 0 )
+ {
+ printf("Cannot seek postion to %zd\n", offset);
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_size.c b/cmd/cmd_size.c
new file mode 100644
index 0000000..5f27ee1
--- /dev/null
+++ b/cmd/cmd_size.c
@@ -0,0 +1,25 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+int c_size( cmd_arg_t *arg )
+{
+ off_t size;
+
+ size = file_size( g_file );
+ if ( size < 0 )
+ {
+ printf("Cannot get file size\n");
+ return -1;
+ }
+
+ printf("File size %zu\n", size);
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_write.c b/cmd/cmd_write.c
new file mode 100644
index 0000000..f5d6e01
--- /dev/null
+++ b/cmd/cmd_write.c
@@ -0,0 +1,91 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+//support masks
+int c_write( cmd_arg_t *arg)
+{
+ /*
+ anonymous function
+ */
+ uint8_t hex2u8( uint8_t *buf )
+ {
+ uint8_t ret = 0x00;
+ unsigned long int uli;
+ char str[3];
+ str[0] = buf[0];
+ str[1] = buf[1];
+ str[2] = 0;
+
+ uli = strtoul( str, NULL, 16 );
+
+ ret = uli;
+
+ return ret;
+ }
+
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ int i;
+ uint8_t *buf = NULL;
+ int fret;
+
+ if ( argc != 1 )
+ {
+ printf("One argument needed\n");
+ return -1;
+ }
+
+ if ( (strlen(argv[0])%2) != 0 )
+ {
+ printf("Input string should be ( str mod 2 == 0) \n");
+ return -1;
+ }
+
+ for (i=0;i<strlen(argv[0]);i++)
+ {
+ if ( !isxdigit(argv[0][i]) )
+ {
+ printf("CH %c not hexlike at pos %d\n", argv[0][i], i);
+ return -1;
+ }
+ }
+
+ if (strlen(argv[0]) > g_buf->size*2)
+ {
+ printf("Input param bigger then buffer\n");
+ return -1;
+ }
+
+
+ for (i=0; i<strlen(argv[0]); i+=2)
+ {
+ printf("%02x ",(unsigned char)hex2u8((unsigned char*)&argv[0][i]));
+ }
+ printf("\n");
+
+
+ buf = malloc(strlen(argv[0])/2);
+ for (i=0; i<(strlen(argv[0])/2); i++)
+ {
+ buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
+ }
+
+ memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
+ fret = file_write_blk( g_file, g_buf->buf );
+ free( buf );
+
+ if ( fret < 0)
+ {
+ printf("Couldnt write block to file\n");
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd/cmd_writes.c b/cmd/cmd_writes.c
new file mode 100644
index 0000000..1ddc95a
--- /dev/null
+++ b/cmd/cmd_writes.c
@@ -0,0 +1,46 @@
+#include "buf.h"
+#include "core.h"
+
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+extern file_t *g_file;
+extern buf_t *g_buf;
+extern int g_flags;
+
+//white spaces should be supported
+int c_writes( cmd_arg_t *arg )
+{
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ int fret = 0;
+
+ if ( argc != 1)
+ {
+ printf("Need one argument mister\n");
+ return -1;
+ }
+
+ if (((g_buf == NULL) || (g_file == NULL)) || (g_buf->buf == NULL))
+ {
+ printf("Buffer or file not initialised");
+ return -1;
+ }
+
+ if ( strlen(argv[0]) <= g_buf->size )
+ {
+ memcpy( g_buf->buf, argv[0], strlen(argv[0]) );
+ fret = file_write_blk( g_file, g_buf->buf );
+ if ( fret < 0 )
+ {
+ printf("Couldnt write block to file\n");
+ return -1;
+ }
+ } else
+ {
+ printf("Input bigger then buffer buf %d input %zu\n", g_buf->size, strlen(argv[0]));
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/ihe.c b/ihe.c
index c38380b..00f654e 100644
--- a/ihe.c
+++ b/ihe.c
@@ -20,6 +20,8 @@ type == 2, print in hex
*/
int cmd_buf_print( cmd_in_buf_t *buf, int type );
int cmd_buf_add( cmd_in_buf_t *buf, char *ch, int size );
+int cmd_buf_rem1( cmd_in_buf_t *buf );
+int cmd_buf_full( cmd_in_buf_t *buf );
int cmd_buf_clean( cmd_in_buf_t *buf )
{
@@ -48,7 +50,7 @@ int cmd_buf_print( cmd_in_buf_t *buf, int type )
printf("%02x ", (unsigned char)(buf->buf[i]&0xff) );
}
}
- printf("\n");
+ fflush(stdout);
return 0;
}
@@ -147,6 +149,7 @@ int cmd_tab_autocomplete( char *in_buf )
struct cmd_acq_head_t *ac = cmd_acq(tab,args->argv[0]);
if (ac != NULL)
{
+ printf("\n>");
//printf("Did you mean ");
SLIST_FOREACH(iter_sugg,ac,next_sugg)
{
@@ -194,12 +197,37 @@ int cmd_buf_add( cmd_in_buf_t *buf, char *ch, int size )
return 0;
}
+int cmd_buf_rem1( cmd_in_buf_t *buf )
+{
+ int ret = -1;
+
+ if ( buf->cur_sz > 0 )
+ {
+ buf->cur_sz -= 1;
+ ret = 0;
+ }
+
+ return ret;
+}
+
+int cmd_buf_full( cmd_in_buf_t *buf )
+{
+ int ret = 0;
+
+ if ( buf->cur_sz == (CMD_IN_BUF_SIZE-1) )
+ {
+ return 1;
+ }
+
+ return ret;
+}
+
/*
GLOBAL VARIABLES
*/
-static file_t *g_file = NULL;
-static buf_t *g_buf = NULL;
-static int g_flags = FD_RW;
+file_t *g_file = NULL;
+buf_t *g_buf = NULL;
+int g_flags = FD_RW;
int c_version( cmd_arg_t *arg )
@@ -253,459 +281,6 @@ int c_help( cmd_arg_t *arg )
}
-/*
-OPEN <FILENAME>
-*/
-int c_open( cmd_arg_t *arg )
-{
-
- int argc = arg->argc;
- char **argv = arg->argv;
- char *fname = NULL;
- int fret = 0;
-
- if ( argc != 1 )
- {
- printf("Neeed one argument\n");
- return -1;
- }
-
- fname = argv[0];
-
- fret = file_open_fn( g_file, fname, g_flags ); //!if failure fields could be non empty inside struct
- if ( fret < 0 )
- {
- printf("Cannot open file %s\n",fname);
- return -1;
- }
-
-
-
- return 0;
-}
-
-
-/*
-CLOSE
-*/
-int c_close( cmd_arg_t *arg )
-{
- int fret = 0;
-
- fret = file_close( g_file );
- if ( fret != 0 )
- {
- printf("Cannot close file\n");
- return -1;
- }
-
- return 0;
-}
-
-
-/*
-FILE
-*/
-int c_info( cmd_arg_t *arg )
-{
-
- if ( g_file == NULL )
- {
- printf("no opened files\n");
- } else
- {
- printf("FILE INFO:\n");
- printf("NAME : %s\n", g_file->filename );
- printf("FD : %d\n", g_file->fd );
- printf("FLAGS : 0x%08x\n", g_file->flags );
- printf("MODE : 0x%08x\n", g_file->mode );
- printf("OFFSET : %zd\n", g_file->offset );
- printf("POSITION: %ld\n", g_file->position );
- printf("SIZE : %zd\n", g_file->size );
- printf("BLOCK : %u\n", g_file->blk_size );
- }
-
- if ( g_buf == NULL )
- {
- printf("buffer not initialised\n");
- } else
- {
- printf("BUF:\n");
- printf("ADDR : %p\n", g_buf->buf);
- printf("SIZE : %d\n", g_buf->size);
- printf("BUFSIZE: %d\n", g_buf->buf_size);
- }
-
- return 0;
-}
-
-
-int c_seek( cmd_arg_t *arg )
-{
- int fret;
- int argc = arg->argc;
- char **argv = arg->argv;
- off_t offset;
- int off_type = 0; //-1 seek down, 0 set pos, +1 seek up
-
- if (argc != 1)
- {
- printf("One argument needed\n");
- return -1;
- }
-
- if (g_file->fd == 0)
- {
- printf("File descriptor not set\n");
- return -1;
- }
-
- //set seek type
- switch( argv[0][0] )
- {
- case '+':
- off_type = 1;
- break;
- case '-':
- off_type = -1;
- break;
- default:
- off_type = 0;
- }
-
- offset = atoi( argv[0] ); //!fix that to strtol at least
-
- if (off_type == 0)
- {
- //g_file offset maybe wrong
- fret = file_seekp( g_file, offset );
- } else
- {
- fret = file_seek( g_file, offset );
- }
- if ( fret != 0 )
- {
- printf("Cannot seek postion to %zd\n", offset);
- return -1;
- }
-
- return 0;
-}
-
-
-int c_pos( cmd_arg_t *arg )
-{
- int fret = 0;
-
- fret = file_pos( g_file );
- if ( fret < 0)
- {
- printf("Cannot get file position\n");
- return -1;
- }
-
- printf("POS:%d\n",fret);
-
- return 0;
-}
-
-
-int c_size( cmd_arg_t *arg )
-{
- off_t size;
-
- size = file_size( g_file );
- if ( size < 0 )
- {
- printf("Cannot get file size\n");
- return -1;
- }
-
- printf("File size %zu\n", size);
-
- return 0;
-}
-
-
-int c_blk( cmd_arg_t *arg )
-{
- printf("FILE BLOCK SIZE %u\n", g_file->blk_size );
- printf("BUFFER BLOCK SIZE %d (MAX %d)\n", g_buf->size, g_buf->buf_size );
-
- return 0;
-}
-
-
-int c_read( cmd_arg_t *arg )
-{
- int ret;
-
- if ( g_buf->buf == NULL )
- {
- printf("Buffer mem not allocated\n");
- return -1;
- }
-
- ret = file_read_blk( g_file, g_buf->buf );
- printf("Readed %d bytes\n", ret);
- if ( (ret >= 0) && (ret <= g_buf->buf_size) )
- {
- g_buf->size = ret;
- }
-
- return 0;
-}
-
-
-int c_dump( cmd_arg_t *arg )
-{
- int i;
-
- if ( g_buf->buf == NULL)
- {
- printf("Buffer to print empty\n");
- return -1;
- }
-
- for (i=0; i<g_buf->size; i++)
- {
- printf("%02x",(unsigned char)g_buf->buf[i]);
- }
- printf("\n");
-
- return 0;
-}
-
-
-int c_dumpx( cmd_arg_t *arg )
-{
- int i,j;
-
- if ( g_buf->buf == NULL)
- {
- printf("Buffer to print empty\n");
- return -1;
- }
-
- for (i=0; i<g_buf->size; i+=16)
- {
- for (j=i; j<i+16; j++)
- {
- if ( j<g_buf->size )
- {
- printf("%02x ",(unsigned char)g_buf->buf[j]);
- } else
- {
- printf(" ");
- }
- }
-
- for (j=i; j<i+16; j++)
- {
- if ( j<g_buf->size ) //wrong place move to cycle?
- {
- if ( isprint(g_buf->buf[j]) )
- {
- printf("%c",(unsigned char)g_buf->buf[j]);
- } else
- {
- printf("\e[7m.\e[0m");
- }
- }
- }
- printf("\n");
- }
- printf("\n");
-
- return 0;
-}
-
-
-int c_dumps( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- char **argv = arg->argv;
- int i=0;
-
- if ( argc != 0 )
- {
- printf("No arguments plz\n");
- return -1;
- }
-
- for (i=0; i<g_buf->size; i++)
- {
- if (isprint(g_buf->buf[i]))
- {
- printf("%c", g_buf->buf[i]);
- } else
- {
- printf("\e[7m.\e[0m");
- }
- }
- printf("\n");
-
- return 0;
-}
-
-
-//support masks
-int c_write( cmd_arg_t *arg)
-{
- /*
- anonymous function
- */
- uint8_t hex2u8( uint8_t *buf )
- {
- uint8_t ret = 0x00;
- unsigned long int uli;
- char str[3];
- str[0] = buf[0];
- str[1] = buf[1];
- str[2] = 0;
-
- uli = strtoul( str, NULL, 16 );
-
- ret = uli;
-
- return ret;
- }
-
- int argc = arg->argc;
- char **argv = arg->argv;
- int i;
- uint8_t *buf = NULL;
- int fret;
-
- if ( argc != 1 )
- {
- printf("One argument needed\n");
- return -1;
- }
-
- if ( (strlen(argv[0])%2) != 0 )
- {
- printf("Input string should be ( str mod 2 == 0) \n");
- return -1;
- }
-
- for (i=0;i<strlen(argv[0]);i++)
- {
- if ( !isxdigit(argv[0][i]) )
- {
- printf("CH %c not hexlike at pos %d\n", argv[0][i], i);
- return -1;
- }
- }
-
- if (strlen(argv[0]) > g_buf->size*2)
- {
- printf("Input param bigger then buffer\n");
- return -1;
- }
-
-
- for (i=0; i<strlen(argv[0]); i+=2)
- {
- printf("%02x ",(unsigned char)hex2u8((unsigned char*)&argv[0][i]));
- }
- printf("\n");
-
-
- buf = malloc(strlen(argv[0])/2);
- for (i=0; i<(strlen(argv[0])/2); i++)
- {
- buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
- }
-
- memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
- fret = file_write_blk( g_file, g_buf->buf );
- free( buf );
-
- if ( fret < 0)
- {
- printf("Couldnt write block to file\n");
- return -1;
- }
-
- return 0;
-}
-
-
-//white spaces should be supported
-int c_writes( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- char **argv = arg->argv;
- int fret = 0;
-
- if ( argc != 1)
- {
- printf("Need one argument mister\n");
- return -1;
- }
-
- if (((g_buf == NULL) || (g_file == NULL)) || (g_buf->buf == NULL))
- {
- printf("Buffer or file not initialised");
- return -1;
- }
-
- if ( strlen(argv[0]) <= g_buf->size )
- {
- memcpy( g_buf->buf, argv[0], strlen(argv[0]) );
- fret = file_write_blk( g_file, g_buf->buf );
- if ( fret < 0 )
- {
- printf("Couldnt write block to file\n");
- return -1;
- }
- } else
- {
- printf("Input bigger then buffer buf %d input %zu\n", g_buf->size, strlen(argv[0]));
- return -1;
- }
-
- return 0;
-}
-
-
-int c_flags( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- char **argv = arg->argv;
-
- if ( argc == 0 )
- {
- printf("FLAGS: 0x%08x\n", g_flags );
- return 0;
- }
-
- if ( argc > 1 )
- {
- printf("Only one argument needed\n");
- return -1;
- }
-
- if ( strncmp(argv[0],"R",2) == 0 )
- {
- g_flags = FD_RO;
- } else if ( strncmp(argv[0],"W",2) == 0 )
- {
- g_flags = FD_WO;
- } else if ( strncmp(argv[0],"RW",3) == 0 )
- {
- g_flags = FD_RW;
- } else
- {
- printf("Unknown mode. Suported R/W/RW\n");
- return -1;
- }
-
- return 0;
-}
-
-
int c_manifesto( cmd_arg_t *arg )
{
printf("""\
@@ -749,91 +324,6 @@ int c_manifesto( cmd_arg_t *arg )
return 0;
}
-
-int c_ls( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- //char **argv = arg->argv;
- uint8_t **f_ls = NULL;
- int i=0;
-
- if (argc > 0)
- {
- printf("Plz dont use arguments\n");
- return -1;
- }
-
- f_ls = dir_list("./");
- if ( f_ls == NULL )
- {
- printf("Cannot list current directory\n");
- return -1;
- }
-
- i = 0;
- while ( f_ls[i] != NULL )
- {
- printf("%s\n",f_ls[i]);
- free( f_ls[i] );
- i++;
- }
- free( f_ls );
-
- return 0;
-}
-
-int c_pwd( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- //char **argv = arg->argv;
- char *cur_dir;
-
- if ( argc > 0 )
- {
- printf("PLZ no arguments\n");
- return -1;
- }
-
- cur_dir = get_current_dir_name();
- if ( errno != 0 )
- {
- printf("Cannot get current dir\n");
- free( cur_dir ); //on failure content unknown;
- return -1;
- }
-
- printf("%s\n", cur_dir);
- free( cur_dir );
- cur_dir = NULL;
-
- return 0;
-}
-
-
-int c_cd( cmd_arg_t *arg )
-{
- int argc = arg->argc;
- char **argv = arg->argv;
- int fret = -1;
-
- if ( argc != 1 )
- {
- printf("Only 1 argument needed\n");
- return -1;
- }
-
- //printf("[%s]\n", argv[0]);
- fret = chdir( argv[0] );
- if ( fret == -1 )
- {
- printf("Cannot set dir to %s\n", argv[0]);
- return -1;
- }
-
- return 0;
-}
-
-
int c_th( cmd_arg_t *arg )
{
/*
@@ -886,7 +376,7 @@ int c_tw( cmd_arg_t *arg )
CMD COMMANDS
*/
cmd_table_t tab[] = {
- /*
+
{"version", c_version },
{"arg", c_arg },
{"quit", c_quit},
@@ -909,7 +399,7 @@ cmd_table_t tab[] = {
{"manifesto", c_manifesto},
{"ls", c_ls},
{"pwd", c_pwd},
- {"cd", c_cd},*/
+ {"cd", c_cd},
//{"th", c_th},
//{"tw", c_tw},
{NULL, NULL }
@@ -923,6 +413,10 @@ int main( int argc, char **argv )
uint8_t in_buf[sz_buf];
int ret_read;
int new_c=0, new_r=0, old_r=0, old_c=0;
+ cmd_arg_t *args;
+
+ cmd_tok_t tl, *ptr = &tl, *iter = NULL;
+ memset( &tl, 0, sizeof( cmd_tok_t ));
//preapre global stuff
g_file = file_init();
@@ -953,9 +447,10 @@ int main( int argc, char **argv )
term_cur_set_c( &ts, 0);
term_cur_set_r( &ts, old_r);
+ write(1,"$",1);
while ( 1 == 1 )
{
-
+ //write(1,"$",1);
ret_read = read(2, in_buf, sz_buf );
if (ret_read < 0)
{
@@ -981,14 +476,26 @@ int main( int argc, char **argv )
cmd_in.buf[cmd_in.cur_sz] = '\n';
cmd_in.buf[cmd_in.cur_sz+1] = 0;
cmd_tab_autocomplete( cmd_in.buf );
+ write(1,"$",1);
cmd_buf_print( &cmd_in, 1 );
continue;
}
+ /*
if (CMK_KEYMAP1('?'))
{
cmd_buf_print( &cmd_in, 1 );
}
+ */
+
+ if (CMK_KEYMAP1(8))
+ {
+ cmd_buf_rem1( &cmd_in );
+ term_cur_set_c( &ts, 0);
+ write(1,"$",1);
+ cmd_buf_print( &cmd_in, 1 );
+ continue;
+ }
if (CMK_KEYMAP1(27))
{
@@ -996,9 +503,41 @@ int main( int argc, char **argv )
break;
}
+ //exec cmd
if (CMK_KEYMAP1('\n'))
{
+ int i;
+
+ cmd_in.buf[cmd_in.cur_sz] = '\n';
+ cmd_in.buf[cmd_in.cur_sz+1] = 0;
+ if ( parse_cmd( ptr, cmd_in.buf) == -1)
+ {
+ continue;
+ }
+
+ iter = ptr->next;
+ args = cmd_arg_create( iter );
+ cmd_tok_destroy( ptr->next );
+ ptr->next = NULL;
+
+ /*
+ for (i=0; i<args->argc; i++)
+ {
+ printf("ARG:%d TYPE:%d %s\n", i, args->type[i], args->argv[i]);
+ }
+ */
+
+
+ if (cmd_exec(tab, args) != 0)
+ {
+ printf("!Unknown command\n");
+ }
+
+
+ cmd_arg_destroy( args );
cmd_buf_clean( &cmd_in );
+
+ write(1,"$",1);
}
}
diff --git a/ihe.h b/ihe.h
index 5331e55..fd2c0b2 100644
--- a/ihe.h
+++ b/ihe.h
@@ -15,4 +15,24 @@
#include "libterm/term.h"
#include "libterm/term_io.h"
+int c_blk( cmd_arg_t *arg );
+int c_cd( cmd_arg_t *arg );
+int c_close( cmd_arg_t *arg );
+int c_dump( cmd_arg_t *arg );
+int c_dumps( cmd_arg_t *arg );
+int c_dumpx( cmd_arg_t *arg );
+int c_flags( cmd_arg_t *arg );
+int c_info( cmd_arg_t *arg );
+int c_ls( cmd_arg_t *arg );
+int c_open( cmd_arg_t *arg );
+int c_pos( cmd_arg_t *arg );
+int c_pwd( cmd_arg_t *arg );
+int c_read( cmd_arg_t *arg );
+int c_seek( cmd_arg_t *arg );
+int c_size( cmd_arg_t *arg );
+int c_write( cmd_arg_t *arg );
+int c_writes( cmd_arg_t *arg );
+
+
+
#endif \ No newline at end of file