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 <getopt.h>
19 #include "hilog_wrapper.h"
20
21 namespace OHOS {
22 namespace AAFwk {
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 || argc > MAX_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 {}
42
OnCommand()43 ErrCode ShellCommand::OnCommand()
44 {
45 int result = OHOS::ERR_OK;
46
47 auto respond = commandMap_[cmd_];
48 if (respond == nullptr) {
49 resultReceiver_.append(GetCommandErrorMsg());
50 respond = commandMap_["help"];
51 }
52
53 if (init() == OHOS::ERR_OK) {
54 respond();
55 } else {
56 result = OHOS::ERR_INVALID_VALUE;
57 }
58
59 return result;
60 }
61
ExecCommand()62 std::string ShellCommand::ExecCommand()
63 {
64 int result = CreateCommandMap();
65 if (result != OHOS::ERR_OK) {
66 HILOG_ERROR("failed to create command map.\n");
67 }
68
69 result = CreateMessageMap();
70 if (result != OHOS::ERR_OK) {
71 HILOG_ERROR("failed to create message map.\n");
72 }
73
74 result = OnCommand();
75 if (result != OHOS::ERR_OK) {
76 HILOG_ERROR("failed to execute your command.\n");
77
78 resultReceiver_ = "error: failed to execute your command.\n";
79 }
80
81 return resultReceiver_;
82 }
83
GetCommandErrorMsg() const84 std::string ShellCommand::GetCommandErrorMsg() const
85 {
86 std::string commandErrorMsg =
87 name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
88
89 return commandErrorMsg;
90 }
91
GetUnknownOptionMsg(std::string & unknownOption) const92 std::string ShellCommand::GetUnknownOptionMsg(std::string& unknownOption) const
93 {
94 std::string result = "";
95
96 if (optind < 0 || optind > argc_) {
97 return result;
98 }
99
100 result.append("error: unknown option");
101 result.append(".\n");
102
103 return result;
104 }
105
GetMessageFromCode(const int32_t code) const106 std::string ShellCommand::GetMessageFromCode(const int32_t code) const
107 {
108 HILOG_INFO("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
109 HILOG_INFO("code = %{public}d", code);
110
111 std::string result = "";
112 if (messageMap_.find(code) != messageMap_.end()) {
113 std::string message = messageMap_.at(code);
114 if (message.size() != 0) {
115 result.append(message + "\n");
116 }
117 }
118
119 HILOG_INFO("result = %{public}s", result.c_str());
120
121 return result;
122 }
123 } // namespace AAFwk
124 } // namespace OHOS
125