diff --git a/emul_inst_decipher.c b/emul_inst_decipher.c index 13cd26d..a4538a1 100644 --- a/emul_inst_decipher.c +++ b/emul_inst_decipher.c @@ -174,6 +174,14 @@ return 1; } +void emul_inst_dec_get_line(char *str_line, int num_line) { + strcpy(str_line, program[num_line]); +} + +int emul_inst_dec_get_program_line_max() { + return program_line_max; +} + int emul_inst_dec_get_token(char *token) { static char line[LOAD_LINE_WIDTH_MAX] = {0}; static char *current = line; diff --git a/emul_inst_decipher.h b/emul_inst_decipher.h index 4f2e8f9..dc292d2 100644 --- a/emul_inst_decipher.h +++ b/emul_inst_decipher.h @@ -30,6 +30,10 @@ int emul_inst_dec_loadline(const char *line); +void emul_inst_dec_get_line(char *str_line, int num_line); + +int emul_inst_dec_get_program_line_max(); + void emul_inst_dec_set_pc(int pc); void emul_inst_dec_update_pc(); diff --git a/emul_repl.c b/emul_repl.c index 61019f5..16a30d1 100644 --- a/emul_repl.c +++ b/emul_repl.c @@ -1,5 +1,6 @@ #include "emul_repl.h" #include "emul_inst_exec.h" +#include "emul_inst_decipher.h" #include "emul_io_std.h" #include #include @@ -10,28 +11,77 @@ static WINDOW *win; void emul_repl_readline(char input[READ_LINE_MAX]) { - char c; + static int current_line = 0; + int history = 0; + int c; int i = 0; int y, x; + current_line = emul_inst_dec_get_program_line_max(); printw(">>"); refresh(); noecho(); + cbreak(); while( (c = getch()) != '\n' ) { - if (c == KEY_BACKSPACE) { - getyx(win, y, x); - input[--i] = '\0'; - move(y, x-1); + if (c == 127) { // Delete and back + if ( i > 0 ) { + getyx(win, y, x); + input[--i] = '\0'; + move(y, x-1); + clrtoeol(); + refresh(); + } + } else if (c == KEY_UP) { + if (current_line+history > 0) { + history--; + input[0] = '\0'; + emul_inst_dec_get_line(input, current_line+history); + getyx(win, y, x); + move(y, 0); + clrtoeol(); + printw("%d>", current_line+history); + i = 0; + while ( input[i] != '\n' && input[i] != '\0') { + echochar(input[i++]); + } + refresh(); + } + } else if (c == KEY_DOWN) { + if (current_line+history < current_line) { + history++; + input[0] = '\0'; + emul_inst_dec_get_line(input, current_line+history); + getyx(win, y, x); + move(y, 0); + clrtoeol(); + printw("%d>", current_line+history); + i = 0; + while ( input[i] != '\n' && input[i] != '\0') { + echochar(input[i++]); + } + refresh(); + } + } else if (c == KEY_RIGHT) { + // Nothing do + } else if (c == KEY_LEFT) { + if ( i > 0 ) { + getyx(win, y, x); + input[--i] = '\0'; + move(y, x-1); + clrtoeol(); + refresh(); + } } else { input[i++] = c; - echochar(c); + input[i] = '\0'; + printw("%c", c); + refresh(); } } echo(); - input[i++] = '\0'; - printw("\r"); clrtoeol(); - //printw("\n"); + printw("\n"); refresh(); + current_line++; } void emul_repl_printline();