Shell.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  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. /**
  17. * `Shell` provides a collection of functions to use with `Subprocess` that make
  18. * it easier to safely run processes in a unix shell.
  19. *
  20. * Note: use this rarely and carefully. By default you should use `Subprocess`
  21. * with a vector of arguments.
  22. */
  23. #pragma once
  24. #include <string>
  25. #include <vector>
  26. #include <folly/Conv.h>
  27. #include <folly/Format.h>
  28. #include <folly/Range.h>
  29. namespace folly {
  30. /**
  31. * Quotes an argument to make it suitable for use as shell command arguments.
  32. */
  33. std::string shellQuote(StringPiece argument);
  34. namespace detail {
  35. template <typename... Arguments>
  36. std::vector<std::string> shellify(
  37. StringPiece format,
  38. Arguments&&... arguments) {
  39. auto command = sformat(
  40. format,
  41. shellQuote(to<std::string>(std::forward<Arguments>(arguments)))...);
  42. return {"/bin/sh", "-c", command};
  43. }
  44. struct ShellCmdFormat {
  45. StringPiece format;
  46. template <typename... Arguments>
  47. std::vector<std::string> operator()(Arguments&&... arguments) const {
  48. return ::folly::detail::shellify(
  49. format, std::forward<Arguments>(arguments)...);
  50. }
  51. };
  52. } // namespace detail
  53. inline namespace literals {
  54. inline namespace shell_literals {
  55. constexpr detail::ShellCmdFormat operator"" _shellify(
  56. char const* name,
  57. std::size_t length) {
  58. return {folly::StringPiece(name, length)};
  59. }
  60. } // namespace shell_literals
  61. } // namespace literals
  62. /**
  63. * Create argument array for `Subprocess()` for a process running in a
  64. * shell.
  65. *
  66. * The shell to use is always going to be `/bin/sh`.
  67. *
  68. * This is deprecated in favour of the user-defined-literal `_shellify`
  69. * from namespace `folly::shell_literals` because that requires that the format
  70. * string is a compile-time constant which can be inspected during code reviews
  71. */
  72. // clang-format off
  73. template <typename... Arguments>
  74. [[deprecated(
  75. "Use `\"command {} {} ...\"_shellify(argument1, argument2 ...)` from "
  76. "namespace `folly::literals::shell_literals`")]]
  77. std::vector<std::string> shellify(
  78. StringPiece format,
  79. Arguments&&... arguments) {
  80. return detail::shellify(format, std::forward<Arguments>(arguments)...);
  81. }
  82. // clang-format on
  83. } // namespace folly