0
|
1 |
/* |
|
2 |
* env.c -- Tampering with mcabber's environment |
|
3 |
* |
|
4 |
* Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
|
|
22 |
#include <glib.h> |
|
23 |
#include <gmodule.h> |
|
24 |
#include <string.h> |
|
25 |
#include <stdlib.h> |
3
|
26 |
#include <unistd.h> |
|
27 |
#include <errno.h> |
0
|
28 |
|
|
29 |
#include "utils.h" |
|
30 |
#include "compl.h" |
|
31 |
#include "logprint.h" |
|
32 |
|
|
33 |
extern char **environ; |
|
34 |
|
|
35 |
// /env [option] [value] |
|
36 |
static void do_env (char *arg) |
|
37 |
{ |
|
38 |
char **args = split_arg (arg, 2, 1); |
|
39 |
char *var = args[0]; |
|
40 |
char *val; |
|
41 |
|
|
42 |
if (var) |
|
43 |
val = args[1]; |
|
44 |
|
|
45 |
if (!var) { |
|
46 |
// print full variables list |
|
47 |
GSList *vars = NULL; |
|
48 |
int max = 0; |
|
49 |
|
|
50 |
{ |
|
51 |
char **envvar; |
|
52 |
|
|
53 |
for (envvar = environ; *envvar; ++envvar) { |
|
54 |
char *end = strchr (*envvar, '='); |
|
55 |
|
|
56 |
if (end) { |
|
57 |
int len = end - *envvar; |
|
58 |
char *name = g_strndup (*envvar, len); |
|
59 |
|
|
60 |
if (len > max) |
|
61 |
max = len; |
|
62 |
|
|
63 |
vars = g_slist_prepend (vars, name); |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
if (max) { |
|
69 |
gchar *format = g_strdup_printf ("%%-%ds = %%s", max); |
|
70 |
GSList *eel; |
|
71 |
|
|
72 |
vars = g_slist_sort (vars, (GCompareFunc) strcmp); |
|
73 |
|
|
74 |
for (eel = vars; eel; eel = eel->next) { |
|
75 |
char *name = (char *) eel->data; |
|
76 |
char *value = getenv (name); |
|
77 |
|
|
78 |
scr_LogPrint (LPRINT_NORMAL, format, name, value); |
|
79 |
|
|
80 |
g_free (name); |
|
81 |
} |
|
82 |
|
|
83 |
g_free (format); |
|
84 |
} |
|
85 |
|
|
86 |
g_slist_free (vars); |
|
87 |
|
|
88 |
} else if (!val) { |
|
89 |
// print variable alone |
|
90 |
char *value = getenv (var); |
|
91 |
|
|
92 |
if (value) |
|
93 |
scr_LogPrint (LPRINT_NORMAL, "%s = %s", var, value); |
|
94 |
} else |
|
95 |
// set variable |
|
96 |
setenv (var, val, 1); |
|
97 |
|
|
98 |
free_arg_lst (args); |
|
99 |
} |
|
100 |
|
3
|
101 |
// /cd [path] |
|
102 |
static void do_cd (char *arg) |
|
103 |
{ |
|
104 |
if (!*arg) { // get |
|
105 |
char cwd[256]; |
|
106 |
if (getcwd (cwd, 256) == NULL) |
|
107 |
scr_LogPrint (LPRINT_NORMAL, "Cannot obtain current working directory: %s.", strerror (errno)); |
|
108 |
else |
|
109 |
scr_LogPrint (LPRINT_NORMAL, "%s", cwd); |
|
110 |
} else if (chdir (arg) == -1) // set |
|
111 |
scr_LogPrint (LPRINT_NORMAL, "Cannot change current working directory: %s.", strerror (errno)); |
|
112 |
} |
|
113 |
|
0
|
114 |
const gchar *g_module_check_init(GModule *module) |
|
115 |
{ |
|
116 |
// command |
|
117 |
cmd_add ("env", "", 0, 0, do_env, NULL); |
3
|
118 |
cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL); |
0
|
119 |
|
|
120 |
return NULL; |
|
121 |
} |
|
122 |
|
|
123 |
void g_module_unload(GModule *module) |
|
124 |
{ |
|
125 |
// command |
3
|
126 |
cmd_del ("cd"); |
0
|
127 |
cmd_del ("env"); |
|
128 |
} |
|
129 |
|
3
|
130 |
/* vim: se ts=4 sw=4: */ |