Merge libubox with the version 2016.02.26 from its git.

This commit is contained in:
2016-02-26 23:13:29 +01:00
parent 6d5f11268b
commit 737ecc15d0
39 changed files with 2971 additions and 486 deletions

View File

@@ -1,24 +1,23 @@
cmake_minimum_required(VERSION 2.6)
PROJECT(ubox-examples C)
ADD_DEFINITIONS(-O1 -Wall -Werror --std=gnu99 -g3)
IF (BUILD_EXAMPLES)
PROJECT(ubox-examples C)
ADD_DEFINITIONS(-O1 -Wall -Werror --std=gnu99 -g3)
IF(APPLE)
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/..)
LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/..)
FIND_LIBRARY(json NAMES json-c json)
ADD_EXECUTABLE(blobmsg-example blobmsg-example.c)
TARGET_LINK_LIBRARIES(blobmsg-example ubox blobmsg_json ${json})
ADD_EXECUTABLE(ustream-example ustream-example.c)
TARGET_LINK_LIBRARIES(ustream-example ubox)
ADD_EXECUTABLE(runqueue-example runqueue-example.c)
TARGET_LINK_LIBRARIES(runqueue-example ubox)
ADD_EXECUTABLE(json_script-example json_script-example.c)
TARGET_LINK_LIBRARIES(json_script-example ubox blobmsg_json json_script ${json})
ENDIF()
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/..)
LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/..)
FIND_LIBRARY(json NAMES json-c json)
ADD_EXECUTABLE(blobmsg-example blobmsg-example.c)
TARGET_LINK_LIBRARIES(blobmsg-example ubox blobmsg_json ${json})
ADD_EXECUTABLE(ustream-example ustream-example.c)
TARGET_LINK_LIBRARIES(ustream-example ubox)
ADD_EXECUTABLE(runqueue-example runqueue-example.c)
TARGET_LINK_LIBRARIES(runqueue-example ubox)

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <inttypes.h>
#include "blobmsg.h"
#include "blobmsg_json.h"

View File

@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <json.h>
#include "blobmsg.h"
#include "blobmsg_json.h"
#include "json_script.h"
struct json_script_ctx jctx;
struct blob_buf b_vars;
struct blob_buf b_script;
static void handle_command(struct json_script_ctx *ctx, const char *name,
struct blob_attr *data, struct blob_attr *vars)
{
struct blob_attr *cur;
int rem;
fprintf(stdout, "%s", name);
blobmsg_for_each_attr(cur, data, rem)
fprintf(stdout, " %s", (char *) blobmsg_data(cur));
fprintf(stdout, "\n");
}
static struct json_script_file *
handle_file(struct json_script_ctx *ctx, const char *filename)
{
json_object *obj;
obj = json_object_from_file(filename);
if (!obj) {
fprintf(stderr, "load JSON data from %s failed.\n", filename);
return NULL;
}
blob_buf_init(&b_script, 0);
blobmsg_add_json_element(&b_script, "", obj);
json_object_put(obj);
return json_script_file_from_blobmsg(filename,
blob_data(b_script.head), blob_len(b_script.head));
}
static void usage(const char *prog, int exit_code)
{
fprintf(stderr, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog);
exit(exit_code);
}
int main(int argc, char *argv[])
{
int i;
char *file = NULL;
const char *prog = argv[0];
blobmsg_buf_init(&b_vars);
blobmsg_buf_init(&b_script);
json_script_init(&jctx);
jctx.handle_command = handle_command;
jctx.handle_file = handle_file;
for (i = 1; i < argc; i++) {
char *sep = strchr(argv[i], '=');
if (sep) {
*sep = '\0';
blobmsg_add_string(&b_vars, argv[i], sep + 1);
} else if (!file) {
file = argv[i];
} else {
usage(prog, -1);
}
}
if (i < argc || !file)
usage(prog, -2);
json_script_run(&jctx, file, b_vars.head);
json_script_free(&jctx);
blob_buf_free(&b_script);
blob_buf_free(&b_vars);
return 0;
}

