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