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