GDB (xrefs)
Loading...
Searching...
No Matches
mi-cmd-env.c
Go to the documentation of this file.
1/* MI Command Set - environment commands.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3
4 Contributed by Red Hat Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22#include "inferior.h"
23#include "value.h"
24#include "mi-out.h"
25#include "mi-cmds.h"
26#include "mi-getopt.h"
27#include "symtab.h"
28#include "target.h"
29#include "gdbsupport/environ.h"
30#include "command.h"
31#include "ui-out.h"
32#include "top.h"
33#include <sys/stat.h>
34#include "source.h"
35
36static const char path_var_name[] = "PATH";
37static char *orig_path = NULL;
38
39/* The following is copied from mi-main.c so for m1 and below we can
40 perform old behavior and use cli commands. If ARGS is non-null,
41 append it to the CMD. */
42
43static void
44env_execute_cli_command (const char *cmd, const char *args)
45{
46 if (cmd != 0)
47 {
48 gdb::unique_xmalloc_ptr<char> run;
49
50 if (args != NULL)
51 run = xstrprintf ("%s %s", cmd, args);
52 else
53 run.reset (xstrdup (cmd));
54 execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
55 }
56}
57
58/* Print working directory. */
59
60void
61mi_cmd_env_pwd (const char *command, const char *const *argv, int argc)
62{
63 struct ui_out *uiout = current_uiout;
64
65 if (argc > 0)
66 error (_("-environment-pwd: No arguments allowed"));
67
68 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
69 if (cwd == NULL)
70 error (_("-environment-pwd: error finding name of working directory: %s"),
71 safe_strerror (errno));
72
73 uiout->field_string ("cwd", cwd.get ());
74}
75
76/* Change working directory. */
77
78void
79mi_cmd_env_cd (const char *command, const char *const *argv, int argc)
80{
81 if (argc == 0 || argc > 1)
82 error (_("-environment-cd: Usage DIRECTORY"));
83
84 env_execute_cli_command ("cd", argv[0]);
85}
86
87static void
88env_mod_path (const char *dirname, std::string &which_path)
89{
90 if (dirname == 0 || dirname[0] == '\0')
91 return;
92
93 /* Call add_path with last arg 0 to indicate not to parse for
94 separator characters. */
95 add_path (dirname, which_path, 0);
96}
97
98/* Add one or more directories to start of executable search path. */
99
100void
101mi_cmd_env_path (const char *command, const char *const *argv, int argc)
102{
103 struct ui_out *uiout = current_uiout;
104 const char *env;
105 int reset = 0;
106 int oind = 0;
107 int i;
108 const char *oarg;
109 enum opt
110 {
111 RESET_OPT
112 };
113 static const struct mi_opt opts[] =
114 {
115 {"r", RESET_OPT, 0},
116 { 0, 0, 0 }
117 };
118
119 dont_repeat ();
120
121 while (1)
122 {
123 int opt = mi_getopt ("-environment-path", argc, argv, opts,
124 &oind, &oarg);
125
126 if (opt < 0)
127 break;
128 switch ((enum opt) opt)
129 {
130 case RESET_OPT:
131 reset = 1;
132 break;
133 }
134 }
135 argv += oind;
136 argc -= oind;
137
138 std::string exec_path;
139 if (reset)
140 {
141 /* Reset implies resetting to original path first. */
142 exec_path = orig_path;
143 }
144 else
145 {
146 /* Otherwise, get current path to modify. */
148
149 /* Can be null if path is not set. */
150 if (!env)
151 env = "";
152
153 exec_path = env;
154 }
155
156 for (i = argc - 1; i >= 0; --i)
157 env_mod_path (argv[i], exec_path);
158
159 current_inferior ()->environment.set (path_var_name, exec_path.c_str ());
161 uiout->field_string ("path", env);
162}
163
164/* Add zero or more directories to the front of the source path. */
165
166void
167mi_cmd_env_dir (const char *command, const char *const *argv, int argc)
168{
169 struct ui_out *uiout = current_uiout;
170 int i;
171 int oind = 0;
172 int reset = 0;
173 const char *oarg;
174 enum opt
175 {
176 RESET_OPT
177 };
178 static const struct mi_opt opts[] =
179 {
180 {"r", RESET_OPT, 0},
181 { 0, 0, 0 }
182 };
183
184 dont_repeat ();
185
186 while (1)
187 {
188 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
189 &oind, &oarg);
190
191 if (opt < 0)
192 break;
193 switch ((enum opt) opt)
194 {
195 case RESET_OPT:
196 reset = 1;
197 break;
198 }
199 }
200 argv += oind;
201 argc -= oind;
202
203 if (reset)
204 {
205 /* Reset means setting to default path first. */
207 }
208
209 for (i = argc - 1; i >= 0; --i)
210 env_mod_path (argv[i], source_path);
211
212 uiout->field_string ("source-path", source_path);
214}
215
216/* Set the inferior terminal device name. */
217
218void
219mi_cmd_inferior_tty_set (const char *command, const char *const *argv,
220 int argc)
221{
222 if (argc > 0)
223 current_inferior ()->set_tty (argv[0]);
224 else
225 current_inferior ()->set_tty ("");
226}
227
228/* Print the inferior terminal device name. */
229
230void
231mi_cmd_inferior_tty_show (const char *command, const char *const *argv,
232 int argc)
233{
234 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
235 error (_("-inferior-tty-show: Usage: No args"));
236
237 const std::string &inferior_tty = current_inferior ()->tty ();
238 if (!inferior_tty.empty ())
239 current_uiout->field_string ("inferior_tty_terminal", inferior_tty);
240}
241
243void
245{
246 const char *env;
247
248 /* We want original execution path to reset to, if desired later.
249 At this point, current inferior is not created, so cannot use
250 current_inferior ()->environment. We use getenv here because it
251 is not necessary to create a whole new gdb_environ just for one
252 variable. */
253 env = getenv (path_var_name);
254
255 /* Can be null if path is not set. */
256 if (!env)
257 env = "";
258 orig_path = xstrdup (env);
259}
gdb_environ environment
Definition inferior.h:590
const std::string & tty()
Definition inferior.c:163
void set_tty(std::string terminal_name)
Definition inferior.c:157
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void dont_repeat()
Definition top.c:696
void execute_command(const char *, int)
Definition top.c:459
struct inferior * current_inferior(void)
Definition inferior.c:55
static void env_mod_path(const char *dirname, std::string &which_path)
Definition mi-cmd-env.c:88
static void env_execute_cli_command(const char *cmd, const char *args)
Definition mi-cmd-env.c:44
void _initialize_mi_cmd_env()
Definition mi-cmd-env.c:244
static char * orig_path
Definition mi-cmd-env.c:37
static const char path_var_name[]
Definition mi-cmd-env.c:36
mi_cmd_argv_ftype mi_cmd_inferior_tty_show
mi_cmd_argv_ftype mi_cmd_inferior_tty_set
mi_cmd_argv_ftype mi_cmd_env_cd
mi_cmd_argv_ftype mi_cmd_env_path
mi_cmd_argv_ftype mi_cmd_env_pwd
mi_cmd_argv_ftype mi_cmd_env_dir
int mi_getopt(const char *prefix, int argc, const char *const *argv, const struct mi_opt *opts, int *oind, const char **oarg)
Definition mi-getopt.c:82
int mi_valid_noargs(const char *prefix, int argc, const char *const *argv)
Definition mi-getopt.c:100
static procfs_run run
Definition nto-procfs.c:56
void forget_cached_source_info(void)
Definition source.c:417
void init_source_path(void)
Definition source.c:428
void add_path(const char *dirname, char **which_path, int parse_separators)
Definition source.c:489
std::string source_path
Definition source.c:62
#define current_uiout
Definition ui-out.h:40