shell_pwd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /* Init VARs */
  19. pwd->viewid = lub_list_new(clish_var_compare, clish_var_delete);
  20. }
  21. /*--------------------------------------------------------- */
  22. void clish_shell__fini_pwd(clish_shell_pwd_t *pwd)
  23. {
  24. lub_string_free(pwd->line);
  25. lub_string_free(pwd->cmd);
  26. if (pwd->prefix)
  27. lub_string_free(pwd->prefix);
  28. pwd->view = NULL;
  29. clish_pargv_delete(pwd->pargv);
  30. /* Free VARs */
  31. lub_list_free_all(pwd->viewid);
  32. }
  33. /*--------------------------------------------------------- */
  34. void clish_shell__set_pwd(clish_shell_t *this,
  35. const char *line, clish_view_t *view, const 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. const clish_command_t *full_cmd = clish_context__get_cmd(context);
  43. /* Create new element */
  44. newpwd = malloc(sizeof(*newpwd));
  45. assert(newpwd);
  46. clish_shell__init_pwd(newpwd);
  47. /* Resize the pwd vector */
  48. if (index >= this->pwdc) {
  49. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  50. tmp = realloc(this->pwdv, new_size);
  51. assert(tmp);
  52. this->pwdv = tmp;
  53. /* Initialize new elements */
  54. for (i = this->pwdc; i <= index; i++) {
  55. clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
  56. assert(pwd);
  57. clish_shell__init_pwd(pwd);
  58. this->pwdv[i] = pwd;
  59. }
  60. this->pwdc = index + 1;
  61. }
  62. /* Fill the new pwd entry */
  63. newpwd->line = line ? lub_string_dup(line) : NULL;
  64. newpwd->view = view;
  65. newpwd->pargv = clish_pargv_clone(clish_context__get_pargv(context));
  66. if (full_cmd) {
  67. const clish_command_t *cmd = clish_command__get_cmd(full_cmd);
  68. newpwd->cmd = lub_string_dup(clish_command__get_name(cmd));
  69. if (cmd != full_cmd) {
  70. const char *full_cmd_name = clish_command__get_name(full_cmd);
  71. const char *cmd_name = clish_command__get_name(cmd);
  72. int len = strlen(full_cmd_name) - strlen(cmd_name);
  73. if (len > 1)
  74. newpwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
  75. }
  76. }
  77. clish_shell__expand_viewid(viewid, newpwd->viewid, context);
  78. clish_shell__fini_pwd(this->pwdv[index]);
  79. free(this->pwdv[index]);
  80. this->pwdv[index] = newpwd;
  81. this->depth = index;
  82. }
  83. /*--------------------------------------------------------- */
  84. char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
  85. {
  86. if (index >= this->pwdc)
  87. return NULL;
  88. return this->pwdv[index]->line;
  89. }
  90. /*--------------------------------------------------------- */
  91. clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *this, unsigned int index)
  92. {
  93. if (index >= this->pwdc)
  94. return NULL;
  95. return this->pwdv[index]->pargv;
  96. }
  97. /*--------------------------------------------------------- */
  98. char *clish_shell__get_pwd_cmd(const clish_shell_t *this, unsigned int index)
  99. {
  100. if (index >= this->pwdc)
  101. return NULL;
  102. return this->pwdv[index]->cmd;
  103. }
  104. /*--------------------------------------------------------- */
  105. char *clish_shell__get_pwd_prefix(const clish_shell_t *this, unsigned int index)
  106. {
  107. if (index >= this->pwdc)
  108. return NULL;
  109. return this->pwdv[index]->prefix;
  110. }
  111. /*--------------------------------------------------------- */
  112. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
  113. {
  114. char *pwd = NULL;
  115. unsigned int i;
  116. for (i = 1; i <= depth; i++) {
  117. const char *str =
  118. clish_shell__get_pwd_line(this, i);
  119. /* Cannot get full path */
  120. if (!str) {
  121. lub_string_free(pwd);
  122. return NULL;
  123. }
  124. if (pwd)
  125. lub_string_cat(&pwd, " ");
  126. lub_string_cat(&pwd, "\"");
  127. lub_string_cat(&pwd, str);
  128. lub_string_cat(&pwd, "\"");
  129. }
  130. return pwd;
  131. }
  132. /*--------------------------------------------------------- */
  133. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
  134. {
  135. if (index >= this->pwdc)
  136. return NULL;
  137. return this->pwdv[index]->view;
  138. }
  139. /*--------------------------------------------------------- */
  140. int clish_shell__set_socket(clish_shell_t * this, const char * path)
  141. {
  142. if (!this || !path)
  143. return -1;
  144. konf_client_free(this->client);
  145. this->client = konf_client_new(path);
  146. return 0;
  147. }
  148. CLISH_SET_STR(shell, lockfile);
  149. CLISH_GET_STR(shell, lockfile);
  150. CLISH_SET(shell, int, log_facility);
  151. CLISH_GET(shell, int, log_facility);
  152. CLISH_GET(shell, konf_client_t *, client);