esh_init.c 5.2 KB

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