/*
* 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 <mcabber/utils.h>
#include <mcabber/compl.h>
#include <mcabber/logprint.h>
extern char **environ;
// /env [option [= [value]]]
static void do_env (char *arg)
{
if (!*arg) {
// 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 ("Env %%-%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 {
char *varend = strchr (arg, '=');
if (!varend) {
// print variable alone
char *value = getenv (arg);
if (value)
scr_LogPrint (LPRINT_NORMAL, "Env %s = %s", arg, value);
} else if (varend == arg)
scr_LogPrint (LPRINT_NORMAL, "You must specify variable name.");
else {
char *argstart = varend + 1;
char *var;
for (varend--; varend > arg && *varend == ' '; --varend);
var = g_strndup (arg, varend - arg + 1);
while (!*argstart && *argstart == ' ')
++argstart;
if (*argstart)
// set variable
setenv (var, argstart, 1);
else
// unset variable
unsetenv (var);
g_free (var);
}
}
}
// /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: */