esh_misc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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) {
  44. return (c >= 'a' && c <= 'z') ||
  45. (c >= 'A' && c <= 'Z') ||
  46. (c >= 'а' && c <= 'я') ||
  47. (c >= 'А' && c <= 'Я') ||
  48. (c >= '0' && c <= '9') ||
  49. (c >= ' ' && c <= '/') ||
  50. (c >= ':' && c <= '?');
  51. }