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