esh_misc.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "esh_misc.h"
  2. void EShOptimiseCurrentWorkingDirectory() {
  3. if (strstr(esh_info_global->current_working_dir, esh_info_global->user_data->pw_dir) == esh_info_global->current_working_dir) {
  4. char new_cwd[PATH_MAX + 1];
  5. memset(new_cwd, 0, PATH_MAX + 1);
  6. strcat(new_cwd, "~");
  7. strcat(new_cwd, esh_info_global->current_working_dir + strlen(esh_info_global->user_data->pw_dir));
  8. strcpy(esh_info_global->current_working_dir, new_cwd);
  9. }
  10. }
  11. void EShUpdateInviteMessage() {
  12. memset(esh_info_global->invite_message, 0, (esh_info_global->max_invite_message_len + 1) * sizeof(char));
  13. strcat(esh_info_global->invite_message, ANSI_COLOR_BLUE);
  14. strcat(esh_info_global->invite_message, esh_info_global->user_data->pw_name);
  15. strcat(esh_info_global->invite_message, ANSI_COLOR_RESET);
  16. strcat(esh_info_global->invite_message, "@");
  17. strcat(esh_info_global->invite_message, ANSI_COLOR_GREEN);
  18. strcat(esh_info_global->invite_message, esh_info_global->current_working_dir);
  19. strcat(esh_info_global->invite_message, ANSI_COLOR_RESET);
  20. strcat(esh_info_global->invite_message, " » ");
  21. }
  22. void EShShowHelpAndExit() {
  23. printf(ANSI_COLOR_RED "ESh (Endevir Shell) build %.8s\n" ANSI_COLOR_RESET
  24. "\n"
  25. "Usage: esh [-h] [-config FILENAME] [-history FILENAME] [-history_limit INT] [-max_active_jobs INT] [-max_command_length INT]\n"
  26. "\n"
  27. "Description:\n"
  28. "This is a simple shell, written in C\n"
  29. "\n"
  30. "Parameters:\n"
  31. "-h shows this message and exits\n"
  32. "-config FILENAME uses file with name FILENAME as config file (default: ~/.esh_conf)\n"
  33. "-history_path FILENAME uses file with name FILENAME as storage for entered commands history (default: ~/.esh_history)\n"
  34. "-history_limit INT limits commands number to keep up to INT (default: 5000 lines)\n"
  35. "-max_active_jobs INT limits shell active jobs number up to INT (default: 64)\n"
  36. "-max_command_length INT limits maximum length of single command up to INT (default: 1024 chars)\n"
  37. "\n"
  38. "\n"
  39. "Copyleft. Written by Ivan Arkhipov aka Endevir (me@endevir.ru), MIPT, Dolgoprudny, Russia, 2019.\n",
  40. esh_info_global->build_ref);
  41. exit(0);
  42. }
  43. int EShIsShellLetter(char c) { // TODO: Russian symbols
  44. return (c >= 32 && c <= 127);
  45. }
  46. ESH_JOB_DELIMITER EshIsJobDelimiter(char* c) {
  47. if (strstr(c, "&&") == c) {
  48. return LOGIC_AND;
  49. }
  50. if (strstr(c, "||") == c) {
  51. return LOGIC_OR;
  52. }
  53. if (strstr(c, "&") == c) {
  54. return BIT_AND;
  55. }
  56. if (strstr(c, "|") == c) {
  57. return BIT_OR;
  58. }
  59. if (strstr(c, ";") == c || strstr(c, "\n") == c) {
  60. return SEMICOLON;
  61. }
  62. return NOT_A_DELIMITER;
  63. }
  64. int EShGetJobDelimiterSize(ESH_JOB_DELIMITER delim) {
  65. if (delim == LOGIC_AND || delim == LOGIC_OR) {
  66. return 2;
  67. }
  68. if (delim == BIT_AND || delim == BIT_OR || delim == SEMICOLON) {
  69. return 1;
  70. }
  71. return 0;
  72. }