shell_new.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. /*-------------------------------------------------------- */
  8. static void
  9. clish_shell_init(clish_shell_t * this,
  10. const clish_shell_hooks_t * hooks,
  11. void *cookie, FILE * istream)
  12. {
  13. /* initialise the tree of views */
  14. lub_bintree_init(&this->view_tree,
  15. clish_view_bt_offset(),
  16. clish_view_bt_compare, clish_view_bt_getkey);
  17. /* initialise the tree of ptypes */
  18. lub_bintree_init(&this->ptype_tree,
  19. clish_ptype_bt_offset(),
  20. clish_ptype_bt_compare, clish_ptype_bt_getkey);
  21. assert((NULL != hooks) && (NULL != hooks->script_fn));
  22. /* set up defaults */
  23. this->client_hooks = hooks;
  24. this->client_cookie = cookie;
  25. this->view = NULL;
  26. this->viewid = NULL;
  27. this->global = NULL;
  28. this->startup = NULL;
  29. this->state = SHELL_STATE_INITIALISING;
  30. this->overview = NULL;
  31. clish_shell_iterator_init(&this->iter, CLISH_NSPACE_NONE);
  32. this->tinyrl = clish_shell_tinyrl_new(istream, stdout, 0);
  33. this->current_file = NULL;
  34. this->cfg_pwdv = NULL;
  35. this->cfg_pwdc = 0;
  36. this->client = conf_client_new(CONFD_SOCKET_PATH);
  37. this->completion_pargv = NULL;
  38. }
  39. /*-------------------------------------------------------- */
  40. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  41. void *cookie, FILE * istream)
  42. {
  43. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  44. if (this) {
  45. clish_shell_init(this, hooks, cookie, istream);
  46. if (hooks->init_fn) {
  47. /* now call the client initialisation */
  48. if (BOOL_TRUE != hooks->init_fn(this)) {
  49. this->state = SHELL_STATE_CLOSING;
  50. }
  51. }
  52. }
  53. return this;
  54. }
  55. /*-------------------------------------------------------- */