View File

@@ -0,0 +1,38 @@
[
[ "exec", "%EXECVAR%", "/%%/" ],
[ "if",
[ "eq", "EQVAR", "eqval" ],
[ "exec_if", "%VAR%", "%%", "jk" ]
],
[ "case", "CASEVAR", {
"caseval0": ["cmd_case_0", "cmd_case_arg0", "case_cmd_arg1"],
"caseval1": ["cmd_case_1", "cmd_case_arg0", "case_cmd_arg1"]
} ],
[ "if",
[ "and", [ "eq", "EQVAR", "eqval" ],
[ "has", "HASVAR" ],
[ "regex", "REGEXVAR0", "regexval" ],
[ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ],
[ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ],
[ "exec_if_and", "%ANDVAR%" ]
],
[ "if",
[ "or", [ "eq", "EQVAR", "eqval" ],
[ "has", "HASVAR" ],
[ "regex", "REGEXVAR0", "regexval" ],
[ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ],
[ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ],
[ "exec_if_or", "%ORVAR%" ]
],
[ "if",
[ "isdir", "%ISDIRVAR%" ],
[ "exec_isdir", "%ISDIRVAR%" ]
],
[ "return", "foobar" ],
[ "exec_non_reachable", "Arghhh" ]
]

View File

@@ -0,0 +1,287 @@
JSON_SCRIPT=tests.json
JSON_SCRIPT_BIN=./json_script-example
FILE_STDOUT=tests.stdout
FILE_STDERR=tests.stderr
FILE_EXPECTED=tests.expected
call_json_script() {
#export LD_PRELOAD=../libjson_script.so
$JSON_SCRIPT_BIN "$@" "$JSON_SCRIPT" >"$FILE_STDOUT" 2>"$FILE_STDERR"
}
assertStdioEquals() {
local expected="$1"
local file_stdio="$2"
echo "$expected" >"$FILE_EXPECTED"
if [ -z "$expected" ]; then
# we are expecting empty output, but we deliberately added a newline
# with echo above, so adding another echo to compensate for that
echo >>"$file_stdio"
fi
diff -up "$FILE_EXPECTED" "$file_stdio" >/dev/null 2>&1 || {
cat >&2 <<EOF
|--- expecting
$expected<
|--- actual
$(cat $file_stdio)<
|--- END
EOF
exit 1
}
}
assertStdoutEquals() {
assertStdioEquals "$1" "$FILE_STDOUT"
}
assertStderrEquals() {
assertStdioEquals "$1" "$FILE_STDERR"
}
test_bad_json() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ ]
[ ]
]
EOF
call_json_script
assertStderrEquals "load JSON data from $JSON_SCRIPT failed."
}
test_expr_eq() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "eq", "VAR", "foo" ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=foo"
assertStdoutEquals "echo bar"
call_json_script "VAR=xxx"
assertStdoutEquals "echo baz"
}
test_expr_has() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "has", "VAR" ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=foo"
assertStdoutEquals "echo bar"
call_json_script
assertStdoutEquals "echo baz"
}
test_expr_regex_single() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "regex", "VAR", ".ell." ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=hello"
assertStdoutEquals "echo bar"
call_json_script "VAR=.ell."
assertStdoutEquals "echo bar"
call_json_script
assertStdoutEquals "echo baz"
call_json_script "VAR="
assertStdoutEquals "echo baz"
call_json_script "VAR=hell"
assertStdoutEquals "echo baz"
}
test_expr_regex_multi() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "regex", "VAR", [ ".ell.", "w.rld" ] ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=hello"
assertStdoutEquals "echo bar"
call_json_script "VAR=world"
assertStdoutEquals "echo bar"
call_json_script "VAR=.ell."
assertStdoutEquals "echo bar"
call_json_script "VAR=w.rld"
assertStdoutEquals "echo bar"
call_json_script
assertStdoutEquals "echo baz"
call_json_script "VAR="
assertStdoutEquals "echo baz"
call_json_script "VAR=hell"
assertStdoutEquals "echo baz"
}
test_expr_not() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "not", [ "has", "VAR" ] ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=foo"
assertStdoutEquals "echo baz"
call_json_script
assertStdoutEquals "echo bar"
}
test_expr_and() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "and", [ "eq", "EQVAR", "eqval" ],
[ "regex", "REGEXVAR", "regex..." ]
],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
assertStdoutEquals "echo bar"
call_json_script "EQVAR=foo"
assertStdoutEquals "echo baz"
call_json_script "REGEXVAR=regex***"
assertStdoutEquals "echo baz"
call_json_script
assertStdoutEquals "echo baz"
}
test_expr_or() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "or", [ "not", [ "eq", "EQVAR", "eqval" ] ],
[ "regex", "REGEXVAR", [ "regexva.[0-9]", "regexva.[a-z]" ] ]
],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "EQVAR=eqval" "REGEXVAR=regexval1"
assertStdoutEquals "echo bar"
call_json_script "EQVAR=neq" "REGEXVAR=sxc"
assertStdoutEquals "echo bar"
call_json_script "REGEXVAR=sxc"
assertStdoutEquals "echo bar"
call_json_script "EQVAR=foo"
assertStdoutEquals "echo bar"
call_json_script
assertStdoutEquals "echo bar"
call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
assertStdoutEquals "echo baz"
}
test_expr_isdir() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "isdir", "%VAR%" ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=/"
assertStdoutEquals "echo bar"
call_json_script "VAR=$(mktemp -u)"
assertStdoutEquals "echo baz"
call_json_script
assertStdoutEquals "echo baz"
}
test_cmd_case() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "case", "CASEVAR", {
"0": [ "echo", "foo" ],
"1": [
[ "echo", "bar" ],
[ "echo", "baz" ]
],
"%VAR%": [ "echo", "quz" ]
} ]
]
EOF
call_json_script "CASEVAR=0"
assertStdoutEquals "echo foo"
call_json_script "CASEVAR=1"
assertStdoutEquals "echo bar
echo baz"
call_json_script "CASEVAR=%VAR%"
assertStdoutEquals "echo quz"
call_json_script "CASEVAR="
assertStdoutEquals ""
call_json_script
assertStdoutEquals ""
call_json_script "CASEVAR=xxx" "VAR=xxx"
assertStdoutEquals ""
}
test_cmd_if() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "if",
[ "eq", "VAR", "foo" ],
[ "echo", "bar" ],
[ "echo", "baz" ]
]
]
EOF
call_json_script "VAR=foo"
assertStdoutEquals "echo bar"
call_json_script "VAR=xxx"
assertStdoutEquals "echo baz"
}
test_cmd_cb() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "exec", "%VAR%", "/%VAS%%%/" ]
]
EOF
call_json_script
assertStdoutEquals "exec /%/"
call_json_script "VAR="
assertStdoutEquals "exec /%/"
call_json_script "VAR=qux" "VAS=3"
assertStdoutEquals "exec qux /3%/"
}
test_cmd_return() {
cat >"$JSON_SCRIPT" <<-EOF
[
[ "heh", "%HEHVAR%" ],
[ "%VAR%", "%VAR%" ],
[ "return" ],
[ "exec_non_reachable", "Arghhh" ]
]
EOF
call_json_script "HEHVAR=dude" "VAR=ow"
assertStdoutEquals "heh dude
%VAR% ow"
}
. ./shunit2

View File

@@ -16,11 +16,11 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <libubox/uloop.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "uloop.h"
#include "runqueue.h"
static struct runqueue q;

1067
3P/libubox/examples/shunit2 Normal file

File diff suppressed because it is too large Load Diff