-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrt11modm.c
352 lines (319 loc) · 7.78 KB
/
rt11modm.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
#include <stdio.h>
/*
* rtmodem.c -- This program allows the RT-11 user to transfer files to
* and from a host system (usually CP/M) using Ward Christensen's
* MODEM/XMODEM protocol. No terminal emulation is provided (yet).
* Written in DECUS C by:
*
* Robert B. Hoffman, N3CVL 5-Mar-82
* <Hoffman%[email protected]>
* {allegra, bellcore, cadre, idis, psuvax1}!pitt!hoffman
*
* 5-Mar-82 Original Version
*
* 18-Mar-83 Changed transmit routine to wait for one of (ACK,NAK,CAN,
* or TIMEOUT) before re-sending the sector. Garbage chars are
* now ignored. Changed receive routine to print a count of
* bytes transferred.
*
* 26-Aug-83 Changed transmit routine to print a count of bytes
* transferred. Changed receive routine to cancel transfer if
* the allocated file size is exceeded. *** Note: This is
* bogus -- putc() isn't returning any error condition. (see below)
*
* 9-Dec-83 Changed receive routine to send NAKs only after it times
* out waiting for a SOH, rather than NAKing every non-SOH.
*
* 13-Feb-84 Changed buffer array from char to ints. It seems that when
* putc was called with a char with the eighth bit set, then
* it was returning a negative int, which appeared as an error.
* If putc is called with an int, it works just fine, and returns
* the expected error condition not found above. Also changed
* command structure to make it quicker. Now, when a transfer
* completes, it returns to command mode rather than exiting.
*
* 19-Feb-84 Changed the 'bytes transmitted' message to indicate sectors,
* blocks, and bytes. Added bell chars to termination messages.
*
* 15-Aug-84 Fixed a bug that allowed nulls to be transmitted in text mode
* and appended an extra sector's worth of nulls in binary mode.
* Also changed command scanner to add Ethernet escape option
* and to make it easier to add more options. Added Ethernet
* escapes to output driver.
*/
struct device {
int rcsr;
int rbuf;
int xcsr;
int xbuf;
};
#define SOH 01
#define EOT 04
#define ACK 06
#define NAK 025
#define CAN 030
#define TIMEOUT -1
#define TRUE 1
#define FALSE 0
#define EVER ;;
int $$narg = 1;
struct device *dladdr = 0176500;
int buf[128];
int ether;
FILE *file;
main() {
register int i, c;
register char *p;
char blkno, cblkno, *filnam, cmdlin[40];
int bcnt, cksm, ok, nakcount, sndrcv, txtbin;
long nbytes;
printf("RTMODEM -- RT-11 file transfer program\n\n");
cmd:
do {
printf("RTMODEM command=> ");
gets(cmdlin);
for (i=0; i<strlen(cmdlin); i++)
cmdlin[i] = (isupper(cmdlin[i]) ? tolower(cmdlin[i]) : cmdlin[i]);
filnam = index(cmdlin,' ');
*filnam++ = 0; /* this is sneaky -- it null-terminates the */
/* command part of the string and makes */
/* filnam point to the file name */
ok = TRUE; /* assume cmd will be OK */
p = index(cmdlin,'s'); /* sending */
if (p == NULL) {
p = index(cmdlin,'r'); /* receiving */
}
if (p == NULL) {
ok = FALSE; /* one of these MUST be present */
}
sndrcv = *p;
p = index(cmdlin,'t'); /* text */
if (p == NULL) {
p = index(cmdlin,'b'); /* binary */
}
if (p == NULL) {
ok = FALSE; /* one of these must also be present */
}
txtbin = *p;
if (index(cmdlin,'e') == NULL) { /* ethernet mode */
ether = FALSE;
} else {
ether = TRUE;
}
if (!ok) {
printf("\nCommands are of the form:\n");
printf(" rt|rb|st|sb [e] filename\n");
printf(" rt -- receive text\n");
printf(" rb -- receive binary\n");
printf(" st -- send text\n");
printf(" sb -- send binary\n");
printf(" e -- use Ethernet escape\n\n");
}
} while (!ok);
freopen("tt:", "wu", stdout);
if (ether) {
printf("Using Ethernet character escape.\n");
}
if (sndrcv == 's') {
printf("Sending file %s, %s mode.\n", filnam,
(txtbin == 't' ? "text" : "binary"));
if ((file = fopen(filnam,"rn")) == NULL) {
fprintf(stderr,"Couldn't open input file %s.\n",
filnam);
goto cmd;
}
printf("File open, ready to send.\n");
nbytes = 512L * (long)(file->io_size);
printf("File is %d sectors (%ld bytes) long.\n",
(file->io_size) * 4, nbytes);
nbytes /= 84L; /* 120 cps * 70% = 84 cps effective */
printf("Estimated transfer time: %ld min, %ld sec at 1200 bps.\n",
nbytes/60L, nbytes%60L);
while ((c = getmod()) != NAK)
;
nbytes = 0L;
blkno = 0;
do {
i = 0;
do {
c = getc(file);
if (c == EOF) {
if (i == 0)
goto sfin;
if (txtbin == 't')
buf[i++] = 032;
else
buf[i++] = 0;
}
else if (txtbin != 't' || c != 0) {
buf[i++] = c;
}
} while (i < 128);
blkno++;
do {
outmod(SOH);
outmod(blkno);
outmod(~blkno);
cksm = 0;
for (bcnt = 0; bcnt < 128; bcnt++) {
outmod(buf[bcnt]);
cksm = (cksm + buf[bcnt]) % 256;
}
outmod(cksm);
do {
ok = TRUE; /* Assume all OK */
c = getmod();
switch (c) {
case ACK:
case NAK:
case CAN:
case TIMEOUT:
break;
default:
ok = FALSE;
break;
}
} while (!ok);
} while (c != ACK && c != CAN);
nbytes += 128L;
printf("%ld sec, %ld blk, %ld b\r", nbytes/128L,
(nbytes+384L)/512L, nbytes);
if (c == CAN) {
fclose(file);
printf("\n\007\007\007Transfer cancelled by host.\n");
goto cmd;
}
} while (c == ACK && !feof(file));
sfin:
outmod(EOT);
c = getmod();
if (c == ACK)
printf("\n\007\007\007File transmitted\n");
else
printf("\n\007\007\007Final ACK was 0%o instead!\n",c);
fclose(file);
goto cmd;
}
else {
printf("Receiving file %s, %s mode.\n", filnam,
(txtbin == 't' ? "text" : "binary"));
if ((file = fopen(filnam,"wn")) == NULL) {
fprintf(stderr,"Couldn't open output file %s\n",
filnam);
goto cmd;
}
printf("File open, ready to receive.\n");
nbytes = 0L;
outmod(NAK); /* Send initial NAK */
nakcount = 0;
for (EVER) {
do {
c = getmod();
switch (c) {
case TIMEOUT:
outmod(NAK);
nakcount++;
break;
case EOT:
goto rfin;
default:
break;
}
} while (c != SOH);
blkno = getmod();
cblkno = getmod();
cksm = 0;
for (i=0; i<128; i++) {
c = buf[i] = getmod();
cksm = (cksm + c) % 256;
}
c = getmod();
ok = (((SOH+blkno+cblkno) % 256) == 0 && cksm == c);
if (ok) {
for (i=0; i<128; i++) {
if (buf[i] == '\032' && txtbin == 't')
break;
c = putc(buf[i],file);
if (c == EOF) {
outmod(CAN);
fclose(file);
printf("\n\07Disk full! Transfer cancelled.\n");
goto cmd;
}
}
nbytes += 128L;
printf("%ld sec, %ld blk, %ld b\r",
nbytes/128L, (nbytes+384L)/512L,
nbytes);
outmod(ACK);
nakcount = 0;
} else {
if (nakcount > 10) {
printf("\nR)etry or Q)uit=> ");
gets(cmdlin);
c = cmdlin[0];
if (c == 'q' || c == 'Q') {
outmod(CAN);
fclose(file);
goto cmd;
}
nakcount = 0;
}
nakcount++;
printf("\nNAK %d\n", nakcount);
outmod(NAK);
}
}
rfin:
fclose(file);
outmod(ACK);
printf("\n\007\007\007Transfer complete\n");
goto cmd;
}
}
/*
* get a character from the modem. A 10 second timeout is
* needed to preclude hanging the modem if one side should
* terminate abnormally. The constant of 160000 provides this
* delay for an LSI-11.
*/
getmod() {
register struct device *MODEM;
long n;
MODEM = dladdr;
for (n=160000L; ((MODEM->rcsr & 0200) == 0) && (n > 0L); n--)
;
if (n == 0L) {
printf("\nTimeout!\n");
return(-1);
}
return((MODEM->rbuf) & 0377);
}
/*
* Send a character to the modem.
*/
outmod(c)
char c;
{
if (ether) {
switch(c) {
case 020:
case 021:
case 023:
outm(020);
break;
default:
break;
}
}
outm(c);
}
outm(c)
char c;
{
register struct device *MODEM;
MODEM = dladdr;
while ((MODEM->xcsr & 0200) == 0)
;
MODEM->xbuf = c;
}