author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 07 Dec 2009 01:04:25 +0200 | |
changeset 4 | 2c5225ab6eb7 |
parent 3 | 490c62fe90eb |
child 5 | da2fdd072650 |
permissions | -rw-r--r-- |
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 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
35 |
// /env [option [= [value]]] |
0 | 36 |
static void do_env (char *arg) |
37 |
{ |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
38 |
if (!*arg) { |
0 | 39 |
// print full variables list |
40 |
GSList *vars = NULL; |
|
41 |
int max = 0; |
|
42 |
||
43 |
{ |
|
44 |
char **envvar; |
|
45 |
||
46 |
for (envvar = environ; *envvar; ++envvar) { |
|
47 |
char *end = strchr (*envvar, '='); |
|
48 |
||
49 |
if (end) { |
|
50 |
int len = end - *envvar; |
|
51 |
char *name = g_strndup (*envvar, len); |
|
52 |
||
53 |
if (len > max) |
|
54 |
max = len; |
|
55 |
||
56 |
vars = g_slist_prepend (vars, name); |
|
57 |
} |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
if (max) { |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
62 |
gchar *format = g_strdup_printf ("Env %%-%ds = %%s", max); |
0 | 63 |
GSList *eel; |
64 |
||
65 |
vars = g_slist_sort (vars, (GCompareFunc) strcmp); |
|
66 |
||
67 |
for (eel = vars; eel; eel = eel->next) { |
|
68 |
char *name = (char *) eel->data; |
|
69 |
char *value = getenv (name); |
|
70 |
||
71 |
scr_LogPrint (LPRINT_NORMAL, format, name, value); |
|
72 |
||
73 |
g_free (name); |
|
74 |
} |
|
75 |
||
76 |
g_free (format); |
|
77 |
} |
|
78 |
||
79 |
g_slist_free (vars); |
|
80 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
81 |
} else { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
82 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
83 |
char *varend = strchr (arg, '='); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
84 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
85 |
if (!varend) { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
86 |
// print variable alone |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
87 |
char *value = getenv (arg); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
88 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
89 |
if (value) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
90 |
scr_LogPrint (LPRINT_NORMAL, "Env %s = %s", arg, value); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
91 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
92 |
} else if (varend == arg) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
93 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
94 |
scr_LogPrint (LPRINT_NORMAL, "You must specify variable name."); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
95 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
96 |
else { |
0 | 97 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
98 |
char *argstart = varend + 1; |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
99 |
char *var; |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
100 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
101 |
for (varend--; varend > arg && *varend == ' '; --varend); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
102 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
103 |
var = g_strndup (arg, varend - arg + 1); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
104 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
105 |
while (!*argstart && *argstart == ' ') |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
106 |
++argstart; |
0 | 107 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
108 |
if (*argstart) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
109 |
// set variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
110 |
setenv (var, argstart, 1); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
111 |
else |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
112 |
// unset variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
113 |
unsetenv (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
114 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
115 |
g_free (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
116 |
} |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
117 |
} |
0 | 118 |
} |
119 |
||
3 | 120 |
// /cd [path] |
121 |
static void do_cd (char *arg) |
|
122 |
{ |
|
123 |
if (!*arg) { // get |
|
124 |
char cwd[256]; |
|
125 |
if (getcwd (cwd, 256) == NULL) |
|
126 |
scr_LogPrint (LPRINT_NORMAL, "Cannot obtain current working directory: %s.", strerror (errno)); |
|
127 |
else |
|
128 |
scr_LogPrint (LPRINT_NORMAL, "%s", cwd); |
|
129 |
} else if (chdir (arg) == -1) // set |
|
130 |
scr_LogPrint (LPRINT_NORMAL, "Cannot change current working directory: %s.", strerror (errno)); |
|
131 |
} |
|
132 |
||
0 | 133 |
const gchar *g_module_check_init(GModule *module) |
134 |
{ |
|
135 |
// command |
|
136 |
cmd_add ("env", "", 0, 0, do_env, NULL); |
|
3 | 137 |
cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL); |
0 | 138 |
|
139 |
return NULL; |
|
140 |
} |
|
141 |
||
142 |
void g_module_unload(GModule *module) |
|
143 |
{ |
|
144 |
// command |
|
3 | 145 |
cmd_del ("cd"); |
0 | 146 |
cmd_del ("env"); |
147 |
} |
|
148 |
||
3 | 149 |
/* vim: se ts=4 sw=4: */ |