-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnda.c
711 lines (583 loc) · 14.7 KB
/
nda.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#pragma nda NDAOpen NDAClose NDAAction NDAInit 15 0xffff "--Dictionary\\H**"
#pragma lint - 1
#pragma optimize - 1
#include <Control.h>
#include <Desk.h>
#include <Event.h>
#include <GSOS.h>
#include <Loader.h>
#include <Locator.h>
#include <Memory.h>
#include <Resources.h>
#include <TCPIP.h>
#include <TextEdit.h>
#include <Types.h>
#include <Window.h>
#include <intmath.h>
#include <misctool.h>
#include <quickdraw.h>
#include <lineedit.h>
#include <ctype.h>
#include <stdio.h>
#include "connection.h"
#include "nda.h"
#define TBBlack "\x01" "C" "\x00\x00"
#define TBBlue "\x01" "C" "\x11\x11"
#define TBRed "\x01" "C" "\x44\x44"
unsigned NDAStartUpTools(Word memID, StartStopRecord *ssRef);
void NDAShutDownTools(StartStopRecord *ssRef);
typedef struct NDAResourceCookie {
Word oldPrefs;
Word oldRApp;
Word resFileID;
} NDAResourceCookie;
void NDAResourceRestore(NDAResourceCookie *cookie);
void NDAResourceShutDown(NDAResourceCookie *cookie);
Word NDAResourceStartUp(Word memID, Word access, NDAResourceCookie *cookie);
Word MyID;
Word FlagTCP;
Word ToolsLoaded;
GrafPortPtr MyWindow;
Handle TECtrlHandle;
Handle TextHandle;
LongWord TextHandleSize;
LongWord TextHandleUsed;
static StartStopRecord ss = {0,
0,
0,
0,
4,
{
0x12, 0x0000, /* QD Aux */
0x1b, 0x0000, /* Font Manager */
0x22, 0x0000, /* Text Edit */
0x36, 0x0300, /* TCP */
}
};
static NDAResourceCookie resInfo;
static Connection connection;
enum {
st_none,
st_idle,
st_connect,
st_login,
st_client,
st_define1,
st_define2,
st_define3,
st_quit,
st_force_quit,
st_disconnect,
};
static unsigned st = 0;
static LongWord qtick = 0;
const char *ReqName = "\pTCP/IP~kelvin~dict~";
int define(Word ipid, const char *dict);
pascal void MarinettiCallback(char *str);
void EnableControls(void);
void DisableControls(void);
void AppendText(word length, char *cp) {
Handle h = TextHandle;
LongWord size;
size = TextHandleUsed + length;
if (size > TextHandleSize) {
size += 4095;
size &= ~4095;
if (h) {
HUnlock(h);
SetHandleSize(size, h);
if (_toolErr) return;
HLock(h);
TextHandleSize = size;
} else {
TextHandle = h = NewHandle(size, MyID, attrLocked|attrNoSpec, 0);
if (_toolErr) return;
TextHandleSize = size;
}
}
BlockMove(cp, *h + TextHandleUsed, length);
TextHandleUsed += length;
}
void AppendText2(word length, char *cp) {
unsigned i;
unsigned char c;
unsigned start = 0;
for (i = 0; i < length; ++i) {
c = cp[i];
if (c == '{' || c == '}' || c == 0x01) {
/* flush any pending data */
if (start < i)
AppendText(i - start, cp + start);
if (c == '{') AppendText(4, TBBlue);
if (c == '}') AppendText(4, TBBlack);
start = i + 1;
}
}
if (start < length)
AppendText(length - start, cp + start);
}
void SetText(void) {
longword oldStart, oldEnd;
TESetSelection((Pointer)-1, (Pointer)-1, TECtrlHandle);
TESetText(teDataIsTextBox2|teTextIsPtr, (Ref)*TextHandle, TextHandleUsed, 0, NULL, TECtrlHandle);
TextHandleUsed = 0;
}
static char buffer[512];
void TCPLoop(void) {
static srBuff sr;
static rlrBuff rlr;
Word terr;
unsigned x;
int status;
Word ipid = connection.ipid;
switch (st) {
case st_idle:
if (GetTick() < qtick) break;
case st_force_quit:
TCPIPWriteTCP(ipid, "QUIT\r\n", 6, 1, 0);
qtick = GetTick() + 60 * 30;
st = st_quit;
return;
break;
case st_connect:
x = ConnectionPoll(&connection);
switch (connection.state) {
case kConnectionStateConnected:
MarinettiCallback("\pConnected");
++st;
break;
case kConnectionStateDisconnected:
MarinettiCallback("\pDisconnected");
EnableControls();
st = st_none;
break;
case kConnectionStateError:
MarinettiCallback("\pConnection Error");
EnableControls();
st = st_none;
break;
}
return;
case st_disconnect:
x = ConnectionPoll(&connection);
switch (connection.state) {
case kConnectionStateDisconnected:
case kConnectionStateError:
MarinettiCallback("\pDisconnected");
EnableControls();
st = st_none;
break;
}
return;
redo:
case st_login:
case st_client:
case st_define1:
case st_define2:
case st_define3:
case st_quit:
TCPIPPoll();
sr.srRcvQueued = 0;
terr = TCPIPStatusTCP(ipid, &sr);
if (sr.srRcvQueued == 0) {
if (GetTick() >= qtick) {
/* timeout */
MarinettiCallback("\pTimed out");
ConnectionClose(&connection);
st = st_disconnect;
}
return;
}
/* all data should be \r\n delimited. we want to keep the \r */
terr = TCPIPReadLineTCP(ipid, "\p\n", 0x0000, (Ref)&buffer,
sizeof(buffer) - 2, &rlr);
if (terr == tcperrConClosing) terr = 0;
if (terr) return;
if (!rlr.rlrIsDataFlag)
return;
qtick += 60 * 15; /* bump timeout */
/* ensure a trailing \r and 0 */
x = rlr.rlrBuffCount;
if (x) {
--x;
if (buffer[x] != '\r') {
buffer[x] = '\r';
++x;
}
++x;
}
buffer[x] = 0;
rlr.rlrBuffCount = x;
if (st != st_define3) {
unsigned i;
status = 0;
for (i = 0;; ++i) {
unsigned c = buffer[i];
if (isdigit(c)) {
status *= 10;
status += c - '0';
continue;
}
if (isspace(c) || c == 0)
break;
status = -1;
break;
}
if (i == 0)
status = -1;
}
break;
}
switch (st) {
case st_login:
/* expect a 220 status */
if (status == 220) {
++st;
terr = TCPIPWriteTCP(ipid, "CLIENT dict-nda-iigs\r\n", 22, 1, 0);
} else {
MarinettiCallback("\pLogin Error");
st = st_force_quit;
}
break;
case st_client:
/* expect 250 status */
if (status == 250) {
++st;
/* send define string... */
define(ipid, NULL);
} else {
MarinettiCallback("\pClient Error");
AppendText(4, TBRed);
AppendText(rlr.rlrBuffCount, buffer);
SetText();
st = st_force_quit;
}
break;
case st_define1:
/* expect 550, 552, or 150 status */
if (status == 150) {
++st;
MarinettiCallback("\pReceiving...");
break;
}
if (status == 552) {
MarinettiCallback("\pNo match");
st = st_idle;
} else {
MarinettiCallback("\pServer Error");
AppendText(4, TBRed);
AppendText(rlr.rlrBuffCount, buffer);
SetText();
st = st_idle;
}
EnableControls();
qtick = GetTick() + 60 * 60 * 2; /* 2-minute timeout */
break;
case st_define2:
/* expect 151 */
if (status == 151) {
++st;
} else if (status == 250) {
st = st_idle;
qtick = GetTick() + 60 * 60 * 2; /* 2-minute timeout */
SetText();
EnableControls();
MarinettiCallback("\pConnected");
} else {
MarinettiCallback("\pServer Error");
AppendText(4, TBRed);
AppendText(rlr.rlrBuffCount, buffer);
SetText();
st = st_idle;
}
break;
case st_define3:
/* expect definition text. '.' terminates. */
if (buffer[0] == '.') {
AppendText(rlr.rlrBuffCount - 1, buffer + 1);
if (buffer[1] == '\r') {
--st;
}
} else {
AppendText2(rlr.rlrBuffCount, buffer);
}
goto redo;
break;
case st_quit:
/* expect 221 but doesn't really matter... */
ConnectionClose(&connection);
st = st_disconnect;
MarinettiCallback("\pDisconnecting...");
break;
}
}
// activate/inactivate controls based on Marinetti status
void UpdateStatus(Boolean redraw) {
if (FlagTCP) {
SetInfoRefCon((LongWord) "\pNetwork Connected", MyWindow);
} else {
SetInfoRefCon((LongWord) "\pNetwork Disconnected", MyWindow);
}
if (redraw)
DrawInfoBar(MyWindow);
}
#pragma databank 1
/*
* Watch for TCP status updates.
*/
pascal word HandleRequest(word request, longword dataIn, longword dataOut) {
Word oldRApp;
oldRApp = GetCurResourceApp();
SetCurResourceApp(MyID);
if (request == TCPIPSaysNetworkUp) {
FlagTCP = true;
UpdateStatus(true);
}
if (request == TCPIPSaysNetworkDown) {
FlagTCP = false;
UpdateStatus(true);
}
SetCurResourceApp(oldRApp);
}
pascal void MarinettiCallback(char *str) {
if (MyWindow) {
SetInfoRefCon((LongWord)str, MyWindow);
DrawInfoBar(MyWindow);
}
}
pascal void DrawInfo(Rect *rect, const char *str, GrafPortPtr w) {
SetForeColor(0x00);
SetBackColor(0x0f);
EraseRect(rect);
if (str) {
MoveTo(/*8, 22*/ rect->h1 + 10, rect->v1 + 9);
DrawString(str);
}
}
void DrawWindow(void) { DrawControls(GetPort()); }
#pragma databank 0
void MakeCtlTargetByID(GrafPortPtr window, Long resID) {
CtlRecHndl h = GetCtlHandleFromID(window, resID);
if (h) MakeThisCtlTarget(h);
}
void NDAInit(Word code) {
if (code) {
MyWindow = NULL;
FlagTCP = false;
ToolsLoaded = false;
MyID = MMStartUp();
st = st_none;
} else {
if (ToolsLoaded)
NDAShutDownTools(&ss);
ToolsLoaded = false;
}
}
void NDAClose(void) {
// if running, shut down.
AcceptRequests(ReqName, MyID, NULL);
ConnectionAbort(&connection);
st = st_none;
CloseWindow(MyWindow);
MyWindow = NULL;
TECtrlHandle = NULL;
NDAResourceShutDown(&resInfo);
if (TextHandle) {
DisposeHandle(TextHandle);
TextHandle = NULL;
TextHandleUsed = 0;
TextHandleSize = 0;
}
}
GrafPortPtr NDAOpen(void) {
MyWindow = NULL;
TextHandle = NULL;
TextHandleSize = 0;
TextHandleSize = 0;
st = st_none;
if (!ToolsLoaded) {
if (NDAStartUpTools(MyID, &ss)) {
NDAShutDownTools(&ss);
return NULL;
}
ToolsLoaded = true;
}
if (TCPIPLongVersion() < 0x03006011) {
AlertWindow(awCString, NULL,
(Ref) "24~Marinetti 3.0b11 or newer required.~^Ok");
return NULL;
}
// Check if Marinetti Active.
FlagTCP = TCPIPGetConnectStatus();
if (NDAResourceStartUp(MyID, readEnable, &resInfo)) {
MyWindow = NewWindow2(NULL, 0, DrawWindow, NULL, refIsResource, rWindow,
rWindParam1);
SetInfoDraw(DrawInfo, MyWindow);
UpdateStatus(0);
AcceptRequests(ReqName, MyID, &HandleRequest);
MakeCtlTargetByID(MyWindow, rCtrlLE);
SetSysWindow(MyWindow);
ShowWindow(MyWindow);
SelectWindow(MyWindow);
TECtrlHandle = (Handle)GetCtlHandleFromID(MyWindow, rCtrlTE);
ConnectionInit(&connection, MyID, MarinettiCallback);
NDAResourceRestore(&resInfo);
return MyWindow;
}
return NULL;
}
static char word_to_define[256];
int define(Word ipid, const char *dict) {
word terr;
int ok;
unsigned x;
static char buffer[512];
if (!dict || !*dict)
dict = "!";
x = sprintf(buffer, "DEFINE %s \"%b\"\r\n", dict, word_to_define);
terr = TCPIPWriteTCP(ipid, buffer, x, 1, 0);
return 0;
}
void DoDefine(void) {
unsigned i;
Handle handle;
handle = (Handle)GetCtlHandleFromID(MyWindow, rCtrlTE);
TESetText(teDataIsTextBlock|teTextIsPtr, (Ref)"", 0, 0, NULL, handle);
GetLETextByID(MyWindow, rCtrlLE, (StringPtr)word_to_define);
i = word_to_define[0];
while (i && isspace(word_to_define[i])) --i;
if (!i) return;
word_to_define[0] = i;
word_to_define[i+1] = 0;
/* considerations:
1. is the network connected?
2. is a tcp connection already established?
*/
if (!FlagTCP) {
MarinettiCallback("\pConnecting to network...");
TCPIPConnect(MarinettiCallback);
if (!FlagTCP) return;
}
qtick = GetTick() + 30 * 60;
DisableControls();
switch(st) {
case st_idle:
define(connection.ipid, NULL);
st = st_define1;
break;
default:
ConnectionAbort(&connection);
case st_none:
ConnectionOpenC(&connection, "dict.org", 2628);
st = st_connect;
break;
}
}
void EnableControls(void) {
HiliteCtlByID(noHilite, MyWindow, rCtrlLE);
HiliteCtlByID(noHilite, MyWindow, rCtrlDefine);
}
void DisableControls(void) {
HiliteCtlByID(inactiveHilite, MyWindow, rCtrlLE);
HiliteCtlByID(inactiveHilite, MyWindow, rCtrlDefine);
}
void EditKeys(Word key, Handle target, LongWord targetID) {
LERecHndl le;
if (!target) {
target = (Handle)FindTargetCtl();
if (!target) return;
targetID = GetCtlID((CtlRecHndl)target);
}
le = (LERecHndl)(*(CtlRecHndl)target)->ctlData;
switch (key) {
case 'a':
case 'A':
if (targetID == rCtrlTE) {
LongWord start = 0;
LongWord end = 0;
TEGetTextInfo((Pointer)&end, 1, TECtrlHandle);
TESetSelection((Pointer)start, (Pointer)end, TECtrlHandle);
} else if (targetID == rCtrlLE) {
Word len = LEGetTextLen(le);
LESetSelect(0, len, le);
}
break;
case 'c':
case 'C':
if (targetID == rCtrlTE) {
TECopy(target);
}
if (targetID == rCtrlLE) {
LECopy(le);
}
break;
case 'v':
case 'V':
if (targetID == rCtrlLE)
LEPaste(le);
break;
case 'x':
case 'X':
if (targetID == rCtrlLE)
LECut(le);
break;
case 'k':
if (targetID == rCtrlLE)
LEDelete(le);
break;
}
}
word NDAAction(void *param, int code) {
word eventCode;
static EventRecord event = {0};
Handle target = NULL;
if (code == runAction) {
if (st)
TCPLoop();
return 1;
}
else if (code == eventAction) {
BlockMove((Pointer)param, (Pointer)&event, 16);
event.wmTaskMask = 0x001FFFFF;
eventCode = TaskMasterDA(0, &event);
switch (eventCode) {
case updateEvt:
BeginUpdate(MyWindow);
DrawWindow();
EndUpdate(MyWindow);
break;
case wInControl:
target = (Handle)event.wmTaskData2;
switch ((Word)event.wmTaskData4) {
case rCtrlDefine:
DoDefine();
break;
case rCtrlTE:
case rCtrlLE:
/* check for control-C, control-A */
if (event.what != keyDownEvt) break;
if (!(event.modifiers & 0x0100)) break;
EditKeys(event.message, (Handle)event.wmTaskData2, event.wmTaskData4);
break;
}
break;
case keyDownEvt:
/* line-edit needs to be handled here */
if (!(event.modifiers & 0x0100)) break;
EditKeys(event.message, 0, 0);
break;
}
} else if (code == copyAction) {
EditKeys('c', 0, 0);
return 1; // yes we handled it.
} else if (code == pasteAction) {
EditKeys('v', 0, 0);
return 1; // yes we handled it.
} else if (code == cutAction) {
EditKeys('x', 0, 0);
return 1; // yes we handled it.
} else if (code == clearAction) {
EditKeys('k', 0, 0);
}
return 0;
}