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