shell_cwd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * shell_cwd.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_cwd(clish_shell_cwd_t *cwd)
  12. {
  13. cwd->line = NULL;
  14. cwd->view = NULL;
  15. cwd->pargv = NULL;
  16. cwd->cmd = NULL;
  17. cwd->prefix = NULL;
  18. /* Init VARs */
  19. cwd->viewid = lub_list_new(clish_var_compare, clish_var_delete);
  20. }
  21. /*--------------------------------------------------------- */
  22. void clish_shell__fini_cwd(clish_shell_cwd_t *cwd)
  23. {
  24. lub_string_free(cwd->line);
  25. lub_string_free(cwd->cmd);
  26. if (cwd->prefix)
  27. lub_string_free(cwd->prefix);
  28. cwd->view = NULL;
  29. clish_pargv_delete(cwd->pargv);
  30. /* Free VARs */
  31. lub_list_free_all(cwd->viewid);
  32. }
  33. /*--------------------------------------------------------- */
  34. void clish_shell__set_cwd(clish_shell_t *this,
  35. const char *line, clish_view_t *view, const char *viewid, clish_context_t *context)
  36. {
  37. clish_shell_cwd_t **tmp;
  38. size_t new_size = 0;
  39. unsigned int i;
  40. unsigned int index = clish_view__get_depth(view);
  41. clish_shell_cwd_t *newcwd;
  42. const clish_command_t *full_cmd = clish_context__get_cmd(context);
  43. /* Create new element */
  44. newcwd = malloc(sizeof(*newcwd));
  45. assert(newcwd);
  46. clish_shell__init_cwd(newcwd);
  47. /* Resize the cwd vector */
  48. if (index >= this->cwdc) {
  49. new_size = (index + 1) * sizeof(clish_shell_cwd_t *);
  50. tmp = realloc(this->cwdv, new_size);
  51. assert(tmp);
  52. this->cwdv = tmp;
  53. /* Initialize new elements */
  54. for (i = this->cwdc; i <= index; i++) {
  55. clish_shell_cwd_t *cwd = malloc(sizeof(*cwd));
  56. assert(cwd);
  57. clish_shell__init_cwd(cwd);
  58. this->cwdv[i] = cwd;
  59. }
  60. this->cwdc = index + 1;
  61. }
  62. /* Fill the new cwd entry */
  63. newcwd->line = line ? lub_string_dup(line) : NULL;
  64. newcwd->view = view;
  65. newcwd->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. newcwd->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. newcwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
  75. }
  76. }
  77. clish_shell__expand_viewid(viewid, newcwd->viewid, context);
  78. clish_shell__fini_cwd(this->cwdv[index]);
  79. free(this->cwdv[index]);
  80. this->cwdv[index] = newcwd;
  81. this->depth = index;
  82. }
  83. /*--------------------------------------------------------- */
  84. char *clish_shell__get_cwd_line(const clish_shell_t *this, unsigned int index)
  85. {
  86. if (index >= this->cwdc)
  87. return NULL;
  88. return this->cwdv[index]->line;
  89. }
  90. /*--------------------------------------------------------- */
  91. clish_pargv_t *clish_shell__get_cwd_pargv(const clish_shell_t *this, unsigned int index)
  92. {
  93. if (index >= this->cwdc)
  94. return NULL;
  95. return this->cwdv[index]->pargv;
  96. }
  97. /*--------------------------------------------------------- */
  98. char *clish_shell__get_cwd_cmd(const clish_shell_t *this, unsigned int index)
  99. {
  100. if (index >= this->cwdc)
  101. return NULL;
  102. return this->cwdv[index]->cmd;
  103. }
  104. /*--------------------------------------------------------- */
  105. char *clish_shell__get_cwd_prefix(const clish_shell_t *this, unsigned int index)
  106. {
  107. if (index >= this->cwdc)
  108. return NULL;
  109. return this->cwdv[index]->prefix;
  110. }
  111. /*--------------------------------------------------------- */
  112. char *clish_shell__get_cwd_full(const clish_shell_t * this, unsigned int depth)
  113. {
  114. char *cwd = NULL;
  115. unsigned int i;
  116. for (i = 1; i <= depth; i++) {
  117. const char *str =
  118. clish_shell__get_cwd_line(this, i);
  119. /* Cannot get full path */
  120. if (!str) {
  121. lub_string_free(cwd);
  122. return NULL;
  123. }
  124. if (cwd)
  125. lub_string_cat(&cwd, " ");
  126. lub_string_cat(&cwd, "\"");
  127. lub_string_cat(&cwd, str);
  128. lub_string_cat(&cwd, "\"");
  129. }
  130. return cwd;
  131. }
  132. /*--------------------------------------------------------- */
  133. clish_view_t *clish_shell__get_cwd_view(const clish_shell_t * this, unsigned int index)
  134. {
  135. if (index >= this->cwdc)
  136. return NULL;
  137. return this->cwdv[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);