mi-cmd-env.c 6.3 KB

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