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