2013-02-12 07:30:37 +00:00
|
|
|
/*
|
|
|
|
Serval command line parsing and processing.
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2012,2013 Serval Project Inc.
|
2013-02-12 07:30:37 +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.
|
|
|
|
*/
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#ifndef __SERVAL_DNA__CLI_H
|
|
|
|
#define __SERVAL_DNA__CLI_H
|
2012-09-25 04:01:34 +00:00
|
|
|
|
2013-07-13 05:17:06 +00:00
|
|
|
#include <stdint.h>
|
2013-07-03 07:21:27 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
|
|
|
#include <jni.h>
|
2014-09-01 02:25:50 +00:00
|
|
|
|
|
|
|
// Stop OpenJDK 7 from foisting their UNUSED() macro on us in <jni_md.h>
|
|
|
|
#ifdef UNUSED
|
|
|
|
# undef UNUSED
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 07:21:27 +00:00
|
|
|
#endif
|
2014-07-23 01:03:41 +00:00
|
|
|
#include "xprintf.h"
|
|
|
|
#include "log.h"
|
2013-07-03 07:21:27 +00:00
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
#define COMMAND_LINE_MAX_LABELS (16)
|
2013-02-12 07:30:37 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
struct cli_parsed;
|
2013-07-03 07:21:27 +00:00
|
|
|
struct cli_context{
|
|
|
|
#ifdef HAVE_JNI_H
|
|
|
|
JNIEnv *jni_env;
|
|
|
|
int jni_exception;
|
|
|
|
jobject jniResults;
|
|
|
|
char *outv_buffer;
|
|
|
|
char *outv_current;
|
|
|
|
char *outv_limit;
|
|
|
|
#endif
|
|
|
|
void *context;
|
|
|
|
};
|
2013-02-12 07:30:37 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
struct cli_schema {
|
2013-07-03 07:21:27 +00:00
|
|
|
int (*function)(const struct cli_parsed *parsed, struct cli_context *context);
|
2013-02-12 07:30:37 +00:00
|
|
|
const char *words[COMMAND_LINE_MAX_LABELS];
|
2013-07-13 05:17:06 +00:00
|
|
|
uint64_t flags;
|
2013-04-02 07:19:56 +00:00
|
|
|
#define CLIFLAG_PERMISSIVE_CONFIG (1<<0) /* Accept defective configuration file */
|
2012-09-25 04:01:34 +00:00
|
|
|
const char *description; // describe this invocation
|
2013-02-12 07:30:37 +00:00
|
|
|
};
|
2012-09-25 04:01:34 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
struct cli_parsed {
|
2013-03-25 01:35:26 +00:00
|
|
|
const struct cli_schema *commands;
|
2014-08-18 09:08:05 +00:00
|
|
|
const struct cli_schema *end_commands;
|
2013-03-25 01:35:26 +00:00
|
|
|
unsigned int cmdi;
|
2013-02-12 07:30:37 +00:00
|
|
|
struct labelv {
|
|
|
|
const char *label;
|
|
|
|
unsigned int len;
|
2013-02-13 05:25:00 +00:00
|
|
|
const char *text;
|
2013-02-12 07:30:37 +00:00
|
|
|
} labelv[COMMAND_LINE_MAX_LABELS];
|
|
|
|
unsigned labelc;
|
|
|
|
const char *const *args;
|
|
|
|
unsigned argc;
|
2013-04-12 07:46:50 +00:00
|
|
|
int varargi; // -1 means no var args
|
2013-02-12 07:30:37 +00:00
|
|
|
};
|
2012-09-25 04:01:34 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
void _debug_cli_parsed(struct __sourceloc __whence, const struct cli_parsed *parsed);
|
2013-02-12 07:30:37 +00:00
|
|
|
|
2013-02-13 07:13:24 +00:00
|
|
|
#define DEBUG_cli_parsed(parsed) _debug_cli_parsed(__WHENCE__, parsed)
|
2013-02-12 07:30:37 +00:00
|
|
|
|
2014-08-18 09:08:05 +00:00
|
|
|
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);
|
2013-03-25 01:35:26 +00:00
|
|
|
int cli_usage_parsed(const struct cli_parsed *parsed, XPRINTF xpf);
|
2014-08-18 09:08:05 +00:00
|
|
|
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);
|
2013-07-03 07:21:27 +00:00
|
|
|
int cli_invoke(const struct cli_parsed *parsed, struct cli_context *context);
|
2013-02-14 05:30:47 +00:00
|
|
|
|
2015-03-27 18:07:38 +00:00
|
|
|
/* First, assign 'defaultvalue' to '*dst', to guarantee that '*dst' is in a
|
|
|
|
* known state regardless of the return value and provide the caller with an
|
|
|
|
* alternative way to check if an argument was found.
|
|
|
|
*
|
|
|
|
* Then, if there is an argument labelled 'label' present on the 'parsed'
|
|
|
|
* command line:
|
|
|
|
* - if a validator function was supplied (not NULL) and it returns false (0)
|
|
|
|
* when invoked on '*dst', return -1, otherwise
|
|
|
|
* - assign the argument's value as a NUL-terminated string to '*dst' and
|
|
|
|
* return 0.
|
|
|
|
*
|
|
|
|
* Otherwise, there is no argument labelled 'label', so return 1.
|
|
|
|
*/
|
2013-02-14 05:30:47 +00:00
|
|
|
#define cli_arg(parsed, label, dst, validator, defaultvalue) _cli_arg(__WHENCE__, parsed, label, dst, validator, defaultvalue)
|
2015-03-27 18:07:38 +00:00
|
|
|
int _cli_arg(struct __sourceloc __whence, const struct cli_parsed *parsed, char *label, const char **dst, int (*validator)(const char *arg), char *defaultvalue);
|
2012-09-25 04:01:34 +00:00
|
|
|
|
|
|
|
int cli_lookup_did(const char *text);
|
2013-04-30 07:59:06 +00:00
|
|
|
int cli_path_regular(const char *arg);
|
2012-09-25 04:01:34 +00:00
|
|
|
int cli_absolute_path(const char *arg);
|
|
|
|
int cli_optional_sid(const char *arg);
|
2014-11-12 23:33:07 +00:00
|
|
|
int cli_optional_bundle_secret_key(const char *arg);
|
2012-09-25 04:01:34 +00:00
|
|
|
int cli_manifestid(const char *arg);
|
|
|
|
int cli_fileid(const char *arg);
|
|
|
|
int cli_optional_bundle_crypt_key(const char *arg);
|
2013-04-22 06:31:38 +00:00
|
|
|
int cli_interval_ms(const char *arg);
|
2012-09-25 04:01:34 +00:00
|
|
|
int cli_uint(const char *arg);
|
|
|
|
int cli_optional_did(const char *text);
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#endif // __SERVAL_DNA__CLI_H
|