Commit | Line | Data |
---|---|---|
068890be | 1 | /* MI Command Set - environment commands. |
1bac305b AC |
2 | |
3 | Copyright 2002, 2003 Free Software Foundation, Inc. | |
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 | |
11 | the Free Software Foundation; either version 2 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, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
068890be JJ |
24 | #include "defs.h" |
25 | #include "inferior.h" | |
26 | #include "value.h" | |
27 | #include "mi-out.h" | |
28 | #include "mi-cmds.h" | |
29 | #include "mi-getopt.h" | |
30 | #include "symtab.h" | |
31 | #include "target.h" | |
32 | #include "environ.h" | |
33 | #include "command.h" | |
34 | #include "ui-out.h" | |
35 | #include "top.h" | |
36 | ||
4ef3f3be | 37 | #include "gdb_string.h" |
5cb316ef | 38 | #include "gdb_stat.h" |
4ef3f3be | 39 | |
d303380b | 40 | static void env_cli_command (const char *cli, const char *args); |
068890be JJ |
41 | static void env_mod_path (char *dirname, char **which_path); |
42 | extern void _initialize_mi_cmd_env (void); | |
43 | ||
44 | static const char path_var_name[] = "PATH"; | |
45 | static char *orig_path = NULL; | |
46 | ||
d303380b AC |
47 | /* The following is copied from mi-main.c so for m1 and below we can |
48 | perform old behavior and use cli commands. If ARGS is non-null, | |
49 | append it to the CMD. */ | |
068890be | 50 | static void |
d303380b | 51 | env_execute_cli_command (const char *cmd, const char *args) |
068890be | 52 | { |
d303380b | 53 | if (cmd != 0) |
068890be JJ |
54 | { |
55 | struct cleanup *old_cleanups; | |
56 | char *run; | |
d303380b AC |
57 | if (args != NULL) |
58 | xasprintf (&run, "%s %s", cmd, args); | |
59 | else | |
60 | run = xstrdup (cmd); | |
068890be JJ |
61 | old_cleanups = make_cleanup (xfree, run); |
62 | execute_command ( /*ui */ run, 0 /*from_tty */ ); | |
63 | do_cleanups (old_cleanups); | |
64 | return; | |
65 | } | |
66 | } | |
67 | ||
68 | ||
69 | /* Print working directory. */ | |
70 | enum mi_cmd_result | |
71 | mi_cmd_env_pwd (char *command, char **argv, int argc) | |
72 | { | |
73 | if (argc > 0) | |
74 | error ("mi_cmd_env_pwd: No arguments required"); | |
75 | ||
76 | if (mi_version (uiout) < 2) | |
77 | { | |
78 | env_execute_cli_command ("pwd", NULL); | |
79 | return MI_CMD_DONE; | |
80 | } | |
81 | ||
82 | /* Otherwise the mi level is 2 or higher. */ | |
83 | ||
84 | getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); | |
85 | ui_out_field_string (uiout, "cwd", gdb_dirbuf); | |
86 | ||
87 | return MI_CMD_DONE; | |
88 | } | |
89 | ||
90 | /* Change working directory. */ | |
91 | enum mi_cmd_result | |
92 | mi_cmd_env_cd (char *command, char **argv, int argc) | |
93 | { | |
94 | if (argc == 0 || argc > 1) | |
95 | error ("mi_cmd_env_cd: Usage DIRECTORY"); | |
96 | ||
d303380b | 97 | env_execute_cli_command ("cd", argv[0]); |
068890be JJ |
98 | |
99 | return MI_CMD_DONE; | |
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 | enum mi_cmd_result | |
115 | mi_cmd_env_path (char *command, char **argv, int argc) | |
116 | { | |
117 | char *exec_path; | |
118 | char *env; | |
119 | int reset = 0; | |
120 | int optind = 0; | |
121 | int i; | |
122 | char *optarg; | |
123 | enum opt | |
124 | { | |
125 | RESET_OPT | |
126 | }; | |
127 | static struct mi_opt opts[] = | |
128 | { | |
129 | {"r", RESET_OPT, 0}, | |
130 | 0 | |
131 | }; | |
132 | ||
133 | dont_repeat (); | |
134 | ||
135 | if (mi_version (uiout) < 2) | |
136 | { | |
137 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 138 | env_execute_cli_command ("path", argv[i]); |
068890be JJ |
139 | return MI_CMD_DONE; |
140 | } | |
141 | ||
142 | /* Otherwise the mi level is 2 or higher. */ | |
143 | while (1) | |
144 | { | |
145 | int opt = mi_getopt ("mi_cmd_env_path", argc, argv, opts, | |
146 | &optind, &optarg); | |
147 | if (opt < 0) | |
148 | break; | |
149 | switch ((enum opt) opt) | |
150 | { | |
151 | case RESET_OPT: | |
152 | reset = 1; | |
153 | break; | |
154 | } | |
155 | } | |
156 | argv += optind; | |
157 | argc -= optind; | |
158 | ||
159 | ||
160 | if (reset) | |
161 | { | |
162 | /* Reset implies resetting to original path first. */ | |
163 | exec_path = xstrdup (orig_path); | |
164 | } | |
165 | else | |
166 | { | |
167 | /* Otherwise, get current path to modify. */ | |
168 | env = get_in_environ (inferior_environ, path_var_name); | |
169 | ||
170 | /* Can be null if path is not set. */ | |
171 | if (!env) | |
172 | env = ""; | |
173 | exec_path = xstrdup (env); | |
174 | } | |
175 | ||
176 | for (i = argc - 1; i >= 0; --i) | |
177 | env_mod_path (argv[i], &exec_path); | |
178 | ||
179 | set_in_environ (inferior_environ, path_var_name, exec_path); | |
180 | xfree (exec_path); | |
181 | env = get_in_environ (inferior_environ, path_var_name); | |
182 | ui_out_field_string (uiout, "path", env); | |
183 | ||
184 | return MI_CMD_DONE; | |
185 | } | |
186 | ||
187 | /* Add zero or more directories to the front of the source path. */ | |
188 | enum mi_cmd_result | |
189 | mi_cmd_env_dir (char *command, char **argv, int argc) | |
190 | { | |
191 | int i; | |
192 | int optind = 0; | |
193 | int reset = 0; | |
194 | char *optarg; | |
195 | enum opt | |
196 | { | |
197 | RESET_OPT | |
198 | }; | |
199 | static struct mi_opt opts[] = | |
200 | { | |
201 | {"r", RESET_OPT, 0}, | |
202 | 0 | |
203 | }; | |
204 | ||
205 | dont_repeat (); | |
206 | ||
207 | if (mi_version (uiout) < 2) | |
208 | { | |
209 | for (i = argc - 1; i >= 0; --i) | |
d303380b | 210 | env_execute_cli_command ("dir", argv[i]); |
068890be JJ |
211 | return MI_CMD_DONE; |
212 | } | |
213 | ||
214 | /* Otherwise mi level is 2 or higher. */ | |
215 | while (1) | |
216 | { | |
217 | int opt = mi_getopt ("mi_cmd_env_dir", argc, argv, opts, | |
218 | &optind, &optarg); | |
219 | if (opt < 0) | |
220 | break; | |
221 | switch ((enum opt) opt) | |
222 | { | |
223 | case RESET_OPT: | |
224 | reset = 1; | |
225 | break; | |
226 | } | |
227 | } | |
228 | argv += optind; | |
229 | argc -= optind; | |
230 | ||
231 | if (reset) | |
232 | { | |
233 | /* Reset means setting to default path first. */ | |
234 | xfree (source_path); | |
235 | init_source_path (); | |
236 | } | |
237 | ||
238 | for (i = argc - 1; i >= 0; --i) | |
239 | env_mod_path (argv[i], &source_path); | |
240 | init_last_source_visited (); | |
241 | ||
242 | ui_out_field_string (uiout, "source-path", source_path); | |
243 | forget_cached_source_info (); | |
244 | ||
245 | return MI_CMD_DONE; | |
246 | } | |
247 | ||
248 | void | |
249 | _initialize_mi_cmd_env (void) | |
250 | { | |
251 | char *env; | |
252 | ||
253 | /* We want original execution path to reset to, if desired later. */ | |
254 | env = get_in_environ (inferior_environ, path_var_name); | |
255 | ||
256 | /* Can be null if path is not set. */ | |
257 | if (!env) | |
258 | env = ""; | |
259 | orig_path = xstrdup (env); | |
260 | } |