VTY: pass program name, version and copyright to vty_init()

This enables us to make the VTY completely independent of any
compile-time program-specific information, i.e. one step closer
to using VTY as a shared library from multiple programs.
diff --git a/openbsc/include/vty/command.h b/openbsc/include/vty/command.h
index 38d90b4..ec594ed 100644
--- a/openbsc/include/vty/command.h
+++ b/openbsc/include/vty/command.h
@@ -57,6 +57,10 @@
 	/* Banner configuration. */
 	const char *motd;
 	char *motdfile;
+
+	const char *prog_name;
+	const char *prog_version;
+	const char *prog_copyright;
 };
 
 /* There are some command levels which called from command node. */
@@ -377,7 +381,8 @@
 char *host_config_file();
 void host_config_set(const char *);
 
-void print_version(const char *);
+/* This is called from main when a daemon is invoked with -v or --version. */
+void print_version(int print_copyright);
 
 extern void *tall_vty_cmd_ctx;
 
diff --git a/openbsc/include/vty/vty.h b/openbsc/include/vty/vty.h
index 0441fc5..23430d5 100644
--- a/openbsc/include/vty/vty.h
+++ b/openbsc/include/vty/vty.h
@@ -128,7 +128,7 @@
 }
 
 /* Prototypes. */
-void vty_init (void);
+void vty_init(const char *name, const char *version, const char *copyright);
 int vty_read_config_file(const char *file_name);
 void vty_init_vtysh (void);
 void vty_reset (void);
diff --git a/openbsc/src/Makefile.am b/openbsc/src/Makefile.am
index 177c410..6c89bd3 100644
--- a/openbsc/src/Makefile.am
+++ b/openbsc/src/Makefile.am
@@ -7,7 +7,6 @@
 
 sbin_PROGRAMS = bsc_hack bs11_config isdnsync bsc_mgcp
 noinst_LIBRARIES = libbsc.a libmsc.a libvty.a libsccp.a
-noinst_HEADERS = vty/cardshell.h
 
 bscdir = $(libdir)
 bsc_LIBRARIES = libsccp.a
diff --git a/openbsc/src/bsc_hack.c b/openbsc/src/bsc_hack.c
index a50d4ab..7435122 100644
--- a/openbsc/src/bsc_hack.c
+++ b/openbsc/src/bsc_hack.c
@@ -42,8 +42,6 @@
 struct gsm_network *bsc_gsmnet = 0;
 static const char *database_name = "hlr.sqlite3";
 static const char *config_file = "openbsc.cfg";
-extern const char *openbsc_version;
-extern const char *openbsc_copyright;
 
 /* timer to store statistics */
 #define DB_SYNC_INTERVAL	60, 0
@@ -87,16 +85,6 @@
 	printf("  -e --log-level number. Set a global loglevel.\n");
 }
 
