esh_init.c 4.3 KB

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