shell_new.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include "lub/string.h"
  8. /*-------------------------------------------------------- */
  9. static void
  10. clish_shell_init(clish_shell_t * this,
  11. const clish_shell_hooks_t * hooks,
  12. void *cookie, FILE * istream,
  13. FILE * ostream,
  14. bool_t stop_on_error)
  15. {
  16. clish_ptype_t *tmp_ptype = NULL;
  17. /* initialise the tree of views */
  18. lub_bintree_init(&this->view_tree,
  19. clish_view_bt_offset(),
  20. clish_view_bt_compare, clish_view_bt_getkey);
  21. /* initialise the tree of ptypes */
  22. lub_bintree_init(&this->ptype_tree,
  23. clish_ptype_bt_offset(),
  24. clish_ptype_bt_compare, clish_ptype_bt_getkey);
  25. assert((NULL != hooks) && (NULL != hooks->script_fn));
  26. /* set up defaults */
  27. this->client_hooks = hooks;
  28. this->client_cookie = cookie;
  29. this->view = NULL;
  30. this->viewid = NULL;
  31. this->global = NULL;
  32. this->startup = NULL;
  33. this->state = SHELL_STATE_INITIALISING;
  34. this->overview = NULL;
  35. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  36. this->current_file = NULL;
  37. this->cfg_pwdv = NULL;
  38. this->cfg_pwdc = 0;
  39. this->client = NULL;
  40. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  41. this->default_shebang = lub_string_dup("/bin/sh");
  42. this->fifo_name = NULL;
  43. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  44. /* Create internal ptypes and params */
  45. /* Current depth */
  46. tmp_ptype = clish_shell_find_create_ptype(this,
  47. "__DEPTH", "Depth", "[0-9]+",
  48. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  49. assert(tmp_ptype);
  50. this->param_depth = clish_param_new("__cur_depth",
  51. "Current depth", tmp_ptype);
  52. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  53. /* Current pwd */
  54. tmp_ptype = clish_shell_find_create_ptype(this,
  55. "__PWD", "Path", ".+",
  56. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  57. assert(tmp_ptype);
  58. this->param_pwd = clish_param_new("__cur_pwd",
  59. "Current path", tmp_ptype);
  60. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  61. /* Interactive */
  62. tmp_ptype = clish_shell_find_create_ptype(this,
  63. "__INTERACTIVE", "Interactive flag", "[01]",
  64. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  65. assert(tmp_ptype);
  66. this->param_interactive = clish_param_new("__interactive",
  67. "Interactive flag", tmp_ptype);
  68. clish_param__set_hidden(this->param_interactive, BOOL_TRUE);
  69. /* Initialize context */
  70. this->context.completion_pargv = NULL;
  71. clish_shell_iterator_init(&this->context.iter, CLISH_NSPACE_NONE);
  72. /* Push non-NULL istream */
  73. if (istream)
  74. clish_shell_push_fd(this, istream, stop_on_error);
  75. }
  76. /*-------------------------------------------------------- */
  77. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  78. void *cookie,
  79. FILE * istream,
  80. FILE * ostream,
  81. bool_t stop_on_error)
  82. {
  83. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  84. if (this) {
  85. clish_shell_init(this, hooks, cookie,
  86. istream, ostream, stop_on_error);
  87. if (hooks->init_fn) {
  88. /* now call the client initialisation */
  89. if (BOOL_TRUE != hooks->init_fn(this)) {
  90. this->state = SHELL_STATE_CLOSING;
  91. }
  92. }
  93. }
  94. return this;
  95. }
  96. /*-------------------------------------------------------- */