shell_pwd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 int i;
  37. unsigned int index = clish_view__get_depth(view);
  38. clish_shell_pwd_t *newpwd;
  39. /* Create new element */
  40. newpwd = malloc(sizeof(*newpwd));
  41. assert(newpwd);
  42. clish_shell__init_pwd(newpwd);
  43. /* Resize the pwd vector */
  44. if (index >= this->pwdc) {
  45. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  46. tmp = realloc(this->pwdv, new_size);
  47. assert(tmp);
  48. this->pwdv = tmp;
  49. /* Initialize new elements */
  50. for (i = this->pwdc; i <= index; i++) {
  51. clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
  52. assert(pwd);
  53. clish_shell__init_pwd(pwd);
  54. this->pwdv[i] = pwd;
  55. }
  56. this->pwdc = index + 1;
  57. }
  58. /* Fill the new pwd entry */
  59. newpwd->line = line ? lub_string_dup(line) : NULL;
  60. newpwd->view = view;
  61. clish_shell__expand_viewid(viewid, &newpwd->viewid, context);
  62. clish_shell__fini_pwd(this->pwdv[index]);
  63. free(this->pwdv[index]);
  64. this->pwdv[index] = newpwd;
  65. this->depth = index;
  66. }
  67. /*--------------------------------------------------------- */
  68. char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
  69. {
  70. if (index >= this->pwdc)
  71. return NULL;
  72. return this->pwdv[index]->line;
  73. }
  74. /*--------------------------------------------------------- */
  75. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
  76. {
  77. char *pwd = NULL;
  78. unsigned i;
  79. for (i = 1; i <= depth; i++) {
  80. const char *str =
  81. clish_shell__get_pwd_line(this, i);
  82. /* Cannot get full path */
  83. if (!str) {
  84. lub_string_free(pwd);
  85. return NULL;
  86. }
  87. if (pwd)
  88. lub_string_cat(&pwd, " ");
  89. lub_string_cat(&pwd, "\"");
  90. lub_string_cat(&pwd, str);
  91. lub_string_cat(&pwd, "\"");
  92. }
  93. return pwd;
  94. }
  95. /*--------------------------------------------------------- */
  96. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
  97. {
  98. if (index >= this->pwdc)
  99. return NULL;
  100. return this->pwdv[index]->view;
  101. }
  102. /*--------------------------------------------------------- */
  103. konf_client_t *clish_shell__get_client(const clish_shell_t * this)
  104. {
  105. return this->client;
  106. }
  107. /*--------------------------------------------------------- */
  108. void clish_shell__set_lockfile(clish_shell_t * this, const char * path)
  109. {
  110. if (!this)
  111. return;
  112. lub_string_free(this->lockfile);
  113. this->lockfile = NULL;
  114. if (path)
  115. this->lockfile = lub_string_dup(path);
  116. }
  117. /*--------------------------------------------------------- */
  118. char * clish_shell__get_lockfile(clish_shell_t * this)
  119. {
  120. if (!this)
  121. return NULL;
  122. return this->lockfile;
  123. }
  124. /*--------------------------------------------------------- */
  125. int clish_shell__set_socket(clish_shell_t * this, const char * path)
  126. {
  127. if (!this || !path)
  128. return -1;
  129. konf_client_free(this->client);
  130. this->client = konf_client_new(path);
  131. return 0;
  132. }