unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs
@ 2015-10-14 22:08 Evgeny Roubinchtein
  2015-10-15  7:47 ` Juanma Barranquero
  2015-10-15 15:31 ` Eli Zaretskii
  0 siblings, 2 replies; 4+ messages in thread
From: Evgeny Roubinchtein @ 2015-10-14 22:08 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1179 bytes --]

Dear Emacs developers,

Would you consider applying the attached patch to nt\runemacs.c?

My "use case" is that I compiled 64-bit Emacs under Windows following
instructions at
http://sourceforge.net/p/emacsbinw64/wiki/Build%20guideline%20for%20MSYS2-MinGW-w64%20system/,
but I want to avoid copying libraries from ${msys2_root}/mingw64/bin
to the Emacs installation directory, and I also don't want to add
${msys2_root}/mingw64/bin to the PATH for my user account (let alone
system-wide).  The attached patch gives me a way to augment system
environment variables before runemacs starts the actual emacs
executable.

With this patch applied, I can set the environment variable EMACS_ENV
to ">PATH>${msys2_root}/mingw64/bin", and have that directory appended
to the PATH but only in the environment of the runemacs process.  That
environment is inherited by the emacs process, so I can allow Emacs to
find the libraries it needs in order to run without copying them to
the Emacs installation directory and without altering the PATH for my
user account.

Please let me know if it would be possible to get this patch applied
to the Emacs sources.

Thank you in advance!

-- 
Evgeny

[-- Attachment #2: 0001-Expand-the-EMACS_ENV-environment-variable.patch --]
[-- Type: application/octet-stream, Size: 3326 bytes --]

From a470d3685d96f9c3d6e3cc82ef75df39a49a54a8 Mon Sep 17 00:00:00 2001
From: Evgeny Roubinchtein <zhenya1007@gmail.com>
Date: Wed, 14 Oct 2015 21:45:32 +0100
Subject: [PATCH] Expand the EMACS_ENV environment variable.

---
 nt/runemacs.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 102 insertions(+), 1 deletion(-)

diff --git a/nt/runemacs.c b/nt/runemacs.c
index 86644b4..70cb5a5 100644
--- a/nt/runemacs.c
+++ b/nt/runemacs.c
@@ -46,6 +46,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 static void set_user_model_id (void);
 static int ensure_unicows_dll (void);
+static void expand_emacs_env (void);
 
 int WINAPI
 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
@@ -166,6 +167,8 @@ WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
       SetEnvironmentVariable ("emacs_dir", modname);
     }
 
+  expand_emacs_env ();
+
   memset (&start, 0, sizeof (start));
   start.cb = sizeof (start);
   start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
@@ -257,7 +260,7 @@ ensure_unicows_dll (void)
 	    {
 	      case IDOK:
 	      default:
-	        return 0;
+		return 0;
 	    }
 	}
       FreeLibrary (h);
@@ -265,3 +268,101 @@ ensure_unicows_dll (void)
     }
   return 1;
 }
+
+static int
+get_env_var(char const *const name, char **buf)
+{
+  int len;
+  len = GetEnvironmentVariable(name, NULL, 0);
+  if (0 == len)
+    return 0;
+  *buf = malloc(len);
+  if (!*buf)
+    return 0;
+  if (!GetEnvironmentVariable (name, *buf, len))
+    return 0;
+  return 1;
+}
+
+/* BNF grammar:
+   emacs_env_string = 'EMACS_ENV=' {prepend_clause|append_clause|overwrite_clause} '\0'
+   prepend_clause = '<' name '<' value
+   append_clause = '>' name '>' value
+   overwrite_clause = '*' name '*'
+   , where name is valid environment variable name.
+*/
+static void
+expand_emacs_env()
+{
+  int len;
+  char *buf;
+  char *b;
+  char *e;
+  int name = 0;
+  char sep[2] = {'\0', '\0'};
+
+  if (!get_env_var ("EMACS_ENV", &buf))
+    goto free_buf;
+
+  b = e = buf;
+  while (NULL != (b = strpbrk (b, ">*<")))
+    {
+      char *ee;
+      char *val = NULL;
+      char *nval = NULL;
+
+      sep[0] = *b;
+      ++b;
+      e = strpbrk(b, sep);
+      if (NULL == e)
+	goto free_buf;
+      else
+	*e = '\0';
+      ee = strpbrk(e+1, sep);
+      if (NULL != ee)
+	*ee = '\0';
+      switch (sep[0])
+	{
+	case '>': /*append*/
+	  if (!get_env_var (b, &val))
+	    goto cleanup;
+	  nval = malloc (strlen(val) + strlen(e+1) + 1);
+	  strcpy(nval, val);
+	  strcat(nval, e+1);
+	  if (!SetEnvironmentVariable (b, nval))
+	    goto cleanup;
+	  break;
+	case '<': /*prepend*/
+	  if (!get_env_var (b, &val))
+	    goto cleanup;
+	  nval = malloc (strlen(val) + strlen(e+1) + 1);
+	  strcpy(nval, e+1);
+	  strcat(nval, val);
+	  if (!SetEnvironmentVariable (b, nval))
+	    goto cleanup;
+	  break;
+	case '*': /*overwrite*/
+	  if (!SetEnvironmentVariable (b, e+1))
+	    goto cleanup;
+	  break;
+	default:
+	  return;
+	}
+    cleanup:
+      if (NULL != val)
+	free (val);
+      if (NULL != nval)
+	free (nval);
+      *e = sep[0];
+      if (NULL != ee)
+	{
+	  b = ee + 1;
+	  *ee = sep[0];
+	}
+      else
+	b = e + 1;
+    }
+ free_buf:
+  if (NULL != buf)
+    free (buf);
+}
-- 
2.6.1.windows.1


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

