1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

Merge pull request #422 from LemonBoy/misc-cleanup

Clean up misc.c
This commit is contained in:
ailin-nemui 2016-06-14 12:35:30 +02:00 committed by GitHub
commit fd371cc345
20 changed files with 63 additions and 189 deletions

View File

@ -6,7 +6,7 @@
#define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */ #define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */
#define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */ #define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */
#define IRSSI_ABI_VERSION 5 #define IRSSI_ABI_VERSION 6
#define DEFAULT_SERVER_ADD_PORT 6667 #define DEFAULT_SERVER_ADD_PORT 6667

View File

@ -241,7 +241,7 @@ IGNORE_REC *ignore_find_full(const char *servertag, const char *mask, const char
if (channels == NULL || rec->channels == NULL) if (channels == NULL || rec->channels == NULL)
continue; /* other doesn't have channels */ continue; /* other doesn't have channels */
if (strarray_length(channels) != strarray_length(rec->channels)) if (g_strv_length(channels) != g_strv_length(rec->channels))
continue; /* different amount of channels */ continue; /* different amount of channels */
/* check that channels match */ /* check that channels match */

View File

@ -114,7 +114,7 @@ int log_start_logging(LOG_REC *log)
/* path may contain variables (%time, $vars), /* path may contain variables (%time, $vars),
make sure the directory is created */ make sure the directory is created */
dir = g_path_get_dirname(log->real_fname); dir = g_path_get_dirname(log->real_fname);
mkpath(dir, log_dir_create_mode); g_mkdir_with_parents(dir, log_dir_create_mode);
g_free(dir); g_free(dir);
} }

View File

