shell_new.c 2.6 KB

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