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