Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFC] usage of environment variable from the command line
@ 2007-09-19 15:24 Denis PILAT
  2007-09-21 22:55 ` Daniel Jacobowitz
  0 siblings, 1 reply; 14+ messages in thread
From: Denis PILAT @ 2007-09-19 15:24 UTC (permalink / raw)
  To: gdb-patches

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')



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2007-12-22 18:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-19 15:24 [RFC] usage of environment variable from the command line Denis PILAT
2007-09-21 22:55 ` Daniel Jacobowitz
2007-09-22  7:16   ` Eli Zaretskii
2007-09-22 13:58     ` Daniel Jacobowitz
2007-09-24 15:05       ` Denis PILAT
2007-09-24 16:44       ` Jim Blandy
2007-09-24 21:41         ` Eli Zaretskii
2007-09-25 17:11           ` Jim Blandy
2007-09-25 21:38             ` Eli Zaretskii
2007-09-30  1:30         ` Daniel Jacobowitz
2007-09-30  7:17           ` Eli Zaretskii
2007-10-11 17:51             ` Daniel Jacobowitz
2007-12-21 15:31             ` Denis PILAT
2007-12-22 19:27               ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox