Browse Source

Begin impl of operations history support

Ivan Arkhipov 5 years ago
parent
commit
ea4e93d670
4 changed files with 52 additions and 0 deletions
  1. 9 0
      esh_history.c
  2. 11 0
      esh_history.h
  3. 29 0
      esh_init.c
  4. 3 0
      esh_types.h

+ 9 - 0
esh_history.c

@@ -0,0 +1,9 @@
+#include "esh_history.h"
+
+void addCommandToHistory(char* command) {
+	// TODO
+}
+
+char* receiveCommandFromHistory(int step = 1) {
+	// TODO
+}

+ 11 - 0
esh_history.h

@@ -0,0 +1,11 @@
+#include "esh_types.h"
+
+#ifndef ESH_HISTORY_H
+#define ESH_HISTORY_H
+
+void addCommandToHistory(char* command); // saves command copy to history list.
+
+char* receiveCommandFromHistory(int step = 1); // returns copy of command, executed _step_ steps ago.
+
+
+#endif // ESH_HISTORY_H

+ 29 - 0
esh_init.c

@@ -8,6 +8,7 @@ void EShInit(int argc, char** argv) {
 	EShInitInfo(argc, argv);
 	EShParseCommandLineArgs(argc, argv);
 	EShProcessConfigFile();
+	EShProcessHistoryFile();
 
 	printf("===================================\n"
 		   "Hello, %s!\n"
@@ -65,6 +66,10 @@ void EShInitInfo(int argc, char** argv) {
 
 	esh_info_global->jobs_number = 0;
 
+	esh_info_global->history_command_list = malloc((esh_info_global->history_limit + 1) * sizeof(char*));
+	
+	esh_info_global->history_command_list_size = 0;
+
 	esh_info_global->max_invite_message_len = 2048;
 
 	esh_info_global->invite_message = malloc((esh_info_global->max_invite_message_len + 1) * sizeof(char));
@@ -141,3 +146,27 @@ void EShProcessConfigFile() {
 
 	fclose(file);
 }
+
+void EShProcessHistoryFile() {
+	FILE* file = fopen(esh_info_global->history_file_path, "r");
+	if (!file) {
+		return;
+	}
+
+	char* line = NULL;
+	ssize_t len = 0;
+	int line_length = 0;
+	
+    while ((line_length = getline(&line, &len, file)) != -1) {
+    	if (line[line_length - 1] == '\n') {
+    		line[line_length - 1] = '\0';
+    		--line_length;
+    	}
+
+    	esh_info_global->history_command_list[esh_info_global->history_command_list_size] = malloc(strlen(line) + 1);
+    	strcpy(esh_info_global->history_command_list[esh_info_global->history_command_list_size], line);
+    	++esh_info_global->history_command_list_size;
+    }
+
+	fclose(file);
+}

+ 3 - 0
esh_types.h

@@ -53,6 +53,9 @@ typedef struct {
 	int max_jobs_number; // max active jobs (foreground & background) limit (changeable via settings/cmd line args)
 	int max_command_length; // max length of single command (changeable via settings/cmd line args)
 
+	char** history_command_list;
+	int history_command_list_size;
+
 	int max_invite_message_len;
 	char* invite_message;