misc: Replace the idiom for replacing a string with a function call

Remove a lot of code in favor of a new function that is freeing
the old string and copying the new one. I should have gotten the
context and the strings right.
This commit is contained in:
Holger Hans Peter Freyther 2010-10-12 23:21:54 +02:00
parent 9ecf678995
commit 3e9a7f80bd
8 changed files with 29 additions and 57 deletions

View File

@ -35,5 +35,6 @@ enum bsc_vty_node {
};
extern int bsc_vty_is_config_node(struct vty *vty, int node);
extern void bsc_replace_string(void *ctx, char **dst, const char *newstr);
#endif

View File

@ -72,10 +72,7 @@ DEFUN(cfg_net_msc_token,
{
struct osmo_msc_data *data = osmo_msc_data(vty);
if (data->bsc_token)
talloc_free(data->bsc_token);
data->bsc_token = talloc_strdup(data, argv[0]);
bsc_replace_string(data, &data->bsc_token, argv[0]);
return CMD_SUCCESS;
}
@ -84,10 +81,8 @@ DEFUN(cfg_net_msc_ip,
"ip A.B.C.D", "Set the MSC/MUX IP address.")
{
struct osmo_msc_data *data = osmo_msc_data(vty);
if (data->msc_ip)
talloc_free(data->msc_ip);
data->msc_ip = talloc_strdup(data, argv[0]);
bsc_replace_string(data, &data->msc_ip, argv[0]);
return CMD_SUCCESS;
}
@ -142,9 +137,7 @@ DEFUN(cfg_net_msc_grace_ussd,
if (!txt)
return CMD_WARNING;
if (data->ussd_grace_txt)
talloc_free(data->ussd_grace_txt);
data->ussd_grace_txt = talloc_strdup(data, txt);
bsc_replace_string(data, &data->ussd_grace_txt, txt);
talloc_free(txt);
return CMD_SUCCESS;
}

View File

@ -1062,11 +1062,7 @@ DEFUN(cfg_net_name_short,
{
struct gsm_network *gsmnet = gsmnet_from_vty(vty);
if (gsmnet->name_short)
talloc_free(gsmnet->name_short);
gsmnet->name_short = talloc_strdup(gsmnet, argv[0]);
bsc_replace_string(gsmnet, &gsmnet->name_short, argv[0]);
return CMD_SUCCESS;
}
@ -1077,11 +1073,7 @@ DEFUN(cfg_net_name_long,
{
struct gsm_network *gsmnet = gsmnet_from_vty(vty);
if (gsmnet->name_long)
talloc_free(gsmnet->name_long);
gsmnet->name_long = talloc_strdup(gsmnet, argv[0]);
bsc_replace_string(gsmnet, &gsmnet->name_long, argv[0]);
return CMD_SUCCESS;
}

View File

@ -205,3 +205,11 @@ int bsc_vty_is_config_node(struct vty *vty, int node)
return 1;
}
}
/* a talloc string replace routine */
void bsc_replace_string(void *ctx, char **dst, const char *newstr)
{
if (*dst)
talloc_free(*dst);
*dst = talloc_strdup(ctx, newstr);
}

View File

@ -127,9 +127,7 @@ DEFUN(cfg_mgcp_local_ip,
"local ip A.B.C.D",
"Set the IP to be used in SDP records")
{
if (g_cfg->local_ip)
talloc_free(g_cfg->local_ip);
g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->local_ip, argv[0]);
return CMD_SUCCESS;
}
@ -138,9 +136,7 @@ DEFUN(cfg_mgcp_bts_ip,
"bts ip A.B.C.D",
"Set the IP of the BTS for RTP forwarding")
{
if (g_cfg->bts_ip)
talloc_free(g_cfg->bts_ip);
g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->bts_ip, argv[0]);
inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
return CMD_SUCCESS;
}
@ -150,9 +146,7 @@ DEFUN(cfg_mgcp_bind_ip,
"bind ip A.B.C.D",
"Bind the MGCP to this local addr")
{
if (g_cfg->source_addr)
talloc_free(g_cfg->source_addr);
g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->source_addr, argv[0]);
return CMD_SUCCESS;
}
@ -281,9 +275,7 @@ DEFUN(cfg_mgcp_sdp_payload_name,
"sdp audio payload name NAME",
"Set the audio name to use")
{
if (g_cfg->audio_name)
talloc_free(g_cfg->audio_name);
g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->audio_name, argv[0]);
return CMD_SUCCESS;
}
@ -311,9 +303,7 @@ DEFUN(cfg_mgcp_agent_addr,
"call agent ip IP",
"Set the address of the call agent.")
{
if (g_cfg->call_agent_addr)
talloc_free(g_cfg->call_agent_addr);
g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->call_agent_addr, argv[0]);
return CMD_SUCCESS;
}
@ -323,9 +313,7 @@ DEFUN(cfg_mgcp_transcoder,
"Use a MGW to detranscoder RTP\n"
"The IP address of the MGW")
{
if (g_cfg->transcoder_ip)
talloc_free(g_cfg->transcoder_ip);
g_cfg->transcoder_ip = talloc_strdup(g_cfg, argv[0]);
bsc_replace_string(g_cfg, &g_cfg->transcoder_ip, argv[0]);
inet_aton(g_cfg->transcoder_ip, &g_cfg->transcoder_in);
return CMD_SUCCESS;

View File

@ -28,6 +28,7 @@
#include <openbsc/gsm_data.h>
#include <openbsc/debug.h>
#include <openbsc/ipaccess.h>
#include <openbsc/vty.h>
#include <osmocore/linuxlist.h>
#include <osmocore/talloc.h>
@ -100,9 +101,7 @@ struct bsc_nat *bsc_nat_alloc(void)
void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
{
if (nat->msc_ip)
talloc_free(nat->msc_ip);
nat->msc_ip = talloc_strdup(nat, ip);
bsc_replace_string(nat, &nat->msc_ip, ip);
}
struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)

