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