1545fdf9bSopenharmony_ci/*
2545fdf9bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3545fdf9bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4545fdf9bSopenharmony_ci * you may not use this file except in compliance with the License.
5545fdf9bSopenharmony_ci * You may obtain a copy of the License at
6545fdf9bSopenharmony_ci *
7545fdf9bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8545fdf9bSopenharmony_ci *
9545fdf9bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10545fdf9bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11545fdf9bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12545fdf9bSopenharmony_ci * See the License for the specific language governing permissions and
13545fdf9bSopenharmony_ci * limitations under the License.
14545fdf9bSopenharmony_ci */
15545fdf9bSopenharmony_ci
16545fdf9bSopenharmony_ci#include "shell_command.h"
17545fdf9bSopenharmony_ci
18545fdf9bSopenharmony_ci#include <getopt.h>
19545fdf9bSopenharmony_ci#include "app_log_wrapper.h"
20545fdf9bSopenharmony_ci
21545fdf9bSopenharmony_cinamespace OHOS {
22545fdf9bSopenharmony_cinamespace AppExecFwk {
23545fdf9bSopenharmony_ci
24545fdf9bSopenharmony_ciShellCommand::ShellCommand(int argc, char *argv[], std::string name)
25545fdf9bSopenharmony_ci{
26545fdf9bSopenharmony_ci    opterr = 0;
27545fdf9bSopenharmony_ci    argc_ = argc;
28545fdf9bSopenharmony_ci    argv_ = argv;
29545fdf9bSopenharmony_ci    name_ = name;
30545fdf9bSopenharmony_ci
31545fdf9bSopenharmony_ci    if (argc < MIN_ARGUMENT_NUMBER) {
32545fdf9bSopenharmony_ci        cmd_ = "help";
33545fdf9bSopenharmony_ci        return;
34545fdf9bSopenharmony_ci    }
35545fdf9bSopenharmony_ci    cmd_ = argv[1];
36545fdf9bSopenharmony_ci    for (int i = 2; i < argc; i++) {
37545fdf9bSopenharmony_ci        argList_.push_back(argv[i]);
38545fdf9bSopenharmony_ci    }
39545fdf9bSopenharmony_ci}
40545fdf9bSopenharmony_ci
41545fdf9bSopenharmony_ciShellCommand::~ShellCommand()
42545fdf9bSopenharmony_ci{}
43545fdf9bSopenharmony_ci
44545fdf9bSopenharmony_ciErrCode ShellCommand::OnCommand()
45545fdf9bSopenharmony_ci{
46545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
47545fdf9bSopenharmony_ci
48545fdf9bSopenharmony_ci    auto respond = commandMap_[cmd_];
49545fdf9bSopenharmony_ci    if (respond == nullptr) {
50545fdf9bSopenharmony_ci        resultReceiver_.append(GetCommandErrorMsg());
51545fdf9bSopenharmony_ci        respond = commandMap_["help"];
52545fdf9bSopenharmony_ci    }
53545fdf9bSopenharmony_ci
54545fdf9bSopenharmony_ci    if (Init() == OHOS::ERR_OK) {
55545fdf9bSopenharmony_ci        respond();
56545fdf9bSopenharmony_ci    } else {
57545fdf9bSopenharmony_ci        result = OHOS::ERR_INVALID_VALUE;
58545fdf9bSopenharmony_ci    }
59545fdf9bSopenharmony_ci
60545fdf9bSopenharmony_ci    return result;
61545fdf9bSopenharmony_ci}
62545fdf9bSopenharmony_ci
63545fdf9bSopenharmony_cistd::string ShellCommand::ExecCommand()
64545fdf9bSopenharmony_ci{
65545fdf9bSopenharmony_ci    int result = CreateCommandMap();
66545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
67545fdf9bSopenharmony_ci        APP_LOGE("failed to create command map.\n");
68545fdf9bSopenharmony_ci    }
69545fdf9bSopenharmony_ci
70545fdf9bSopenharmony_ci    result = CreateMessageMap();
71545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
72545fdf9bSopenharmony_ci        APP_LOGE("failed to create message map.\n");
73545fdf9bSopenharmony_ci    }
74545fdf9bSopenharmony_ci
75545fdf9bSopenharmony_ci    result = OnCommand();
76545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
77545fdf9bSopenharmony_ci        APP_LOGE("failed to execute your command.\n");
78545fdf9bSopenharmony_ci
79545fdf9bSopenharmony_ci        resultReceiver_ = "error: failed to execute your command.\n";
80545fdf9bSopenharmony_ci    }
81545fdf9bSopenharmony_ci
82545fdf9bSopenharmony_ci    return resultReceiver_;
83545fdf9bSopenharmony_ci}
84545fdf9bSopenharmony_ci
85545fdf9bSopenharmony_cistd::string ShellCommand::GetCommandErrorMsg() const
86545fdf9bSopenharmony_ci{
87545fdf9bSopenharmony_ci    std::string commandErrorMsg =
88545fdf9bSopenharmony_ci        name_ + ": '" + cmd_ + "' is not a valid " + name_ + " command. See '" + name_ + " help'.\n";
89545fdf9bSopenharmony_ci
90545fdf9bSopenharmony_ci    return commandErrorMsg;
91545fdf9bSopenharmony_ci}
92545fdf9bSopenharmony_ci
93545fdf9bSopenharmony_cistd::string ShellCommand::GetUnknownOptionMsg(std::string &unknownOption) const
94545fdf9bSopenharmony_ci{
95545fdf9bSopenharmony_ci    std::string result = "";
96545fdf9bSopenharmony_ci
97545fdf9bSopenharmony_ci    if (optind < 0 || optind > argc_) {
98545fdf9bSopenharmony_ci        return result;
99545fdf9bSopenharmony_ci    }
100545fdf9bSopenharmony_ci
101545fdf9bSopenharmony_ci    result.append("error: unknown option");
102545fdf9bSopenharmony_ci    result.append(".\n");
103545fdf9bSopenharmony_ci
104545fdf9bSopenharmony_ci    return result;
105545fdf9bSopenharmony_ci}
106545fdf9bSopenharmony_ci
107545fdf9bSopenharmony_cistd::string ShellCommand::GetMessageFromCode(const int32_t code) const
108545fdf9bSopenharmony_ci{
109545fdf9bSopenharmony_ci    APP_LOGI("[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
110545fdf9bSopenharmony_ci    APP_LOGI("code = %{public}d", code);
111545fdf9bSopenharmony_ci
112545fdf9bSopenharmony_ci    std::string result = "";
113545fdf9bSopenharmony_ci    if (messageMap_.find(code) != messageMap_.end()) {
114545fdf9bSopenharmony_ci        std::string message = messageMap_.at(code);
115545fdf9bSopenharmony_ci        if (message.size() != 0) {
116545fdf9bSopenharmony_ci            result.append(STRING_CODE + std::to_string(code) + "\n");
117545fdf9bSopenharmony_ci            result.append(message + "\n");
118545fdf9bSopenharmony_ci        }
119545fdf9bSopenharmony_ci    }
120545fdf9bSopenharmony_ci
121545fdf9bSopenharmony_ci    APP_LOGI("result = %{public}s", result.c_str());
122545fdf9bSopenharmony_ci
123545fdf9bSopenharmony_ci    return result;
124545fdf9bSopenharmony_ci}
125545fdf9bSopenharmony_ci
126545fdf9bSopenharmony_ci}  // namespace AppExecFwk
127545fdf9bSopenharmony_ci}  // namespace OHOS