From 3e9a7f80bd6b1d62d3c3b175382ff15fa2f106c7 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Tue, 12 Oct 2010 23:21:54 +0200 Subject: 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. --- openbsc/include/openbsc/vty.h | 1 + openbsc/src/bsc/osmo_bsc_vty.c | 13 +++---------- openbsc/src/bsc_vty.c | 12 ++---------- openbsc/src/common_vty.c | 8 ++++++++ openbsc/src/mgcp/mgcp_vty.c | 24 ++++++------------------ openbsc/src/nat/bsc_nat_utils.c | 5 ++--- openbsc/src/nat/bsc_nat_vty.c | 20 +++++--------------- openbsc/tests/bsc-nat/Makefile.am | 3 ++- 8 files changed, 29 insertions(+), 57 deletions(-) (limited to 'openbsc') diff --git a/openbsc/include/openbsc/vty.h b/openbsc/include/openbsc/vty.h index d62778840..5cdd204d8 100644 --- a/openbsc/include/openbsc/vty.h +++ b/openbsc/include/openbsc/vty.h @@ -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 diff --git a/openbsc/src/bsc/osmo_bsc_vty.c b/openbsc/src/bsc/osmo_bsc_vty.c index ac366abb9..d5c018b7a 100644 --- a/openbsc/src/bsc/osmo_bsc_vty.c +++ b/openbsc/src/bsc/osmo_bsc_vty.c @@ -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; } diff --git a/openbsc/src/bsc_vty.c b/openbsc/src/bsc_vty.c index 97641f2b1..488ecc7f1 100644 --- a/openbsc/src/bsc_vty.c +++ b/openbsc/src/bsc_vty.c @@ -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; } diff --git a/openbsc/src/common_vty.c b/openbsc/src/common_vty.c index 72d5163d8..541c18994 100644 --- a/openbsc/src/common_vty.c +++ b/openbsc/src/common_vty.c @@ -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); +} diff --git a/openbsc/src/mgcp/mgcp_vty.c b/openbsc/src/mgcp/mgcp_vty.c index a9845a12f..d78b8c78a 100644 --- a/openbsc/src/mgcp/mgcp_vty.c +++ b/openbsc/src/mgcp/mgcp_vty.c @@ -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; diff --git a/openbsc/src/nat/bsc_nat_utils.c b/openbsc/src/nat/bsc_nat_utils.c index e11b48c31..89d7d4b22 100644 --- a/openbsc/src/nat/bsc_nat_utils.c +++ b/openbsc/src/nat/bsc_nat_utils.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -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) diff --git a/openbsc/src/nat/bsc_nat_vty.c b/openbsc/src/nat/bsc_nat_vty.c index 1dbc75569..9822e5c57 100644 --- a/openbsc/src/nat/bsc_nat_vty.c +++ b/openbsc/src/nat/bsc_nat_vty.c @@ -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; } diff --git a/openbsc/tests/bsc-nat/Makefile.am b/openbsc/tests/bsc-nat/Makefile.am index f5af721b9..fc77557d0 100644 --- a/openbsc/tests/bsc-nat/Makefile.am +++ b/openbsc/tests/bsc-nat/Makefile.am @@ -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) -- cgit v1.2.3