1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*
* Copyright (C) 2008 Dmitry Kobylin <fnfal@academ.tsc.ru>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* weechat-tcl.c: Tcl plugin for WeeChat */
#undef _
#include <tcl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "../../weechat-plugin.h"
#include "../script.h"
#include "weechat-tcl.h"
#include "weechat-tcl-api.h"
WEECHAT_PLUGIN_NAME(TCL_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION("Tcl plugin for WeeChat");
WEECHAT_PLUGIN_AUTHOR("Dmitry Kobylin <fnfal@academ.tsc.ru>");
WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION);
WEECHAT_PLUGIN_WEECHAT_VERSION(WEECHAT_VERSION);
WEECHAT_PLUGIN_LICENSE("GPL3");
struct t_weechat_plugin *weechat_tcl_plugin = NULL;
struct t_plugin_script *tcl_scripts = NULL;
struct t_plugin_script *tcl_current_script = NULL;
const char *tcl_current_script_filename = NULL;
Tcl_Interp* cinterp;
/*
* weechat_tcl_exec: execute a Tcl script
*/
void *
weechat_tcl_exec (struct t_plugin_script *script,
int ret_type, const char *function, char **argv)
{
int i;
int *ret_i;
char *ret_cv;
void *ret_val;
Tcl_DString ds;
Tcl_Obj *scptr;
Tcl_Interp *interp;
struct t_plugin_script *old_tcl_script;
old_tcl_script = tcl_current_script;
tcl_current_script = script;
interp = (Tcl_Interp*)script->interpreter;
Tcl_DStringInit (&ds);
if (function && function[0])
Tcl_DStringAppend (&ds,function, -1);
else
{
tcl_current_script = old_tcl_script;
return NULL;
}
if (argv)
{
for (i = 0; argv[i]; i++)
{
Tcl_DStringAppend (&ds, " \"", -1);
Tcl_DStringAppend (&ds, argv[i], -1);
Tcl_DStringAppend (&ds, "\"", -1);
}
}
scptr = Tcl_NewStringObj (Tcl_DStringValue(&ds), -1);
Tcl_IncrRefCount (scptr);
Tcl_DStringFree (&ds);
if (Tcl_EvalObjEx (interp, scptr, TCL_EVAL_DIRECT) == TCL_OK)
{
Tcl_DecrRefCount (scptr);
ret_val = NULL;
if (ret_type == WEECHAT_SCRIPT_EXEC_STRING)
{
ret_cv = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &i);
if (ret_cv && ret_cv[0])
ret_val = (void *)strdup (ret_cv);
else
ret_val = NULL;
}
else if ( ret_type == WEECHAT_SCRIPT_EXEC_INT
&& Tcl_GetIntFromObj (interp, Tcl_GetObjResult (interp), &i) == TCL_OK)
{
ret_i = (int *)malloc (sizeof (*ret_i));
if (ret_i)
*ret_i = i;
ret_val = (void *)ret_i;
}
tcl_current_script = old_tcl_script;
if (ret_val)
return ret_val;
weechat_printf (NULL,
weechat_gettext ("%s%s: function \"%s\" must return a "
"valid value"),
weechat_prefix ("error"), TCL_PLUGIN_NAME, function);
return NULL;
}
Tcl_DecrRefCount(scptr);
weechat_printf (NULL,
weechat_gettext ("%s%s unable to run function \"%s\": %s"),
weechat_prefix ("error"), TCL_PLUGIN_NAME, function,
Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &i));
tcl_current_script = old_tcl_script;
return NULL;
}
/*
* weechat_tcl_load: load a Tcl script
*/
int
weechat_tcl_load (const char *filename)
{
int i;
Tcl_Interp *interp;
struct stat buf;
if (stat (filename, &buf) != 0)
{
weechat_printf (NULL,
weechat_gettext ("%s%s: script \"%s\" not found"),
weechat_prefix ("error"), TCL_PLUGIN_NAME, filename);
return 0;
}
weechat_printf (NULL,
weechat_gettext ("%s: loading script \"%s\""),
TCL_PLUGIN_NAME, filename);
tcl_current_script = NULL;
if (!(interp = Tcl_CreateInterp ())) {
weechat_printf (NULL,
weechat_gettext ("%s%s: unable to create new "
"interpreter"),
weechat_prefix ("error"), TCL_PLUGIN_NAME);
return 0;
}
tcl_current_script_filename = filename;
weechat_tcl_api_init (interp);
if (Tcl_EvalFile (interp, filename) != TCL_OK)
{
weechat_printf (NULL,
weechat_gettext ("%s%s: error occured while "
"parsing file \"%s\": %s"),
weechat_prefix ("error"), TCL_PLUGIN_NAME, filename,
Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &i));
/* this ok, maybe "register" was called, so not return */
/* return 0; */
}
if (!tcl_current_script)
{
weechat_printf (NULL,
weechat_gettext ("%s%s: function \"register\" not "
"found (or failed) in file \"%s\""),
weechat_prefix ("error"), TCL_PLUGIN_NAME, filename);
Tcl_DeleteInterp (interp);
return 0;
}
return 1;
}
/*
* weechat_tcl_load_cb: callback for weechat_script_auto_load() function
*/
void
weechat_tcl_load_cb (void *data, const char *filename)
{
/* make C compiler happy */
(void) data;
weechat_tcl_load (filename);
}
/*
* weechat_tcl_unload: unload a Tcl script
*/
void
weechat_tcl_unload (struct t_plugin_script *script)
{
Tcl_Interp* interp;
void *pointer;
weechat_printf (NULL,
weechat_gettext ("%s: unloading script \"%s\""),
TCL_PLUGIN_NAME, script->name);
if (script->shutdown_func && script->shutdown_func[0])
{
pointer = weechat_tcl_exec (script,
WEECHAT_SCRIPT_EXEC_INT,
script->shutdown_func,
NULL);
if (pointer)
free (pointer);
}
interp = (Tcl_Interp*)script->interpreter;
script_remove (weechat_tcl_plugin, &tcl_scripts, script);
Tcl_DeleteInterp(interp);
}
/*
* weechat_tcl_unload_name: unload a Tcl script by name
*/
void
weechat_tcl_unload_name (const char *name)
{
struct t_plugin_script *ptr_script;
ptr_script = script_search (weechat_tcl_plugin, tcl_scripts, name);
if (ptr_script)
{
weechat_tcl_unload (ptr_script);
weechat_printf (NULL,
weechat_gettext ("%s: script \"%s\" unloaded"),
TCL_PLUGIN_NAME, name);
}
else
{
weechat_printf (NULL,
weechat_gettext ("%s%s: script \"%s\" not loaded"),
weechat_prefix ("error"), TCL_PLUGIN_NAME, name);
}
}
/*
* weechat_tcl_unload_all: unload all Tcl scripts
*/
void
weechat_tcl_unload_all ()
{
while (tcl_scripts)
{
weechat_tcl_unload (tcl_scripts);
}
}
/*
* weechat_tcl_command_cb: callback for "/tcl" command
*/
int
weechat_tcl_command_cb (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
char *path_script;
/* make C compiler happy */
(void) data;
(void) buffer;
if (argc == 1)
{
script_display_list (weechat_tcl_plugin, tcl_scripts,
NULL, 0);
}
else if (argc == 2)
{
if (weechat_strcasecmp (argv[1], "list") == 0)
{
script_display_list (weechat_tcl_plugin, tcl_scripts,
NULL, 0);
}
else if (weechat_strcasecmp (argv[1], "listfull") == 0)
{
script_display_list (weechat_tcl_plugin, tcl_scripts,
NULL, 1);
}
else if (weechat_strcasecmp (argv[1], "autoload") == 0)
{
script_auto_load (weechat_tcl_plugin, &weechat_tcl_load_cb);
}
else if (weechat_strcasecmp (argv[1], "reload") == 0)
{
weechat_tcl_unload_all ();
script_auto_load (weechat_tcl_plugin, &weechat_tcl_load_cb);
}
else if (weechat_strcasecmp (argv[1], "unload") == 0)
{
weechat_tcl_unload_all ();
}
}
else
{
if (weechat_strcasecmp (argv[1], "list") == 0)
{
script_display_list (weechat_tcl_plugin, tcl_scripts,
argv_eol[2], 0);
}
else if (weechat_strcasecmp (argv[1], "listfull") == 0)
{
script_display_list (weechat_tcl_plugin, tcl_scripts,
argv_eol[2], 1);
}
else if (weechat_strcasecmp (argv[1], "load") == 0)
{
/* load Tcl script */
path_script = script_search_full_name (weechat_tcl_plugin,
argv_eol[2]);
weechat_tcl_load ((path_script) ? path_script : argv_eol[2]);
if (path_script)
free (path_script);
}
else if (weechat_strcasecmp (argv[1], "unload") == 0)
{
/* unload Tcl script */
weechat_tcl_unload_name (argv_eol[2]);
}
else
{
weechat_printf (NULL,
weechat_gettext ("%s%s: unknown option for "
"command \"%s\""),
weechat_prefix ("error"), TCL_PLUGIN_NAME, "tcl");
}
}
return WEECHAT_RC_OK;
}
/*
* weechat_tcl_completion_cb: callback for script completion
*/
int
weechat_tcl_completion_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
script_completion (weechat_tcl_plugin, completion, tcl_scripts);
return WEECHAT_RC_OK;
}
/*
* weechat_tcl_debug_dump_cb: dump Tcl plugin data in WeeChat log file
*/
int
weechat_tcl_debug_dump_cb (void *data, const char *signal, const char *type_data,
void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
(void) signal_data;
script_print_log (weechat_tcl_plugin, tcl_scripts);
return WEECHAT_RC_OK;
}
/*
* weechat_tcl_buffer_closed_cb: callback called when a buffer is closed
*/
int
weechat_tcl_buffer_closed_cb (void *data, const char *signal, const char *type_data,
void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
if (signal_data)
script_remove_buffer_callbacks (tcl_scripts, signal_data);
return WEECHAT_RC_OK;
}
/*
* weechat_plugin_init: initialize Tcl plugin
*/
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
/* make C compiler happy */
(void) argc;
(void) argv;
weechat_tcl_plugin = plugin;
script_init (weechat_tcl_plugin,
weechat_tcl_command_cb,
weechat_tcl_completion_cb,
weechat_tcl_debug_dump_cb,
weechat_tcl_buffer_closed_cb,
weechat_tcl_load_cb);
/* init ok */
return WEECHAT_RC_OK;
}
/*
* weechat_plugin_end: end Tcl plugin
*/
int
weechat_plugin_end (struct t_weechat_plugin *plugin)
{
(void) plugin;
/* unload all scripts */
weechat_tcl_unload_all ();
return WEECHAT_RC_OK;
}
|