Commit | Line | Data |
---|---|---|
068890be | 1 | /* MI Command Set - environment commands. |
1bac305b | 2 | |
9b254dd1 | 3 | Copyright (C) 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc. |
1bac305b | 4 | |
068890be JJ |
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 | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
068890be JJ |
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 | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
068890be | 21 | |
068890be JJ |
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 | ||
4ef3f3be | 35 | #include "gdb_string.h" |
5cb316ef | 36 | #include "gdb_stat.h" |
4ef3f3be | 37 | |
068890be JJ |
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 | ||
d303380b AC |
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. */ | |
068890be | 47 | static void |
d303380b | 48 | env_execute_cli_command (const char *cmd, const char *args) |
068890be | 49 | { |
d303380b | 50 | if (cmd != 0) |
068890be JJ |
51 | { |
52 | struct cleanup *old_cleanups; | |
53 | char *run; | |
d303380b | 54 | if (args != NULL) |
c6902d46 | 55 | run = xstrprintf ("%s %s", cmd, args); |
d303380b AC |
56 | else |
57 | run = xstrdup (cmd); | |
068890be JJ |
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. */ | |
ce8f13f8 | 67 | void |
068890be JJ |
68 | mi_cmd_env_pwd (char *command, char **argv, int argc) |
69 | { | |
70 | if (argc > 0) | |
8a3fe4f8 | 71 | error (_("mi_cmd_env_pwd: No arguments required")); |
068890be JJ |
72 | |
73 | if (mi_version (uiout) < 2) | |
74 | { | |
75 | env_execute_cli_command ("pwd", NULL); | |
ce8f13f8 | 76 | return; |
068890be JJ |
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); | |
068890be JJ |
83 | } |
84 | ||
85 | /* Change working directory. */ | |
ce8f13f8 | 86 | void |
068890be JJ |
87 | mi_cmd_env_cd (char *command, char **argv, int argc) |
88 | { | |
89 | if (argc == 0 || argc > 1) | |
8a3fe4f8 | 90 | error (_("mi_cmd_env_cd: Usage DIRECTORY")); |
068890be | 91 | |
d303380b | 92 | env_execute_cli_command ("cd", argv[0]); |
068890be JJ |
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. */ | |
ce8f13f8 | 107 | void |
068890be JJ |
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}, | |
d5d6fca5 | 123 | { 0, 0, 0 } |
068890be JJ |
124 | }; |
125 | ||
126 | dont_repeat (); | |
127 | ||
128 | if (mi_version (uiout) < 2) | |
129 | { | |
130 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 131 | env_execute_cli_command ("path", argv[i]); |
ce8f13f8 | 132 | return; |
068890be JJ |
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); | |
068890be JJ |
176 | } |
177 | ||
178 | /* Add zero or more directories to the front of the source path. */ | |
ce8f13f8 | 179 | void |
068890be JJ |
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}, | |
d5d6fca5 | 193 | { 0, 0, 0 } |
068890be JJ |
194 | }; |
195 | ||
196 | dont_repeat (); | |
197 | ||
198 | if (mi_version (uiout) < 2) | |
199 | { | |
200 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 201 | env_execute_cli_command ("dir", argv[i]); |
ce8f13f8 | 202 | return; |
068890be JJ |
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 (); | |
068890be JJ |
235 | } |
236 | ||
3cb3b8df | 237 | /* Set the inferior terminal device name. */ |
ce8f13f8 | 238 | void |
3cb3b8df BR |
239 | mi_cmd_inferior_tty_set (char *command, char **argv, int argc) |
240 | { | |
241 | set_inferior_io_terminal (argv[0]); | |
3cb3b8df BR |
242 | } |
243 | ||
244 | /* Print the inferior terminal device name */ | |
ce8f13f8 | 245 | void |
3cb3b8df BR |
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); | |
3cb3b8df BR |
255 | } |
256 | ||
068890be JJ |
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 | } |