shell_new.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, FILE * ostream)
  13. {
  14. clish_ptype_t *tmp_ptype = NULL;
  15. /* initialise the tree of views */
  16. lub_bintree_init(&this->view_tree,
  17. clish_view_bt_offset(),
  18. clish_view_bt_compare, clish_view_bt_getkey);
  19. /* initialise the tree of ptypes */
  20. lub_bintree_init(&this->ptype_tree,
  21. clish_ptype_bt_offset(),
  22. clish_ptype_bt_compare, clish_ptype_bt_getkey);
  23. assert((NULL != hooks) && (NULL != hooks->script_fn));
  24. /* set up defaults */
  25. this->client_hooks = hooks;
  26. this->client_cookie = cookie;
  27. this->view = NULL;
  28. this->viewid = NULL;
  29. this->global = NULL;
  30. this->startup = NULL;
  31. this->state = SHELL_STATE_INITIALISING;
  32. this->overview = NULL;
  33. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  34. this->current_file = NULL;
  35. this->cfg_pwdv = NULL;
  36. this->cfg_pwdc = 0;
  37. this->client = konf_client_new(KONFD_SOCKET_PATH);
  38. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  39. /* Create internal ptypes and params */
  40. /* Current depth */
  41. tmp_ptype = clish_shell_find_create_ptype(this,
  42. "__DEPTH", "Depth", "[0-9]+",
  43. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  44. assert(tmp_ptype);
  45. this->param_depth = clish_param_new("__cur_depth",
  46. "Current depth", tmp_ptype);
  47. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  48. /* Current pwd */
  49. tmp_ptype = clish_shell_find_create_ptype(this,
  50. "__PWD", "Path", ".+",
  51. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  52. assert(tmp_ptype);
  53. this->param_pwd = clish_param_new("__cur_pwd",
  54. "Current path", tmp_ptype);
  55. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  56. /* Initialize context */
  57. this->context.completion_pargv = NULL;
  58. clish_shell_iterator_init(&this->context.iter, CLISH_NSPACE_NONE);
  59. }
  60. /*-------------------------------------------------------- */
  61. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  62. void *cookie, FILE * istream, FILE * ostream)
  63. {
  64. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  65. if (this) {
  66. clish_shell_init(this, hooks, cookie,
  67. istream, ostream);
  68. if (hooks->init_fn) {
  69. /* now call the client initialisation */
  70. if (BOOL_TRUE != hooks->init_fn(this)) {
  71. this->state = SHELL_STATE_CLOSING;
  72. }
  73. }
  74. }
  75. return this;
  76. }
  77. /*-------------------------------------------------------- */