blob: 1534bf987a99986e6c9d83d2f1db844612d42bbd [file] [log] [blame]
Tom Cherryfa0c21c2015-07-23 17:53:11 -07001/*
2 * Copyright (C) 2015 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
Tom Cherry4772f1d2019-07-30 09:34:41 -070017#pragma once
Tom Cherryfa0c21c2015-07-23 17:53:11 -070018
19#include <map>
20#include <queue>
21#include <string>
Tom Cherry26ed9cb2017-04-17 13:25:29 -070022#include <variant>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023#include <vector>
24
Tom Cherryb7349902015-08-26 11:43:36 -070025#include "builtins.h"
Tom Cherryb7349902015-08-26 11:43:36 -070026#include "keyword_map.h"
Tom Cherry557946e2017-08-01 13:50:23 -070027#include "result.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070028#include "subcontext.h"
Tom Cherryb7349902015-08-26 11:43:36 -070029
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070033Result<void> RunBuiltinFunction(const BuiltinFunction& function,
34 const std::vector<std::string>& args, const std::string& context);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070035
Tom Cherryb7349902015-08-26 11:43:36 -070036class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070037 public:
Tom Cherry018a4382018-10-17 11:11:23 -070038 Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070039 int line);
Tom Cherryb7349902015-08-26 11:43:36 -070040
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070041 Result<void> InvokeFunc(Subcontext* subcontext) const;
Tom Cherryb7349902015-08-26 11:43:36 -070042 std::string BuildCommandString() const;
Tom Cherry4772f1d2019-07-30 09:34:41 -070043 Result<void> CheckCommand() const;
Tom Cherryb7349902015-08-26 11:43:36 -070044
Tom Cherry012c5732017-04-18 13:21:54 -070045 int line() const { return line_; }
46
47 private:
Tom Cherryb7349902015-08-26 11:43:36 -070048 BuiltinFunction func_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070049 bool execute_in_subcontext_;
Tom Cherryb7349902015-08-26 11:43:36 -070050 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070051 int line_;
52};
53
Tom Cherry26ed9cb2017-04-17 13:25:29 -070054using EventTrigger = std::string;
55using PropertyChange = std::pair<std::string, std::string>;
56using BuiltinAction = class Action*;
57
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070059 public:
Tom Cherry9cbf5702018-02-13 16:24:51 -080060 Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
61 const std::string& event_trigger,
62 const std::map<std::string, std::string>& property_triggers);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070063
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070064 Result<void> AddCommand(std::vector<std::string>&& args, int line);
Tom Cherry018a4382018-10-17 11:11:23 -070065 void AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line);
Tom Cherry4772f1d2019-07-30 09:34:41 -070066 size_t NumCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070067 void ExecuteOneCommand(std::size_t command) const;
68 void ExecuteAllCommands() const;
Tom Cherry26ed9cb2017-04-17 13:25:29 -070069 bool CheckEvent(const EventTrigger& event_trigger) const;
70 bool CheckEvent(const PropertyChange& property_change) const;
71 bool CheckEvent(const BuiltinAction& builtin_action) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070072 std::string BuildTriggersString() const;
73 void DumpState() const;
Tom Cherry4772f1d2019-07-30 09:34:41 -070074 size_t CheckAllCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070075
Tom Cherryb7349902015-08-26 11:43:36 -070076 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070077 const std::string& filename() const { return filename_; }
78 int line() const { return line_; }
Tom Cherryd52a5b32019-07-22 16:05:36 -070079 static void set_function_map(const BuiltinFunctionMap* function_map) {
Tom Cherryb7349902015-08-26 11:43:36 -070080 function_map_ = function_map;
81 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070082
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070083 private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070084 void ExecuteCommand(const Command& command) const;
85 bool CheckPropertyTriggers(const std::string& name = "",
86 const std::string& value = "") const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070087
88 std::map<std::string, std::string> property_triggers_;
89 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070090 std::vector<Command> commands_;
91 bool oneshot_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070092 Subcontext* subcontext_;
Tom Cherry012c5732017-04-18 13:21:54 -070093 std::string filename_;
94 int line_;
Tom Cherryd52a5b32019-07-22 16:05:36 -070095 static const BuiltinFunctionMap* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070096};
97
Tom Cherry81f5d3e2017-06-22 12:53:17 -070098} // namespace init
99} // namespace android