• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 
10 #ifndef GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__
11 #define GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__
12 
13 #ifdef _WIN32
14 #ifndef WIN32_LEAN_AND_MEAN
15 #define WIN32_LEAN_AND_MEAN  // right...
16 #endif
17 #include <windows.h>
18 #else  // _WIN32
19 #include <sys/types.h>
20 #include <unistd.h>
21 #endif  // !_WIN32
22 #include <string>
23 
24 #include "google/protobuf/port.h"
25 
26 // Must be included last.
27 #include "google/protobuf/port_def.inc"
28 
29 namespace google {
30 namespace protobuf {
31 
32 class Message;
33 
34 namespace compiler {
35 
36 // Utility class for launching sub-processes.
37 class PROTOC_EXPORT Subprocess {
38  public:
39   Subprocess();
40   ~Subprocess();
41 
42   enum SearchMode {
43     SEARCH_PATH,  // Use PATH environment variable.
44     EXACT_NAME    // Program is an exact file name; don't use the PATH.
45   };
46 
47   // Start the subprocess.  Currently we don't provide a way to specify
48   // arguments as protoc plugins don't have any.
49   void Start(const std::string& program, SearchMode search_mode);
50 
51   // Serialize the input message and pipe it to the subprocess's stdin, then
52   // close the pipe.  Meanwhile, read from the subprocess's stdout and parse
53   // the data into *output.  All this is done carefully to avoid deadlocks.
54   // Returns true if successful.  On any sort of error, returns false and sets
55   // *error to a description of the problem.
56   bool Communicate(const Message& input, Message* output, std::string* error);
57 
58 #ifdef _WIN32
59   // Given an error code, returns a human-readable error message.  This is
60   // defined here so that CommandLineInterface can share it.
61   static std::string Win32ErrorMessage(DWORD error_code);
62 #endif
63 
64  private:
65 #ifdef _WIN32
66   DWORD process_start_error_;
67   HANDLE child_handle_;
68 
69   // The file handles for our end of the child's pipes.  We close each and
70   // set it to NULL when no longer needed.
71   HANDLE child_stdin_;
72   HANDLE child_stdout_;
73 
74 #else  // _WIN32
75   pid_t child_pid_;
76 
77   // The file descriptors for our end of the child's pipes.  We close each and
78   // set it to -1 when no longer needed.
79   int child_stdin_;
80   int child_stdout_;
81 
82 #endif  // !_WIN32
83 };
84 
85 }  // namespace compiler
86 }  // namespace protobuf
87 }  // namespace google
88 
89 #include "google/protobuf/port_undef.inc"
90 
91 #endif  // GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__
92