author | Myhailo Danylenko <isbear@ukrpost.net> |
Thu, 25 Mar 2010 20:47:56 +0200 | |
changeset 13 | dbb001e9f800 |
parent 12 | 14cd74b518ea |
child 14 | 61d94594e8fb |
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 |
|
5
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
28 |
#include <mcabber/utils.h> |
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
29 |
#include <mcabber/compl.h> |
da2fdd072650
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
30 |
#include <mcabber/logprint.h> |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
31 |
#include <mcabber/modules.h> |
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
32 |
|
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
33 |
#include "config.h" |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
34 |
|
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
35 |
static void env_load (void); |
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
36 |
static void env_unload (void); |
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
37 |
|
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
38 |
#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
|
39 |
|
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
40 |
static module_info_t info_env_experimental = { |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
41 |
.branch = "experimental", |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
42 |
.api = 10, |
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
43 |
.version = PROJECT_VERSION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
44 |
.description = DESCRIPTION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
45 |
.requires = NULL, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
46 |
.init = env_load, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
47 |
.uninit = env_unload, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
48 |
.next = &info_env_experimental, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
49 |
}; |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
50 |
|
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
51 |
module_info_t info_env = { |
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
52 |
.branch = "dev", |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
53 |
.api = 11, |
12
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
54 |
.version = PROJECT_VERSION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
55 |
.description = DESCRIPTION, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
56 |
.requires = NULL, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
57 |
.init = env_load, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
58 |
.uninit = env_unload, |
14cd74b518ea
Use new module struct with apis
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
59 |
.next = &info_env_experimental, |
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
60 |
}; |
0 | 61 |
|
62 |
extern char **environ; |
|
63 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
64 |
// /env [option [= [value]]] |
0 | 65 |
static void do_env (char *arg) |
66 |
{ |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
67 |
if (!*arg) { |
0 | 68 |
// print full variables list |
69 |
GSList *vars = NULL; |
|
70 |
int max = 0; |
|
71 |
||
72 |
{ |
|
73 |
char **envvar; |
|
74 |
||
75 |
for (envvar = environ; *envvar; ++envvar) { |
|
76 |
char *end = strchr (*envvar, '='); |
|
77 |
||
78 |
if (end) { |
|
79 |
int len = end - *envvar; |
|
80 |
char *name = g_strndup (*envvar, len); |
|
81 |
||
82 |
if (len > max) |
|
83 |
max = len; |
|
84 |
||
85 |
vars = g_slist_prepend (vars, name); |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
if (max) { |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
91 |
gchar *format = g_strdup_printf ("Env %%-%ds = %%s", max); |
0 | 92 |
GSList *eel; |
93 |
||
94 |
vars = g_slist_sort (vars, (GCompareFunc) strcmp); |
|
95 |
||
96 |
for (eel = vars; eel; eel = eel->next) { |
|
97 |
char *name = (char *) eel->data; |
|
98 |
char *value = getenv (name); |
|
99 |
||
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
100 |
scr_log_print (LPRINT_NORMAL, format, name, value); |
0 | 101 |
|
102 |
g_free (name); |
|
103 |
} |
|
104 |
||
105 |
g_free (format); |
|
106 |
} |
|
107 |
||
108 |
g_slist_free (vars); |
|
109 |
||
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
110 |
} else { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
111 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
112 |
char *varend = strchr (arg, '='); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
113 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
114 |
if (!varend) { |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
115 |
// print variable alone |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
116 |
char *value = getenv (arg); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
117 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
118 |
if (value) |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
119 |
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
|
120 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
121 |
} else if (varend == arg) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
122 |
|
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
123 |
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
|
124 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
125 |
else { |
0 | 126 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
127 |
char *argstart = varend + 1; |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
128 |
char *var; |
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 |
for (varend--; varend > arg && *varend == ' '; --varend); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
131 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
132 |
var = g_strndup (arg, varend - arg + 1); |
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 |
while (!*argstart && *argstart == ' ') |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
135 |
++argstart; |
0 | 136 |
|
4
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
137 |
if (*argstart) |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
138 |
// set variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
139 |
setenv (var, argstart, 1); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
140 |
else |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
141 |
// unset variable |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
142 |
unsetenv (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
143 |
|
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
144 |
g_free (var); |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
145 |
} |
2c5225ab6eb7
Changed command syntax to allow unsetting
Myhailo Danylenko <isbear@ukrpost.net>
parents:
3
diff
changeset
|
146 |
} |
0 | 147 |
} |
148 |
||
3 | 149 |
// /cd [path] |
150 |
static void do_cd (char *arg) |
|
151 |
{ |
|
152 |
if (!*arg) { // get |
|
153 |
char cwd[256]; |
|
154 |
if (getcwd (cwd, 256) == NULL) |
|
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
155 |
scr_log_print (LPRINT_NORMAL, "Cannot obtain current working directory: %s.", strerror (errno)); |
3 | 156 |
else |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
157 |
scr_log_print (LPRINT_NORMAL, "%s", cwd); |
3 | 158 |
} else if (chdir (arg) == -1) // set |
13
dbb001e9f800
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
159 |
scr_log_print (LPRINT_NORMAL, "Cannot change current working directory: %s.", strerror (errno)); |
3 | 160 |
} |
161 |
||
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
162 |
static void env_load (void) |
0 | 163 |
{ |
164 |
// command |
|
165 |
cmd_add ("env", "", 0, 0, do_env, NULL); |
|
3 | 166 |
cmd_add ("cd", "", COMPL_FILENAME, 0, do_cd, NULL); |
0 | 167 |
} |
168 |
||
11
dc7bcc0fa5e1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
169 |
static void env_unload (void) |
0 | 170 |
{ |
171 |
// command |
|
3 | 172 |
cmd_del ("cd"); |
0 | 173 |
cmd_del ("env"); |
174 |
} |
|
175 |
||
3 | 176 |
/* vim: se ts=4 sw=4: */ |