shell_pwd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * shell_pwd.c
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <syslog.h>
  8. #include "lub/string.h"
  9. #include "private.h"
  10. /*--------------------------------------------------------- */
  11. void clish_shell__init_pwd(clish_shell_pwd_t *pwd)
  12. {
  13. pwd->line = NULL;
  14. pwd->view = NULL;
  15. pwd->pargv = NULL;
  16. pwd->cmd = NULL;
  17. pwd->prefix = NULL;
  18. /* initialise the tree of vars */
  19. lub_bintree_init(&pwd->viewid,
  20. clish_var_bt_offset(),
  21. clish_var_bt_compare, clish_var_bt_getkey);
  22. }
  23. /*--------------------------------------------------------- */
  24. void clish_shell__fini_pwd(clish_shell_pwd_t *pwd)
  25. {
  26. clish_var_t *var;
  27. lub_string_free(pwd->line);
  28. lub_string_free(pwd->cmd);
  29. if (pwd->prefix)
  30. lub_string_free(pwd->prefix);
  31. pwd->view = NULL;
  32. clish_pargv_delete(pwd->pargv);
  33. /* delete each VAR held */
  34. while ((var = lub_bintree_findfirst(&pwd->viewid))) {
  35. lub_bintree_remove(&pwd->viewid, var);
  36. clish_var_delete(var);
  37. }
  38. }
  39. /*--------------------------------------------------------- */
  40. void clish_shell__set_pwd(clish_shell_t *this,
  41. const char *line, clish_view_t *view, char *viewid, clish_context_t *context)
  42. {
  43. clish_shell_pwd_t **tmp;
  44. size_t new_size = 0;
  45. unsigned int i;
  46. unsigned int index = clish_view__get_depth(view);
  47. clish_shell_pwd_t *newpwd;
  48. const clish_command_t *full_cmd = clish_context__get_cmd(context);
  49. /* Resize the pwd vector */
  50. if (index >= this->pwdc) {
  51. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  52. tmp = realloc(this->pwdv, new_size);
  53. assert(tmp);
  54. this->pwdv = tmp;
  55. /* Initialize new elements */
  56. for (i = this->pwdc; i <= index; i++) {
  57. clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
  58. assert(pwd);
  59. clish_shell__init_pwd(pwd);
  60. this->pwdv[i] = pwd;
  61. }
  62. this->pwdc = index + 1;
  63. }
  64. /* Create new element */
  65. newpwd = malloc(sizeof(*newpwd));
  66. assert(newpwd);
  67. clish_shell__init_pwd(newpwd);
  68. /* Fill the new pwd entry */
  69. newpwd->line = line ? lub_string_dup(line) : NULL;
  70. newpwd->view = view;
  71. newpwd->pargv = clish_pargv_clone(clish_context__get_pargv(context));
  72. if (full_cmd) {
  73. const clish_command_t *cmd = clish_command__get_cmd(full_cmd);
  74. newpwd->cmd = lub_string_dup(clish_command__get_name(cmd));
  75. if (cmd != full_cmd) {
  76. const char *full_cmd_name = clish_command__get_name(full_cmd);
  77. const char *cmd_name = clish_command__get_name(cmd);
  78. int len = strlen(full_cmd_name) - strlen(cmd_name);
  79. if (len > 1)
  80. newpwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
  81. }
  82. }
  83. clish_shell__expand_viewid(viewid, &newpwd->viewid, context);
  84. clish_shell__fini_pwd(this->pwdv[index]);
  85. free(this->pwdv[index]);
  86. this->pwdv[index] = newpwd;
  87. /* Cleanup intermadiate levels in a case of a long depth jump.
  88. For example jump from depth 0 to depth 3. The levels 1 and
  89. two must be cleaned up.
  90. */
  91. for (i = this->depth + 1; i < index; i++) {
  92. clish_shell__fini_pwd(this->pwdv[i]);
  93. clish_shell__init_pwd(this->pwdv[i]);
  94. }
  95. this->depth = index;
  96. }
  97. /*--------------------------------------------------------- */
  98. char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
  99. {
  100. if (index >= this->pwdc)
  101. return NULL;
  102. return this->pwdv[index]->line;
  103. }
  104. /*--------------------------------------------------------- */
  105. clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *this, unsigned int index)
  106. {
  107. if (index >= this->pwdc)
  108. return NULL;
  109. return this->pwdv[index]->pargv;
  110. }
  111. /*--------------------------------------------------------- */
  112. char *clish_shell__get_pwd_cmd(const clish_shell_t *this, unsigned int index)
  113. {
  114. if (index >= this->pwdc)
  115. return NULL;
  116. return this->pwdv[index]->cmd;
  117. }
  118. /*--------------------------------------------------------- */
  119. char *clish_shell__get_pwd_prefix(const clish_shell_t *this, unsigned int index)
  120. {
  121. if (index >= this->pwdc)
  122. return NULL;
  123. return this->pwdv[index]->prefix;
  124. }
  125. /*--------------------------------------------------------- */
  126. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
  127. {
  128. char *pwd = NULL;
  129. unsigned int i;
  130. for (i = 1; i <= depth; i++) {
  131. const char *str =
  132. clish_shell__get_pwd_line(this, i);
  133. if (!str)
  134. continue;
  135. if (pwd)
  136. lub_string_cat(&pwd, " ");
  137. lub_string_cat(&pwd, "\"");
  138. lub_string_cat(&pwd, str);
  139. lub_string_cat(&pwd, "\"");
  140. }
  141. return pwd;
  142. }
  143. /*--------------------------------------------------------- */
  144. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
  145. {
  146. if (index >= this->pwdc)
  147. return NULL;
  148. return this->pwdv[index]->view;
  149. }
  150. /*--------------------------------------------------------- */
  151. konf_client_t *clish_shell__get_client(const clish_shell_t * this)
  152. {
  153. return this->client;
  154. }
  155. /*--------------------------------------------------------- */
  156. void clish_shell__set_lockfile(clish_shell_t * this, const char * path)
  157. {
  158. if (!this)
  159. return;
  160. lub_string_free(this->lockfile);
  161. this->lockfile = NULL;
  162. if (path)
  163. this->lockfile = lub_string_dup(path);
  164. }
  165. /*--------------------------------------------------------- */
  166. char * clish_shell__get_lockfile(clish_shell_t * this)
  167. {
  168. if (!this)
  169. return NULL;
  170. return this->lockfile;
  171. }
  172. /*--------------------------------------------------------- */
  173. int clish_shell__set_socket(clish_shell_t * this, const char * path)
  174. {
  175. if (!this || !path)
  176. return -1;
  177. konf_client_free(this->client);
  178. this->client = konf_client_new(path);
  179. return 0;
  180. }
  181. /*--------------------------------------------------------- */
  182. void clish_shell__set_facility(clish_shell_t *this, int facility)
  183. {
  184. if (!this)
  185. return;
  186. this->log_facility = facility;
  187. }
  188. /*--------------------------------------------------------- */
  189. int clish_shell__get_facility(clish_shell_t *this)
  190. {
  191. if (!this)
  192. return LOG_LOCAL0;
  193. return this->log_facility;
  194. }