shell_pwd.c 4.4 KB

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