2012-04-20 07:32:29 +00:00
|
|
|
/*
|
2013-04-09 07:48:37 +00:00
|
|
|
Serval DNA command-line functions
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2010-2013 Serval Project Inc.
|
2012-02-16 14:05:28 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-09-14 02:17:48 +00:00
|
|
|
#include "mdp_client.h"
|
2012-09-25 04:01:34 +00:00
|
|
|
#include "cli.h"
|
2013-10-16 03:00:00 +00:00
|
|
|
#include "keyring.h"
|
2013-11-25 02:39:54 +00:00
|
|
|
#include "dataformats.h"
|
2014-08-18 09:08:05 +00:00
|
|
|
#include "commandline.h"
|
2014-08-25 04:54:00 +00:00
|
|
|
#include "server.h"
|
2012-02-16 14:05:28 +00:00
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
DEFINE_CMD(commandline_usage,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Display command usage.",
|
|
|
|
"help|-h|--help","...");
|
|
|
|
static int commandline_usage(const struct cli_parsed *parsed, struct cli_context *UNUSED(context))
|
2013-02-12 07:30:37 +00:00
|
|
|
{
|
2013-03-25 01:35:26 +00:00
|
|
|
printf("Serval DNA version %s\nUsage:\n", version_servald);
|
|
|
|
return cli_usage_parsed(parsed, XPRINTF_STDIO(stdout));
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
DEFINE_CMD(version_message,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Display copyright information.",
|
|
|
|
"version|copyright");
|
|
|
|
static int version_message(const struct cli_parsed *UNUSED(parsed), struct cli_context *UNUSED(context))
|
2013-12-05 02:51:58 +00:00
|
|
|
{
|
|
|
|
printf("Serval DNA version %s\n%s\n", version_servald, copyright_servald);
|
|
|
|
printf("\
|
|
|
|
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n\
|
|
|
|
This is free software: you are free to change and redistribute it.\n\
|
|
|
|
There is NO WARRANTY, to the extent permitted by law.\n\
|
|
|
|
");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
/* Data structures for accumulating output of a single JNI call.
|
|
|
|
*/
|
|
|
|
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
|
|
|
|
2012-04-30 08:15:24 +00:00
|
|
|
#define OUTV_BUFFER_ALLOCSIZE (8192)
|
2012-04-23 07:42:10 +00:00
|
|
|
|
2013-01-22 04:56:40 +00:00
|
|
|
jclass IJniResults = NULL;
|
|
|
|
jmethodID startResultSet, setColumnName, putString, putBlob, putLong, putDouble, totalRowCount;
|
2012-04-23 07:42:10 +00:00
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
static int outv_growbuf(struct cli_context *context, size_t needed)
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2013-12-10 06:26:23 +00:00
|
|
|
assert(context->outv_current <= context->outv_limit);
|
|
|
|
size_t remaining = (size_t)(context->outv_limit - context->outv_current);
|
|
|
|
if (remaining < needed) {
|
|
|
|
size_t cursize = context->outv_current - context->outv_buffer;
|
|
|
|
size_t newsize = cursize + needed;
|
2012-04-30 08:15:24 +00:00
|
|
|
// Round up to nearest multiple of OUTV_BUFFER_ALLOCSIZE.
|
|
|
|
newsize = newsize + OUTV_BUFFER_ALLOCSIZE - ((newsize - 1) % OUTV_BUFFER_ALLOCSIZE + 1);
|
2013-12-10 06:26:23 +00:00
|
|
|
assert(newsize > cursize);
|
|
|
|
assert((size_t)(newsize - cursize) >= needed);
|
2013-07-03 07:21:27 +00:00
|
|
|
context->outv_buffer = realloc(context->outv_buffer, newsize);
|
|
|
|
if (context->outv_buffer == NULL)
|
2012-04-24 07:36:48 +00:00
|
|
|
return WHYF("Out of memory allocating %lu bytes", (unsigned long) newsize);
|
2013-12-10 06:26:23 +00:00
|
|
|
context->outv_current = context->outv_buffer + cursize;
|
2013-07-03 07:21:27 +00:00
|
|
|
context->outv_limit = context->outv_buffer + newsize;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
static int put_blob(struct cli_context *context, jbyte *value, jsize length){
|
2013-01-22 04:56:40 +00:00
|
|
|
jbyteArray arr = NULL;
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return -1;
|
2014-11-03 02:22:54 +00:00
|
|
|
arr = (*context->jni_env)->NewByteArray(context->jni_env, length);
|
|
|
|
if (arr == NULL || (*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
|
|
|
context->jni_exception = 1;
|
|
|
|
return WHY("Exception thrown from NewByteArray()");
|
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
if (value && length>0){
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->SetByteArrayRegion(context->jni_env, arr, 0, length, value);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2013-01-22 04:56:40 +00:00
|
|
|
return WHYF("Exception thrown from SetByteArrayRegion()");
|
|
|
|
}
|
2012-11-07 06:42:04 +00:00
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putBlob, arr);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2014-02-19 05:05:08 +00:00
|
|
|
return WHY("Exception thrown from CallVoidMethod(putBlob)");
|
2012-04-30 08:15:24 +00:00
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
if (arr)
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->DeleteLocalRef(context->jni_env, arr);
|
2012-04-23 07:42:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-20 07:32:29 +00:00
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
static int outv_end_field(struct cli_context *context)
|
2013-01-22 04:56:40 +00:00
|
|
|
{
|
2013-07-03 07:21:27 +00:00
|
|
|
jsize length = context->outv_current - context->outv_buffer;
|
|
|
|
context->outv_current = context->outv_buffer;
|
|
|
|
return put_blob(context, (jbyte *)context->outv_buffer, length);
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 08:53:34 +00:00
|
|
|
int Throw(JNIEnv *env, const char *class, const char *msg)
|
|
|
|
{
|
2013-01-22 04:56:40 +00:00
|
|
|
jclass exceptionClass = NULL;
|
|
|
|
if ((exceptionClass = (*env)->FindClass(env, class)) == NULL)
|
|
|
|
return -1; // exception
|
|
|
|
(*env)->ThrowNew(env, exceptionClass, msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-03 02:22:54 +00:00
|
|
|
int initJniTypes(JNIEnv *env)
|
|
|
|
{
|
|
|
|
if (IJniResults)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
IJniResults = (*env)->FindClass(env, "org/servalproject/servaldna/IJniResults");
|
|
|
|
if (IJniResults==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate class org.servalproject.servaldna.IJniResults");
|
|
|
|
startResultSet = (*env)->GetMethodID(env, IJniResults, "startResultSet", "(I)V");
|
|
|
|
if (startResultSet==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method startResultSet");
|
|
|
|
setColumnName = (*env)->GetMethodID(env, IJniResults, "setColumnName", "(ILjava/lang/String;)V");
|
|
|
|
if (setColumnName==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method setColumnName");
|
|
|
|
putString = (*env)->GetMethodID(env, IJniResults, "putString", "(Ljava/lang/String;)V");
|
|
|
|
if (putString==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putString");
|
|
|
|
putBlob = (*env)->GetMethodID(env, IJniResults, "putBlob", "([B)V");
|
|
|
|
if (putBlob==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putBlob");
|
|
|
|
putLong = (*env)->GetMethodID(env, IJniResults, "putLong", "(J)V");
|
|
|
|
if (putLong==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putLong");
|
|
|
|
putDouble = (*env)->GetMethodID(env, IJniResults, "putDouble", "(D)V");
|
|
|
|
if (putDouble==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putDouble");
|
|
|
|
totalRowCount = (*env)->GetMethodID(env, IJniResults, "totalRowCount", "(I)V");
|
|
|
|
if (totalRowCount==NULL)
|
|
|
|
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method totalRowCount");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-20 07:32:29 +00:00
|
|
|
/* JNI entry point to command line. See org.servalproject.servald.ServalD class for the Java side.
|
2012-04-30 08:15:24 +00:00
|
|
|
JNI method descriptor: "(Ljava/util/List;[Ljava/lang/String;)I"
|
2012-04-20 07:32:29 +00:00
|
|
|
*/
|
2014-02-12 02:37:31 +00:00
|
|
|
JNIEXPORT jint JNICALL Java_org_servalproject_servaldna_ServalDCommand_rawCommand(JNIEnv *env, jobject UNUSED(this), jobject outv, jobjectArray args)
|
2012-04-20 07:32:29 +00:00
|
|
|
{
|
2013-07-03 07:21:27 +00:00
|
|
|
struct cli_context context;
|
|
|
|
bzero(&context, sizeof(context));
|
|
|
|
|
2014-11-03 02:22:54 +00:00
|
|
|
int r;
|
2013-07-03 07:21:27 +00:00
|
|
|
// find jni results methods
|
2014-11-03 02:22:54 +00:00
|
|
|
if ((r=initJniTypes(env))!=0)
|
|
|
|
return r;
|
|
|
|
|
2013-01-22 04:56:40 +00:00
|
|
|
unsigned char status = 0; // to match what the shell gets: 0..255
|
2013-07-03 07:21:27 +00:00
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
// Construct argv, argc from this method's arguments.
|
2012-04-20 07:32:29 +00:00
|
|
|
jsize len = (*env)->GetArrayLength(env, args);
|
2013-01-22 04:56:40 +00:00
|
|
|
const char **argv = alloca(sizeof(char*) * (len + 1));
|
|
|
|
if (argv == NULL)
|
2013-02-15 08:53:34 +00:00
|
|
|
return Throw(env, "java/lang/OutOfMemoryError", "alloca() returned NULL");
|
2014-11-03 02:22:54 +00:00
|
|
|
bzero(argv, sizeof(char*) * (len + 1));
|
2012-04-20 07:32:29 +00:00
|
|
|
jsize i;
|
2012-04-24 07:36:48 +00:00
|
|
|
int argc = len;
|
|
|
|
// From now on, in case of an exception we have to free some resources before
|
|
|
|
// returning.
|
2014-11-03 02:22:54 +00:00
|
|
|
static const char *EMPTY="";
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
for (i = 0; !context.jni_exception && i < len; ++i) {
|
2012-04-20 07:32:29 +00:00
|
|
|
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
|
2014-11-03 02:22:54 +00:00
|
|
|
if ((*env)->ExceptionCheck(env)){
|
2013-07-03 07:21:27 +00:00
|
|
|
context.jni_exception = 1;
|
2014-11-03 02:22:54 +00:00
|
|
|
}else if (arg == NULL) {
|
|
|
|
argv[i] = EMPTY;
|
|
|
|
} else {
|
|
|
|
argv[i] = (*env)->GetStringUTFChars(env, arg, NULL);
|
|
|
|
if (argv[i] == NULL)
|
2013-07-03 07:21:27 +00:00
|
|
|
context.jni_exception = 1;
|
2012-04-24 07:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
if (!context.jni_exception) {
|
2012-04-24 07:36:48 +00:00
|
|
|
// Set up the output buffer.
|
2013-07-03 07:21:27 +00:00
|
|
|
context.jniResults = outv;
|
|
|
|
context.outv_current = context.outv_buffer;
|
2012-04-24 07:36:48 +00:00
|
|
|
// Execute the command.
|
2013-07-03 07:21:27 +00:00
|
|
|
context.jni_env = env;
|
|
|
|
status = parseCommandLine(&context, NULL, argc, argv);
|
2012-04-24 07:36:48 +00:00
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
|
|
|
|
// free any temporary output buffer
|
|
|
|
if (context.outv_buffer)
|
2013-07-04 06:32:19 +00:00
|
|
|
free(context.outv_buffer);
|
2013-07-03 07:21:27 +00:00
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
// Release argv Java string buffers.
|
2013-01-22 04:56:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2014-11-03 02:22:54 +00:00
|
|
|
if (argv[i] && argv[i]!=EMPTY) {
|
2012-04-24 07:36:48 +00:00
|
|
|
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
|
2014-11-03 02:22:54 +00:00
|
|
|
if (arg)
|
|
|
|
(*env)->ReleaseStringUTFChars(env, arg, argv[i]);
|
2012-04-24 07:36:48 +00:00
|
|
|
}
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
// Deal with Java exceptions: NewStringUTF out of memory in outv_end_field().
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context.jni_exception || (context.outv_current != context.outv_buffer && outv_end_field(&context) == -1))
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1;
|
2013-07-03 07:21:27 +00:00
|
|
|
|
2012-05-03 02:47:12 +00:00
|
|
|
return (jint) status;
|
2012-04-20 07:32:29 +00:00
|
|
|
}
|
2012-04-23 02:35:00 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_JNI_H */
|
2012-04-20 07:32:29 +00:00
|
|
|
|
2012-07-24 06:09:36 +00:00
|
|
|
/* The argc and argv arguments must be passed verbatim from main(argc, argv), so argv[0] is path to
|
|
|
|
executable.
|
|
|
|
*/
|
2013-07-03 07:21:27 +00:00
|
|
|
int parseCommandLine(struct cli_context *context, const char *argv0, int argc, const char *const *args)
|
2012-02-16 14:05:28 +00:00
|
|
|
{
|
2012-07-03 05:42:42 +00:00
|
|
|
fd_clearstats();
|
|
|
|
IN();
|
2012-09-25 04:01:34 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
struct cli_parsed parsed;
|
2014-08-18 09:08:05 +00:00
|
|
|
int result = cli_parse(argc, args, __start_commands, __stop_commands, &parsed);
|
2013-04-09 07:48:37 +00:00
|
|
|
switch (result) {
|
|
|
|
case 0:
|
|
|
|
// Do not run the command if the configuration does not load ok.
|
2013-03-25 01:35:26 +00:00
|
|
|
if (((parsed.commands[parsed.cmdi].flags & CLIFLAG_PERMISSIVE_CONFIG) ? cf_reload_permissive() : cf_reload()) != -1)
|
2013-07-03 07:21:27 +00:00
|
|
|
result = cli_invoke(&parsed, context);
|
2012-12-14 01:09:08 +00:00
|
|
|
else {
|
|
|
|
strbuf b = strbuf_alloca(160);
|
|
|
|
strbuf_append_argv(b, argc, args);
|
2013-03-28 13:52:08 +00:00
|
|
|
result = WHYF("configuration defective, not running command: %s", strbuf_str(b));
|
2012-12-14 01:09:08 +00:00
|
|
|
}
|
2013-04-09 07:48:37 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
// Load configuration so that log messages can get out.
|
|
|
|
cf_reload_permissive();
|
|
|
|
NOWHENCE(HINTF("Run \"%s help\" for more information.", argv0 ? argv0 : "servald"));
|
2014-02-19 05:05:08 +00:00
|
|
|
result =-1;
|
2013-04-09 07:48:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Load configuration so that log error messages can get out.
|
2012-12-14 01:09:08 +00:00
|
|
|
cf_reload_permissive();
|
2013-04-09 07:48:37 +00:00
|
|
|
break;
|
2012-12-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 02:18:46 +00:00
|
|
|
cli_cleanup();
|
2014-05-15 06:21:02 +00:00
|
|
|
|
2012-07-03 05:42:42 +00:00
|
|
|
OUT();
|
2012-07-25 02:44:53 +00:00
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.timing)
|
2012-07-03 05:42:42 +00:00
|
|
|
fd_showstats();
|
2012-04-30 10:34:07 +00:00
|
|
|
return result;
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 06:42:04 +00:00
|
|
|
/* Write a buffer of data to output. If in a JNI call, then this appends the data to the
|
|
|
|
current output field, including any embedded nul characters. Returns a non-negative integer on
|
2012-04-23 07:42:10 +00:00
|
|
|
success, EOF on error.
|
|
|
|
*/
|
2014-05-22 04:47:36 +00:00
|
|
|
int cli_write(struct cli_context *UNUSED(context), const unsigned char *buf, size_t len)
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
|
|
|
size_t avail = context->outv_limit - context->outv_current;
|
2013-01-22 04:56:40 +00:00
|
|
|
if (avail < len) {
|
2013-07-03 07:21:27 +00:00
|
|
|
memcpy(context->outv_current, buf, avail);
|
|
|
|
context->outv_current = context->outv_limit;
|
|
|
|
if (outv_growbuf(context, len) == -1)
|
2013-01-22 04:56:40 +00:00
|
|
|
return EOF;
|
|
|
|
len -= avail;
|
|
|
|
buf += avail;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
memcpy(context->outv_current, buf, len);
|
|
|
|
context->outv_current += len;
|
2013-01-22 04:56:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-07 06:42:04 +00:00
|
|
|
#endif
|
2013-01-22 04:56:40 +00:00
|
|
|
return fwrite(buf, len, 1, stdout);
|
2012-11-07 06:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a null-terminated string to output. If in a JNI call, then this appends the string to the
|
|
|
|
current output field. The terminating null is not included. Returns a non-negative integer on
|
|
|
|
success, EOF on error.
|
|
|
|
*/
|
2014-05-22 04:47:36 +00:00
|
|
|
int cli_puts(struct cli_context *UNUSED(context), const char *str)
|
2012-11-07 06:42:04 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_JNI_H
|
2013-09-30 06:36:23 +00:00
|
|
|
if (context && context->jni_env)
|
|
|
|
return cli_write(context, (const unsigned char *) str, strlen(str));
|
|
|
|
else
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2013-09-30 06:36:23 +00:00
|
|
|
return fputs(str, stdout);
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a formatted string to output. If in a JNI call, then this appends the string to the
|
2013-12-10 06:26:23 +00:00
|
|
|
current output field, excluding the terminating null.
|
2012-04-23 07:42:10 +00:00
|
|
|
*/
|
2014-05-22 04:47:36 +00:00
|
|
|
void cli_printf(struct cli_context *UNUSED(context), const char *fmt, ...)
|
2012-04-23 02:35:00 +00:00
|
|
|
{
|
2012-05-23 06:37:52 +00:00
|
|
|
va_list ap;
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2013-12-10 06:26:23 +00:00
|
|
|
assert(context->outv_current <= context->outv_limit);
|
2013-07-03 07:21:27 +00:00
|
|
|
size_t avail = context->outv_limit - context->outv_current;
|
2012-05-23 06:37:52 +00:00
|
|
|
va_start(ap, fmt);
|
2013-07-03 07:21:27 +00:00
|
|
|
int count = vsnprintf(context->outv_current, avail, fmt, ap);
|
2012-05-23 06:37:52 +00:00
|
|
|
va_end(ap);
|
2013-12-10 06:26:23 +00:00
|
|
|
if (count < 0) {
|
|
|
|
WHYF("vsnprintf(%p,%zu,%s,...) failed", context->outv_current, avail, alloca_str_toprint(fmt));
|
|
|
|
return;
|
|
|
|
} else if ((size_t)count < avail) {
|
|
|
|
context->outv_current += count;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (outv_growbuf(context, count) == -1)
|
|
|
|
return;
|
|
|
|
avail = context->outv_limit - context->outv_current;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
count = vsprintf(context->outv_current, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (count < 0) {
|
|
|
|
WHYF("vsprintf(%p,%s,...) failed", context->outv_current, alloca_str_toprint(fmt));
|
|
|
|
return;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
2013-12-10 06:26:23 +00:00
|
|
|
assert((size_t)count < avail);
|
|
|
|
context->outv_current += (size_t)count;
|
2012-04-23 07:42:10 +00:00
|
|
|
} else
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2012-05-23 06:37:52 +00:00
|
|
|
{
|
|
|
|
va_start(ap, fmt);
|
2013-12-10 06:26:23 +00:00
|
|
|
if (vfprintf(stdout, fmt, ap) < 0)
|
|
|
|
WHYF("vfprintf(stdout,%s,...) failed", alloca_str_toprint(fmt));
|
2012-05-23 06:37:52 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:36:23 +00:00
|
|
|
void cli_columns(struct cli_context *context, int columns, const char *names[])
|
|
|
|
{
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return;
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, startResultSet, columns);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2014-02-19 05:05:08 +00:00
|
|
|
WHY("Exception thrown from CallVoidMethod(startResultSet)");
|
2013-01-22 04:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i=0;i<columns;i++){
|
2013-07-03 07:21:27 +00:00
|
|
|
jstring str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, names[i]);
|
2013-01-22 04:56:40 +00:00
|
|
|
if (str == NULL) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2013-01-22 04:56:40 +00:00
|
|
|
WHY("Exception thrown from NewStringUTF()");
|
|
|
|
return;
|
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, setColumnName, i, str);
|
|
|
|
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2014-02-19 05:05:08 +00:00
|
|
|
context->jni_exception = 1;
|
|
|
|
WHY("Exception thrown from CallVoidMethod(setColumnName)");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-30 06:36:23 +00:00
|
|
|
cli_printf(context, "%d", columns);
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_delim(context, "\n");
|
2013-01-22 04:56:40 +00:00
|
|
|
int i;
|
|
|
|
for (i=0;i<columns;i++){
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_puts(context, names[i]);
|
2013-01-22 04:56:40 +00:00
|
|
|
if (i+1==columns)
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_delim(context, "\n");
|
2013-01-22 04:56:40 +00:00
|
|
|
else
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_delim(context, ":");
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:36:23 +00:00
|
|
|
void cli_field_name(struct cli_context *context, const char *name, const char *delim)
|
|
|
|
{
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return;
|
2013-07-03 07:21:27 +00:00
|
|
|
jstring str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, name);
|
2013-01-22 04:56:40 +00:00
|
|
|
if (str == NULL) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2013-01-22 04:56:40 +00:00
|
|
|
WHY("Exception thrown from NewStringUTF()");
|
|
|
|
return;
|
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, setColumnName, -1, str);
|
|
|
|
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2014-02-19 05:05:08 +00:00
|
|
|
context->jni_exception = 1;
|
|
|
|
WHY("Exception thrown from CallVoidMethod(setColumnName)");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_puts(context, name);
|
|
|
|
cli_delim(context, delim);
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
void cli_put_long(struct cli_context *context, int64_t value, const char *delim){
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return;
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putLong, value);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2014-02-19 05:05:08 +00:00
|
|
|
context->jni_exception = 1;
|
|
|
|
WHY("Exception thrown from CallVoidMethod(putLong)");
|
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-15 00:29:24 +00:00
|
|
|
cli_printf(context, "%" PRId64, value);
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_delim(context, delim);
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
void cli_put_string(struct cli_context *context, const char *value, const char *delim){
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return;
|
2013-01-22 04:56:40 +00:00
|
|
|
jstring str = NULL;
|
|
|
|
if (value){
|
2013-07-03 07:21:27 +00:00
|
|
|
str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, value);
|
2013-01-22 04:56:40 +00:00
|
|
|
if (str == NULL) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2013-01-22 04:56:40 +00:00
|
|
|
WHY("Exception thrown from NewStringUTF()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putString, str);
|
|
|
|
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2014-02-19 05:05:08 +00:00
|
|
|
context->jni_exception = 1;
|
|
|
|
WHY("Exception thrown from CallVoidMethod(putLong)");
|
|
|
|
}
|
2013-01-22 04:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (value)
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_puts(context, value);
|
|
|
|
cli_delim(context, delim);
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
void cli_put_hexvalue(struct cli_context *context, const unsigned char *value, int length, const char *delim){
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
|
|
|
put_blob(context, (jbyte*)value, length);
|
2013-01-22 04:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (value)
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_puts(context, alloca_tohex(value, length));
|
|
|
|
cli_delim(context, delim);
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 04:47:36 +00:00
|
|
|
void cli_row_count(struct cli_context *UNUSED(context), int UNUSED(rows)){
|
2013-01-22 04:56:40 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env) {
|
2014-02-19 05:05:08 +00:00
|
|
|
if (context->jni_exception)
|
|
|
|
return;
|
2013-07-03 07:21:27 +00:00
|
|
|
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, totalRowCount, rows);
|
2014-03-27 03:00:46 +00:00
|
|
|
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
|
2013-07-03 07:21:27 +00:00
|
|
|
context->jni_exception = 1;
|
2013-01-22 04:56:40 +00:00
|
|
|
WHY("Exception thrown from CallVoidMethod()");
|
|
|
|
}
|
2014-02-19 05:05:08 +00:00
|
|
|
return;
|
2013-01-22 04:56:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
/* Delimit the current output field. This closes the current field, so that the next cli_ output
|
|
|
|
function will start appending to a new field. Returns 0 on success, -1 on error. If not in a
|
|
|
|
JNI call, then this simply writes a newline to standard output (or the value of the
|
|
|
|
SERVALD_OUTPUT_DELIMITER env var if set).
|
|
|
|
*/
|
2014-05-22 04:47:36 +00:00
|
|
|
int cli_delim(struct cli_context *UNUSED(context), const char *opt)
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env)
|
|
|
|
return outv_end_field(context);
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2012-10-08 06:43:17 +00:00
|
|
|
const char *delim = getenv("SERVALD_OUTPUT_DELIMITER");
|
|
|
|
if (delim == NULL)
|
|
|
|
delim = opt ? opt : "\n";
|
|
|
|
fputs(delim, stdout);
|
2012-04-23 02:35:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 06:43:17 +00:00
|
|
|
/* Flush the output fields if they are being written to standard output.
|
|
|
|
*/
|
2014-05-22 04:47:36 +00:00
|
|
|
void cli_flush(struct cli_context *UNUSED(context))
|
2012-10-08 06:43:17 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_JNI_H
|
2013-07-03 07:21:27 +00:00
|
|
|
if (context && context->jni_env)
|
2012-10-08 06:43:17 +00:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
DEFINE_CMD(app_echo,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Output the supplied string.",
|
|
|
|
"echo","[-e]","[--]","...");
|
|
|
|
static int app_echo(const struct cli_parsed *parsed, struct cli_context *context)
|
2012-04-23 08:55:26 +00:00
|
|
|
{
|
2013-02-12 07:30:37 +00:00
|
|
|
if (config.debug.verbose)
|
2013-02-13 07:13:24 +00:00
|
|
|
DEBUG_cli_parsed(parsed);
|
2013-02-12 07:30:37 +00:00
|
|
|
int escapes = !cli_arg(parsed, "-e", NULL, NULL, NULL);
|
2013-12-10 05:51:23 +00:00
|
|
|
unsigned i;
|
2013-02-12 07:30:37 +00:00
|
|
|
for (i = parsed->varargi; i < parsed->argc; ++i) {
|
|
|
|
const char *arg = parsed->args[i];
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.verbose)
|
2013-02-12 07:30:37 +00:00
|
|
|
DEBUGF("echo:argv[%d]=\"%s\"", i, arg);
|
2012-11-07 06:42:04 +00:00
|
|
|
if (escapes) {
|
2013-02-12 07:30:37 +00:00
|
|
|
unsigned char buf[strlen(arg)];
|
2013-10-17 12:45:25 +00:00
|
|
|
size_t len = strn_fromprint(buf, sizeof buf, arg, 0, '\0', NULL);
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_write(context, buf, len);
|
2012-11-07 06:42:04 +00:00
|
|
|
} else
|
2013-07-03 07:21:27 +00:00
|
|
|
cli_puts(context, arg);
|
|
|
|
cli_delim(context, NULL);
|
2012-04-23 08:55:26 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
DEFINE_CMD(app_log,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Log the supplied message at given level.",
|
|
|
|
"log","error|warn|hint|info|debug","<message>");
|
|
|
|
static int app_log(const struct cli_parsed *parsed, struct cli_context *UNUSED(context))
|
2013-04-02 06:29:55 +00:00
|
|
|
{
|
|
|
|
if (config.debug.verbose)
|
|
|
|
DEBUG_cli_parsed(parsed);
|
|
|
|
assert(parsed->argc == 3);
|
|
|
|
const char *lvl = parsed->args[1];
|
|
|
|
const char *msg = parsed->args[2];
|
|
|
|
int level = string_to_log_level(lvl);
|
|
|
|
if (level == LOG_LEVEL_INVALID)
|
|
|
|
return WHYF("invalid log level: %s", lvl);
|
|
|
|
logMessage(level, __NOWHERE__, "%s", msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|