summaryrefslogtreecommitdiff
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
parentcda6f82b0e0035ee1176cdb5b6f2f797f897e8f0 (diff)
downloadbiruda-7ca7fa71346f64494010ea6ac8cc5e71fb1ad343.tar.gz
biruda-7ca7fa71346f64494010ea6ac8cc5e71fb1ad343.tar.bz2
started cli with linenoise
-rw-r--r--README16
-rw-r--r--TODOS10
-rw-r--r--src/GNUmakefile3
-rw-r--r--src/biruda.c94
-rw-r--r--src/biruda.conf7
-rw-r--r--src/biruda.ggo4
6 files changed, 118 insertions, 16 deletions
diff --git a/README b/README
index 4b02c16..c87887a 100644
--- a/README
+++ b/README
@@ -16,22 +16,28 @@ Requirements
------------
* biruda is written in plain C and uses libraries written in plain C.
- You need a C compiler and GNU make.
-* gengetopt: for command argument line parsing
+ You need a C compiler (gcc, clang, tcc, MSVC) and GNU make (resp.
+ NMAKE on Windows).
+* gengetopt: for command argument line parsing (on Windows you need the
+ Cygwin version of gengetopt)
http://www.gnu.org/software/gengetopt/gengetopt.html
* confuse: configuration library
http://www.nongnu.org/confuse/
* nanomsg: for communication between nodes (on the same machine or on several machines)
http://nanomsg.org/
-* libmicrohttpd: for an embedded web interface
+* libmicrohttpd: for an embedded web interface (not on Windows, there is
+ no master webserver on Windows currently)
http://www.gnu.org/software/libmicrohttpd/
-* pthreads: for threading support
+* pthreads: for threading support on Windows
http://sourceware.org/pthreads-win32/
* json-c: JSON for message payload
https://github.com/json-c/json-c/wiki
+* glib2: for direct execution of children in the worker
+ http://www.gtk.org/
* libdaemon: for daemonizing support on Unix
+* linenoise: lightweight editline/readline replacement for the baruda client 'bcli'
+ (integrated)
-
Other projects
--------------
diff --git a/TODOS b/TODOS
index a38bc85..b70ccc5 100644
--- a/TODOS
+++ b/TODOS
@@ -1,17 +1,7 @@
- find ways to easy control subprocesses
- - http://libpipeline.nongnu.org/ (from man-db)
- glibc process
- Poco
- links
http://blog.borovsak.si/2009/07/spawning-processes-using-glib.html
-http://stackoverflow.com/questions/4586405/get-number-of-cpus-in-linux-using-c
-https://lkml.org/lkml/2012/9/5/419
http://www.codeguru.com/cpp/misc/misc/system/article.php/c8973/Determine-Windows-Version-and-Edition.htm
-http://xmodulo.com/alternatives-skype-linux.html
-
--which embedded web server
-microhttpd (GNU) or swill
-
-- platform/compiler macros
-http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
diff --git a/src/GNUmakefile b/src/GNUmakefile
index 5e531c8..ceb26f3 100644
--- a/src/GNUmakefile
+++ b/src/GNUmakefile
@@ -10,7 +10,7 @@ LIBS = -lconfuse -lpthread -lnanomsg -ljson-c -lmicrohttpd
%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
-biruda: biruda.o biruda_cmdline.o master.o coordinator.o worker.o system.o webserver.o
+biruda: biruda.o biruda_cmdline.o master.o coordinator.o worker.o system.o webserver.o linenoise.o
$(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
biruda_cmdline.o: biruda_cmdline.c
@@ -20,6 +20,7 @@ coordinator.o: coordinator.c coordinator.h port.h system.h
worker.o: worker.c worker.h port.h
system.o: system.c system.h
webserver.o: webserver.c webserver.h
+linenoise.o: linenoise.c linenoise.h
biruda_cmdline.c: biruda.ggo
gengetopt -F biruda_cmdline --unamed-opts --conf-parser --include-getopt -i $<
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 ) {
diff --git a/src/biruda.conf b/src/biruda.conf
index df78a0f..51da929 100644
--- a/src/biruda.conf
+++ b/src/biruda.conf
@@ -27,3 +27,10 @@ worker worker2
# control = "inproc://control"
control = "tcp://localhost:5555"
}
+
+webserver
+{
+ threads = 4
+ host = localhost
+ port = 8080
+}
diff --git a/src/biruda.ggo b/src/biruda.ggo
index 7d4c215..b6758fd 100644
--- a/src/biruda.ggo
+++ b/src/biruda.ggo
@@ -20,6 +20,10 @@ section "Main Options"
option "verbose" v
"increase verbosity (can be given multiple times)"
optional multiple
+
+ option "cli" i
+ "start in command line interface (CLI), interactive mode"
+ optional
section "Unix Daemon"