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

[PATCH] getenv/setenv/unsetenv



As requested by maxfragg. Simply calls {get,set,unset}env(3).

-- 
() ascii ribbon campaign - stop html mail    www.asciiribbon.org
/\ www.the-compiler.org  | I love long mails http://email.is-not-s.ms/
It's hard to drive at the limit, but it's harder to know where the limits are. 
-- Stirling Moss 
>From 7a4c70e6f35ba7866a5ff413be51366c70d8ad86 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/main.c           | 42 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 54 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/main.c b/src/main.c
index 07be5e6..f351078 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,40 @@ 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_INVALID_ARGUMENT;
+    }
+    g_string_append_printf(result, "%s\n", envvar);
+    return 0;
+}
+
+int setenv_command(int argc, char** argv) {
+    if (argc < 3) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    int ret = setenv(argv[1], argv[2], 1);
+    if (!ret) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    return 0;
+} 
+
+int unsetenv_command(int argc, char** argv) {
+    if (argc < 2) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    int ret = unsetenv(argv[1]);
+    if (!ret) {
+        return HERBST_INVALID_ARGUMENT;
+    }
+    return 0;
+}
+
 // handle x-events:
 
 void event_on_configure(XEvent event) {
-- 
1.8.0