author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 15 Feb 2010 19:24:10 +0200 | |
changeset 4 | b4b586665a8f |
parent 1 | 9eb794ebef85 |
child 5 | c403a516dc04 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
* |
|
4 |
* This file is part of mcabber-templatecmd |
|
5 |
* |
|
6 |
* mcabber-templatecmd 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 |
|
9 |
* (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU 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, see <http://www.gnu.org/licenses/>. */ |
|
18 |
||
19 |
#include <glib.h> |
|
20 |
#include <gmodule.h> |
|
21 |
#include <stdlib.h> |
|
22 |
#include <string.h> |
|
23 |
||
1
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
24 |
#include <mcabber/compl.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
25 |
#include <mcabber/commands.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
26 |
#include <mcabber/utils.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
27 |
#include <mcabber/logprint.h> |
0 | 28 |
|
29 |
typedef struct { |
|
30 |
gchar *name; |
|
31 |
gchar *template; |
|
32 |
int maxarg; |
|
33 |
gboolean stararg; |
|
34 |
} tcmd_t; |
|
35 |
||
36 |
static GSList *template_commands = NULL; |
|
37 |
||
38 |
static void tcmd_callback (char *arg, gpointer udata) |
|
39 |
{ |
|
40 |
tcmd_t *tcmd = (tcmd_t *) udata; |
|
41 |
char **args = NULL; |
|
42 |
GString *command = g_string_new (NULL); |
|
43 |
char *template = tcmd -> template; |
|
44 |
char *p; |
|
45 |
||
46 |
if (tcmd -> maxarg > 0) |
|
47 |
args = split_arg (arg, tcmd -> maxarg, TRUE); |
|
48 |
||
49 |
for (p = template; *p; ++p) { |
|
50 |
if (*p != '$' || !*(p + 1) || !strchr ("*$0123456789", *(p + 1))) { |
|
51 |
g_string_append_c (command, *p); |
|
52 |
continue; |
|
53 |
} |
|
54 |
||
55 |
++p; |
|
56 |
||
57 |
if (*p == '*') |
|
58 |
g_string_append (command, arg); |
|
59 |
else if (*p == '$') |
|
60 |
g_string_append_c (command, '$'); |
|
61 |
else { |
|
62 |
int argnum = atoi (p); |
|
63 |
||
64 |
if (argnum == 0) |
|
65 |
g_string_append (command, tcmd -> name); |
|
66 |
else if (args[argnum - 1]) |
|
67 |
g_string_append (command, args[argnum - 1]); |
|
68 |
||
69 |
while (*(p + 1) && strchr ("0123456789", *(p + 1))) |
|
70 |
++p; |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
{ |
|
75 |
char *cmd = g_string_free (command, FALSE); |
|
76 |
process_command (cmd, 1); |
|
77 |
g_free (cmd); |
|
78 |
} |
|
79 |
||
80 |
if (args) |
|
81 |
free_arg_lst (args); |
|
82 |
} |
|
83 |
||
84 |
static void do_templatecmd (char *arg) |
|
85 |
{ |
|
86 |
if (!*arg) { // list tcmds |
|
87 |
||
88 |
GSList *tel; |
|
89 |
||
90 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
91 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
92 |
scr_LogPrint (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
|
93 |
} |
|
94 |
||
95 |
} else { |
|
96 |
||
97 |
char *nend = strchr (arg, '='); |
|
98 |
||
99 |
if (!nend) { // show tcmd |
|
100 |
||
101 |
GSList *tel; |
|
102 |
||
103 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
104 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
105 |
||
106 |
if (!strcmp (arg, tcmd -> name)) { |
|
107 |
scr_LogPrint (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
|
108 |
return; |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
scr_LogPrint (LPRINT_NORMAL, "No template with such name."); |
|
113 |
||
114 |
} else if (nend == arg) // error |
|
115 |
||
116 |
scr_LogPrint (LPRINT_NORMAL, "You must specify command name."); |
|
117 |
||
118 |
else { // new/modify tcmd |
|
119 |
||
120 |
GSList *tel; |
|
121 |
tcmd_t *template_command = NULL; |
|
122 |
char *astart = nend + 1; |
|
123 |
int len; |
|
124 |
||
125 |
for (--nend; nend > arg && *nend == ' '; --nend); |
|
126 |
len = nend + 1 - arg; |
|
127 |
||
128 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
129 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
130 |
||
131 |
if (!strncmp (arg, tcmd -> name, len)) { |
|
132 |
template_command = tcmd; |
|
133 |
break; |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
for (;*astart && *astart == ' '; ++astart); |
|
138 |
||
139 |
if (!*astart) { // delete tcmd |
|
140 |
if (template_command) { |
|
141 |
cmd_del (template_command -> name); |
|
142 |
template_commands = g_slist_remove (template_commands, template_command); |
|
143 |
g_free (template_command -> name); |
|
144 |
g_free (template_command -> template); |
|
145 |
g_free (template_command); |
|
146 |
} |
|
147 |
return; |
|
148 |
} |
|
149 |
||
150 |
if (!template_command) { |
|
151 |
template_command = g_new (tcmd_t, 1); |
|
152 |
template_command -> name = g_strndup (arg, len); |
|
153 |
} else |
|
154 |
g_free (template_command -> template); |
|
155 |
||
156 |
template_command -> template = g_strdup (astart); |
|
157 |
||
158 |
{ |
|
159 |
int maxarg = -1; |
|
160 |
gboolean stararg = FALSE; |
|
161 |
||
162 |
for (astart = strchr (astart, '$'); astart; astart = strchr (astart, '$')) { |
|
163 |
++astart; |
|
164 |
||
165 |
if (!*astart) |
|
166 |
break; |
|
167 |
||
168 |
if (strchr ("0123456789", *astart)) { |
|
169 |
int anum = atoi (astart); |
|
170 |
||
171 |
if (maxarg < anum) |
|
172 |
maxarg = anum; |
|
173 |
||
174 |
} else if (*astart == '*') |
|
175 |
stararg = TRUE; |
|
176 |
} |
|
177 |
||
178 |
template_command -> maxarg = maxarg; |
|
179 |
template_command -> stararg = stararg; |
|
180 |
} |
|
181 |
||
182 |
template_commands = g_slist_append (template_commands, template_command); |
|
183 |
||
184 |
cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command); |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
||
190 |
const gchar *g_module_check_init (GModule *module) |
|
191 |
{ |
|
192 |
cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL); |
|
193 |
||
194 |
return NULL; |
|
195 |
} |
|
196 |
||
197 |
void g_module_unload (GModule *module) |
|
198 |
{ |
|
199 |
GSList *tel; |
|
200 |
||
201 |
cmd_del ("templatecmd"); |
|
202 |
||
203 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
204 |
tcmd_t *template_command = (tcmd_t *) tel -> data; |
|
205 |
cmd_del (template_command -> name); |
|
206 |
g_free (template_command -> name); |
|
207 |
g_free (template_command -> template); |
|
208 |
g_free (template_command); |
|
209 |
} |
|
210 |
||
211 |
g_slist_free (template_commands); |
|
212 |
} |
|
213 |
||
214 |
/* vim: se ts=4 sw=4: */ |