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