diff options
Diffstat (limited to 'ihe.c')
-rw-r--r-- | ihe.c | 558 |
1 files changed, 500 insertions, 58 deletions
@@ -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 |