esh_init.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "esh_types.h"
  2. #include "esh_misc.h"
  3. #include "esh_init.h"
  4. EShInfo* esh_info_global;
  5. void EShInit(int argc, char** argv) {
  6. EShInitInfo(argc, argv);
  7. EShParseCommandLineArgs(argc, argv);
  8. EShProcessConfigFile();
  9. EShProcessHistoryFile();
  10. printf("\n"
  11. "===================================\n"
  12. "Hello, %s!\n"
  13. "This is Endevir Shell (ESh)\n"
  14. "ESh build %.8s\n"
  15. "\n"
  16. "Home directory: %s\n"
  17. "Default shell program: %s\n"
  18. "Config file path: %s\n"
  19. "Commands history file path: %s\n"
  20. "Max history lines: %d\n"
  21. "Max running jobs: %d\n"
  22. "Max single command length: %d\n"
  23. "===================================\n"
  24. "\n",
  25. esh_info_global->user_data->pw_name,
  26. esh_info_global->build_ref,
  27. esh_info_global->user_data->pw_dir,
  28. esh_info_global->user_data->pw_shell,
  29. esh_info_global->config_file_path,
  30. esh_info_global->history_file_path,
  31. esh_info_global->history_limit,
  32. esh_info_global->max_jobs_number,
  33. esh_info_global->max_command_length);
  34. }
  35. void EShInitInfo(int argc, char** argv) {
  36. esh_info_global = malloc(sizeof(EShInfo));
  37. esh_info_global->build_ref = malloc((strlen(BUILD_REF) + 1) * sizeof(char));
  38. strcpy(esh_info_global->build_ref, BUILD_REF);
  39. esh_info_global->user_data = getpwuid(geteuid());
  40. esh_info_global->current_working_dir = malloc((PATH_MAX + 1) * sizeof(char));
  41. memset(esh_info_global->current_working_dir, 0, (PATH_MAX + 1) * sizeof(char));
  42. getcwd(esh_info_global->current_working_dir, PATH_MAX);
  43. EShOptimiseCurrentWorkingDirectory();
  44. esh_info_global->config_file_path = malloc((PATH_MAX + 1) * sizeof(char));
  45. memset(esh_info_global->config_file_path, 0, (PATH_MAX + 1) * sizeof(char));
  46. strcat(esh_info_global->config_file_path, esh_info_global->user_data->pw_dir);
  47. strcat(esh_info_global->config_file_path, "/.esh_conf");
  48. esh_info_global->history_file_path = malloc((PATH_MAX + 1) * sizeof(char));
  49. memset(esh_info_global->history_file_path, 0, (PATH_MAX + 1) * sizeof(char));
  50. strcat(esh_info_global->history_file_path, esh_info_global->user_data->pw_dir);
  51. strcat(esh_info_global->history_file_path, "/.esh_history");
  52. esh_info_global->history_limit = 5000;
  53. esh_info_global->max_jobs_number = 64;
  54. esh_info_global->max_command_length = 1024;
  55. esh_info_global->jobs_number = 0;
  56. esh_info_global->history_command_list = malloc((esh_info_global->history_limit + 1) * sizeof(char*));
  57. esh_info_global->history_command_list_size = 0;
  58. esh_info_global->max_invite_message_len = 2048;
  59. esh_info_global->invite_message = malloc((esh_info_global->max_invite_message_len + 1) * sizeof(char));
  60. EShUpdateInviteMessage();
  61. esh_info_global->exited = 0;
  62. }
  63. void EShParseCommandLineArgs(int argc, char** argv) {
  64. for (int i = 1; i < argc; i += 2) {
  65. if (strcmp(argv[i], "-h") == 0) {
  66. EShShowHelpAndExit();
  67. }
  68. if (i + 1 >= argc) {
  69. printf("ERROR: Incorrect command-line parameters number!");
  70. exit(1);
  71. }
  72. if (strcmp(argv[i], "-config") == 0) {
  73. strcpy(esh_info_global->config_file_path, argv[i + 1]);
  74. }
  75. if (strcmp(argv[i], "-history_path") == 0) {
  76. strcpy(esh_info_global->history_file_path, argv[i + 1]);
  77. }
  78. if (strcmp(argv[i], "-history_limit") == 0) {
  79. sscanf(argv[i + 1], "%d", &esh_info_global->history_limit);
  80. }
  81. if (strcmp(argv[i], "-max_active_jobs") == 0) {
  82. sscanf(argv[i + 1], "%d", &esh_info_global->max_jobs_number);
  83. }
  84. if (strcmp(argv[i], "-max_command_length") == 0) {
  85. sscanf(argv[i + 1], "%d", &esh_info_global->max_command_length);
  86. }
  87. }
  88. }
  89. void EShProcessConfigFile() {
  90. FILE* file = fopen(esh_info_global->config_file_path, "r");
  91. if (!file) {
  92. return;
  93. }
  94. char* line = NULL;
  95. ssize_t len = 0;
  96. int line_length = 0;
  97. while ((line_length = getline(&line, &len, file)) != -1) {
  98. if (line[line_length - 1] == '\n') {
  99. line[line_length - 1] = '\0';
  100. --line_length;
  101. }
  102. if (strstr(line, "history_path=") == line) {
  103. strcpy(esh_info_global->history_file_path, line + strlen("history_path="));
  104. }
  105. if (strstr(line, "history_limit=") == line) {
  106. sscanf(line + strlen("history_limit="), "%d", &esh_info_global->history_limit);
  107. }
  108. if (strstr(line, "max_active_jobs=") == line) {
  109. sscanf(line + strlen("max_active_jobs="), "%d", &esh_info_global->max_jobs_number);
  110. }
  111. if (strstr(line, "max_command_length=") == line) {
  112. sscanf(line + strlen("max_command_length="), "%d", &esh_info_global->max_command_length);
  113. }
  114. }
  115. fclose(file);
  116. }
  117. void EShProcessHistoryFile() {
  118. FILE* file = fopen(esh_info_global->history_file_path, "r");
  119. if (!file) {
  120. return;
  121. }
  122. char* line = NULL;
  123. ssize_t len = 0;
  124. int line_length = 0;
  125. while ((line_length = getline(&line, &len, file)) != -1) {
  126. if (line[line_length - 1] == '\n') {
  127. line[line_length - 1] = '\0';
  128. --line_length;
  129. }
  130. esh_info_global->history_command_list[esh_info_global->history_command_list_size] = malloc(strlen(line) + 1);
  131. strcpy(esh_info_global->history_command_list[esh_info_global->history_command_list_size], line);
  132. ++esh_info_global->history_command_list_size;
  133. }
  134. fclose(file);
  135. }