-static void print_version()
-{
-	printf("%s\n", openbsc_version);
-}
-
-static void print_copyright()
-{
-	puts(openbsc_copyright);
-}
-
 static void handle_options(int argc, char** argv)
 {
 	while (1) {
@@ -151,9 +139,7 @@
 			log_set_log_level(stderr_target, atoi(optarg));
 			break;
 		case 'V':
-			print_version();
-			printf("\n");
-			print_copyright();
+			print_version(1);
 			exit(0);
 			break;
 		default:
@@ -227,6 +213,11 @@
 	/* enable filters */
 	log_set_all_filter(stderr_target, 1);
 
+	/* This needs to precede handle_options() as it calls vty_init() */
+	rc = bsc_bootstrap_network(mncc_recv, config_file);
+	if (rc < 0)
+		exit(1);
+
 	/* parse options */
 	handle_options(argc, argv);
 
@@ -250,10 +241,6 @@
 	db_sync_timer.data = NULL;
 	bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
 
-	rc = bsc_bootstrap_network(mncc_recv, config_file);
-	if (rc < 0)
-		exit(1);
-
 	signal(SIGINT, &signal_handler);
 	signal(SIGABRT, &signal_handler);
 	signal(SIGUSR1, &signal_handler);
diff --git a/openbsc/src/bsc_version.c b/openbsc/src/bsc_version.c
index 0266194..432e63b 100644
--- a/openbsc/src/bsc_version.c
+++ b/openbsc/src/bsc_version.c
@@ -20,7 +20,6 @@
 
 #include "bscconfig.h"
 
-const char *openbsc_version = "OpenBSC " PACKAGE_VERSION;
 const char *openbsc_copyright =
 	"Copyright (C) 2008-2010 Harald Welte, Holger Freyther\n"
 	"Contributions by Daniel Willmann, Jan Lübbe,Stefan Schmidt\n"
diff --git a/openbsc/src/gprs/gb_proxy_main.c b/openbsc/src/gprs/gb_proxy_main.c
index de83186..9cd73e1 100644
--- a/openbsc/src/gprs/gb_proxy_main.c
+++ b/openbsc/src/gprs/gb_proxy_main.c
@@ -46,6 +46,8 @@
 #include <openbsc/vty.h>
 #include <openbsc/gb_proxy.h>
 
+#include <vty/command.h>
+
 #include "../../bscconfig.h"
 
 /* this is here for the vty... it will never be called */
@@ -56,11 +58,8 @@
 
 void *tall_bsc_ctx;
 
-const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION;
 const char *openbsc_copyright =
 	"Copyright (C) 2010 Harald Welte and On-Waves\n"
-	"Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\n"
-	"Dieter Spaar, Andreas Eversberg, Holger Freyther\n\n"
 	"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
 	"This is free software: you are free to change and redistribute it.\n"
 	"There is NO WARRANTY, to the extent permitted by law.\n";
@@ -175,9 +174,7 @@
 			log_set_log_level(stderr_target, atoi(optarg));
 			break;
 		case 'V':
-			printf("%s\n", openbsc_version);
-			printf("\n");
-			puts(openbsc_copyright);
+			print_version(1);
 			exit(0);
 			break;
 		default:
@@ -208,10 +205,11 @@
 	log_add_target(stderr_target);
 	log_set_all_filter(stderr_target, 1);
 
+	telnet_init(&dummy_network, 4246);
+
 	handle_options(argc, argv);
 
 	rate_ctr_init(tall_bsc_ctx);
-	telnet_init(&dummy_network, 4246);
 
 	bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb);
 	if (!bssgp_nsi) {
@@ -254,7 +252,7 @@
 int bsc_vty_init(struct gsm_network *dummy)
 {
 	cmd_init(1);
-	vty_init();
+	vty_init("Osmocom Gb Proxy", PACKAGE_VERSION, openbsc_copyright);
 
 	openbsc_vty_add_cmds();
         gbproxy_vty_init();
diff --git a/openbsc/src/gprs/sgsn_main.c b/openbsc/src/gprs/sgsn_main.c
index b355fb5..81b130b 100644
--- a/openbsc/src/gprs/sgsn_main.c
+++ b/openbsc/src/gprs/sgsn_main.c
@@ -58,11 +58,8 @@
 
 struct gprs_ns_inst *sgsn_nsi;
 
-const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION;
 const char *openbsc_copyright =
 	"Copyright (C) 2010 Harald Welte and On-Waves\n"
-	"Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\n"
-	"Dieter Spaar, Andreas Eversberg, Holger Freyther\n\n"
 	"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
 	"This is free software: you are free to change and redistribute it.\n"
 	"There is NO WARRANTY, to the extent permitted by law.\n";
@@ -174,7 +171,7 @@
 int bsc_vty_init(struct gsm_network *dummy)
 {
 	cmd_init(1);
-	vty_init();
+	vty_init("Osmocom SGSN", PACKAGE_VERSION, openbsc_copyright);
 
 	openbsc_vty_add_cmds();
         sgsn_vty_init();
diff --git a/openbsc/src/mgcp/mgcp_main.c b/openbsc/src/mgcp/mgcp_main.c
index f7f1f80..fd03c99 100644
--- a/openbsc/src/mgcp/mgcp_main.c
+++ b/openbsc/src/mgcp/mgcp_main.c
@@ -58,7 +58,6 @@
 static struct mgcp_config *cfg;
 static int reset_endpoints = 0;
 
-const char *openbsc_version = "OpenBSC MGCP " PACKAGE_VERSION;
 const char *openbsc_copyright =
 	"Copyright (C) 2009-2010 Holger Freyther and On-Waves\n"
 	"Contributions by Daniel Willmann, Jan Lübbe,Stefan Schmidt\n"
@@ -79,12 +78,6 @@
 	printf(" -c --config-file filename The config file to use.\n");
 }
 
-static void print_mgcp_version()
-{
-	printf("%s\n\n", openbsc_version);
-	printf("%s", openbsc_copyright);
-}
-
 static void handle_options(int argc, char** argv)
 {
 	while (1) {
@@ -110,7 +103,7 @@
 			config_file = talloc_strdup(tall_bsc_ctx, optarg);
 			break;
 		case 'V':
-			print_mgcp_version();
+			print_version(1);
 			exit(0);
 			break;
 		default:
@@ -200,9 +193,10 @@
 	if (!cfg)
 		return -1;
 
+	telnet_init(&dummy_network, 4243);
+
 	handle_options(argc, argv);
 
-	telnet_init(&dummy_network, 4243);
         rc = mgcp_parse_config(config_file, cfg);
 	if (rc < 0)
 		return rc;
@@ -264,7 +258,7 @@
 int bsc_vty_init(struct gsm_network *dummy)
 {
 	cmd_init(1);
-	vty_init();
+	vty_init("OpenBSC MGCP", PACKAGE_VERSION, openbsc_copyright);
 
 	openbsc_vty_add_cmds();
         mgcp_vty_init();
diff --git a/openbsc/src/telnet_interface.c b/openbsc/src/telnet_interface.c
index c7de026..b86afef 100644
--- a/openbsc/src/telnet_interface.c
+++ b/openbsc/src/telnet_interface.c
@@ -99,7 +99,6 @@
 }
 
 extern const char *openbsc_copyright;
-extern const char *openbsc_version;
 
 static void print_welcome(int fd) {
 	int ret;
diff --git a/openbsc/src/vty/cardshell.h b/openbsc/src/vty/cardshell.h
deleted file mode 100644
index 85164d2..0000000
--- a/openbsc/src/vty/cardshell.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#include "../../bscconfig.h"
-#define QUAGGA_PROGNAME	PACKAGE_NAME
-#define QUAGGA_VERSION	PACKAGE_VERSION
-#define QUAGGA_COPYRIGHT "Harald Welte <laforge@gnumonks.org>"
-#define CONFIGFILE_MASK 022
-#define SYSCONFDIR "/usr/local/etc"
diff --git a/openbsc/src/vty/command.c b/openbsc/src/vty/command.c
index f45ecdd..5be0092 100644
--- a/openbsc/src/vty/command.c
+++ b/openbsc/src/vty/command.c
@@ -21,8 +21,6 @@
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
-#include "cardshell.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -44,6 +42,8 @@
 #include <openbsc/gsm_subscriber.h>
 #include <osmocore/talloc.h>
 
+#define CONFIGFILE_MASK 022
+
 void *tall_vty_cmd_ctx;
 
 /* Command vector which includes some level of command lists. Normally
@@ -81,16 +81,14 @@
 };
 
 /* Default motd string. */
-const char *default_motd = "\r\n\
-Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
-" QUAGGA_COPYRIGHT "\r\n\
-\r\n";
+const char *default_motd = "";
 
 /* This is called from main when a daemon is invoked with -v or --version. */
-void print_version(const char *progname)
+void print_version(int print_copyright)
 {
-	printf("%s version %s\n", progname, QUAGGA_VERSION);
-	printf("%s\n", QUAGGA_COPYRIGHT);
+	printf("%s version %s\n", host.prog_name, host.prog_version);
+	if (print_copyright)
+		printf("\n%s\n", host.prog_copyright);
 }
 
 /* Utility function to concatenate argv argument into a single string
@@ -2199,9 +2197,9 @@
 DEFUN(show_version,
       show_version_cmd, "show version", SHOW_STR "Displays program version\n")
 {
-	vty_out(vty, "%s %s (%s).%s", QUAGGA_PROGNAME, QUAGGA_VERSION,
+	vty_out(vty, "%s %s (%s).%s", host.prog_name, host.prog_version,
 		host.name ? host.name : "", VTY_NEWLINE);
-	vty_out(vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
+	vty_out(vty, "%s%s", host.prog_copyright, VTY_NEWLINE);
 
 	return CMD_SUCCESS;
 }
@@ -2294,7 +2292,8 @@
 	file_vty->type = VTY_FILE;
 
 	/* Config file header print. */
-	vty_out(file_vty, "!\n! OpenBSC configuration saved from vty\n!   ");
+	vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
+		host.prog_name, host.prog_version);
 	//vty_time_print (file_vty, 1);
 	vty_out(file_vty, "!\n");
 
diff --git a/openbsc/src/vty/vty.c b/openbsc/src/vty/vty.c
index 7a6ad97..e5b5b48 100644
--- a/openbsc/src/vty/vty.c
+++ b/openbsc/src/vty/vty.c
@@ -13,12 +13,13 @@
 
 #include <arpa/telnet.h>
 
-#include "cardshell.h"
 #include <vty/vty.h>
 #include <vty/command.h>
 #include <vty/buffer.h>
 #include <osmocore/talloc.h>
 
+#define SYSCONFDIR "/usr/local/etc"
+
 /* our callback, located in telnet_interface.c */
 void vty_event(enum event event, int sock, struct vty *vty);
 
@@ -468,6 +469,7 @@
 	vty->hp = vty->hindex;
 }
 
+#define TELNET_OPTION_DEBUG
 /* Get telnet window size. */
 static int
 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
@@ -1631,8 +1633,12 @@
 
 extern void *tall_bsc_ctx;
 /* Install vty's own commands like `who' command. */
-void vty_init()
+void vty_init(const char *name, const char *version, const char *copyright)
 {
+	host.prog_name = name;
+	host.prog_version = version;
+	host.prog_copyright = copyright;
+
 	tall_vty_ctx = talloc_named_const(NULL, 0, "vty");
 	tall_vty_vec_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_vector");
 	tall_vty_cmd_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_command");
diff --git a/openbsc/src/vty_interface.c b/openbsc/src/vty_interface.c
index c05847a..eaa0eac 100644
--- a/openbsc/src/vty_interface.c
+++ b/openbsc/src/vty_interface.c
@@ -41,6 +41,8 @@
 #include <openbsc/vty.h>
 #include <openbsc/gprs_ns.h>
 
+#include "../bscconfig.h"
+
 static struct gsm_network *gsmnet;
 
 /* FIXME: this should go to some common file */
@@ -1942,13 +1944,14 @@
 }
 
 extern int bsc_vty_init_extra(struct gsm_network *net);
+extern const char *openbsc_copyright;
 
 int bsc_vty_init(struct gsm_network *net)
 {
 	gsmnet = net;
 
 	cmd_init(1);
-	vty_init();
+	vty_init("OpenBSC", PACKAGE_VERSION, openbsc_copyright);
 
 	install_element_ve(&show_net_cmd);
 	install_element_ve(&show_bts_cmd);