GDB (xrefs)

mi-cmd-env.c
Go to the documentation of this file.
1 /* MI Command Set - environment commands.
2 Copyright (C) 2002-2018 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 "environ.h"
30 #include "command.h"
31 #include "ui-out.h"
32 #include "top.h"
33 #include <sys/stat.h>
34
35 static const char path_var_name[] = "PATH";
36 static char *orig_path = NULL;
37
38 /* The following is copied from mi-main.c so for m1 and below we can
39 perform old behavior and use cli commands. If ARGS is non-null,
40 append it to the CMD. */
41
42 static void
43 env_execute_cli_command (const char *cmd, const char *args)
44 {
45 if (cmd != 0)
46 {
47 gdb::unique_xmalloc_ptr<char> run;
48
49 if (args != NULL)
50 run.reset (xstrprintf ("%s %s", cmd, args));
51 else
52 run.reset (xstrdup (cmd));
53 execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
54 }
55 }
56
57 /* Print working directory. */
58
59 void
60 mi_cmd_env_pwd (const char *command, char **argv, int argc)
61 {
62 struct ui_out *uiout = current_uiout;
63
64 if (argc > 0)
65 error (_("-environment-pwd: No arguments allowed"));
66
67 if (mi_version (uiout) < 2)
68 {
69 env_execute_cli_command ("pwd", NULL);
70 return;
71 }
72
73 /* Otherwise the mi level is 2 or higher. */
74
75 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
76 if (cwd == NULL)
77 error (_("-environment-pwd: error finding name of working directory: %s"),
78 safe_strerror (errno));
79
80 uiout->field_string ("cwd", cwd.get ());
81 }
82
83 /* Change working directory. */
84
85 void
86 mi_cmd_env_cd (const char *command, char **argv, int argc)
87 {
88 if (argc == 0 || argc > 1)
89 error (_("-environment-cd: Usage DIRECTORY"));
90
91 env_execute_cli_command ("cd", argv[0]);
92 }
93
94 static void
95 env_mod_path (const char *dirname, char **which_path)
96 {
97 if (dirname == 0 || dirname[0] == '\0')
98 return;
99
100 /* Call add_path with last arg 0 to indicate not to parse for
101 separator characters. */
102 add_path (dirname, which_path, 0);
103 }
104
105 /* Add one or more directories to start of executable search path. */
106
107 void
108 mi_cmd_env_path (const char *command, char **argv, int argc)
109 {
110 struct ui_out *uiout = current_uiout;
111 char *exec_path;
112 const char *env;
113 int reset = 0;
114 int oind = 0;
115 int i;
116 char *oarg;
117 enum opt
118 {
119 RESET_OPT
120 };
121 static const struct mi_opt opts[] =
122 {
123 {"r", RESET_OPT, 0},
124 { 0, 0, 0 }
125 };
126
127 dont_repeat ();
128
129 if (mi_version (uiout) < 2)
130 {
131 for (i = argc - 1; i >= 0; --i)
132 env_execute_cli_command ("path", argv[i]);
133 return;
134 }
135
136 /* Otherwise the mi level is 2 or higher. */
137 while (1)
138 {
139 int opt = mi_getopt ("-environment-path", argc, argv, opts,
140 &oind, &oarg);
141
142 if (opt < 0)
143 break;
144 switch ((enum opt) opt)
145 {
146 case RESET_OPT:
147 reset = 1;
148 break;
149 }
150 }
151 argv += oind;
152 argc -= oind;
153
154
155 if (reset)
156 {
157 /* Reset implies resetting to original path first. */
158 exec_path = xstrdup (orig_path);
159 }
160 else
161 {
162 /* Otherwise, get current path to modify. */
163 env = current_inferior ()->environment.get (path_var_name);
164
165 /* Can be null if path is not set. */
166 if (!env)
167 env = "";
168 exec_path = xstrdup (env);
169 }
170
171 for (i = argc - 1; i >= 0; --i)
172 env_mod_path (argv[i], &exec_path);
173
174 current_inferior ()->environment.set (path_var_name, exec_path);
175 xfree (exec_path);
176 env = current_inferior ()->environment.get (path_var_name);
177 uiout->field_string ("path", env);
178 }
179
180 /* Add zero or more directories to the front of the source path. */
181
182 void
183 mi_cmd_env_dir (const char *command, char **argv, int argc)
184 {
185 struct ui_out *uiout = current_uiout;
186 int i;
187 int oind = 0;
188 int reset = 0;
189 char *oarg;
190 enum opt
191 {
192 RESET_OPT
193 };
194 static const struct mi_opt opts[] =
195 {
196 {"r", RESET_OPT, 0},
197 { 0, 0, 0 }
198 };
199
200 dont_repeat ();
201
202 if (mi_version (uiout) < 2)
203 {
204 for (i = argc - 1; i >= 0; --i)
205 env_execute_cli_command ("dir", argv[i]);
206 return;
207 }
208
209 /* Otherwise mi level is 2 or higher. */
210 while (1)
211 {
212 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
213 &oind, &oarg);
214
215 if (opt < 0)
216 break;
217 switch ((enum opt) opt)
218 {
219 case RESET_OPT:
220 reset = 1;
221 break;
222 }
223 }
224 argv += oind;
225 argc -= oind;
226
227 if (reset)
228 {
229 /* Reset means setting to default path first. */
230 xfree (source_path);
231 init_source_path ();
232 }
233
234 for (i = argc - 1; i >= 0; --i)
235 env_mod_path (argv[i], &source_path);
236
237 uiout->field_string ("source-path", source_path);
238 forget_cached_source_info ();
239 }
240
241 /* Set the inferior terminal device name. */
242
243 void
244 mi_cmd_inferior_tty_set (const char *command, char **argv, int argc)
245 {
246 set_inferior_io_terminal (argv[0]);
247 }
248
249 /* Print the inferior terminal device name. */
250
251 void
252 mi_cmd_inferior_tty_show (const char *command, char **argv, int argc)
253 {
254 const char *inferior_io_terminal = get_inferior_io_terminal ();
255
256 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
257 error (_("-inferior-tty-show: Usage: No args"));
258
259 if (inferior_io_terminal)
260 current_uiout->field_string ("inferior_tty_terminal", inferior_io_terminal);
261 }
262
263 void
264 _initialize_mi_cmd_env (void)
265 {
266 const char *env;
267
268 /* We want original execution path to reset to, if desired later.
269 At this point, current inferior is not created, so cannot use
270 current_inferior ()->environment. We use getenv here because it
271 is not necessary to create a whole new gdb_environ just for one
272 variable. */
273 env = getenv (path_var_name);
274
275 /* Can be null if path is not set. */
276 if (!env)
277 env = "";
278 orig_path = xstrdup (env);
279 }
top.h
gdb_environ::get
const char * get(const char *var) const
Definition: environ.c:92
selftests::reset
void reset()
add_path
void add_path(const char *, char **, int)
Definition: source.c:460
ui_out::field_string
void field_string(const char *fldname, const char *string)
Definition: ui-out.c:544
command.h
xfree
void xfree(void *)
mi-cmds.h
gdb_environ::set
void set(const char *var, const char *value)
Definition: environ.c:106
env_mod_path
static void env_mod_path(const char *dirname, char **which_path)
Definition: mi-cmd-env.c:95
set_inferior_io_terminal
void set_inferior_io_terminal(const char *terminal_name)
Definition: infcmd.c:120
mi_cmd_env_dir
void mi_cmd_env_dir(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:183
mi_cmd_env_path
void mi_cmd_env_path(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:108
_
#define _(String)
Definition: gdb_locale.h:35
defs.h
mi_cmd_inferior_tty_show
void mi_cmd_inferior_tty_show(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:252
execute_command
void execute_command(const char *, int)
Definition: top.c:540
init_source_path
void init_source_path(void)
Definition: source.c:403
symtab.h
mi_getopt
int mi_getopt(const char *prefix, int argc, char **argv, const struct mi_opt *opts, int *oind, char **oarg)
Definition: mi-getopt.c:83
gdb::unique_xmalloc_ptr
std::unique_ptr< T, xfree_deleter< T > > unique_xmalloc_ptr
Definition: gdb_unique_ptr.h:48
get_inferior_io_terminal
const char * get_inferior_io_terminal(void)
Definition: infcmd.c:131
mi_version
int mi_version(ui_out *uiout)
Definition: mi-out.c:311
current_uiout
#define current_uiout
Definition: ui-out.h:39
inferior.h
source_path
char * source_path
Definition: source.c:58
env_execute_cli_command
static void env_execute_cli_command(const char *cmd, const char *args)
Definition: mi-cmd-env.c:43
xstrprintf
char * xstrprintf(const char *format,...)
Definition: common-utils.c:107
mi_valid_noargs
int mi_valid_noargs(const char *prefix, int argc, char **argv)
Definition: mi-getopt.c:99
mi_cmd_env_cd
void mi_cmd_env_cd(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:86
run
static procfs_run run
Definition: nto-procfs.c:55
environ.h
inferior::environment
gdb_environ environment
Definition: inferior.h:372
_initialize_mi_cmd_env
void _initialize_mi_cmd_env(void)
Definition: mi-cmd-env.c:264
ui_out
Definition: ui-out.h:77
orig_path
static char * orig_path
Definition: mi-cmd-env.c:36
safe_strerror
char * safe_strerror(int)
Definition: mingw-strerror.c:32
mi_cmd_inferior_tty_set
void mi_cmd_inferior_tty_set(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:244
forget_cached_source_info
void forget_cached_source_info(void)
Definition: source.c:388
target.h
value.h
current_inferior
struct inferior * current_inferior(void)
Definition: inferior.c:58
path_var_name
static const char path_var_name[]
Definition: mi-cmd-env.c:35
gdb.argv
argv
Definition: __init__.py:63
dont_repeat
void dont_repeat(void)
Definition: top.c:695
mi-getopt.h
error
void error(const char *fmt,...)
Definition: errors.c:38
mi_opt
Definition: mi-getopt.h:26
mi_cmd_env_pwd
void mi_cmd_env_pwd(const char *command, char **argv, int argc)
Definition: mi-cmd-env.c:60
mi-out.h
ui-out.h