author | Jesus Romero Hebrero <hebrerillo@hotmail.com> | 2012年02月02日 10:12:10 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年02月02日 21:18:02 +0100 |
commit | fffd371ed410eb17799cf0d0aea3d2cd4bbe8fde (patch) | |
tree | 58eaf72fd1a6f34f831458ca5f9a319afb67d629 | |
parent | 1cd74c56726fb1ebbc7a9360cd4d72ad06b8f338 (diff) | |
download | gsl-shell-editor.tar.gz |
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | completion.c | 16 | ||||
-rwxr-xr-x | edit_functions.c | 392 | ||||
-rwxr-xr-x | edit_functions.h | 101 | ||||
-rwxr-xr-x | gsl_edit.c | 586 |
@@ -45,7 +45,7 @@ endif SUBDIRS = $(LUADIR) -C_SRC_FILES = str.c gs-types.c lua-utils.c sf.c lua-graph.c lua-gsl.c +C_SRC_FILES = str.c gs-types.c lua-utils.c sf.c lua-graph.c lua-gsl.c gsl_edit.c edit_functions.c ifeq ($(strip $(USE_READLINE)),yes) C_SRC_FILES += completion.c diff --git a/completion.c b/completion.c index 3a617728..35381b07 100644 --- a/completion.c +++ b/completion.c @@ -5,16 +5,20 @@ #include <stdlib.h> #include <readline/readline.h> #include <readline/history.h> -#include <lua.h> - +#include <sys/wait.h> +#include <unistd.h> #include "completion.h" #include "gsl-shell.h" +#include "edit_functions.h" +//lua_State *globalL = NULL; static char *my_generator (const char *text, int state); +void bind_keys(); //will bind the keys to their appropiate functions +int launch_editor(); //will launch the editor void initialize_readline() { - + bind_keys(); rl_completion_entry_function = my_generator; rl_basic_word_break_characters = " \t\n\"'~><=*+-/;,|[{("; } @@ -136,3 +140,9 @@ char *my_generator (const char *text, int state) lua_pop (L, 1); return NULL; } + +void bind_keys() +{ + rl_bind_key(0x05,launch_editor); // ctrl+e will launch the editor +} + diff --git a/edit_functions.c b/edit_functions.c new file mode 100755 index 00000000..7fd79a63 --- /dev/null +++ b/edit_functions.c @@ -0,0 +1,392 @@ +#include "edit_functions.h" + +/** +* Get the actual position of the character in the pos position on the screen +* +* @param ln +* @param rpos +* @return +*/ +int getActualPos(line *ln,int rpos) +{ + int pos_screen=0,pos_real=0; + character *caux=ln->first_ch; + while(caux) + { + if(caux->c == '\t') + pos_screen+=SPACES_TAB-1; + pos_screen++; + if(pos_screen > rpos) + return pos_real; + caux=caux->next; + pos_real++; + } + return --pos_real; +} + +/** +* Get the actual position of the character in the pos position on the screen +* +* @param ln +* @param rpos +* @return +*/ +int getScreenPos(line *ln, int rpos) +{ + int cont=0; + character *aux=ln->first_ch; + while(aux && cont<rpos) + { + if(aux->c=='\t') + cont+=SPACES_TAB-1; + cont++; + aux=aux->next; + + } + if(cont>rpos) + cont-=SPACES_TAB; + if(!aux) + cont--; + return cont; + +} + +/** +* Get the number of tabs of the line ln, starting in position pos +* +* @param ln +* @param pos +* @return +*/ +int getNumTabs(line *ln, int pos) +{ + character *aux=ln->first_ch; + int cont=0,num_tbs=0; + while( aux && cont < pos ) + { + aux=aux->next; + cont++; + } + + while(aux) + { + + if(aux->c == '\t') + { + num_tbs++; + } + aux=aux->next; + } + return num_tbs; +} + + +/** +* Get the position of the character ch in the line ln +* +* @param ln +* @param ch +* @return +*/ +int getPosCh(line *ln, character *ch) +{ + character *aux=ln->first_ch; + int cont=0; + while(aux && aux!=ch) + { + cont++; + aux=aux->next; + } + if(!aux) + { + printw("ERROR ON getPosCH\n"); + return -1; + } + return cont; + +} + +/** +* Print SPACES_TAB blank spaces +*/ +void print_tab() +{ + int i; + for(i=0;i<SPACES_TAB;i++) + printw(" "); +} + +void empty_line(int nline,int size_line) +{ + int i; + move(nline,0); + for(i=size_line-1;i>=0;i--) + delch(); +} + +/** +* Print the all the options at the bottom of the screen +*/ +void print_options() +{ + move(LINES-1,0); + printw("^F:EXIT ^X:EXECUTE ^H:SAVE ^L:LOAD ^W:CLEAR"); +} + +/** +* Free all the memory allocated to data structures of the editor +* +*/ +void freeEditor() +{ + line *aux_l=first_line,*aux_l2; + character *aux_c=aux_l->first_ch,*aux_c2; + while(aux_l) + { + aux_l2=aux_l->next; + while(aux_c) + { + aux_c2=aux_c->next; + freeCharacter(&aux_c); + aux_c=aux_c2; + } + freeLine(&aux_l); + aux_l=aux_l2; + } + clear(); + move(0,0); + refresh(); +} + + +/** + * Initialize the data structures of the editor + * + */ +void init_editor() +{ + modified=0;//if the user has pressed any key + ncurrent_line=0;//number of the current line + num_lines=1; //number of lines + actual_rows=LINES-2; + actual_cols=COLS; + num_lines_scrolled=0; + init_line(&first_line); + init_character(&eof); + eof->c='0円';//EOF + init_character(&prev_character); + current_line=first_line; + current_line->num_line=1; + current_character=eof; + prev_character=eof; + current_line->first_ch=current_character; + current_line->size=1; + print_options(); + move(0,0); +} + +/** + * Returns the character in the pos position in the ln line + * + * @param ln + * @param pos + * @return + */ +character* getChLine(line *ln,int pos) +{ + character *ret=ln->first_ch; + int n=0; + + while(n<pos && ret) + { + ret=ret->next; + n++; + } + + return ret; + +} + +/** + * Free the memory allocated to character c + * @param c + */ + +void freeCharacter(character **c) +{ + free(*c); + *c=NULL; +} + + +/** + * Free the memory allocated to line l + * @param l + */ +void freeLine(line **l) +{ + free(*l); + *l=NULL; +} + + +/** + * Print the text written in the editor, from the line ln, in the Window wn + * to the end of the text + * @param ln + * @param wn + */ + +void print_ln(const line *ln, WINDOW *wn) +{ + character *aux=ln->first_ch; + while(aux) + { + if(aux->c != '\n' && aux->c !='0円' && aux->c!='\t') + wprintw(wn,"%c",aux->c); + else if(aux->c == '\t') + print_tab(); + aux=aux->next; + } +} + + +/** + * Allocates memory to the character ch + * @param ch + */ +void init_character(character **ch) +{ + *ch=malloc(sizeof(struct character)); + (*ch)->next=NULL; + (*ch)->prev=NULL; +} + +/** + * Allocates memory to the line l + * @param l + */ +void init_line(line **l) +{ + *l=malloc(sizeof(struct line)); + (*l)->next=NULL; + (*l)->prev=NULL; + (*l)->first_ch=NULL; + (*l)->size=0; + (*l)->num_tabs=0; +} + +/** + * Print a subset of the written text in the editor, taking into account the + * number of lines scrolled and the number of rows of the screen + * @param ln_scr number of lines scrolled + * @param num_rows number of rows of the screen + */ +void print_all(int ln_scr, int num_rows) +{ + int n=0; + line *aux=first_line; + while(n<ln_scr && aux) + { + aux=aux->next; + n++; + } + clear(); + n=0; + while(aux && n<num_rows) + { + move(n,0); + print_ln(aux,stdscr); + aux=aux->next; + n++; + } + print_options(); + refresh(); +} + + + +/** +* Loads the file file +* @param file The descriptor of the file +*/ + +void loadFile(int file) +{ + char buff[1]; + line *prev_line=NULL,*curr_line=first_line; + character *curr_c=NULL,*prev_character=NULL; + curr_line->size=0;int cont=0; + while(read(file,buff,1)) + { + if(buff[0] == '\t') + curr_line->num_tabs++; + init_character(&curr_c); + curr_c->c=buff[0]; + if(prev_character) + prev_character->next=curr_c; + curr_c->prev=prev_character; + if(curr_line->size == 0) + curr_line->first_ch=curr_c; + curr_line->size++; + prev_character=curr_c; + if(buff[0] == '\n') + { + num_lines++; + curr_c->next=NULL; + prev_character=NULL; + prev_line=curr_line; + init_line(&curr_line); + prev_line->next=curr_line; + curr_line->prev=prev_line; + curr_line->size=0; + curr_line->num_line=num_lines; + curr_line->first_ch=eof; + } + + //printw("c[%d]=%c;\n",cont,buff[0]); + cont++; + } + //the last character is eof + if(curr_line->first_ch != eof) + { + curr_c->next=eof; + eof->prev=curr_c; + } + curr_line->size++;//the eof character + current_character=first_line->first_ch; + current_line=first_line; +} + + +/** + * Save the contents of the editor to the file file, + * @param file The descriptor of the file + * @return + */ +int saveToFile(int file) +{ + int i,last=0; + character *caux; + char *buffer; + line *aux=first_line; + do + { + buffer=malloc(aux->size); + caux=aux->first_ch; + for(i=0;i<aux->size && caux;i++) + { + if(caux -> c != '0円') + { + buffer[i]=caux->c; + } + else + last=1; + caux=caux->next; + } + write(file,buffer, last==0 ? aux->size: aux->size-1); + aux=aux->next; + } + while(aux); + return 1; +} diff --git a/edit_functions.h b/edit_functions.h new file mode 100755 index 00000000..23d14b27 --- /dev/null +++ b/edit_functions.h @@ -0,0 +1,101 @@ +#ifndef EDIT_FUNCTIONS_H +#define EDIT_FUNCTIONS_H +#include <ncurses.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include "lua.h" +#include "lauxlib.h" + +#define DEBUG 1 + +#define SIGNAL_EXECUTE SIGUSR1 //the signal to execute the process +#define SPACES_TAB 3 //number of blank spaces associated with a TAB +#define default_file_name ".execut.lua" //default name of the to be executed +#define MAX_FILE_LENGHT 30 //the maximum number of characters of the file name +#define SAVE 0x08 //CTRL+H Save the file +#define CLEAR_EDITOR 0X17 //CTRL+W Clear the editor +#define EXECUTE 0x18 //CTRL+X Execute the file and exit the editor +#define LOAD_FILE 0x0C //CTRL+L load a file +#define EXIT_EDITOR 0x06 //CTRL+F exit editor + +#define STATUS_EXECUTE 2 //if the editor process write this to the shared memory, the script will be executed + +struct line +{ + int num_line;//number of line of this line + int size;//the number of characters of this line + int num_tabs;//number of tabs of this line + struct line *prev,*next; //previous and next line + struct character *first_ch; // the first character in the line +}; + +struct character +{ + char c;//the character itself + struct character *prev,*next;//previous and next character +}; + + +typedef struct character character; +typedef struct line line; + + +//the actual rows and cols, taking into account the bottom bar +extern int actual_rows; +extern int actual_cols; +extern int errno; + +extern line *first_line; +extern line *current_line; +extern character *prev_character,*current_character; +extern character *eof; + +extern int modified; +extern int ncurrent_line; +extern int num_lines; +extern int num_lines_scrolled; + +extern void freeEditor(); + +extern void init_editor(); + +extern int gsl_editor(); + +extern int launch_editor(); + +extern int getScreenPos(line *ln, int rpos); + +extern void empty_line(int nline,int size_line); + +extern void print_ln(const line *ln, WINDOW *wn); + +extern void init_line(line **l); + +extern void init_character(character **ch); + +extern void print_options(); + +extern void freeCharacter(character **c); + +extern void freeLine(line **l); + +extern character* getChLine(line *ln,int pos); //get the character of line ln, in the position pos + +extern void print_all(int ln_scr, int num_rows); + +extern int saveToFile(int file); + +extern void loadFile(int file); + +extern void print_tab(); + +extern int getPosCh(line *ln, character *ch); + +extern int getNumTabs(line *ln, int pos); + +extern int getActualPos(line *ln,int rpos); + +#endif + diff --git a/gsl_edit.c b/gsl_edit.c new file mode 100755 index 00000000..65df32d9 --- /dev/null +++ b/gsl_edit.c @@ -0,0 +1,586 @@ +#include <stdio.h> +#include <unistd.h> +#include <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include "gsl-shell.h" +#include "edit_functions.h" + + +void do_resize(); +int saveToNewFile(); +int loadNewFile(); +void executeFile(); + + +WINDOW *wnd; +line *first_line;//first line +line *current_line;//current line of the cursor +character *prev_character,*current_character; +character *eof;//the final (empty) character + + +int actual_rows; +int actual_cols; + +int modified;//if the user has pressed any key +int ncurrent_line;//number of the current line +int num_lines; //number of lines +int num_lines_scrolled; + + + + +void executeFile() +{ + luaL_dofile(globalL,default_file_name); +} + +int launch_editor() +{ + + int status; + pid_t pid; + if((pid=fork()) == 0)//child process, the editor + { + gsl_editor(); + } + else //parent process + { + //the parent will ignore the CTRL+C signal while editor is executing and the previous CTRL+C behaviour is saved + struct sigaction act_ign,old_ign,act_sigexec,old_sigexec; + act_ign.sa_handler = SIG_IGN; + sigemptyset(&act_ign.sa_mask); + act_ign.sa_flags = 0; + sigaction(SIGINT, &act_ign, &old_ign); + + act_sigexec.sa_handler=executeFile; + sigemptyset(&act_sigexec.sa_mask); + act_sigexec.sa_flags=0; + sigaction(SIGNAL_EXECUTE, &act_sigexec, &old_sigexec); + + while(1) + { + waitpid(pid,&status,0); + if(WIFEXITED(status)) + break; + } + if(!WIFEXITED(status)) //improve the error handling + printf("Child terminated with error.\n"); + //once the editor has finished, the previous behaviour is restored + sigaction(SIGINT, &old_ign, NULL); + sigaction(SIGNAL_EXECUTE, &old_sigexec, NULL); + + } + return 1; +} + +int gsl_editor() +{ + line *prev_line,*aux_line; + character *aux_character; + + int row,col,nrows,ncols,aux_size,aux; + + int file; + int ch; + int end=0; + int cont; + (void) signal(SIGINT, SIG_IGN); + signal(SIGWINCH, do_resize); + initscr(); + wnd=stdscr; + idlok(wnd,TRUE); + idcok(wnd,TRUE); + scrollok(wnd, TRUE); + cbreak(); + noecho(); + keypad(wnd, TRUE); + getmaxyx(wnd,nrows,ncols); + clear(); + refresh(); + init_editor(); + + while( (ch=wgetch(wnd))!= EXIT_EDITOR && !end) + { + getyx(wnd, row,col); + if(ch == LOAD_FILE) + { + if(loadNewFile()) + { + print_all(num_lines_scrolled,actual_rows+1); + move(0,0); + } + else + { + print_all(num_lines_scrolled,actual_rows+1); + move(row,col); + } + } + else if(ch == SAVE) + { + saveToNewFile(); + clear(); + print_all(num_lines_scrolled,actual_rows+1); + move(row,col); + } + else if(ch == CLEAR_EDITOR) + { + freeEditor(); + init_editor(); + } + else if(ch == EXECUTE) + { + file=open(default_file_name, O_CREAT | O_WRONLY,S_IRWXU); + saveToFile(file); + close(file); + kill(getppid(),SIGNAL_EXECUTE); + print_all(num_lines_scrolled,actual_rows+1); + move(row,col); + } + else if(ch == KEY_UP) + { + if(row==0 && current_line->prev!=NULL) + { + num_lines_scrolled--; + current_line=current_line->prev; + ncurrent_line--; + print_all(num_lines_scrolled,actual_rows+1); + current_character=getChLine(current_line,getActualPos(current_line,col)); + move(0,getScreenPos(current_line,col)); + } + else + { + if(current_line->prev!=NULL) + { + ncurrent_line--; + current_line=current_line->prev; + current_character=getChLine(current_line,getActualPos(current_line,col)); + move(row-1,getScreenPos(current_line,col)); + } + } + } + else if (ch == KEY_LEFT) + { + if(current_character->prev == NULL && row ==0 && current_line->prev!=NULL) + { + num_lines_scrolled--; + ncurrent_line--; + current_line=current_line->prev; + current_character=getChLine(current_line,current_line->size-1); + print_all(num_lines_scrolled,actual_rows+1); + move(0,current_line->size + current_line->num_tabs*(SPACES_TAB-1)-1); + } + else + { + if(current_character->prev == NULL && current_line->prev!=NULL)//go up one line + { + ncurrent_line--; + current_line=current_line->prev; + current_character=getChLine(current_line,current_line->size-1); + move(row-1,current_line->size + current_line->num_tabs*(SPACES_TAB-1)-1); + } + else if(current_character->prev!=NULL) + { + if(current_character->prev->c == '\t') + move(row,col-SPACES_TAB); + else + move(row,col-1); + current_character=current_character->prev; + } + } + } + else if (ch == KEY_RIGHT) + { + if(current_character->next!=NULL) + { + if(current_character->c == '\t') + move(row,col+SPACES_TAB); + else + move(row,col+1); + current_character=current_character->next; + } + else + { + if((row == (actual_rows)) && current_line->next!=NULL) + { + num_lines_scrolled++; + ncurrent_line++; + current_line=current_line->next; + current_character=current_line->first_ch; + print_all(num_lines_scrolled,actual_rows+1); + move(row,0); + } + else + { + if(current_line->next!=NULL) + { + move(row+1,0); + ncurrent_line++; + current_line=current_line->next; + current_character=current_line->first_ch; + } + } + } + } + else if (ch == KEY_DOWN) + { + + if( (row == (actual_rows)) && current_line->next!=NULL ) + { + num_lines_scrolled++; + ncurrent_line++; + current_line=current_line->next; + print_all(num_lines_scrolled,actual_rows+1); + current_character=getChLine(current_line,getActualPos(current_line,col)); + move(row,getScreenPos(current_line,col)); + } + else + { + if(current_line->next!=NULL) + { + ncurrent_line++; + current_line=current_line->next; + current_character=getChLine(current_line,getActualPos(current_line,col)); + move(row+1,getScreenPos(current_line,col)); + } + } + } + else if (ch == KEY_DC) + { + if(current_character != eof) + { + if(current_character->c != '\n') + { + aux_character=current_character->next; + current_character->next->prev=current_character->prev; + if(current_character == current_line->first_ch) + { + current_line->first_ch=current_character->next; + } + else + current_character->prev->next=current_character->next; + + if(current_character->c == '\t') + { + for(cont=0;cont<SPACES_TAB-1;cont++) + delch(); + current_line->num_tabs--; + } + delch(); + freeCharacter(¤t_character); + current_character=aux_character; + current_line->size--; + + } + else //delete a line + { + aux_line=current_line->next; + aux_size=current_line->size + getNumTabs(current_line,0)*(SPACES_TAB-1); + if(current_character == current_line->first_ch) + current_line->first_ch=current_line->next->first_ch; + else + current_character->prev->next=current_line->next->first_ch; + aux_character=current_line->next->first_ch; + current_line->next->first_ch->prev=current_character->prev; + current_line->size+=current_line->next->size-1; + if(current_line->next->next) + current_line->next->next->prev=current_line; + current_line->next=current_line->next->next; + current_line->num_tabs+=aux_line->num_tabs; + freeLine(&aux_line); + freeCharacter(¤t_character); + current_character=aux_character; + print_all(num_lines_scrolled,actual_rows+1); + move(row,aux_size-1); + } + } + } + else if(ch == KEY_BACKSPACE) + { + if(current_character->prev == NULL && current_line->prev != NULL) //move up a line + { + aux_line=current_line->prev; + aux_line->num_tabs+=current_line->num_tabs; + aux_size=current_line->prev->size + getNumTabs(current_line->prev, 0)*(SPACES_TAB-1); + aux_character=getChLine(current_line->prev,current_line->prev->size-1);//get the last character + if(aux_character != current_line->prev->first_ch) + { + aux_character->prev->next=current_character; + } + else + { + current_line->prev->first_ch=current_character; + } + current_character->prev=aux_character->prev; + current_line->prev->next=current_line->next; + if(current_line->next!=NULL) + current_line->next->prev=current_line->prev; + freeCharacter(&aux_character); + aux_line->size+=current_line->size-1; + freeLine(¤t_line); + current_line=aux_line; + if(row == 0) + { + num_lines_scrolled--; + } + print_all(num_lines_scrolled,actual_rows+1); + move(row == 0 ? 0:row-1 ,aux_size-1); + } + else if(current_character->prev!=NULL) + { + aux_character=current_character->prev; + if(current_character->prev == current_line->first_ch) + { + current_character->prev=NULL; + current_line->first_ch=current_character; + } + else + { + current_character->prev->prev->next=current_character; + current_character->prev=current_character->prev->prev; + } + + current_line->size--; + if(aux_character->c == '\t') + { + move(row,col - SPACES_TAB); + for(cont=0;cont<SPACES_TAB-1;cont++) + delch(); + current_line->num_tabs--; + } + else + move(row,col-1); + delch(); + freeCharacter(&aux_character); + } + } + else //normal characters + { + if(modified==0) + { + modified=1; + } + current_line->size++; + if(current_character == eof) + { + if(current_line->first_ch==eof) + { + init_character(&prev_character); + current_line->first_ch=prev_character; + prev_character->c=ch; + if(ch!='\n') + { + current_character=eof; + prev_character->next=current_character; + current_character->prev=prev_character; + } + else + { + prev_character->next=NULL; + current_line->size--;//eof erased + } + } + else + { + prev_character=current_character->prev; + init_character(¤t_character); + current_character->c=ch; + prev_character->next=current_character; + current_character->prev=prev_character; + if(ch!='\n') + { + current_character->next=eof; + eof->prev=current_character; + prev_character=current_character; + current_character=eof; + } + else + { + current_character->next=NULL; + current_line->size--;//eof erased + } + } + if(ch == '\n') + { + move(row+1,0); + num_lines++; + ncurrent_line++; + prev_line=current_line; + init_line(¤t_line); + init_character(&eof); + eof->c='0円'; + current_character=eof; + current_line->size=1; + current_line->first_ch=current_character; + prev_line->next=current_line; + current_line->prev=prev_line; + current_line->num_line=ncurrent_line; + if(row == (actual_rows)) + { + /*wscrl(wnd,1); + move(row,0);*/ + num_lines_scrolled++; + print_all(num_lines_scrolled,actual_rows+1); + move(row,0); + } + } + else + { + if(ch == '\t') + { + current_line->num_tabs++; + print_tab(); + } + else + printw("%c",ch); + } + } + else //insert a character + { + init_character(&aux_character); + aux_character->c=ch; + aux_character->prev=current_character->prev; + aux_character->next=NULL; + if(ch!='\n') + { + aux_character->next=current_character; + if(current_character == current_line->first_ch) + current_line->first_ch=aux_character; + else + current_character->prev->next=aux_character; + current_character->prev=aux_character; + move(row,0); + print_ln(current_line,wnd); + if(ch == '\t') + { + move(row,col+SPACES_TAB); + current_line->num_tabs++; + } + else + move(row,col+1); + } + else //insert a line + { + current_line->size--;//eof erased + aux=getPosCh(current_line,current_character); + init_line(&aux_line); + current_line->num_tabs-=getNumTabs(current_line,aux);; + aux_line->num_tabs=getNumTabs(current_line,aux); + num_lines++; + ncurrent_line++; + if(current_character == current_line->first_ch) + current_line->first_ch=aux_character; + else + current_character->prev->next=aux_character; + current_character->prev=NULL; + aux_line->first_ch=current_character; + aux_line->next=current_line->next; + aux_line->prev=current_line; + aux_line->size=current_line->size-aux; + if(current_line->next) + current_line->next->prev=aux_line; + current_line->size=aux+1; + current_line->next=aux_line; + prev_line=current_line; + current_line=aux_line; + if(row == actual_rows) + { + num_lines_scrolled++; + print_all(num_lines_scrolled,actual_rows+1); + move(row,0); + } + else + { + print_all(num_lines_scrolled,actual_rows+1); + move(row+1,0); + } + } + } + } + refresh(); + } + endwin(); + exit(EXIT_SUCCESS); +} + + +int loadNewFile() +{ + echo(); + char fileName[MAX_FILE_LENGHT];int file,er; + clear(); + printw("Enter the name of the file to load:"); + getstr(fileName); + noecho(); + file=open(fileName,O_RDONLY,S_IRWXU); + er=errno; + if(file==-1) + { + printw("Error opening file:%s\nPress a key to continue",strerror(er)); + getch(); + return 0; + } + freeEditor(); + init_editor(); + loadFile(file); + close(file); + clear(); + return 1; +} + +int saveToNewFile() +{ + echo(); + char fileName[MAX_FILE_LENGHT];int file,er; + clear(); + printw("Enter the name of the file:"); + getstr(fileName); + noecho(); + file=open(fileName, O_CREAT | O_WRONLY,S_IRWXU); + er=errno; + if(file==-1) + { + printw("Error opening file:%s\nPress a key to continue",strerror(er)); + getch(); + return 0; + } + saveToFile(file); + close(file); + clear(); + return 1; +} + + +void do_resize() +{ + const char *tty = ttyname(0); + int fd, result = 0; + struct winsize win; + + if (tty == NULL) + return; + fd = open(tty, O_RDWR); + if (fd == -1) + return; + result = ioctl(fd,TIOCGWINSZ, &win); + close(fd); + if (result == -1) + return; + COLS = win.ws_col; + LINES = win.ws_row; + resize_term(LINES,COLS); + print_all(0,actual_rows); + wrefresh(wnd); + /*clear(); + refresh(); + print_all(first_line,0,LINES); + sleep(3); + clear(); + refresh(); + print_all(first_line,0,LINES);*/ +}
\ No newline at end of file |