1
0

shell_pwd.c 4.0 KB

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