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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/*
special-vars.c : irssi
Copyright (C) 2000 Timo Sirainen
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "signals.h"
#include "special-vars.h"
#include "expandos.h"
#include "settings.h"
#include "misc.h"
#define ALIGN_RIGHT 0x01
#define ALIGN_CUT 0x02
#define isvarchar(c) \
(isalnum(c) || (c) == '_')
static SPECIAL_HISTORY_FUNC history_func = NULL;
static char *get_argument(char **cmd, char **arglist)
{
GString *str;
char *ret;
int max, arg, argcount;
arg = 0;
max = -1;
argcount = strarray_length(arglist);
if (**cmd == '*') {
/* get all arguments */
} else if (**cmd == '~') {
/* get last argument */
arg = max = argcount-1;
} else {
if (isdigit(**cmd)) {
/* first argument */
arg = max = (**cmd)-'0';
(*cmd)++;
}
if (**cmd == '-') {
/* get more than one argument */
(*cmd)++;
if (!isdigit(**cmd))
max = -1; /* get all the rest */
else {
max = (**cmd)-'0';
(*cmd)++;
}
}
(*cmd)--;
}
str = g_string_new(NULL);
while (arg < argcount && (arg <= max || max == -1)) {
g_string_append(str, arglist[arg]);
g_string_append_c(str, ' ');
arg++;
}
if (str->len > 0) g_string_truncate(str, str->len-1);
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
static char *get_internal_setting(const char *key, int type, int *free_ret)
{
switch (type) {
case SETTING_TYPE_BOOLEAN:
return settings_get_bool(key) ? "yes" : "no";
case SETTING_TYPE_INT:
*free_ret = TRUE;
return g_strdup_printf("%d", settings_get_int(key));
case SETTING_TYPE_STRING:
return (char *) settings_get_str(key);
}
return NULL;
}
static char *get_long_variable_value(const char *key, SERVER_REC *server,
void *item, int *free_ret)
{
EXPANDO_FUNC func;
char *ret;
int type;
*free_ret = FALSE;
/* expando? */
func = expando_find_long(key);
if (func != NULL)
return func(server, item, free_ret);
/* internal setting? */
type = settings_get_type(key);
if (type != -1)
return get_internal_setting(key, type, free_ret);
/* environment variable? */
ret = g_getenv(key);
if (ret != NULL)
return ret;
return NULL;
}
static char *get_long_variable(char **cmd, SERVER_REC *server,
void *item, int *free_ret, int getname)
{
char *start, *var, *ret;
/* get variable name */
start = *cmd;
while (isvarchar((*cmd)[1])) (*cmd)++;
var = g_strndup(start, (int) (*cmd-start)+1);
if (getname) {
*free_ret = TRUE;
return var;
}
ret = get_long_variable_value(var, server, item, free_ret);
g_free(var);
return ret;
}
/* return the value of the variable found from `cmd'.
if 'getname' is TRUE, return the name of the variable instead it's value */
static char *get_variable(char **cmd, SERVER_REC *server, void *item,
char **arglist, int *free_ret, int *arg_used,
int getname)
{
EXPANDO_FUNC func;
if (isdigit(**cmd) || **cmd == '*' || **cmd == '-' || **cmd == '~') {
/* argument */
*free_ret = TRUE;
if (arg_used != NULL) *arg_used = TRUE;
return getname ? g_strdup_printf("%c", **cmd) :
get_argument(cmd, arglist);
}
if (isalpha(**cmd) && isvarchar((*cmd)[1])) {
/* long variable name.. */
return get_long_variable(cmd, server, item, free_ret, getname);
}
/* single character variable. */
if (getname) {
*free_ret = TRUE;
return g_strdup_printf("%c", **cmd);
}
*free_ret = FALSE;
func = expando_find_char(**cmd);
return func == NULL ? NULL : func(server, item, free_ret);
}
static char *get_history(char **cmd, void *item, int *free_ret)
{
char *start, *text, *ret;
/* get variable name */
start = ++(*cmd);
while (**cmd != '\0' && **cmd != '!') (*cmd)++;
if (history_func == NULL)
ret = NULL;
else {
text = g_strndup(start, (int) (*cmd-start)+1);
ret = history_func(text, item, free_ret);
g_free(text);
}
if (**cmd == '\0') (*cmd)--;
return ret;
}
static char *get_special_value(char **cmd, SERVER_REC *server, void *item,
char **arglist, int *free_ret, int *arg_used,
int flags)
{
char command, *value, *p;
int len;
if (**cmd == '!') {
/* find text from command history */
if (flags & PARSE_FLAG_GETNAME)
return "!";
return get_history(cmd, item, free_ret);
}
command = 0;
if (**cmd == '#' || **cmd == '@') {
command = **cmd;
if ((*cmd)[1] != '\0')
(*cmd)++;
else {
/* default to $* */
char *temp_cmd = "*";
if (flags & PARSE_FLAG_GETNAME)
return "*";
*free_ret = TRUE;
return get_argument(&temp_cmd, arglist);
}
}
value = get_variable(cmd, server, item, arglist, free_ret,
arg_used, flags & PARSE_FLAG_GETNAME);
if (flags & PARSE_FLAG_GETNAME)
return value;
if (command == '#') {
/* number of words */
if (value == NULL || *value == '\0') {
if (value != NULL && *free_ret) {
g_free(value);
*free_ret = FALSE;
}
return "0";
}
len = 1;
for (p = value; *p != '\0'; p++) {
if (*p == ' ' && (p[1] != ' ' && p[1] != '\0'))
len++;
}
if (*free_ret) g_free(value);
*free_ret = TRUE;
return g_strdup_printf("%d", len);
}
if (command == '@') {
/* number of characters */
if (value == NULL) return "0";
len = strlen(value);
if (*free_ret) g_free(value);
*free_ret = TRUE;
return g_strdup_printf("%d", len);
}
return value;
}
/* get alignment arguments (inside the []) */
static int get_alignment_args(char **data, int *align, int *flags, char *pad)
{
char *str;
*align = 0;
*flags = ALIGN_CUT;
*pad = ' ';
/* '!' = don't cut, '-' = right padding */
str = *data;
while (*str != '\0' && *str != ']' && !isdigit(*str)) {
if (*str == '!')
*flags &= ~ALIGN_CUT;
else if (*str == '-')
*flags |= ALIGN_RIGHT;
str++;
}
if (!isdigit(*str))
return FALSE; /* expecting number */
/* get the alignment size */
while (isdigit(*str)) {
*align = (*align) * 10 + (*str-'0');
str++;
}
/* get the pad character */
while (*str != '\0' && *str != ']') {
*pad = *str;
str++;
}
if (*str++ != ']') return FALSE;
*data = str;
return TRUE;
}
/* return the aligned text */
static char *get_alignment(const char *text, int align, int flags, char pad)
{
GString *str;
char *ret;
g_return_val_if_fail(text != NULL, NULL);
str = g_string_new(text);
/* cut */
if ((flags & ALIGN_CUT) && align > 0 && str->len > align)
g_string_truncate(str, align);
/* add pad characters */
while (str->len < align) {
if (flags & ALIGN_RIGHT)
g_string_prepend_c(str, pad);
else
g_string_append_c(str, pad);
}
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
/* Parse and expand text after '$' character. return value has to be
g_free()'d if `free_ret' is TRUE. */
char *parse_special(char **cmd, SERVER_REC *server, void *item,
char **arglist, int *free_ret, int *arg_used, int flags)
{
static char **nested_orig_cmd = NULL; /* FIXME: KLUDGE! */
char command, *value;
char align_pad;
int align, align_flags;
char *nest_value;
int brackets, nest_free;
*free_ret = FALSE;
command = **cmd; (*cmd)++;
switch (command) {
case '[':
/* alignment */
if (!get_alignment_args(cmd, &align, &align_flags,
&align_pad) || **cmd == '\0') {
(*cmd)--;
return NULL;
}
break;
default:
command = 0;
(*cmd)--;
}
nest_free = FALSE; nest_value = NULL;
if (**cmd == '(') {
/* subvariable */
int toplevel = nested_orig_cmd == NULL;
if (toplevel) nested_orig_cmd = cmd;
(*cmd)++;
if (**cmd != '$') {
/* ... */
nest_value = *cmd;
} else {
(*cmd)++;
nest_value = parse_special(cmd, server, item, arglist,
&nest_free, arg_used,
flags);
}
while ((*nested_orig_cmd)[1] != '\0') {
(*nested_orig_cmd)++;
if (**nested_orig_cmd == ')')
break;
}
cmd = &nest_value;
if (toplevel) nested_orig_cmd = NULL;
}
if (**cmd != '{')
brackets = FALSE;
else {
/* special value is inside {...} (foo${test}bar -> fooXXXbar) */
(*cmd)++;
brackets = TRUE;
}
value = get_special_value(cmd, server, item, arglist,
free_ret, arg_used, flags);
if (**cmd == '\0')
g_error("parse_special() : buffer overflow!");
if (value != NULL && *value != '\0' && (flags & PARSE_FLAG_ISSET_ANY))
*arg_used = TRUE;
if (brackets) {
while (**cmd != '}' && (*cmd)[1] != '\0')
(*cmd)++;
}
if (nest_free) g_free(nest_value);
if (command == '[' && (flags & PARSE_FLAG_GETNAME) == 0) {
/* alignment */
char *p;
if (value == NULL) return "";
p = get_alignment(value, align, align_flags, align_pad);
if (*free_ret) g_free(value);
*free_ret = TRUE;
return p;
}
return value;
}
/* parse the whole string. $ and \ chars are replaced */
char *parse_special_string(const char *cmd, SERVER_REC *server, void *item,
const char *data, int *arg_used, int flags)
{
char code, **arglist, *ret;
GString *str;
int need_free;
g_return_val_if_fail(cmd != NULL, NULL);
g_return_val_if_fail(data != NULL, NULL);
/* create the argument list */
arglist = g_strsplit(data, " ", -1);
if (arg_used != NULL) *arg_used = FALSE;
code = 0;
str = g_string_new(NULL);
while (*cmd != '\0') {
if (code == '\\'){
switch (*cmd) {
case 't':
g_string_append_c(str, '\t');
break;
case 'n':
g_string_append_c(str, '\n');
break;
default:
g_string_append_c(str, *cmd);
}
code = 0;
} else if (code == '$') {
char *ret;
ret = parse_special((char **) &cmd, server, item,
arglist, &need_free, arg_used,
flags);
if (ret != NULL) {
g_string_append(str, ret);
if (need_free) g_free(ret);
}
code = 0;
} else {
if (*cmd == '\\' || *cmd == '$')
code = *cmd;
else
g_string_append_c(str, *cmd);
}
cmd++;
}
g_strfreev(arglist);
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
#define is_split_char(str, start) \
((str)[0] == ';' && ((start) == (str) || \
((str)[-1] != '\\' && (str)[-1] != '$')))
/* execute the commands in string - commands can be split with ';' */
void eval_special_string(const char *cmd, const char *data,
SERVER_REC *server, void *item)
{
const char *cmdchars;
char *orig, *str, *start, *ret;
int arg_used, arg_used_ever;
GSList *commands;
commands = NULL;
arg_used_ever = FALSE;
cmdchars = settings_get_str("cmdchars");
/* get a list of all the commands to run */
orig = start = str = g_strdup(cmd);
do {
if (is_split_char(str, start))
*str++ = '\0';
else if (*str != '\0') {
str++;
continue;
}
ret = parse_special_string(start, server, item,
data, &arg_used, 0);
if (arg_used) arg_used_ever = TRUE;
if (strchr(cmdchars, *ret) == NULL) {
/* no command char - let's put it there.. */
char *old = ret;
ret = g_strdup_printf("%c%s", *cmdchars, old);
g_free(old);
}
commands = g_slist_append(commands, ret);
start = str;
} while (*start != '\0');
/* run the command, if no arguments were ever used, append all of them
after each command */
while (commands != NULL) {
ret = commands->data;
if (!arg_used_ever && *data != '\0') {
char *old = ret;
ret = g_strconcat(old, " ", data, NULL);
g_free(old);
}
signal_emit("send command", 3, ret, server, item);
g_free(ret);
commands = g_slist_remove(commands, commands->data);
}
g_free(orig);
}
void special_history_func_set(SPECIAL_HISTORY_FUNC func)
{
history_func = func;
}
static void special_vars_signals_do(const char *text, int funccount,
SIGNAL_FUNC *funcs, int bind)
{
char *ret;
int need_free;
while (*text != '\0') {
if (*text == '\\' && text[1] != '\0') {
text += 2;
} else if (*text == '$' && text[1] != '\0') {
text++;
ret = parse_special((char **) &text, NULL, NULL,
NULL, &need_free, NULL,
PARSE_FLAG_GETNAME);
if (ret != NULL) {
if (bind)
expando_bind(ret, funccount, funcs);
else
expando_unbind(ret, funccount, funcs);
if (need_free) g_free(ret);
}
}
else text++;
}
}
void special_vars_add_signals(const char *text,
int funccount, SIGNAL_FUNC *funcs)
{
special_vars_signals_do(text, funccount, funcs, TRUE);
}
void special_vars_remove_signals(const char *text,
int funccount, SIGNAL_FUNC *funcs)
{
special_vars_signals_do(text, funccount, funcs, FALSE);
}
|