shell_pwd.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. char *clish_shell__get_pwd_line(const clish_shell_t * this, unsigned index)
  42. {
  43. if (index >= this->cfg_pwdc)
  44. return NULL;
  45. return this->cfg_pwdv[index]->line;
  46. }
  47. char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned depth)
  48. {
  49. char *pwd = NULL;
  50. unsigned i;
  51. for (i = 0; i < depth; i++) {
  52. const char *str =
  53. clish_shell__get_pwd_line(this, i);
  54. /* Cannot get full path */
  55. if (!str) {
  56. lub_string_free(pwd);
  57. return NULL;
  58. }
  59. if (pwd)
  60. lub_string_cat(&pwd, " ");
  61. lub_string_cat(&pwd, "\"");
  62. lub_string_cat(&pwd, str);
  63. lub_string_cat(&pwd, "\"");
  64. }
  65. return pwd;
  66. }
  67. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned index)
  68. {
  69. if (index >= this->cfg_pwdc)
  70. return NULL;
  71. return this->cfg_pwdv[index]->view;
  72. }
  73. char *clish_shell__get_pwd_viewid(const clish_shell_t * this, unsigned index)
  74. {
  75. if (index >= this->cfg_pwdc)
  76. return NULL;
  77. return this->cfg_pwdv[index]->viewid;
  78. }
  79. konf_client_t *clish_shell__get_client(const clish_shell_t * this)
  80. {
  81. return this->client;
  82. }