aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2016-05-10 22:42:11 +0100
committerFreeArtMan <dos21h@gmail.com>2016-05-10 22:42:11 +0100
commit44dedb90f005bb0de9bddbd592a5c78f28bc67d1 (patch)
tree274b9cea44df3f96a965c564e26cdac42d3af94f
parentbb426577c85f6d97cc28ab677e98e2b1f73eadac (diff)
downloadihe-44dedb90f005bb0de9bddbd592a5c78f28bc67d1.tar.gz
ihe-44dedb90f005bb0de9bddbd592a5c78f28bc67d1.zip
Replace cmd first part
-rw-r--r--Makefile8
-rw-r--r--cmd.c168
-rw-r--r--cmd.h33
-rw-r--r--core.c71
-rw-r--r--core.h21
-rw-r--r--ihe.c558
-rw-r--r--ihe.h8
-rw-r--r--libcmd/Makefile43
-rw-r--r--libcmd/cmd.c544
-rw-r--r--libcmd/cmd.h85
-rw-r--r--libcmd/cmd_parse.c309
-rw-r--r--libcmd/cmd_parse.h8
-rw-r--r--libcmd/cmd_parse.ragel86
-rw-r--r--libcmd/debug.h69
-rw-r--r--libcmd/queue.h533
-rw-r--r--libterm/debug.h69
-rw-r--r--libterm/screen_modes.h15
-rw-r--r--libterm/term.h70
-rw-r--r--libterm/term_gui.h24
-rw-r--r--libterm/term_io.h32
20 files changed, 2481 insertions, 273 deletions
diff --git a/Makefile b/Makefile
index 7a35c26..4a6b232 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,14 @@
PROJECT=ihe
CC=gcc
CFLAGS=
-SOURCES=buf.c cmd.c core.c
-OBJECTS=$(SOURCES:.c=.o)
+SOURCES=buf.c core.c
+OBJECTS=$(SOURCES:.c=.o)
+LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o
all: clean $(OBJECTS) $(PROJECT)
$(PROJECT):
- $(CC) $(CFLAGS) $(OBJECTS) $(PROJECT).c -o $(PROJECT)
+ $(CC) $(CFLAGS) $(OBJECTS) $(LIB_OBJECTS) $(PROJECT).c -o $(PROJECT)
%.o: %.c
$(CC) $(CFLAGS) -c $<
@@ -18,5 +19,4 @@ clean:
leak:
valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./ihe
- #valgrind --track-origins=yes --log-file=log.txt ./dm -f test/test.bin -m test/one_byte.dm
diff --git a/cmd.c b/cmd.c
deleted file mode 100644
index 2fb1a76..0000000
--- a/cmd.c
+++ /dev/null
@@ -1,168 +0,0 @@
-#include "cmd.h"
-
-
-int cnt_sep( char *s )
-{
- int cnt=0;
- char *p=s;
- while ( *p++ )
- {
- if (p[0]==' ')
- {
- cnt++;
- }
- }
-
- return cnt;
-
-}
-
-
-char* cmd_line( char *prompt )
-{
- char *ret = NULL;
- char *pmt = ">";
- const int buf_sz = 32;
- int ret_sz;
- char buf[buf_sz];
-
-
- if ( prompt != NULL )
- {
- pmt = prompt;
- }
-
- memset( buf, 0, buf_sz );
- write( 1, pmt, strlen(pmt) );
- ret_sz = read( 2, buf,buf_sz);
- if (ret_sz < 1)
- return ret;
-
- ret = malloc(ret_sz-1);
- memcpy( ret, buf, ret_sz-1 );
-
- return ret;
-}
-
-
-cmd_arg* cmd_parse( char *str )
-{
- int cnt=0;
- cmd_arg *ret = NULL;
- int i,j;
- char *last=str;
- int sz;
-
-
- //count white spaces
- cnt = cnt_sep( str )+1;
-
- ret = malloc( sizeof(cmd_arg) );
- ret->argv = malloc( sizeof(void*)*cnt );
-
- ret->argc = cnt;
-
- if ( cnt == 1 )
- {
- ret->argv[0] = malloc( strlen(str) );
- memcpy( ret->argv[0], str, strlen(str) );
- return ret;
- }
-
- //best practices
- j = 0;
- for (i=0;i<strlen(str);i++)
- {
- if (str[i]==' ')
- {
- sz = (str+i)-last;
- ret->argv[j] = malloc( sz+1 );
- memcpy(ret->argv[j],last,sz);
- ret->argv[j][sz] = '\0';
- last = str+i+1;
- j++;
- }
- }
- sz = str + i - last;
- ret->argv[j] = malloc(sz+1);
- memcpy( ret->argv[j], last, sz );
- ret->argv[j][sz] = '\0';
-
- return ret;
-}
-
-int cmd_exec( cmd_arg *cmd, cmd_table *table )
-{
- int ret = 0;
- int fret = 0;
- int i;
- cmd_arg *sub_arg;
-
- if (cmd->argc < 1)
- {
- printf("Hm ... no arguments\n");
- return -1;
- }
-
- i = 0;
- while ( (table[i].cmd != NULL) && (table[i].clb != NULL) )
- {
- if ((strlen(table[i].cmd) == strlen(cmd->argv[0]))
- && (strlen(table[i].cmd) != 0))
- if( strncmp( table[i].cmd, cmd->argv[0], strlen(cmd->argv[0]) ) == 0 )
- {
- if ( table[i].clb == NULL )
- {
- printf("Empty callback for %s\n",table[i].cmd);
- ret = -1;
- break;
- }
-
- sub_arg = sub_cmd( cmd );
- fret = table[i].clb( sub_arg );
- if ( fret != 0 )
- {
- printf("Command unsuccesfull execution\n");
- ret = -1;
- break;
- }
- break;
-
- }
-
- i++;
- }
-
-
- return ret;
-}
-
-
-void cmd_arg_free( cmd_arg *arg )
-{
-
-}
-
-void cmd_sub_arg_free( cmd_arg *arg )
-{
-
-}
-
-
-cmd_arg* sub_cmd( cmd_arg *arg )
-{
- int i;
- cmd_arg *ret=NULL;
-
- if ( arg->argc < 1)
- return NULL;
-
- ret = malloc( sizeof(cmd_arg) );
- ret->argc = arg->argc-1;
- ret->argv = malloc( sizeof(void*)*ret->argc );
- for (i=0;i<ret->argc;i++)
- ret->argv[i] = arg->argv[i+1];
-
- return ret;
-}
-
diff --git a/cmd.h b/cmd.h
deleted file mode 100644
index 436feac..0000000
--- a/cmd.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#ifndef __IHE_CMD_H
-#define __IHE_CMD_H
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <unistd.h>
-
-typedef struct cmd_arg
-{
- int argc;
- char **argv;
-} cmd_arg;
-
-
-typedef struct cmd_table
-{
- char *cmd;
- int (*clb)(cmd_arg*);
-} cmd_table;
-
-
-int cnt_sep( char *s );
-char* cmd_line( char *prompt );
-cmd_arg* cmd_parse( char *str );
-int cmd_exec( cmd_arg *cmd, cmd_table *table );
-void cmd_arg_free( cmd_arg *arg );
-void cmd_sub_arg_free( cmd_arg *arg );
-cmd_arg* sub_cmd( cmd_arg *arg );
-
-
-#endif
diff --git a/core.c b/core.c
index 2711111..a152656 100644
--- a/core.c
+++ b/core.c
@@ -32,6 +32,7 @@ int fd_seek( int fd, off_t offset, int whence )
errno = 0; //why i need to reset it?
return -1;
}
+ ret = off_new;
return ret;
}
@@ -82,7 +83,6 @@ ssize_t fd_write( int fd, void *buf, size_t count )
{
ssize_t ret;
- printf("%d %x %d\n", fd, buf, count);
ret = write( fd, buf, count );
if ( errno != 0)
{
@@ -165,6 +165,18 @@ int file_seek( file_t *ft, off_t offset )
}
+int file_seekp( file_t *ft, off_t offset )
+{
+ int ret = 0;
+ off_t new_off = 0;
+
+ ret = fd_seek( ft->fd, offset, FD_SEEK_SET );
+ ft->offset = fd_seek( ft->fd, 0, FD_SEEK_CUR ); //cul be errors?
+
+ return ret;
+}
+
+
int file_pos( file_t *ft )
{
int ret = 0;
@@ -239,7 +251,7 @@ int file_read( file_t *ft, uint8_t *buf, size_t count )
printf("Cannot read\n");
} else if ( ret < count )
{
- printf("Requestd %d readed %d\n", count, ret);
+ printf("Requestd %zu readed %d\n", count, ret);
}
return ret;
@@ -259,7 +271,7 @@ int file_write_blk( file_t *ft, uint8_t *buf )
{
sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
}
- printf(" %d %x %u \n", ft->fd, buf, sz);
+
ret = fd_write( ft->fd, buf, sz );
if ( ret < 0 )
{
@@ -283,7 +295,7 @@ int file_write( file_t *ft, uint8_t *buf, size_t count )
printf("Cannot write\n");
} else if ( ret < count )
{
- printf("Requested %d written %d\n", count, ret);
+ printf("Requested %zu written %d\n", count, ret);
}
return ret;
@@ -324,3 +336,54 @@ int file_close( file_t *ft )
return ret;
}
+
+
+uint8_t **dir_list( char *path)
+{
+ uint8_t **ret = NULL, **new_ptr;
+ int cnt=0;
+ DIR *dp;
+ struct dirent *ep;
+ int str_sz = 0;
+
+ /* lets use libc example. they know what they are doing */
+ dp = opendir( path );
+ if ( dp != NULL )
+ {
+ while ( (ep = readdir( dp )) != NULL )
+ {
+ //count one more in da list
+ cnt += 1;
+ //lets alloc pointer on pointer where we put pointer
+ new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt) );
+ if ( new_ptr == NULL )
+ goto failed_realloc;
+ ret = new_ptr;
+ str_sz = strlen(ep->d_name);
+ ret[cnt-1] = malloc( str_sz+1 );
+ memcpy( ret[cnt-1], ep->d_name, str_sz );
+ ret[cnt-1][str_sz] = 0;
+ }
+ closedir( dp );
+ dp = NULL;
+ } else
+ {
+ perror("Couldnt list directory files");
+ *ret = NULL;
+ }
+
+ //add NULL element at the end
+ new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt+1) );
+ if ( new_ptr == NULL )
+ goto failed_add_entry;
+ ret = new_ptr;
+ ret[cnt] = NULL;
+
+ return ret;
+
+failed_realloc:
+ closedir( dp );
+failed_add_entry:
+ //forgot to free ret
+ return NULL;
+} \ No newline at end of file
diff --git a/core.h b/core.h
index 02f072b..11ba289 100644
--- a/core.h
+++ b/core.h
@@ -6,14 +6,15 @@
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
-/*
+/*******************************************************************************
BASIC FILE OPERATIONS ON FILE
-*/
+ ******************************************************************************/
#define FD_RO O_RDONLY
#define FD_WO O_WRONLY
@@ -76,9 +77,9 @@ search and check for more
*/
-/*
+/*******************************************************************************
FILE OPERATION MANAGMENT STRUCTURE
-*/
+ ******************************************************************************/
#define DEFAULT_BLK_SIZE 256
@@ -101,10 +102,20 @@ int file_read_blk( file_t *ft, uint8_t *buf );
int file_read( file_t *ft, uint8_t *buf, size_t count );
int file_write_blk( file_t *ft, uint8_t *buf );
int file_write( file_t *ft, uint8_t *buf, size_t count );
-int file_seek( file_t *ft, off_t offset );
+int file_seek( file_t *ft, off_t offset ); //seek by offset
+int file_seekp( file_t *ft, off_t offset );
int file_pos( file_t *ft );
int file_size( file_t *ft );
int file_s_mode( file_t *ft, int mode );
int file_close( file_t *ft );
+/*******************************************************************************
+UTILITITIES
+ ******************************************************************************/
+
+/*
+LIST DIRECTORY FILES
+*/
+uint8_t **dir_list( char *path );
+
#endif \ No newline at end of file
diff --git a/ihe.c b/ihe.c
index d838b5c..c38380b 100644
--- a/ihe.c
+++ b/ihe.c
@@ -1,9 +1,198 @@
#include "ihe.h"
-#include "cmd.h"
#include "core.h"
static int cmd_loop = 1;
-extern cmd_table tab[];
+extern cmd_table_t tab[];
+
+#define CMK_KEYMAP1(KEY) (in_buf[0]==(KEY)&&(ret_read==1))
+
+#define CMD_IN_BUF_SIZE 32
+typedef struct cmd_in_buf_t
+{
+ char buf[CMD_IN_BUF_SIZE+2];//enought space to put "'\n',NULL"
+ int cur_sz;
+} cmd_in_buf_t;
+
+int cmd_buf_clean( cmd_in_buf_t *buf );
+/*
+type == 1, usual printable chars
+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_clean( cmd_in_buf_t *buf )
+{
+ memset( buf, 0, sizeof(cmd_in_buf_t));
+ return 0;
+}
+
+
+int cmd_buf_print( cmd_in_buf_t *buf, int type )
+{
+ int i;
+ //printf("sz:%d\n",buf->cur_sz);
+ for (i=0;(i<buf->cur_sz)&&(i<CMD_IN_BUF_SIZE);i++)
+ {
+ if (type == 1)
+ {
+ if (isprint(buf->buf[i]))
+ {
+ printf("%c",(char)(buf->buf[i]&0xff));
+ } else
+ {
+ printf(".");
+ }
+ } else if (type == 2)
+ {
+ printf("%02x ", (unsigned char)(buf->buf[i]&0xff) );
+ }
+ }
+ printf("\n");
+ return 0;
+}
+
+int term_set_std_mode( term_screen *ts )
+{
+ int ret = 0;
+
+ if ( tcgetattr( ts->ifd, &ts->orig_i ) == -1 )
+ {
+ ERROR("Cannot get input terminal attributes\n");
+ goto exit_error;
+ }
+
+ ts->raw_i = ts->orig_i; /* modify the original mode */
+ /* input modes: no break, no CR to NL, no parity check, no strip char,
+ * no start/stop output control. */
+ //ts->raw_i.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+ ts->raw_i.c_iflag = IUTF8|ICRNL;
+ /* output modes - disable post raw */
+ //ts->raw_i.c_oflag &= ~(OPOST);
+ ts->raw_i.c_oflag = OPOST|ONLCR;
+ /* control modes - set 8 bit chars */
+ //ts->raw_i.c_cflag |= (CS8);
+ ts->raw_i.c_cflag = CS8|CREAD;
+ /* local modes - choing off, canonical off, no extended functions,
+ * no signal chars (^Z,^C) */
+ //ts->raw_i.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
+ //if ICANNON ignore ESC char
+ //ts->raw_i.c_lflag = ISIG|ECHO;
+ ts->raw_i.c_lflag = ISIG;
+ /* control chars - set return condition: min number of bytes and timer.
+ * We want read to return every single byte, without timeout. */
+ ts->raw_i.c_cc[VMIN] = 1;
+ ts->raw_i.c_cc[VTIME] = 0; /* 1 byte, no timer */
+
+ /* put terminal in raw mode after flushing */
+ if (tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i) < 0)
+ {
+ ERROR("Cannot set new terminal input attribures\n");
+ goto exit_error;
+ }
+
+ return ret;
+
+exit_error:
+ errno = ENOTTY;
+ return -1;
+
+}
+
+
+int custom_term_echo( char *keybuf )
+{
+ if (isprint(keybuf[0]))
+ {
+ write(1,keybuf,1);
+ }
+ if (keybuf[0]=='\n')
+ {
+ write(1,"\r\n",2);
+ }
+ return 0;
+}
+
+
+int cmd_tab_autocomplete( char *in_buf )
+{
+ cmd_arg_t *args;
+ cmd_tok_t tl, *ptr = &tl, *iter = NULL;
+ struct cmd_acq_t *iter_sugg = NULL;
+ memset( &tl, 0, sizeof( cmd_tok_t ));
+ //int i;
+
+ //printf("[%s]\n", in_buf);
+ if ( parse_cmd( ptr, in_buf) == -1)
+ {
+ printf("Cmd problems\n");
+ return -1;
+ }
+
+ iter = ptr->next;
+ args = cmd_arg_create( iter );
+
+ /*
+ for (i=0; i<args->argc; i++)
+ {
+ printf("ARG:%d TYPE:%d %s\n", i, args->type[i], args->argv[i]);
+ }
+ */
+
+ //printf("Unkn command\n");
+ //if command not found offer auto complete options
+ if (args->argc > 0)
+ {
+ //printf("asd\n");
+ struct cmd_acq_head_t *ac = cmd_acq(tab,args->argv[0]);
+ if (ac != NULL)
+ {
+ //printf("Did you mean ");
+ SLIST_FOREACH(iter_sugg,ac,next_sugg)
+ {
+ printf("%s ", iter_sugg->suggestion);
+ }
+ printf("\n");
+ }
+ cmd_acq_free( ac );
+ }
+
+ cmd_tok_destroy( ptr->next );
+ ptr->next = NULL;
+ cmd_arg_destroy( args );
+
+ return 0;
+}
+
+
+int cmd_buf_add( cmd_in_buf_t *buf, char *ch, int size )
+{
+ int i,j;
+
+ /*
+ for (i=0;i<size;i++)
+ {
+ printf("|%c|",ch[i]);
+ } printf("\n");
+ */
+
+ i=buf->cur_sz;
+ for ( j=0;
+ j<size;
+ j+=1 )
+ {
+ if ( (i+j) < CMD_IN_BUF_SIZE )
+ {
+ //printf("%d %c",(i+j),ch[j]);
+ buf->buf[i+j] = ch[j];
+ buf->cur_sz = i+j+1;
+ //printf("%d\n", buf->cur_sz );
+ //printf("\n");
+ }
+ }
+
+ return 0;
+}
/*
GLOBAL VARIABLES
@@ -13,7 +202,7 @@ static buf_t *g_buf = NULL;
static int g_flags = FD_RW;
-int c_version(cmd_arg *arg)
+int c_version( cmd_arg_t *arg )
{
int argc = arg->argc;
@@ -23,13 +212,13 @@ int c_version(cmd_arg *arg)
return -1;
}
- printf("Version 0.0.1\n");
+ printf("Version 0.0.2\n");
return 0;
}
-int c_arg( cmd_arg *arg )
+int c_arg( cmd_arg_t *arg )
{
int i;
int argc = arg->argc;
@@ -44,14 +233,14 @@ int c_arg( cmd_arg *arg )
}
-int c_quit( cmd_arg *arg )
+int c_quit( cmd_arg_t *arg )
{
cmd_loop = 0;
return 0;
}
-int c_help( cmd_arg *arg )
+int c_help( cmd_arg_t *arg )
{
int i = 0;
printf("Command list\n");
@@ -67,7 +256,7 @@ int c_help( cmd_arg *arg )
/*
OPEN <FILENAME>
*/
-int c_open( cmd_arg *arg )
+int c_open( cmd_arg_t *arg )
{
int argc = arg->argc;
@@ -99,7 +288,7 @@ int c_open( cmd_arg *arg )
/*
CLOSE
*/
-int c_close( cmd_arg *arg )
+int c_close( cmd_arg_t *arg )
{
int fret = 0;
@@ -117,7 +306,7 @@ int c_close( cmd_arg *arg )
/*
FILE
*/
-int c_info( cmd_arg *arg )
+int c_info( cmd_arg_t *arg )
{
if ( g_file == NULL )
@@ -131,7 +320,7 @@ int c_info( cmd_arg *arg )
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: %d\n", g_file->position );
+ printf("POSITION: %ld\n", g_file->position );
printf("SIZE : %zd\n", g_file->size );
printf("BLOCK : %u\n", g_file->blk_size );
}
@@ -142,7 +331,7 @@ int c_info( cmd_arg *arg )
} else
{
printf("BUF:\n");
- printf("ADDR : %08x\n", g_buf->buf);
+ printf("ADDR : %p\n", g_buf->buf);
printf("SIZE : %d\n", g_buf->size);
printf("BUFSIZE: %d\n", g_buf->buf_size);
}
@@ -151,12 +340,13 @@ int c_info( cmd_arg *arg )
}
-int c_seek( cmd_arg *arg )
+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)
{
@@ -170,8 +360,29 @@ int c_seek( cmd_arg *arg )
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
- fret = file_seek( g_file, offset );
+
+ 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);
@@ -182,7 +393,7 @@ int c_seek( cmd_arg *arg )
}
-int c_pos( cmd_arg *arg )
+int c_pos( cmd_arg_t *arg )
{
int fret = 0;
@@ -199,7 +410,7 @@ int c_pos( cmd_arg *arg )
}
-int c_size( cmd_arg *arg )
+int c_size( cmd_arg_t *arg )
{
off_t size;
@@ -216,7 +427,7 @@ int c_size( cmd_arg *arg )
}
-int c_blk( cmd_arg *arg )
+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 );
@@ -225,7 +436,7 @@ int c_blk( cmd_arg *arg )
}
-int c_read( cmd_arg *arg )
+int c_read( cmd_arg_t *arg )
{
int ret;
@@ -246,7 +457,7 @@ int c_read( cmd_arg *arg )
}
-int c_dump( cmd_arg *arg )
+int c_dump( cmd_arg_t *arg )
{
int i;
@@ -266,7 +477,7 @@ int c_dump( cmd_arg *arg )
}
-int c_dumpx( cmd_arg *arg )
+int c_dumpx( cmd_arg_t *arg )
{
int i,j;
@@ -310,8 +521,36 @@ int c_dumpx( cmd_arg *arg )
}
+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 *arg )
+int c_write( cmd_arg_t *arg)
{
/*
anonymous function
@@ -368,7 +607,7 @@ int c_write( cmd_arg *arg )
for (i=0; i<strlen(argv[0]); i+=2)
{
- printf("%02x ",hex2u8(&argv[0][i]));
+ printf("%02x ",(unsigned char)hex2u8((unsigned char*)&argv[0][i]));
}
printf("\n");
@@ -376,7 +615,7 @@ int c_write( cmd_arg *arg )
buf = malloc(strlen(argv[0])/2);
for (i=0; i<(strlen(argv[0])/2); i++)
{
- buf[i] = hex2u8(&argv[0][i*2]);
+ buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
}
memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
@@ -394,7 +633,7 @@ int c_write( cmd_arg *arg )
//white spaces should be supported
-int c_writes( cmd_arg *arg )
+int c_writes( cmd_arg_t *arg )
{
int argc = arg->argc;
char **argv = arg->argv;
@@ -423,7 +662,7 @@ int c_writes( cmd_arg *arg )
}
} else
{
- printf("Input bigger then buffer buf %d input %d\n", g_buf->size, strlen(argv[0]));
+ printf("Input bigger then buffer buf %d input %zu\n", g_buf->size, strlen(argv[0]));
return -1;
}
@@ -431,7 +670,7 @@ int c_writes( cmd_arg *arg )
}
-int c_flags( cmd_arg *arg )
+int c_flags( cmd_arg_t *arg )
{
int argc = arg->argc;
char **argv = arg->argv;
@@ -467,7 +706,7 @@ int c_flags( cmd_arg *arg )
}
-int c_manifesto( cmd_arg *arg )
+int c_manifesto( cmd_arg_t *arg )
{
printf("""\
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\
@@ -510,29 +749,169 @@ int c_manifesto( cmd_arg *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 )
+{
+ /*
+ int argc = arg->argc;
+ int th = -1;
+
+ if ( argc > 0 )
+ {
+ printf("No arguments needed\n");
+ return -1;
+ }
+
+ th = term_cur_get_r();
+ if (th == -1)
+ {
+ printf("Cannot get current term height\n");
+ return -1;
+ }
+ printf("%d\n",th);
+ */
+
+ return 0;
+}
+
+
+int c_tw( cmd_arg_t *arg )
+{
+ /*
+ int argc = arg->argc;
+ int tw = -1;
+
+ if ( argc > 0 )
+ {
+ printf("No arguments needed\n");
+ return -1;
+ }
+
+ tw = term_cur_get_c();
+ if (tw == -1)
+ {
+ printf("Cannot get current term width\n");
+ return -1;
+ }
+ printf("%d\n",tw);
+ */
+
+ return 0;
+}
/*
CMD COMMANDS
*/
-cmd_table tab[] = {
- {"version", c_version },
- {"arg", c_arg },
- {"quit", c_quit},
- {"help", c_help},
- {"?", c_help},
- {"open", c_open},
- {"close", c_close},
- {"info", c_info},
- {"seek", c_seek},
- {"pos", c_pos},
- {"size", c_size},
- {"blk", c_blk},
- {"read", c_read},
- {"dump", c_dump},
- {"dumpx", c_dumpx},
- {"write", c_write},
- {"writes", c_writes},
- {"flags", c_flags},
+cmd_table_t tab[] = {
+ /*
+ {"version", c_version },
+ {"arg", c_arg },
+ {"quit", c_quit},
+ {"help", c_help},
+ {"?", c_help},
+ {"open", c_open},
+ {"close", c_close},
+ {"info", c_info},
+ {"seek", c_seek},
+ {"pos", c_pos},
+ {"size", c_size},
+ {"blk", c_blk},
+ {"read", c_read},
+ {"dump", c_dump},
+ {"dumpx", c_dumpx},
+ {"dumps", c_dumps},
+ {"write", c_write},
+ {"writes", c_writes},
+ {"flags", c_flags},
{"manifesto", c_manifesto},
+ {"ls", c_ls},
+ {"pwd", c_pwd},
+ {"cd", c_cd},*/
+ //{"th", c_th},
+ //{"tw", c_tw},
{NULL, NULL }
};
@@ -540,9 +919,11 @@ cmd_table tab[] = {
int main( int argc, char **argv )
{
- char *cmd = NULL;
- cmd_arg *tok = NULL;
-
+ const int sz_buf = 1024;
+ uint8_t in_buf[sz_buf];
+ int ret_read;
+ int new_c=0, new_r=0, old_r=0, old_c=0;
+
//preapre global stuff
g_file = file_init();
@@ -550,20 +931,81 @@ int main( int argc, char **argv )
g_buf = buf_init();
buf_size( g_buf, DEFAULT_BLK_SIZE );
- //read line from cmd
- while( cmd_loop )
+
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ cmd_in_buf_t cmd_in;
+ cmd_buf_clean( &cmd_in );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ //term_set_raw_mode( &ts );
+ term_set_std_mode( &ts );
+
+ term_clr_scr( &ts );
+
+ new_c = term_get_maxcol( &ts );
+ new_r = term_get_maxrow( &ts );
+ old_r = new_r;
+ old_c = new_c;
+
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, old_r);
+
+ while ( 1 == 1 )
{
- cmd = cmd_line(NULL);
- //printf("cmd [%s]\n", cmd);
- //printf("cnt %d\n",cnt_sep(cmd));
+
+ ret_read = read(2, in_buf, sz_buf );
+ if (ret_read < 0)
+ {
+ printf("Cannot read\n");
+ continue;
+ }
+ in_buf[ret_read] = 0;
+
+ //custom echoing service to ignore some special chars like TAB
+ custom_term_echo( (char *)in_buf );
- tok = cmd_parse( cmd );
- free( cmd );
+ //printf("(%d)%s\n",ret_read,in_buf);
+ if ( (isprint(in_buf[0])) && (in_buf[0]!='?') && (in_buf[0]!='\t'))
+ {
+ //printf("asd %d\n", ret_read);
+ cmd_buf_add( &cmd_in, (char *)in_buf, ret_read );
+ }
+
+ //auto complete advice
+ if (CMK_KEYMAP1(9))
+ {
+ //cmd_buf_print( &cmd_in, 2 );
+ cmd_in.buf[cmd_in.cur_sz] = '\n';
+ cmd_in.buf[cmd_in.cur_sz+1] = 0;
+ cmd_tab_autocomplete( cmd_in.buf );
+ cmd_buf_print( &cmd_in, 1 );
+ continue;
+ }
+
+ if (CMK_KEYMAP1('?'))
+ {
+ cmd_buf_print( &cmd_in, 1 );
+ }
- cmd_exec( tok, tab );
+ if (CMK_KEYMAP1(27))
+ {
+ cmd_buf_print( &cmd_in, 1 );
+ break;
+ }
- cmd_arg_free( tok );
+ if (CMK_KEYMAP1('\n'))
+ {
+ cmd_buf_clean( &cmd_in );
+ }
+
}
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
return 0;
} \ No newline at end of file
diff --git a/ihe.h b/ihe.h
index 9589236..5331e55 100644
--- a/ihe.h
+++ b/ihe.h
@@ -1,12 +1,18 @@
#ifndef __IHE_H
#define __IHE_H
+#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "buf.h"
-#include "cmd.h"
#include "core.h"
+#include "libcmd/cmd.h"
+#include "libcmd/cmd_parse.h"
+
+#include "libterm/term.h"
+#include "libterm/term_io.h"
+
#endif \ No newline at end of file
diff --git a/libcmd/Makefile b/libcmd/Makefile
new file mode 100644
index 0000000..86f0ae6
--- /dev/null
+++ b/