summaryrefslogtreecommitdiff
path: root/src/biruda.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-11-14 22:13:48 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2014-11-14 22:13:48 +0100
commit7ca7fa71346f64494010ea6ac8cc5e71fb1ad343 (patch)
tree55ffeed30db507607c7d438f8e36891f21cf51a0 /src/biruda.c
parentcda6f82b0e0035ee1176cdb5b6f2f797f897e8f0 (diff)
downloadbiruda-7ca7fa71346f64494010ea6ac8cc5e71fb1ad343.tar.gz
biruda-7ca7fa71346f64494010ea6ac8cc5e71fb1ad343.tar.bz2
started cli with linenoise
Diffstat (limited to 'src/biruda.c')
-rw-r--r--src/biruda.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/biruda.c b/src/biruda.c
index ccd17f1..6bbcafe 100644
--- a/src/biruda.c
+++ b/src/biruda.c
@@ -10,8 +10,14 @@
#include <signal.h>
#endif
+// command line parser
#include "biruda_cmdline.h"
+
+// configuration parser
+
#include "confuse.h"
+
+// daemonizing
#ifndef _WIN32
#include "libdaemon/daemon.h"
#endif
@@ -19,10 +25,19 @@
#include "master.h"
#include "worker.h"
#include "coordinator.h"
+
+// for integrated webserver
#ifndef _WIN32
#include "webserver.h"
#endif
+// for cli/interactive biruda client
+#ifndef _WIN32
+#include <strings.h>
+#include <ctype.h>
+#include "linenoise.h"
+#endif
+
#include "port.h"
static const char *DEFAULT_CONFIG_FILE = "/etc/biruda/biruda.conf";
@@ -198,6 +213,81 @@ static void terminate_foreground_func( int sig )
}
#endif
+#ifndef _WIN32
+
+#define HISTORY_FILE ".biruda.history"
+
+static char *commands[] = {
+ "help", "quit", NULL
+};
+
+static void completion_func( const char *buf, linenoiseCompletions *lc )
+{
+ unsigned int i;
+ size_t len = strlen( buf );
+
+ for (i = 0; commands[i]; ++i) {
+ char *cmd = commands[i];
+ if( strlen( cmd ) < len ) continue;
+ if( strncasecmp( buf, cmd, len ) == 0 ) {
+ linenoiseAddCompletion( lc, cmd );
+ }
+ }
+}
+
+static void print_help( )
+{
+ puts( "\n"
+ " help - show this help page\n"
+ " quit - quit the client\n"
+ );
+}
+
+static int start_interactive( )
+{
+ char history_filename[1024];
+
+ char *home = getenv( "HOME" );
+ if( home != NULL ) {
+ snprintf( history_filename, sizeof( history_filename ), "%s/%s", home, HISTORY_FILE );
+ FILE *history_file = fopen( history_filename, "w+" );
+ if( history_file != NULL ) {
+ fclose( history_file );
+ linenoiseHistoryLoad( HISTORY_FILE );
+ linenoiseSetCompletionCallback( completion_func );
+ }
+ }
+
+ char *context = "biruda";
+ for( ;; ) {
+ char buf[4096];
+ char *line;
+
+ snprintf( buf, sizeof( buf ), "%s> ", context );
+ if( ( line = linenoise( buf ) ) == NULL ) {
+ break;
+ }
+
+ if( strlen( line ) > 1 ) {
+ if( line[strlen( line )-1] == '\n' ) {
+ line[strlen( line )-1] = '\0';
+ }
+ }
+
+ linenoiseHistoryAdd( line );
+
+ if( strncasecmp( line, "quit", 4 ) == 0 ) {
+ linenoiseHistorySave( history_filename );
+ return EXIT_SUCCESS;
+ } else if( strncasecmp( line, "help", 4 ) == 0 ){
+ print_help( );
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
+#endif
+
int main( int argc, char *argv[] )
{
struct gengetopt_args_info args_info;
@@ -227,6 +317,10 @@ int main( int argc, char *argv[] )
return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
}
+ if( args_info.cli_given ) {
+ return start_interactive( );
+ }
+
unsigned int has_master = cfg_size( cfg, "master" );
if( has_master ) {
if( create_master( cfg ) != 0 ) {