-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
392 lines (311 loc) · 6.81 KB
/
main.c
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#pragma optimize 79
#pragma lint - 1
#include <Locator.h>
#include <Memory.h>
#include <MiscTool.h>
#include <tcpip.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "connection.h"
#define IncBusy() asm { jsl 0xE10064}
#define DecBusy() asm { jsl 0xE10068}
#define Resched() asm { cop 0x7f}
#define BusyFlag ((byte *)0xE100FFl)
unsigned d_flag = 0;
// startup/shutdown flags.
enum {
kLoaded = 1,
kStarted = 2,
kConnected = 4,
kLoadError = -1,
kVersionError = -2
};
int StartUpTCP(displayPtr fx) {
word status;
word flags = 0;
// TCPIP is an init, not a tool, so it should always
// be loaded.
status = TCPIPStatus();
if (_toolErr) {
LoadOneTool(54, 0x0300);
if (_toolErr == toolVersionErr)
return kVersionError;
if (_toolErr)
return kLoadError;
status = 0;
flags |= kLoaded;
}
// require 3.0b11
if (TCPIPLongVersion() < 0x03006011) {
if (flags & kLoaded)
UnloadOneTool(54);
return kVersionError;
}
if (!status) {
TCPIPStartUp();
if (_toolErr)
return kLoadError;
flags |= kStarted;
}
status = TCPIPGetConnectStatus();
if (!status) {
TCPIPConnect(fx);
flags |= kConnected;
}
return flags;
}
void ShutDownTCP(int flags, Boolean force, displayPtr fx) {
if (flags <= 0)
return;
if (flags & kConnected) {
TCPIPDisconnect(force, fx);
if (_toolErr)
return;
}
if (flags & kStarted) {
TCPIPShutDown();
if (_toolErr)
return;
}
if (flags & kLoaded) {
UnloadOneTool(54);
}
}
// #pragma databank [push | pop] would be nice...
#pragma databank 1
pascal void DisplayCallback(const char *message) {
unsigned length;
if (!d_flag) return;
// message is a p-string.
length = message ? message[0] : 0;
if (!length)
return;
fprintf(stderr, "%.*s\n", length, message + 1);
}
#pragma databank 0
int ConnectLoop(char *host, Word port, Connection *connection) {
LongWord qtick;
ConnectionInit(connection, MMStartUp(), DisplayCallback);
ConnectionOpenC(connection, host, port);
// 30 second timeout.
qtick = GetTick() + 30 * 60;
while (!ConnectionPoll(connection)) {
if (GetTick() >= qtick) {
fputs("Connection timed out.\n", stderr);
IncBusy();
TCPIPAbortTCP(connection->ipid);
TCPIPLogout(connection->ipid);
DecBusy();
return 0;
}
}
if (connection->state != kConnectionStateConnected) {
fprintf(stderr, "Unable to open host: %s:%u\n", host, port);
return 0;
}
return 1;
}
int CloseLoop(Connection *connection) {
ConnectionClose(connection);
while (!ConnectionPoll(connection))
; // wait for it to close.
return 1;
}
static char buffer[512];
int ReadLineSync(word ipid) {
LongWord qtick;
static srBuff sr;
static rlrBuff rlr;
unsigned x;
word terr;
// asm { brk 0xea }
buffer[0] = 0;
qtick = GetTick() + 30 * 60;
for (;;) {
Word terr;
TCPIPPoll();
terr = TCPIPStatusTCP(ipid, &sr);
if (sr.srRcvQueued)
break;
if (terr)
return -1;
if (GetTick() >= qtick) {
fputs("Read timed out.\n", stderr);
return -1;
}
}
for (;;) {
terr = TCPIPReadLineTCP(ipid, "\p\r\n", 0x0000, (Ref)&buffer,
sizeof(buffer) - 1, &rlr);
if (terr) return -1; /* nb - marinetti < 3.0b11 return value inaccurate */
if (!rlr.rlrIsDataFlag) {
if (GetTick() >= qtick) {
fputs("Read timed out.\n", stderr);
return -1;
}
TCPIPPoll();
continue;
}
break;
}
x = rlr.rlrBuffCount;
buffer[x] = 0;
return x;
}
int status(void) {
unsigned x;
if (sscanf(buffer, "%u", &x) == 1) {
if (d_flag) fprintf(stderr, "status: %d\n", x);
return x;
}
return -1;
}
int client(word ipid) {
word terr;
int ok;
/* also checks for 220 header. */
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
ok = status();
if (ok != 220)
return -1;
terr = TCPIPWriteTCP(ipid, "CLIENT dict-iigs\r\n", 18, 1, 0);
if (terr != 0)
fprintf(stderr, "terr: %04x\n", terr);
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
/*
3.6.2. Responses
250 ok (optional timing information here)
*/
ok = status();
if (ok != 250)
return -1;
return 0;
}
int quit(word ipid) {
word terr;
int ok;
terr = TCPIPWriteTCP(ipid, "QUIT\r\n", 6, 1, 0);
if (terr != 0)
fprintf(stderr, "terr: %04x\n", terr);
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
/*
3.9.2. Responses
221 Closing Connection
*/
return 0;
}
int one_def(Word ipid) {
int ok;
for (;;) {
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
ok = status();
if (ok == 250)
return 0;
if (ok != 151)
return -1;
for (;;) {
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
if (buffer[0] == '.') {
fputc('\n', stdout);
break;
}
fputs(buffer, stdout);
fputc('\n', stdout);
}
}
fputc('\n', stdout);
return 0;
}
int define(Word ipid, const char *term, const char *dict) {
word terr;
int ok;
unsigned x;
static char buffer[512];
if (!dict || !*dict)
dict = "!";
if (!term || !*term)
return -1;
x = sprintf(buffer, "DEFINE %s \"%s\"\r\n", dict, term);
terr = TCPIPWriteTCP(ipid, buffer, x, 1, 0);
if (terr != 0)
fprintf(stderr, "terr: %04x\n", terr);
/*
3.2.2. Responses
550 Invalid database, use "SHOW DB" for list of databases
552 No match
150 n definitions retrieved - definitions follow
151 word database name - text follows
250 ok (optional timing information here)
*/
ok = ReadLineSync(ipid);
if (ok < 0)
return ok;
ok = status();
switch (status()) {
default:
return -1;
case 550:
fputs("Invalid database.\n", stderr);
return -1;
case 552:
fputs("No match.\n", stderr);
return 0;
case 150:
return one_def(ipid);
}
}
int main(int argc, char **argv) {
Connection connection;
int mf;
int ok;
word terr;
--argc;
++argv;
d_flag = 0;
for (; argc; --argc, ++argv) {
unsigned char c = **argv;
if (c != '-') break;
if (!strcmp(*argv, "-d")) {
d_flag = 1;
continue;
}
}
if (argc < 1) {
fputs("Usage: dict [-d] word...\n", stderr);
exit(1);
}
mf = StartUpTCP(DisplayCallback);
if (mf < 0) {
fputs("Marinetti 3.0b11 or greater is required.\n", stderr);
exit(1);
}
ok = ConnectLoop("dict.org", 2628, &connection);
if (ok) {
unsigned i;
int ok;
ok = client(connection.ipid);
if (ok == 0) {
for (i = 0; i < argc; ++i) {
ok = define(connection.ipid, argv[i], NULL);
if (ok < 0)
break;
}
}
quit(connection.ipid);
CloseLoop(&connection);
}
ShutDownTCP(mf, false, DisplayCallback);
return 0;
}