1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "script_instructionhelper.h"
16 #include <dlfcn.h>
17 #include <set>
18 #include "script_basicinstruction.h"
19 #include "script_loadscript.h"
20 #include "script_manager_impl.h"
21 #include "script_registercmd.h"
22 #include "script_updateprocesser.h"
23 #include "script_utils.h"
24
25 using namespace BasicInstruction;
26
27 namespace uscript {
28 static std::set<std::string> g_reservedInstructions = {
29 "LoadScript", "RegisterCmd", "abort", "assert", "concat",
30 "is_substring", "stdout", "sleep", "set_progress", "ui_print",
31 "show_progress"
32 };
33
34 static ScriptInstructionHelper* g_instructionHelper = nullptr;
35
GetBasicInstructionHelper(ScriptManagerImpl * impl)36 ScriptInstructionHelper* ScriptInstructionHelper::GetBasicInstructionHelper(ScriptManagerImpl *impl)
37 {
38 if (g_instructionHelper == nullptr) {
39 if (impl == nullptr) {
40 return nullptr;
41 }
42 g_instructionHelper = new ScriptInstructionHelper(impl);
43 }
44 return g_instructionHelper;
45 }
46
ReleaseBasicInstructionHelper()47 void ScriptInstructionHelper::ReleaseBasicInstructionHelper()
48 {
49 if (g_instructionHelper != nullptr) {
50 delete g_instructionHelper;
51 }
52 g_instructionHelper = nullptr;
53 }
54
~ScriptInstructionHelper()55 ScriptInstructionHelper::~ScriptInstructionHelper()
56 {
57 if (instrLib_ != nullptr) {
58 dlclose(instrLib_);
59 }
60 instrLib_ = nullptr;
61 }
62
RegisterInstructions() const63 int32_t ScriptInstructionHelper::RegisterInstructions() const
64 {
65 scriptManager_->AddInstruction("RegisterCmder", new ScriptRegisterCmd());
66 scriptManager_->AddInstruction("LoadScript", new ScriptLoadScript());
67 scriptManager_->AddInstruction("Stdout", new UScriptInstructionStdout());
68 scriptManager_->AddInstruction("Abort", new UScriptInstructionAbort());
69 scriptManager_->AddInstruction("Assert", new UScriptInstructionAssert());
70 scriptManager_->AddInstruction("Sleep", new UScriptInstructionSleep());
71 scriptManager_->AddInstruction("Concat", new UScriptInstructionConcat());
72 scriptManager_->AddInstruction("IsSubString", new UScriptInstructionIsSubString());
73 scriptManager_->AddInstruction("set_progress", new UScriptInstructionSetProcess());
74 scriptManager_->AddInstruction("show_progress", new UScriptInstructionShowProcess());
75 scriptManager_->AddInstruction("ui_print", new UScriptInstructionUiPrint());
76 return USCRIPT_SUCCESS;
77 }
78
IsReservedInstruction(const std::string & scriptName) const79 bool ScriptInstructionHelper::IsReservedInstruction(const std::string &scriptName) const
80 {
81 if (g_reservedInstructions.find(scriptName) != g_reservedInstructions.end()) {
82 return true;
83 }
84 return false;
85 }
86
AddScript(const std::string & scriptName,int32_t priority)87 int32_t ScriptInstructionHelper::AddScript(const std::string &scriptName, int32_t priority)
88 {
89 return scriptManager_->AddScript(scriptName, priority);
90 }
91
AddInstruction(const std::string & instrName,const UScriptInstructionPtr instr)92 int32_t ScriptInstructionHelper::AddInstruction(const std::string &instrName, const UScriptInstructionPtr instr)
93 {
94 if (IsReservedInstruction(instrName)) {
95 USCRIPT_LOGE(" %s reserved", instrName.c_str());
96 return USCRIPT_ERROR_REVERED;
97 }
98 return scriptManager_->AddInstruction(instrName, instr);
99 }
100
RegisterUserInstruction(const std::string & libName,const std::string & instrName)101 int32_t ScriptInstructionHelper::RegisterUserInstruction(const std::string& libName,
102 const std::string &instrName)
103 {
104 if (!userInstrLibName_.empty() && userInstrLibName_.compare(libName) != 0) {
105 USCRIPT_LOGE("Lib name must be equal %s ", libName.c_str());
106 return USCRIPT_INVALID_PARAM;
107 }
108
109 userInstrLibName_.assign(libName);
110 uscript::UScriptInstructionFactoryPtr factory = nullptr;
111 if (instrLib_ == nullptr) {
112 char *realPath = realpath(libName.c_str(), NULL);
113 USCRIPT_CHECK(realPath != nullptr, return USCRIPT_INVALID_PARAM, "realPath is NULL");
114 instrLib_ = dlopen(realPath, RTLD_LAZY);
115 free(realPath);
116 }
117 USCRIPT_CHECK(instrLib_ != nullptr, return USCRIPT_INVALID_PARAM,
118 "Fail to dlopen %s ", libName.c_str());
119
120 uscript::UScriptInstructionFactoryPtr (*pGetInstructionFactory)();
121 pGetInstructionFactory = (uscript::UScriptInstructionFactoryPtr(*)())dlsym(instrLib_, "GetInstructionFactory");
122 if (pGetInstructionFactory == nullptr) {
123 USCRIPT_LOGE("Fail to get sym %s ", libName.c_str());
124 return USCRIPT_INVALID_PARAM;
125 }
126 factory = pGetInstructionFactory();
127 USCRIPT_CHECK(factory != nullptr, return USCRIPT_INVALID_PARAM,
128 "Fail to create instruction factory for %s", instrName.c_str());
129
130 // Create instruction and register it
131 UScriptInstructionPtr instr = nullptr;
132 int32_t ret = factory->CreateInstructionInstance(instr, instrName);
133 USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Fail to create instruction for %s", instrName.c_str());
134
135 AddInstruction(instrName, instr);
136 USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Fail to add instruction for %s", instrName.c_str());
137
138 return ret;
139 }
140 } // namespace uscript
141