shell_spawn.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. /*
  13. * if CLISH_PATH is unset in the environment then this is the value used.
  14. */
  15. const char *default_path = "/etc/clish;~/.clish";
  16. /*-------------------------------------------------------- */
  17. /* perform a simple tilde substitution for the home directory
  18. * defined in HOME
  19. */
  20. static char *clish_shell_tilde_expand(const char *path)
  21. {
  22. char *home_dir = getenv("HOME");
  23. char *result = NULL;
  24. char *tilde;
  25. while ((tilde = strchr(path, '~'))) {
  26. lub_string_catn(&result, path, tilde - path);
  27. lub_string_cat(&result, home_dir);
  28. path = tilde + 1;
  29. }
  30. lub_string_cat(&result, path);
  31. return result;
  32. }
  33. /*-------------------------------------------------------- */
  34. void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
  35. {
  36. const char *path = xml_path;
  37. char *buffer;
  38. char *dirname;
  39. char *saveptr;
  40. /* use the default path */
  41. if (!path)
  42. path = default_path;
  43. /* take a copy of the path */
  44. buffer = clish_shell_tilde_expand(path);
  45. /* now loop though each directory */
  46. for (dirname = strtok_r(buffer, ";", &saveptr);
  47. dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
  48. DIR *dir;
  49. struct dirent *entry;
  50. /* search this directory for any XML files */
  51. dir = opendir(dirname);
  52. if (NULL == dir) {
  53. #ifdef DEBUG
  54. tinyrl_printf(this->tinyrl,
  55. "*** Failed to open '%s' directory\n",
  56. dirname);
  57. #endif
  58. continue;
  59. }
  60. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  61. const char *extension = strrchr(entry->d_name, '.');
  62. /* check the filename */
  63. if ((NULL != extension) &&
  64. (0 == strcmp(".xml", extension))) {
  65. char *filename = NULL;
  66. /* build the filename */
  67. lub_string_cat(&filename, dirname);
  68. lub_string_cat(&filename, "/");
  69. lub_string_cat(&filename, entry->d_name);
  70. /* load this file */
  71. (void)clish_shell_xml_read(this, filename);
  72. /* release the resource */
  73. lub_string_free(filename);
  74. }
  75. }
  76. /* all done for this directory */
  77. closedir(dir);
  78. }
  79. /* tidy up */
  80. lub_string_free(buffer);
  81. #ifdef DEBUG
  82. clish_shell_dump(this);
  83. #endif
  84. }
  85. /*-------------------------------------------------------- */
  86. int clish_shell_loop(clish_shell_t *this)
  87. {
  88. int running = 0;
  89. int retval = SHELL_STATE_OK;
  90. assert(this);
  91. if (!tinyrl__get_istream(this->tinyrl))
  92. return SHELL_STATE_IO_ERROR;
  93. /* Check the shell isn't closing down */
  94. if (this && (SHELL_STATE_CLOSING == this->state))
  95. return retval;
  96. /* Loop reading and executing lines until the user quits */
  97. while (!running) {
  98. retval = SHELL_STATE_OK;
  99. /* Get input from the stream */
  100. running = clish_shell_readline(this, NULL);
  101. if (running) {
  102. switch (this->state) {
  103. case SHELL_STATE_SCRIPT_ERROR:
  104. case SHELL_STATE_SYNTAX_ERROR:
  105. /* Interactive session doesn't exit on error */
  106. if (tinyrl__get_isatty(this->tinyrl) ||
  107. (this->current_file &&
  108. !this->current_file->stop_on_error))
  109. running = 0;
  110. retval = this->state;
  111. default:
  112. break;
  113. }
  114. }
  115. if (SHELL_STATE_CLOSING == this->state)
  116. running = -1;
  117. if (running)
  118. running = clish_shell_pop_file(this);
  119. }
  120. return retval;
  121. }
  122. /*-------------------------------------------------------- */