1
0
mirror of https://github.com/irssi/irssi.git synced 2024-11-03 04:27:19 -05:00

Moved PARAM_FLAG_OPTCHAN handling to core. Removed support for adding own

command parameter parsers, it's probably useless now that opt.channels are
in core.


git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1482 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2001-05-11 15:48:09 +00:00 committed by cras
parent d44b803c3c
commit e3b33796ff
4 changed files with 55 additions and 109 deletions

View File

@ -22,9 +22,11 @@
#include "signals.h" #include "signals.h"
#include "commands.h" #include "commands.h"
#include "misc.h" #include "misc.h"
#include "special-vars.h"
#include "servers.h" #include "servers.h"
#include "servers-redirect.h" #include "servers-redirect.h"
#include "special-vars.h" #include "channels.h"
#include "lib-config/iconfig.h" #include "lib-config/iconfig.h"
#include "settings.h" #include "settings.h"
@ -32,7 +34,6 @@
GSList *commands; GSList *commands;
char *current_command; char *current_command;
static GSList *cmdget_funcs;
static int signal_default_command; static int signal_default_command;
static GSList *alias_runstack; static GSList *alias_runstack;
@ -613,69 +614,56 @@ static int get_cmd_options(char **data, int ignore_unknown,
return 0; return 0;
} }
char *cmd_get_callfuncs(const char *data, int *count, va_list *args)
{
CMD_GET_FUNC func;
GSList *tmp;
char *ret, *old;
ret = g_strdup(data);
for (tmp = cmdget_funcs; tmp != NULL; tmp = tmp->next) {
func = (CMD_GET_FUNC) tmp->data;
old = ret;
ret = func(ret, count, args);
g_free(old);
}
return ret;
}
typedef struct { typedef struct {
char *data; char *data;
GHashTable *options; GHashTable *options;
} CMD_TEMP_REC; } CMD_TEMP_REC;
static char *get_optional_channel(CHANNEL_REC *active_channel, char **data)
{
CHANNEL_REC *chanrec;
char *tmp, *origtmp, *channel, *ret;
if (active_channel == NULL) {
/* no active channel in window, channel required */
return cmd_get_param(data);
}
origtmp = tmp = g_strdup(*data);
channel = cmd_get_param(&tmp);
if (strcmp(channel, "*") == 0 ||
!active_channel->server->ischannel(channel))
ret = active_channel->name;
else {
/* Find the channel first and use it's name if found.
This allows automatic !channel -> !XXXXXchannel replaces. */
chanrec = channel_find(active_channel->server, channel);
ret = chanrec == NULL ? channel : chanrec->name;
cmd_get_param(data);
}
g_free(origtmp);
return ret;
}
int cmd_get_params(const char *data, gpointer *free_me, int count, ...) int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
{ {
CMD_TEMP_REC *rec; CMD_TEMP_REC *rec;
GHashTable **opthash; GHashTable **opthash;
char **str, *arg, *datad, *old; char **str, *arg, *datad;
va_list args; va_list args;
int cnt, error, len, ignore_unknown; int cnt, error, ignore_unknown;
g_return_val_if_fail(data != NULL, FALSE); g_return_val_if_fail(data != NULL, FALSE);
va_start(args, count); va_start(args, count);
/* get the length of the options in string */
if ((count & PARAM_FLAG_OPTIONS) == 0)
len = 0;
else {
old = datad = g_strdup(data);
get_cmd_options(&datad, TRUE, NULL, NULL);
len = (int) (datad-old);
g_free(old);
}
/* send the text to custom functions to handle - skip options */
old = datad = cmd_get_callfuncs(data+len, &count, &args);
if (len > 0) {
/* put the options + the new data to one string */
datad = g_malloc(len+1 + strlen(old)+1);
memcpy(datad, data, len);
datad[len] = ' ';
memcpy(datad+len+1, old, strlen(old)+1);
g_free(old);
old = datad;
}
rec = g_new0(CMD_TEMP_REC, 1); rec = g_new0(CMD_TEMP_REC, 1);
rec->data = old; rec->data = g_strdup(data);
*free_me = rec; *free_me = rec;
datad = rec->data;
error = FALSE; error = FALSE;
if (count & PARAM_FLAG_OPTIONS) { if (count & PARAM_FLAG_OPTIONS) {
arg = (char *) va_arg(args, char *); arg = (char *) va_arg(args, char *);
@ -693,6 +681,18 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
if (!error) { if (!error) {
/* and now handle the string */ /* and now handle the string */
cnt = PARAM_WITHOUT_FLAGS(count); cnt = PARAM_WITHOUT_FLAGS(count);
if (count & PARAM_FLAG_OPTCHAN) {
/* optional channel as first parameter */
CHANNEL_REC *chanrec;
chanrec = (CHANNEL_REC *) va_arg(args, CHANNEL_REC *);
arg = get_optional_channel(chanrec, &datad);
str = (char **) va_arg(args, char **);
if (str != NULL) *str = arg;
cnt--;
}
while (cnt-- > 0) { while (cnt-- > 0) {
if (cnt == 0 && count & PARAM_FLAG_GETREST) { if (cnt == 0 && count & PARAM_FLAG_GETREST) {
/* get rest */ /* get rest */
@ -729,16 +729,6 @@ void cmd_params_free(void *free_me)
g_free(rec); g_free(rec);
} }
void cmd_get_add_func(CMD_GET_FUNC func)
{
cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func);
}
void cmd_get_remove_func(CMD_GET_FUNC func)
{
cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func);
}
static void command_module_unbind_all(COMMAND_REC *rec, static void command_module_unbind_all(COMMAND_REC *rec,
COMMAND_MODULE_REC *modrec) COMMAND_MODULE_REC *modrec)
{ {
@ -894,7 +884,6 @@ static void cmd_cd(const char *data)
void commands_init(void) void commands_init(void)
{ {
commands = NULL; commands = NULL;
cmdget_funcs = NULL;
current_command = NULL; current_command = NULL;
alias_runstack = NULL; alias_runstack = NULL;
@ -910,7 +899,6 @@ void commands_init(void)
void commands_deinit(void) void commands_deinit(void)
{ {
g_free_not_null(current_command); g_free_not_null(current_command);
g_slist_free(cmdget_funcs);
signal_remove("send command", (SIGNAL_FUNC) event_command); signal_remove("send command", (SIGNAL_FUNC) event_command);

View File

@ -97,11 +97,11 @@ void command_set_options_module(const char *module,
int command_have_option(const char *cmd, const char *option); int command_have_option(const char *cmd, const char *option);
/* count can have these flags: */ /* count can have these flags: */
#define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00ffffff) #define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00000fff)
/* don't check for quotes - "arg1 arg2" is NOT treated as one argument */ /* don't check for quotes - "arg1 arg2" is NOT treated as one argument */
#define PARAM_FLAG_NOQUOTES 0x01000000 #define PARAM_FLAG_NOQUOTES 0x00001000
/* final argument gets all the rest of the arguments */ /* final argument gets all the rest of the arguments */
#define PARAM_FLAG_GETREST 0x02000000 #define PARAM_FLAG_GETREST 0x00002000
/* command contains options - first you need to specify them with /* command contains options - first you need to specify them with
command_set_options() function. Example: command_set_options() function. Example:
@ -124,9 +124,11 @@ int command_have_option(const char *cmd, const char *option);
"cmd2" = "another arg" "cmd2" = "another arg"
"optnumarg" = "" - this is because "rest" isn't a numeric value "optnumarg" = "" - this is because "rest" isn't a numeric value
*/ */
#define PARAM_FLAG_OPTIONS 0x04000000 #define PARAM_FLAG_OPTIONS 0x00004000
/* don't complain about unknown options */ /* don't complain about unknown options */
#define PARAM_FLAG_UNKNOWN_OPTIONS 0x08000000 #define PARAM_FLAG_UNKNOWN_OPTIONS 0x00008000
/* optional channel in first argument */
#define PARAM_FLAG_OPTCHAN 0x00010000
char *cmd_get_param(char **data); char *cmd_get_param(char **data);
/* get parameters from command - you should point free_me somewhere and /* get parameters from command - you should point free_me somewhere and
@ -137,10 +139,6 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...);
void cmd_params_free(void *free_me); void cmd_params_free(void *free_me);
typedef char* (*CMD_GET_FUNC) (const char *data, int *count, va_list *args);
void cmd_get_add_func(CMD_GET_FUNC func);
void cmd_get_remove_func(CMD_GET_FUNC func);
void commands_remove_module(const char *module); void commands_remove_module(const char *module);
void commands_init(void); void commands_init(void);

View File

@ -377,44 +377,8 @@ static void irc_init_server(IRC_SERVER_REC *server)
(GInputFunction) irc_parse_incoming, server); (GInputFunction) irc_parse_incoming, server);
} }
#define isoptchan(a) \
(ischannel((a)[0]) || ((a)[0] == '*' && ((a)[1] == '\0' || (a)[1] == ' ')))
static char *irc_cmd_get_func(const char *data, int *count, va_list *vargs)
{
IRC_CHANNEL_REC *channel;
char *ret, *args, *chan, *p;
if ((*count & PARAM_FLAG_OPTCHAN) == 0)
return g_strdup(data);
*count &= ~PARAM_FLAG_OPTCHAN;
channel = (void *) va_arg(*vargs, void *);
channel = IRC_CHANNEL(channel);
/* change first argument in data to full channel name. */
p = args = g_strdup(data);
chan = isoptchan(args) ? cmd_get_param(&args) : NULL;
if (chan != NULL && *chan == '!' && channel != NULL) {
/* whenever trying to send something to !channel,
change it to the real joined !XXXXXchannel */
channel = irc_channel_find(channel->server, chan);
if (channel != NULL) chan = channel->name;
}
if (chan == NULL || strcmp(chan, "*") == 0)
chan = channel == NULL ? "*" : channel->name;
ret = g_strconcat(chan, " ", args, NULL);
g_free(p);
return ret;
}
void irc_irc_init(void) void irc_irc_init(void)
{ {
cmd_get_add_func(irc_cmd_get_func);
signal_add("server event", (SIGNAL_FUNC) irc_server_event); signal_add("server event", (SIGNAL_FUNC) irc_server_event);
signal_add("server connected", (SIGNAL_FUNC) irc_init_server); signal_add("server connected", (SIGNAL_FUNC) irc_init_server);
signal_add("server incoming", (SIGNAL_FUNC) irc_parse_incoming_line); signal_add("server incoming", (SIGNAL_FUNC) irc_parse_incoming_line);

View File

@ -50,12 +50,8 @@ void irc_send_cmd_now(IRC_SERVER_REC *server, const char *cmd);
void irc_send_cmd_full(IRC_SERVER_REC *server, const char *cmd, void irc_send_cmd_full(IRC_SERVER_REC *server, const char *cmd,
int send_now, int immediate, int raw); int send_now, int immediate, int raw);
#include "commands.h" /* contains the generic PARAM_FLAG_xxx defines */
/* IRC specific: optional channel in first argument */
#define PARAM_FLAG_OPTCHAN 0x10000000
/* Get count parameters from data */ /* Get count parameters from data */
#include "commands.h"
char *event_get_param(char **data); char *event_get_param(char **data);
char *event_get_params(const char *data, int count, ...); char *event_get_params(const char *data, int count, ...);