gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-env.c
CommitLineData
068890be 1/* MI Command Set - environment commands.
b811d2c2 2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
1bac305b 3
068890be
JJ
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
068890be
JJ
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
068890be 20
068890be
JJ
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"
268a13a5 29#include "gdbsupport/environ.h"
068890be
JJ
30#include "command.h"
31#include "ui-out.h"
32#include "top.h"
53ce3c39 33#include <sys/stat.h>
b46a8d7c 34#include "source.h"
4ef3f3be 35
068890be
JJ
36static const char path_var_name[] = "PATH";
37static char *orig_path = NULL;
38
d303380b
AC
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. */
2b03b41d 42
068890be 43static void
d303380b 44env_execute_cli_command (const char *cmd, const char *args)
068890be 45{
d303380b 46 if (cmd != 0)
068890be 47 {
e91a1fa7 48 gdb::unique_xmalloc_ptr<char> run;
102040f0 49
d303380b 50 if (args != NULL)
e91a1fa7 51 run.reset (xstrprintf ("%s %s", cmd, args));
d303380b 52 else
e91a1fa7
TT
53 run.reset (xstrdup (cmd));
54 execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
068890be
JJ
55 }
56}
57
068890be 58/* Print working directory. */
2b03b41d 59
ce8f13f8 60void
9f33b8b7 61mi_cmd_env_pwd (const char *command, char **argv, int argc)
068890be 62{
79a45e25
PA
63 struct ui_out *uiout = current_uiout;
64
068890be 65 if (argc > 0)
2b03b41d 66 error (_("-environment-pwd: No arguments allowed"));
068890be
JJ
67
68 if (mi_version (uiout) < 2)
69 {
70 env_execute_cli_command ("pwd", NULL);
ce8f13f8 71 return;
068890be
JJ
72 }
73
74 /* Otherwise the mi level is 2 or higher. */
75
43573013
SDJ
76 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
77 if (cwd == NULL)
1b05df00 78 error (_("-environment-pwd: error finding name of working directory: %s"),
bf1d7d9c 79 safe_strerror (errno));
43573013
SDJ
80
81 uiout->field_string ("cwd", cwd.get ());
068890be
JJ
82}
83
84/* Change working directory. */
2b03b41d 85
ce8f13f8 86void
9f33b8b7 87mi_cmd_env_cd (const char *command, char **argv, int argc)
068890be
JJ
88{
89 if (argc == 0 || argc > 1)
1b05df00 90 error (_("-environment-cd: Usage DIRECTORY"));
068890be 91
d303380b 92 env_execute_cli_command ("cd", argv[0]);
068890be
JJ
93}
94
95static void
5614fb77 96env_mod_path (const char *dirname, char **which_path)
068890be
JJ
97{
98 if (dirname == 0 || dirname[0] == '\0')
99 return;
100
101 /* Call add_path with last arg 0 to indicate not to parse for
102 separator characters. */
103 add_path (dirname, which_path, 0);
104}
105
106/* Add one or more directories to start of executable search path. */
2b03b41d 107
ce8f13f8 108void
9f33b8b7 109mi_cmd_env_path (const char *command, char **argv, int argc)
068890be 110{
79a45e25 111 struct ui_out *uiout = current_uiout;
068890be 112 char *exec_path;
a121b7c1 113 const char *env;
068890be 114 int reset = 0;
7082409d 115 int oind = 0;
068890be 116 int i;
7082409d 117 char *oarg;
068890be
JJ
118 enum opt
119 {
120 RESET_OPT
121 };
91174723 122 static const struct mi_opt opts[] =
068890be
JJ
123 {
124 {"r", RESET_OPT, 0},
d5d6fca5 125 { 0, 0, 0 }
068890be
JJ
126 };
127
128 dont_repeat ();
129
130 if (mi_version (uiout) < 2)
131 {
132 for (i = argc - 1; i >= 0; --i)
d303380b 133 env_execute_cli_command ("path", argv[i]);
ce8f13f8 134 return;
068890be
JJ
135 }
136
137 /* Otherwise the mi level is 2 or higher. */
138 while (1)
139 {
1b05df00 140 int opt = mi_getopt ("-environment-path", argc, argv, opts,
7082409d 141 &oind, &oarg);
102040f0 142
068890be
JJ
143 if (opt < 0)
144 break;
145 switch ((enum opt) opt)
146 {
147 case RESET_OPT:
148 reset = 1;
149 break;
150 }
151 }
7082409d
AS
152 argv += oind;
153 argc -= oind;
068890be
JJ
154
155
156 if (reset)
157 {
158 /* Reset implies resetting to original path first. */
159 exec_path = xstrdup (orig_path);
160 }
161 else
162 {
163 /* Otherwise, get current path to modify. */
9a6c7d9c 164 env = current_inferior ()->environment.get (path_var_name);
068890be
JJ
165
166 /* Can be null if path is not set. */
167 if (!env)
168 env = "";
169 exec_path = xstrdup (env);
170 }
171
172 for (i = argc - 1; i >= 0; --i)
173 env_mod_path (argv[i], &exec_path);
174
9a6c7d9c 175 current_inferior ()->environment.set (path_var_name, exec_path);
068890be 176 xfree (exec_path);
9a6c7d9c 177 env = current_inferior ()->environment.get (path_var_name);
112e8700 178 uiout->field_string ("path", env);
068890be
JJ
179}
180
181/* Add zero or more directories to the front of the source path. */
2b03b41d 182
ce8f13f8 183void
9f33b8b7 184mi_cmd_env_dir (const char *command, char **argv, int argc)
068890be 185{
79a45e25 186 struct ui_out *uiout = current_uiout;
068890be 187 int i;
7082409d 188 int oind = 0;
068890be 189 int reset = 0;
7082409d 190 char *oarg;
068890be
JJ
191 enum opt
192 {
193 RESET_OPT
194 };
91174723 195 static const struct mi_opt opts[] =
068890be
JJ
196 {
197 {"r", RESET_OPT, 0},
d5d6fca5 198 { 0, 0, 0 }
068890be
JJ
199 };
200
201 dont_repeat ();
202
203 if (mi_version (uiout) < 2)
204 {
205 for (i = argc - 1; i >= 0; --i)
d303380b 206 env_execute_cli_command ("dir", argv[i]);
ce8f13f8 207 return;
068890be
JJ
208 }
209
210 /* Otherwise mi level is 2 or higher. */
211 while (1)
212 {
1b05df00 213 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
7082409d 214 &oind, &oarg);
102040f0 215
068890be
JJ
216 if (opt < 0)
217 break;
218 switch ((enum opt) opt)
219 {
220 case RESET_OPT:
221 reset = 1;
222 break;
223 }
224 }
7082409d
AS
225 argv += oind;
226 argc -= oind;
068890be
JJ
227
228 if (reset)
229 {
230 /* Reset means setting to default path first. */
231 xfree (source_path);
232 init_source_path ();
233 }
234
235 for (i = argc - 1; i >= 0; --i)
236 env_mod_path (argv[i], &source_path);
068890be 237
112e8700 238 uiout->field_string ("source-path", source_path);
068890be 239 forget_cached_source_info ();
068890be
JJ
240}
241
3cb3b8df 242/* Set the inferior terminal device name. */
2b03b41d 243
ce8f13f8 244void
9f33b8b7 245mi_cmd_inferior_tty_set (const char *command, char **argv, int argc)
3cb3b8df
BR
246{
247 set_inferior_io_terminal (argv[0]);
3cb3b8df
BR
248}
249
2b03b41d
SS
250/* Print the inferior terminal device name. */
251
ce8f13f8 252void
9f33b8b7 253mi_cmd_inferior_tty_show (const char *command, char **argv, int argc)
3cb3b8df
BR
254{
255 const char *inferior_io_terminal = get_inferior_io_terminal ();
256
1b05df00
TT
257 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
258 error (_("-inferior-tty-show: Usage: No args"));
3cb3b8df
BR
259
260 if (inferior_io_terminal)
112e8700 261 current_uiout->field_string ("inferior_tty_terminal", inferior_io_terminal);
3cb3b8df
BR
262}
263
6c265988 264void _initialize_mi_cmd_env ();
068890be 265void
6c265988 266_initialize_mi_cmd_env ()
068890be 267{
a121b7c1 268 const char *env;
068890be 269
3f81c18a
VP
270 /* We want original execution path to reset to, if desired later.
271 At this point, current inferior is not created, so cannot use
1c8e01c9
SDJ
272 current_inferior ()->environment. We use getenv here because it
273 is not necessary to create a whole new gdb_environ just for one
274 variable. */
275 env = getenv (path_var_name);
068890be
JJ
276
277 /* Can be null if path is not set. */
278 if (!env)
279 env = "";
280 orig_path = xstrdup (env);
281}
This page took 2.225413 seconds and 4 git commands to generate.