On 2014-06-02 03:53 -0700, Jesse Molina wrote:
> Because a number of my vars have spaces or special characters in them,
> I have to do this nonsense because env output doesn't quote the
> values.

Yeah, bash might not be the best thing to do this. You could for
instance create a custom "env" command. This could read the environment
variables from a file instead from the argument list where it can be
very flaky. I've attached an example of such program. Use it like this:

env -0 >/tmp/env && tmux new-session "modify_env /tmp/env bash"

Here, the attached modify_env reads the null separated list of
environment variables, updates it's current environment (it won't
overwrite already existing vars), deletes the env file (why would I need
it anymore?) and then just executes the rest of the command line.

Would something like that work for the moment? You could even hardcode
the filename to make the usage shorter.

> tmux new-session -setenv "$EXPORT_VARS" "bash"

So you are proposing a new flag to new-session/new-window/split-window
with which we can mark some variables to copy from the current
environment to the newly created process? This sounds like it could be
useful. But this is something for the tmux devs to decide, I'm just a
lurker here. :)

-- 
Balazs
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define CHECK(x) do { \
		if (!(x)) { \
			fprintf(stderr, "%s failed.\n", #x); \
			exit(1); \
		} \
	} while (0)

enum { MAX_ENVVAR_LENGTH = 4096 };

int main(int argc, char **argv)
{
	if (argc < 3) {
		puts("Usage: modify_env [file with env] [command...]");
		return 0;
	}
	FILE *f = fopen(argv[1], "r");

	char buf[MAX_ENVVAR_LENGTH+4];
	int ch;

	while ((ch = getc(f)) >= 0) {
		char *b = buf;
		*b++ = ch;

		while ((ch = getc(f)) >= 0 && ch != '=') {
			CHECK(b-buf < MAX_ENVVAR_LENGTH);
			*b++ = ch;
		}

		*b++ = 0;
		const char *var = buf;
		const char *value = b;

		while ((ch = getc(f)) >= 0 && ch != 0) {
			CHECK(b-buf < MAX_ENVVAR_LENGTH);
			*b++ = ch;
		}
		*b = 0;

		setenv(var, value, 0);
	}

	fclose(f);
	unlink(argv[1]);
	execvp(argv[2], &argv[2]);
	perror("execvp()");

	return 0;
}
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their 
applications. Written by three acclaimed leaders in the field, 
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to