Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 27 | #include <errno.h> |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 29 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 30 | #include <memory> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 31 | #include <string> |
| 32 | |
| 33 | #include <android-base/file.h> |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 34 | |
| 35 | #include "expr.h" |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 36 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 37 | static void ExprDump(int depth, const std::unique_ptr<Expr>& n, const std::string& script) { |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 38 | printf("%*s", depth*2, ""); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 39 | printf("%s %p (%d-%d) \"%s\"\n", |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 40 | n->name.c_str(), n->fn, n->start, n->end, |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 41 | script.substr(n->start, n->end - n->start).c_str()); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 42 | for (size_t i = 0; i < n->argv.size(); ++i) { |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 43 | ExprDump(depth+1, n->argv[i], script); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int main(int argc, char** argv) { |
| 48 | RegisterBuiltins(); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 49 | |
| 50 | if (argc != 2) { |
| 51 | printf("Usage: %s <edify script>\n", argv[0]); |
| 52 | return 1; |
| 53 | } |
| 54 | |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 55 | std::string buffer; |
| 56 | if (!android::base::ReadFileToString(argv[1], &buffer)) { |
| 57 | printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno)); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 58 | return 1; |
| 59 | } |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 60 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 61 | std::unique_ptr<Expr> root; |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 62 | int error_count = 0; |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 63 | int error = parse_string(buffer.data(), &root, &error_count); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 64 | printf("parse returned %d; %d errors encountered\n", error, error_count); |
| 65 | if (error == 0 || error_count > 0) { |
| 66 | |
| 67 | ExprDump(0, root, buffer); |
| 68 | |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 69 | State state(buffer, nullptr); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 70 | std::string result; |
| 71 | if (!Evaluate(&state, root, &result)) { |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 72 | printf("result was NULL, message is: %s\n", |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 73 | (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str())); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 74 | } else { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 75 | printf("result is [%s]\n", result.c_str()); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return 0; |
| 79 | } |