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.
diff --git a/openbsc/include/openbsc/vty.h b/openbsc/include/openbsc/vty.h
index d627788..5cdd204 100644
--- a/openbsc/include/openbsc/vty.h
+++ b/openbsc/include/openbsc/vty.h
@@ -35,5 +35,6 @@
 };
 
 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 ac366ab..d5c018b 100644
--- a/openbsc/src/bsc/osmo_bsc_vty.c
+++ b/openbsc/src/bsc/osmo_bsc_vty.c
@@ -72,10 +72,7 @@
 {
 	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 @@
       "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 @@
 	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 97641f2..488ecc7 100644
--- a/openbsc/src/bsc_vty.c
+++ b/openbsc/src/bsc_vty.c
@@ -1062,11 +1062,7 @@
 {
 	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 @@
 {
 	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 72d5163..541c189 100644
--- a/openbsc/src/common_vty.c
+++ b/openbsc/src/common_vty.c
@@ -205,3 +205,11 @@
 		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 a9845a1..d78b8c7 100644
--- a/openbsc/src/mgcp/mgcp_vty.c
+++ b/openbsc/src/mgcp/mgcp_vty.c
@@ -127,9 +127,7 @@
       "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 @@
       "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 @@
       "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 @@
       "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 @@
       "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 @@
       "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 e11b48c..89d7d4b 100644
--- a/openbsc/src/nat/bsc_nat_utils.c
+++ b/openbsc/src/nat/bsc_nat_utils.c
@@ -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 @@
 
 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 1dbc755..9822e5c 100644
--- a/openbsc/src/nat/bsc_nat_vty.c
+++ b/openbsc/src/nat/bsc_nat_vty.c
@@ -368,9 +368,7 @@
       "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 @@
       "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 @@
 {
 	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 @@
 {
 	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 @@
 {
 	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 f5af721..fc77557 100644
--- a/openbsc/tests/bsc-nat/Makefile.am
+++ b/openbsc/tests/bsc-nat/Makefile.am
@@ -6,10 +6,11 @@
 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)