subprocess  0.4.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  // ssize_t is not a standard type and not supported in MSVC
21  typedef intptr_t ssize_t;
22 
23 
24  #ifdef _WIN32
25 
29  constexpr bool kIsWin32 = true;
30  #else
31  constexpr bool kIsWin32 = false;
32  #endif
33  /* windows doesnt'h have all of these. The numeric values I hope are
34  standardized. Posix specifies the number in the standard so most
35  systems should be fine.
36  */
37 
41  enum SigNum {
42  PSIGHUP = 1,
43  PSIGINT = SIGINT,
44  PSIGQUIT = 3,
45  PSIGILL = SIGILL,
46  PSIGTRAP = 5,
47  PSIGABRT = SIGABRT,
48  PSIGIOT = 6,
49  PSIGBUS = 7,
50  PSIGFPE = SIGFPE,
51  PSIGKILL = 9,
52  PSIGUSR1 = 10,
53  PSIGSEGV = SIGSEGV,
54  PSIGUSR2 = 12,
55  PSIGPIPE = 13,
56  PSIGALRM = 14,
57  PSIGTERM = SIGTERM,
58  PSIGSTKFLT = 16,
59  PSIGCHLD = 17,
60  PSIGCONT = 18,
61  PSIGSTOP = 19,
62  PSIGTSTP = 20,
63  PSIGTTIN = 21,
64  PSIGTTOU = 22,
65  PSIGURG = 23,
66  PSIGXCPU = 24,
67  PSIGXFSZ = 25,
68  PSIGVTALRM = 26,
69  PSIGPROF = 27,
70  PSIGWINCH = 28,
71  PSIGIO = 29
72  };
73 #ifndef _WIN32
74  typedef int PipeHandle;
76 
78  constexpr char kPathDelimiter = ':';
79  // to please windows we can't have this be a constexpr and be standard c++
82 #else
83  typedef HANDLE PipeHandle;
84  typedef DWORD pid_t;
85 
86  constexpr char kPathDelimiter = ';';
87  const PipeHandle kBadPipeValue = INVALID_HANDLE_VALUE;
88 #endif
89  constexpr int kStdInValue = 0;
90  constexpr int kStdOutValue = 1;
91  constexpr int kStdErrValue = 2;
92 
94  constexpr int kBadReturnCode = -1000;
95 
96  typedef std::vector<std::string> CommandLine;
97  typedef std::map<std::string, std::string> EnvMap;
98 
100  enum class PipeOption : int {
101  inherit,
102  cout,
103  cerr,
104 
107  specific,
108  pipe,
109  close
110  };
111 
112  struct SubprocessError : std::runtime_error {
113  using std::runtime_error::runtime_error;
114  };
115 
117  using SubprocessError::SubprocessError;
118  };
119 
121  using SubprocessError::SubprocessError;
122  };
123 
124  // when the API for spawning a process fails. I don't know if this ever
125  // happens in practice.
126  struct SpawnError : OSError {
127  using OSError::OSError;
128  };
129 
131  using SubprocessError::SubprocessError;
135  double timeout;
136 
138  std::string cout;
140  std::string cerr;
141  };
142 
144  using SubprocessError::SubprocessError;
145  // credit for documentation is from python docs. They say it simply
146  // and well.
147 
155  std::string cout;
157  std::string cerr;
158  };
159 
167  int returncode = -1;
169  std::string cout;
171  std::string cerr;
172  explicit operator bool() const {
173  return returncode == 0;
174  }
175  };
176 
177  namespace details {
178  void throw_os_error(const char* function, int errno_code);
179  }
180 }
subprocess::PSIGPROF
@ PSIGPROF
Definition: basic_types.hpp:69
subprocess::PSIGINT
@ PSIGINT
Definition: basic_types.hpp:43
subprocess::PSIGHUP
@ PSIGHUP
Definition: basic_types.hpp:42
subprocess::PSIGQUIT
@ PSIGQUIT
Definition: basic_types.hpp:44
subprocess::CompletedProcess::cerr
std::string cerr
Definition: basic_types.hpp:171
subprocess::PSIGUSR2
@ PSIGUSR2
Definition: basic_types.hpp:54
subprocess::PSIGBUS
@ PSIGBUS
Definition: basic_types.hpp:49
subprocess::kBadPipeValue
const PipeHandle kBadPipeValue
Definition: basic_types.hpp:81
subprocess::TimeoutExpired::command
CommandLine command
Definition: basic_types.hpp:133
subprocess::kBadReturnCode
constexpr int kBadReturnCode
Definition: basic_types.hpp:94
subprocess::PSIGTERM
@ PSIGTERM
Definition: basic_types.hpp:57
subprocess::PSIGSTKFLT
@ PSIGSTKFLT
Definition: basic_types.hpp:58
subprocess::PSIGSTOP
@ PSIGSTOP
Definition: basic_types.hpp:61
subprocess::PipeOption
PipeOption
Definition: basic_types.hpp:100
subprocess::PSIGWINCH
@ PSIGWINCH
Definition: basic_types.hpp:70
subprocess::PSIGSEGV
@ PSIGSEGV
Definition: basic_types.hpp:53
subprocess::PipeHandle
int PipeHandle
Definition: basic_types.hpp:74
subprocess::PipeOption::close
@ close
Troll the child by providing a closed pipe.
subprocess::ssize_t
intptr_t ssize_t
Definition: basic_types.hpp:21
subprocess::PSIGTRAP
@ PSIGTRAP
Definition: basic_types.hpp:46
subprocess::PipeOption::pipe
@ pipe
Redirects to a new handle created for you.
subprocess::kStdInValue
constexpr int kStdInValue
Definition: basic_types.hpp:89
subprocess::pid_t
::pid_t pid_t
Definition: basic_types.hpp:75
subprocess::TimeoutExpired::timeout
double timeout
Definition: basic_types.hpp:135
subprocess::CompletedProcess::args
CommandLine args
Definition: basic_types.hpp:165
subprocess::SigNum
SigNum
Definition: basic_types.hpp:41
subprocess::PSIGFPE
@ PSIGFPE
Definition: basic_types.hpp:50
subprocess::PSIGTTOU
@ PSIGTTOU
Definition: basic_types.hpp:64
subprocess::CalledProcessError::cout
std::string cout
Definition: basic_types.hpp:155
subprocess::PSIGCHLD
@ PSIGCHLD
Definition: basic_types.hpp:59
subprocess::PSIGCONT
@ PSIGCONT
Definition: basic_types.hpp:60
subprocess::EnvMap
std::map< std::string, std::string > EnvMap
Definition: basic_types.hpp:97
subprocess::PSIGXFSZ
@ PSIGXFSZ
Definition: basic_types.hpp:67
subprocess::PipeOption::cerr
@ cerr
redirects to stderr
subprocess::CalledProcessError::cmd
CommandLine cmd
Definition: basic_types.hpp:153
subprocess::CompletedProcess::returncode
int returncode
Definition: basic_types.hpp:167
subprocess::CompletedProcess
Definition: basic_types.hpp:161
subprocess::PSIGUSR1
@ PSIGUSR1
Definition: basic_types.hpp:52
subprocess::CalledProcessError::cerr
std::string cerr
Definition: basic_types.hpp:157
subprocess::PSIGABRT
@ PSIGABRT
Definition: basic_types.hpp:47
subprocess::TimeoutExpired::cout
std::string cout
Definition: basic_types.hpp:138
subprocess::CommandNotFoundError
Definition: basic_types.hpp:120
subprocess::PipeOption::cout
@ cout
Redirects to stdout.
subprocess::PipeOption::specific
@ specific
subprocess::SubprocessError
Definition: basic_types.hpp:112
subprocess::TimeoutExpired
Definition: basic_types.hpp:130
subprocess::kIsWin32
constexpr bool kIsWin32
Definition: basic_types.hpp:31
subprocess::kPathDelimiter
constexpr char kPathDelimiter
Definition: basic_types.hpp:78
subprocess
Definition: basic_types.hpp:19
subprocess::PSIGVTALRM
@ PSIGVTALRM
Definition: basic_types.hpp:68
subprocess::PSIGKILL
@ PSIGKILL
Definition: basic_types.hpp:51
subprocess::CommandLine
std::vector< std::string > CommandLine
Definition: basic_types.hpp:96
subprocess::PSIGTSTP
@ PSIGTSTP
Definition: basic_types.hpp:62
subprocess::kStdErrValue
constexpr int kStdErrValue
Definition: basic_types.hpp:91
subprocess::PSIGIOT
@ PSIGIOT
Definition: basic_types.hpp:48
subprocess::CalledProcessError::returncode
int returncode
Definition: basic_types.hpp:151
subprocess::TimeoutExpired::cerr
std::string cerr
Definition: basic_types.hpp:140
subprocess::CompletedProcess::cout
std::string cout
Definition: basic_types.hpp:169
subprocess::PipeOption::inherit
@ inherit
Inherits current process handle.
subprocess::PSIGTTIN
@ PSIGTTIN
Definition: basic_types.hpp:63
subprocess::PSIGALRM
@ PSIGALRM
Definition: basic_types.hpp:56
subprocess::PSIGXCPU
@ PSIGXCPU
Definition: basic_types.hpp:66
subprocess::CalledProcessError
Definition: basic_types.hpp:143
subprocess::kStdOutValue
constexpr int kStdOutValue
Definition: basic_types.hpp:90
subprocess::PSIGIO
@ PSIGIO
Definition: basic_types.hpp:71
subprocess::details::throw_os_error
void throw_os_error(const char *function, int errno_code)
subprocess::SpawnError
Definition: basic_types.hpp:126
subprocess::PSIGURG
@ PSIGURG
Definition: basic_types.hpp:65
subprocess::PSIGILL
@ PSIGILL
Definition: basic_types.hpp:45
subprocess::PSIGPIPE
@ PSIGPIPE
Definition: basic_types.hpp:55
subprocess::OSError
Definition: basic_types.hpp:116