Skip to content

Commit

Permalink
Merge branch 'development' of github.com:servalproject/serval-dna int…
Browse files Browse the repository at this point in the history
…o development
  • Loading branch information
gardners committed Oct 1, 2014
2 parents dce0feb + 4440cf3 commit c7fde5e
Show file tree
Hide file tree
Showing 60 changed files with 3,396 additions and 2,982 deletions.
28 changes: 16 additions & 12 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,26 @@ SERVALD_LOCAL_LDLIBS = -L$(SYSROOT)/usr/lib -llog

# Build libserval.so
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(NACL_SOURCES) $(SERVALD_SRC_FILES) version_servald.c
LOCAL_SRC_FILES := $(NACL_SOURCES) $(SERVALD_SRC_FILES) version_servald.c android.c
LOCAL_CFLAGS += $(SERVALD_LOCAL_CFLAGS)
LOCAL_LDLIBS := $(SERVALD_LOCAL_LDLIBS)
LOCAL_MODULE := serval
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= servalwrap.c
LOCAL_MODULE:= servald
include $(BUILD_EXECUTABLE)
ifdef SERVALD_WRAP
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= servalwrap.c
LOCAL_MODULE:= servald
include $(BUILD_EXECUTABLE)
endif

# Build servald for use with gdb
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= $(NACL_SOURCES) $(SERVALD_SRC_FILES) version_servald.c
LOCAL_CFLAGS += $(SERVALD_LOCAL_CFLAGS)
LOCAL_LDLIBS := $(SERVALD_LOCAL_LDLIBS)
LOCAL_STATIC_LIBRARIES := $(SERVALD_LOCAL_STATIC_LIBRARIES)
LOCAL_MODULE:= servaldsimple
include $(BUILD_EXECUTABLE)
ifdef SERVALD_SIMPLE
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= $(NACL_SOURCES) $(SERVALD_SRC_FILES) version_servald.c
LOCAL_CFLAGS += $(SERVALD_LOCAL_CFLAGS)
LOCAL_LDLIBS := $(SERVALD_LOCAL_LDLIBS)
LOCAL_STATIC_LIBRARIES := $(SERVALD_LOCAL_STATIC_LIBRARIES)
LOCAL_MODULE:= servaldsimple
include $(BUILD_EXECUTABLE)
endif
32 changes: 32 additions & 0 deletions android.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright (C) 2014 Serval Project Inc.
Android specific functions that we don't need on other platforms
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <stdio.h>
#include "log.h"
#include "conf.h"

// We don't want to call any at_exit functions from the dalvik VM
void _exit(int status);
void exit(int status)
{
if (config.debug.verbose)
DEBUGF("Calling _exit(%d)", status);
fflush(stdout);
_exit(status);
}
83 changes: 58 additions & 25 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,72 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "strbuf_helpers.h"
#include "dataformats.h"

int cli_usage(const struct cli_schema *commands, XPRINTF xpf)
int cli_usage(const struct cli_schema *commands, const struct cli_schema *end_commands, XPRINTF xpf)
{
return cli_usage_args(0, NULL, commands, xpf);
return cli_usage_args(0, NULL, commands, end_commands, xpf);
}

int cli_usage_parsed(const struct cli_parsed *parsed, XPRINTF xpf)
{
if (parsed->varargi == -1)
return cli_usage(parsed->commands, xpf);
return cli_usage_args(parsed->argc - parsed->varargi, &parsed->args[parsed->varargi], parsed->commands, xpf);
return cli_usage(parsed->commands, parsed->end_commands, xpf);
return cli_usage_args(parsed->argc - parsed->varargi, &parsed->args[parsed->varargi],
parsed->commands, parsed->end_commands, xpf);
}

int cli_usage_args(const int argc, const char *const *args, const struct cli_schema *commands, XPRINTF xpf)
static int cli_usage_print(const int argc, const char *const *args, const struct cli_schema *command, XPRINTF xpf)
{
int opt;
const char *word;
for (opt = 0; opt < argc && (word = command->words[opt]); ++opt)
if (strncmp(word, args[opt], strlen(args[opt])) != 0)
return 0;
for (opt = 0; (word = command->words[opt]); ++opt) {
if (word[0] == '|')
++word;
xprintf(xpf, " %s", word);
}
xputc('\n', xpf);
if (command->description && command->description[0])
xprintf(xpf, " %s\n", command->description);
return 1;
}

static int cmp_command(const void *one, const void *two)
{
const struct cli_schema *cone = *(const struct cli_schema **)one;
const struct cli_schema *ctwo = *(const struct cli_schema **)two;
unsigned i;
for (i=0; ; i++){
if (!cone->words[i] || !ctwo->words[i]){
if (cone->words[i])
return 1;
if (ctwo->words[i])
return -1;
return 0;
}
int r = strcmp(cone->words[i],ctwo->words[i]);
if (r)
return r;
}
}

