From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28997 invoked by alias); 19 Sep 2007 15:24:47 -0000 Received: (qmail 28985 invoked by uid 22791); 19 Sep 2007 15:24:46 -0000 X-Spam-Check-By: sourceware.org Received: from s200aob12.obsmtp.com (HELO s200aob12.obsmtp.com) (207.126.144.116) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 19 Sep 2007 15:24:33 +0000 Received: from source ([164.129.1.35]) (using TLSv1) by eu1sys200aob012.postini.com ([207.126.147.11]) with SMTP; Wed, 19 Sep 2007 15:24:27 UTC Received: from zeta.dmz-eu.st.com (ns2.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 157EBDC21 for ; Wed, 19 Sep 2007 15:24:27 +0000 (GMT) Received: from mail1.cro.st.com (mail1.cro.st.com [164.129.40.131]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id DB3484C0B8 for ; Wed, 19 Sep 2007 15:24:26 +0000 (GMT) Received: from crx595.cro.st.com (crx595.cro.st.com [164.129.44.95]) by mail1.cro.st.com (MOS 3.7.5a-GA) with ESMTP id CLQ37646 (AUTH "denis pilat"); Wed, 19 Sep 2007 17:24:26 +0200 (CEST) Message-ID: <46F13F2A.8010507@st.com> Date: Wed, 19 Sep 2007 15:24:00 -0000 From: Denis PILAT User-Agent: Thunderbird 2.0.0.6 (X11/20070728) MIME-Version: 1.0 To: gdb-patches@sourceware.org Subject: [RFC] usage of environment variable from the command line Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2007-09/txt/msg00254.txt.bz2 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')