From 1353f961ef285c23776199419989f5b5039565a1 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 16 May 2010 19:20:24 +0200 Subject: [PATCH] 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. --- openbsc/include/vty/command.h | 7 ++++++- openbsc/include/vty/vty.h | 2 +- openbsc/src/Makefile.am | 1 - openbsc/src/bsc_hack.c | 25 ++++++------------------- openbsc/src/bsc_version.c | 1 - openbsc/src/gprs/gb_proxy_main.c | 14 ++++++-------- openbsc/src/gprs/sgsn_main.c | 5 +---- openbsc/src/mgcp/mgcp_main.c | 14 ++++---------- openbsc/src/telnet_interface.c | 1 - openbsc/src/vty/cardshell.h | 6 ------ openbsc/src/vty/command.c | 23 +++++++++++------------ openbsc/src/vty/vty.c | 10 ++++++++-- openbsc/src/vty_interface.c | 5 ++++- 13 files changed, 47 insertions(+), 67 deletions(-) delete mode 100644 openbsc/src/vty/cardshell.h diff --git a/openbsc/include/vty/command.h b/openbsc/include/vty/command.h index 38d90b460..ec594ed1b 100644 --- a/openbsc/include/vty/command.h +++ b/openbsc/include/vty/command.h @@ -57,6 +57,10 @@ struct host { /* 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 @@ extern struct cmd_element config_list_cmd; 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 0441fc5f7..23430d5e7 100644 --- a/openbsc/include/vty/vty.h +++ b/openbsc/include/vty/vty.h @@ -128,7 +128,7 @@ static inline char *vty_newline(struct vty *vty) } /* 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 177c410ad..6c89bd3fc 100644 --- a/openbsc/src/Makefile.am +++ b/openbsc/src/Makefile.am @@ -7,7 +7,6 @@ SUBDIRS = . ipaccess gprs 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 a50d4ab0b..74351229f 100644 --- a/openbsc/src/bsc_hack.c +++ b/openbsc/src/bsc_hack.c @@ -42,8 +42,6 @@ static struct log_target *stderr_target; 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 @@ static void print_help() 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 @@ static void handle_options(int argc, char** argv) 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 @@ int main(int argc, char **argv) /* 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 @@ int main(int argc, char **argv) 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 02661940e..432e63b6d 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 de83186f3..9cd73e17d 100644 --- a/openbsc/src/gprs/gb_proxy_main.c +++ b/openbsc/src/gprs/gb_proxy_main.c @@ -46,6 +46,8 @@ #include #include +#include + #include "../../bscconfig.h" /* this is here for the vty... it will never be called */ @@ -56,11 +58,8 @@ void subscr_put() { abort(); } 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 \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 @@ static void handle_options(int argc, char **argv) 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 @@ int main(int argc, char **argv) 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 @@ struct gsm_network; 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 b355fb58d..81b130b12 100644 --- a/openbsc/src/gprs/sgsn_main.c +++ b/openbsc/src/gprs/sgsn_main.c @@ -58,11 +58,8 @@ void *tall_bsc_ctx; 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 \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 @@ struct gsm_network; 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 f7f1f80fc..fd03c9943 100644 --- a/openbsc/src/mgcp/mgcp_main.c +++ b/openbsc/src/mgcp/mgcp_main.c @@ -58,7 +58,6 @@ static struct bsc_fd bfd; 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 @@ static void print_help() 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 @@ static void handle_options(int argc, char** argv) 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 @@ int main(int argc, char** argv) 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 @@ struct gsm_network; 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 c7de026d1..b86afefc2 100644 --- a/openbsc/src/telnet_interface.c +++ b/openbsc/src/telnet_interface.c @@ -99,7 +99,6 @@ void telnet_init(struct gsm_network *network, int port) { } 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 85164d2bd..000000000 --- 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 " -#define CONFIGFILE_MASK 022 -#define SYSCONFDIR "/usr/local/etc" diff --git a/openbsc/src/vty/command.c b/openbsc/src/vty/command.c index f45ecdd73..5be0092a6 100644 --- a/openbsc/src/vty/command.c +++ b/openbsc/src/vty/command.c @@ -21,8 +21,6 @@ along with GNU Zebra; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "cardshell.h" - #include #include #include @@ -44,6 +42,8 @@ Boston, MA 02111-1307, USA. */ #include #include +#define CONFIGFILE_MASK 022 + void *tall_vty_cmd_ctx; /* Command vector which includes some level of command lists. Normally @@ -81,16 +81,14 @@ struct cmd_node config_node = { }; /* 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 @@ gDEFUN(config_exit, 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 @@ DEFUN(config_write_file, 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 7a6ad977d..e5b5b48f0 100644 --- a/openbsc/src/vty/vty.c +++ b/openbsc/src/vty/vty.c @@ -13,12 +13,13 @@ #include -#include "cardshell.h" #include #include #include #include +#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 @@ static void vty_hist_add(struct vty *vty) 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 @@ void vty_init_vtysh() 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 c05847a5c..eaa0eac1d 100644 --- a/openbsc/src/vty_interface.c +++ b/openbsc/src/vty_interface.c @@ -41,6 +41,8 @@ #include #include +#include "../bscconfig.h" + static struct gsm_network *gsmnet; /* FIXME: this should go to some common file */ @@ -1942,13 +1944,14 @@ DEFUN(cfg_ts_e1_subslot, } 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);