GDB (xrefs)

gdb_tilde_expand.c
Go to the documentation of this file.
1 /* Perform tilde expansion on paths for GDB and gdbserver.
2
3 Copyright (C) 2017-2018 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 "common-defs.h"
21 #include "gdb_tilde_expand.h"
22 #include <glob.h>
23
24 /* RAII-style class wrapping "glob". */
25
26 class gdb_glob
27 {
28 public:
29 /* Construct a "gdb_glob" object by calling "glob" with the provided
30 parameters. This function can throw if "glob" fails. */
31 gdb_glob (const char *pattern, int flags,
32 int (*errfunc) (const char *epath, int eerrno))
33 {
34 int ret = glob (pattern, flags, errfunc, &m_glob);
35
36 if (ret != 0)
37 {
38 if (ret == GLOB_NOMATCH)
39 error (_("Could not find a match for '%s'."), pattern);
40 else
41 error (_("glob could not process pattern '%s'."),
42 pattern);
43 }
44 }
45
46 /* Destroy the object and free M_GLOB. */
47 ~gdb_glob ()
48 {
49 globfree (&m_glob);
50 }
51
52 /* Return the GL_PATHC component of M_GLOB. */
53 int pathc () const
54 {
55 return m_glob.gl_pathc;
56 }
57
58 /* Return the GL_PATHV component of M_GLOB. */
59 char **pathv () const
60 {
61 return m_glob.gl_pathv;
62 }
63
64 private:
65 /* The actual glob object we're dealing with. */
66 glob_t m_glob;
67 };
68
69 /* See common/gdb_tilde_expand.h. */
70
71 std::string
72 gdb_tilde_expand (const char *dir)
73 {
74 gdb_glob glob (dir, GLOB_TILDE_CHECK, NULL);
75
76 gdb_assert (glob.pathc () > 0);
77 /* "glob" may return more than one match to the path provided by the
78 user, but we are only interested in the first match. */
79 std::string expanded_dir = glob.pathv ()[0];
80
81 return expanded_dir;
82 }
string
const char * string
Definition: signals.c:50
gdb_glob
Definition: gdb_tilde_expand.c:26
_
#define _(String)
Definition: gdb_locale.h:35
gdb_glob::gdb_glob
gdb_glob(const char *pattern, int flags, int(*errfunc)(const char *epath, int eerrno))
Definition: gdb_tilde_expand.c:31
flags
mach_port_t kern_return_t mach_port_t msgports mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition: gnu-nat.c:1891
gdb_glob::m_glob
glob_t m_glob
Definition: gdb_tilde_expand.c:66
common-defs.h
gdb_glob::pathv
char ** pathv() const
Definition: gdb_tilde_expand.c:59
gdb_glob::pathc
int pathc() const
Definition: gdb_tilde_expand.c:53
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
gdb_tilde_expand.h
gdb_tilde_expand
std::string gdb_tilde_expand(const char *dir)
Definition: gdb_tilde_expand.c:72
gdb_glob::~gdb_glob
~gdb_glob()
Definition: gdb_tilde_expand.c:47
error
void error(const char *fmt,...)
Definition: errors.c:38