env.c
changeset 0 53ef7728edcf
child 3 490c62fe90eb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/env.c	Fri Nov 06 14:50:29 2009 +0200
@@ -0,0 +1,113 @@
+/*
+ * env.c                -- Tampering with mcabber's environment
+ *
+ * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <glib.h>
+#include <gmodule.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "utils.h"
+#include "compl.h"
+#include "logprint.h"
+
+extern char **environ;
+
+// /env [option] [value]
+static void do_env (char *arg)
+{
+	char **args = split_arg (arg, 2, 1);
+	char  *var  = args[0];
+	char  *val;
+
+	if (var)
+		val = args[1];
+
+	if (!var) {
+		// print full variables list
+		GSList  *vars   = NULL;
+		int      max    = 0;
+
+		{
+			char   **envvar;
+
+			for (envvar = environ; *envvar; ++envvar) {
+				char *end = strchr (*envvar, '=');
+
+				if (end) {
+					int   len  = end - *envvar;
+					char *name = g_strndup (*envvar, len);
+	
+					if (len > max)
+						max = len;
+
+					vars = g_slist_prepend (vars, name);
+				}
+			}
+		}
+
+		if (max) {
+			gchar  *format = g_strdup_printf ("%%-%ds = %%s", max);
+			GSList *eel;
+
+			vars = g_slist_sort (vars, (GCompareFunc) strcmp);
+
+			for (eel = vars; eel; eel = eel->next) {
+				char *name  = (char *) eel->data;
+				char *value = getenv (name);
+
+				scr_LogPrint (LPRINT_NORMAL, format, name, value);
+
+				g_free (name);
+			}
+
+			g_free (format);
+		}
+
+		g_slist_free (vars);
+
+	} else if (!val) {
+		// print variable alone
+		char *value = getenv (var);
+
+		if (value)
+			scr_LogPrint (LPRINT_NORMAL, "%s = %s", var, value);
+	} else
+		// set variable
+		setenv (var, val, 1);
+
+	free_arg_lst (args);
+}
+
+const gchar *g_module_check_init(GModule *module)
+{
+	// command
+	cmd_add ("env", "", 0, 0, do_env, NULL);
+
+	return NULL;
+}
+
+void g_module_unload(GModule *module)
+{
+	// command
+	cmd_del ("env");
+}
+
+/* vim: se ts=4: */