* Re: Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs
  2015-10-14 22:08 Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs Evgeny Roubinchtein
@ 2015-10-15  7:47 ` Juanma Barranquero
       [not found]   ` <CAGYXaSbpcuTbCh6aB+9m6MLz0U3PqaaGoebpgQoMMUkGoHvd9w@mail.gmail.com>
  2015-10-15 15:31 ` Eli Zaretskii
  1 sibling, 1 reply; 4+ messages in thread
From: Juanma Barranquero @ 2015-10-15  7:47 UTC (permalink / raw)
  To: Evgeny Roubinchtein; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 658 bytes --]

On Thu, Oct 15, 2015 at 12:08 AM, Evgeny Roubinchtein <zhenya1007@gmail.com>
wrote:

> With this patch applied, I can set the environment variable EMACS_ENV
> to ">PATH>${msys2_root}/mingw64/bin", and have that directory appended
> to the PATH but only in the environment of the runemacs process.  That
> environment is inherited by the emacs process, so I can allow Emacs to
> find the libraries it needs in order to run without copying them to
> the Emacs installation directory and without altering the PATH for my
> user account.

Why not simply have a myrunemacs.bat

@echo off
setlocal
set PATH=%PATH;/your/path/to/mingw64/bin
runemacs %*
endlocal

??

[-- Attachment #2: Type: text/html, Size: 910 bytes --]

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

* Re: Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs
       [not found]   ` <CAGYXaSbpcuTbCh6aB+9m6MLz0U3PqaaGoebpgQoMMUkGoHvd9w@mail.gmail.com>
@ 2015-10-15 14:32     ` Evgeny Roubinchtein
  0 siblings, 0 replies; 4+ messages in thread
From: Evgeny Roubinchtein @ 2015-10-15 14:32 UTC (permalink / raw)
  To: emacs-devel

For the same reason runemacs is an executable, rather than a batch
script: having a batch script will pop up a console window, and I
would like to keep that hidden from view.

On Thu, Oct 15, 2015 at 7:31 AM, Evgeny Roubinchtein
<zhenya1007@gmail.com> wrote:
> For the same reason runemacs is an executable, rather than a batch
> script: having a batch script will pop up a console window, and I
> would like to keep that hidden from view.
>
> On Thu, Oct 15, 2015 at 12:47 AM, Juanma Barranquero <lekktu@gmail.com> wrote:
>> On Thu, Oct 15, 2015 at 12:08 AM, Evgeny Roubinchtein <zhenya1007@gmail.com>
>> wrote:
>>
>>> With this patch applied, I can set the environment variable EMACS_ENV
>>> to ">PATH>${msys2_root}/mingw64/bin", and have that directory appended
>>> to the PATH but only in the environment of the runemacs process.  That
>>> environment is inherited by the emacs process, so I can allow Emacs to
>>> find the libraries it needs in order to run without copying them to
>>> the Emacs installation directory and without altering the PATH for my
>>> user account.
>>
>> Why not simply have a myrunemacs.bat
>>
>> @echo off
>> setlocal
>> set PATH=%PATH;/your/path/to/mingw64/bin
>> runemacs %*
>> endlocal
>>
>> ??



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

* Re: Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs
  2015-10-14 22:08 Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs Evgeny Roubinchtein
  2015-10-15  7:47 ` Juanma Barranquero
@ 2015-10-15 15:31 ` Eli Zaretskii
  1 sibling, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2015-10-15 15:31 UTC (permalink / raw)
  To: Evgeny Roubinchtein; +Cc: emacs-devel

> Date: Wed, 14 Oct 2015 15:08:45 -0700
> From: Evgeny Roubinchtein <zhenya1007@gmail.com>
> 
> Would you consider applying the attached patch to nt\runemacs.c?

I replied to your bug report (there's no need to post here as well).

Thanks.



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

end of thread, other threads:[~2015-10-15 15:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-14 22:08 Proposed patch to nt\runemacs.c: a way to augment environment variables prior to starting Emacs Evgeny Roubinchtein
2015-10-15  7:47 ` Juanma Barranquero
     [not found]   ` <CAGYXaSbpcuTbCh6aB+9m6MLz0U3PqaaGoebpgQoMMUkGoHvd9w@mail.gmail.com>
2015-10-15 14:32     ` Evgeny Roubinchtein
2015-10-15 15:31 ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).