esh_init.c 3.1 KB

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