shell_pwd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * shell_pwd.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include "lub/string.h"
  7. #include "private.h"
  8. /*--------------------------------------------------------- */
  9. void
  10. clish_shell__set_pwd(clish_shell_t * this, unsigned index,
  11. const char * line, clish_view_t * view, char * viewid)
  12. {
  13. clish_shell_pwd_t **tmp;
  14. size_t new_size = 0;
  15. unsigned i;
  16. /* Create new element */
  17. if (index >= this->cfg_pwdc) {
  18. new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
  19. /* resize the pwd vector */
  20. tmp = realloc(this->cfg_pwdv, new_size);
  21. assert(tmp);
  22. this->cfg_pwdv = tmp;
  23. /* Initialize new elements */
  24. for (i = this->cfg_pwdc; i <= index; i++) {
  25. clish_shell_pwd_t *pwd;
  26. pwd = malloc(sizeof(*pwd));
  27. assert(pwd);
  28. pwd->line = NULL;
  29. pwd->view = NULL;
  30. pwd->viewid = NULL;
  31. this->cfg_pwdv[i] = pwd;
  32. }
  33. this->cfg_pwdc = index + 1;
  34. }
  35. lub_string_free(this->cfg_pwdv[index]->line);
  36. this->cfg_pwdv[index]->line = line ? lub_string_dup(line) : NULL;
  37. this->cfg_pwdv[index]->view = view;
  38. lub_string_free(this->cfg_pwdv[index]->viewid);
  39. this->cfg_pwdv[index]->viewid = viewid ? lub_string_dup(viewid) : NULL;
  40. }
  41. /*--------------------------------------------------------- */
  42. char *clish_shell__get_pwd_line(const clish_shell_t * this, unsigned index)
  43. {
  44. if (index >= this->cfg_pwdc)
  45. return NULL;
  46. return this->cfg_pwdv[index]->line;
  47. }
  48. /*--------------------------------------------------------- */
  49. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned depth)
  50. {
  51. char *pwd = NULL;
  52. unsigned i;
  53. for (i = 0; i < depth; i++) {
  54. const char *str =
  55. clish_shell__get_pwd_line(this, i);
  56. /* Cannot get full path */
  57. if (!str) {
  58. lub_string_free(pwd);
  59. return NULL;
  60. }
  61. if (pwd)
  62. lub_string_cat(&pwd, " ");
  63. lub_string_cat(&pwd, "\"");
  64. lub_string_cat(&pwd, str);
  65. lub_string_cat(&pwd, "\"");
  66. }
  67. return pwd;
  68. }
  69. /*--------------------------------------------------------- */
  70. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned index)
  71. {
  72. if (index >= this->cfg_pwdc)
  73. return NULL;
  74. return this->cfg_pwdv[index]->view;
  75. }
  76. /*--------------------------------------------------------- */
  77. char *clish_shell__get_pwd_viewid(const clish_shell_t * this, unsigned index)
  78. {
  79. if (index >= this->cfg_pwdc)
  80. return NULL;
  81. return this->cfg_pwdv[index]->viewid;
  82. }
  83. /*--------------------------------------------------------- */
  84. konf_client_t *clish_shell__get_client(const clish_shell_t * this)
  85. {
  86. return this->client;
  87. }
  88. /*--------------------------------------------------------- */
  89. void clish_shell__set_lockfile(clish_shell_t * this, const char * path)
  90. {
  91. if (!this)
  92. return;
  93. lub_string_free(this->lockfile);
  94. this->lockfile = NULL;
  95. if (path)
  96. this->lockfile = lub_string_dup(path);
  97. }
  98. /*--------------------------------------------------------- */
  99. char * clish_shell__get_lockfile(clish_shell_t * this)
  100. {
  101. if (!this)
  102. return NULL;
  103. return this->lockfile;
  104. }