1 /*
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 
17 #pragma once
18 
19 #include <map>
20 #include <queue>
21 #include <string>
22 #include <variant>
23 #include <vector>
24 
25 #include "builtins.h"
26 #include "keyword_map.h"
27 #include "result.h"
28 #include "subcontext.h"
29 
30 namespace android {
31 namespace init {
32 
33 Result<void> RunBuiltinFunction(const BuiltinFunction& function,
34                                 const std::vector<std::string>& args, const std::string& context);
35 
36 class Command {
37   public:
38     Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
39             int line);
40 
41     Result<void> InvokeFunc(Subcontext* subcontext) const;
42     std::string BuildCommandString() const;
43     Result<void> CheckCommand() const;
44 
line()45     int line() const { return line_; }
46 
47   private:
48     BuiltinFunction func_;
49     bool execute_in_subcontext_;
50     std::vector<std::string> args_;
51     int line_;
52 };
53 
54 using EventTrigger = std::string;
55 using PropertyChange = std::pair<std::string, std::string>;
56 using BuiltinAction = class Action*;
57 
58 class Action {
59   public:
60     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);
63 
64     Result<void> AddCommand(std::vector<std::string>&& args, int line);
65     void AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line);
66     size_t NumCommands() const;
67     void ExecuteOneCommand(std::size_t command) const;
68     void ExecuteAllCommands() const;
69     bool CheckEvent(const EventTrigger& event_trigger) const;
70     bool CheckEvent(const PropertyChange& property_change) const;
71     bool CheckEvent(const BuiltinAction& builtin_action) const;
72     std::string BuildTriggersString() const;
73     void DumpState() const;
74     size_t CheckAllCommands() const;
75 
oneshot()76     bool oneshot() const { return oneshot_; }
filename()77     const std::string& filename() const { return filename_; }
line()78     int line() const { return line_; }
set_function_map(const BuiltinFunctionMap * function_map)79     static void set_function_map(const BuiltinFunctionMap* function_map) {
80         function_map_ = function_map;
81     }
82 
83   private:
84     void ExecuteCommand(const Command& command) const;
85     bool CheckPropertyTriggers(const std::string& name = "",
86                                const std::string& value = "") const;
87 
88     std::map<std::string, std::string> property_triggers_;
89     std::string event_trigger_;
90     std::vector<Command> commands_;
91     bool oneshot_;
92     Subcontext* subcontext_;
93     std::string filename_;
94     int line_;
95     static const BuiltinFunctionMap* function_map_;
96 };
97 
98 }  // namespace init
99 }  // namespace android
100