2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-04-26 04:03:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "special-vars.h"
|
2000-12-04 19:53:04 -05:00
|
|
|
#include "expandos.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "settings.h"
|
2002-02-03 14:24:51 -05:00
|
|
|
#include "servers.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "misc.h"
|
2016-05-12 21:42:56 -04:00
|
|
|
#include "utf8.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-10 00:11:36 -05:00
|
|
|
#define isvarchar(c) \
|
2002-01-27 15:45:59 -05:00
|
|
|
(i_isalnum(c) || (c) == '_')
|
2000-12-10 00:11:36 -05:00
|
|
|
|
2001-09-20 12:43:24 -04:00
|
|
|
#define isarg(c) \
|
2002-01-27 15:45:59 -05:00
|
|
|
(i_isdigit(c) || (c) == '*' || (c) == '~' || (c) == '-')
|
2001-09-20 12:43:24 -04:00
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
static SPECIAL_HISTORY_FUNC history_func = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
static char *get_argument(char **cmd, char **arglist)
|
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
char *ret;
|
|
|
|
int max, arg, argcount;
|
|
|
|
|
|
|
|
arg = 0;
|
|
|
|
max = -1;
|
|
|
|
|
2016-02-13 08:16:05 -05:00
|
|
|
argcount = arglist == NULL ? 0 : g_strv_length(arglist);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (**cmd == '*') {
|
|
|
|
/* get all arguments */
|
|
|
|
} else if (**cmd == '~') {
|
|
|
|
/* get last argument */
|
|
|
|
arg = max = argcount-1;
|
|
|
|
} else {
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_isdigit(**cmd)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* first argument */
|
|
|
|
arg = max = (**cmd)-'0';
|
|
|
|
(*cmd)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (**cmd == '-') {
|
|
|
|
/* get more than one argument */
|
|
|
|
(*cmd)++;
|
2002-01-27 15:45:59 -05:00
|
|
|
if (!i_isdigit(**cmd))
|
2000-04-26 04:03:38 -04:00
|
|
|
max = -1; /* get all the rest */
|
|
|
|
else {
|
|
|
|
max = (**cmd)-'0';
|
|
|
|
(*cmd)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*cmd)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = g_string_new(NULL);
|
2001-10-20 07:30:24 -04:00
|
|
|
while (arg >= 0 && arg < argcount && (arg <= max || max == -1)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static char *get_long_variable_value(const char *key, SERVER_REC *server,
|
2000-07-16 16:18:05 -04:00
|
|
|
void *item, int *free_ret)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
EXPANDO_FUNC func;
|
2002-04-09 22:53:06 -04:00
|
|
|
const char *ret;
|
2007-05-14 17:22:41 -04:00
|
|
|
SETTINGS_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
*free_ret = FALSE;
|
|
|
|
|
|
|
|
/* expando? */
|
2000-12-04 19:53:04 -05:00
|
|
|
func = expando_find_long(key);
|
2002-10-27 17:30:41 -05:00
|
|
|
if (func != NULL) {
|
|
|
|
current_expando = key;
|
2000-04-26 04:03:38 -04:00
|
|
|
return func(server, item, free_ret);
|
2002-10-27 17:30:41 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* internal setting? */
|
2007-05-14 17:22:41 -04:00
|
|
|
rec = settings_get_record(key);
|
|
|
|
if (rec != NULL) {
|
|
|
|
*free_ret = TRUE;
|
|
|
|
return settings_get_print(rec);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* environment variable? */
|
|
|
|
ret = g_getenv(key);
|
2000-05-25 09:54:43 -04:00
|
|
|
if (ret != NULL)
|
2002-04-09 22:53:06 -04:00
|
|
|
return (char *) ret;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static char *get_long_variable(char **cmd, SERVER_REC *server,
|
2000-12-17 02:39:22 -05:00
|
|
|
void *item, int *free_ret, int getname)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
char *start, *var, *ret;
|
|
|
|
|
|
|
|
/* get variable name */
|
|
|
|
start = *cmd;
|
2000-12-10 00:11:36 -05:00
|
|
|
while (isvarchar((*cmd)[1])) (*cmd)++;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
var = g_strndup(start, (int) (*cmd-start)+1);
|
2000-12-17 02:39:22 -05:00
|
|
|
if (getname) {
|
|
|
|
*free_ret = TRUE;
|
|
|
|
return var;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
ret = get_long_variable_value(var, server, item, free_ret);
|
|
|
|
g_free(var);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
/* return the value of the variable found from `cmd'.
|
|
|
|
if 'getname' is TRUE, return the name of the variable instead it's value */
|
2000-08-26 11:39:44 -04:00
|
|
|
static char *get_variable(char **cmd, SERVER_REC *server, void *item,
|
2000-12-17 02:39:22 -05:00
|
|
|
char **arglist, int *free_ret, int *arg_used,
|
|
|
|
int getname)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-12-04 19:53:04 -05:00
|
|
|
EXPANDO_FUNC func;
|
|
|
|
|
2001-09-20 12:43:24 -04:00
|
|
|
if (isarg(**cmd)) {
|
2000-12-17 02:39:22 -05:00
|
|
|
/* argument */
|
2000-04-26 04:03:38 -04:00
|
|
|
*free_ret = TRUE;
|
|
|
|
if (arg_used != NULL) *arg_used = TRUE;
|
2000-12-17 02:39:22 -05:00
|
|
|
return getname ? g_strdup_printf("%c", **cmd) :
|
|
|
|
get_argument(cmd, arglist);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_isalpha(**cmd) && isvarchar((*cmd)[1])) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* long variable name.. */
|
2000-12-17 02:39:22 -05:00
|
|
|
return get_long_variable(cmd, server, item, free_ret, getname);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* single character variable. */
|
2000-12-17 02:39:22 -05:00
|
|
|
if (getname) {
|
|
|
|
*free_ret = TRUE;
|
|
|
|
return g_strdup_printf("%c", **cmd);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
*free_ret = FALSE;
|
2000-12-04 19:53:04 -05:00
|
|
|
func = expando_find_char(**cmd);
|
2002-10-27 17:30:41 -05:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
|
|
|
else {
|
|
|
|
char str[2];
|
|
|
|
|
|
|
|
str[0] = **cmd; str[1] = '\0';
|
|
|
|
current_expando = str;
|
|
|
|
return func(server, item, free_ret);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2001-12-07 14:45:47 -05:00
|
|
|
text = g_strndup(start, (int) (*cmd-start));
|
2000-04-26 04:03:38 -04:00
|
|
|
ret = history_func(text, item, free_ret);
|
|
|
|
g_free(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (**cmd == '\0') (*cmd)--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static char *get_special_value(char **cmd, SERVER_REC *server, void *item,
|
2000-12-17 02:39:22 -05:00
|
|
|
char **arglist, int *free_ret, int *arg_used,
|
|
|
|
int flags)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
char command, *value, *p;
|
|
|
|
int len;
|
|
|
|
|
2001-09-20 12:44:22 -04:00
|
|
|
if ((flags & PARSE_FLAG_ONLY_ARGS) && !isarg(**cmd)) {
|
2001-09-20 11:58:14 -04:00
|
|
|
*free_ret = TRUE;
|
|
|
|
return g_strdup_printf("$%c", **cmd);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (**cmd == '!') {
|
|
|
|
/* find text from command history */
|
2000-12-17 02:39:22 -05:00
|
|
|
if (flags & PARSE_FLAG_GETNAME)
|
|
|
|
return "!";
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
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 = "*";
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
if (flags & PARSE_FLAG_GETNAME)
|
|
|
|
return "*";
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
*free_ret = TRUE;
|
|
|
|
return get_argument(&temp_cmd, arglist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
value = get_variable(cmd, server, item, arglist, free_ret,
|
|
|
|
arg_used, flags & PARSE_FLAG_GETNAME);
|
|
|
|
|
|
|
|
if (flags & PARSE_FLAG_GETNAME)
|
|
|
|
return value;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
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;
|
2017-05-17 05:18:49 -04:00
|
|
|
char *endptr;
|
|
|
|
guint align_;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
*align = 0;
|
2001-01-12 12:05:05 -05:00
|
|
|
*flags = ALIGN_CUT|ALIGN_PAD;
|
2000-04-26 04:03:38 -04:00
|
|
|
*pad = ' ';
|
|
|
|
|
|
|
|
/* '!' = don't cut, '-' = right padding */
|
|
|
|
str = *data;
|
2002-01-27 15:45:59 -05:00
|
|
|
while (*str != '\0' && *str != ']' && !i_isdigit(*str)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
if (*str == '!')
|
|
|
|
*flags &= ~ALIGN_CUT;
|
|
|
|
else if (*str == '-')
|
|
|
|
*flags |= ALIGN_RIGHT;
|
2001-01-12 12:05:05 -05:00
|
|
|
else if (*str == '.')
|
|
|
|
*flags &= ~ALIGN_PAD;
|
2000-04-26 04:03:38 -04:00
|
|
|
str++;
|
|
|
|
}
|
2002-01-27 15:45:59 -05:00
|
|
|
if (!i_isdigit(*str))
|
2000-04-26 04:03:38 -04:00
|
|
|
return FALSE; /* expecting number */
|
|
|
|
|
|
|
|
/* get the alignment size */
|
2017-05-17 05:18:49 -04:00
|
|
|
if (!parse_uint(str, &endptr, 10, &align_)) {
|
|
|
|
return FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2017-05-17 05:18:49 -04:00
|
|
|
str = endptr;
|
|
|
|
*align = align_;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* get the pad character */
|
|
|
|
while (*str != '\0' && *str != ']') {
|
|
|
|
*pad = *str;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*str++ != ']') return FALSE;
|
|
|
|
|
|
|
|
*data = str;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the aligned text */
|
2016-05-12 21:51:48 -04:00
|
|
|
char *get_alignment(const char *text, int align, int flags, char pad)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
char *ret;
|
2016-05-12 21:42:56 -04:00
|
|
|
int policy;
|
|
|
|
unsigned int cut_bytes;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(text != NULL, NULL);
|
|
|
|
|
2016-05-12 21:42:56 -04:00
|
|
|
policy = string_policy(text);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
str = g_string_new(text);
|
|
|
|
|
|
|
|
/* cut */
|
2016-05-12 21:42:56 -04:00
|
|
|
if ((flags & ALIGN_CUT) && align > 0 && string_width(text, policy) > align) {
|
|
|
|
string_chars_for_width(text, policy, align, &cut_bytes);
|
|
|
|
g_string_truncate(str, cut_bytes);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* add pad characters */
|
2001-01-12 12:05:05 -05:00
|
|
|
if (flags & ALIGN_PAD) {
|
2016-05-12 21:42:56 -04:00
|
|
|
while (string_width(str->str, policy) < align) {
|
2001-01-12 12:05:05 -05:00
|
|
|
if (flags & ALIGN_RIGHT)
|
|
|
|
g_string_prepend_c(str, pad);
|
|
|
|
else
|
|
|
|
g_string_append_c(str, pad);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = str->str;
|
2016-05-12 21:31:23 -04:00
|
|
|
g_string_free(str, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse and expand text after '$' character. return value has to be
|
|
|
|
g_free()'d if `free_ret' is TRUE. */
|
2000-08-26 11:39:44 -04:00
|
|
|
char *parse_special(char **cmd, SERVER_REC *server, void *item,
|
2000-12-17 02:39:22 -05:00
|
|
|
char **arglist, int *free_ret, int *arg_used, int flags)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
static char **nested_orig_cmd = NULL; /* FIXME: KLUDGE! */
|
|
|
|
char command, *value;
|
|
|
|
|
2010-04-04 08:19:54 -04:00
|
|
|
char align_pad = '\0';
|
|
|
|
int align = 0, align_flags = 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
char *nest_value;
|
|
|
|
int brackets, nest_free;
|
|
|
|
|
|
|
|
*free_ret = FALSE;
|
2002-02-17 04:37:23 -05:00
|
|
|
if (**cmd == '\0')
|
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
command = **cmd; (*cmd)++;
|
|
|
|
switch (command) {
|
|
|
|
case '[':
|
|
|
|
/* alignment */
|
2000-07-16 16:18:05 -04:00
|
|
|
if (!get_alignment_args(cmd, &align, &align_flags,
|
|
|
|
&align_pad) || **cmd == '\0') {
|
|
|
|
(*cmd)--;
|
2000-04-26 04:03:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
command = 0;
|
|
|
|
(*cmd)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
nest_free = FALSE; nest_value = NULL;
|
2018-01-03 09:51:51 -05:00
|
|
|
#if 0 /* this code is disabled due to security issues until it is fixed */
|
2002-11-05 08:10:58 -05:00
|
|
|
if (**cmd == '(' && (*cmd)[1] != '\0') {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* subvariable */
|
|
|
|
int toplevel = nested_orig_cmd == NULL;
|
|
|
|
|
|
|
|
if (toplevel) nested_orig_cmd = cmd;
|
|
|
|
(*cmd)++;
|
|
|
|
if (**cmd != '$') {
|
|
|
|
/* ... */
|
|
|
|
nest_value = *cmd;
|
|
|
|
} else {
|
|
|
|
(*cmd)++;
|
2000-07-16 16:18:05 -04:00
|
|
|
nest_value = parse_special(cmd, server, item, arglist,
|
2000-12-17 02:39:22 -05:00
|
|
|
&nest_free, arg_used,
|
|
|
|
flags);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2002-11-05 08:10:58 -05:00
|
|
|
if (nest_value == NULL || *nest_value == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
while ((*nested_orig_cmd)[1] != '\0') {
|
|
|
|
(*nested_orig_cmd)++;
|
2000-07-16 16:18:05 -04:00
|
|
|
if (**nested_orig_cmd == ')')
|
|
|
|
break;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
cmd = &nest_value;
|
|
|
|
|
|
|
|
if (toplevel) nested_orig_cmd = NULL;
|
|
|
|
}
|
2018-01-03 09:51:51 -05:00
|
|
|
#else
|
|
|
|
if (nested_orig_cmd) nested_orig_cmd = NULL;
|
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (**cmd != '{')
|
|
|
|
brackets = FALSE;
|
|
|
|
else {
|
|
|
|
/* special value is inside {...} (foo${test}bar -> fooXXXbar) */
|
2002-11-05 08:10:58 -05:00
|
|
|
if ((*cmd)[1] == '\0')
|
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
(*cmd)++;
|
|
|
|
brackets = TRUE;
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
value = get_special_value(cmd, server, item, arglist,
|
2000-12-17 02:39:22 -05:00
|
|
|
free_ret, arg_used, flags);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (**cmd == '\0')
|
|
|
|
g_error("parse_special() : buffer overflow!");
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
if (value != NULL && *value != '\0' && (flags & PARSE_FLAG_ISSET_ANY))
|
|
|
|
*arg_used = TRUE;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (brackets) {
|
|
|
|
while (**cmd != '}' && (*cmd)[1] != '\0')
|
|
|
|
(*cmd)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nest_free) g_free(nest_value);
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
if (command == '[' && (flags & PARSE_FLAG_GETNAME) == 0) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2001-06-26 11:33:07 -04:00
|
|
|
static void gstring_append_escaped(GString *str, const char *text, int flags)
|
2001-03-03 16:34:06 -05:00
|
|
|
{
|
2001-06-26 11:33:07 -04:00
|
|
|
char esc[4], *escpos;
|
2008-02-04 10:30:38 -05:00
|
|
|
|
2001-06-26 11:33:07 -04:00
|
|
|
escpos = esc;
|
|
|
|
if (flags & PARSE_FLAG_ESCAPE_VARS)
|
|
|
|
*escpos++ = '%';
|
|
|
|
if (flags & PARSE_FLAG_ESCAPE_THEME) {
|
|
|
|
*escpos++ = '{';
|
|
|
|
*escpos++ = '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (escpos == esc) {
|
|
|
|
g_string_append(str, text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-04 10:30:38 -05:00
|
|
|
*escpos = '\0';
|
2001-03-03 16:34:06 -05:00
|
|
|
while (*text != '\0') {
|
2001-06-26 11:33:07 -04:00
|
|
|
for (escpos = esc; *escpos != '\0'; escpos++) {
|
|
|
|
if (*text == *escpos) {
|
|
|
|
g_string_append_c(str, '%');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-03-03 16:34:06 -05:00
|
|
|
g_string_append_c(str, *text);
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* parse the whole string. $ and \ chars are replaced */
|
2000-08-26 11:39:44 -04:00
|
|
|
char *parse_special_string(const char *cmd, SERVER_REC *server, void *item,
|
2000-12-17 02:39:22 -05:00
|
|
|
const char *data, int *arg_used, int flags)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
char code, **arglist, *ret;
|
|
|
|
GString *str;
|
2001-08-08 16:00:25 -04:00
|
|
|
int need_free, chr;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
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') {
|
2001-08-08 16:00:25 -04:00
|
|
|
if (code == '\\') {
|
|
|
|
if (*cmd == ';')
|
|
|
|
g_string_append_c(str, ';');
|
|
|
|
else {
|
|
|
|
chr = expand_escape(&cmd);
|
2001-10-21 14:58:09 -04:00
|
|
|
g_string_append_c(str, chr != -1 ? chr : *cmd);
|
2000-06-11 18:58:17 -04:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
code = 0;
|
|
|
|
} else if (code == '$') {
|
|
|
|
char *ret;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
ret = parse_special((char **) &cmd, server, item,
|
2000-12-17 02:39:22 -05:00
|
|
|
arglist, &need_free, arg_used,
|
|
|
|
flags);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (ret != NULL) {
|
2001-06-26 11:33:07 -04:00
|
|
|
gstring_append_escaped(str, ret, flags);
|
2000-04-26 04:03:38 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
#define is_split_char(str, start) \
|
|
|
|
((str)[0] == ';' && ((start) == (str) || \
|
|
|
|
((str)[-1] != '\\' && (str)[-1] != '$')))
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* execute the commands in string - commands can be split with ';' */
|
2000-07-16 16:18:05 -04:00
|
|
|
void eval_special_string(const char *cmd, const char *data,
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_REC *server, void *item)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
const char *cmdchars;
|
|
|
|
char *orig, *str, *start, *ret;
|
2000-11-30 17:58:45 -05:00
|
|
|
int arg_used, arg_used_ever;
|
|
|
|
GSList *commands;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-11-30 17:58:45 -05:00
|
|
|
commands = NULL;
|
|
|
|
arg_used_ever = FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
cmdchars = settings_get_str("cmdchars");
|
2000-11-30 17:58:45 -05:00
|
|
|
|
|
|
|
/* get a list of all the commands to run */
|
2000-04-26 04:03:38 -04:00
|
|
|
orig = start = str = g_strdup(cmd);
|
|
|
|
do {
|
2001-11-19 17:44:04 -05:00
|
|
|
if (is_split_char(str, start)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
*str++ = '\0';
|
2001-11-19 17:44:04 -05:00
|
|
|
while (*str == ' ') str++;
|
|
|
|
} else if (*str != '\0') {
|
2000-04-26 04:03:38 -04:00
|
|
|
str++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
ret = parse_special_string(start, server, item,
|
2000-12-17 02:39:22 -05:00
|
|
|
data, &arg_used, 0);
|
2001-11-19 17:44:04 -05:00
|
|
|
if (*ret != '\0') {
|
|
|
|
if (arg_used) arg_used_ever = TRUE;
|
2000-11-30 17:58:45 -05:00
|
|
|
|
2001-11-19 17:44:04 -05:00
|
|
|
if (strchr(cmdchars, *ret) == NULL) {
|
|
|
|
/* no command char - let's put it there.. */
|
|
|
|
char *old = ret;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-19 17:44:04 -05:00
|
|
|
ret = g_strdup_printf("%c%s", *cmdchars, old);
|
|
|
|
g_free(old);
|
|
|
|
}
|
|
|
|
commands = g_slist_append(commands, ret);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2000-11-30 17:58:45 -05:00
|
|
|
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') {
|
2000-04-26 04:03:38 -04:00
|
|
|
char *old = ret;
|
|
|
|
|
|
|
|
ret = g_strconcat(old, " ", data, NULL);
|
|
|
|
g_free(old);
|
|
|
|
}
|
2002-02-03 13:32:46 -05:00
|
|
|
|
2002-02-03 18:14:22 -05:00
|
|
|
if (server != NULL)
|
|
|
|
server_ref(server);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_emit("send command", 3, ret, server, item);
|
|
|
|
|
2002-02-03 18:14:22 -05:00
|
|
|
if (server != NULL && !server_unref(server)) {
|
2002-02-03 13:32:46 -05:00
|
|
|
/* the server was destroyed */
|
|
|
|
server = NULL;
|
|
|
|
item = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: window item would need reference counting as well,
|
|
|
|
eg. "/EVAL win close;say hello" wouldn't work now.. */
|
|
|
|
|
2000-11-30 17:58:45 -05:00
|
|
|
g_free(ret);
|
|
|
|
commands = g_slist_remove(commands, commands->data);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void special_history_func_set(SPECIAL_HISTORY_FUNC func)
|
|
|
|
{
|
|
|
|
history_func = func;
|
|
|
|
}
|
2000-12-17 02:39:22 -05:00
|
|
|
|
2001-09-23 13:32:05 -04:00
|
|
|
static void update_signals_hash(GHashTable **hash, int *signals)
|
2000-12-17 02:39:22 -05:00
|
|
|
{
|
2001-09-23 13:32:05 -04:00
|
|
|
void *signal_id;
|
|
|
|
int arg_type;
|
|
|
|
|
|
|
|
if (*hash == NULL) {
|
|
|
|
*hash = g_hash_table_new((GHashFunc) g_direct_hash,
|
|
|
|
(GCompareFunc) g_direct_equal);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*signals != -1) {
|
|
|
|
signal_id = GINT_TO_POINTER(*signals);
|
|
|
|
arg_type = GPOINTER_TO_INT(g_hash_table_lookup(*hash, signal_id));
|
|
|
|
if (arg_type != 0 && arg_type != signals[1]) {
|
|
|
|
/* same signal is used for different purposes ..
|
|
|
|
not sure if this should ever happen, but change
|
|
|
|
the argument type to none so it will at least
|
|
|
|
work. */
|
|
|
|
arg_type = EXPANDO_ARG_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_type == 0) arg_type = signals[1];
|
|
|
|
g_hash_table_insert(*hash, signal_id,
|
|
|
|
GINT_TO_POINTER(arg_type));
|
|
|
|
signals += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_signal_hash(void *signal_id, void *arg_type, int **pos)
|
|
|
|
{
|
|
|
|
(*pos)[0] = GPOINTER_TO_INT(signal_id);
|
|
|
|
(*pos)[1] = GPOINTER_TO_INT(arg_type);
|
|
|
|
(*pos) += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int *get_signals_list(GHashTable *hash)
|
|
|
|
{
|
|
|
|
int *signals, *pos;
|
|
|
|
|
|
|
|
if (hash == NULL) {
|
|
|
|
/* no expandos in text - never needs updating */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = signals = g_new(int, g_hash_table_size(hash)*2 + 1);
|
|
|
|
g_hash_table_foreach(hash, (GHFunc) get_signal_hash, &pos);
|
|
|
|
*pos = -1;
|
|
|
|
|
|
|
|
g_hash_table_destroy(hash);
|
|
|
|
return signals;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TASK_BIND 1
|
|
|
|
#define TASK_UNBIND 2
|
|
|
|
#define TASK_GET_SIGNALS 3
|
2000-12-17 02:39:22 -05:00
|
|
|
|
2001-09-23 13:32:05 -04:00
|
|
|
static int *special_vars_signals_task(const char *text, int funccount,
|
|
|
|
SIGNAL_FUNC *funcs, int task)
|
|
|
|
{
|
|
|
|
GHashTable *signals;
|
|
|
|
char *expando;
|
|
|
|
int need_free, *expando_signals;
|
|
|
|
|
|
|
|
signals = NULL;
|
2000-12-17 02:39:22 -05:00
|
|
|
while (*text != '\0') {
|
|
|
|
if (*text == '\\' && text[1] != '\0') {
|
2001-09-23 13:32:05 -04:00
|
|
|
/* escape */
|
2000-12-17 02:39:22 -05:00
|
|
|
text += 2;
|
|
|
|
} else if (*text == '$' && text[1] != '\0') {
|
2001-09-23 13:32:05 -04:00
|
|
|
/* expando */
|
2000-12-17 02:39:22 -05:00
|
|
|
text++;
|
2001-09-23 13:32:05 -04:00
|
|
|
expando = parse_special((char **) &text, NULL, NULL,
|
|
|
|
NULL, &need_free, NULL,
|
|
|
|
PARSE_FLAG_GETNAME);
|
|
|
|
if (expando == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (task) {
|
|
|
|
case TASK_BIND:
|
|
|
|
expando_bind(expando, funccount, funcs);
|
|
|
|
break;
|
|
|
|
case TASK_UNBIND:
|
|
|
|
expando_unbind(expando, funccount, funcs);
|
|
|
|
break;
|
|
|
|
case TASK_GET_SIGNALS:
|
|
|
|
expando_signals = expando_get_signals(expando);
|
|
|
|
if (expando_signals != NULL) {
|
|
|
|
update_signals_hash(&signals,
|
|
|
|
expando_signals);
|
|
|
|
g_free(expando_signals);
|
|
|
|
}
|
|
|
|
break;
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
2001-09-23 13:32:05 -04:00
|
|
|
if (need_free) g_free(expando);
|
|
|
|
} else {
|
|
|
|
/* just a char */
|
|
|
|
text++;
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
}
|
2001-09-23 13:32:05 -04:00
|
|
|
|
|
|
|
if (task == TASK_GET_SIGNALS)
|
|
|
|
return get_signals_list(signals);
|
|
|
|
|
|
|
|
return NULL;
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void special_vars_add_signals(const char *text,
|
|
|
|
int funccount, SIGNAL_FUNC *funcs)
|
|
|
|
{
|
2001-09-23 13:32:05 -04:00
|
|
|
special_vars_signals_task(text, funccount, funcs, TASK_BIND);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void special_vars_remove_signals(const char *text,
|
|
|
|
int funccount, SIGNAL_FUNC *funcs)
|
|
|
|
{
|
2001-09-23 13:32:05 -04:00
|
|
|
special_vars_signals_task(text, funccount, funcs, TASK_UNBIND);
|
|
|
|
}
|
|
|
|
|
|
|
|
int *special_vars_get_signals(const char *text)
|
|
|
|
{
|
|
|
|
return special_vars_signals_task(text, 0, NULL, TASK_GET_SIGNALS);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|