1/* 2 * Copyright (c) 2021 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 <iostream> 17#include "power_shell_command.h" 18 19#ifndef POWER_SHELL_USER 20#include <sstream> 21#include "securec.h" 22 23constexpr int MAX_PARAMETER_COUNT = 10; 24constexpr int MAX_PARAMETER_LENGTH = 30; 25static void Loop(char* arg0) 26{ 27 std::cout << "===================Power-Shell===================" << std::endl; 28 std::cout << "You can run Power-Shell commands here without exiting the process(" << std::endl; 29 std::cout << "help: to show help message" << std::endl; 30 std::cout << "exit: to exit this programm" << std::endl; 31 std::cout << "don't type \"power-shell\" again!" << std::endl; 32 char* argv[MAX_PARAMETER_COUNT]; 33 for (int i = 0; i < MAX_PARAMETER_COUNT; ++i) { 34 argv[i] = new char[MAX_PARAMETER_LENGTH]; 35 } 36 argv[0] = arg0; 37 std::string input; 38 std::cout << "--------------------------------------------" << std::endl; 39 std::cout << std::endl << "power-shell > "; 40 std::getline(std::cin, input); 41 while (input != "exit") { 42 std::stringstream ss(input); 43 int argc = 1; 44 int ret = 0; 45 for (; std::getline(ss, input, ' ') && argc < MAX_PARAMETER_COUNT; ++argc) { 46 if (input == "exit") { 47 return; 48 } 49 ret = strcpy_s(argv[argc], MAX_PARAMETER_LENGTH, input.c_str()); 50 if (ret != 0) { 51 std::cout << "invalid input, the string is too long" << std::endl; 52 break; 53 } 54 } 55 if (ret == 0) { 56 OHOS::PowerMgr::PowerShellCommand cmd(argc, argv); 57 std::cout << cmd.ExecCommand(); 58 } 59 std::cout << "--------------------------------------------" << std::endl; 60 std::cout << std::endl << "power-shell > "; 61 std::getline(std::cin, input); 62 } 63} 64#endif 65int main(int argc, char *argv[]) 66{ 67 if (argc <= 1) { 68#ifndef POWER_SHELL_USER 69 Loop(argv[0]); 70#endif 71 } else { 72 OHOS::PowerMgr::PowerShellCommand cmd(argc, argv); 73 std::cout << cmd.ExecCommand(); 74 } 75 return 0; 76}