15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include <iostream>
175ccb8f90Sopenharmony_ci#include "power_shell_command.h"
185ccb8f90Sopenharmony_ci
195ccb8f90Sopenharmony_ci#ifndef POWER_SHELL_USER
205ccb8f90Sopenharmony_ci#include <sstream>
215ccb8f90Sopenharmony_ci#include "securec.h"
225ccb8f90Sopenharmony_ci
235ccb8f90Sopenharmony_ciconstexpr int MAX_PARAMETER_COUNT = 10;
245ccb8f90Sopenharmony_ciconstexpr int MAX_PARAMETER_LENGTH = 30;
255ccb8f90Sopenharmony_cistatic void Loop(char* arg0)
265ccb8f90Sopenharmony_ci{
275ccb8f90Sopenharmony_ci    std::cout << "===================Power-Shell===================" << std::endl;
285ccb8f90Sopenharmony_ci    std::cout << "You can run Power-Shell commands here without exiting the process(" << std::endl;
295ccb8f90Sopenharmony_ci    std::cout << "help: to show help message" << std::endl;
305ccb8f90Sopenharmony_ci    std::cout << "exit: to exit this programm" << std::endl;
315ccb8f90Sopenharmony_ci    std::cout << "don't type \"power-shell\" again!" << std::endl;
325ccb8f90Sopenharmony_ci    char* argv[MAX_PARAMETER_COUNT];
335ccb8f90Sopenharmony_ci    for (int i = 0; i < MAX_PARAMETER_COUNT; ++i) {
345ccb8f90Sopenharmony_ci        argv[i] = new char[MAX_PARAMETER_LENGTH];
355ccb8f90Sopenharmony_ci    }
365ccb8f90Sopenharmony_ci    argv[0] = arg0;
375ccb8f90Sopenharmony_ci    std::string input;
385ccb8f90Sopenharmony_ci    std::cout << "--------------------------------------------" << std::endl;
395ccb8f90Sopenharmony_ci    std::cout << std::endl << "power-shell > ";
405ccb8f90Sopenharmony_ci    std::getline(std::cin, input);
415ccb8f90Sopenharmony_ci    while (input != "exit") {
425ccb8f90Sopenharmony_ci        std::stringstream ss(input);
435ccb8f90Sopenharmony_ci        int argc = 1;
445ccb8f90Sopenharmony_ci        int ret = 0;
455ccb8f90Sopenharmony_ci        for (; std::getline(ss, input, ' ') && argc < MAX_PARAMETER_COUNT; ++argc) {
465ccb8f90Sopenharmony_ci            if (input == "exit") {
475ccb8f90Sopenharmony_ci                return;
485ccb8f90Sopenharmony_ci            }
495ccb8f90Sopenharmony_ci            ret = strcpy_s(argv[argc], MAX_PARAMETER_LENGTH, input.c_str());
505ccb8f90Sopenharmony_ci            if (ret != 0) {
515ccb8f90Sopenharmony_ci                std::cout << "invalid input, the string is too long" << std::endl;
525ccb8f90Sopenharmony_ci                break;
535ccb8f90Sopenharmony_ci            }
545ccb8f90Sopenharmony_ci        }
555ccb8f90Sopenharmony_ci        if (ret == 0) {
565ccb8f90Sopenharmony_ci            OHOS::PowerMgr::PowerShellCommand cmd(argc, argv);
575ccb8f90Sopenharmony_ci            std::cout << cmd.ExecCommand();
585ccb8f90Sopenharmony_ci        }
595ccb8f90Sopenharmony_ci        std::cout << "--------------------------------------------" << std::endl;
605ccb8f90Sopenharmony_ci        std::cout << std::endl << "power-shell > ";
615ccb8f90Sopenharmony_ci        std::getline(std::cin, input);
625ccb8f90Sopenharmony_ci    }
635ccb8f90Sopenharmony_ci}
645ccb8f90Sopenharmony_ci#endif
655ccb8f90Sopenharmony_ciint main(int argc, char *argv[])
665ccb8f90Sopenharmony_ci{
675ccb8f90Sopenharmony_ci    if (argc <= 1) {
685ccb8f90Sopenharmony_ci#ifndef POWER_SHELL_USER
695ccb8f90Sopenharmony_ci        Loop(argv[0]);
705ccb8f90Sopenharmony_ci#endif
715ccb8f90Sopenharmony_ci    } else {
725ccb8f90Sopenharmony_ci        OHOS::PowerMgr::PowerShellCommand cmd(argc, argv);
735ccb8f90Sopenharmony_ci        std::cout << cmd.ExecCommand();
745ccb8f90Sopenharmony_ci    }
755ccb8f90Sopenharmony_ci    return 0;
765ccb8f90Sopenharmony_ci}