[RFC] usage of environment variable from the command line

Denis PILAT denis.pilat@st.com
Fri Dec 21 15:31:00 GMT 2007


Hi everyone,

I'going back to the patch proposed a few months ago.

I'm now having a patch that use the suggested syntax "${...}" and 
transform commands at the beginning of top.c/execute_command() only if 
the  ${...} match an environment variable.
It works as well for scripts that can contains env. variables.
Anything from the command line is transformed, there is no limit to the 
kind of commands that can use env. variables.

Attached is the patch, I know that you won't accept it but it's just to 
discuss. I guess it's not generic enough and may be does not cover some 
language ..., but what's sure is that it does not involved any 
regression in the full testsuite.

The old discussion was here: 
http://sources.redhat.com/ml/gdb-patches/2007-09/msg00430.html
My point is:
- What do you mean by a specification ?
- You also told about quoting issues, could you please be more precise ? 
I don't see any issue in my patch (like in every patches I've done ... ;-) )

Thanks,
Denis

Index: top.c
===================================================================
--- top.c       (revision 624)
+++ top.c       (working copy)
@@ -364,6 +364,49 @@ do_chdir_cleanup (void *old_dir)
 }
 #endif

+
+#define ENV_DELIMITOR_BEGIN  "${"
+#define ENV_DELIMITOR_END  "}"
+static char*
+evaluate_environment_from_string(char* input)
+{
+  char *p1, *p2, *output, *env_var, *env_var_value;
+  int env_var_len, delimitor_begin_len, delimitor_end_len;
+
+  p1 = (char *) strstr (input, ENV_DELIMITOR_BEGIN);
+  if (p1 == NULL)
+    return input;
+
+  p2 = (char *) strstr (p1+1, ENV_DELIMITOR_END);
+  if (p2 == NULL)
+    return input;
+
+  delimitor_begin_len = strlen(ENV_DELIMITOR_BEGIN);
+  delimitor_end_len = strlen(ENV_DELIMITOR_END);
+  /* get the env var.  */
+  env_var_len = p2 - p1 - delimitor_begin_len;
+  env_var = xmalloc (env_var_len + 1);
+  strncpy (env_var, p1 + delimitor_begin_len, 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 - delimitor_begin_len - delimitor_end_len + 1);
+  strncpy (output, input, p1-input);
+  strcpy  (output+(p1-input), env_var_value);
+  strcat  (output+(p1-input), p2 + delimitor_end_len);
+
+  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.  */

@@ -385,6 +428,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')




More information about the Gdb-patches mailing list