int cli_usage_args(const int argc, const char *const *args, const struct cli_schema *commands, const struct cli_schema *end_commands, XPRINTF xpf)
{
unsigned count;
for (count=0; (!end_commands || &commands[count] < end_commands) && commands[count].function; ++count)
;

const struct cli_schema *cmds[count];
unsigned cmd;
int matched_any = 0;
for (cmd = 0; commands[cmd].function; ++cmd) {
int opt;
const char *word;
int matched = 1;
for (opt = 0; matched && opt < argc && (word = commands[cmd].words[opt]); ++opt)
if (strncmp(word, args[opt], strlen(args[opt])) != 0)
matched = 0;
if (matched) {
for (cmd = 0; cmd < count; cmd++)
cmds[cmd] = &commands[cmd];

qsort(cmds, count, sizeof(struct cli_schema *), cmp_command);
unsigned matched_any = 0;
for (cmd = 0; cmd < count; cmd++){
if (cli_usage_print(argc,args,cmds[cmd],xpf)==1)
matched_any = 1;
for (opt = 0; (word = commands[cmd].words[opt]); ++opt) {
if (word[0] == '|')
++word;
xprintf(xpf, " %s", word);
}
xputc('\n', xpf);
if (commands[cmd].description && commands[cmd].description[0])
xprintf(xpf, " %s\n", commands[cmd].description);
}
}
if (!matched_any && argc) {
strbuf b = strbuf_alloca(160);
Expand All @@ -87,15 +119,16 @@ int cli_usage_args(const int argc, const char *const *args, const struct cli_sch
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int cli_parse(const int argc, const char *const *args, const struct cli_schema *commands, struct cli_parsed *parsed)
int cli_parse(const int argc, const char *const *args, const struct cli_schema *commands, const struct cli_schema *end_commands, struct cli_parsed *parsed)
{
int ambiguous = 0;
int matched_cmd = -1;
int cmd;
for (cmd = 0; commands[cmd].function; ++cmd) {
for (cmd = 0; (!end_commands || &commands[cmd] < end_commands) && commands[cmd].function; ++cmd) {
struct cli_parsed cmdpa;
memset(&cmdpa, 0, sizeof cmdpa);
cmdpa.commands = commands;
cmdpa.end_commands = end_commands;
cmdpa.cmdi = cmd;
cmdpa.args = args;
cmdpa.argc = argc;
Expand Down
26 changes: 11 additions & 15 deletions cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
#include <stdint.h>
#ifdef HAVE_JNI_H
#include <jni.h>

// Stop OpenJDK 7 from foisting their UNUSED() macro on us in <jni_md.h>
#ifdef UNUSED
# undef UNUSED
#endif

#endif
#include "xprintf.h"
#include "log.h"

#define COMMAND_LINE_MAX_LABELS (32)
#define COMMAND_LINE_MAX_LABELS (16)

struct cli_parsed;
struct cli_context{
Expand All @@ -52,6 +58,7 @@ struct cli_schema {

struct cli_parsed {
const struct cli_schema *commands;
const struct cli_schema *end_commands;
unsigned int cmdi;
struct labelv {
const char *label;
Expand All @@ -68,10 +75,10 @@ void _debug_cli_parsed(struct __sourceloc __whence, const struct cli_parsed *par

#define DEBUG_cli_parsed(parsed) _debug_cli_parsed(__WHENCE__, parsed)

int cli_usage(const struct cli_schema *commands, XPRINTF xpf);
int cli_usage_args(const int argc, const char *const *args, const struct cli_schema *commands, XPRINTF xpf);
int cli_usage(const struct cli_schema *commands, const struct cli_schema *end_commands, XPRINTF xpf);
int cli_usage_args(const int argc, const char *const *args, const struct cli_schema *commands, const struct cli_schema *end_commands, XPRINTF xpf);
int cli_usage_parsed(const struct cli_parsed *parsed, XPRINTF xpf);
int cli_parse(const int argc, const char *const *args, const struct cli_schema *commands, struct cli_parsed *parsed);
int cli_parse(const int argc, const char *const *args, const struct cli_schema *commands, const struct cli_schema *end_commands, struct cli_parsed *parsed);
int cli_invoke(const struct cli_parsed *parsed, struct cli_context *context);
int _cli_arg(struct __sourceloc __whence, const struct cli_parsed *parsed, char *label, const char **dst, int (*validator)(const char *arg), char *defaultvalue);

Expand All @@ -89,15 +96,4 @@ int cli_interval_ms(const char *arg);
int cli_uint(const char *arg);
int cli_optional_did(const char *text);

int cli_putchar(struct cli_context *context, char c);
int cli_puts(struct cli_context *context, const char *str);
void cli_printf(struct cli_context *context, const char *fmt, ...) __attribute__ (( format(printf,2,3) ));
int cli_delim(struct cli_context *context, const char *opt);
void cli_columns(struct cli_context *context, int columns, const char *names[]);
void cli_row_count(struct cli_context *context, int rows);
void cli_field_name(struct cli_context *context, const char *name, const char *delim);
void cli_put_long(struct cli_context *context, int64_t value, const char *delim);
void cli_put_string(struct cli_context *context, const char *value, const char *delim);
void cli_put_hexvalue(struct cli_context *context, const unsigned char *value, int length, const char *delim);

#endif // __SERVAL_DNA__CLI_H
Loading

0 comments on commit c7fde5e

Please sign in to comment.