shell_new.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. clish_shell_iterator_init(&this->iter, CLISH_NSPACE_NONE);
  34. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  35. this->current_file = NULL;
  36. this->cfg_pwdv = NULL;
  37. this->cfg_pwdc = 0;
  38. this->client = konf_client_new(KONFD_SOCKET_PATH);
  39. this->completion_pargv = 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. }
  59. /*-------------------------------------------------------- */
  60. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  61. void *cookie, FILE * istream, FILE * ostream)
  62. {
  63. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  64. if (this) {
  65. clish_shell_init(this, hooks, cookie,
  66. istream, ostream);
  67. if (hooks->init_fn) {
  68. /* now call the client initialisation */
  69. if (BOOL_TRUE != hooks->init_fn(this)) {
  70. this->state = SHELL_STATE_CLOSING;
  71. }
  72. }
  73. }
  74. return this;
  75. }
  76. /*-------------------------------------------------------- */