1 /* MI Command Set - environment commands.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Red Hat Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "mi-getopt.h"
35 #include "gdb_string.h"
38 static void env_cli_command (const char *cli, char *args);
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
46 can perform old behavior and use cli commands. */
48 env_execute_cli_command (const char *cli, char *args)
52 struct cleanup *old_cleanups;
54 xasprintf (&run, cli, args);
55 old_cleanups = make_cleanup (xfree, run);
56 execute_command ( /*ui */ run, 0 /*from_tty */ );
57 do_cleanups (old_cleanups);
63 /* Print working directory. */
65 mi_cmd_env_pwd (char *command, char **argv, int argc)
68 error ("mi_cmd_env_pwd: No arguments required");
70 if (mi_version (uiout) < 2)
72 env_execute_cli_command ("pwd", NULL);
76 /* Otherwise the mi level is 2 or higher. */
78 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
79 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
84 /* Change working directory. */
86 mi_cmd_env_cd (char *command, char **argv, int argc)
88 if (argc == 0 || argc > 1)
89 error ("mi_cmd_env_cd: Usage DIRECTORY");
91 env_execute_cli_command ("cd %s", 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. */
109 mi_cmd_env_path (char *command, char **argv, int argc)
121 static struct mi_opt opts[] =
129 if (mi_version (uiout) < 2)
131 for (i = argc - 1; i >= 0; --i)
132 env_execute_cli_command ("path %s", argv[i]);
136 /* Otherwise the mi level is 2 or higher. */
139 int opt = mi_getopt ("mi_cmd_env_path", argc, argv, opts,
143 switch ((enum opt) opt)
156 /* Reset implies resetting to original path first. */
157 exec_path = xstrdup (orig_path);
161 /* Otherwise, get current path to modify. */
162 env = get_in_environ (inferior_environ, path_var_name);
164 /* Can be null if path is not set. */
167 exec_path = xstrdup (env);
170 for (i = argc - 1; i >= 0; --i)
171 env_mod_path (argv[i], &exec_path);
173 set_in_environ (inferior_environ, path_var_name, exec_path);
175 env = get_in_environ (inferior_environ, path_var_name);
176 ui_out_field_string (uiout, "path", env);
181 /* Add zero or more directories to the front of the source path. */
183 mi_cmd_env_dir (char *command, char **argv, int argc)
193 static struct mi_opt opts[] =
201 if (mi_version (uiout) < 2)
203 for (i = argc - 1; i >= 0; --i)
204 env_execute_cli_command ("dir %s", argv[i]);
208 /* Otherwise mi level is 2 or higher. */
211 int opt = mi_getopt ("mi_cmd_env_dir", argc, argv, opts,
215 switch ((enum opt) opt)
227 /* Reset means setting to default path first. */
232 for (i = argc - 1; i >= 0; --i)
233 env_mod_path (argv[i], &source_path);
234 init_last_source_visited ();
236 ui_out_field_string (uiout, "source-path", source_path);
237 forget_cached_source_info ();
243 _initialize_mi_cmd_env (void)
247 /* We want original execution path to reset to, if desired later. */
248 env = get_in_environ (inferior_environ, path_var_name);
250 /* Can be null if path is not set. */
253 orig_path = xstrdup (env);