gdb/riscv: Use legacy register numbers in default target description
[external/binutils.git] / gdb / common / gdb_tilde_expand.c
1 /* Perform tilde expansion on paths for GDB and gdbserver.
2
3    Copyright (C) 2017-2019 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 }
83
84 /* See common/gdb_tilde_expand.h.  */
85
86 gdb::unique_xmalloc_ptr<char>
87 gdb_tilde_expand_up (const char *dir)
88 {
89   gdb_glob glob (dir, GLOB_TILDE_CHECK, NULL);
90
91   gdb_assert (glob.pathc () > 0);
92   /* "glob" may return more than one match to the path provided by the
93      user, but we are only interested in the first match.  */
94   return gdb::unique_xmalloc_ptr<char> (xstrdup (glob.pathv ()[0]));
95 }