2016-10-04 00:26:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a host-side tool for validating a given edify script file.
|
|
|
|
*
|
|
|
|
* We used to have edify test cases here, which have been moved to
|
|
|
|
* tests/component/edify_test.cpp.
|
|
|
|
*
|
|
|
|
* Caveat: It doesn't recognize functions defined through updater, which
|
|
|
|
* makes the tool less useful. We should either extend the tool or remove it.
|
|
|
|
*/
|
|
|
|
|
2016-10-04 03:06:46 +02:00
|
|
|
#include <errno.h>
|
2016-10-04 00:26:06 +02:00
|
|
|
#include <stdio.h>
|
2016-10-04 03:06:46 +02:00
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
#include <memory>
|
2016-10-04 03:06:46 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2016-10-04 00:26:06 +02:00
|
|
|
|
|
|
|
#include "expr.h"
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
static void ExprDump(int depth, const std::unique_ptr<Expr>& n, const std::string& script) {
|
2016-10-04 00:26:06 +02:00
|
|
|
printf("%*s", depth*2, "");
|
|
|
|
printf("%s %p (%d-%d) \"%s\"\n",
|
2017-03-06 23:44:59 +01:00
|
|
|
n->name.c_str(), n->fn, n->start, n->end,
|
2016-10-04 03:06:46 +02:00
|
|
|
script.substr(n->start, n->end - n->start).c_str());
|
2017-03-06 23:44:59 +01:00
|
|
|
for (size_t i = 0; i < n->argv.size(); ++i) {
|
2016-10-04 00:26:06 +02:00
|
|
|
ExprDump(depth+1, n->argv[i], script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
RegisterBuiltins();
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage: %s <edify script>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-04 03:06:46 +02:00
|
|
|
std::string buffer;
|
|
|
|
if (!android::base::ReadFileToString(argv[1], &buffer)) {
|
|
|
|
printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
|
2016-10-04 00:26:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
std::unique_ptr<Expr> root;
|
2016-10-04 00:26:06 +02:00
|
|
|
int error_count = 0;
|
2016-10-04 03:06:46 +02:00
|
|
|
int error = parse_string(buffer.data(), &root, &error_count);
|
2016-10-04 00:26:06 +02:00
|
|
|
printf("parse returned %d; %d errors encountered\n", error, error_count);
|
|
|
|
if (error == 0 || error_count > 0) {
|
|
|
|
|
|
|
|
ExprDump(0, root, buffer);
|
|
|
|
|
2016-10-04 03:06:46 +02:00
|
|
|
State state(buffer, nullptr);
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string result;
|
|
|
|
if (!Evaluate(&state, root, &result)) {
|
2016-10-04 00:26:06 +02:00
|
|
|
printf("result was NULL, message is: %s\n",
|
2016-10-04 03:06:46 +02:00
|
|
|
(state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
|
2016-10-04 00:26:06 +02:00
|
|
|
} else {
|
2016-10-12 19:55:04 +02:00
|
|
|
printf("result is [%s]\n", result.c_str());
|
2016-10-04 00:26:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|