[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] getenv/setenv/unsetenv



* Florian Bruhin <me _at_ the _minus_ compiler _dot_ org> [2012-11-05 11:52:42 +0100]:
> As requested by maxfragg. Simply calls {get,set,unset}env(3).

As discussed in IRC:

thorsten`       The-Compiler: i even would create another error-code HERBST_ENV_
thorsten`       HERBST_ENV_UNSET
The-Compiler    and what about setenv() then?
The-Compiler    I don't really want to check errno and all that, so I'd prefer just returning HERBST_ENV_ERROR or whatever
thorsten`       yes that's ok
thorsten`       for setenv it's ok.. but i think the check is wrong
The-Compiler    how so?
thorsten`       The setenv() function returns zero on success, or -1 on error

Fixed that in the attached patch

-- 
() ascii ribbon campaign - stop html mail    www.asciiribbon.org
/\ www.the-compiler.org  | I love long mails http://email.is-not-s.ms/
You know it's going to be a long day when you get up, shave and shower, start 
to get dressed and your shoes are still warm. -- Dean Webber 
From be74a9021d597f99cdbfb3796c52f1454bb13af0 Mon Sep 17 00:00:00 2001
From: Florian Bruhin <git _at_ the _minus_ compiler _dot_ org>
Date: Mon, 5 Nov 2012 11:49:11 +0100
Subject: [PATCH] New commands: getenv/setenv/unsetenv

Requested by maxfragg. They could be used like this:

if ! hc getenv SSH_AGENT_PID > /dev/null; then
    hc setenv SSH_AGENT_PID "$SSH_AGENT_PID"
fi
---
 NEWS                 |  1 +
 doc/herbstluftwm.txt |  9 +++++++++
 src/command.c        |  3 +++
 src/ipc-protocol.h   |  1 +
 src/main.c           | 40 +++++++++++++++++++++++++++++++++++++++-
 5 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 16ea50b..3d91deb 100644
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,7 @@ Changes:
     * set the urgent flag on _NET_WM_STATE_DEMANDS_ATTENTION
     * clear the urgent flag on window focus
     * new command: list_padding
+    * new commands: getenv/setenv/unsetenv
 
 Release: 0.4.1 on 2012-08-30
 ----------------------------
diff --git a/doc/herbstluftwm.txt b/doc/herbstluftwm.txt
index bf97816..b74e98b 100644
--- a/doc/herbstluftwm.txt
+++ b/doc/herbstluftwm.txt
@@ -598,6 +598,15 @@ pseudotile *on*|*off*|*toggle*::
     the client size will stay the floating size. The only reason to resize the
     client is to ensure, that it fits into its tile.
 
+getenv 'NAME'::
+    Gets the value of the environment variable 'NAME'.
+
+setenv 'NAME' 'VALUE'::
+    Set the value of the environment variable 'NAME' to 'VALUE'.
+
+unsetenv 'NAME'::
+    Unsets the environment variable 'NAME'.
+
 [[SETTINGS]]
 SETTINGS
 --------
diff --git a/src/command.c b/src/command.c
index e7c9b76..20c1370 100644
--- a/src/command.c
+++ b/src/command.c
@@ -114,6 +114,9 @@ struct {
     { "unrule",         2,  no_completion },
     { "fullscreen",     2,  no_completion },
     { "pseudotile",     2,  no_completion },
+    { "getenv",         2,  no_completion },
+    { "setenv",         3,  no_completion },
+    { "unsetenv",       2,  no_completion },
     { 0 },
 };
 
diff --git a/src/ipc-protocol.h b/src/ipc-protocol.h
index effa207..8038597 100644
--- a/src/ipc-protocol.h
+++ b/src/ipc-protocol.h
@@ -29,6 +29,7 @@ enum {
     HERBST_TAG_IN_USE,
     HERBST_FORBIDDEN,
     HERBST_NO_PARAMETER_EXPECTED,
+    HERBST_ENV_UNSET,
 };
 
 #endif
diff --git a/src/main.c b/src/main.c
index 07be5e6..f31a42e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -60,6 +60,9 @@ int wmexec(int argc, char** argv);
 static void remove_zombies(int signal);
 int custom_hook_emit(int argc, char** argv);
 int jumpto_command(int argc, char** argv);
+int getenv_command(int argc, char** argv, GString* result);
+int setenv_command(int argc, char** argv);
+int unsetenv_command(int argc, char** argv);
 
 // handler for X-Events
 void buttonpress(XEvent* event);
@@ -149,7 +152,10 @@ CommandBinding g_commands[] = {
     CMD_BIND_NO_OUTPUT(   "detect_monitors",detect_monitors_command),
     CMD_BIND(             "chain",          command_chain_command),
     CMD_BIND(             "and",            command_chain_command),
-    CMD_BIND(             "or",            command_chain_command),
+    CMD_BIND(             "or",             command_chain_command),
+    CMD_BIND(             "getenv",         getenv_command),
+    CMD_BIND_NO_OUTPUT(   "setenv",         setenv_command),
+    CMD_BIND_NO_OUTPUT(   "unsetenv",       unsetenv_command),
     {{ NULL }}
 };
 
@@ -352,6 +358,38 @@ int jumpto_command(int argc, char** argv) {
     return 0;
 }
 
+int getenv_command(int argc, char** argv, GString* result) {
+    if (argc < 2) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    char* envvar = getenv(argv[1]);
+    if (envvar == NULL) {
+        return HERBST_ENV_UNSET;
+    }
+    g_string_append_printf(result, "%s\n", envvar);
+    return 0;
+}
+
+int setenv_command(int argc, char** argv) {
+    if (argc < 3) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    if (setenv(argv[1], argv[2], 1) != 0) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    return 0;
+} 
+
+int unsetenv_command(int argc, char** argv) {
+    if (argc < 2) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    if (unsetenv(argv[1]) != 0) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    return 0;
+}
+
 // handle x-events:
 
 void event_on_configure(XEvent event) {
-- 
1.8.0

Attachment: pgp_FIh1qxlFB.pgp
Description: PGP signature