author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 03 May 2011 14:44:28 +0300 | |
changeset 21 | 8b61328ab7c1 |
parent 18 | 962e2fc6a708 |
child 22 | b71ca771fa1a |
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 <string.h> |
|
24 |
#include <stdlib.h> |
|
3 | 25 |
#include <unistd.h> |
26 |
#include <errno.h> |
|
0 | 27 |
|
14
61d94594e8fb
Fix compilation warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
28 |
#include <mcabber/commands.h> |
5
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
29 |
#include <mcabber/utils.h> |
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
30 |
#include <mcabber/compl.h> |
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
31 |
#include <mcabber/logprint.h> |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
32 |
#include <mcabber/modules.h> |
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
33 |
#include <mcabber/api.h> |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
34 |
|
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
35 |
#include "config.h" |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
36 |
|
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
37 |
void env_load (void); |
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
38 |
void env_unload (void); |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
39 |
|
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
40 |
#define DESCRIPTION ( "Some debugging routines\nProvides commands /env and /cd" ) |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
41 |
|
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
42 |
static module_info_t info_env_dev = { |
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
43 |
.branch = "dev", |
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
44 |
.api = 20, |
18
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
45 |
.version = PROJECT_VERSION, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
46 |
.description = DESCRIPTION, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
47 |
.requires = NULL, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
48 |
.init = env_load, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
49 |
.uninit = env_unload, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
50 |
.next = NULL, |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
51 |
}; |
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
52 |
|
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
53 |
module_info_t info_env = { |
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
54 |
.branch = "0.10.1", |
18
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
55 |
.api = 1, |
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
56 |
.version = PROJECT_VERSION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
57 |
.description = DESCRIPTION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
58 |
.requires = NULL, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
59 |
.init = env_load, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
60 |
.uninit = env_unload, |
18
962e2fc6a708
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
61 |
.next = &info_env_dev, |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
62 |
}; |
0 | 63 |
|
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
64 |
#ifdef MCABBER_API_HAVE_CMD_ID |
16
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
65 |
static gpointer env_env_cmid = NULL; |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
66 |
static gpointer env_cd_cmid = NULL; |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
67 |
static gboolean env_env_set_safe = FALSE; |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
68 |
static gboolean env_cd_set_safe = FALSE; |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
69 |
#endif |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
70 |
|
0 | 71 |
extern char **environ; |
72 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
73 |
// /env [option [= [value]]] |
0 | 74 |
static void do_env (char *arg) |
75 |
{ |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
76 |
if (!*arg) { |
0 | 77 |
// print full variables list |
78 |
GSList *vars = NULL; |
|
79 |
int max = 0; |
|
80 |
||
81 |
{ |
|
82 |
char **envvar; |
|
83 |
||
84 |
for (envvar = environ; *envvar; ++envvar) { |
|
85 |
char *end = strchr (*envvar, '='); |
|
86 |
||
87 |
if (end) { |
|
88 |
int len = end - *envvar; |
|
89 |
char *name = g_strndup (*envvar, len); |
|
90 |
||
91 |
if (len > max) |
|
92 |
max = len; |
|
93 |
||
94 |
vars = g_slist_prepend (vars, name); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
if (max) { |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
100 |
gchar *format = g_strdup_printf ("Env %%-%ds = %%s", max); |
0 | 101 |
GSList *eel; |
102 |
||
103 |
vars = g_slist_sort (vars, (GCompareFunc) strcmp); |
|
104 |
||
105 |
for (eel = vars; eel; eel = eel->next) { |
|
106 |
char *name = (char *) eel->data; |
|
107 |
char *value = getenv (name); |
|
108 |
||
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
109 |
scr_log_print (LPRINT_NORMAL, format, name, value); |
0 | 110 |
|
111 |
g_free (name); |
|
112 |
} |
|
113 |
||
114 |
g_free (format); |
|
115 |
} |
|
116 |
||
117 |
g_slist_free (vars); |
|
118 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
119 |
} else { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
120 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
121 |
char *varend = strchr (arg, '='); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
122 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
123 |
if (!varend) { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
124 |
// print variable alone |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
125 |
char *value = getenv (arg); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
126 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
127 |
if (value) |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
128 |
scr_log_print (LPRINT_NORMAL, "Env %s = %s", arg, value); |
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
129 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
130 |
} else if (varend == arg) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
131 |
|
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
132 |
scr_log_print (LPRINT_NORMAL, "You must specify variable name."); |
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
133 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
134 |
else { |
0 | 135 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
136 |
char *argstart = varend + 1; |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
137 |
char *var; |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
138 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
139 |
for (varend--; varend > arg && *varend == ' '; --varend); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
140 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
141 |
var = g_strndup (arg, varend - arg + 1); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
142 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
143 |
while (!*argstart && *argstart == ' ') |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
144 |
++argstart; |
0 | 145 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
146 |
if (*argstart) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
147 |
// set variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
148 |
setenv (var, argstart, 1); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
149 |
else |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
150 |
// unset variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
151 |
unsetenv (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
152 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
153 |
g_free (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
154 |
} |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
155 |
} |
0 | 156 |
} |
157 |
||
3 | 158 |
// /cd [path] |
159 |
static void do_cd (char *arg) |
|
160 |
{ |
|
161 |
if (!*arg) { // get |
|
162 |
char cwd[256]; |
|
163 |
if (getcwd (cwd, 256) == NULL) |
|
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
164 |
scr_log_print (LPRINT_NORMAL, "Cannot obtain current working directory: %s.", strerror (errno)); |
3 | 165 |
else |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
166 |
scr_log_print (LPRINT_NORMAL, "%s", cwd); |
3 | 167 |
} else if (chdir (arg) == -1) // set |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
168 |
scr_log_print (LPRINT_NORMAL, "Cannot change current working directory: %s.", strerror (errno)); |
3 | 169 |
} |
170 |
||
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
171 |
void env_load (void) |
0 | 172 |
{ |
173 |
// command |
|
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
174 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 175 |
cmd_add ("env", "", 0, 0, do_env, NULL); |
3 | 176 |
cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL); |
16
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
177 |
#else |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
178 |
env_env_cmid = cmd_add ("env", "", 0, 0, do_env, NULL); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
179 |
env_cd_cmid = cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
180 |
env_env_set_safe = cmd_set_safe ("env", TRUE); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
181 |
env_cd_set_safe = cmd_set_safe ("cd", TRUE); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
182 |
#endif |
0 | 183 |
} |
184 |
||
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
185 |
void env_unload (void) |
0 | 186 |
{ |
187 |
// command |
|
21
8b61328ab7c1
Update versions & add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
188 |
#ifndef MCABBER_API_HAVE_CMD_ID |
3 | 189 |
cmd_del ("cd"); |
0 | 190 |
cmd_del ("env"); |
16
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
191 |
#else |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
192 |
if (env_env_cmid) |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
193 |
cmd_del (env_env_cmid); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
194 |
if (env_cd_cmid) |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
195 |
cmd_del (env_cd_cmid); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
196 |
if (env_env_set_safe) |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
197 |
cmd_set_safe ("env", FALSE); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
198 |
if (env_cd_set_safe) |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
199 |
cmd_set_safe ("cd", FALSE); |
543bd8927354
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
200 |
#endif |
0 | 201 |
} |
202 |
||
3 | 203 |
/* vim: se ts=4 sw=4: */ |