Commit | Line | Data |
---|---|---|
068890be | 1 | /* MI Command Set - environment commands. |
32d0add0 | 2 | Copyright (C) 2002-2015 Free Software Foundation, Inc. |
1bac305b | 3 | |
068890be JJ |
4 | Contributed by Red Hat Inc. |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
068890be JJ |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
068890be | 20 | |
068890be JJ |
21 | #include "defs.h" |
22 | #include "inferior.h" | |
23 | #include "value.h" | |
24 | #include "mi-out.h" | |
25 | #include "mi-cmds.h" | |
26 | #include "mi-getopt.h" | |
27 | #include "symtab.h" | |
28 | #include "target.h" | |
29 | #include "environ.h" | |
30 | #include "command.h" | |
31 | #include "ui-out.h" | |
32 | #include "top.h" | |
53ce3c39 | 33 | #include <sys/stat.h> |
4ef3f3be | 34 | |
068890be | 35 | static void env_mod_path (char *dirname, char **which_path); |
2b03b41d | 36 | |
068890be JJ |
37 | extern void _initialize_mi_cmd_env (void); |
38 | ||
39 | static const char path_var_name[] = "PATH"; | |
40 | static char *orig_path = NULL; | |
41 | ||
d303380b AC |
42 | /* The following is copied from mi-main.c so for m1 and below we can |
43 | perform old behavior and use cli commands. If ARGS is non-null, | |
44 | append it to the CMD. */ | |
2b03b41d | 45 | |
068890be | 46 | static void |
d303380b | 47 | env_execute_cli_command (const char *cmd, const char *args) |
068890be | 48 | { |
d303380b | 49 | if (cmd != 0) |
068890be JJ |
50 | { |
51 | struct cleanup *old_cleanups; | |
52 | char *run; | |
102040f0 | 53 | |
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 | ||
068890be | 65 | /* Print working directory. */ |
2b03b41d | 66 | |
ce8f13f8 | 67 | void |
068890be JJ |
68 | mi_cmd_env_pwd (char *command, char **argv, int argc) |
69 | { | |
79a45e25 PA |
70 | struct ui_out *uiout = current_uiout; |
71 | ||
068890be | 72 | if (argc > 0) |
2b03b41d | 73 | error (_("-environment-pwd: No arguments allowed")); |
068890be JJ |
74 | |
75 | if (mi_version (uiout) < 2) | |
76 | { | |
77 | env_execute_cli_command ("pwd", NULL); | |
ce8f13f8 | 78 | return; |
068890be JJ |
79 | } |
80 | ||
81 | /* Otherwise the mi level is 2 or higher. */ | |
82 | ||
bf1d7d9c | 83 | if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf))) |
1b05df00 | 84 | error (_("-environment-pwd: error finding name of working directory: %s"), |
bf1d7d9c JB |
85 | safe_strerror (errno)); |
86 | ||
068890be | 87 | ui_out_field_string (uiout, "cwd", gdb_dirbuf); |
068890be JJ |
88 | } |
89 | ||
90 | /* Change working directory. */ | |
2b03b41d | 91 | |
ce8f13f8 | 92 | void |
068890be JJ |
93 | mi_cmd_env_cd (char *command, char **argv, int argc) |
94 | { | |
95 | if (argc == 0 || argc > 1) | |
1b05df00 | 96 | error (_("-environment-cd: Usage DIRECTORY")); |
068890be | 97 | |
d303380b | 98 | env_execute_cli_command ("cd", argv[0]); |
068890be JJ |
99 | } |
100 | ||
101 | static void | |
102 | env_mod_path (char *dirname, char **which_path) | |
103 | { | |
104 | if (dirname == 0 || dirname[0] == '\0') | |
105 | return; | |
106 | ||
107 | /* Call add_path with last arg 0 to indicate not to parse for | |
108 | separator characters. */ | |
109 | add_path (dirname, which_path, 0); | |
110 | } | |
111 | ||
112 | /* Add one or more directories to start of executable search path. */ | |
2b03b41d | 113 | |
ce8f13f8 | 114 | void |
068890be JJ |
115 | mi_cmd_env_path (char *command, char **argv, int argc) |
116 | { | |
79a45e25 | 117 | struct ui_out *uiout = current_uiout; |
068890be JJ |
118 | char *exec_path; |
119 | char *env; | |
120 | int reset = 0; | |
7082409d | 121 | int oind = 0; |
068890be | 122 | int i; |
7082409d | 123 | char *oarg; |
068890be JJ |
124 | enum opt |
125 | { | |
126 | RESET_OPT | |
127 | }; | |
91174723 | 128 | static const struct mi_opt opts[] = |
068890be JJ |
129 | { |
130 | {"r", RESET_OPT, 0}, | |
d5d6fca5 | 131 | { 0, 0, 0 } |
068890be JJ |
132 | }; |
133 | ||
134 | dont_repeat (); | |
135 | ||
136 | if (mi_version (uiout) < 2) | |
137 | { | |
138 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 139 | env_execute_cli_command ("path", argv[i]); |
ce8f13f8 | 140 | return; |
068890be JJ |
141 | } |
142 | ||
143 | /* Otherwise the mi level is 2 or higher. */ | |
144 | while (1) | |
145 | { | |
1b05df00 | 146 | int opt = mi_getopt ("-environment-path", argc, argv, opts, |
7082409d | 147 | &oind, &oarg); |
102040f0 | 148 | |
068890be JJ |
149 | if (opt < 0) |
150 | break; | |
151 | switch ((enum opt) opt) | |
152 | { | |
153 | case RESET_OPT: | |
154 | reset = 1; | |
155 | break; | |
156 | } | |
157 | } | |
7082409d AS |
158 | argv += oind; |
159 | argc -= oind; | |
068890be JJ |
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. */ | |
3f81c18a | 170 | env = get_in_environ (current_inferior ()->environment, path_var_name); |
068890be JJ |
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 | ||
3f81c18a | 181 | set_in_environ (current_inferior ()->environment, path_var_name, exec_path); |
068890be | 182 | xfree (exec_path); |
3f81c18a | 183 | env = get_in_environ (current_inferior ()->environment, path_var_name); |
068890be | 184 | ui_out_field_string (uiout, "path", env); |
068890be JJ |
185 | } |
186 | ||
187 | /* Add zero or more directories to the front of the source path. */ | |
2b03b41d | 188 | |
ce8f13f8 | 189 | void |
068890be JJ |
190 | mi_cmd_env_dir (char *command, char **argv, int argc) |
191 | { | |
79a45e25 | 192 | struct ui_out *uiout = current_uiout; |
068890be | 193 | int i; |
7082409d | 194 | int oind = 0; |
068890be | 195 | int reset = 0; |
7082409d | 196 | char *oarg; |
068890be JJ |
197 | enum opt |
198 | { | |
199 | RESET_OPT | |
200 | }; | |
91174723 | 201 | static const struct mi_opt opts[] = |
068890be JJ |
202 | { |
203 | {"r", RESET_OPT, 0}, | |
d5d6fca5 | 204 | { 0, 0, 0 } |
068890be JJ |
205 | }; |
206 | ||
207 | dont_repeat (); | |
208 | ||
209 | if (mi_version (uiout) < 2) | |
210 | { | |
211 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 212 | env_execute_cli_command ("dir", argv[i]); |
ce8f13f8 | 213 | return; |
068890be JJ |
214 | } |
215 | ||
216 | /* Otherwise mi level is 2 or higher. */ | |
217 | while (1) | |
218 | { | |
1b05df00 | 219 | int opt = mi_getopt ("-environment-directory", argc, argv, opts, |
7082409d | 220 | &oind, &oarg); |
102040f0 | 221 | |
068890be JJ |
222 | if (opt < 0) |
223 | break; | |
224 | switch ((enum opt) opt) | |
225 | { | |
226 | case RESET_OPT: | |
227 | reset = 1; | |
228 | break; | |
229 | } | |
230 | } | |
7082409d AS |
231 | argv += oind; |
232 | argc -= oind; | |
068890be JJ |
233 | |
234 | if (reset) | |
235 | { | |
236 | /* Reset means setting to default path first. */ | |
237 | xfree (source_path); | |
238 | init_source_path (); | |
239 | } | |
240 | ||
241 | for (i = argc - 1; i >= 0; --i) | |
242 | env_mod_path (argv[i], &source_path); | |
068890be JJ |
243 | |
244 | ui_out_field_string (uiout, "source-path", source_path); | |
245 | forget_cached_source_info (); | |
068890be JJ |
246 | } |
247 | ||
3cb3b8df | 248 | /* Set the inferior terminal device name. */ |
2b03b41d | 249 | |
ce8f13f8 | 250 | void |
3cb3b8df BR |
251 | mi_cmd_inferior_tty_set (char *command, char **argv, int argc) |
252 | { | |
253 | set_inferior_io_terminal (argv[0]); | |
3cb3b8df BR |
254 | } |
255 | ||
2b03b41d SS |
256 | /* Print the inferior terminal device name. */ |
257 | ||
ce8f13f8 | 258 | void |
3cb3b8df BR |
259 | mi_cmd_inferior_tty_show (char *command, char **argv, int argc) |
260 | { | |
261 | const char *inferior_io_terminal = get_inferior_io_terminal (); | |
262 | ||
1b05df00 TT |
263 | if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv)) |
264 | error (_("-inferior-tty-show: Usage: No args")); | |
3cb3b8df BR |
265 | |
266 | if (inferior_io_terminal) | |
79a45e25 PA |
267 | ui_out_field_string (current_uiout, |
268 | "inferior_tty_terminal", inferior_io_terminal); | |
3cb3b8df BR |
269 | } |
270 | ||
068890be JJ |
271 | void |
272 | _initialize_mi_cmd_env (void) | |
273 | { | |
3f81c18a | 274 | struct gdb_environ *environment; |
068890be JJ |
275 | char *env; |
276 | ||
3f81c18a VP |
277 | /* We want original execution path to reset to, if desired later. |
278 | At this point, current inferior is not created, so cannot use | |
279 | current_inferior ()->environment. Also, there's no obvious | |
2b03b41d | 280 | place where this code can be moved such that it surely run |
3f81c18a VP |
281 | before any code possibly mangles original PATH. */ |
282 | environment = make_environ (); | |
283 | init_environ (environment); | |
284 | env = get_in_environ (environment, path_var_name); | |
068890be JJ |
285 | |
286 | /* Can be null if path is not set. */ | |
287 | if (!env) | |
288 | env = ""; | |
289 | orig_path = xstrdup (env); | |
0350914a | 290 | free_environ (environment); |
068890be | 291 | } |