From cb141140579d796ae5cafe6da52b4f0b87be6a84 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Mon, 23 May 2016 21:51:44 +0100 Subject: Simplifying logic. Removing buffer features --- cmd/cmd_blk.c | 20 ++- cmd/cmd_info.c | 11 -- core.c | 19 ++- core.h | 1 + ihe.c | 21 +-- libterm/Makefile | 16 +++ libterm/screen_modes.c | 3 + libterm/term.c | 361 +++++++++++++++++++++++++++++++++++++++++++++++++ libterm/term_gui.c | 31 +++++ libterm/term_io.c | 336 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 793 insertions(+), 26 deletions(-) create mode 100644 libterm/Makefile create mode 100644 libterm/screen_modes.c create mode 100644 libterm/term.c create mode 100644 libterm/term_gui.c create mode 100644 libterm/term_io.c diff --git a/cmd/cmd_blk.c b/cmd/cmd_blk.c index db64f42..e497780 100644 --- a/cmd/cmd_blk.c +++ b/cmd/cmd_blk.c @@ -8,10 +8,26 @@ extern file_t *g_file; extern buf_t *g_buf; extern int g_flags; +/* + * blk - show block size + * blk - set block size + */ 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 ); + int argc = arg->argc; + char **argv = arg->argv; + int *type = arg->type; + if ( argc == 0) + { + printf("FILE BLOCK SIZE %u\n", g_file->blk_size ); + } else if ( argc == 1 ) + { + if ((type[0] == CMDT_INT) || (type[0] == CMDT_HEX)) + g_file->blk_size = atoi( argv[0] ); + } else + { + return -1; + } return 0; } \ No newline at end of file diff --git a/cmd/cmd_info.c b/cmd/cmd_info.c index b06f8bc..8744efe 100644 --- a/cmd/cmd_info.c +++ b/cmd/cmd_info.c @@ -30,16 +30,5 @@ int c_info( cmd_arg_t *arg ) 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/core.c b/core.c index a152656..90f8fd9 100644 --- a/core.c +++ b/core.c @@ -121,7 +121,11 @@ int file_open_fn( file_t *ft, const char *filename, int flags ) ft->fd = fd; ft->flags = flags; - ft->filename = filename; + + uint8_t fn_sz = strlen( filename ); + char *fn = malloc( fn_sz ); + memcpy( fn, filename, fn_sz ); + ft->filename = fn; /* set current file cursor postion and file size */ @@ -314,6 +318,15 @@ int file_s_mode( file_t *ft, int mode ) return ret; } +int file_s_bufs( file_t *ft, unsigned int size ) +{ + int ret = 0; + + ft->blk_size = size; + + return ret; +} + int file_close( file_t *ft ) { @@ -322,13 +335,13 @@ int file_close( file_t *ft ) if ( ft->fd < 1 ) { printf("File descriptor <1\n"); - ret = -1; + return -1; } if ( ft->filename == NULL ) { printf("File name is empty\n"); - ret = -1; + return -1; } fd_close( ft->fd ); diff --git a/core.h b/core.h index 11ba289..da8c911 100644 --- a/core.h +++ b/core.h @@ -106,6 +106,7 @@ 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_bufs( file_t *ft, unsigned int size ); int file_s_mode( file_t *ft, int mode ); int file_close( file_t *ft ); diff --git a/ihe.c b/ihe.c index f24dc77..a2769f5 100644 --- a/ihe.c +++ b/ihe.c @@ -270,6 +270,7 @@ int c_arg( cmd_arg_t *arg ) int c_quit( cmd_arg_t *arg ) { cmd_loop = 0; + exit(0); return 0; } @@ -385,22 +386,22 @@ cmd_table_t tab[] = { {"version", c_version }, {"arg", c_arg }, - {"quit", c_quit}, + //{"quit", c_quit}, {"help", c_help}, {"?", c_help}, {"open", c_open}, {"close", c_close}, {"info", c_info}, - {"seek", c_seek}, - {"pos", c_pos}, + //{"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}, + //{"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}, @@ -454,7 +455,7 @@ int main( int argc, char **argv ) term_cur_set_r( &ts, old_r); write(1,"$",1); - while ( 1 == 1 ) + while ( cmd_loop ) { //write(1,"$",1); ret_read = read(2, in_buf, sz_buf ); diff --git a/libterm/Makefile b/libterm/Makefile new file mode 100644 index 0000000..759b3f5 --- /dev/null +++ b/libterm/Makefile @@ -0,0 +1,16 @@ +PROJECT=libterm +CC=gcc +LD=ld +CFLAGS=-g3 + +make: + $(CC) $(CFLAGS) -c term.c + $(CC) $(CFLAGS) -c screen_modes.c + $(CC) $(CFLAGS) -c term_io.c + $(CC) $(CFLAGS) -c term_gui.c + $(LD) -r term.o screen_modes.o term_io.o term_gui.o -o $(PROJECT).o + +clean: + rm *.o + + diff --git a/libterm/screen_modes.c b/libterm/screen_modes.c new file mode 100644 index 0000000..709b332 --- /dev/null +++ b/libterm/screen_modes.c @@ -0,0 +1,3 @@ +#include "screen_modes.h" + + diff --git a/libterm/term.c b/libterm/term.c new file mode 100644 index 0000000..81d967e --- /dev/null +++ b/libterm/term.c @@ -0,0 +1,361 @@ +#include "term.h" + +#include "debug.h" + +#define T_ESC "\x1b" + +//setup initial terminal stuff +//this coulc act differently, becouse allways there is +//different terminal setting that should be default +int term_init( term_screen *term ) +{ + int ret=0; + + memset( term, 0, sizeof( term_screen )); + + if ( !isatty(STDIN_FILENO) ) + { + ERROR("isatty failed\n") + goto exit_error; + } + //!!!!!! + //register atexit + + term->ifd = STDIN_FILENO; + term->ofd = STDOUT_FILENO; + + //if you whant raw mode then you should set it man + if ( tcgetattr( term->ifd, &term->orig_i ) == -1 ) goto exit_error; + term->raw_i = term->orig_i; + if ( tcgetattr( term->ofd, &term->orig_o ) == -1 ) goto exit_error; + term->raw_o = term->orig_o; + + term->mode = SCREEN_MODE_80x25; + + return ret; + +exit_error: + errno = ENOTTY; + return -1; +} + +//set terminal speed return 0 if OK and 1 if not +//im trust to input arguments that they are ok +int term_set_speed( term_screen *ts, speed_t speed) +{ + int ret = cfsetospeed( &ts->raw_o, speed ); + ret = tcsetattr( ts->ofd, TCSANOW, &ts->raw_o ); + if ( ret != 0 ) + return 1; + + //if baudrate set to there then input speed same as + //output speed + cfsetispeed( &ts->raw_i, B0); + ret = tcsetattr( ts->ifd, TCSANOW, &ts->raw_i ); + if ( ret != 0 ) + return 1; + return 0; +} + +//get maximal number of columns setting up cursor to 999 column +//and getting on with place terminal have placed cursor and getting +//column on with terminal putted cursor +int term_get_maxcol( term_screen *ts ) +{ + int ret=-1; + int orig_c; + int cur_c; + + /* get initial cursor position */ + if ( (orig_c = term_cur_get_c( ts )) == -1 ) + goto exit_error; + + + /* go to right marging and get position */ + if ( write( ts->ofd, T_ESC "[999C", 6 ) != 6 ) + goto exit_error; + if ( (cur_c = term_cur_get_c( ts )) == -1 ) + goto exit_error; + ret = cur_c; + + /* restore position */ + { + char buf[32]; + snprintf( buf, 32, T_ESC "[" "%d" "D", cur_c-orig_c); + write( ts->ofd, buf, strlen(buf) ); + } + + return ret; + +exit_error: + return -1; + +} + +//try to setup far away line after that read position where +//terminal have putted cursor and read line of that postion +int term_get_maxrow( term_screen *ts ) +{ + int ret=-1; + int orig_r; + int cur_r; + + /* get initial cursor position */ + if ( (orig_r = term_cur_get_r( ts )) == -1 ) + goto exit_error; + + + /* go to right marging and get position */ + if ( write( ts->ofd, T_ESC "[999B", 6 ) != 6 ) + goto exit_error; + if ( (cur_r = term_cur_get_r( ts )) == -1 ) + goto exit_error; + ret = cur_r; + + /* restore position */ + { + char buf[32]; + snprintf( buf, 32, T_ESC "[" "%d" "A", cur_r-orig_r); + write( ts->ofd, buf, strlen(buf) ); + } + + return ret; + +exit_error: + return -1; +} + + + +int term_cur_get_c( term_screen *ts ) +{ + unsigned int i; + int row, col; + char buf[32]; + + if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error; + + i = 0; + while (i < sizeof(buf)-1) + { + if ( read( ts->ifd, buf+i,1 ) != 1 ) break; + if (buf[i] == 'R') break; + i++; + } + buf[i] = '\0'; + + /* Parse terminal response */ + if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error; + if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error; + + return col; +exit_error: + return -1; +} + + +int term_cur_get_r( term_screen *ts ) +{ + unsigned int i; + int row, col; + char buf[32]; + + if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error; + + i = 0; + while (i < sizeof(buf)-1) + { + if ( read( ts->ifd, buf+i,1 ) != 1 ) break; + if (buf[i] == 'R') break; + i++; + } + buf[i] = '\0'; + + /* Parse terminal response */ + if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error; + if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error; + + return row; +exit_error: + return -1; +} + +//set cursor column position +int term_cur_set_c( term_screen *ts, unsigned int pc ) +{ + int ret = 0; + int cur_r; + + /* go to right marging and get position */ + if ( (cur_r = term_cur_get_r( ts )) == -1 ) + goto exit_error; + /* set position */ + { + char buf[32]; + int l; + + snprintf( buf, 32, T_ESC "[%d;%dH", cur_r, pc); + l = strlen( buf ); + if ( write( ts->ofd, buf, l ) != l) + goto exit_error; + } + + return ret; + +exit_error: + return -1; +} + +//set cursor row/line position +int term_cur_set_r( term_screen *ts, unsigned int pr ) +{ + int ret = 0; + int cur_c; + + /* go to right marging and get position */ + if ( (cur_c = term_cur_get_c( ts )) == -1 ) + goto exit_error; + /* set position */ + { + char buf[32]; + int l; + + snprintf( buf, 32, T_ESC "[%d;%dH", pr, cur_c); + l = strlen( buf ); + if ( write( ts->ofd, buf, l ) != l) + goto exit_error; + } + + return ret; + +exit_error: + return -1; +} + +int term_cur_set_cr( term_screen *ts, unsigned int pc, unsigned int pr ) +{ + int ret = 0; + + /* set position */ + { + char buf[32]; + int l; + + snprintf( buf, 32, T_ESC "[%d;%dH", pr, pc); + l = strlen( buf ); + if ( write( ts->ofd, buf, l ) != l) + goto exit_error; + } + return ret; + +exit_error: + return -1; + +} + + +//clean terminal with escape command +int term_clr_scr( term_screen *ts ) +{ + int ret = 0; + + if ( write( ts->ofd, T_ESC "[H" T_ESC "[2J", 7 ) <= 0 ){}; + + return ret; +} + +//set terminal default input/output behavior +int term_set_raw_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); + /* output modes - disable post raw */ + ts->raw_i.c_oflag &= ~(OPOST); + /* control modes - set 8 bit chars */ + ts->raw_i.c_cflag |= (CS8); + /* local modes - choing off, canonical off, no extended functions, + * no signal chars (^Z,^C) */ + ts->raw_i.c_lflag &= ~(ECHO | ICANON | IEXTEN | 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; + +} + + +//if there is no mode with some rows/columns , then just show that no mode setet +// up and user should decide by his own what to do +int term_mode_rows( term_screen *ts ) +{ + int ret = -1; + + if ( ts == NULL) return -1; + + switch ( ts->mode ) + { + //--------------------- + case SCREEN_MODE_80x25: + ret = 25; + break; + //-------------------- + case SCREEN_MODE_NONE: + default: + ret = -1; + } + + return ret; +} + + +//if there is no mode with some rows/columns , then just show that no mode setet +// up and user should decide by his own what to do +int term_mode_columns( term_screen *ts ) +{ + int ret = -1; + + if ( ts == NULL) return -1; + + switch ( ts->mode ) + { + //--------------------- + case SCREEN_MODE_80x25: + ret = 80; + break; + //-------------------- + case SCREEN_MODE_NONE: + default: + ret = -1; + } + + return ret; +} + + +void term_set_orig_mode( term_screen *ts ) +{ + tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig_i ); +} \ No newline at end of file diff --git a/libterm/term_gui.c b/libterm/term_gui.c new file mode 100644 index 0000000..dcdc840 --- /dev/null +++ b/libterm/term_gui.c @@ -0,0 +1,31 @@ +#include "term_gui.h" + +int term_gui_init( term_gui *tg, term_screen *ts ) +{ + return 0; +} + + +int term_set_wh( term_screen *tg, int width, int height ) +{ + return 0; +} + + +int term_gui_input_box( term_gui *ts, int x, int y, int w, int h, char *prompt, + char *str, size_t sz ) +{ + return 0; +} + + +int term_gui_draw( term_gui *tg ) +{ + return 0; +} + + +int term_gui_destroy( term_gui *tg ) +{ + return 0; +} \ No newline at end of file diff --git a/libterm/term_io.c b/libterm/term_io.c new file mode 100644 index 0000000..a1f157c --- /dev/null +++ b/libterm/term_io.c @@ -0,0 +1,336 @@ +#include "term_io.h" + +int term_fprint( screen_mode_e mode, FILE *f ) +{ + int ret=-1; + if (f == NULL) + return -1; + + ERROR("\n"); + switch ( mode ) + { + case SCREEN_MODE_80x25: + { + } + break; + default: + printf("Unknown screen mode\n"); + } + + return ret; +} + +int term_print( term_screen *ts, const char *s, size_t n) +{ + int ret=0; + + if ( ts == NULL ) + return -1; + + if ( s == NULL ) + return -1; + + if ( n < 1 ) + return -1; + + if ( n > 80*25 ) + return -1; + + ret = write( ts->ofd, s, n ); + + return ret; +} + +//print data to terminal starting from x,y +int term_print_xy( term_screen *ts, const char *buf, size_t size, + int init_column, int init_row ) +{ + int ret=-1; + if ( buf == NULL ) + { + return -1; + } + + if ( size <= 0 ) + { + return -1; + } + + switch ( ts->mode ) + { + case SCREEN_MODE_80x25: + { + + } + break; + default: + printf("Unknown mode\n"); + } + + return ret; +} + +int term_draw_hline( term_screen *ts, int pc, int pr, const int sz, char ch) +{ + int ret=0; + char buf[sz]; memset( buf, ch, sz ); + + if ( sz > 0) + { + term_cur_set_c( ts, pc ); + term_cur_set_r( ts, pr ); + write( ts->ofd, buf, sz ); + } else + { + ret = -1; + } + + return ret; +} + +//read one character from stream +int term_getc( term_screen *ts ) +{ + int ret=-1; + int fret=-1; + char buf; + + fret = read( ts->ifd, &buf, 1 ); + if ( fret == 1 ) + { + ret = buf; + } + return ret; +} + +int64_t term_getb( term_screen *ts ) +{ + int64_t ret = -1; + int fret = -1; + unsigned char buf[8]; + + fret = read( ts->ifd, buf, 8 ); + if ( fret == 1 ) + { + //printf("1\n"); + + ret = buf[0]; + } else if ( fret == 2 ) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t r = 0x0; + + r = (a1+((a2<<8)&0xff)); + + ret = r; + + } else if ( fret == 3 ) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t a3 = buf[2]&0xff; + uint32_t r=0x0; + + //printf("3\n"); + + r = (a1+((a2<<8)&0xFF00)+((a3<<16)&0xFF0000)); + + ret = r; + } else if ( fret == 4) + { + uint32_t a1 = buf[0]&0xff; + uint32_t a2 = buf[1]&0xff; + uint32_t a3 = buf[2]&0xff; + uint32_t a4 = buf[3]&0xff; + uint32_t r=0x0; + + //printf("4\n"); + + r = (a1+((a2<<8)&0xFF00)+((a3<<16)&0xFF0000)+((a4<<24)&(0xFF000000))); + + ret = r; + } else if ( fret == 5 ) + { + uint64_t a1 = buf[0]&0xff; + uint64_t a2 = buf[1]&0xff; + uint64_t a3 = buf[2]&0xff; + uint64_t a4 = buf[3]&0xff; + uint64_t a5 = buf[4]&0xff; + uint64_t r=0x0; + + //printf("5 %02x %02x %02x %02x %02x\n",a1,a2,a3,a4,a5); + + r = (a1+ + ((a2<<8)&0xFF00)+ + ((a3<<16)&0xFF0000)+ + ((a4<<24)&0xFF000000)+ + ((a5<<32)&0xFF00000000)); + + ret = r; + } else + { + int i; + for (i=0;iofd, &c, 1 ); + if ( fret != 1 ) + { + ret = -1; + } + + return ret; +} + +//return amoutn fo characters readed +//on error or if nothing is imputed 0 returned +int term_readline( term_screen *ts, char *str, size_t str_size, int flag ) +{ + int ret = 0; + int max_row, max_column; + char *buf = NULL; + int buf_size = str_size-1; + int buf_curent = 0; + int menu_input=0; + char menu_cmd = 0; + int orig_row; + int orig_col; + + buf = malloc( buf_size ); + memset( buf, 0, buf_size ); + + max_row = term_get_maxrow( ts ); + max_column = term_get_maxcol( ts ); + orig_row = term_cur_get_r( ts ); + orig_col = term_cur_get_c( ts ); + + menu_cmd = 0; + buf_curent = 0; + //finsih when escape button is setted + while ( menu_cmd != TK_ESC ) + { + menu_input = term_getc( ts ); + if ( menu_input != -1 ) + { + menu_cmd = (char)menu_input; + //add to buffer any printable alpahnumeric char + if ( isalpha( menu_cmd ) && + (flag == READLINE_TEXT || flag == READLINE_ALPHA + || flag == READLINE_HIDDEN) ) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //add to buffer number 0-9 + } else if ( isdigit( menu_cmd ) && + ( flag == READLINE_TEXT || flag == READLINE_NUMBER + || flag == READLINE_HIDDEN )) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //if space allowed + } else if ( (menu_cmd == ' ') && (flag == READLINE_TEXT) ) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //remaining printable chars for READLINE_TEXT + } else if ( ispunct( menu_cmd ) && + (flag == READLINE_TEXT )) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //deleteone char from buffer with backspace + } else if ( menu_cmd == TK_BACKSPACE ) + { + if ( buf_curent > 0) + { + buf[ buf_curent ] = 0x0; + buf_curent -= 1; + } + //input ready lets finish input + } else if ( menu_cmd == TK_ENTER ) + { + ret = buf_curent; + memcpy( str, buf, buf_size ); + str[ str_size-1 ] = 0x0; + break; + //finsih input without saving result + } else if ( menu_cmd == TK_ESC ) + { + ret = -1; + } + + { + int i; + term_cur_set_r( ts, orig_row ); + term_cur_set_c( ts, orig_col ); + for (i=0;iofd, format, args); + + va_end(args); + + return ret; +} -- cgit v1.2.3