From 34717433ceaf64ec7dd2c2102aff4811f15b3713 Mon Sep 17 00:00:00 2001 From: Georgi Chorbadzhiyski Date: Fri, 21 Nov 2014 13:45:55 +0200 Subject: [PATCH 1/3] Refactor main() in preparation for adding support for configuration file. --- obecli.c | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/obecli.c b/obecli.c index 0752a77e..93ec2df3 100644 --- a/obecli.c +++ b/obecli.c @@ -1465,7 +1465,7 @@ static int parse_command( char *command, obecli_command_t *commmand_list ) return 0; } -int main( int argc, char **argv ) +static int run_cmd_prompt( void ) { char *home_dir = getenv( "HOME" ); char *history_filename; @@ -1481,19 +1481,6 @@ int main( int argc, char **argv ) sprintf( history_filename, "%s/.obecli_history", home_dir ); read_history( history_filename ); - cli.h = obe_setup(); - if( !cli.h ) - { - fprintf( stderr, "obe_setup failed\n" ); - return -1; - } - - cli.avc_profile = -1; - - printf( "\nOpen Broadcast Encoder command line interface.\n" ); - printf( "Version 1.0 \n" ); - printf( "\n" ); - while( 1 ) { if( line_read ) @@ -1518,17 +1505,6 @@ int main( int argc, char **argv ) int ret = parse_command( line_read, main_commands ); if( ret == -1 ) fprintf( stderr, "%s: command not found \n", line_read ); - - if( !cli.h ) - { - cli.h = obe_setup(); - if( !cli.h ) - { - fprintf( stderr, "obe_setup failed\n" ); - return -1; - } - cli.avc_profile = -1; - } } } @@ -1539,3 +1515,22 @@ int main( int argc, char **argv ) return 0; } + +int main( int argc, char **argv ) +{ + printf( "\n" ); + printf( "Open Broadcast Encoder command line interface.\n" ); + printf( "Version 1.0\n" ); + printf( "\n" ); + + cli.h = obe_setup(); + if( !cli.h ) + { + fprintf( stderr, "obe_setup failed\n" ); + return -1; + } + + cli.avc_profile = -1; + + return run_cmd_prompt(); +} From f9a1706c77400edc7cb871298e6772362188af8f Mon Sep 17 00:00:00 2001 From: Georgi Chorbadzhiyski Date: Fri, 21 Nov 2014 15:11:37 +0200 Subject: [PATCH 2/3] Add support for command line parameters and reading config file. This patch adds support for running OBE like any other program that needs configuration. Save the commands that you usually type into OBE shell in regular text file and then run 'obecli --config-file=my.conf' and you are good to go. NOTE: There is possible change in behaviour! After this patch the interactive shell is run only if there are no command line parameters after 'obecli' or '-s'/'--shell' parameter is used. --- obecli.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/obecli.c b/obecli.c index 93ec2df3..1cdc4380 100644 --- a/obecli.c +++ b/obecli.c @@ -22,15 +22,18 @@ * *****************************************************************************/ +#define _GNU_SOURCE + #include #include #include #include +#include +#include #include "config.h" #include -#define _GNU_SOURCE #include #include @@ -51,6 +54,8 @@ typedef struct obe_mux_opts_t mux_opts; obe_output_opts_t output; int avc_profile; + int run_shell; + char *config_file; } obecli_ctx_t; obecli_ctx_t cli; @@ -1516,6 +1521,81 @@ static int run_cmd_prompt( void ) return 0; } +static int keep_running = 1; + +void signal_quit(int sig) { + if (!keep_running) + raise(sig); + keep_running = 0; +} + +int run_with_config( void ) +{ + FILE *fp = fopen( cli.config_file, "r" ); + if ( !fp ) { + fprintf( stderr, "ERROR: Can't open config file '%s': %s\n", cli.config_file, strerror( errno ) ); + return -1; + } + + size_t len = 0; + ssize_t read, i; + while ( ( read = getline(&line_read, &len, fp) ) != -1 ) { + if ( !len ) + continue; + + // Trim line + for ( i = strlen( line_read ) - 1; i >= 0; i-- ) { + if ( line_read[i] == '\r' || line_read[i] == '\n' ) + line_read[i] = '\0'; + } + + // Skip comments and empty lines + if ( strlen(line_read) && line_read[0] != '#' ) { + printf("CMD: %s\n", line_read); + int ret = parse_command( line_read, main_commands ); + if ( ret == -1 ) + fprintf( stderr, "ERROR: '%s': command not found.\n", line_read ); + } + } + free( line_read ); + + if ( !cli.run_shell ) + { + signal(SIGINT , signal_quit); + signal(SIGTERM, signal_quit); + + while ( keep_running ) + sleep(3600); + + stop_encode( NULL, NULL ); + } + + return 0; +} + +static const char short_options[] = "c:sh"; + +static const struct option long_options[] = { + { "config-file", required_argument, NULL, 'c' }, + { "shell", no_argument, NULL, 's' }, + { "help", no_argument, NULL, 'h' }, + { 0, 0, 0, 0 } +}; + +static void show_cmdline_params( void ) { + // 10 20 30 40 50 60 70 80 + // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 + printf("Command line parameters:\n"); + printf("\n"); + printf(" -c --config-file | Read commands from .\n"); + printf(" -s --shell | Run interactive OBE shell. This is the default\n"); + printf(" . action when no command line parameters are used.\n"); + printf(" -h --help | Show command line parameters.\n"); + printf("\n"); +} + +extern char *optarg; + int main( int argc, char **argv ) { printf( "\n" ); @@ -1523,6 +1603,21 @@ int main( int argc, char **argv ) printf( "Version 1.0\n" ); printf( "\n" ); + int j; + while( ( j = getopt_long( argc, argv, short_options, long_options, NULL ) ) != -1 ) { + switch ( j ) { + case 'c': // --config-file + cli.config_file = optarg; + break; + case 's': // --shell + cli.run_shell = !cli.run_shell; + break; + case 'h': // --help + show_cmdline_params(); + return 0; + } + } + cli.h = obe_setup(); if( !cli.h ) { @@ -1532,5 +1627,12 @@ int main( int argc, char **argv ) cli.avc_profile = -1; - return run_cmd_prompt(); + int ret = 0; + if ( cli.config_file ) + ret = run_with_config(); + + if ( argc < 2 || cli.run_shell ) + ret = run_cmd_prompt(); + + return ret; } From 332e4c6d33aaa596daab90bafeee1de7f2ac7751 Mon Sep 17 00:00:00 2001 From: Georgi Chorbadzhiyski Date: Fri, 21 Nov 2014 15:51:07 +0200 Subject: [PATCH 3/3] Add -H / --full-help parameters that shows OBE internal help. --- obecli.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/obecli.c b/obecli.c index 1cdc4380..c852725e 100644 --- a/obecli.c +++ b/obecli.c @@ -1573,12 +1573,13 @@ int run_with_config( void ) return 0; } -static const char short_options[] = "c:sh"; +static const char short_options[] = "c:shH"; static const struct option long_options[] = { { "config-file", required_argument, NULL, 'c' }, { "shell", no_argument, NULL, 's' }, { "help", no_argument, NULL, 'h' }, + { "full-help", no_argument, NULL, 'H' }, { 0, 0, 0, 0 } }; @@ -1591,9 +1592,17 @@ static void show_cmdline_params( void ) { printf(" -s --shell | Run interactive OBE shell. This is the default\n"); printf(" . action when no command line parameters are used.\n"); printf(" -h --help | Show command line parameters.\n"); + printf(" -H --full-help | Show --help and show OBE configuration commands.\n"); printf("\n"); } +static void show_cmd_help( char *in_cmd ) +{ + char *cmd = strdup( in_cmd ); + parse_command( cmd, main_commands ); + free( cmd ); +} + extern char *optarg; int main( int argc, char **argv ) @@ -1615,6 +1624,16 @@ int main( int argc, char **argv ) case 'h': // --help show_cmdline_params(); return 0; + case 'H': // --full-help + show_cmdline_params(); + show_cmd_help( "help "); + for( int i = 0; show_commands[i].name != 0; i++ ) + { + char show_cmd[32]; + snprintf( show_cmd, sizeof(show_cmd), "show %s", show_commands[i].name ); + show_cmd_help( show_cmd ); + } + return 0; } }