1
0

shell_spawn.c 2.8 KB

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