View File

@ -368,9 +368,7 @@ DEFUN(cfg_nat_token, cfg_nat_token_cmd,
"token TOKEN",
"Set a token for the NAT")
{
if (_nat->token)
talloc_free(_nat->token);
_nat->token = talloc_strdup(_nat, argv[0]);
bsc_replace_string(_nat, &_nat->token, argv[0]);
return CMD_SUCCESS;
}
@ -393,9 +391,7 @@ DEFUN(cfg_nat_acc_lst_name,
"Set the name of the access list to use.\n"
"The name of the to be used access list.")
{
if (_nat->acc_lst_name)
talloc_free(_nat->acc_lst_name);
_nat->acc_lst_name = talloc_strdup(_nat, argv[0]);
bsc_replace_string(_nat, &_nat->acc_lst_name, argv[0]);
return CMD_SUCCESS;
}
@ -428,9 +424,7 @@ DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
{
struct bsc_config *conf = vty->index;
if (conf->token)
talloc_free(conf->token);
conf->token = talloc_strdup(conf, argv[0]);
bsc_replace_string(conf, &conf->token, argv[0]);
return CMD_SUCCESS;
}
@ -559,9 +553,7 @@ DEFUN(cfg_bsc_acc_lst_name,
{
struct bsc_config *conf = vty->index;
if (conf->acc_lst_name)
talloc_free(conf->acc_lst_name);
conf->acc_lst_name = talloc_strdup(conf, argv[0]);
bsc_replace_string(conf, &conf->acc_lst_name, argv[0]);
return CMD_SUCCESS;
}
@ -587,9 +579,7 @@ DEFUN(cfg_bsc_desc,
{
struct bsc_config *conf = vty->index;
if (conf->description)
talloc_free(conf->description);
conf->description = talloc_strdup(conf, argv[0]);
bsc_replace_string(conf, &conf->description, argv[0]);
return CMD_SUCCESS;
}

View File

@ -6,10 +6,11 @@ EXTRA_DIST = bsc_data.c
noinst_PROGRAMS = bsc_nat_test
bsc_nat_test_SOURCES = bsc_nat_test.c \
$(top_srcdir)/src/common_vty.c \
$(top_srcdir)/src/nat/bsc_filter.c \
$(top_srcdir)/src/nat/bsc_sccp.c \
$(top_srcdir)/src/nat/bsc_nat_utils.c \
$(top_srcdir)/src/nat/bsc_mgcp_utils.c \
$(top_srcdir)/src/mgcp/mgcp_protocol.c \
$(top_srcdir)/src/mgcp/mgcp_network.c
bsc_nat_test_LDADD = $(top_builddir)/src/libbsc.a $(LIBOSMOCORE_LIBS) -lrt $(LIBOSMOSCCP_LIBS)
bsc_nat_test_LDADD = $(top_builddir)/src/libbsc.a $(LIBOSMOCORE_LIBS) -lrt $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS)