From 63ee1afca501224efd492604c4304551b89f3772 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 6 Apr 2015 14:14:29 +0200 Subject: cli mode reads port from config if config is given, otherwise defaults to DEFAULT_HTTP_PORT --- src/biruda.c | 40 +++++++++++++++++++++++++--------------- src/biruda.conf | 2 +- src/cli.c | 6 +++--- src/cli.h | 2 +- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/biruda.c b/src/biruda.c index 0507c1c..7f02847 100644 --- a/src/biruda.c +++ b/src/biruda.c @@ -334,28 +334,30 @@ static void terminate_foreground_func( int sig ) int main( int argc, char *argv[] ) { struct gengetopt_args_info args_info; - cfg_t *cfg; + cfg_t *cfg = NULL; if( parse_options_and_arguments( argc, argv, &args_info ) ) { exit( EXIT_FAILURE ); } - if( read_config( args_info.config_file_given ? - args_info.config_file_arg : DEFAULT_CONFIG_FILE, &cfg ) ) { - cfg_free( cfg ); - cmdline_parser_free( &args_info ); - return 1; + if( !args_info.cli_given || args_info.config_file_given ) { + if( read_config( args_info.config_file_given ? + args_info.config_file_arg : DEFAULT_CONFIG_FILE, &cfg ) ) { + if( cfg ) cfg_free( cfg ); + cmdline_parser_free( &args_info ); + return 1; + } } if( args_info.test_given ) { - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE ); } if( args_info.print_given ) { print_config( &args_info, cfg ); - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE ); } @@ -366,14 +368,22 @@ int main( int argc, char *argv[] ) in = fopen( args_info.filename_arg, "r" ); if( in == NULL ) { fprintf( stderr, "ERROR: Unable to open command file '%s'\n", args_info.filename_arg ); - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( EXIT_FAILURE ); } } int ret; #ifndef _WIN32 - ret = start_interactive( ( in == stdin ) ? !args_info.no_colors_given : false, in ); + unsigned int port = DEFAULT_WEBSERVER_PORT; + if( args_info.config_file_given ) { + cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 ); + unsigned int has_webserver = cfg_size( cfg, "webserver" ); + if( has_webserver > 0 ) { + port = cfg_getint( webserver_cfg, "port" ); + } + } + ret = start_interactive( ( in == stdin ) ? !args_info.no_colors_given : false, port, in ); #else if( args_info.filename_given ) { ret = start_interactive( false, in ); @@ -382,7 +392,7 @@ int main( int argc, char *argv[] ) ret = -1; } #endif - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( ( ret == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE ); } @@ -391,7 +401,7 @@ int main( int argc, char *argv[] ) if( has_master ) { if( create_master( cfg ) != 0 ) { fprintf( stderr, "FATAL: Unable to create master thread!\n" ); - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( EXIT_FAILURE ); } @@ -401,7 +411,7 @@ int main( int argc, char *argv[] ) if( has_coordinator ) { if( create_coordinator( cfg ) != 0 ) { fprintf( stderr, "FATAL: Unable to create coordinator thread!\n" ); - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( EXIT_FAILURE ); } @@ -412,7 +422,7 @@ int main( int argc, char *argv[] ) #ifndef _WIN32 if( create_webserver( cfg ) != 0 ) { fprintf( stderr, "FATAL: Unable to create webserver thread!\n" ); - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( EXIT_FAILURE ); } @@ -464,7 +474,7 @@ int main( int argc, char *argv[] ) } #endif - cfg_free( cfg ); + if( cfg ) cfg_free( cfg ); cmdline_parser_free( &args_info ); exit( EXIT_SUCCESS ); diff --git a/src/biruda.conf b/src/biruda.conf index d7fae19..bf92b40 100644 --- a/src/biruda.conf +++ b/src/biruda.conf @@ -53,5 +53,5 @@ webserver { threads = 4 host = localhost - port = 8080 + port = 8081 } diff --git a/src/cli.c b/src/cli.c index 0596b24..f882c3b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -255,13 +255,13 @@ static void stop_worker( const char *worker_name ) } } -int start_interactive( bool colors, FILE *in ) +int start_interactive( bool colors, unsigned short port, FILE *in ) { char history_filename[1024]; // for http_tidy, tell it where to issue requests to - http_server = "localhost"; - http_port = 8080; + http_server = "localhost"; // server is hard-coded + http_port = port; // port not is_interactive = isatty( fileno( in ) ); print_colors = is_interactive ? colors : false; diff --git a/src/cli.h b/src/cli.h index c80fab6..4086686 100644 --- a/src/cli.h +++ b/src/cli.h @@ -5,6 +5,6 @@ #include -int start_interactive( bool colors, FILE *in ); +int start_interactive( bool colors, unsigned short port, FILE *in ); #endif -- cgit v1.2.3-54-g00ecf