1 /*
2 * Copyright (c) 2022 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
16 #include "shell_command.h"
17
18 #include <getopt.h>
19
20 #include "edm_log.h"
21
22 namespace OHOS {
23 namespace EDM {
ShellCommand(int argc,char * argv[],std::string name)24 ShellCommand::ShellCommand(int argc, char *argv[], std::string name)
25 {
26 opterr = 0;
27 argc_ = argc;
28 argv_ = argv;
29 name_ = name;
30
31 if (argc < MIN_ARGUMENT_NUMBER) {
32 cmd_ = "help";
33 return;
34 }
35 cmd_ = argv[1];
36 for (int i = 2; i < argc; i++) {
37 argList_.push_back(argv[i]);
38 }
39 }
40
~ShellCommand()41 ShellCommand::~ShellCommand()
42 {}
43
OnCommand()44 ErrCode ShellCommand::OnCommand()
45 {
46 int result = OHOS::ERR_OK;
47 if (commandMap_.find("help") == commandMap_.end()) {
48 return OHOS::ERR_INVALID_VALUE;
49 }
50
51 auto respond = commandMap_["help"];
52 if (commandMap_.find(cmd_) != commandMap_.end()) {
53 respond = commandMap_[cmd_];
54 }
55 if (respond == nullptr) {
56 resultReceiver_.append(GetCommandErrorMsg());
57 respond = commandMap_["help"];
58 }
59 if (init() == OHOS::ERR_OK) {
60 respond();
61 } else {
62 result = OHOS::ERR_INVALID_VALUE;
63 }
64
65 return result;
66 }
67
ExecCommand()68 std::string ShellCommand::ExecCommand()
69 {
70 int result = CreateCommandMap();
71 if (result != OHOS::ERR_OK) {
72 EDMLOGE("failed to create command map.\n");
73 }
74
75 result = CreateMessageMap();
76 if (result != OHOS::ERR_OK) {
77 EDMLOGE("failed to create message map.\n");
78 }
79
80 result = OnCommand();
81 if (result != OHOS::ERR_OK) {
82 EDMLOGE("failed to execute your command.\n");
83 resultReceiver_ = "error: failed to execute your command.\n";
84 }
85
86 return resultReceiver_;
87 }
88
GetCommandErrorMsg() const89 std::string ShellCommand::GetCommandErrorMsg() const
90 {
91 std::string commandErrorMsg =
92 name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
93
94 return commandErrorMsg;
95 }
96
GetMessageFromCode(const int32_t code) const97 std::string ShellCommand::GetMessageFromCode(const int32_t code) const
98 {
99 EDMLOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
100 EDMLOGI("code = %{public}d", code);
101
102 std::string result = "";
103 if (messageMap_.find(code) != messageMap_.end()) {
104 std::string message = messageMap_.at(code);
105 if (message.size() != 0) {
106 result.append(message + "\n");
107 }
108 }
109 EDMLOGI("result = %{public}s", result.c_str());
110 return result;
111 }
112 } // namespace EDM
113 } // namespace OHOS
114