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