@ -20,6 +20,7 @@
#include "module.h" #include "module.h"
#include "misc.h" #include "misc.h"
#include "commands.h"
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
# include <regex.h> # include <regex.h>
@ -150,20 +151,6 @@ int find_substr(const char *list, const char *item)
return FALSE; return FALSE;
} }
int strarray_length(char **array)
{
int len;
g_return_val_if_fail(array != NULL, 0);
len = 0;
while (*array) {
len++;
array++;
}
return len;
}
int strarray_find(char **array, const char *item) int strarray_find(char **array, const char *item)
{ {
char **tmp; char **tmp;
@ -279,14 +266,23 @@ void hash_save_key(char *key, void *value, GSList **list)
*list = g_slist_append(*list, key); *list = g_slist_append(*list, key);
} }
/* save all keys in hash table to linked list - you shouldn't remove any /* remove all the options from the optlist hash table that are valid for the
items while using this list, use g_slist_free() after you're done with it */ * command cmd */
GSList *hashtable_get_keys(GHashTable *hash) GList *optlist_remove_known(const char *cmd, GHashTable *optlist)
{ {
GSList *list; GList *list, *tmp, *next;
list = g_hash_table_get_keys(optlist);
if (cmd != NULL && list != NULL) {
for (tmp = list; tmp != NULL; tmp = next) {
char *option = tmp->data;
next = tmp->next;
if (command_have_option(cmd, option))
list = g_list_remove(list, option);
}
}
list = NULL;
g_hash_table_foreach(hash, (GHFunc) hash_save_key, &list);
return list; return list;
} }
@ -389,62 +385,6 @@ char *stristr_full(const char *data, const char *key)
return strstr_full_case(data, key, TRUE); return strstr_full_case(data, key, TRUE);
} }
int regexp_match(const char *str, const char *regexp)
{
#ifdef HAVE_REGEX_H
regex_t preg;
int ret;
if (regcomp(&preg, regexp, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
return 0;
ret = regexec(&preg, str, 0, NULL, 0);
regfree(&preg);
return ret == 0;
#else
return FALSE;
#endif
}
/* Create the directory and all it's parent directories */
int mkpath(const char *path, int mode)
{
struct stat statbuf;
const char *p;
char *dir;
g_return_val_if_fail(path != NULL, -1);
p = g_path_skip_root((char *) path);
if (p == NULL) {
/* not a full path, maybe not what we wanted
but continue anyway.. */
p = path;
}
for (;;) {
if (*p != G_DIR_SEPARATOR && *p != '\0') {
p++;
continue;
}
dir = g_strndup(path, (int) (p-path));
if (stat(dir, &statbuf) != 0) {
if (mkdir(dir, mode) == -1)
{
g_free(dir);
return -1;
}
}
g_free(dir);
if (*p++ == '\0')
break;
}
return 0;
}
/* convert ~/ to $HOME */ /* convert ~/ to $HOME */
char *convert_home(const char *path) char *convert_home(const char *path)
{ {
@ -471,22 +411,15 @@ int g_istr_cmp(gconstpointer v, gconstpointer v2)
return g_ascii_strcasecmp((const char *) v, (const char *) v2); return g_ascii_strcasecmp((const char *) v, (const char *) v2);
} }
/* a char* hash function from ASU */ guint g_istr_hash(gconstpointer v)
unsigned int g_istr_hash(gconstpointer v)
{ {
const char *s = (const char *) v; const signed char *p;
unsigned int h = 0, g; guint32 h = 5381;
while (*s != '\0') { for (p = v; *p != '\0'; p++)
h = (h << 4) + i_toupper(*s); h = (h << 5) + h + g_ascii_toupper(*p);
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h /* % M */; return h;
} }
/* Find `mask' from `data', you can use * and ? wildcards. */ /* Find `mask' from `data', you can use * and ? wildcards. */
@ -592,15 +525,11 @@ int dec2octal(int decimal)
/* string -> uoff_t */ /* string -> uoff_t */
uoff_t str_to_uofft(const char *str) uoff_t str_to_uofft(const char *str)
{ {
uoff_t ret; #ifdef UOFF_T_LONG_LONG
return (uoff_t)strtoull(str, NULL, 10);
ret = 0; #else
while (*str != '\0') { return (uoff_t)strtoul(str, NULL, 10);
ret = ret*10 + (*str - '0'); #endif
str++;
}
return ret;
} }
/* convert all low-ascii (<32) to ^<A..> combinations */ /* convert all low-ascii (<32) to ^<A..> combinations */
@ -811,20 +740,6 @@ char *escape_string(const char *str)
return ret; return ret;
} }
int strocpy(char *dest, const char *src, size_t dstsize)
{
if (dstsize == 0)
return -1;
while (*src != '\0' && dstsize > 1) {
*dest++ = *src++;
dstsize--;
}
*dest++ = '\0';
return *src == '\0' ? 0 : -1;
}
int nearest_power(int num) int nearest_power(int num)
{ {
int n = 1; int n = 1;

View File

@ -32,15 +32,8 @@ char *gslistptr_to_string(GSList *list, int offset, const char *delimiter);
/* `list' contains char* */ /* `list' contains char* */
char *gslist_to_string(GSList *list, const char *delimiter); char *gslist_to_string(GSList *list, const char *delimiter);
/* save all keys in hash table to linked list - you shouldn't remove any GList *optlist_remove_known(const char *cmd, GHashTable *optlist);
items while using this list, use g_slist_free() after you're done with it */
GSList *hashtable_get_keys(GHashTable *hash);
/* easy way to check if regexp matches */
int regexp_match(const char *str, const char *regexp);
/* Create the directory and all it's parent directories */
int mkpath(const char *path, int mode);
/* convert ~/ to $HOME */ /* convert ~/ to $HOME */
char *convert_home(const char *path); char *convert_home(const char *path);
@ -85,9 +78,6 @@ int parse_size(const char *size, int *bytes);
Stop when `end_char' is found from string. */ Stop when `end_char' is found from string. */
int is_numeric(const char *str, char end_char); int is_numeric(const char *str, char end_char);
/* Like strlcpy(), but return -1 if buffer was overflown, 0 if not. */
int strocpy(char *dest, const char *src, size_t dstsize);
/* strstr() with case-ignoring */ /* strstr() with case-ignoring */
char *stristr(const char *data, const char *key); char *stristr(const char *data, const char *key);
@ -107,8 +97,6 @@ char *show_lowascii(const char *str);
/* replace all `from' chars in string to `to' chars. returns `str' */ /* replace all `from' chars in string to `to' chars. returns `str' */
char *replace_chars(char *str, char from, char to); char *replace_chars(char *str, char from, char to);
/* return how many items `array' has */
int strarray_length(char **array);
/* return index of `item' in `array' or -1 if not found */ /* return index of `item' in `array' or -1 if not found */
int strarray_find(char **array, const char *item); int strarray_find(char **array, const char *item);

View File

@ -37,7 +37,7 @@ GIOChannel *g_io_channel_new(int handle);
int net_ip_compare(IPADDR *ip1, IPADDR *ip2); int net_ip_compare(IPADDR *ip1, IPADDR *ip2);
/* Connect to socket */ /* Connect to socket */
GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip); GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip) G_GNUC_DEPRECATED;
/* Connect to socket with ip address and SSL*/ /* Connect to socket with ip address and SSL*/
GIOChannel *net_connect_ip_ssl(IPADDR *ip, int port, IPADDR *my_ip, SERVER_REC *server); GIOChannel *net_connect_ip_ssl(IPADDR *ip, int port, IPADDR *my_ip, SERVER_REC *server);
int irssi_ssl_handshake(GIOChannel *handle); int irssi_ssl_handshake(GIOChannel *handle);

View File

@ -150,13 +150,18 @@ void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
int f; int f;
dir = g_path_get_dirname(fname); dir = g_path_get_dirname(fname);
mkpath(dir, log_dir_create_mode); g_mkdir_with_parents(dir, log_dir_create_mode);
g_free(dir); g_free(dir);
path = convert_home(fname); path = convert_home(fname);
f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode); f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
g_free(path); g_free(path);
if (f < 0) {
g_warning("rawlog open() failed: %s", strerror(errno));
return;
}
rawlog_dump(rawlog, f); rawlog_dump(rawlog, f);
close(f); close(f);
} }

