env.c
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 13 Nov 2009 17:48:13 +0200
changeset 3 490c62fe90eb
parent 0 53ef7728edcf
child 4 2c5225ab6eb7
permissions -rw-r--r--
Added command cd

/*
 * 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 <unistd.h>
#include <errno.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);
}

// /cd [path]
static void do_cd (char *arg)
{
	if (!*arg) { // get
		char cwd[256];
		if (getcwd (cwd, 256) == NULL)
			scr_LogPrint (LPRINT_NORMAL, "Cannot obtain current working directory: %s.", strerror (errno));
		else
			scr_LogPrint (LPRINT_NORMAL, "%s", cwd);
	} else if (chdir (arg) == -1) // set
		scr_LogPrint (LPRINT_NORMAL, "Cannot change current working directory: %s.", strerror (errno));
}

const gchar *g_module_check_init(GModule *module)
{
	// command
	cmd_add ("env", "", 0, 0, do_env, NULL);
	cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL);

	return NULL;
}

void g_module_unload(GModule *module)
{
	// command
	cmd_del ("cd");
	cmd_del ("env");
}

/* vim: se ts=4 sw=4: */