Debian Codegraph @

third_party/upb/upbc/subprocess.h

raw
1 /*
2 * Copyright (c) 2009-2022, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 // Shamelessly copied from the protobuf compiler's subprocess.h
29 // except this version passes strings instead of Messages.
30
31 #ifndef THIRD_PARTY_UPB_UPBC_H_
32 #define THIRD_PARTY_UPB_UPBC_H_
33
34 #ifdef _WIN32
35 #ifndef WIN32_LEAN_AND_MEAN
36 #define WIN32_LEAN_AND_MEAN // right...
37 #endif
38 #include <windows.h>
39 #else // _WIN32
40 #include <sys/types.h>
41 #include <unistd.h>
42 #endif // !_WIN32
43 #include <string>
44
45 namespace upbc {
46
47 // Utility class for launching sub-processes.
48 class Subprocess {
49 public:
50 Subprocess();
51 ~Subprocess();
52
53 enum SearchMode {
54 SEARCH_PATH, // Use PATH environment variable.
55 EXACT_NAME // Program is an exact file name; don't use the PATH.
56 };
57
58 // Start the subprocess. Currently we don't provide a way to specify
59 // arguments as protoc plugins don't have any.
60 void Start(const std::string& program, SearchMode search_mode);
61
62 // Pipe the input message to the subprocess's stdin, then close the pipe.
63 // Meanwhile, read from the subprocess's stdout and copy into *output.
64 // All this is done carefully to avoid deadlocks.
65 // Returns true if successful. On any sort of error, returns false and sets
66 // *error to a description of the problem.
67 bool Communicate(const std::string& input_data, std::string* output_data,
68 std::string* error);
69
70 #ifdef _WIN32
71 // Given an error code, returns a human-readable error message. This is
72 // defined here so that CommandLineInterface can share it.
73 static std::string Win32ErrorMessage(DWORD error_code);
74 #endif
75
76 private:
77 #ifdef _WIN32
78 DWORD process_start_error_;
79 HANDLE child_handle_;
80
81 // The file handles for our end of the child's pipes. We close each and
82 // set it to NULL when no longer needed.
83 HANDLE child_stdin_;
84 HANDLE child_stdout_;
85
86 #else // _WIN32
87 pid_t child_pid_;
88
89 // The file descriptors for our end of the child's pipes. We close each and
90 // set it to -1 when no longer needed.
91 int child_stdin_;
92 int child_stdout_;
93
94 #endif // !_WIN32
95 };
96
97 } // namespace upbc
98
99 #endif // THIRD_PARTY_UPB_UPBC_H_