-
Notifications
You must be signed in to change notification settings - Fork 3
/
ckcpro.w
602 lines (539 loc) · 22.1 KB
/
ckcpro.w
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
#ifndef NOICP
char *protv = " Protocol, 4G(074)";
#endif /* ifndef NOICP */
/* C K C P R O - Kermit Protocol Module in Wart preprocessor notation */
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2021, 2022, 2023 Jeffrey H. Johnson <trnsz@pobox.com>
*
* Copyright (c) 1981-2011,
* Trustees of Columbia University in the City of New York.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* - Neither the name of Columbia University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ckcdeb.h"
#include "ckcker.h"
#define WART 1
#define WARTVER WART
%states ipkt rfile rattr rdata ssinit ssfile ssattr ssdata sseof sseot
%states serve generic get rgen
extern char sstate, *versio, *srvtxt, *cmarg, *cmarg2, *rpar();
extern char data[], filnam[], srvcmd[], ttname[], *srvptr;
extern int pktnum, timint, nfils, hcflg, xflg, speed, flow, mdmtyp;
extern int prvpkt, cxseen, czseen, local;
#ifndef NOSERVER
extern int server;
#endif /* ifndef NOSERVER */
extern int displa, bctu, bctr, quiet;
#ifndef NOSTATS
extern int tsecs;
#endif /* ifndef NOSTATS */
#ifndef NOATTR
extern int atcapu;
#endif /* ifndef NOATTR */
extern int parity, backgrd, nakstate;
extern int putsrv(), puttrm(), putfil(), errpkt();
extern CHAR *rdatap, recpkt[];
extern char *DIRCMD, *DELCMD, *TYPCMD, *SPACMD, *SPACM2, *WHOCMD;
extern struct zattr iattr;
/*
* Local
* variables
*/
static char vstate = 0; /* Saved State */
static char vcmd = 0; /* Saved Command */
int x; /* General-purpose integer */
char *s; /* General-purpose string pointer */
#define input kinput
#define SERVE \
tinit(); BEGIN serve
#ifndef NOSERVER
#define RESUME \
if (server) { SERVE; } else \
{ sleep(2); \
return; }
#else /* ifndef NOSERVER */
#define RESUME { sleep(2); return; }
#endif /* ifndef NOSERVER */
%%
/*
* Protocol entry points, one
* for each start state (sstate)
*/
s { tinit(); /* Do Send command */
if (sinit()) BEGIN ssinit;
else RESUME; }
v { tinit(); BEGIN get; } /* Receive */
r { tinit(); vstate = get; vcmd = 0; \
sipkt('I'); BEGIN ipkt; } /* Get */
c { tinit(); vstate = rgen; vcmd = 'C'; \
sipkt('I'); BEGIN ipkt; } /* Host */
g { tinit(); vstate = rgen; vcmd = 'G'; \
sipkt('I'); BEGIN ipkt; } /* Generic */
x { sleep(1); SERVE; } /* Be a Server */
a { errpkt("User cancel"); /* ABEND: Tell other side. */
return(0); } /* Return from protocol. */
/*
* Dynamic states:
* <current-states>input-character { action }
*/
<rgen,get,serve>S { /* Receive Send-Init packet */
rinit(rdatap); /* Set parameters */
bctu = bctr; /* Switch to agreed-upon block check */
#ifndef NOSTATS
resetc(); /* Reset counters */
rtimer(); /* Reset timer */
#endif /* ifndef NOSTATS */
BEGIN rfile; /* Go into receive-file state */
}
/*
* States in which we get replies back
* from commands sent to a server.
*/
<ipkt>Y { /* Get ack for I-packet */
spar(rdatap); /* Set parameters */
if (vcmd) { /* If sending a generic command */
scmd(vcmd,cmarg); /* Do that */
vcmd = 0; /* and then un-remember it. */
}
if (vstate == get)
srinit(); /* If sending GET command, do that */
BEGIN vstate; /* Switch to desired state */
}
<ipkt>E { /* Ignore Error reply to I packet */
if (vcmd) { /* in case other Kermit doesn't */
scmd(vcmd,cmarg); /* understand I-packets. */
vcmd = 0; /* Otherwise act as above... */
}
if (vstate == get)
srinit();
BEGIN vstate;
}
<get>Y { /* Resend of previous I-pkt ACK, */
srinit(); /* same seq number! */
}
/*
* States in which we're
* being a server
*/
<serve>I { /* Get I-packet */
spar(rdatap); /* Set parameters from it */
ack1(rpar()); /* Respond with our own parameters */
pktnum = 0; /* Reset packet sequence numbers */
prvpkt = -1; /* Stay in server command wait */
}
<serve>R { /* Get Receive-Init */
srvptr = srvcmd; /* Point to server command buffer */
decode(rdatap,putsrv); /* Decode the GET command into it */
cmarg = srvcmd;
nfils = -1; /* Initialize number of files */
if (sinit()) /* Send Send-Init */
BEGIN ssinit; /* If successful, switch state */
else { SERVE; } /* Else back to server command wait */
}
<serve>G { /* Generic server command */
srvptr = srvcmd; /* Point to command buffer */
decode(rdatap,putsrv); /* Decode packet data into it */
putsrv('\0'); /* Insert a couple nulls */
putsrv('\0'); /* for termination */
sstate = srvcmd[0]; /* Set requested start state */
BEGIN generic; /* Switch to generic command state */
}
<serve>C { /* Receive Host command */
srvptr = srvcmd; /* Point to command buffer */
decode(rdatap,putsrv); /* Decode command packet into it */
putsrv('\0'); /* Null-terminate */
if (syscmd(srvcmd,"")) /* Try to execute the command */
BEGIN ssinit; /* If OK, send back its output */
else { /* Otherwise */
errpkt(
"Error"); /* report error */
SERVE; /* & go back to server command wait */
}
}
<serve>. { /* Any other command in this state */
errpkt(
"Error"); /* we don't know about */
SERVE; /* back to server command wait */
}
<generic>C { /* Got REMOTE CWD command */
if (!cwd(srvcmd+1))
errpkt(
"Error"); /* Try to do it */
SERVE; /* Back to server command wait */
}
<generic>D { /* REMOTE DIRECTORY command */
if (syscmd(DIRCMD,srvcmd+2)) /* If it can be done */
BEGIN ssinit; /* send the results back */
else { /* otherwise */
errpkt(
"Error"); /* report failure */
SERVE; /* & return to server command wait */
}
}
<generic>E { /* REMOTE DELETE (Erase) command */
if (syscmd(DELCMD,srvcmd+2)) /* Try to do it */
BEGIN ssinit; /* If OK send results back */
else { /* otherwise */
errpkt(
"Error"); /* report failure */
SERVE; /* & return to server command wait */
}
}
<generic>F { /* FINISH */
ack(); /* Acknowledge */
screen(SCR_TC,0,0l,""); /* Display */
return(0); /* Done */
}
<generic>L { /* BYE (LOGOUT) */
ack(); /* Acknowledge */
ttres(); /* Reset the terminal */
screen(SCR_TC,0,0l,""); /* Display */
return(zkself()); /* Try to log self out */
}
<generic>H { /* REMOTE HELP */
if (sndhlp()) BEGIN ssinit; /* Try to send it */
else { /* If not ok, */
errpkt(
"Error"); /* send error message instead */
SERVE; /* & return to server command wait */
}
}
<generic>T { /* REMOTE TYPE */
if (syscmd(TYPCMD,srvcmd+2)) /* Try */
BEGIN ssinit; /* OK */
else { /* not OK */
errpkt(
"Error"); /* give error message */
SERVE; /* wait for next server command */
}
}
<generic>U { /* REMOTE SPACE */
x = *(srvcmd+1); /* Get area to check */
x = ((x == '\0') || \
(x == SP));
x = (x ? syscmd(SPACMD,"") : \
syscmd(SPACM2,srvcmd+2));
if (x) /* If we got the info */
BEGIN ssinit; /* send it */
else { /* otherwise */
errpkt(
"Error"); /* send error message */
SERVE; /* and await next server command */
}
}
<generic>W { /* REMOTE WHO */
if (syscmd(WHOCMD,srvcmd+2)) /* The now-familiar scenario... */
BEGIN ssinit;
else {
errpkt(
"Error");
SERVE;
}
}
<generic>. { /* Anything else in this state ... */
errpkt(
"Error"); /* Complain */
SERVE; /* & return to server command wait */
}
<rgen>Y { /* Short-Form reply */
decode(rdatap,puttrm); /* in ACK Data field */
RESUME;
}
<rgen,rfile>F { /* File header */
xflg = 0; /* Not screen data */
rcvfil(filnam); /* Figure out local filename */
encstr(filnam); /* Encode it */
ack1(data); /* Send it back in ACK */
initattr(&iattr); /* Clear file attribute structure */
nakstate = 1; /* In this state we can send NAKs */
BEGIN rattr; /* Now expect Attribute packets */
}
<rgen,rfile>X { /* X-packet instead of file header */
xflg = 1; /* Screen data */
ack(); /* Acknowledge the X-packet */
initattr(&iattr); /* Initialize attribute structure */
nakstate = 1; /* Say that we can send NAKs */
BEGIN rattr; /* Expect Attribute packets */
}
<rattr>A { /* Attribute packet */
if (gattr(rdatap,&iattr) == 0) /* Read into attribute structure */
ack(); /* If OK, acknowledge */
else /* If not */
ack1("N"); /* refuse to accept the file */
}
<rattr>D { /* First data packet */
if (xflg) /* If screen data */
x = opent(); /* "open" the screen */
else /* otherwise */
x = opena(filnam,&iattr); /* open the file, with attributes */
if (x) { /* If file was opened ok */
if (
decode(
rdatap,putfil) < 0) { /* decode first data packet */
errpkt(
"Error");
RESUME;
}
ack(); /* acknowledge it */
BEGIN rdata; /* and switch to receive-data state */
} else { /* otherwise */
errpkt(
"Error"); /* send error message */
RESUME; /* and quit. */
}
}
<rfile>B { /* EOT, no more files */
ack(); /* Acknowledge */
#ifndef NOSTATS
tsecs = gtimer(); /* Get timing for statistics */
#endif /* ifndef NOSTATS */
reot(); /* Do EOT things */
RESUME; /* and quit */
}
<rdata>D { /* Data packet */
if (cxseen) /* If file interrupt */
ack1("X"); /* put "X" in ACK */
else if (czseen) /* If file-group interrupt */
ack1("Z"); /* put "Z" in ACK */
else if (
decode(
rdatap,putfil) < 0) { /* Normal case, */
errpkt(
"Error"); /* decode data to file */
RESUME; /* Send ACK if data written */
} else ack(); /* to file OK. */
}
<rdata,rattr>Z { /* End Of File (EOF) Packet */
if (reof(&iattr) < 0) { /* Close & dispose of the file */
errpkt(
"Error"); /* If problem, send error message */
RESUME; /* and quit */
} else { /* otherwise */
ack(); /* acknowledge the EOF packet */
BEGIN rfile; /* and await another file */
}
}
<ssinit>Y { /* ACK for Send-Init */
spar(rdatap); /* set parameters from it */
bctu = bctr; /* switch to agreed-on block check */
x = sfile(xflg); /* Send X or F header packet */
if (x) { /* If the packet was sent OK */
#ifndef NOSTATS
resetc(); /* reset per-transaction counters */
rtimer(); /* reset timers */
#endif /* ifndef NOSTATS */
BEGIN ssfile; /* and switch to receive-file state */
} else { /* otherwise send error msg & quit */
s = xflg ? \
"Error" : \
"Error";
errpkt(s);
RESUME;
}
}
<ssfile>Y { /* ACK for F packet */
srvptr = srvcmd; /* Point to string buffer */
decode(rdatap,putsrv); /* Decode data field, if any */
putsrv('\0'); /* Terminate with null */
#ifndef NOLOGS
#ifdef TLOG
if (*srvcmd) { /* If remote name was recorded */
tlog(
F110," stored as",srvcmd,0); /* Record it in transaction log. */
}
#endif /* ifdef TLOG */
#endif /* ifndef NOLOGS */
#ifndef NOATTR
if (atcapu) { /* If attributes are to be used */
if (sattr(xflg) < 0) { /* set and send them */
errpkt(
"Error"); /* if problem, say so */
RESUME; /* and quit */
} else BEGIN ssattr; /* if ok, switch to attribute state */
} else
#endif /* NOATTR */
if (sdata() < 0) { /* No attributes, send data */
clsif(); /* If not ok, close input file, */
seof(""); /* send EOF packet */
BEGIN sseof; /* and switch to EOF state. */
} else BEGIN ssdata; /* OK, switch to send-data state */
}
<ssattr>Y { /* Got ACK to A packet */
if (rsattr(rdatap) < 0) { /* Was the file refused? */
clsif(); /* yes, close it */
seof("D"); /* send EOF with "discard" code */
BEGIN sseof; /* switch to send-EOF state */
} else if (sdata() < 0) { /* File accepted, send data */
clsif(); /* If problem, close input file */
seof(""); /* send EOF packet */
BEGIN sseof; /* and switch to send-EOF state. */
} else BEGIN ssdata; /* All ok, enter send-data state. */
}
<ssdata>Y { /* Got ACK to Data packet */
if (canned(rdatap)) { /* If file transfer cancelled */
clsif(); /* close input file */
seof("D"); /* send EOF packet w/ Discard code */
BEGIN sseof; /* switch to EOF state */
} else if (sdata() < 0) { /* Not cancelled, send next data */
clsif(); /* If there is a problem close file */
seof(""); /* Send EOF packet */
BEGIN sseof; /* enter send-eof state */
}
}
<sseof>Y { /* Got ACK to EOF */
if (gnfile() > 0) { /* Any more files to send? */
if (sfile(xflg)) /* Yes try sending next file header */
BEGIN ssfile; /* if ok, enter send-file state */
else { /* otherwise */
errpkt(
"Error"); /* send error message */
RESUME; /* and quit */
}
} else { /* No next file */
#ifndef NOSTATS
tsecs = gtimer(); /* get statistics timers */
#endif /* ifndef NOSTATS */
seot(); /* send EOT packet */
BEGIN sseot; /* enter send-eot state */
}
}
<sseot>Y { /* Got ACK to EOT */
RESUME; /* All done, just quit */
}
E { /* Got Error packet, in any state */
ermsg(rdatap); /* Issue message. */
x = quiet; quiet = 1; /* Close files silently, */
clsif(); clsof(1); /* discarding any output file. */
#ifndef NOSTATS
tsecs = gtimer(); /* Get timers */
#endif /* ifndef NOSTATS */
quiet = x; /* restore quiet state */
if (backgrd
#ifndef NOSERVER
&& !server
#endif /* ifndef NOSERVER */
)
fatal("ERR");
RESUME;
}
. { /* Anything not accounted for above */
errpkt("ERROR"); /* Give error message */
RESUME; /* and quit */
}
%%
int
proto()
{
extern int sigint();
int x;
conint(sigint); /* Enable console interrupts */
/*
* Set up the communication
* line for file transfer.
*/
if (local && (speed < 0)) {
screen(
SCR_EM,0,0l,"'set speed' first");
return;
}
x = -1;
if (ttopen(ttname,&x,mdmtyp) < 0) {
#ifdef DEBUG
debug(
F111,"failed: proto ttopen local",ttname,local);
#endif /* ifdef DEBUG */
screen(
SCR_EM,0,0l,"Can't open line");
return;
}
if (x > -1) local = x;
#ifdef DEBUG
debug(
F111,"proto ttopen local",ttname,local);
#endif /* ifdef DEBUG */
x = (local) ? speed : -1;
if (ttpkt(x,flow,parity) < 0) { /* Put line in packet mode, */
screen(
SCR_EM,0,0l,"Can't condition line");
return;
}
#ifndef NOSERVER
if (sstate == 'x') { /* If entering server mode, */
server = 1; /* set flag, */
if (!quiet) {
if (!local) /* and issue appropriate message. */
conol(srvtxt);
else {
conol(
"Starting Server mode on ");
conoll(ttname);
}
}
} else { server = 0; }
#endif /* ifndef NOSERVER */
if (sstate == 'v' && !local && !quiet)
#ifdef NODOHLP
conoll(
"... SEND now.");
#else /* ifdef NODOHLP */
conoll(
"Escape back to your local Kermit and give a SEND command...");
#endif /* ifdef NODOHLP */
if (sstate == 's' && !local && !quiet)
#ifdef NODOHLP
conoll(
"... RECEIVE now.");
#else /* ifdef NODOHLP */
conoll(
"Escape back to your local Kermit and give a RECEIVE command...");
#endif /* ifdef NODOHLP */
sleep(1);
/*
* The 'wart()' function is generated by the wart program. It gets a
* character from the input() routine and then based on that character
* and the current state, selects the appropriate action, according to
* the state table above, which is transformed by the wart program into
* a big case statement. The function is active for one transaction.
*/
wart(); /* Enter the state table switcher. */
#ifndef NOSERVER
if (server) { /* Back from packet protocol. */
server = 0;
if (!quiet) /* Give appropriate message */
conoll(
"Server done");
}
#endif /* ifndef NOSERVER */
ttres();
screen(
SCR_TC,0,0l,""); /* Transaction complete */
}