shell_pwd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * shell_pwd.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include "lub/string.h"
  7. #include "private.h"
  8. /*--------------------------------------------------------- */
  9. void clish_shell__init_pwd(clish_shell_pwd_t *pwd)
  10. {
  11. pwd->line = NULL;
  12. pwd->view = NULL;
  13. /* initialise the tree of vars */
  14. lub_bintree_init(&pwd->viewid,
  15. clish_var_bt_offset(),
  16. clish_var_bt_compare, clish_var_bt_getkey);
  17. }
  18. /*--------------------------------------------------------- */
  19. void clish_shell__fini_pwd(clish_shell_pwd_t *pwd)
  20. {
  21. clish_var_t *var;
  22. lub_string_free(pwd->line);
  23. pwd->view = NULL;
  24. /* delete each VAR held */
  25. while ((var = lub_bintree_findfirst(&pwd->viewid))) {
  26. lub_bintree_remove(&pwd->viewid, var);
  27. clish_var_delete(var);
  28. }
  29. }
  30. /*--------------------------------------------------------- */
  31. void clish_shell__set_pwd(clish_shell_t *this,
  32. const char *line, clish_view_t *view, char *viewid, clish_context_t *context)
  33. {
  34. clish_shell_pwd_t **tmp;
  35. size_t new_size = 0;
  36. unsigned i;
  37. unsigned int index = clish_view__get_depth(view);
  38. /* Create new element */
  39. if (index >= this->pwdc) {
  40. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  41. /* resize the pwd vector */
  42. tmp = realloc(this->pwdv, new_size);
  43. assert(tmp);
  44. this->pwdv = tmp;
  45. /* Initialize new elements */
  46. for (i = this->pwdc; i <= index; i++) {
  47. clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
  48. assert(pwd);
  49. clish_shell__init_pwd(pwd);
  50. this->pwdv[i] = pwd;
  51. }
  52. this->pwdc = index + 1;
  53. }
  54. clish_shell__fini_pwd(this->pwdv[index]);
  55. this->pwdv[index]->line = line ? lub_string_dup(line) : NULL;
  56. this->pwdv[index]->view = view;
  57. clish_shell__expand_viewid(viewid, &this->pwdv[index]->viewid, context);
  58. this->depth = index;
  59. }
  60. /*--------------------------------------------------------- */
  61. char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
  62. {
  63. if (index >= this->pwdc)
  64. return NULL;
  65. return this->pwdv[index]->line;
  66. }
  67. /*--------------------------------------------------------- */
  68. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned depth)
  69. {
  70. char *pwd = NULL;
  71. unsigned i;
  72. for (i = 1; i <= depth; i++) {
  73. const char *str =
  74. clish_shell__get_pwd_line(this, i);
  75. /* Cannot get full path */
  76. if (!str) {
  77. lub_string_free(pwd);
  78. return NULL;
  79. }
  80. if (pwd)
  81. lub_string_cat(&pwd, " ");
  82. lub_string_cat(&pwd, "\"");
  83. lub_string_cat(&pwd, str);
  84. lub_string_cat(&pwd, "\"");
  85. }
  86. return pwd;
  87. }
  88. /*--------------------------------------------------------- */
  89. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
  90. {
  91. if (index >= this->pwdc)
  92. return NULL;
  93. return this->pwdv[index]->view;
  94. }
  95. /*--------------------------------------------------------- */
  96. konf_client_t *clish_shell__get_client(const clish_shell_t * this)
  97. {
  98. return this->client;
  99. }
  100. /*--------------------------------------------------------- */
  101. void clish_shell__set_lockfile(clish_shell_t * this, const char * path)
  102. {
  103. if (!this)
  104. return;
  105. lub_string_free(this->lockfile);
  106. this->lockfile = NULL;
  107. if (path)
  108. this->lockfile = lub_string_dup(path);
  109. }
  110. /*--------------------------------------------------------- */
  111. char * clish_shell__get_lockfile(clish_shell_t * this)
  112. {
  113. if (!this)
  114. return NULL;
  115. return this->lockfile;
  116. }
  117. /*--------------------------------------------------------- */
  118. int clish_shell__set_socket(clish_shell_t * this, const char * path)
  119. {
  120. if (!this || !path)
  121. return -1;
  122. konf_client_free(this->client);
  123. this->client = konf_client_new(path);
  124. return 0;
  125. }