gdb_tilde_expand.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Perform tilde expansion on paths for GDB and gdbserver.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include <algorithm>
  16. #include "filenames.h"
  17. #include "gdb_tilde_expand.h"
  18. #include <glob.h>
  19. /* RAII-style class wrapping "glob". */
  20. class gdb_glob
  21. {
  22. public:
  23. /* Construct a "gdb_glob" object by calling "glob" with the provided
  24. parameters. This function can throw if "glob" fails. */
  25. gdb_glob (const char *pattern, int flags,
  26. int (*errfunc) (const char *epath, int eerrno))
  27. {
  28. int ret = glob (pattern, flags, errfunc, &m_glob);
  29. if (ret != 0)
  30. {
  31. if (ret == GLOB_NOMATCH)
  32. error (_("Could not find a match for '%s'."), pattern);
  33. else
  34. error (_("glob could not process pattern '%s'."),
  35. pattern);
  36. }
  37. }
  38. /* Destroy the object and free M_GLOB. */
  39. ~gdb_glob ()
  40. {
  41. globfree (&m_glob);
  42. }
  43. /* Return the GL_PATHC component of M_GLOB. */
  44. int pathc () const
  45. {
  46. return m_glob.gl_pathc;
  47. }
  48. /* Return the GL_PATHV component of M_GLOB. */
  49. char **pathv () const
  50. {
  51. return m_glob.gl_pathv;
  52. }
  53. private:
  54. /* The actual glob object we're dealing with. */
  55. glob_t m_glob;
  56. };
  57. /* See gdbsupport/gdb_tilde_expand.h. */
  58. std::string
  59. gdb_tilde_expand (const char *dir)
  60. {
  61. if (dir[0] != '~')
  62. return std::string (dir);
  63. /* This function uses glob in order to expand the ~. However, this function
  64. will fail to expand if the actual dir we are looking for does not exist.
  65. Given "~/does/not/exist", glob will fail.
  66. In order to avoid such limitation, we only use glob to expand "~" and keep
  67. "/does/not/exist" unchanged.
  68. Similarly, to expand ~gdb/might/not/exist, we only expand "~gdb" using
  69. glob and leave "/might/not/exist" unchanged. */
  70. const std::string d (dir);
  71. /* Look for the first dir separator (if any) and split d around it. */
  72. const auto first_sep
  73. = std::find_if (d.cbegin (), d.cend(),
  74. [] (const char c) -> bool
  75. {
  76. return IS_DIR_SEPARATOR (c);
  77. });
  78. const std::string to_expand (d.cbegin (), first_sep);
  79. const std::string remainder (first_sep, d.cend ());
  80. const gdb_glob glob (to_expand.c_str (), GLOB_TILDE_CHECK, nullptr);
  81. gdb_assert (glob.pathc () == 1);
  82. return std::string (glob.pathv ()[0]) + remainder;
  83. }
  84. /* See gdbsupport/gdb_tilde_expand.h. */
  85. gdb::unique_xmalloc_ptr<char>
  86. gdb_tilde_expand_up (const char *dir)
  87. {
  88. const std::string expanded = gdb_tilde_expand (dir);
  89. return make_unique_xstrdup (expanded.c_str ());
  90. }