1 /* MI Command Set - environment commands.
3 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Red Hat Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "mi-getopt.h"
36 #include "gdb_string.h"
39 static void env_mod_path (char *dirname, char **which_path);
40 extern void _initialize_mi_cmd_env (void);
42 static const char path_var_name[] = "PATH";
43 static char *orig_path = NULL;
45 /* The following is copied from mi-main.c so for m1 and below we can
46 perform old behavior and use cli commands. If ARGS is non-null,
47 append it to the CMD. */
49 env_execute_cli_command (const char *cmd, const char *args)
53 struct cleanup *old_cleanups;
57 run = xstrprintf ("%s %s", cmd, args);
60 old_cleanups = make_cleanup (xfree, run);
61 execute_command ( /*ui */ run, 0 /*from_tty */ );
62 do_cleanups (old_cleanups);
68 /* Print working directory. */
70 mi_cmd_env_pwd (char *command, char **argv, int argc)
73 error (_("-environment-pwd: No arguments required"));
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. */
92 mi_cmd_env_cd (char *command, char **argv, int argc)
94 if (argc == 0 || argc > 1)
95 error (_("-environment-cd: Usage DIRECTORY"));
97 env_execute_cli_command ("cd", argv[0]);
101 env_mod_path (char *dirname, char **which_path)
103 if (dirname == 0 || dirname[0] == '\0')
106 /* Call add_path with last arg 0 to indicate not to parse for
107 separator characters. */
108 add_path (dirname, which_path, 0);
111 /* Add one or more directories to start of executable search path. */
113 mi_cmd_env_path (char *command, char **argv, int argc)
125 static struct mi_opt opts[] =
133 if (mi_version (uiout) < 2)
135 for (i = argc - 1; i >= 0; --i)
136 env_execute_cli_command ("path", argv[i]);
140 /* Otherwise the mi level is 2 or higher. */
143 int opt = mi_getopt ("-environment-path", argc, argv, opts,
148 switch ((enum opt) opt)
161 /* Reset implies resetting to original path first. */
162 exec_path = xstrdup (orig_path);
166 /* Otherwise, get current path to modify. */
167 env = get_in_environ (current_inferior ()->environment, path_var_name);
169 /* Can be null if path is not set. */
172 exec_path = xstrdup (env);
175 for (i = argc - 1; i >= 0; --i)
176 env_mod_path (argv[i], &exec_path);
178 set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
180 env = get_in_environ (current_inferior ()->environment, path_var_name);
181 ui_out_field_string (uiout, "path", env);
184 /* Add zero or more directories to the front of the source path. */
186 mi_cmd_env_dir (char *command, char **argv, int argc)
196 static struct mi_opt opts[] =
204 if (mi_version (uiout) < 2)
206 for (i = argc - 1; i >= 0; --i)
207 env_execute_cli_command ("dir", argv[i]);
211 /* Otherwise mi level is 2 or higher. */
214 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
219 switch ((enum opt) opt)
231 /* Reset means setting to default path first. */
236 for (i = argc - 1; i >= 0; --i)
237 env_mod_path (argv[i], &source_path);
239 ui_out_field_string (uiout, "source-path", source_path);
240 forget_cached_source_info ();
243 /* Set the inferior terminal device name. */
245 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
247 set_inferior_io_terminal (argv[0]);
250 /* Print the inferior terminal device name */
252 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
254 const char *inferior_io_terminal = get_inferior_io_terminal ();
256 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
257 error (_("-inferior-tty-show: Usage: No args"));
259 if (inferior_io_terminal)
260 ui_out_field_string (uiout, "inferior_tty_terminal", inferior_io_terminal);
264 _initialize_mi_cmd_env (void)
266 struct gdb_environ *environment;
269 /* We want original execution path to reset to, if desired later.
270 At this point, current inferior is not created, so cannot use
271 current_inferior ()->environment. Also, there's no obvious
272 place where this code can be moved suchs that it surely run
273 before any code possibly mangles original PATH. */
274 environment = make_environ ();
275 init_environ (environment);
276 env = get_in_environ (environment, path_var_name);
278 /* Can be null if path is not set. */
281 orig_path = xstrdup (env);
282 free_environ (environment);