1 /*
2 * Copyright (c) 2021-2024 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_tag_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 TAG_LOGD(AAFwkTag::AA_TOOL, "start");
26 opterr = 0;
27 argc_ = argc;
28 argv_ = argv;
29 name_ = name;
30
31 if (argc < MIN_ARGUMENT_NUMBER || argc > MAX_ARGUMENT_NUMBER) {
32 cmd_ = "help";
33 return;
34 }
35 if (argv == nullptr) {
36 TAG_LOGE(AAFwkTag::AA_TOOL, "null argv");
37 return;
38 }
39 cmd_ = argv[1];
40 for (int i = 2; i < argc; i++) {
41 argList_.push_back(argv[i]);
42 }
43 TAG_LOGD(AAFwkTag::AA_TOOL, "exit");
44 }
45
~ShellCommand()46 ShellCommand::~ShellCommand()
47 {}
48
OnCommand()49 ErrCode ShellCommand::OnCommand()
50 {
51 TAG_LOGD(AAFwkTag::AA_TOOL, "start");
52 int result = OHOS::ERR_OK;
53
54 auto respond = commandMap_[cmd_];
55 if (respond == nullptr) {
56 resultReceiver_.append(GetCommandErrorMsg());
57 respond = commandMap_["help"];
58 }
59
60 if (init() == OHOS::ERR_OK) {
61 respond();
62 } else {
63 result = OHOS::ERR_INVALID_VALUE;
64 }
65
66 TAG_LOGD(AAFwkTag::AA_TOOL, "end");
67 return result;
68 }
69
ExecCommand()70 std::string ShellCommand::ExecCommand()
71 {
72 TAG_LOGD(AAFwkTag::AA_TOOL, "start");
73 int result = CreateCommandMap();
74 if (result != OHOS::ERR_OK) {
75 TAG_LOGE(AAFwkTag::AA_TOOL, "CreateCommandMap failed\n");
76 }
77
78 result = CreateMessageMap();
79 if (result != OHOS::ERR_OK) {
80 TAG_LOGE(AAFwkTag::AA_TOOL, "CreateMessageMap failed\n");
81 }
82
83 result = OnCommand();
84 if (result != OHOS::ERR_OK) {
85 TAG_LOGE(AAFwkTag::AA_TOOL, "failed to execute your command\n");
86
87 resultReceiver_ = "error: failed to execute your command.\n";
88 }
89
90 return resultReceiver_;
91 TAG_LOGD(AAFwkTag::AA_TOOL, "end");
92 }
93
GetCommandErrorMsg() const94 std::string ShellCommand::GetCommandErrorMsg() const
95 {
96 TAG_LOGD(AAFwkTag::AA_TOOL, "start");
97 std::string commandErrorMsg =
98 name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
99
100 return commandErrorMsg;
101 }
102
GetUnknownOptionMsg(std::string & unknownOption) const103 std::string ShellCommand::GetUnknownOptionMsg(std::string& unknownOption) const
104 {
105 TAG_LOGD(AAFwkTag::AA_TOOL, "start");
106 std::string result = "";
107
108 if (optind < 0 || optind > argc_) {
109 return result;
110 }
111
112 result.append("fail: unknown option");
113 result.append(".\n");
114
115 return result;
116 }
117
GetMessageFromCode(const int32_t code) const118 std::string ShellCommand::GetMessageFromCode(const int32_t code) const
119 {
120 TAG_LOGI(AAFwkTag::AA_TOOL, "code = %{public}d", code);
121
122 std::string result = "";
123 if (messageMap_.find(code) != messageMap_.end()) {
124 std::string message = messageMap_.at(code);
125 if (message.size() != 0) {
126 result.append(message + "\n");
127 }
128 }
129
130 TAG_LOGI(AAFwkTag::AA_TOOL, "result: %{public}s", result.c_str());
131
132 return result;
133 }
134 } // namespace AAFwk
135 } // namespace OHOS
136