1 /* MI Command Set - environment commands.
2    Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 
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
10    the Free Software Foundation; either version 3 of the License, or
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
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 #include "inferior.h"
22 #include "value.h"
23 #include "mi-out.h"
24 #include "mi-cmds.h"
25 #include "mi-getopt.h"
26 #include "symtab.h"
27 #include "target.h"
28 #include "gdbsupport/environ.h"
29 #include "command.h"
30 #include "ui-out.h"
31 #include "top.h"
32 #include <sys/stat.h>
33 #include "source.h"
34 
35 static const char path_var_name[] = "PATH";
36 static char *orig_path = NULL;
37 
38 /* The following is copied from mi-main.c so for m1 and below we can
39    perform old behavior and use cli commands.  If ARGS is non-null,
40    append it to the CMD.  */
41 
42 static void
env_execute_cli_command(const char * cmd,const char * args)43 env_execute_cli_command (const char *cmd, const char *args)
44 {
45   if (cmd != 0)
46     {
47       gdb::unique_xmalloc_ptr<char> run;
48 
49       if (args != NULL)
50           run = xstrprintf ("%s %s", cmd, args);
51       else
52           run.reset (xstrdup (cmd));
53       execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
54     }
55 }
56 
57 /* Print working directory.  */
58 
59 void
mi_cmd_env_pwd(const char * command,const char * const * argv,int argc)60 mi_cmd_env_pwd (const char *command, const char *const *argv, int argc)
61 {
62   struct ui_out *uiout = current_uiout;
63 
64   if (argc > 0)
65     error (_("-environment-pwd: No arguments allowed"));
66 
67   gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
68   if (cwd == NULL)
69     error (_("-environment-pwd: error finding name of working directory: %s"),
70              safe_strerror (errno));
71 
72   uiout->field_string ("cwd", cwd.get ());
73 }
74 
75 /* Change working directory.  */
76 
77 void
mi_cmd_env_cd(const char * command,const char * const * argv,int argc)78 mi_cmd_env_cd (const char *command, const char *const *argv, int argc)
79 {
80   if (argc == 0 || argc > 1)
81     error (_("-environment-cd: Usage DIRECTORY"));
82 
83   env_execute_cli_command ("cd", argv[0]);
84 }
85 
86 static void
env_mod_path(const char * dirname,std::string & which_path)87 env_mod_path (const char *dirname, std::string &which_path)
88 {
89   if (dirname == 0 || dirname[0] == '\0')
90     return;
91 
92   /* Call add_path with last arg 0 to indicate not to parse for
93      separator characters.  */
94   add_path (dirname, which_path, 0);
95 }
96 
97 /* Add one or more directories to start of executable search path.  */
98 
99 void
mi_cmd_env_path(const char * command,const char * const * argv,int argc)100 mi_cmd_env_path (const char *command, const char *const *argv, int argc)
101 {
102   struct ui_out *uiout = current_uiout;
103   const char *env;
104   int reset = 0;
105   int oind = 0;
106   int i;
107   const char *oarg;
108   enum opt
109     {
110       RESET_OPT
111     };
112   static const struct mi_opt opts[] =
113   {
114     {"r", RESET_OPT, 0},
115     { 0, 0, 0 }
116   };
117 
118   dont_repeat ();
119 
120   while (1)
121     {
122       int opt = mi_getopt ("-environment-path", argc, argv, opts,
123                                  &oind, &oarg);
124 
125       if (opt < 0)
126           break;
127       switch ((enum opt) opt)
128           {
129           case RESET_OPT:
130             reset = 1;
131             break;
132           }
133     }
134   argv += oind;
135   argc -= oind;
136 
137   std::string exec_path;
138   if (reset)
139     {
140       /* Reset implies resetting to original path first.  */
141       exec_path = orig_path;
142     }
143   else
144     {
145       /* Otherwise, get current path to modify.  */
146       env = current_inferior ()->environment.get (path_var_name);
147 
148       /* Can be null if path is not set.  */
149       if (!env)
150           env = "";
151 
152       exec_path = env;
153     }
154 
155   for (i = argc - 1; i >= 0; --i)
156     env_mod_path (argv[i], exec_path);
157 
158   current_inferior ()->environment.set (path_var_name, exec_path.c_str ());
159   env = current_inferior ()->environment.get (path_var_name);
160   uiout->field_string ("path", env);
161 }
162 
163 /* Add zero or more directories to the front of the source path.  */
164 
165 void
mi_cmd_env_dir(const char * command,const char * const * argv,int argc)166 mi_cmd_env_dir (const char *command, const char *const *argv, int argc)
167 {
168   struct ui_out *uiout = current_uiout;
169   int i;
170   int oind = 0;
171   int reset = 0;
172   const char *oarg;
173   enum opt
174     {
175       RESET_OPT
176     };
177   static const struct mi_opt opts[] =
178   {
179     {"r", RESET_OPT, 0},
180     { 0, 0, 0 }
181   };
182 
183   dont_repeat ();
184 
185   while (1)
186     {
187       int opt = mi_getopt ("-environment-directory", argc, argv, opts,
188                                  &oind, &oarg);
189 
190       if (opt < 0)
191           break;
192       switch ((enum opt) opt)
193           {
194           case RESET_OPT:
195             reset = 1;
196             break;
197           }
198     }
199   argv += oind;
200   argc -= oind;
201 
202   if (reset)
203     {
204       /* Reset means setting to default path first.  */
205       init_source_path ();
206     }
207 
208   for (i = argc - 1; i >= 0; --i)
209     env_mod_path (argv[i], source_path);
210 
211   uiout->field_string ("source-path", source_path);
212   forget_cached_source_info ();
213 }
214 
215 /* Set the inferior terminal device name.  */
216 
217 void
mi_cmd_inferior_tty_set(const char * command,const char * const * argv,int argc)218 mi_cmd_inferior_tty_set (const char *command, const char *const *argv,
219                                int argc)
220 {
221   if (argc > 0)
222     current_inferior ()->set_tty (argv[0]);
223   else
224     current_inferior ()->set_tty ("");
225 }
226 
227 /* Print the inferior terminal device name.  */
228 
229 void
mi_cmd_inferior_tty_show(const char * command,const char * const * argv,int argc)230 mi_cmd_inferior_tty_show (const char *command, const char *const *argv,
231                                 int argc)
232 {
233   if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
234     error (_("-inferior-tty-show: Usage: No args"));
235 
236   const std::string &inferior_tty = current_inferior ()->tty ();
237   if (!inferior_tty.empty ())
238     current_uiout->field_string ("inferior_tty_terminal", inferior_tty);
239 }
240 
241 void _initialize_mi_cmd_env ();
242 void
_initialize_mi_cmd_env()243 _initialize_mi_cmd_env ()
244 {
245   const char *env;
246 
247   /* We want original execution path to reset to, if desired later.
248      At this point, current inferior is not created, so cannot use
249      current_inferior ()->environment.  We use getenv here because it
250      is not necessary to create a whole new gdb_environ just for one
251      variable.  */
252   env = getenv (path_var_name);
253 
254   /* Can be null if path is not set.  */
255   if (!env)
256     env = "";
257   orig_path = xstrdup (env);
258 }
259