* g++.dg/cpp0x/gen-attrs-42.C: Add x86_64-*-* target.
[official-gcc.git] / libcc1 / connection.cc
blob3e57bbc0efe994ca7ca006ea4507e6712eb9c510
1 /* Connect implementation
2 Copyright (C) 2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <cc1plugin-config.h>
21 #include <string>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #include "marshall.hh"
26 #include "connection.hh"
27 #include "rpc.hh"
29 cc1_plugin::connection::~connection ()
33 void
34 cc1_plugin::connection::print (const char *)
38 cc1_plugin::status
39 cc1_plugin::connection::send (char c)
41 if (write (m_fd, &c, 1) != 1)
42 return FAIL;
43 return OK;
46 cc1_plugin::status
47 cc1_plugin::connection::send (const void *buf, int len)
49 if (write (m_fd, buf, len) != len)
50 return FAIL;
51 return OK;
54 cc1_plugin::status
55 cc1_plugin::connection::require (char c)
57 char result;
59 if (read (m_fd, &result, 1) != 1
60 || result != c)
61 return FAIL;
63 return OK;
66 cc1_plugin::status
67 cc1_plugin::connection::get (void *buf, int len)
69 if (read (m_fd, buf, len) != len)
70 return FAIL;
71 return OK;
74 cc1_plugin::status
75 cc1_plugin::connection::do_wait (bool want_result)
77 while (true)
79 char result;
80 fd_set read_set;
82 FD_ZERO (&read_set);
83 FD_SET (m_fd, &read_set);
84 if (m_aux_fd != -1)
85 FD_SET (m_aux_fd, &read_set);
87 int nfds = select (FD_SETSIZE, &read_set, NULL, NULL, NULL);
88 if (nfds == -1)
90 if (errno == EINTR)
91 continue;
92 return FAIL;
95 // We have to check the stderr fd first, to avoid a possible
96 // blocking scenario when do_wait is called reentrantly. In
97 // such a call, if we handle the primary fd first, then we may
98 // re-enter this function, read from gcc's stderr, causing the
99 // outer invocation of this function to block when trying to
100 // read.
101 if (m_aux_fd != -1 && FD_ISSET (m_aux_fd, &read_set))
103 char buf[1024];
104 int n = read (m_aux_fd, buf, sizeof (buf) - 1);
105 if (n < 0)
106 return FAIL;
107 if (n > 0)
109 buf[n] = '\0';
110 print (buf);
114 if (FD_ISSET (m_fd, &read_set))
116 int n = read (m_fd, &result, 1);
117 if (n == 0)
118 return want_result ? FAIL : OK;
119 if (n != 1)
120 return FAIL;
122 switch (result)
124 case 'R':
125 // The reply is ready; the caller will unmarshall it.
126 return want_result ? OK : FAIL;
128 case 'Q':
129 // While waiting for a reply, the other side made a method
130 // call.
132 // Use an argument_wrapper here to simplify management
133 // of the string's lifetime.
134 argument_wrapper<char *> method_name;
136 if (!method_name.unmarshall (this))
137 return FAIL;
139 callback_ftype *callback
140 = m_callbacks.find_callback (method_name);
141 // The call to CALLBACK is where we may end up in a
142 // reentrant call.
143 if (callback == NULL || !callback (this))
144 return FAIL;
146 break;
148 default:
149 return FAIL;