blob: 16ad2ab719f17324a99838334e46283bf03d9864 [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
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include "expr.h"
32#include "parser.h"
33
34void ExprDump(int depth, Expr* n, char* script) {
35 printf("%*s", depth*2, "");
36 char temp = script[n->end];
37 script[n->end] = '\0';
38 printf("%s %p (%d-%d) \"%s\"\n",
39 n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
40 script+n->start);
41 script[n->end] = temp;
42 for (int i = 0; i < n->argc; ++i) {
43 ExprDump(depth+1, n->argv[i], script);
44 }
45}
46
47int main(int argc, char** argv) {
48 RegisterBuiltins();
49 FinishRegistration();
50
51 if (argc != 2) {
52 printf("Usage: %s <edify script>\n", argv[0]);
53 return 1;
54 }
55
56 FILE* f = fopen(argv[1], "r");
57 if (f == NULL) {
58 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
59 return 1;
60 }
61 char buffer[8192];
62 int size = fread(buffer, 1, 8191, f);
63 fclose(f);
64 buffer[size] = '\0';
65
66 Expr* root;
67 int error_count = 0;
68 int error = parse_string(buffer, &root, &error_count);
69 printf("parse returned %d; %d errors encountered\n", error, error_count);
70 if (error == 0 || error_count > 0) {
71
72 ExprDump(0, root, buffer);
73
74 State state;
75 state.cookie = NULL;
76 state.script = buffer;
77 state.errmsg = NULL;
78
79 char* result = Evaluate(&state, root);
80 if (result == NULL) {
81 printf("result was NULL, message is: %s\n",
82 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
83 free(state.errmsg);
84 } else {
85 printf("result is [%s]\n", result);
86 }
87 }
88 return 0;
89}