Browse Source

Major memory management improvements (esh_deinit impl)

Ivan Arkhipov 5 years ago
parent
commit
67482a59ad
4 changed files with 76 additions and 19 deletions
  1. 59 2
      esh_deinit.c
  2. 11 0
      esh_deinit.h
  3. 1 9
      esh_init.c
  4. 5 8
      esh_main_loop.c

+ 59 - 2
esh_deinit.c

@@ -1,5 +1,62 @@
 #include "esh_deinit.h"
 
 void EShDeinit() {
-	
-}
+	EShSaveConfigFile();
+	EShSaveHistoryFile();
+	EShDeinitInfo();
+	printf("\nESh: Goodbye!\n");
+}
+
+void EShDeinitInfo() {
+	// TO BE IMPLEMENTED
+}
+
+void EShSaveConfigFile() {
+	FILE* file = fopen(esh_info_global->config_file_path, "w");
+	if (!file) {
+		printf("\nESh: error, cannot open config file %s, settings may not be saved...\n", esh_info_global->config_file_path);
+		return;
+	}
+
+	fprintf(file, "history_path=%s\n", esh_info_global->history_file_path);
+	fprintf(file, "history_limit=%d\n", esh_info_global->history_limit);
+	fprintf(file, "max_active_jobs=%d\n", esh_info_global->max_jobs_number);
+	fprintf(file, "max_command_length=%d\n", esh_info_global->max_command_length);
+
+	fclose(file);
+}
+
+void EShSaveHistoryFile() {
+	FILE* file = fopen(esh_info_global->history_file_path, "w");
+	if (!file) {
+		printf("\nESh: error, cannot open history file %s, commands may not be saved...\n", esh_info_global->history_file_path);
+		return;
+	}
+
+	for (int i = 0; i < esh_info_global->history_command_list_size; ++i) {
+		fprintf(file, "%s\n", esh_info_global->history_command_list[i]);
+	}
+
+	fclose(file);
+}
+
+void EShDeinitJob(EShJob* job) {
+	if (!job)
+		return;
+
+	free(job->command);
+
+	for (int i = 0; i < esh_info_global->max_command_tokens; ++i) {
+		if (job->command_tokens[i]) {
+			free(job->command_tokens[i]);
+		}
+	}
+	free(job->command_tokens);
+}
+
+void EShFreeJobsList(EShJob* jobs_list) {
+	for (int i = 0; i < esh_info_global->max_jobs_number; ++i) {
+		EShDeinitJob(jobs_list + i);
+	} 
+	free(jobs_list);
+}

+ 11 - 0
esh_deinit.h

@@ -1,7 +1,18 @@
+#include "esh_types.h"
+
 #ifndef ESH_DEINIT_H
 #define ESH_DEINIT_H
 
 void EShDeinit();
 
+void EShDeinitInfo();
+
+void EShSaveConfigFile();
+
+void EShSaveHistoryFile();
+
+void EShDeinitJob(EShJob* job);
+
+void EShFreeJobsList(EShJob* jobs_list);
 
 #endif // ESH_DEINIT_H

+ 1 - 9
esh_init.c

@@ -19,21 +19,13 @@ void EShInit(int argc, char** argv) {
 		   "Home directory: %s\n"
 		   "Default shell program: %s\n"
 		   "Config file path: %s\n"
-		   "Commands history file path: %s\n"
-		   "Max history lines: %d\n"
-		   "Max running jobs: %d\n"
-		   "Max single command length: %d\n"
 		   "===================================\n"
 		   "\n",
 		   esh_info_global->user_data->pw_name, 
 		   esh_info_global->build_ref,
 		   esh_info_global->user_data->pw_dir,
 		   esh_info_global->user_data->pw_shell,
-		   esh_info_global->config_file_path,
-		   esh_info_global->history_file_path,
-		   esh_info_global->history_limit,
-		   esh_info_global->max_jobs_number,
-		   esh_info_global->max_command_length);
+		   esh_info_global->config_file_path);
 }
  
 void EShInitInfo(int argc, char** argv) {

+ 5 - 8
esh_main_loop.c

@@ -2,6 +2,7 @@
 #include "esh_misc.h"
 #include "esh_history.h"
 #include "esh_init.h"
+#include "esh_deinit.h"
 
 void EShRunLoop() {
     struct termios orig_term_attr;
@@ -122,8 +123,7 @@ void EShRunLoop() {
 
 			if (input_char == 4) {
 				// Ctrl + d
-				printf("\n");
-				exit(0);
+				return;
 			}
 
 			if (input_char == 127) {
@@ -151,7 +151,7 @@ void EShRunLoop() {
 			if (input_char == 3) {
 				// Ctrl + c
 				printf("\n");
-				exit(0);
+				return;
 			}
 
 			if (EShIsShellLetter(input_char)) {
@@ -176,13 +176,11 @@ void EShRunLoop() {
 		}
 
 		EShAddCommandToHistory(command);
-		
-		EShJob* jobs;
 		int jobs_num = 0;
-
-		jobs = EShParseCommandIntoJobs(command, &jobs_num);
+		EShJob* jobs = EShParseCommandIntoJobs(command, &jobs_num);
 		// EShPrintJobsDebugInfo(command, jobs, jobs_num);
 		EShExecuteJobs(jobs_num, jobs);
+		EShFreeJobsList(jobs);
 	}
 
 	free(command);
@@ -199,7 +197,6 @@ void EShShowMsg() {
 
 EShJob* EShParseCommandIntoJobs(char* command, int* jobs_num) {
 	EShJob* jobs = EShMakeJobsList();
-
 	*jobs_num = 1;
 	int current_job_cmd_length = 0;