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