degesch: introduce cstr_set()

This commit is contained in:
Přemysl Eric Janouch 2018-01-08 22:15:29 +01:00
parent b4507b56af
commit 4586b0e1e4
Signed by: p
GPG Key ID: B715679E3A361BE6
2 changed files with 39 additions and 74 deletions

View File

@ -51,6 +51,13 @@ init_openssl (void)
// --- To be moved to liberty --------------------------------------------------
static void
cstr_set (char **s, char *new)
{
free (*s);
*s = new;
}
static ssize_t
strv_find (const struct strv *v, const char *s)
{

106
degesch.c
View File

@ -320,8 +320,7 @@ static void
input_rl_set_prompt (void *input, char *prompt)
{
struct input_rl *self = input;
free (self->prompt);
self->prompt = prompt;
cstr_set (&self->prompt, prompt);
if (!self->active)
return;
@ -543,8 +542,7 @@ input_rl__restore_buffer (struct input_rl *self, struct input_rl_buffer *buffer)
rl_replace_line (buffer->saved_line, true);
rl_point = buffer->saved_point;
rl_mark = buffer->saved_mark;
free (buffer->saved_line);
buffer->saved_line = NULL;
cstr_set (&buffer->saved_line, NULL);
if (self->prompt_shown > 0)
rl_redisplay ();
@ -652,8 +650,7 @@ input_rl__restore (struct input_rl *self)
rl_replace_line (self->saved_line, false);
rl_point = self->saved_point;
rl_mark = self->saved_mark;
free (self->saved_line);
self->saved_line = NULL;
cstr_set (&self->saved_line, NULL);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -834,8 +831,7 @@ static void
input_el_set_prompt (void *input, char *prompt)
{
struct input_el *self = input;
free (self->prompt);
self->prompt = prompt;
cstr_set (&self->prompt, prompt);
if (self->prompt_shown > 0)
input_el__redisplay (self);
@ -1020,8 +1016,7 @@ input_el__restore_buffer (struct input_el *self, struct input_el_buffer *buffer)
el_winsertstr (self->editline, buffer->saved_line);
el_cursor (self->editline,
-(buffer->saved_len - buffer->saved_point));
free (buffer->saved_line);
buffer->saved_line = NULL;
cstr_set (&buffer->saved_line, NULL);
}
}
@ -2697,10 +2692,9 @@ on_config_attribute_change (struct config_item *item)
ssize_t id = attr_by_name (item->schema->name);
if (id != -1)
{
free (ctx->attrs[id]);
ctx->attrs[id] = xstrdup (item->type == CONFIG_ITEM_NULL
cstr_set (&ctx->attrs[id], xstrdup (item->type == CONFIG_ITEM_NULL
? ctx->attrs_defaults[id]
: item->value.string.str);
: item->value.string.str));
}
}
@ -4256,8 +4250,7 @@ buffer_rename (struct app_context *ctx,
buffer_close_log_file (buffer);
buffer_open_log_file (ctx, buffer);
free (buffer->name);
buffer->name = xstrdup (new_name);
cstr_set (&buffer->name, xstrdup (new_name));
// We might have renamed the current buffer
refresh_prompt (ctx);
@ -4854,8 +4847,7 @@ irc_destroy_state (struct server *s)
}
str_reset (&s->irc_user_mode);
free (s->irc_user_host);
s->irc_user_host = NULL;
cstr_set (&s->irc_user_host, NULL);
s->cap_echo_message = false;
@ -6753,8 +6745,7 @@ irc_handle_nick (struct server *s, const struct irc_message *msg)
str_map_set (&s->irc_users, user->nickname, NULL);
str_map_set (&s->irc_users, new_nickname, user);
free (user->nickname);
user->nickname = xstrdup (new_nickname);
cstr_set (&user->nickname, xstrdup (new_nickname));
}
static void
@ -7066,10 +7057,7 @@ irc_handle_topic (struct server *s, const struct irc_message *msg)
// It would be is weird for this to be false
if (channel)
{
free (channel->topic);
channel->topic = xstrdup (topic);
}
cstr_set (&channel->topic, xstrdup (topic));
if (buffer)
{
@ -7111,9 +7099,8 @@ irc_try_parse_word_for_userhost (struct server *s, const char *word)
bool result = false;
if (!regexec (&re, word, 2, matches, 0))
{
free (s->irc_user_host);
s->irc_user_host = xstrndup (word + matches[1].rm_so,
matches[1].rm_eo - matches[1].rm_so);
cstr_set (&s->irc_user_host, xstrndup (word + matches[1].rm_so,
matches[1].rm_eo - matches[1].rm_so));
result = true;
}
regfree (&re);
@ -7140,7 +7127,7 @@ irc_on_registered (struct server *s, const char *nickname)
{
s->irc_user = irc_get_or_make_user (s, nickname);
str_reset (&s->irc_user_mode);
s->irc_user_host = NULL;
cstr_set (&s->irc_user_host, NULL);
s->state = IRC_REGISTERED;
refresh_prompt (s->ctx);
@ -7200,12 +7187,8 @@ irc_handle_rpl_userhost (struct server *s, const struct irc_message *msg)
char *userhost = equals + 2;
if (irc_is_this_us (s, nick))
{
free (s->irc_user_host);
s->irc_user_host = xstrdup (userhost);
}
cstr_set (&s->irc_user_host, xstrdup (userhost));
}
strv_free (&v);
}
@ -7401,10 +7384,7 @@ irc_handle_rpl_topic (struct server *s, const struct irc_message *msg)
hard_assert (channel || !buffer);
if (channel)
{
free (channel->topic);
channel->topic = xstrdup (topic);
}
cstr_set (&channel->topic, xstrdup (topic));
if (buffer)
log_server_status (s, buffer, "The topic is: #m", topic);
@ -7541,11 +7521,8 @@ irc_handle_isupport_prefix (struct server *s, char *value)
if (*modes++ != '(' || !prefixes++ || strlen (value) != 2 * n_prefixes--)
return;
free (s->irc_chanuser_modes);
free (s->irc_chanuser_prefixes);
s->irc_chanuser_modes = xstrndup (modes, n_prefixes);
s->irc_chanuser_prefixes = xstrndup (prefixes, n_prefixes);
cstr_set (&s->irc_chanuser_modes, xstrndup (modes, n_prefixes));
cstr_set (&s->irc_chanuser_prefixes, xstrndup (prefixes, n_prefixes));
}
static void
@ -7562,8 +7539,7 @@ irc_handle_isupport_casemapping (struct server *s, char *value)
static void
irc_handle_isupport_chantypes (struct server *s, char *value)
{
free (s->irc_chantypes);
s->irc_chantypes = xstrdup (value);
cstr_set (&s->irc_chantypes, xstrdup (value));
}
static void
@ -7581,16 +7557,13 @@ irc_handle_isupport_idchan (struct server *s, char *value)
str_append_data (&prefixes, pair, colon - pair);
}
strv_free (&v);
free (s->irc_idchan_prefixes);
s->irc_idchan_prefixes = str_steal (&prefixes);
cstr_set (&s->irc_idchan_prefixes, str_steal (&prefixes));
}
static void
irc_handle_isupport_statusmsg (struct server *s, char *value)
{
free (s->irc_statusmsg);
s->irc_statusmsg = xstrdup (value);
cstr_set (&s->irc_statusmsg, xstrdup (value));
}
static void
@ -7600,14 +7573,10 @@ irc_handle_isupport_chanmodes (struct server *s, char *value)
cstr_split (value, ",", true, &v);
if (v.len >= 4)
{
free (s->irc_chanmodes_list);
s->irc_chanmodes_list = xstrdup (v.vector[0]);
free (s->irc_chanmodes_param_always);
s->irc_chanmodes_param_always = xstrdup (v.vector[1]);
free (s->irc_chanmodes_param_when_set);
s->irc_chanmodes_param_when_set = xstrdup (v.vector[2]);
free (s->irc_chanmodes_param_never);
s->irc_chanmodes_param_never = xstrdup (v.vector[3]);
cstr_set (&s->irc_chanmodes_list, xstrdup (v.vector[0]));
cstr_set (&s->irc_chanmodes_param_always, xstrdup (v.vector[1]));
cstr_set (&s->irc_chanmodes_param_when_set, xstrdup (v.vector[2]));
cstr_set (&s->irc_chanmodes_param_never, xstrdup (v.vector[3]));
}
strv_free (&v);
}
@ -8188,9 +8157,7 @@ server_rename (struct app_context *ctx, struct server *s, const char *new_name)
struct str_map *servers = get_servers_config (ctx);
str_map_set (servers, new_name, str_map_steal (servers, s->name));
free (s->name);
s->name = xstrdup (new_name);
cstr_set (&s->name, xstrdup (new_name));
buffer_rename (ctx, s->buffer, new_name);
struct str_map_iter iter = str_map_iter_make (&s->irc_buffer_map);
@ -8366,8 +8333,7 @@ lua_plugin_handle_string_filter_result (struct lua_plugin *self,
lua_State *L = self->L;
if (lua_isnil (L, -1))
{
free (*original);
*original = NULL;
cstr_set (original, NULL);
return true;
}
if (!lua_isstring (L, -1))
@ -8380,10 +8346,7 @@ lua_plugin_handle_string_filter_result (struct lua_plugin *self,
// Only replace the string if it's different
if (strcmp (processed, *original))
{
free (*original);
*original = xstrdup (processed);
}
cstr_set (original, xstrdup (processed));
return true;
}
@ -9889,8 +9852,7 @@ static void
lua_wait_dial_on_error (void *user_data, const char *error)
{
struct lua_wait_dial *self = user_data;
free (self->last_error);
self->last_error = xstrdup (error);
cstr_set (&self->last_error, xstrdup (error));
}
static int
@ -10393,8 +10355,7 @@ plugin_load (struct app_context *ctx, const char *name)
{
// FIXME: this way the real name isn't available to the plugin on load,
// which has effect on e.g. plugin_config_name()
free (plugin->name);
plugin->name = xstrdup (name);
cstr_set (&plugin->name, xstrdup (name));
log_global_status (ctx, "Plugin \"#s\" loaded", name);
LIST_PREPEND (ctx->plugins, plugin);
@ -12431,8 +12392,7 @@ utf8_vector_to_locale (struct app_context *ctx, char **vector)
if (!soft_assert (converted))
converted = xstrdup ("");
free (*vector);
*vector = converted;
cstr_set (vector, converted);
}
}
@ -12647,9 +12607,7 @@ input_editor_cleanup (struct app_context *ctx)
log_global_error (ctx, "Could not unlink `#s': #l",
ctx->editor_filename, strerror (errno));
free (ctx->editor_filename);
ctx->editor_filename = NULL;
cstr_set (&ctx->editor_filename, NULL);
ctx->running_editor = false;
}