subprocess  0.2.0
Modern subprocess library for c++
basic_types.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifdef _WIN32
3 #include <windows.h>
4 #else
5 #include <unistd.h>
6 #endif
7 
8 #include <map>
9 #include <stdexcept>
10 #include <string>
11 #include <vector>
12 #include <csignal>
13 
14 
15 // Fucking stdout, stderr, stdin are macros. So instead of stdout,...
16 // we will use cin, cout, cerr as variable names
17 
18 
19 namespace subprocess {
20  /* windows doesnt'h have all of these. The numeric values I hope are
21  standardized. Posix specifies the number in the standard so most
22  systems should be fine.
23  */
24 
28  enum SigNum {
29  PSIGHUP = 1,
30  PSIGINT = SIGINT,
31  PSIGQUIT = 3,
32  PSIGILL = SIGILL,
33  PSIGTRAP = 5,
34  PSIGABRT = SIGABRT,
35  PSIGIOT = 6,
36  PSIGBUS = 7,
37  PSIGFPE = SIGFPE,
38  PSIGKILL = 9,
39  PSIGUSR1 = 10,
40  PSIGSEGV = SIGSEGV,
41  PSIGUSR2 = 12,
42  PSIGPIPE = 13,
43  PSIGALRM = 14,
44  PSIGTERM = SIGTERM,
45  PSIGSTKFLT = 16,
46  PSIGCHLD = 17,
47  PSIGCONT = 18,
48  PSIGSTOP = 19,
49  PSIGTSTP = 20,
50  PSIGTTIN = 21,
51  PSIGTTOU = 22,
52  PSIGURG = 23,
53  PSIGXCPU = 24,
54  PSIGXFSZ = 25,
55  PSIGVTALRM = 26,
56  PSIGPROF = 27,
57  PSIGWINCH = 28,
58  PSIGIO = 29
59  };
60 #ifndef _WIN32
61  typedef int PipeHandle;
63 
65  constexpr char kPathDelimiter = ':';
66  // to please windows we can't have this be a constexpr and be standard c++
69 #else
70  typedef HANDLE PipeHandle;
71  typedef DWORD pid_t;
72 
73  constexpr char kPathDelimiter = ';';
74  const PipeHandle kBadPipeValue = INVALID_HANDLE_VALUE;
75 #endif
76  constexpr int kStdInValue = 0;
77  constexpr int kStdOutValue = 1;
78  constexpr int kStdErrValue = 2;
79 
81  constexpr int kBadReturnCode = -1000;
82 
83  typedef std::vector<std::string> CommandLine;
84  typedef std::map<std::string, std::string> EnvMap;
85 
87  enum class PipeOption : int {
88  inherit,
89  cout,
90  cerr,
91 
94  specific,
95  pipe,
96  close
97  };
98 
99  struct SubprocessError : std::runtime_error {
100  using std::runtime_error::runtime_error;
101  };
102 
104  using SubprocessError::SubprocessError;
105  };
106 
108  using SubprocessError::SubprocessError;
109  };
110 
111  // when the API for spawning a process fails. I don't know if this ever
112  // happens in practice.
113  struct SpawnError : OSError {
114  using OSError::OSError;
115  };
116 
118  using SubprocessError::SubprocessError;
122  double timeout;
123 
125  std::string cout;
127  std::string cerr;
128  };
129 
131  using SubprocessError::SubprocessError;
132  // credit for documentation is from python docs. They say it simply
133  // and well.
134 
142  std::string cout;
144  std::string cerr;
145  };
146 
154  int returncode = -1;
156  std::string cout;
158  std::string cerr;
159  explicit operator bool() const {
160  return returncode == 0;
161  }
162  };
163 
164  namespace details {
165  void throw_os_error(const char* function, int errno_code);
166  }
167 }
subprocess::PSIGPROF
@ PSIGPROF
Definition: basic_types.hpp:56
subprocess::PSIGINT
@ PSIGINT
Definition: basic_types.hpp:30
subprocess::PSIGHUP
@ PSIGHUP
Definition: basic_types.hpp:29
subprocess::PSIGQUIT
@ PSIGQUIT
Definition: basic_types.hpp:31
subprocess::CompletedProcess::cerr
std::string cerr
Definition: basic_types.hpp:158
subprocess::PSIGUSR2
@ PSIGUSR2
Definition: basic_types.hpp:41
subprocess::PSIGBUS
@ PSIGBUS
Definition: basic_types.hpp:36
subprocess::kBadPipeValue
const PipeHandle kBadPipeValue
Definition: basic_types.hpp:68
subprocess::TimeoutExpired::command
CommandLine command
Definition: basic_types.hpp:120
subprocess::kBadReturnCode
constexpr int kBadReturnCode
Definition: basic_types.hpp:81
subprocess::PSIGTERM
@ PSIGTERM
Definition: basic_types.hpp:44
subprocess::PSIGSTKFLT
@ PSIGSTKFLT
Definition: basic_types.hpp:45
subprocess::PSIGSTOP
@ PSIGSTOP
Definition: basic_types.hpp:48
subprocess::PipeOption
PipeOption
Definition: basic_types.hpp:87
subprocess::PSIGWINCH
@ PSIGWINCH
Definition: basic_types.hpp:57
subprocess::PSIGSEGV
@ PSIGSEGV
Definition: basic_types.hpp:40
subprocess::PipeHandle
int PipeHandle
Definition: basic_types.hpp:61
subprocess::PipeOption::close
@ close
Troll the child by providing a closed pipe.
subprocess::PSIGTRAP
@ PSIGTRAP
Definition: basic_types.hpp:33
subprocess::PipeOption::pipe
@ pipe
Redirects to a new handle created for you.
subprocess::kStdInValue
constexpr int kStdInValue
Definition: basic_types.hpp:76
subprocess::pid_t
::pid_t pid_t
Definition: basic_types.hpp:62
subprocess::TimeoutExpired::timeout
double timeout
Definition: basic_types.hpp:122
subprocess::CompletedProcess::args
CommandLine args
Definition: basic_types.hpp:152
subprocess::SigNum
SigNum
Definition: basic_types.hpp:28
subprocess::PSIGFPE
@ PSIGFPE
Definition: basic_types.hpp:37
subprocess::PSIGTTOU
@ PSIGTTOU
Definition: basic_types.hpp:51
subprocess::CalledProcessError::cout
std::string cout
Definition: basic_types.hpp:142
subprocess::PSIGCHLD
@ PSIGCHLD
Definition: basic_types.hpp:46
subprocess::PSIGCONT
@ PSIGCONT
Definition: basic_types.hpp:47
subprocess::EnvMap
std::map< std::string, std::string > EnvMap
Definition: basic_types.hpp:84
subprocess::PSIGXFSZ
@ PSIGXFSZ
Definition: basic_types.hpp:54
subprocess::PipeOption::cerr
@ cerr
redirects to stderr
subprocess::CalledProcessError::cmd
CommandLine cmd
Definition: basic_types.hpp:140
subprocess::CompletedProcess::returncode
int returncode
Definition: basic_types.hpp:154
subprocess::CompletedProcess
Definition: basic_types.hpp:148
subprocess::PSIGUSR1
@ PSIGUSR1
Definition: basic_types.hpp:39
subprocess::CalledProcessError::cerr
std::string cerr
Definition: basic_types.hpp:144
subprocess::PSIGABRT
@ PSIGABRT
Definition: basic_types.hpp:34
subprocess::TimeoutExpired::cout
std::string cout
Definition: basic_types.hpp:125
subprocess::CommandNotFoundError
Definition: basic_types.hpp:107
subprocess::PipeOption::cout
@ cout
Redirects to stdout.
subprocess::PipeOption::specific
@ specific
subprocess::SubprocessError
Definition: basic_types.hpp:99
subprocess::TimeoutExpired
Definition: basic_types.hpp:117
subprocess::kPathDelimiter
constexpr char kPathDelimiter
Definition: basic_types.hpp:65
subprocess
Definition: basic_types.hpp:19
subprocess::PSIGVTALRM
@ PSIGVTALRM
Definition: basic_types.hpp:55
subprocess::PSIGKILL
@ PSIGKILL
Definition: basic_types.hpp:38
subprocess::CommandLine
std::vector< std::string > CommandLine
Definition: basic_types.hpp:83
subprocess::PSIGTSTP
@ PSIGTSTP
Definition: basic_types.hpp:49
subprocess::kStdErrValue
constexpr int kStdErrValue
Definition: basic_types.hpp:78
subprocess::PSIGIOT
@ PSIGIOT
Definition: basic_types.hpp:35
subprocess::CalledProcessError::returncode
int returncode
Definition: basic_types.hpp:138
subprocess::TimeoutExpired::cerr
std::string cerr
Definition: basic_types.hpp:127
subprocess::CompletedProcess::cout
std::string cout
Definition: basic_types.hpp:156
subprocess::PipeOption::inherit
@ inherit
Inherits current process handle.
subprocess::PSIGTTIN
@ PSIGTTIN
Definition: basic_types.hpp:50
subprocess::PSIGALRM
@ PSIGALRM
Definition: basic_types.hpp:43
subprocess::PSIGXCPU
@ PSIGXCPU
Definition: basic_types.hpp:53
subprocess::CalledProcessError
Definition: basic_types.hpp:130
subprocess::kStdOutValue
constexpr int kStdOutValue
Definition: basic_types.hpp:77
subprocess::PSIGIO
@ PSIGIO
Definition: basic_types.hpp:58
subprocess::details::throw_os_error
void throw_os_error(const char *function, int errno_code)
subprocess::SpawnError
Definition: basic_types.hpp:113
subprocess::PSIGURG
@ PSIGURG
Definition: basic_types.hpp:52
subprocess::PSIGILL
@ PSIGILL
Definition: basic_types.hpp:32
subprocess::PSIGPIPE
@ PSIGPIPE
Definition: basic_types.hpp:42
subprocess::OSError
Definition: basic_types.hpp:103