1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// CODYlib -*- mode:c++ -*-
// Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
// License: Apache v2.0
// Cody
#include "internal.hh"
// C++
#include <tuple>
// C
#include <cerrno>
#include <cstdlib>
#include <cstring>
// Server code
namespace Cody {
// These do not need to be members
static Resolver *ConnectRequest (Server *, Resolver *,
std::vector<std::string> &words);
static int ModuleRepoRequest (Server *, Resolver *,
std::vector<std::string> &words);
static int ModuleExportRequest (Server *, Resolver *,
std::vector<std::string> &words);
static int ModuleImportRequest (Server *, Resolver *,
std::vector<std::string> &words);
static int ModuleCompiledRequest (Server *, Resolver *,
std::vector<std::string> &words);
static int IncludeTranslateRequest (Server *, Resolver *,
std::vector<std::string> &words);
namespace {
using RequestFn = int (Server *, Resolver *, std::vector<std::string> &);
using RequestPair = std::tuple<char const *, RequestFn *>;
static RequestPair
const requestTable[Detail::RC_HWM] =
{
// Same order as enum RequestCode
RequestPair {u8"HELLO", nullptr},
RequestPair {u8"MODULE-REPO", ModuleRepoRequest},
RequestPair {u8"MODULE-EXPORT", ModuleExportRequest},
RequestPair {u8"MODULE-IMPORT", ModuleImportRequest},
RequestPair {u8"MODULE-COMPILED", ModuleCompiledRequest},
RequestPair {u8"INCLUDE-TRANSLATE", IncludeTranslateRequest},
};
}
Server::Server (Resolver *r)
: resolver (r), direction (READING)
{
PrepareToRead ();
}
Server::Server (Server &&src)
: write (std::move (src.write)),
read (std::move (src.read)),
resolver (src.resolver),
is_connected (src.is_connected),
direction (src.direction)
{
fd.from = src.fd.from;
fd.to = src.fd.to;
}
Server::~Server ()
{
}
Server &Server::operator= (Server &&src)
{
write = std::move (src.write);
read = std::move (src.read);
resolver = src.resolver;
is_connected = src.is_connected;
direction = src.direction;
fd.from = src.fd.from;
fd.to = src.fd.to;
return *this;
}
void Server::DirectProcess (Detail::MessageBuffer &from,
Detail::MessageBuffer &to)
{
read.PrepareToRead ();
std::swap (read, from);
ProcessRequests ();
resolver->WaitUntilReady (this);
write.PrepareToWrite ();
std::swap (to, write);
}
void Server::ProcessRequests (void)
{
std::vector<std::string> words;
direction = PROCESSING;
while (!read.IsAtEnd ())
{
int err = 0;
unsigned ix = Detail::RC_HWM;
if (!read.Lex (words))
{
Assert (!words.empty ());
while (ix--)
{
if (words[0] != std::get<0> (requestTable[ix]))
continue; // not this one
if (ix == Detail::RC_CONNECT)
{
// CONNECT
if (IsConnected ())
err = -1;
else if (auto *r = ConnectRequest (this, resolver, words))
resolver = r;
else
err = -1;
}
else
{
if (!IsConnected ())
err = -1;
else if (int res = (std::get<1> (requestTable[ix])
(this, resolver, words)))
err = res;
}
break;
}
}
if (err || ix >= Detail::RC_HWM)
{
// Some kind of error
std::string msg;
if (err > 0)
msg = u8"error processing '";
else if (ix >= Detail::RC_HWM)
msg = u8"unrecognized '";
else if (IsConnected () && ix == Detail::RC_CONNECT)
msg = u8"already connected '";
else if (!IsConnected () && ix != Detail::RC_CONNECT)
msg = u8"not connected '";
else
msg = u8"malformed '";
read.LexedLine (msg);
msg.append (u8"'");
if (err > 0)
{
msg.append (u8" ");
msg.append (strerror (err));
}
resolver->ErrorResponse (this, std::move (msg));
}
}
}
// Return numeric value of STR as an unsigned. Returns ~0u on error
// (so that value is not representable).
static unsigned ParseUnsigned (std::string &str)
{
char *eptr;
unsigned long val = strtoul (str.c_str (), &eptr, 10);
if (*eptr || unsigned (val) != val)
return ~0u;
return unsigned (val);
}
Resolver *ConnectRequest (Server *s, Resolver *r,
std::vector<std::string> &words)
{
if (words.size () < 3 || words.size () > 4)
return nullptr;
if (words.size () == 3)
words.emplace_back (u8"");
unsigned version = ParseUnsigned (words[1]);
if (version == ~0u)
return nullptr;
return r->ConnectRequest (s, version, words[2], words[3]);
}
int ModuleRepoRequest (Server *s, Resolver *r,std::vector<std::string> &words)
{
if (words.size () != 1)
return -1;
return r->ModuleRepoRequest (s);
}
int ModuleExportRequest (Server *s, Resolver *r, std::vector<std::string> &words)
{
if (words.size () < 2 || words.size () > 3 || words[1].empty ())
return -1;
Flags flags = Flags::None;
if (words.size () == 3)
{
unsigned val = ParseUnsigned (words[2]);
if (val == ~0u)
return -1;
flags = Flags (val);
}
return r->ModuleExportRequest (s, flags, words[1]);
}
int ModuleImportRequest (Server *s, Resolver *r, std::vector<std::string> &words)
{
if (words.size () < 2 || words.size () > 3 || words[1].empty ())
return -1;
Flags flags = Flags::None;
if (words.size () == 3)
{
unsigned val = ParseUnsigned (words[2]);
if (val == ~0u)
return -1;
flags = Flags (val);
}
return r->ModuleImportRequest (s, flags, words[1]);
}
int ModuleCompiledRequest (Server *s, Resolver *r,
std::vector<std::string> &words)
{
if (words.size () < 2 || words.size () > 3 || words[1].empty ())
return -1;
Flags flags = Flags::None;
if (words.size () == 3)
{
unsigned val = ParseUnsigned (words[2]);
if (val == ~0u)
return -1;
flags = Flags (val);
}
return r->ModuleCompiledRequest (s, flags, words[1]);
}
int IncludeTranslateRequest (Server *s, Resolver *r,
std::vector<std::string> &words)
{
if (words.size () < 2 || words.size () > 3 || words[1].empty ())
return -1;
Flags flags = Flags::None;
if (words.size () == 3)
{
unsigned val = ParseUnsigned (words[2]);
if (val == ~0u)
return -1;
flags = Flags (val);
}
return r->IncludeTranslateRequest (s, flags, words[1]);
}
void Server::ErrorResponse (char const *error, size_t elen)
{
write.BeginLine ();
write.AppendWord (u8"ERROR");
write.AppendWord (error, true, elen);
write.EndLine ();
}
void Server::OKResponse ()
{
write.BeginLine ();
write.AppendWord (u8"OK");
write.EndLine ();
}
void Server::ConnectResponse (char const *agent, size_t alen)
{
is_connected = true;
write.BeginLine ();
write.AppendWord (u8"HELLO");
write.AppendInteger (Version);
write.AppendWord (agent, true, alen);
write.EndLine ();
}
void Server::PathnameResponse (char const *cmi, size_t clen)
{
write.BeginLine ();
write.AppendWord (u8"PATHNAME");
write.AppendWord (cmi, true, clen);
write.EndLine ();
}
void Server::BoolResponse (bool truthiness)
{
write.BeginLine ();
write.AppendWord (u8"BOOL");
write.AppendWord (truthiness ? u8"TRUE" : u8"FALSE");
write.EndLine ();
}
}
|