esh_init.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "===================================\n",
  21. esh_info_global->user_data->pw_name,
  22. esh_info_global->build_ref,
  23. esh_info_global->user_data->pw_dir,
  24. esh_info_global->user_data->pw_shell,
  25. esh_info_global->config_file_path,
  26. esh_info_global->history_file_path,
  27. esh_info_global->history_limit,
  28. esh_info_global->max_jobs_number);
  29. }
  30. void EShInitInfo(int argc, char** argv) {
  31. esh_info_global = malloc(sizeof(EShInfo));
  32. esh_info_global->build_ref = malloc((strlen(BUILD_REF) + 1) * sizeof(char));
  33. strcpy(esh_info_global->build_ref, BUILD_REF);
  34. esh_info_global->user_data = getpwuid(geteuid());
  35. esh_info_global->current_working_dir = malloc((PATH_MAX + 1) * sizeof(char));
  36. memset(esh_info_global->current_working_dir, 0, (PATH_MAX + 1) * sizeof(char));
  37. getcwd(esh_info_global->current_working_dir, PATH_MAX);
  38. EShOptimiseCurrentWorkingDirectory();
  39. esh_info_global->config_file_path = malloc((PATH_MAX + 1) * sizeof(char));
  40. memset(esh_info_global->config_file_path, 0, (PATH_MAX + 1) * sizeof(char));
  41. strcat(esh_info_global->config_file_path, esh_info_global->user_data->pw_dir);
  42. strcat(esh_info_global->config_file_path, "/.esh_conf");
  43. esh_info_global->history_file_path = malloc((PATH_MAX + 1) * sizeof(char));
  44. memset(esh_info_global->history_file_path, 0, (PATH_MAX + 1) * sizeof(char));
  45. strcat(esh_info_global->history_file_path, esh_info_global->user_data->pw_dir);
  46. strcat(esh_info_global->history_file_path, "/.esh_history");
  47. esh_info_global->history_limit = 5000;
  48. esh_info_global->max_jobs_number = 64;
  49. esh_info_global->jobs_number = 0;
  50. esh_info_global->max_invite_message_len = 2048;
  51. esh_info_global->invite_message = malloc((esh_info_global->max_invite_message_len + 1) * sizeof(char));
  52. EShUpdateInviteMessage();
  53. esh_info_global->exited = 0;
  54. }
  55. void EShParseCommandLineArgs(int argc, char** argv) {
  56. for (int i = 1; i < argc; i += 2) {
  57. if (strcmp(argv[i], "-h") == 0) {
  58. EShShowHelpAndExit();
  59. }
  60. if (i + 1 >= argc) {
  61. printf("ERROR: Incorrect command-line parameters number!");
  62. exit(1);
  63. }
  64. if (strcmp(argv[i], "-config") == 0) {
  65. strcpy(esh_info_global->config_file_path, argv[i + 1]);
  66. }
  67. if (strcmp(argv[i], "-history_path") == 0) {
  68. strcpy(esh_info_global->history_file_path, argv[i + 1]);
  69. }
  70. if (strcmp(argv[i], "-history_limit") == 0) {
  71. sscanf(argv[i + 1], "%d", &esh_info_global->history_limit);
  72. }
  73. if (strcmp(argv[i], "-max_active_jobs") == 0) {
  74. sscanf(argv[i + 1], "%d", &esh_info_global->max_jobs_number);
  75. }
  76. }
  77. }
  78. void EShProcessConfigFile() {
  79. FILE* file = fopen(esh_info_global->config_file_path, "r");
  80. if (!file) {
  81. return;
  82. }
  83. char* line = NULL;
  84. ssize_t len;
  85. int line_length;
  86. while ((line_length = getline(&line, &len, file)) != -1) {
  87. if (line[line_length - 1] == '\n') {
  88. line[line_length - 1] = '\0';
  89. --line_length;
  90. }
  91. if (strstr(line, "history_path=") == line) {
  92. strcpy(esh_info_global->history_file_path, line + strlen("history_path="));
  93. }
  94. if (strstr(line, "history_limit=") == line) {
  95. sscanf(line + strlen("history_limit="), "%d", &esh_info_global->history_limit);
  96. }
  97. if (strstr(line, "max_active_jobs=") == line) {
  98. sscanf(line + strlen("max_active_jobs="), "%d", &esh_info_global->max_jobs_number);
  99. }
  100. }
  101. fclose(file);
  102. }