1 /* MI Command Set - environment commands.
2 Copyright (C) 2002-2017 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 static const char path_var_name[] = "PATH";
38 static char *orig_path = NULL;
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. */
45 env_execute_cli_command (const char *cmd, const char *args)
49 gdb::unique_xmalloc_ptr<char> run;
52 run.reset (xstrprintf ("%s %s", cmd, args));
54 run.reset (xstrdup (cmd));
55 execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
59 /* Print working directory. */
62 mi_cmd_env_pwd (const char *command, char **argv, int argc)
64 struct ui_out *uiout = current_uiout;
67 error (_("-environment-pwd: No arguments allowed"));
69 if (mi_version (uiout) < 2)
71 env_execute_cli_command ("pwd", NULL);
75 /* Otherwise the mi level is 2 or higher. */
77 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
79 error (_("-environment-pwd: error finding name of working directory: %s"),
80 safe_strerror (errno));
82 uiout->field_string ("cwd", cwd.get ());
85 /* Change working directory. */
88 mi_cmd_env_cd (const char *command, char **argv, int argc)
90 if (argc == 0 || argc > 1)
91 error (_("-environment-cd: Usage DIRECTORY"));
93 env_execute_cli_command ("cd", argv[0]);
97 env_mod_path (char *dirname, char **which_path)
99 if (dirname == 0 || dirname[0] == '\0')
102 /* Call add_path with last arg 0 to indicate not to parse for
103 separator characters. */
104 add_path (dirname, which_path, 0);
107 /* Add one or more directories to start of executable search path. */
110 mi_cmd_env_path (const char *command, char **argv, int argc)
112 struct ui_out *uiout = current_uiout;
123 static const struct mi_opt opts[] =
131 if (mi_version (uiout) < 2)
133 for (i = argc - 1; i >= 0; --i)
134 env_execute_cli_command ("path", argv[i]);
138 /* Otherwise the mi level is 2 or higher. */
141 int opt = mi_getopt ("-environment-path", argc, argv, opts,
146 switch ((enum opt) opt)
159 /* Reset implies resetting to original path first. */
160 exec_path = xstrdup (orig_path);
164 /* Otherwise, get current path to modify. */
165 env = current_inferior ()->environment.get (path_var_name);
167 /* Can be null if path is not set. */
170 exec_path = xstrdup (env);
173 for (i = argc - 1; i >= 0; --i)
174 env_mod_path (argv[i], &exec_path);
176 current_inferior ()->environment.set (path_var_name, exec_path);
178 env = current_inferior ()->environment.get (path_var_name);
179 uiout->field_string ("path", env);
182 /* Add zero or more directories to the front of the source path. */
185 mi_cmd_env_dir (const char *command, char **argv, int argc)
187 struct ui_out *uiout = current_uiout;
196 static const 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 uiout->field_string ("source-path", source_path);
240 forget_cached_source_info ();
243 /* Set the inferior terminal device name. */
246 mi_cmd_inferior_tty_set (const char *command, char **argv, int argc)
248 set_inferior_io_terminal (argv[0]);
251 /* Print the inferior terminal device name. */
254 mi_cmd_inferior_tty_show (const char *command, char **argv, int argc)
256 const char *inferior_io_terminal = get_inferior_io_terminal ();
258 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
259 error (_("-inferior-tty-show: Usage: No args"));
261 if (inferior_io_terminal)
262 current_uiout->field_string ("inferior_tty_terminal", inferior_io_terminal);
266 _initialize_mi_cmd_env (void)
270 /* We want original execution path to reset to, if desired later.
271 At this point, current inferior is not created, so cannot use
272 current_inferior ()->environment. We use getenv here because it
273 is not necessary to create a whole new gdb_environ just for one
275 env = getenv (path_var_name);
277 /* Can be null if path is not set. */
280 orig_path = xstrdup (env);