Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / mi / mi-cmd-env.c
blob454f89503af7c3ddeb784293b3a216156341cb4b
1 /* MI Command Set - environment commands.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
4 Contributed by Red Hat Inc.
6 This file is part of GDB.
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.
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.
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/>. */
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 "gdbsupport/environ.h"
30 #include "command.h"
31 #include "ui-out.h"
32 #include "top.h"
33 #include <sys/stat.h>
34 #include "source.h"
36 static const char path_var_name[] = "PATH";
37 static char *orig_path = NULL;
39 /* The following is copied from mi-main.c so for m1 and below we can
40 perform old behavior and use cli commands. If ARGS is non-null,
41 append it to the CMD. */
43 static void
44 env_execute_cli_command (const char *cmd, const char *args)
46 if (cmd != 0)
48 gdb::unique_xmalloc_ptr<char> run;
50 if (args != NULL)
51 run = xstrprintf ("%s %s", cmd, args);
52 else
53 run.reset (xstrdup (cmd));
54 execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
58 /* Print working directory. */
60 void
61 mi_cmd_env_pwd (const char *command, char **argv, int argc)
63 struct ui_out *uiout = current_uiout;
65 if (argc > 0)
66 error (_("-environment-pwd: No arguments allowed"));
68 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
69 if (cwd == NULL)
70 error (_("-environment-pwd: error finding name of working directory: %s"),
71 safe_strerror (errno));
73 uiout->field_string ("cwd", cwd.get ());
76 /* Change working directory. */
78 void
79 mi_cmd_env_cd (const char *command, char **argv, int argc)
81 if (argc == 0 || argc > 1)
82 error (_("-environment-cd: Usage DIRECTORY"));
84 env_execute_cli_command ("cd", argv[0]);
87 static void
88 env_mod_path (const char *dirname, std::string &which_path)
90 if (dirname == 0 || dirname[0] == '\0')
91 return;
93 /* Call add_path with last arg 0 to indicate not to parse for
94 separator characters. */
95 add_path (dirname, which_path, 0);
98 /* Add one or more directories to start of executable search path. */
100 void
101 mi_cmd_env_path (const char *command, char **argv, int argc)
103 struct ui_out *uiout = current_uiout;
104 const char *env;
105 int reset = 0;
106 int oind = 0;
107 int i;
108 char *oarg;
109 enum opt
111 RESET_OPT
113 static const struct mi_opt opts[] =
115 {"r", RESET_OPT, 0},
116 { 0, 0, 0 }
119 dont_repeat ();
121 while (1)
123 int opt = mi_getopt ("-environment-path", argc, argv, opts,
124 &oind, &oarg);
126 if (opt < 0)
127 break;
128 switch ((enum opt) opt)
130 case RESET_OPT:
131 reset = 1;
132 break;
135 argv += oind;
136 argc -= oind;
138 std::string exec_path;
139 if (reset)
141 /* Reset implies resetting to original path first. */
142 exec_path = orig_path;
144 else
146 /* Otherwise, get current path to modify. */
147 env = current_inferior ()->environment.get (path_var_name);
149 /* Can be null if path is not set. */
150 if (!env)
151 env = "";
153 exec_path = env;
156 for (i = argc - 1; i >= 0; --i)
157 env_mod_path (argv[i], exec_path);
159 current_inferior ()->environment.set (path_var_name, exec_path.c_str ());
160 env = current_inferior ()->environment.get (path_var_name);
161 uiout->field_string ("path", env);
164 /* Add zero or more directories to the front of the source path. */
166 void
167 mi_cmd_env_dir (const char *command, char **argv, int argc)
169 struct ui_out *uiout = current_uiout;
170 int i;
171 int oind = 0;
172 int reset = 0;
173 char *oarg;
174 enum opt
176 RESET_OPT
178 static const struct mi_opt opts[] =
180 {"r", RESET_OPT, 0},
181 { 0, 0, 0 }
184 dont_repeat ();
186 while (1)
188 int opt = mi_getopt ("-environment-directory", argc, argv, opts,
189 &oind, &oarg);
191 if (opt < 0)
192 break;
193 switch ((enum opt) opt)
195 case RESET_OPT:
196 reset = 1;
197 break;
200 argv += oind;
201 argc -= oind;
203 if (reset)
205 /* Reset means setting to default path first. */
206 init_source_path ();
209 for (i = argc - 1; i >= 0; --i)
210 env_mod_path (argv[i], source_path);
212 uiout->field_string ("source-path", source_path);
213 forget_cached_source_info ();
216 /* Set the inferior terminal device name. */
218 void
219 mi_cmd_inferior_tty_set (const char *command, char **argv, int argc)
221 if (argc > 0)
222 current_inferior ()->set_tty (argv[0]);
223 else
224 current_inferior ()->set_tty ("");
227 /* Print the inferior terminal device name. */
229 void
230 mi_cmd_inferior_tty_show (const char *command, char **argv, int argc)
232 if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
233 error (_("-inferior-tty-show: Usage: No args"));
235 const std::string &inferior_tty = current_inferior ()->tty ();
236 if (!inferior_tty.empty ())
237 current_uiout->field_string ("inferior_tty_terminal", inferior_tty);
240 void _initialize_mi_cmd_env ();
241 void
242 _initialize_mi_cmd_env ()
244 const char *env;
246 /* We want original execution path to reset to, if desired later.
247 At this point, current inferior is not created, so cannot use
248 current_inferior ()->environment. We use getenv here because it
249 is not necessary to create a whole new gdb_environ just for one
250 variable. */
251 env = getenv (path_var_name);
253 /* Can be null if path is not set. */
254 if (!env)
255 env = "";
256 orig_path = xstrdup (env);