1 /* MI Command Set - environment commands.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 Contributed by Red Hat Inc.
6 This file is part of GDB.
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.
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.
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/>. */
26 #include "mi-getopt.h"
35 static void env_mod_path (char *dirname, char **which_path);
37 extern void _initialize_mi_cmd_env (void);
39 static const char path_var_name[] = "PATH";
40 static char *orig_path = NULL;
42 /* The following is copied from mi-main.c so for m1 and below we can
43 perform old behavior and use cli commands. If ARGS is non-null,
44 append it to the CMD. */
47 env_execute_cli_command (const char *cmd, const char *args)
51 struct cleanup *old_cleanups;
55 run = xstrprintf ("%s %s", cmd, args);
58 old_cleanups = make_cleanup (xfree, run);
59 execute_command ( /*ui */ run, 0 /*from_tty */ );
60 do_cleanups (old_cleanups);
65 /* Print working directory. */
68 mi_cmd_env_pwd (char *command, char **argv, int argc)
70 struct ui_out *uiout = current_uiout;
73 error (_("-environment-pwd: No arguments allowed"));
75 if (mi_version (uiout) < 2)
77 env_execute_cli_command ("pwd", NULL);
81 /* Otherwise the mi level is 2 or higher. */
83 if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
84 error (_("-environment-pwd: error finding name of working directory: %s"),
85 safe_strerror (errno));
87 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
90 /* Change working directory. */
93 mi_cmd_env_cd (char *command, char **argv, int argc)
95 if (argc == 0 || argc > 1)
96 error (_("-environment-cd: Usage DIRECTORY"));
98 env_execute_cli_command ("cd", argv[0]);
102 env_mod_path (char *dirname, char **which_path)
104 if (dirname == 0 || dirname[0] == '\0')
107 /* Call add_path with last arg 0 to indicate not to parse for
108 separator characters. */
109 add_path (dirname, which_path, 0);
112 /* Add one or more directories to start of executable search path. */
115 mi_cmd_env_path (char *command, char **argv, int argc)
117 struct ui_out *uiout = current_uiout;
128 static const struct mi_opt opts[] =
136 if (mi_version (uiout) < 2)
138 for (i = argc - 1; i >= 0; --i)
139 env_execute_cli_command ("path", argv[i]);
143 /* Otherwise the mi level is 2 or higher. */
146 int opt = mi_getopt ("-environment-path", argc, argv, opts,
151 switch ((enum opt) opt)
164 /* Reset implies resetting to original path first. */
165 exec_path = xstrdup (orig_path);
169 /* Otherwise, get current path to modify. */
170 env = get_in_environ (current_inferior ()->environment, path_var_name);
172 /* Can be null if path is not set. */
175 exec_path = xstrdup (env);
178 for (i = argc - 1; i >= 0; --i)
179 env_mod_path (argv[i], &exec_path);
181 set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
183 env = get_in_environ (current_inferior ()->environment, path_var_name);
184 ui_out_field_string (uiout, "path", env);
187 /* Add zero or more directories to the front of the source path. */
190 mi_cmd_env_dir (char *command, char **argv, int argc)
192 struct ui_out *uiout = current_uiout;
201 static const struct mi_opt opts[] =
209 if (mi_version (uiout) < 2)
211 for (i = argc - 1; i >= 0; --i)
212 env_execute_cli_command ("dir", argv[i]);
216 /* Otherwise mi level is 2 or higher. */
219 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
224 switch ((enum opt) opt)
236 /* Reset means setting to default path first. */
241 for (i = argc - 1; i >= 0; --i)
242 env_mod_path (argv[i], &source_path);
244 ui_out_field_string (uiout, "source-path", source_path);
245 forget_cached_source_info ();
248 /* Set the inferior terminal device name. */
251 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
253 set_inferior_io_terminal (argv[0]);
256 /* Print the inferior terminal device name. */
259 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
261 const char *inferior_io_terminal = get_inferior_io_terminal ();
263 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
264 error (_("-inferior-tty-show: Usage: No args"));
266 if (inferior_io_terminal)
267 ui_out_field_string (current_uiout,
268 "inferior_tty_terminal", inferior_io_terminal);
272 _initialize_mi_cmd_env (void)
274 struct gdb_environ *environment;
277 /* We want original execution path to reset to, if desired later.
278 At this point, current inferior is not created, so cannot use
279 current_inferior ()->environment. Also, there's no obvious
280 place where this code can be moved such that it surely run
281 before any code possibly mangles original PATH. */
282 environment = make_environ ();
283 init_environ (environment);
284 env = get_in_environ (environment, path_var_name);
286 /* Can be null if path is not set. */
289 orig_path = xstrdup (env);
290 free_environ (environment);