gdb/mi/mi-cmd-env.c - gdb

Global variables defined

Functions defined

Source code

  1. /* MI Command Set - environment commands.
  2.    Copyright (C) 2002-2015 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 "environ.h"
  24. #include "command.h"
  25. #include "ui-out.h"
  26. #include "top.h"
  27. #include <sys/stat.h>

  28. static void env_mod_path (char *dirname, char **which_path);

  29. extern void _initialize_mi_cmd_env (void);

  30. static const char path_var_name[] = "PATH";
  31. static char *orig_path = NULL;

  32. /* The following is copied from mi-main.c so for m1 and below we can
  33.    perform old behavior and use cli commands.  If ARGS is non-null,
  34.    append it to the CMD.  */

  35. static void
  36. env_execute_cli_command (const char *cmd, const char *args)
  37. {
  38.   if (cmd != 0)
  39.     {
  40.       struct cleanup *old_cleanups;
  41.       char *run;

  42.       if (args != NULL)
  43.         run = xstrprintf ("%s %s", cmd, args);
  44.       else
  45.         run = xstrdup (cmd);
  46.       old_cleanups = make_cleanup (xfree, run);
  47.       execute_command ( /*ui */ run, 0 /*from_tty */ );
  48.       do_cleanups (old_cleanups);
  49.       return;
  50.     }
  51. }

  52. /* Print working directory.  */

  53. void
  54. mi_cmd_env_pwd (char *command, char **argv, int argc)
  55. {
  56.   struct ui_out *uiout = current_uiout;

  57.   if (argc > 0)
  58.     error (_("-environment-pwd: No arguments allowed"));

  59.   if (mi_version (uiout) < 2)
  60.     {
  61.       env_execute_cli_command ("pwd", NULL);
  62.       return;
  63.     }

  64.   /* Otherwise the mi level is 2 or higher.  */

  65.   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
  66.     error (_("-environment-pwd: error finding name of working directory: %s"),
  67.            safe_strerror (errno));

  68.   ui_out_field_string (uiout, "cwd", gdb_dirbuf);
  69. }

  70. /* Change working directory.  */

  71. void
  72. mi_cmd_env_cd (char *command, char **argv, int argc)
  73. {
  74.   if (argc == 0 || argc > 1)
  75.     error (_("-environment-cd: Usage DIRECTORY"));

  76.   env_execute_cli_command ("cd", argv[0]);
  77. }

  78. static void
  79. env_mod_path (char *dirname, char **which_path)
  80. {
  81.   if (dirname == 0 || dirname[0] == '\0')
  82.     return;

  83.   /* Call add_path with last arg 0 to indicate not to parse for
  84.      separator characters.  */
  85.   add_path (dirname, which_path, 0);
  86. }

  87. /* Add one or more directories to start of executable search path.  */

  88. void
  89. mi_cmd_env_path (char *command, char **argv, int argc)
  90. {
  91.   struct ui_out *uiout = current_uiout;
  92.   char *exec_path;
  93.   char *env;
  94.   int reset = 0;
  95.   int oind = 0;
  96.   int i;
  97.   char *oarg;
  98.   enum opt
  99.     {
  100.       RESET_OPT
  101.     };
  102.   static const struct mi_opt opts[] =
  103.   {
  104.     {"r", RESET_OPT, 0},
  105.     { 0, 0, 0 }
  106.   };

  107.   dont_repeat ();

  108.   if (mi_version (uiout) < 2)
  109.     {
  110.       for (i = argc - 1; i >= 0; --i)
  111.         env_execute_cli_command ("path", argv[i]);
  112.       return;
  113.     }

  114.   /* Otherwise the mi level is 2 or higher.  */
  115.   while (1)
  116.     {
  117.       int opt = mi_getopt ("-environment-path", argc, argv, opts,
  118.                            &oind, &oarg);

  119.       if (opt < 0)
  120.         break;
  121.       switch ((enum opt) opt)
  122.         {
  123.         case RESET_OPT:
  124.           reset = 1;
  125.           break;
  126.         }
  127.     }
  128.   argv += oind;
  129.   argc -= oind;


  130.   if (reset)
  131.     {
  132.       /* Reset implies resetting to original path first.  */
  133.       exec_path = xstrdup (orig_path);
  134.     }
  135.   else
  136.     {
  137.       /* Otherwise, get current path to modify.  */
  138.       env = get_in_environ (current_inferior ()->environment, path_var_name);

  139.       /* Can be null if path is not set.  */
  140.       if (!env)
  141.         env = "";
  142.       exec_path = xstrdup (env);
  143.     }

  144.   for (i = argc - 1; i >= 0; --i)
  145.     env_mod_path (argv[i], &exec_path);

  146.   set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
  147.   xfree (exec_path);
  148.   env = get_in_environ (current_inferior ()->environment, path_var_name);
  149.   ui_out_field_string (uiout, "path", env);
  150. }

  151. /* Add zero or more directories to the front of the source path.  */

  152. void
  153. mi_cmd_env_dir (char *command, char **argv, int argc)
  154. {
  155.   struct ui_out *uiout = current_uiout;
  156.   int i;
  157.   int oind = 0;
  158.   int reset = 0;
  159.   char *oarg;
  160.   enum opt
  161.     {
  162.       RESET_OPT
  163.     };
  164.   static const struct mi_opt opts[] =
  165.   {
  166.     {"r", RESET_OPT, 0},
  167.     { 0, 0, 0 }
  168.   };

  169.   dont_repeat ();

  170.   if (mi_version (uiout) < 2)
  171.     {
  172.       for (i = argc - 1; i >= 0; --i)
  173.         env_execute_cli_command ("dir", argv[i]);
  174.       return;
  175.     }

  176.   /* Otherwise mi level is 2 or higher.  */
  177.   while (1)
  178.     {
  179.       int opt = mi_getopt ("-environment-directory", argc, argv, opts,
  180.                            &oind, &oarg);

  181.       if (opt < 0)
  182.         break;
  183.       switch ((enum opt) opt)
  184.         {
  185.         case RESET_OPT:
  186.           reset = 1;
  187.           break;
  188.         }
  189.     }
  190.   argv += oind;
  191.   argc -= oind;

  192.   if (reset)
  193.     {
  194.       /* Reset means setting to default path first.  */
  195.       xfree (source_path);
  196.       init_source_path ();
  197.     }

  198.   for (i = argc - 1; i >= 0; --i)
  199.     env_mod_path (argv[i], &source_path);

  200.   ui_out_field_string (uiout, "source-path", source_path);
  201.   forget_cached_source_info ();
  202. }

  203. /* Set the inferior terminal device name.  */

  204. void
  205. mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
  206. {
  207.   set_inferior_io_terminal (argv[0]);
  208. }

  209. /* Print the inferior terminal device name.  */

  210. void
  211. mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
  212. {
  213.   const char *inferior_io_terminal = get_inferior_io_terminal ();

  214.   if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
  215.     error (_("-inferior-tty-show: Usage: No args"));

  216.   if (inferior_io_terminal)
  217.     ui_out_field_string (current_uiout,
  218.                          "inferior_tty_terminal", inferior_io_terminal);
  219. }

  220. void
  221. _initialize_mi_cmd_env (void)
  222. {
  223.   struct gdb_environ *environment;
  224.   char *env;

  225.   /* We want original execution path to reset to, if desired later.
  226.      At this point, current inferior is not created, so cannot use
  227.      current_inferior ()->environment.  Also, there's no obvious
  228.      place where this code can be moved such that it surely run
  229.      before any code possibly mangles original PATH.  */
  230.   environment = make_environ ();
  231.   init_environ (environment);
  232.   env = get_in_environ (environment, path_var_name);

  233.   /* Can be null if path is not set.  */
  234.   if (!env)
  235.     env = "";
  236.   orig_path = xstrdup (env);
  237.   free_environ (environment);
  238. }