This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] usage of environment variable from the command line


Hi,

I have a patch that allows to use environment variable when typing commands.
Environment variable must be surrounded by "%%" string.
(Ex: "file %%REPOSITORY%%/obj/a.out" would be supported.)


Is there any chance that such a patch could be accepted ?
I've read from the gdb mailing list that I'm not the only one who wants to handle environment variable from the command line.


Thanks for your feedback,
--
Denis

Index: top.c
===================================================================
--- top.c       (revision 598)
+++ top.c       (working copy)
@@ -370,6 +370,46 @@ do_chdir_cleanup (void *old_dir)
}
#endif

+#define ENV_DELIMITOR  "%%"
+static char*
+evaluate_environment_from_string(char* input)
+{
+  char *p1, *p2, *output, *env_var, *env_var_value;
+  int   env_var_len;
+
+
+  p1 = (char *) strstr (input, ENV_DELIMITOR);
+  if (p1 == NULL)
+    return input;
+
+  p2 = (char *) strstr (p1+1, ENV_DELIMITOR);
+  if (p2 == NULL)
+    return input;
+
+  /* get the env var.  */
+  env_var_len = p2 - p1 - strlen(ENV_DELIMITOR);
+  env_var = xmalloc (env_var_len + 1);
+  strncpy (env_var, p1 + strlen(ENV_DELIMITOR), env_var_len);
+  env_var[env_var_len] = 0;
+
+  /* get its value.  */
+  env_var_value = getenv (env_var);
+  if (env_var_value == NULL) {
+    xfree (env_var);
+    return input;
+  }
+
+  /* replace this value into the original string.  */
+  output = xmalloc (strlen (input) + strlen (env_var_value) - env_var_len - 2*strlen(ENV_DELIMITOR) + 1);
+  strncpy (output, input, p1-input);
+  strcpy  (output+(p1-input), env_var_value);
+  strcat  (output+(p1-input), p2 + strlen(ENV_DELIMITOR));
+
+  xfree (env_var);
+  return evaluate_environment_from_string(output);
+}
+
+
/* Execute the line P as a command.
   Pass FROM_TTY as second argument to the defining function.  */

  /* Force cleanup of any alloca areas if using C alloca instead of
@@ -391,6 +431,8 @@ execute_command (char *p, int from_tty)
  if (p == NULL)
    return;

+  p = evaluate_environment_from_string (p);
+
  serial_log_command (p);

while (*p == ' ' || *p == '\t')



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]