GDB (API)
/home/stan/gdb/src/gdb/mi/mi-cmd-env.c
Go to the documentation of this file.
00001 /* MI Command Set - environment commands.
00002    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00003 
00004    Contributed by Red Hat Inc.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "inferior.h"
00023 #include "value.h"
00024 #include "mi-out.h"
00025 #include "mi-cmds.h"
00026 #include "mi-getopt.h"
00027 #include "symtab.h"
00028 #include "target.h"
00029 #include "environ.h"
00030 #include "command.h"
00031 #include "ui-out.h"
00032 #include "top.h"
00033 
00034 #include "gdb_string.h"
00035 #include "gdb_stat.h"
00036 
00037 static void env_mod_path (char *dirname, char **which_path);
00038 
00039 extern void _initialize_mi_cmd_env (void);
00040 
00041 static const char path_var_name[] = "PATH";
00042 static char *orig_path = NULL;
00043 
00044 /* The following is copied from mi-main.c so for m1 and below we can
00045    perform old behavior and use cli commands.  If ARGS is non-null,
00046    append it to the CMD.  */
00047 
00048 static void
00049 env_execute_cli_command (const char *cmd, const char *args)
00050 {
00051   if (cmd != 0)
00052     {
00053       struct cleanup *old_cleanups;
00054       char *run;
00055 
00056       if (args != NULL)
00057         run = xstrprintf ("%s %s", cmd, args);
00058       else
00059         run = xstrdup (cmd);
00060       old_cleanups = make_cleanup (xfree, run);
00061       execute_command ( /*ui */ run, 0 /*from_tty */ );
00062       do_cleanups (old_cleanups);
00063       return;
00064     }
00065 }
00066 
00067 /* Print working directory.  */
00068 
00069 void
00070 mi_cmd_env_pwd (char *command, char **argv, int argc)
00071 {
00072   struct ui_out *uiout = current_uiout;
00073 
00074   if (argc > 0)
00075     error (_("-environment-pwd: No arguments allowed"));
00076           
00077   if (mi_version (uiout) < 2)
00078     {
00079       env_execute_cli_command ("pwd", NULL);
00080       return;
00081     }
00082      
00083   /* Otherwise the mi level is 2 or higher.  */
00084 
00085   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
00086     error (_("-environment-pwd: error finding name of working directory: %s"),
00087            safe_strerror (errno));
00088     
00089   ui_out_field_string (uiout, "cwd", gdb_dirbuf);
00090 }
00091 
00092 /* Change working directory.  */
00093 
00094 void
00095 mi_cmd_env_cd (char *command, char **argv, int argc)
00096 {
00097   if (argc == 0 || argc > 1)
00098     error (_("-environment-cd: Usage DIRECTORY"));
00099           
00100   env_execute_cli_command ("cd", argv[0]);
00101 }
00102 
00103 static void
00104 env_mod_path (char *dirname, char **which_path)
00105 {
00106   if (dirname == 0 || dirname[0] == '\0')
00107     return;
00108 
00109   /* Call add_path with last arg 0 to indicate not to parse for 
00110      separator characters.  */
00111   add_path (dirname, which_path, 0);
00112 }
00113 
00114 /* Add one or more directories to start of executable search path.  */
00115 
00116 void
00117 mi_cmd_env_path (char *command, char **argv, int argc)
00118 {
00119   struct ui_out *uiout = current_uiout;
00120   char *exec_path;
00121   char *env;
00122   int reset = 0;
00123   int oind = 0;
00124   int i;
00125   char *oarg;
00126   enum opt
00127     {
00128       RESET_OPT
00129     };
00130   static const struct mi_opt opts[] =
00131   {
00132     {"r", RESET_OPT, 0},
00133     { 0, 0, 0 }
00134   };
00135 
00136   dont_repeat ();
00137 
00138   if (mi_version (uiout) < 2)
00139     {
00140       for (i = argc - 1; i >= 0; --i)
00141         env_execute_cli_command ("path", argv[i]);
00142       return;
00143     }
00144 
00145   /* Otherwise the mi level is 2 or higher.  */
00146   while (1)
00147     {
00148       int opt = mi_getopt ("-environment-path", argc, argv, opts,
00149                            &oind, &oarg);
00150 
00151       if (opt < 0)
00152         break;
00153       switch ((enum opt) opt)
00154         {
00155         case RESET_OPT:
00156           reset = 1;
00157           break;
00158         }
00159     }
00160   argv += oind;
00161   argc -= oind;
00162 
00163 
00164   if (reset)
00165     {
00166       /* Reset implies resetting to original path first.  */
00167       exec_path = xstrdup (orig_path);
00168     }
00169   else
00170     {
00171       /* Otherwise, get current path to modify.  */
00172       env = get_in_environ (current_inferior ()->environment, path_var_name);
00173 
00174       /* Can be null if path is not set.  */
00175       if (!env)
00176         env = "";
00177       exec_path = xstrdup (env);
00178     }
00179 
00180   for (i = argc - 1; i >= 0; --i)
00181     env_mod_path (argv[i], &exec_path);
00182 
00183   set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
00184   xfree (exec_path);
00185   env = get_in_environ (current_inferior ()->environment, path_var_name);
00186   ui_out_field_string (uiout, "path", env);
00187 }
00188 
00189 /* Add zero or more directories to the front of the source path.  */
00190 
00191 void
00192 mi_cmd_env_dir (char *command, char **argv, int argc)
00193 {
00194   struct ui_out *uiout = current_uiout;
00195   int i;
00196   int oind = 0;
00197   int reset = 0;
00198   char *oarg;
00199   enum opt
00200     {
00201       RESET_OPT
00202     };
00203   static const struct mi_opt opts[] =
00204   {
00205     {"r", RESET_OPT, 0},
00206     { 0, 0, 0 }
00207   };
00208 
00209   dont_repeat ();
00210 
00211   if (mi_version (uiout) < 2)
00212     {
00213       for (i = argc - 1; i >= 0; --i)
00214         env_execute_cli_command ("dir", argv[i]);
00215       return;
00216     }
00217 
00218   /* Otherwise mi level is 2 or higher.  */
00219   while (1)
00220     {
00221       int opt = mi_getopt ("-environment-directory", argc, argv, opts,
00222                            &oind, &oarg);
00223 
00224       if (opt < 0)
00225         break;
00226       switch ((enum opt) opt)
00227         {
00228         case RESET_OPT:
00229           reset = 1;
00230           break;
00231         }
00232     }
00233   argv += oind;
00234   argc -= oind;
00235 
00236   if (reset)
00237     {
00238       /* Reset means setting to default path first.  */
00239       xfree (source_path);
00240       init_source_path ();
00241     }
00242 
00243   for (i = argc - 1; i >= 0; --i)
00244     env_mod_path (argv[i], &source_path);
00245 
00246   ui_out_field_string (uiout, "source-path", source_path);
00247   forget_cached_source_info ();
00248 }
00249 
00250 /* Set the inferior terminal device name.  */
00251 
00252 void
00253 mi_cmd_inferior_tty_set (char *command, char **argv, int argc)
00254 {
00255   set_inferior_io_terminal (argv[0]);
00256 }
00257 
00258 /* Print the inferior terminal device name.  */
00259 
00260 void
00261 mi_cmd_inferior_tty_show (char *command, char **argv, int argc)
00262 {
00263   const char *inferior_io_terminal = get_inferior_io_terminal ();
00264   
00265   if ( !mi_valid_noargs ("-inferior-tty-show", argc, argv))
00266     error (_("-inferior-tty-show: Usage: No args"));
00267 
00268   if (inferior_io_terminal)
00269     ui_out_field_string (current_uiout,
00270                          "inferior_tty_terminal", inferior_io_terminal);
00271 }
00272 
00273 void 
00274 _initialize_mi_cmd_env (void)
00275 {
00276   struct gdb_environ *environment;
00277   char *env;
00278 
00279   /* We want original execution path to reset to, if desired later.
00280      At this point, current inferior is not created, so cannot use
00281      current_inferior ()->environment.  Also, there's no obvious
00282      place where this code can be moved such that it surely run
00283      before any code possibly mangles original PATH.  */
00284   environment = make_environ ();
00285   init_environ (environment);
00286   env = get_in_environ (environment, path_var_name);
00287 
00288   /* Can be null if path is not set.  */
00289   if (!env)
00290     env = "";
00291   orig_path = xstrdup (env);
00292   free_environ (environment);
00293 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines