|
@@ -0,0 +1,104 @@
|
|
|
+#include "esh_types.h"
|
|
|
+#include "esh_misc.h"
|
|
|
+#include "esh_init.h"
|
|
|
+
|
|
|
+EShInfo* esh_info_global;
|
|
|
+
|
|
|
+void EShInit(int argc, char** argv) {
|
|
|
+ EShInitInfo(argc, argv);
|
|
|
+ EShParseCommandLineArgs(argc, argv);
|
|
|
+ EShProcessConfigFile();
|
|
|
+
|
|
|
+ printf("===================================\n"
|
|
|
+ "Hello, %s!\n"
|
|
|
+ "This is Endevir Shell (ESh)\n"
|
|
|
+ "ESh build %.8s\n"
|
|
|
+ "\n"
|
|
|
+ "Home directory: %s\n"
|
|
|
+ "Default shell program: %s\n"
|
|
|
+ "Config file path: %s\n"
|
|
|
+ "Commands history file path: %s\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);
|
|
|
+}
|
|
|
+
|
|
|
+void EShInitInfo(int argc, char** argv) {
|
|
|
+ esh_info_global = malloc(sizeof(EShInfo));
|
|
|
+
|
|
|
+ esh_info_global->build_ref = malloc((strlen(BUILD_REF) + 1) * sizeof(char));
|
|
|
+ strcpy(esh_info_global->build_ref, BUILD_REF);
|
|
|
+
|
|
|
+ esh_info_global->user_data = getpwuid(geteuid());
|
|
|
+
|
|
|
+
|
|
|
+ esh_info_global->current_working_dir = malloc((PATH_MAX + 1) * sizeof(char));
|
|
|
+ memset(esh_info_global->current_working_dir, 0, (PATH_MAX + 1) * sizeof(char));
|
|
|
+ getcwd(esh_info_global->current_working_dir, PATH_MAX);
|
|
|
+ EShOptimiseCurrentWorkingDirectory();
|
|
|
+
|
|
|
+ esh_info_global->config_file_path = malloc((PATH_MAX + 1) * sizeof(char));
|
|
|
+ memset(esh_info_global->config_file_path, 0, (PATH_MAX + 1) * sizeof(char));
|
|
|
+ strcat(esh_info_global->config_file_path, esh_info_global->user_data->pw_dir);
|
|
|
+ strcat(esh_info_global->config_file_path, "/.esh_conf");
|
|
|
+
|
|
|
+ esh_info_global->history_file_path = malloc((PATH_MAX + 1) * sizeof(char));
|
|
|
+ memset(esh_info_global->history_file_path, 0, (PATH_MAX + 1) * sizeof(char));
|
|
|
+ strcat(esh_info_global->history_file_path, esh_info_global->user_data->pw_dir);
|
|
|
+ strcat(esh_info_global->history_file_path, "/.esh_history");
|
|
|
+
|
|
|
+ esh_info_global->history_limit = 5000;
|
|
|
+
|
|
|
+ esh_info_global->max_jobs_number = 64;
|
|
|
+
|
|
|
+ esh_info_global->jobs_number = 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));
|
|
|
+ EShUpdateInviteMessage();
|
|
|
+
|
|
|
+ esh_info_global->exited = 0;
|
|
|
+}
|
|
|
+
|
|
|
+void EShParseCommandLineArgs(int argc, char** argv) {
|
|
|
+ for (int i = 1; i < argc; i += 2) {
|
|
|
+ if (strcmp(argv[i], "-h") == 0) {
|
|
|
+ EShShowHelpAndExit();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i + 1 >= argc) {
|
|
|
+ printf("ERROR: Incorrect command-line parameters number!");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strcmp(argv[i], "-config") == 0) {
|
|
|
+ strcpy(esh_info_global->config_file_path, argv[i + 1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strcmp(argv[i], "-history_path") == 0) {
|
|
|
+ strcpy(esh_info_global->history_file_path, argv[i + 1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strcmp(argv[i], "-history_limit") == 0) {
|
|
|
+ sscanf(argv[i + 1], "%d", &esh_info_global->history_limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strcmp(argv[i], "-max_active_jobs") == 0) {
|
|
|
+ sscanf(argv[i + 1], "%d", &esh_info_global->max_jobs_number);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void EShProcessConfigFile() {
|
|
|
+ FILE* file = fopen(esh_info_global->config_file_path, "r");
|
|
|
+ if (!file) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose(file);
|
|
|
+}
|