shell_new.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /* Create internal ptypes and params */
  44. /* Current depth */
  45. tmp_ptype = clish_shell_find_create_ptype(this,
  46. "__DEPTH", "Depth", "[0-9]+",
  47. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  48. assert(tmp_ptype);
  49. this->param_depth = clish_param_new("__cur_depth",
  50. "Current depth", tmp_ptype);
  51. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  52. /* Current pwd */
  53. tmp_ptype = clish_shell_find_create_ptype(this,
  54. "__PWD", "Path", ".+",
  55. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  56. assert(tmp_ptype);
  57. this->param_pwd = clish_param_new("__cur_pwd",
  58. "Current path", tmp_ptype);
  59. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  60. /* Initialize context */
  61. this->context.completion_pargv = NULL;
  62. clish_shell_iterator_init(&this->context.iter, CLISH_NSPACE_NONE);
  63. /* Push non-NULL istream */
  64. if (istream)
  65. clish_shell_push_fd(this, istream, stop_on_error);
  66. }
  67. /*-------------------------------------------------------- */
  68. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  69. void *cookie,
  70. FILE * istream,
  71. FILE * ostream,
  72. bool_t stop_on_error)
  73. {
  74. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  75. if (this) {
  76. clish_shell_init(this, hooks, cookie,
  77. istream, ostream, stop_on_error);
  78. if (hooks->init_fn) {
  79. /* now call the client initialisation */
  80. if (BOOL_TRUE != hooks->init_fn(this)) {
  81. this->state = SHELL_STATE_CLOSING;
  82. }
  83. }
  84. }
  85. return this;
  86. }
  87. /*-------------------------------------------------------- */