No Description

expr.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #include "edify/expr.h"
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <memory>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include <android-base/parseint.h>
  27. #include <android-base/stringprintf.h>
  28. #include <android-base/strings.h>
  29. #include "otautil/error_code.h"
  30. // Functions should:
  31. //
  32. // - return a malloc()'d string
  33. // - if Evaluate() on any argument returns nullptr, return nullptr.
  34. static bool BooleanString(const std::string& s) {
  35. return !s.empty();
  36. }
  37. bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result) {
  38. if (result == nullptr) {
  39. return false;
  40. }
  41. std::unique_ptr<Value> v(expr->fn(expr->name.c_str(), state, expr->argv));
  42. if (!v) {
  43. return false;
  44. }
  45. if (v->type != VAL_STRING) {
  46. ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type);
  47. return false;
  48. }
  49. *result = v->data;
  50. return true;
  51. }
  52. Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr) {
  53. return expr->fn(expr->name.c_str(), state, expr->argv);
  54. }
  55. Value* StringValue(const char* str) {
  56. if (str == nullptr) {
  57. return nullptr;
  58. }
  59. return new Value(VAL_STRING, str);
  60. }
  61. Value* StringValue(const std::string& str) {
  62. return StringValue(str.c_str());
  63. }
  64. Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  65. if (argv.empty()) {
  66. return StringValue("");
  67. }
  68. std::string result;
  69. for (size_t i = 0; i < argv.size(); ++i) {
  70. std::string str;
  71. if (!Evaluate(state, argv[i], &str)) {
  72. return nullptr;
  73. }
  74. result += str;
  75. }
  76. return StringValue(result);
  77. }
  78. Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  79. if (argv.size() != 2 && argv.size() != 3) {
  80. state->errmsg = "ifelse expects 2 or 3 arguments";
  81. return nullptr;
  82. }
  83. std::string cond;
  84. if (!Evaluate(state, argv[0], &cond)) {
  85. return nullptr;
  86. }
  87. if (!cond.empty()) {
  88. return EvaluateValue(state, argv[1]);
  89. } else if (argv.size() == 3) {
  90. return EvaluateValue(state, argv[2]);
  91. }
  92. return StringValue("");
  93. }
  94. Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  95. std::string msg;
  96. if (!argv.empty() && Evaluate(state, argv[0], &msg)) {
  97. state->errmsg += msg;
  98. } else {
  99. state->errmsg += "called abort()";
  100. }
  101. return nullptr;
  102. }
  103. Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  104. for (size_t i = 0; i < argv.size(); ++i) {
  105. std::string result;
  106. if (!Evaluate(state, argv[i], &result)) {
  107. return nullptr;
  108. }
  109. if (result.empty()) {
  110. int len = argv[i]->end - argv[i]->start;
  111. state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
  112. return nullptr;
  113. }
  114. }
  115. return StringValue("");
  116. }
  117. Value* SleepFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  118. std::string val;
  119. if (!Evaluate(state, argv[0], &val)) {
  120. return nullptr;
  121. }
  122. int v;
  123. if (!android::base::ParseInt(val.c_str(), &v, 0)) {
  124. return nullptr;
  125. }
  126. sleep(v);
  127. return StringValue(val);
  128. }
  129. Value* StdoutFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  130. for (size_t i = 0; i < argv.size(); ++i) {
  131. std::string v;
  132. if (!Evaluate(state, argv[i], &v)) {
  133. return nullptr;
  134. }
  135. fputs(v.c_str(), stdout);
  136. }
  137. return StringValue("");
  138. }
  139. Value* LogicalAndFn(const char* name, State* state,
  140. const std::vector<std::unique_ptr<Expr>>& argv) {
  141. std::string left;
  142. if (!Evaluate(state, argv[0], &left)) {
  143. return nullptr;
  144. }
  145. if (BooleanString(left)) {
  146. return EvaluateValue(state, argv[1]);
  147. } else {
  148. return StringValue("");
  149. }
  150. }
  151. Value* LogicalOrFn(const char* name, State* state,
  152. const std::vector<std::unique_ptr<Expr>>& argv) {
  153. std::string left;
  154. if (!Evaluate(state, argv[0], &left)) {
  155. return nullptr;
  156. }
  157. if (!BooleanString(left)) {
  158. return EvaluateValue(state, argv[1]);
  159. } else {
  160. return StringValue(left);
  161. }
  162. }
  163. Value* LogicalNotFn(const char* name, State* state,
  164. const std::vector<std::unique_ptr<Expr>>& argv) {
  165. std::string val;
  166. if (!Evaluate(state, argv[0], &val)) {
  167. return nullptr;
  168. }
  169. return StringValue(BooleanString(val) ? "" : "t");
  170. }
  171. Value* SubstringFn(const char* name, State* state,
  172. const std::vector<std::unique_ptr<Expr>>& argv) {
  173. std::string needle;
  174. if (!Evaluate(state, argv[0], &needle)) {
  175. return nullptr;
  176. }
  177. std::string haystack;
  178. if (!Evaluate(state, argv[1], &haystack)) {
  179. return nullptr;
  180. }
  181. std::string result = (haystack.find(needle) != std::string::npos) ? "t" : "";
  182. return StringValue(result);
  183. }
  184. Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  185. std::string left;
  186. if (!Evaluate(state, argv[0], &left)) {
  187. return nullptr;
  188. }
  189. std::string right;
  190. if (!Evaluate(state, argv[1], &right)) {
  191. return nullptr;
  192. }
  193. const char* result = (left == right) ? "t" : "";
  194. return StringValue(result);
  195. }
  196. Value* InequalityFn(const char* name, State* state,
  197. const std::vector<std::unique_ptr<Expr>>& argv) {
  198. std::string left;
  199. if (!Evaluate(state, argv[0], &left)) {
  200. return nullptr;
  201. }
  202. std::string right;
  203. if (!Evaluate(state, argv[1], &right)) {
  204. return nullptr;
  205. }
  206. const char* result = (left != right) ? "t" : "";
  207. return StringValue(result);
  208. }
  209. Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  210. std::unique_ptr<Value> left(EvaluateValue(state, argv[0]));
  211. if (!left) {
  212. return nullptr;
  213. }
  214. return EvaluateValue(state, argv[1]);
  215. }
  216. Value* LessThanIntFn(const char* name, State* state,
  217. const std::vector<std::unique_ptr<Expr>>& argv) {
  218. if (argv.size() != 2) {
  219. state->errmsg = "less_than_int expects 2 arguments";
  220. return nullptr;
  221. }
  222. std::vector<std::string> args;
  223. if (!ReadArgs(state, argv, &args)) {
  224. return nullptr;
  225. }
  226. // Parse up to at least long long or 64-bit integers.
  227. int64_t l_int;
  228. if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
  229. state->errmsg = "failed to parse int in " + args[0];
  230. return nullptr;
  231. }
  232. int64_t r_int;
  233. if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
  234. state->errmsg = "failed to parse int in " + args[1];
  235. return nullptr;
  236. }
  237. return StringValue(l_int < r_int ? "t" : "");
  238. }
  239. Value* GreaterThanIntFn(const char* name, State* state,
  240. const std::vector<std::unique_ptr<Expr>>& argv) {
  241. if (argv.size() != 2) {
  242. state->errmsg = "greater_than_int expects 2 arguments";
  243. return nullptr;
  244. }
  245. std::vector<std::string> args;
  246. if (!ReadArgs(state, argv, &args)) {
  247. return nullptr;
  248. }
  249. // Parse up to at least long long or 64-bit integers.
  250. int64_t l_int;
  251. if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
  252. state->errmsg = "failed to parse int in " + args[0];
  253. return nullptr;
  254. }
  255. int64_t r_int;
  256. if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
  257. state->errmsg = "failed to parse int in " + args[1];
  258. return nullptr;
  259. }
  260. return StringValue(l_int > r_int ? "t" : "");
  261. }
  262. Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
  263. return StringValue(name);
  264. }
  265. // -----------------------------------------------------------------
  266. // the function table
  267. // -----------------------------------------------------------------
  268. static std::unordered_map<std::string, Function> fn_table;
  269. void RegisterFunction(const std::string& name, Function fn) {
  270. fn_table[name] = fn;
  271. }
  272. Function FindFunction(const std::string& name) {
  273. if (fn_table.find(name) == fn_table.end()) {
  274. return nullptr;
  275. } else {
  276. return fn_table[name];
  277. }
  278. }
  279. void RegisterBuiltins() {
  280. RegisterFunction("ifelse", IfElseFn);
  281. RegisterFunction("abort", AbortFn);
  282. RegisterFunction("assert", AssertFn);
  283. RegisterFunction("concat", ConcatFn);
  284. RegisterFunction("is_substring", SubstringFn);
  285. RegisterFunction("stdout", StdoutFn);
  286. RegisterFunction("sleep", SleepFn);
  287. RegisterFunction("less_than_int", LessThanIntFn);
  288. RegisterFunction("greater_than_int", GreaterThanIntFn);
  289. }
  290. // -----------------------------------------------------------------
  291. // convenience methods for functions
  292. // -----------------------------------------------------------------
  293. // Evaluate the expressions in argv, and put the results of strings in args. If any expression
  294. // evaluates to nullptr, return false. Return true on success.
  295. bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
  296. std::vector<std::string>* args) {
  297. return ReadArgs(state, argv, args, 0, argv.size());
  298. }
  299. bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
  300. std::vector<std::string>* args, size_t start, size_t len) {
  301. if (args == nullptr) {
  302. return false;
  303. }
  304. if (start + len > argv.size()) {
  305. return false;
  306. }
  307. for (size_t i = start; i < start + len; ++i) {
  308. std::string var;
  309. if (!Evaluate(state, argv[i], &var)) {
  310. args->clear();
  311. return false;
  312. }
  313. args->push_back(var);
  314. }
  315. return true;
  316. }
  317. // Evaluate the expressions in argv, and put the results of Value* in args. If any expression
  318. // evaluate to nullptr, return false. Return true on success.
  319. bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
  320. std::vector<std::unique_ptr<Value>>* args) {
  321. return ReadValueArgs(state, argv, args, 0, argv.size());
  322. }
  323. bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
  324. std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len) {
  325. if (args == nullptr) {
  326. return false;
  327. }
  328. if (len == 0 || start + len > argv.size()) {
  329. return false;
  330. }
  331. for (size_t i = start; i < start + len; ++i) {
  332. std::unique_ptr<Value> v(EvaluateValue(state, argv[i]));
  333. if (!v) {
  334. args->clear();
  335. return false;
  336. }
  337. args->push_back(std::move(v));
  338. }
  339. return true;
  340. }
  341. // Use printf-style arguments to compose an error message to put into
  342. // *state. Returns nullptr.
  343. Value* ErrorAbort(State* state, const char* format, ...) {
  344. va_list ap;
  345. va_start(ap, format);
  346. android::base::StringAppendV(&state->errmsg, format, ap);
  347. va_end(ap);
  348. return nullptr;
  349. }
  350. Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
  351. std::string err_message;
  352. va_list ap;
  353. va_start(ap, format);
  354. android::base::StringAppendV(&err_message, format, ap);
  355. va_end(ap);
  356. // Ensure that there's exactly one line break at the end of the error message.
  357. state->errmsg = android::base::Trim(err_message) + "\n";
  358. state->cause_code = cause_code;
  359. return nullptr;
  360. }
  361. State::State(const std::string& script, void* cookie)
  362. : script(script), cookie(cookie), error_code(kNoError), cause_code(kNoCause) {}