blob: e099b58d388d1600642f326f37703c9a94f8eb8a [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
17#ifndef _INIT_ACTION_H
18#define _INIT_ACTION_H
19
20#include <map>
21#include <queue>
22#include <string>
23#include <vector>
24
Tom Cherryb7349902015-08-26 11:43:36 -070025#include "builtins.h"
26#include "init_parser.h"
27#include "keyword_map.h"
28
29class Command {
Tom Cherry012c5732017-04-18 13:21:54 -070030 public:
31 Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryb7349902015-08-26 11:43:36 -070032
33 int InvokeFunc() const;
34 std::string BuildCommandString() const;
Tom Cherryb7349902015-08-26 11:43:36 -070035
Tom Cherry012c5732017-04-18 13:21:54 -070036 int line() const { return line_; }
37
38 private:
Tom Cherryb7349902015-08-26 11:43:36 -070039 BuiltinFunction func_;
40 std::vector<std::string> args_;
Tom Cherryb7349902015-08-26 11:43:36 -070041 int line_;
42};
43
Tom Cherryfa0c21c2015-07-23 17:53:11 -070044class Action {
Tom Cherry012c5732017-04-18 13:21:54 -070045 public:
46 explicit Action(bool oneshot, const std::string& filename, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070047
Tom Cherry012c5732017-04-18 13:21:54 -070048 bool AddCommand(const std::vector<std::string>& args, int line, std::string* err);
49 void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070050 bool InitTriggers(const std::vector<std::string>& args, std::string* err);
51 bool InitSingleTrigger(const std::string& trigger);
52 std::size_t NumCommands() const;
53 void ExecuteOneCommand(std::size_t command) const;
54 void ExecuteAllCommands() const;
55 bool CheckEventTrigger(const std::string& trigger) const;
56 bool CheckPropertyTrigger(const std::string& name,
57 const std::string& value) const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058 std::string BuildTriggersString() const;
59 void DumpState() const;
60
Tom Cherryb7349902015-08-26 11:43:36 -070061 bool oneshot() const { return oneshot_; }
Tom Cherry012c5732017-04-18 13:21:54 -070062 const std::string& filename() const { return filename_; }
63 int line() const { return line_; }
Tom Cherryb7349902015-08-26 11:43:36 -070064 static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
65 function_map_ = function_map;
66 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070067
Tom Cherryb7349902015-08-26 11:43:36 -070068
69private:
Tom Cherryfa0c21c2015-07-23 17:53:11 -070070 void ExecuteCommand(const Command& command) const;
71 bool CheckPropertyTriggers(const std::string& name = "",
72 const std::string& value = "") const;
73 bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
74
75 std::map<std::string, std::string> property_triggers_;
76 std::string event_trigger_;
Tom Cherryb7349902015-08-26 11:43:36 -070077 std::vector<Command> commands_;
78 bool oneshot_;
Tom Cherry012c5732017-04-18 13:21:54 -070079 std::string filename_;
80 int line_;
Tom Cherryb7349902015-08-26 11:43:36 -070081 static const KeywordMap<BuiltinFunction>* function_map_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070082};
83
Tom Cherrycb716f92015-08-11 12:34:22 -070084class Trigger {
85public:
86 virtual ~Trigger() { }
Tom Cherryb7349902015-08-26 11:43:36 -070087 virtual bool CheckTriggers(const Action& action) const = 0;
Tom Cherrycb716f92015-08-11 12:34:22 -070088};
89
Tom Cherryfa0c21c2015-07-23 17:53:11 -070090class ActionManager {
91public:
92 static ActionManager& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070093
94 void AddAction(std::unique_ptr<Action> action);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095 void QueueEventTrigger(const std::string& trigger);
96 void QueuePropertyTrigger(const std::string& name, const std::string& value);
97 void QueueAllPropertyTriggers();
Tom Cherryb7349902015-08-26 11:43:36 -070098 void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070099 void ExecuteOneCommand();
100 bool HasMoreCommands() const;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700101 void DumpState() const;
102
103private:
104 ActionManager();
105
106 ActionManager(ActionManager const&) = delete;
107 void operator=(ActionManager const&) = delete;
108
Tom Cherryb7349902015-08-26 11:43:36 -0700109 std::vector<std::unique_ptr<Action>> actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700110 std::queue<std::unique_ptr<Trigger>> trigger_queue_;
Tom Cherryb7349902015-08-26 11:43:36 -0700111 std::queue<const Action*> current_executing_actions_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700112 std::size_t current_command_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700113};
114
Tom Cherryb7349902015-08-26 11:43:36 -0700115class ActionParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700116 public:
Tom Cherry30a6f272017-04-19 15:31:58 -0700117 ActionParser(ActionManager* action_manager)
118 : action_manager_(action_manager), action_(nullptr) {}
119 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700120 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700121 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700122 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700123
124 private:
Tom Cherry30a6f272017-04-19 15:31:58 -0700125 ActionManager* action_manager_;
Tom Cherryb7349902015-08-26 11:43:36 -0700126 std::unique_ptr<Action> action_;
127};
128
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129#endif