View File

@ -684,21 +684,11 @@ SERVER_REC *cmd_options_get_server(const char *cmd,
SERVER_REC *defserver) SERVER_REC *defserver)
{ {
SERVER_REC *server; SERVER_REC *server;
GSList *list, *tmp, *next; GList *list;
/* get all the options, then remove the known ones. there should /* get all the options, then remove the known ones. there should
be only one left - the server tag. */ be only one left - the server tag. */
list = hashtable_get_keys(optlist); list = optlist_remove_known(cmd, optlist);
if (cmd != NULL) {
for (tmp = list; tmp != NULL; tmp = next) {
char *option = tmp->data;
next = tmp->next;
if (command_have_option(cmd, option))
list = g_slist_remove(list, option);
}
}
if (list == NULL) if (list == NULL)
return defserver; return defserver;
@ -713,7 +703,7 @@ SERVER_REC *cmd_options_get_server(const char *cmd,
server = NULL; server = NULL;
} }
g_slist_free(list); g_list_free(list);
return server; return server;
} }

View File

@ -752,7 +752,7 @@ static void init_configfile(void)
if (stat(get_irssi_dir(), &statbuf) != 0) { if (stat(get_irssi_dir(), &statbuf) != 0) {
/* ~/.irssi not found, create it. */ /* ~/.irssi not found, create it. */
if (mkpath(get_irssi_dir(), 0700) != 0) { if (g_mkdir_with_parents(get_irssi_dir(), 0700) != 0) {
g_error("Couldn't create %s directory", get_irssi_dir()); g_error("Couldn't create %s directory", get_irssi_dir());
} }
} else if (!S_ISDIR(statbuf.st_mode)) { } else if (!S_ISDIR(statbuf.st_mode)) {

View File

@ -44,7 +44,7 @@ static char *get_argument(char **cmd, char **arglist)
arg = 0; arg = 0;
max = -1; max = -1;
argcount = arglist == NULL ? 0 : strarray_length(arglist); argcount = arglist == NULL ? 0 : g_strv_length(arglist);
if (**cmd == '*') { if (**cmd == '*') {
/* get all arguments */ /* get all arguments */

View File

@ -237,22 +237,13 @@ static int signal_name_to_id(const char *name)
static int cmd_options_get_signal(const char *cmd, static int cmd_options_get_signal(const char *cmd,
GHashTable *optlist) GHashTable *optlist)
{ {
GSList *list, *tmp, *next; GList *list;
char *signame; char *signame;
int signum; int signum;
/* get all the options, then remove the known ones. there should /* get all the options, then remove the known ones. there should
be only one left - the signal */ be only one left - the signal */
list = hashtable_get_keys(optlist); list = optlist_remove_known(cmd, optlist);
if (cmd != NULL) {
for (tmp = list; tmp != NULL; tmp = next) {
char *option = tmp->data;
next = tmp->next;
if (command_have_option(cmd, option))
list = g_slist_remove(list, option);
}
}
if (list == NULL) if (list == NULL)
return -1; return -1;
@ -272,7 +263,7 @@ static int cmd_options_get_signal(const char *cmd,
return -2; return -2;
} }
g_slist_free(list); g_list_free(list);
return signum; return signum;
} }

View File

@ -453,7 +453,7 @@ static void autolog_open(SERVER_REC *server, const char *server_tag,
log_item_add(log, LOG_ITEM_TARGET, target, server_tag); log_item_add(log, LOG_ITEM_TARGET, target, server_tag);
dir = g_path_get_dirname(log->real_fname); dir = g_path_get_dirname(log->real_fname);
mkpath(dir, log_dir_create_mode); g_mkdir_with_parents(dir, log_dir_create_mode);
g_free(dir); g_free(dir);
log->temp = TRUE; log->temp = TRUE;

View File

@ -178,7 +178,7 @@ static HILIGHT_REC *hilight_find(const char *text, char **channels)
if (channels == NULL || rec->channels == NULL) if (channels == NULL || rec->channels == NULL)
continue; /* other doesn't have channels */ continue; /* other doesn't have channels */
if (strarray_length(channels) != strarray_length(rec->channels)) if (g_strv_length(channels) != g_strv_length(rec->channels))
continue; /* different amount of channels */ continue; /* different amount of channels */
/* check that channels match */ /* check that channels match */

View File

@ -39,21 +39,10 @@
Returns -1 if unknown option was given. */ Returns -1 if unknown option was given. */
int cmd_options_get_level(const char *cmd, GHashTable *optlist) int cmd_options_get_level(const char *cmd, GHashTable *optlist)
{ {
GSList *list, *tmp, *next; GList *list;
int level, retlevel; int level, retlevel;
/* get all the options, then remove the known ones. there should list = optlist_remove_known(cmd, optlist);
be only one left - the server tag. */
list = hashtable_get_keys(optlist);
if (cmd != NULL) {
for (tmp = list; tmp != NULL; tmp = next) {
char *option = tmp->data;
next = tmp->next;
if (command_have_option(cmd, option))
list = g_slist_remove(list, option);
}
}
retlevel = 0; retlevel = 0;
while (list != NULL) { while (list != NULL) {
@ -68,7 +57,7 @@ int cmd_options_get_level(const char *cmd, GHashTable *optlist)
} }
retlevel |= level; retlevel |= level;
list = g_slist_remove(list, list->data); list = g_list_remove(list, list->data);
} }
return retlevel; return retlevel;

View File

@ -48,7 +48,7 @@ NICK_REC *irc_nicklist_insert(IRC_CHANNEL_REC *channel, const char *nick,
rec->send_massjoin = send_massjoin; rec->send_massjoin = send_massjoin;
if (prefixes != NULL) { if (prefixes != NULL) {
strocpy(rec->prefixes, prefixes, sizeof(rec->prefixes)); g_strlcpy(rec->prefixes, prefixes, sizeof(rec->prefixes));
} }
nicklist_insert(CHANNEL(channel), rec); nicklist_insert(CHANNEL(channel), rec);

View File

@ -212,7 +212,6 @@ static void server_init(IRC_SERVER_REC *server)
{ {
IRC_SERVER_CONNECT_REC *conn; IRC_SERVER_CONNECT_REC *conn;
char *address, *ptr, *username, *cmd; char *address, *ptr, *username, *cmd;
GTimeVal now;
g_return_if_fail(server != NULL); g_return_if_fail(server != NULL);
@ -287,9 +286,8 @@ static void server_init(IRC_SERVER_REC *server)
/* prevent the queue from sending too early, we have a max cut off of 120 secs */ /* prevent the queue from sending too early, we have a max cut off of 120 secs */
/* this will reset to 1 sec after we get the 001 event */ /* this will reset to 1 sec after we get the 001 event */
g_get_current_time(&now); g_get_current_time(&server->wait_cmd);
memcpy(&((IRC_SERVER_REC *)server)->wait_cmd, &now, sizeof(GTimeVal)); g_time_val_add(&server->wait_cmd, 120 * G_USEC_PER_SEC);
((IRC_SERVER_REC *)server)->wait_cmd.tv_sec += 120;
} }
SERVER_REC *irc_server_init_connect(SERVER_CONNECT_REC *conn) SERVER_REC *irc_server_init_connect(SERVER_CONNECT_REC *conn)
@ -535,7 +533,7 @@ void irc_server_send_data(IRC_SERVER_REC *server, const char *data, int len)
server->wait_cmd.tv_sec = 0; server->wait_cmd.tv_sec = 0;
else { else {
memcpy(&server->wait_cmd, &server->last_cmd, sizeof(GTimeVal)); memcpy(&server->wait_cmd, &server->last_cmd, sizeof(GTimeVal));
server->wait_cmd.tv_sec += 2 + len/100; g_time_val_add(&server->wait_cmd, (2 + len/100) * G_USEC_PER_SEC);
} }
} }
@ -679,7 +677,6 @@ char *irc_server_get_channels(IRC_SERVER_REC *server)
static void event_connected(IRC_SERVER_REC *server, const char *data, const char *from) static void event_connected(IRC_SERVER_REC *server, const char *data, const char *from)
{ {
char *params, *nick; char *params, *nick;
GTimeVal now;
g_return_if_fail(server != NULL); g_return_if_fail(server != NULL);
@ -702,8 +699,7 @@ static void event_connected(IRC_SERVER_REC *server, const char *data, const char
server->real_connect_time = time(NULL); server->real_connect_time = time(NULL);
/* let the queue send now that we are identified */ /* let the queue send now that we are identified */
g_get_current_time(&now); g_get_current_time(&server->wait_cmd);
memcpy(&server->wait_cmd, &now, sizeof(GTimeVal));
if (server->connrec->usermode != NULL) { if (server->connrec->usermode != NULL) {
/* Send the user mode, before the autosendcmd. /* Send the user mode, before the autosendcmd.

View File

@ -619,7 +619,7 @@ static void ctcp_msg_dcc_chat(IRC_SERVER_REC *server, const char *data,
/* CHAT <unused> <address> <port> */ /* CHAT <unused> <address> <port> */
/* CHAT <unused> <address> 0 <id> (DCC CHAT passive protocol) */ /* CHAT <unused> <address> 0 <id> (DCC CHAT passive protocol) */
params = g_strsplit(data, " ", -1); params = g_strsplit(data, " ", -1);
paramcount = strarray_length(params); paramcount = g_strv_length(params);
if (paramcount < 3) { if (paramcount < 3) {
g_strfreev(params); g_strfreev(params);

View File

@ -423,7 +423,7 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
/* SEND <file name> <address> <port> <size> [...] */ /* SEND <file name> <address> <port> <size> [...] */
/* SEND <file name> <address> 0 <size> <id> (DCC SEND passive protocol) */ /* SEND <file name> <address> 0 <size> <id> (DCC SEND passive protocol) */
params = g_strsplit(data, " ", -1); params = g_strsplit(data, " ", -1);
paramcount = strarray_length(params); paramcount = g_strv_length(params);
if (paramcount < 4) { if (paramcount < 4) {
signal_emit("dcc error ctcp", 5, "SEND", data, signal_emit("dcc error ctcp", 5, "SEND", data,
@ -473,7 +473,7 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
net_ip2host(&temp_dcc->addr, temp_dcc->addrstr); net_ip2host(&temp_dcc->addr, temp_dcc->addrstr);
else { else {
/* with IPv6, show it to us as it was sent */ /* with IPv6, show it to us as it was sent */
strocpy(temp_dcc->addrstr, address, g_strlcpy(temp_dcc->addrstr, address,
sizeof(temp_dcc->addrstr)); sizeof(temp_dcc->addrstr));
} }
@ -508,7 +508,7 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
net_ip2host(&dcc->addr, dcc->addrstr); net_ip2host(&dcc->addr, dcc->addrstr);
else { else {
/* with IPv6, show it to us as it was sent */ /* with IPv6, show it to us as it was sent */
strocpy(dcc->addrstr, address, sizeof(dcc->addrstr)); g_strlcpy(dcc->addrstr, address, sizeof(dcc->addrstr));
} }
dcc->port = port; dcc->port = port;
dcc->size = size; dcc->size = size;

View File

@ -88,7 +88,7 @@ static int dcc_ctcp_resume_parse(int type, const char *data, const char *nick,
/* RESUME|ACCEPT <file name> <port> <size> */ /* RESUME|ACCEPT <file name> <port> <size> */
/* RESUME|ACCEPT <file name> 0 <size> <id> (passive protocol) */ /* RESUME|ACCEPT <file name> 0 <size> <id> (passive protocol) */
params = g_strsplit(data, " ", -1); params = g_strsplit(data, " ", -1);
paramcount = strarray_length(params); paramcount = g_strv_length(params);
if (paramcount < 3) if (paramcount < 3)
return 0; return 0;

View File

@ -245,7 +245,7 @@ static void dcc_server_msg(SERVER_DCC_REC *dcc, const char *msg)
/* 120 clientnickname filesize filename */ /* 120 clientnickname filesize filename */
params = g_strsplit(msg, " ", -1); params = g_strsplit(msg, " ", -1);
paramcount = strarray_length(params); paramcount = g_strv_length(params);
if (paramcount < 3) { if (paramcount < 3) {
g_strfreev(params); g_strfreev(params);