From f9d8536d7d944a5f2ba10163719302e3d81ec941 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 24 Apr 2012 17:50:27 +0930 Subject: [PATCH] JNI-ize the output of "dna rhizome list" --- commandline.c | 9 +++------ rhizome_database.c | 28 +++++++++++++++++----------- serval.h | 4 ++++ testframework.sh | 22 ++++++++++++++++++++-- tests/dna_rhizome | 6 +++--- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/commandline.c b/commandline.c index 868394b4..d7459cd6 100644 --- a/commandline.c +++ b/commandline.c @@ -240,8 +240,6 @@ JNIEXPORT jobject JNICALL Java_org_servalproject_servald_ServalD_command(JNIEnv int parseCommandLine(int argc, const char *const *args) { int i; - for (i = 0; i != argc; ++i) - fprintf(stderr, "args[%d]=\"%s\"\n", i, args[i]); int ambiguous=0; int cli_call=-1; for(i=0;command_line_options[i].function;i++) @@ -409,14 +407,14 @@ int cli_printf(const char *fmt, ...) JNI call, then this simply writes a newline to standard output (or the value of the SERVALD_OUTPUT_DELIMITER env var if set). */ -int cli_delim() +int cli_delim(const char *opt) { if (jni_env) { outv_end_field(); } else { const char *delim = getenv("SERVALD_OUTPUT_DELIMITER"); if (delim == NULL) - delim = "\n"; + delim = opt ? opt : "\n"; fputs(delim, stdout); } return 0; @@ -427,7 +425,7 @@ int app_echo(int argc, const char *const *argv, struct command_line_option *o) int i; for (i = 1; i < argc; ++i) { cli_puts(argv[i]); - cli_delim(); + cli_delim(NULL); } return 0; } @@ -1131,4 +1129,3 @@ command_line_option command_line_options[]={ #endif {NULL,{NULL}} }; - diff --git a/rhizome_database.c b/rhizome_database.c index f1dabf45..366d4c77 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -529,6 +529,14 @@ int rhizome_list_manifests(int limit, int offset) ret = WHY(sqlite3_errmsg(rhizome_db)); } else { size_t rows = 0; + cli_puts("fileid"); cli_delim(":"); + cli_puts("manifestid"); cli_delim(":"); + cli_puts("version"); cli_delim(":"); + cli_puts("inserttime"); cli_delim(":"); + cli_puts("length"); cli_delim(":"); + cli_puts("datavalid"); cli_delim(":"); + cli_puts("date"); cli_delim(":"); + cli_puts("name"); cli_delim("\n"); while (sqlite3_step(statement) == SQLITE_ROW) { ++rows; if (!( sqlite3_column_count(statement) == 7 @@ -548,16 +556,14 @@ int rhizome_list_manifests(int limit, int offset) rhizome_manifest *m = rhizome_read_manifest_file(manifestblob, manifestblobsize, 0); const char *name = rhizome_manifest_get(m, "name", NULL, 0); long long date = rhizome_manifest_get_ll(m, "date"); - printf("fileid=%s:manifestid=%s:version=%lld:inserttime=%lld:length=%u:datavalid=%u:date=%lld:name=%s\n", - sqlite3_column_text(statement, 0), - sqlite3_column_text(statement, 3), - (long long) sqlite3_column_int64(statement, 5), - (long long) sqlite3_column_int64(statement, 6), - sqlite3_column_int(statement, 1), - sqlite3_column_int(statement, 2), - date, - name - ); + cli_puts((const char *)sqlite3_column_text(statement, 0)); cli_delim(":"); + cli_puts((const char *)sqlite3_column_text(statement, 3)); cli_delim(":"); + cli_printf("%lld", (long long) sqlite3_column_int64(statement, 5)); cli_delim(":"); + cli_printf("%lld", (long long) sqlite3_column_int64(statement, 6)); cli_delim(":"); + cli_printf("%u", sqlite3_column_int(statement, 1)); cli_delim(":"); + cli_printf("%u", sqlite3_column_int(statement, 2)); cli_delim(":"); + cli_printf("%lld", date); cli_delim(":"); + cli_puts(name); cli_delim("\n"); rhizome_manifest_free(m); } } @@ -795,7 +801,7 @@ int rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found) ret = WHY("Incorrect statement columns"); break; } - const char *manifestid = (char *) sqlite3_column_text(statement, 0); + const char *manifestid = (const char *) sqlite3_column_text(statement, 0); size_t manifestidsize = sqlite3_column_bytes(statement, 0); // must call after sqlite3_column_text() if (manifestidsize != crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES * 2) { ret = WHYF("Malformed manifest.id from query: %s", manifestid); diff --git a/serval.h b/serval.h index bf482e67..45e171a0 100644 --- a/serval.h +++ b/serval.h @@ -1318,6 +1318,10 @@ typedef struct command_line_option { extern command_line_option command_line_options[]; int cli_arg(int argc, const char *const *argv, command_line_option *o, char *argname, const char **dst, int (*validator)(const char *arg), char *defaultvalue); +int cli_putchar(char c); +int cli_puts(const char *str); +int cli_printf(const char *fmt, ...); +int cli_delim(const char *opt); int overlay_mdp_getmyaddr(int index,unsigned char *sid); diff --git a/testframework.sh b/testframework.sh index a55e7f18..45000bae 100644 --- a/testframework.sh +++ b/testframework.sh @@ -508,6 +508,7 @@ _tfw_getopts() { _tfw_opt_error_on_fail=false _tfw_opt_exit_status= _tfw_opt_matches= + _tfw_opt_line= _tfw_getopts_shift=0 while [ $# -ne 0 ]; do case "$context:$1" in @@ -517,6 +518,7 @@ _tfw_getopts() { assert*:--error-on-fail) _tfw_opt_error_on_fail=true;; assert*:--message=*) _tfw_message="${1#*=}";; assertgrep:--matches=*) _tfw_opt_matches="${1#*=}";; + assertfilecontent:--line=*) _tfw_opt_line="${1#*=}";; *:--) let _tfw_getopts_shift=_tfw_getopts_shift+1; shift; break;; *:--*) _tfw_error "unsupported option: $1";; *) break;; @@ -573,9 +575,25 @@ _tfw_assert_stdxxx_is() { _tfw_error "incorrect arguments" return 254 fi - local message="${_tfw_message:-$qual of ${_tfw_execute_argv0##*/} is $*}" + _tfw_shopt -s extglob + case "$_tfw_opt_line" in + +([0-9])) + sed -n -e "${_tfw_opt_line}p" "$_tfw_tmp/$qual" >"$_tfw_tmp/content" + ;; + '') + ln -f "$_tfw_tmp/$qual" "$_tfw_tmp/content" + ;; + *) + _tfw_error "unsupported value for --line=$_tfw_opt_line" + _tfw_backtrace + _tfw_shopt_restore + return 254 + ;; + esac + _tfw_shopt_restore + local message="${_tfw_message:-${_tfw_opt_line:+line $_tfw_opt_line of }$qual of ${_tfw_execute_argv0##*/} is $*}" echo -n "$@" >$_tfw_tmp/stdxxx_is.tmp - if ! cmp --quiet $_tfw_tmp/stdxxx_is.tmp "$_tfw_tmp/$qual"; then + if ! cmp --quiet $_tfw_tmp/stdxxx_is.tmp "$_tfw_tmp/content"; then _tfw_failmsg "assertion failed: $message" _tfw_backtrace return 1 diff --git a/tests/dna_rhizome b/tests/dna_rhizome index b3bef8b2..2bcee402 100755 --- a/tests/dna_rhizome +++ b/tests/dna_rhizome @@ -28,8 +28,8 @@ setup_dna_rhizome() { assert_rhizome_list() { executeOk $dna rhizome list - assertStdoutLineCount '==' $# - assertStdoutGrep --matches=$# '^fileid=.*:name=' + assertStdoutLineCount '==' $(($# + 1)) + assertStdoutIs --line=1 -e 'fileid:manifestid:version:inserttime:length:datavalid:date:name\n' local filename for filename; do local filehash='[^:]\+' @@ -41,7 +41,7 @@ assert_rhizome_list() { filehash=$(sed -n -e '/^filehash=/s///p' "$filename.manifest") fi fi - assertStdoutGrep --matches=1 "^\(.*:\)\?fileid=$filehash:.*:name=$filename\$" + assertStdoutGrep --matches=1 "^$filehash:.*:$filename\$" done }