1
0

shell_pwd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, const 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. /* Create new element */
  50. newpwd = malloc(sizeof(*newpwd));
  51. assert(newpwd);
  52. clish_shell__init_pwd(newpwd);
  53. /* Resize the pwd vector */
  54. if (index >= this->pwdc) {
  55. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  56. tmp = realloc(this->pwdv, new_size);
  57. assert(tmp);
  58. this->pwdv = tmp;
  59. /* Initialize new elements */
  60. for (i = this->pwdc; i <= index; i++) {
  61. clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
  62. assert(pwd);
  63. clish_shell__init_pwd(pwd);
  64. this->pwdv[i] = pwd;
  65. }
  66. this->pwdc = index + 1;
  67. }
  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. this->depth = index;
  88. }
  89. /*--------------------------------------------------------- */
  90. char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
  91. {
  92. if (index >= this->pwdc)
  93. return NULL;
  94. return this->pwdv[index]->line;
  95. }
  96. /*--------------------------------------------------------- */
  97. clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *this, unsigned int index)
  98. {
  99. if (index >= this->pwdc)
  100. return NULL;
  101. return this->pwdv[index]->pargv;
  102. }
  103. /*--------------------------------------------------------- */
  104. char *clish_shell__get_pwd_cmd(const clish_shell_t *this, unsigned int index)
  105. {
  106. if (index >= this->pwdc)
  107. return NULL;
  108. return this->pwdv[index]->cmd;
  109. }
  110. /*--------------------------------------------------------- */
  111. char *clish_shell__get_pwd_prefix(const clish_shell_t *this, unsigned int index)
  112. {
  113. if (index >= this->pwdc)
  114. return NULL;
  115. return this->pwdv[index]->prefix;
  116. }
  117. /*--------------------------------------------------------- */
  118. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
  119. {
  120. char *pwd = NULL;
  121. unsigned int i;
  122. for (i = 1; i <= depth; i++) {
  123. const char *str =
  124. clish_shell__get_pwd_line(this, i);
  125. /* Cannot get full path */
  126. if (!str) {
  127. lub_string_free(pwd);
  128. return NULL;
  129. }
  130. if (pwd)
  131. lub_string_cat(&pwd, " ");
  132. lub_string_cat(&pwd, "\"");
  133. lub_string_cat(&pwd, str);
  134. lub_string_cat(&pwd, "\"");
  135. }
  136. return pwd;
  137. }
  138. /*--------------------------------------------------------- */
  139. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
  140. {
  141. if (index >= this->pwdc)
  142. return NULL;
  143. return this->pwdv[index]->view;
  144. }
  145. /*--------------------------------------------------------- */
  146. int clish_shell__set_socket(clish_shell_t * this, const char * path)
  147. {
  148. if (!this || !path)
  149. return -1;
  150. konf_client_free(this->client);
  151. this->client = konf_client_new(path);
  152. return 0;
  153. }
  154. CLISH_SET_STR(shell, lockfile);
  155. CLISH_GET_STR(shell, lockfile);
  156. CLISH_SET(shell, int, log_facility);
  157. CLISH_GET(shell, int, log_facility);
  158. CLISH_GET(shell, konf_client_t *, client);