1 /* MI Command Set - environment commands.
3 Copyright 2002, 2003 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
29 #include "mi-getopt.h"
37 #include "gdb_string.h"
40 static void env_cli_command (const char *cli, char *args);
41 static void env_mod_path (char *dirname, char **which_path);
42 extern void _initialize_mi_cmd_env (void);
44 static const char path_var_name[] = "PATH";
45 static char *orig_path = NULL;
47 /* The following is copied from mi-main.c so for m1 and below we
48 can perform old behavior and use cli commands. */
50 env_execute_cli_command (const char *cli, char *args)
54 struct cleanup *old_cleanups;
56 xasprintf (&run, cli, args);
57 old_cleanups = make_cleanup (xfree, run);
58 execute_command ( /*ui */ run, 0 /*from_tty */ );
59 do_cleanups (old_cleanups);
65 /* Print working directory. */
67 mi_cmd_env_pwd (char *command, char **argv, int argc)
70 error ("mi_cmd_env_pwd: No arguments required");
72 if (mi_version (uiout) < 2)
74 env_execute_cli_command ("pwd", NULL);
78 /* Otherwise the mi level is 2 or higher. */
80 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
81 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
86 /* Change working directory. */
88 mi_cmd_env_cd (char *command, char **argv, int argc)
90 if (argc == 0 || argc > 1)
91 error ("mi_cmd_env_cd: Usage DIRECTORY");
93 env_execute_cli_command ("cd %s", argv[0]);
99 env_mod_path (char *dirname, char **which_path)
101 if (dirname == 0 || dirname[0] == '\0')
104 /* Call add_path with last arg 0 to indicate not to parse for
105 separator characters. */
106 add_path (dirname, which_path, 0);
109 /* Add one or more directories to start of executable search path. */
111 mi_cmd_env_path (char *command, char **argv, int argc)
123 static struct mi_opt opts[] =
131 if (mi_version (uiout) < 2)
133 for (i = argc - 1; i >= 0; --i)
134 env_execute_cli_command ("path %s", argv[i]);
138 /* Otherwise the mi level is 2 or higher. */
141 int opt = mi_getopt ("mi_cmd_env_path", argc, argv, opts,
145 switch ((enum opt) opt)
158 /* Reset implies resetting to original path first. */
159 exec_path = xstrdup (orig_path);
163 /* Otherwise, get current path to modify. */
164 env = get_in_environ (inferior_environ, path_var_name);
166 /* Can be null if path is not set. */
169 exec_path = xstrdup (env);
172 for (i = argc - 1; i >= 0; --i)
173 env_mod_path (argv[i], &exec_path);
175 set_in_environ (inferior_environ, path_var_name, exec_path);
177 env = get_in_environ (inferior_environ, path_var_name);
178 ui_out_field_string (uiout, "path", env);
183 /* Add zero or more directories to the front of the source path. */
185 mi_cmd_env_dir (char *command, char **argv, int argc)
195 static struct mi_opt opts[] =
203 if (mi_version (uiout) < 2)
205 for (i = argc - 1; i >= 0; --i)
206 env_execute_cli_command ("dir %s", argv[i]);
210 /* Otherwise mi level is 2 or higher. */
213 int opt = mi_getopt ("mi_cmd_env_dir", argc, argv, opts,
217 switch ((enum opt) opt)
229 /* Reset means setting to default path first. */
234 for (i = argc - 1; i >= 0; --i)
235 env_mod_path (argv[i], &source_path);
236 init_last_source_visited ();
238 ui_out_field_string (uiout, "source-path", source_path);
239 forget_cached_source_info ();
245 _initialize_mi_cmd_env (void)
249 /* We want original execution path to reset to, if desired later. */
250 env = get_in_environ (inferior_environ, path_var_name);
252 /* Can be null if path is not set. */
255 orig_path = xstrdup (env);