|
@@ -1,5 +1,6 @@
|
|
|
#include "esh_main_loop.h"
|
|
|
#include "esh_misc.h"
|
|
|
+#include "esh_history.h"
|
|
|
|
|
|
void EShRunLoop() {
|
|
|
struct termios orig_term_attr;
|
|
@@ -13,15 +14,21 @@ void EShRunLoop() {
|
|
|
new_term_attr.c_cc[VMIN] = 1;
|
|
|
tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
|
|
|
|
|
|
+ char* command = malloc(esh_info_global->max_command_length);
|
|
|
+ char* uncomplete_command = malloc(esh_info_global->max_command_length);
|
|
|
+
|
|
|
for (;;) {
|
|
|
EShShowMsg();
|
|
|
- char* command = malloc(esh_info_global->max_command_length);
|
|
|
memset(command, 0, esh_info_global->max_command_length);
|
|
|
+
|
|
|
int command_length = 0;
|
|
|
int current_command_pos = 0;
|
|
|
+ int current_history_step = 0;
|
|
|
|
|
|
char input_char;
|
|
|
|
|
|
+ memset(uncomplete_command, 0, esh_info_global->max_command_length);
|
|
|
+
|
|
|
while (input_char = fgetc(stdin)) {
|
|
|
if (input_char == 10) {
|
|
|
printf("\n");
|
|
@@ -35,10 +42,72 @@ void EShRunLoop() {
|
|
|
if (input_char == '\033') {
|
|
|
fgetc(stdin);
|
|
|
switch(fgetc(stdin)) {
|
|
|
- case 'A':
|
|
|
+ case 'A':
|
|
|
+ {
|
|
|
+ if (current_history_step == 0) {
|
|
|
+ strcpy(uncomplete_command, command);
|
|
|
+ }
|
|
|
+
|
|
|
+ char* new_command = EShReceiveCommandFromHistory(current_history_step);
|
|
|
+ if (new_command != NULL) {
|
|
|
+ ++current_history_step;
|
|
|
+ for (int i = current_command_pos; i < command_length; ++i) {
|
|
|
+ printf(" ");
|
|
|
+ ++current_command_pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = current_command_pos; i > 0; --i) {
|
|
|
+ printf("\b \b");
|
|
|
+ }
|
|
|
+
|
|
|
+ strcpy(command, new_command);
|
|
|
+ command_length = strlen(new_command);
|
|
|
+ current_command_pos = command_length;
|
|
|
+
|
|
|
+ for (int i = 0; i < command_length; ++i) {
|
|
|
+ printf("%c", command[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ free(new_command);
|
|
|
break;
|
|
|
+ }
|
|
|
case 'B':
|
|
|
+ {
|
|
|
+ char* new_command;
|
|
|
+
|
|
|
+ if (current_history_step > 0) {
|
|
|
+ --current_history_step;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current_history_step > 0) {
|
|
|
+ new_command = EShReceiveCommandFromHistory(current_history_step - 1);
|
|
|
+ } else {
|
|
|
+ new_command = uncomplete_command;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = current_command_pos; i < command_length; ++i) {
|
|
|
+ printf(" ");
|
|
|
+ ++current_command_pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = current_command_pos; i > 0; --i) {
|
|
|
+ printf("\b \b");
|
|
|
+ }
|
|
|
+
|
|
|
+ strcpy(command, new_command);
|
|
|
+ command_length = strlen(new_command);
|
|
|
+ current_command_pos = command_length;
|
|
|
+
|
|
|
+ for (int i = 0; i < command_length; ++i) {
|
|
|
+ printf("%c", command[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current_history_step > 0) {
|
|
|
+ free(new_command);
|
|
|
+ }
|
|
|
break;
|
|
|
+ }
|
|
|
case 'C':
|
|
|
if (current_command_pos < command_length) {
|
|
|
printf("%c", command[current_command_pos]);
|
|
@@ -110,6 +179,7 @@ void EShRunLoop() {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ EShAddCommandToHistory(command);
|
|
|
int pid = fork();
|
|
|
if (pid == 0) {
|
|
|
execlp(command, command, NULL);
|