command.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef COMMAND_H
  17. #define COMMAND_H
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. using namespace std;
  22. struct Command
  23. {
  24. explicit Command(const string& prog);
  25. ~Command();
  26. void AddArg(const string& arg);
  27. void AddEnv(const string& name, const string& value);
  28. const char* GetProg() const;
  29. char* const* GetArgv() const;
  30. char* const* GetEnv() const;
  31. string GetCommandline() const;
  32. string prog;
  33. vector<string> args;
  34. map<string,string> env;
  35. };
  36. /**
  37. * Run the command and collect stdout.
  38. * Returns the exit code.
  39. */
  40. string get_command_output(const Command& command, int* err, bool quiet=false);
  41. /**
  42. * Run the command.
  43. * Returns the exit code.
  44. */
  45. int run_command(const Command& command);
  46. // Mac OS doesn't have execvpe. This is the same as execvpe.
  47. int exec_with_path_search(const char* prog, char const* const* argv, char const* const* envp);
  48. #endif // COMMAND_H