b40fac4750f81d5614d0cbdb874e8a64f35d5f7d
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-env.c
1 /* MI Command Set - environment commands.
2
3 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 Contributed by Red Hat Inc.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "value.h"
26 #include "mi-out.h"
27 #include "mi-cmds.h"
28 #include "mi-getopt.h"
29 #include "symtab.h"
30 #include "target.h"
31 #include "environ.h"
32 #include "command.h"
33 #include "ui-out.h"
34 #include "top.h"
35
36 #include "gdb_string.h"
37 #include "gdb_stat.h"
38
39 static void env_mod_path (char *dirname, char **which_path);
40 extern void _initialize_mi_cmd_env (void);
41
42 static const char path_var_name[] = "PATH";
43 static char *orig_path = NULL;
44
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. */
48 static void
49 env_execute_cli_command (const char *cmd, const char *args)
50 {
51 if (cmd != 0)
52 {
53 struct cleanup *old_cleanups;
54 char *run;
55 if (args != NULL)
56 run = xstrprintf ("%s %s", cmd, args);
57 else
58 run = xstrdup (cmd);
59 old_cleanups = make_cleanup (xfree, run);
60 execute_command ( /*ui */ run, 0 /*from_tty */ );
61 do_cleanups (old_cleanups);
62 return;
63 }
64 }
65
66
67 /* Print working directory. */
68 void
69 mi_cmd_env_pwd (char *command, char **argv, int argc)
70 {
71 if (argc > 0)
72 error (_("mi_cmd_env_pwd: No arguments required"));
73
74 if (mi_version (uiout) < 2)
75 {
76 env_execute_cli_command ("pwd", NULL);
77 return;
78 }
79
80 /* Otherwise the mi level is 2 or higher. */
81
82 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
83 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
84 }
85
86 /* Change working directory. */
87 void
88 mi_cmd_env_cd (char *command, char **argv, int argc)
89 {
90 if (argc == 0 || argc > 1)
91 error (_("mi_cmd_env_cd: Usage DIRECTORY"));
92
93 env_execute_cli_command ("cd", argv[0]);
94 }
95
96 static void
97 env_mod_path (char *dirname, char **which_path)
98 {
99 if (dirname == 0 || dirname[0] == '\0')
100 return;
101
102 /* Call add_path with last arg 0 to indicate not to parse for
103 separator characters. */
104 add_path (dirname, which_path, 0);
105 }
106
107 /* Add one or more directories to start of executable search path. */
108 void
109 mi_cmd_env_path (char *command, char **argv, int argc)
110 {
111 char *exec_path;
112 char *env;
113 int reset = 0;
114 int optind = 0;
115 int i;
116 char *optarg;
117 enum opt
118 {
119 RESET_OPT
120 };
121 static struct mi_opt opts[] =
122 {
123 {"r", RESET_OPT, 0},
124 { 0, 0, 0 }
125 };
126
127 dont_repeat ();
128
129 if (mi_version (uiout) < 2)
130 {
131 for (i = argc - 1; i >= 0; --i)
132 env_execute_cli_command ("path", argv[i]);
133 return;
134 }
135
136 /* Otherwise the mi level is 2 or higher. */
137 while (1)
138 {
139 int opt = mi_getopt ("mi_cmd_env_path", argc, argv, opts,
140 &optind, &optarg);
141 if (opt < 0)
142 break;
143 switch ((enum opt) opt)
144 {
145 case RESET_OPT:
146 reset = 1;
147 break;
148 }
149 }
150 argv += optind;
151 argc -= optind;
152
153
154 if (reset)
155 {
156 /* Reset implies resetting to original path first. */
157 exec_path = xstrdup (orig_path);
158 }
159 else
160 {
161 /* Otherwise, get current path to modify. */
162 env = get_in_environ (inferior_environ, path_var_name);
163
164 /* Can be null if path is not set. */
165 if (!env)
166 env = "";
167 exec_path = xstrdup (env);
168 }
169
170 for (i = argc - 1; i >= 0; --i)
171 env_mod_path (argv[i], &exec_path);
172
173 set_in_environ (inferior_environ, path_var_name, exec_path);
174 xfree (exec_path);
175 env = get_in_environ (inferior_environ, path_var_name);
176 ui_out_field_string (uiout, "path", env);
177 }
178
179 /* Add zero or more directories to the front of the source path. */
180 void
181 mi_cmd_env_dir (char *command, char **argv, int argc)
182 {
183 int i;
184 int optind = 0;
185 int reset = 0;
186 char *optarg;
187 enum opt
188 {
189 RESET_OPT
190 };
191 static struct mi_opt opts[] =
192 {
193 {"r", RESET_OPT, 0},
194 { 0, 0, 0 }
195 };
196
197 dont_repeat ();
198
199 if (mi_version (uiout) < 2)
200 {
201 for (i = argc - 1; i >= 0; --i)
202 env_execute_cli_command ("dir", argv[i]);
203 return;
204 }
205
206 /* Otherwise mi level is 2 or higher. */
207 while (1)
208 {
209 int opt = mi_getopt ("mi_cmd_env_dir", argc, argv, opts,
210 &optind, &optarg);
211 if (opt < 0)
212 break;
213 switch ((enum opt) opt)
214 {
215 case RESET_OPT:
216 reset = 1;
217 break;
218 }
219 }
220 argv += optind;
221 argc -= optind;
222
223 if (reset)
224 {
225 /* Reset means setting to default path first. */
226 xfree (source_path);
227 init_source_path ();
228 }
229
230 for (i = argc - 1; i >= 0; --i)
231 env_mod_path (argv[i], &source_path);
232 init_last_source_visited ();
233
234 ui_out_field_string (uiout, "source-path", source_path);
235 forget_cached_source_info ();
236 }
237
238 /* Set the inferior terminal device name. */
239 void
240 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
241 {
242 set_inferior_io_terminal (argv[0]);
243 }
244
245 /* Print the inferior terminal device name */
246 void
247 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
248 {
249 const char *inferior_io_terminal = get_inferior_io_terminal ();
250
251 if ( !mi_valid_noargs ("mi_cmd_inferior_tty_show", argc, argv))
252 error (_("mi_cmd_inferior_tty_show: Usage: No args"));
253
254 if (inferior_io_terminal)
255 ui_out_field_string (uiout, "inferior_tty_terminal", inferior_io_terminal);
256 }
257
258 void
259 _initialize_mi_cmd_env (void)
260 {
261 char *env;
262
263 /* We want original execution path to reset to, if desired later. */
264 env = get_in_environ (inferior_environ, path_var_name);
265
266 /* Can be null if path is not set. */
267 if (!env)
268 env = "";
269 orig_path = xstrdup (env);
270 }
This page took 0.046987 seconds and 4 git commands to generate.