blob: 908fcf13bdb5ba3ad2eacffd445a9081dd257296 [file] [log] [blame]
Tao Baod770d2e2016-10-03 15:26:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * This is a host-side tool for validating a given edify script file.
19 *
20 * We used to have edify test cases here, which have been moved to
21 * tests/component/edify_test.cpp.
22 *
23 * Caveat: It doesn't recognize functions defined through updater, which
24 * makes the tool less useful. We should either extend the tool or remove it.
25 */
26
Tao Bao59dcb9c2016-10-03 18:06:46 -070027#include <errno.h>
Tao Baod770d2e2016-10-03 15:26:06 -070028#include <stdio.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070029
30#include <string>
31
32#include <android-base/file.h>
Tao Baod770d2e2016-10-03 15:26:06 -070033
34#include "expr.h"
Tao Baod770d2e2016-10-03 15:26:06 -070035
Tao Bao59dcb9c2016-10-03 18:06:46 -070036static void ExprDump(int depth, const Expr* n, const std::string& script) {
Tao Baod770d2e2016-10-03 15:26:06 -070037 printf("%*s", depth*2, "");
Tao Baod770d2e2016-10-03 15:26:06 -070038 printf("%s %p (%d-%d) \"%s\"\n",
39 n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
Tao Bao59dcb9c2016-10-03 18:06:46 -070040 script.substr(n->start, n->end - n->start).c_str());
Tao Baod770d2e2016-10-03 15:26:06 -070041 for (int i = 0; i < n->argc; ++i) {
42 ExprDump(depth+1, n->argv[i], script);
43 }
44}
45
46int main(int argc, char** argv) {
47 RegisterBuiltins();
Tao Baod770d2e2016-10-03 15:26:06 -070048
49 if (argc != 2) {
50 printf("Usage: %s <edify script>\n", argv[0]);
51 return 1;
52 }
53
Tao Bao59dcb9c2016-10-03 18:06:46 -070054 std::string buffer;
55 if (!android::base::ReadFileToString(argv[1], &buffer)) {
56 printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
Tao Baod770d2e2016-10-03 15:26:06 -070057 return 1;
58 }
Tao Baod770d2e2016-10-03 15:26:06 -070059
60 Expr* root;
61 int error_count = 0;
Tao Bao59dcb9c2016-10-03 18:06:46 -070062 int error = parse_string(buffer.data(), &root, &error_count);
Tao Baod770d2e2016-10-03 15:26:06 -070063 printf("parse returned %d; %d errors encountered\n", error, error_count);
64 if (error == 0 || error_count > 0) {
65
66 ExprDump(0, root, buffer);
67
Tao Bao59dcb9c2016-10-03 18:06:46 -070068 State state(buffer, nullptr);
Tianjie Xuaced5d92016-10-12 10:55:04 -070069 std::string result;
70 if (!Evaluate(&state, root, &result)) {
Tao Baod770d2e2016-10-03 15:26:06 -070071 printf("result was NULL, message is: %s\n",
Tao Bao59dcb9c2016-10-03 18:06:46 -070072 (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
Tao Baod770d2e2016-10-03 15:26:06 -070073 } else {
Tianjie Xuaced5d92016-10-12 10:55:04 -070074 printf("result is [%s]\n", result.c_str());
Tao Baod770d2e2016-10-03 15:26:06 -070075 }
76 }
77 return 0;
78}