1 /*
2  * Copyright (c) 2022-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 "inputmethod_dump.h"
17 
18 #include <cstdint>
19 #include <functional>
20 #include <list>
21 #include <string>
22 #include <vector>
23 
24 #include "global.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int32_t SUB_CMD_NAME = 0;
29 constexpr int32_t CMD_ONE_PARAM = 1;
30 constexpr const char *CMD_HELP = "-h";
31 constexpr const char *CMD_ALL_DUMP = "-a";
32 static const std::string ILLEGAL_INFO = "input dump parameter error,enter '-h' for usage.\n";
33 
AddDumpAllMethod(const DumpNoParamFunc dumpAllMethod)34 void InputmethodDump::AddDumpAllMethod(const DumpNoParamFunc dumpAllMethod)
35 {
36     if (dumpAllMethod == nullptr) {
37         return;
38     }
39     dumpAllMethod_ = dumpAllMethod;
40 }
41 
Dump(int fd,const std::vector<std::string> & args)42 bool InputmethodDump::Dump(int fd, const std::vector<std::string> &args)
43 {
44     IMSA_HILOGI("InputmethodDump::Dump start.");
45     std::string command = "";
46     if (args.size() == CMD_ONE_PARAM) {
47         command = args.at(SUB_CMD_NAME);
48     } else {
49         ShowIllegalInformation(fd);
50     }
51     if (command == CMD_HELP) {
52         ShowHelp(fd);
53     } else if (command == CMD_ALL_DUMP) {
54         if (dumpAllMethod_ == nullptr) {
55             return false;
56         }
57         dumpAllMethod_(fd);
58     } else {
59         ShowIllegalInformation(fd);
60     }
61     IMSA_HILOGI("InputmethodDump::Dump command=%{public}s.", command.c_str());
62     return true;
63 }
64 
ShowHelp(int fd)65 void InputmethodDump::ShowHelp(int fd)
66 {
67     std::string result;
68     result.append("Usage:dump  <command> [options]\n")
69         .append("Description:\n")
70         .append("-h show help\n")
71         .append("-a dump all input methods\n");
72     dprintf(fd, "%s\n", result.c_str());
73 }
74 
ShowIllegalInformation(int fd)75 void InputmethodDump::ShowIllegalInformation(int fd)
76 {
77     dprintf(fd, "%s\n", ILLEGAL_INFO.c_str());
78 }
79 } // namespace MiscServices
80 } // namespace OHOS