|
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> |
|
26 |
|
27 #include "utils.h" |
|
28 #include "compl.h" |
|
29 #include "logprint.h" |
|
30 |
|
31 extern char **environ; |
|
32 |
|
33 // /env [option] [value] |
|
34 static void do_env (char *arg) |
|
35 { |
|
36 char **args = split_arg (arg, 2, 1); |
|
37 char *var = args[0]; |
|
38 char *val; |
|
39 |
|
40 if (var) |
|
41 val = args[1]; |
|
42 |
|
43 if (!var) { |
|
44 // print full variables list |
|
45 GSList *vars = NULL; |
|
46 int max = 0; |
|
47 |
|
48 { |
|
49 char **envvar; |
|
50 |
|
51 for (envvar = environ; *envvar; ++envvar) { |
|
52 char *end = strchr (*envvar, '='); |
|
53 |
|
54 if (end) { |
|
55 int len = end - *envvar; |
|
56 char *name = g_strndup (*envvar, len); |
|
57 |
|
58 if (len > max) |
|
59 max = len; |
|
60 |
|
61 vars = g_slist_prepend (vars, name); |
|
62 } |
|
63 } |
|
64 } |
|
65 |
|
66 if (max) { |
|
67 gchar *format = g_strdup_printf ("%%-%ds = %%s", max); |
|
68 GSList *eel; |
|
69 |
|
70 vars = g_slist_sort (vars, (GCompareFunc) strcmp); |
|
71 |
|
72 for (eel = vars; eel; eel = eel->next) { |
|
73 char *name = (char *) eel->data; |
|
74 char *value = getenv (name); |
|
75 |
|
76 scr_LogPrint (LPRINT_NORMAL, format, name, value); |
|
77 |
|
78 g_free (name); |
|
79 } |
|
80 |
|
81 g_free (format); |
|
82 } |
|
83 |
|
84 g_slist_free (vars); |
|
85 |
|
86 } else if (!val) { |
|
87 // print variable alone |
|
88 char *value = getenv (var); |
|
89 |
|
90 if (value) |
|
91 scr_LogPrint (LPRINT_NORMAL, "%s = %s", var, value); |
|
92 } else |
|
93 // set variable |
|
94 setenv (var, val, 1); |
|
95 |
|
96 free_arg_lst (args); |
|
97 } |
|
98 |
|
99 const gchar *g_module_check_init(GModule *module) |
|
100 { |
|
101 // command |
|
102 cmd_add ("env", "", 0, 0, do_env, NULL); |
|
103 |
|
104 return NULL; |
|
105 } |
|
106 |
|
107 void g_module_unload(GModule *module) |
|
108 { |
|
109 // command |
|
110 cmd_del ("env"); |
|
111 } |
|
112 |
|
113 /* vim: se ts=4: */ |