1 /* MI Command Set - environment commands.
3 Copyright (C) 2002-2004, 2007-2012 Free Software Foundation, Inc.
5 Contributed by Red Hat Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "mi-getopt.h"
35 #include "gdb_string.h"
38 static void env_mod_path (char *dirname, char **which_path);
39 extern void _initialize_mi_cmd_env (void);
41 static const char path_var_name[] = "PATH";
42 static char *orig_path = NULL;
44 /* The following is copied from mi-main.c so for m1 and below we can
45 perform old behavior and use cli commands. If ARGS is non-null,
46 append it to the CMD. */
48 env_execute_cli_command (const char *cmd, const char *args)
52 struct cleanup *old_cleanups;
56 run = xstrprintf ("%s %s", cmd, args);
59 old_cleanups = make_cleanup (xfree, run);
60 execute_command ( /*ui */ run, 0 /*from_tty */ );
61 do_cleanups (old_cleanups);
67 /* Print working directory. */
69 mi_cmd_env_pwd (char *command, char **argv, int argc)
71 struct ui_out *uiout = current_uiout;
74 error (_("-environment-pwd: No arguments required"));
76 if (mi_version (uiout) < 2)
78 env_execute_cli_command ("pwd", NULL);
82 /* Otherwise the mi level is 2 or higher. */
84 if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
85 error (_("-environment-pwd: error finding name of working directory: %s"),
86 safe_strerror (errno));
88 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
91 /* 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. */
114 mi_cmd_env_path (char *command, char **argv, int argc)
116 struct ui_out *uiout = current_uiout;
127 static const struct mi_opt opts[] =
135 if (mi_version (uiout) < 2)
137 for (i = argc - 1; i >= 0; --i)
138 env_execute_cli_command ("path", argv[i]);
142 /* Otherwise the mi level is 2 or higher. */
145 int opt = mi_getopt ("-environment-path", argc, argv, opts,
150 switch ((enum opt) opt)
163 /* Reset implies resetting to original path first. */
164 exec_path = xstrdup (orig_path);
168 /* Otherwise, get current path to modify. */
169 env = get_in_environ (current_inferior ()->environment, path_var_name);
171 /* Can be null if path is not set. */
174 exec_path = xstrdup (env);
177 for (i = argc - 1; i >= 0; --i)
178 env_mod_path (argv[i], &exec_path);
180 set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
182 env = get_in_environ (current_inferior ()->environment, path_var_name);
183 ui_out_field_string (uiout, "path", env);
186 /* Add zero or more directories to the front of the source path. */
188 mi_cmd_env_dir (char *command, char **argv, int argc)
190 struct ui_out *uiout = current_uiout;
199 static const struct mi_opt opts[] =
207 if (mi_version (uiout) < 2)
209 for (i = argc - 1; i >= 0; --i)
210 env_execute_cli_command ("dir", argv[i]);
214 /* Otherwise mi level is 2 or higher. */
217 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
222 switch ((enum opt) opt)
234 /* Reset means setting to default path first. */
239 for (i = argc - 1; i >= 0; --i)
240 env_mod_path (argv[i], &source_path);
242 ui_out_field_string (uiout, "source-path", source_path);
243 forget_cached_source_info ();
246 /* Set the inferior terminal device name. */
248 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
250 set_inferior_io_terminal (argv[0]);
253 /* Print the inferior terminal device name */
255 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
257 const char *inferior_io_terminal = get_inferior_io_terminal ();
259 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
260 error (_("-inferior-tty-show: Usage: No args"));
262 if (inferior_io_terminal)
263 ui_out_field_string (current_uiout,
264 "inferior_tty_terminal", inferior_io_terminal);
268 _initialize_mi_cmd_env (void)
270 struct gdb_environ *environment;
273 /* We want original execution path to reset to, if desired later.
274 At this point, current inferior is not created, so cannot use
275 current_inferior ()->environment. Also, there's no obvious
276 place where this code can be moved suchs that it surely run
277 before any code possibly mangles original PATH. */
278 environment = make_environ ();
279 init_environ (environment);
280 env = get_in_environ (environment, path_var_name);
282 /* Can be null if path is not set. */
285 orig_path = xstrdup (env);
286 free_environ (environment);