2011-08-04 Pedro Alves <pedro@codesourcery.com>
[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, 2010, 2011
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
56 if (args != NULL)
57 run = xstrprintf ("%s %s", cmd, args);
58 else
59 run = xstrdup (cmd);
60 old_cleanups = make_cleanup (xfree, run);
61 execute_command ( /*ui */ run, 0 /*from_tty */ );
62 do_cleanups (old_cleanups);
63 return;
64 }
65 }
66
67
68 /* Print working directory. */
69 void
70 mi_cmd_env_pwd (char *command, char **argv, int argc)
71 {
72 struct ui_out *uiout = current_uiout;
73
74 if (argc > 0)
75 error (_("-environment-pwd: No arguments required"));
76
77 if (mi_version (uiout) < 2)
78 {
79 env_execute_cli_command ("pwd", NULL);
80 return;
81 }
82
83 /* Otherwise the mi level is 2 or higher. */
84
85 if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
86 error (_("-environment-pwd: error finding name of working directory: %s"),
87 safe_strerror (errno));
88
89 ui_out_field_string (uiout, "cwd", gdb_dirbuf);
90 }
91
92 /* Change working directory. */
93 void
94 mi_cmd_env_cd (char *command, char **argv, int argc)
95 {
96 if (argc == 0 || argc > 1)
97 error (_("-environment-cd: Usage DIRECTORY"));
98
99 env_execute_cli_command ("cd", argv[0]);
100 }
101
102 static void
103 env_mod_path (char *dirname, char **which_path)
104 {
105 if (dirname == 0 || dirname[0] == '\0')
106 return;
107
108 /* Call add_path with last arg 0 to indicate not to parse for
109 separator characters. */
110 add_path (dirname, which_path, 0);
111 }
112
113 /* Add one or more directories to start of executable search path. */
114 void
115 mi_cmd_env_path (char *command, char **argv, int argc)
116 {
117 struct ui_out *uiout = current_uiout;
118 char *exec_path;
119 char *env;
120 int reset = 0;
121 int optind = 0;
122 int i;
123 char *optarg;
124 enum opt
125 {
126 RESET_OPT
127 };
128 static struct mi_opt opts[] =
129 {
130 {"r", RESET_OPT, 0},
131 { 0, 0, 0 }
132 };
133
134 dont_repeat ();
135
136 if (mi_version (uiout) < 2)
137 {
138 for (i = argc - 1; i >= 0; --i)
139 env_execute_cli_command ("path", argv[i]);
140 return;
141 }
142
143 /* Otherwise the mi level is 2 or higher. */
144 while (1)
145 {
146 int opt = mi_getopt ("-environment-path", argc, argv, opts,
147 &optind, &optarg);
148
149 if (opt < 0)
150 break;
151 switch ((enum opt) opt)
152 {
153 case RESET_OPT:
154 reset = 1;
155 break;
156 }
157 }
158 argv += optind;
159 argc -= optind;
160
161
162 if (reset)
163 {
164 /* Reset implies resetting to original path first. */
165 exec_path = xstrdup (orig_path);
166 }
167 else
168 {
169 /* Otherwise, get current path to modify. */
170 env = get_in_environ (current_inferior ()->environment, path_var_name);
171
172 /* Can be null if path is not set. */
173 if (!env)
174 env = "";
175 exec_path = xstrdup (env);
176 }
177
178 for (i = argc - 1; i >= 0; --i)
179 env_mod_path (argv[i], &exec_path);
180
181 set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
182 xfree (exec_path);
183 env = get_in_environ (current_inferior ()->environment, path_var_name);
184 ui_out_field_string (uiout, "path", env);
185 }
186
187 /* Add zero or more directories to the front of the source path. */
188 void
189 mi_cmd_env_dir (char *command, char **argv, int argc)
190 {
191 struct ui_out *uiout = current_uiout;
192 int i;
193 int optind = 0;
194 int reset = 0;
195 char *optarg;
196 enum opt
197 {
198 RESET_OPT
199 };
200 static struct mi_opt opts[] =
201 {
202 {"r", RESET_OPT, 0},
203 { 0, 0, 0 }
204 };
205
206 dont_repeat ();
207
208 if (mi_version (uiout) < 2)
209 {
210 for (i = argc - 1; i >= 0; --i)
211 env_execute_cli_command ("dir", argv[i]);
212 return;
213 }
214
215 /* Otherwise mi level is 2 or higher. */
216 while (1)
217 {
218 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
219 &optind, &optarg);
220
221 if (opt < 0)
222 break;
223 switch ((enum opt) opt)
224 {
225 case RESET_OPT:
226 reset = 1;
227 break;
228 }
229 }
230 argv += optind;
231 argc -= optind;
232
233 if (reset)
234 {
235 /* Reset means setting to default path first. */
236 xfree (source_path);
237 init_source_path ();
238 }
239
240 for (i = argc - 1; i >= 0; --i)
241 env_mod_path (argv[i], &source_path);
242
243 ui_out_field_string (uiout, "source-path", source_path);
244 forget_cached_source_info ();
245 }
246
247 /* Set the inferior terminal device name. */
248 void
249 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
250 {
251 set_inferior_io_terminal (argv[0]);
252 }
253
254 /* Print the inferior terminal device name */
255 void
256 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
257 {
258 const char *inferior_io_terminal = get_inferior_io_terminal ();
259
260 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
261 error (_("-inferior-tty-show: Usage: No args"));
262
263 if (inferior_io_terminal)
264 ui_out_field_string (current_uiout,
265 "inferior_tty_terminal", inferior_io_terminal);
266 }
267
268 void
269 _initialize_mi_cmd_env (void)
270 {
271 struct gdb_environ *environment;
272 char *env;
273
274 /* We want original execution path to reset to, if desired later.
275 At this point, current inferior is not created, so cannot use
276 current_inferior ()->environment. Also, there's no obvious
277 place where this code can be moved suchs that it surely run
278 before any code possibly mangles original PATH. */
279 environment = make_environ ();
280 init_environ (environment);
281 env = get_in_environ (environment, path_var_name);
282
283 /* Can be null if path is not set. */
284 if (!env)
285 env = "";
286 orig_path = xstrdup (env);
287 free_environ (environment);
288 }
This page took 0.04633 seconds and 4 git commands to generate.