Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for command line parameters and reading commands from config file. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 142 additions & 26 deletions obecli.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
*
*****************************************************************************/

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <getopt.h>
#include <unistd.h>

#include "config.h"

#include <signal.h>
#define _GNU_SOURCE

#include <readline/readline.h>
#include <readline/history.h>
Expand All @@ -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;
Expand Down Expand Up @@ -1465,7 +1470,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;
Expand All @@ -1481,19 +1486,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 )
Expand All @@ -1518,17 +1510,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;
}
}
}

Expand All @@ -1539,3 +1520,138 @@ int main( int argc, char **argv )

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: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 }
};

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 <file> | Read commands from <file>.\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(" -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 )
{
printf( "\n" );
printf( "Open Broadcast Encoder command line interface.\n" );
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;
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;
}
}

cli.h = obe_setup();
if( !cli.h )
{
fprintf( stderr, "obe_setup failed\n" );
return -1;
}

cli.avc_profile = -1;

int ret = 0;
if ( cli.config_file )
ret = run_with_config();

if ( argc < 2 || cli.run_shell )
ret = run_cmd_prompt();

return ret;
}