-
Notifications
You must be signed in to change notification settings - Fork 1
/
MODEMIO.C
525 lines (408 loc) · 12.8 KB
/
MODEMIO.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
#include <puppy.h>
#include <ascii.h>
#include <pupmem.h>
/*
These are all "medium low" level I/O for Puppy. There shouldnt be
any DOS dependencies in here; the hardware drivers do that. These call the
"generic" hardware as described in the interface docs.
They may seem large for just a bunch of I/O routines, but there are
things in here that eliminate many so-called "high-level" functions such as
word wrap, command-ahead and input buffer control, etc.
T. Jennings
Fido Software
164 Shipley
San Francisco CA 94107
(k) all rights reversed
All of the following functions call functions from the same
list, or ones below it.
Puppy's Console I/O. These will log the caller off due to
time limits, inactivity for 10 minutes or carrier loss.
getargs(prompt) Displays the prompt, returns a pointer to the inputted string.
isargs() Return true if there are more args that could be read.
input(prompt,buff,length) Displays a prompt, inputs a line of text. This is
the thing that does the actual prompting and input when
getargs runs out of things to return.
ask(ques) Prints a string, inputs Y or N, returns true if Y. This
does a nice neat question input.
cmdflush() Flush any command-ahead.
These do "formatted" I/O; printf() type stuff, word wrap, etc.
mprintf() Same as PRINTF, except goes to the modem.
mputs() Put a string to the modem.
fmconout(c) Outputs characters to the console, wrapping words to fit
within the set screen width. NOTE: You must issue a ^Z
after the last character to flush out any characters
stuck in the word buffer.
These are "plain" console I/O.
mconout(c) Output C to the console. Maintains line/column
cursor position, handles control codes, etc.
mconin() Returns a character from the console.
mconstat() Return true if a character available.
mconflush() Throw away all keyboard input.
ques(q) A low level question asker: accepts various things
for input. Does minimal cursor motion & cleanup.
Puppy's Modem I/O. These will log the caller off if carrier
is lost. These do no control code processing or other
console type functions.
modin(n) Returns character available, else -1 if N centiseconds elapse.
modout(c) Outputs C to the modem.
modstat() Returns true if a modem character available.
Puppy's low level I/O support functions.
limitchk() Checks the set time limit, and logs off if exceeded.
flush(n) Flush the modem until it is clear for n centiseconds.
cd() Returns true if Carrier Detect is true or cd_flag is true.
carrierchk() Checks for modem carrier via cd() and logs off if not present.
limitchk() Checks caller time limits in effect and logs off if exceeded.
*/
/* NOTE: There is a subtle kludge here that enables the detection of
incoming Crash Mail.
The keyboard input routines in BUFIO.C (mconstat(),
mconin(), etc) strip parity of incoming characters, EXCEPT for TSYNC.
If input() detects a TSYNC at anytime, it clears any
input it may have accumulated and returns a string with the single
character TSYNC in it.
If getarg() detects a TSYNC in its input, it immediately
returns a pointer to the TSYNC.
All this for Crash Mail. I hate this kludge.
*/
static char line_buff[SS] = ""; /* command line buffer */
static char *_nargp = line_buff; /* pointer into it */
/* These are used by fmconout() */
static char word[SS]; /* word we're building */
static char wordi = 0; /* index/length of word */
#define MARGIN 2 /* right margin on screen */
/* This buffer lets Puppy do typeahead even on systems that
dont have interrupts. The input is polled during output & input
and any characters are stored away in here. Input requests check the
buffer and do further polling. */
static char conbuf[SS]; /* console type ahead buffer */
static int head = 1; /* indices into it */
static int tail = 0;
/* Main command line input. This function returns a pointer to the
next available argument, if none is available, it displays the prompt
and inputs a string from the console. */
char *getarg(prompt)
char *prompt;
{
char *cp;
if (! isargs()) { /* if line empty, */
input(prompt,line_buff,sizeof(line_buff)); /* get some input, */
_nargp= skip_delim(line_buff); /* init the arg pointer */
}
cp= _nargp; /* ptr to current arg */
_nargp= next_arg(_nargp); /* skip to the next one, */
return(cp);
}
/* Return true if there are any args left. */
isargs() {
return(num_args(_nargp));
}
/* Flush the command buffer */
cmdflush() {
_nargp= line_buff;
*_nargp= NUL;
}
/* Ask a question, return true if "Y" pressed, 0 if "N". */
ask(s)
char *s;
{
char c,cp,buff[SS];
strcpy(buff,s);
strcat(buff,"? [Y,n]: ");
while (1) {
switch (tolower(*getarg(buff))) {
case NUL: return(1); /* CR is Yes */
case 'y': return(1); /* Y is Yes */
case 'n': return(0); /* N is No */
}
cmdflush(); /* else garbage */
}
}
/* Ask a question, get a "hot" (single key) answer. Must ignore ^S and ^Q,
as it may get things all out of sync. */
ques(q)
char *q;
{
line= 0; /* AVOID INFINITE RECURSION!!! */
while (*q) mconout(*q++); /* output directly, */
while (1) {
switch (mconin()) {
case XOF: /* ignore Control-S */
case XON: /* and Control-Q */
case ACK: /* Ignore Control-F */
break;
case 'n':
case 'N':
case ETX:
case VT:
return(0);
break;
case ' ':
case 'y':
case 'Y':
case CR:
return(1);
break;
default: mconout(BEL); break;
}
}
}
/* Issue a prompt, get a line of input. */
input(prompt,buff,len)
char *prompt,*buff;
int len;
{
char c;
int n,i;
mputs(prompt); /* output the prompt, */
i= 0;
line= 0; /* reset "more" */
while (1) {
c= mconin(); /* get a key, */
switch (c) {
case TSYNC: /* KLUDGE */
i= 0; /* clear buffer ... */
buff[i]= NUL;
return(i);
case CR: /* end of input */
buff[i]= NUL;
mputs("\r\n");
abort= 0;
return(i);
case BS: /* delete character */
case DEL:
n= 1; goto del;
case CAN: /* delete line */
case ETX:
case VT:
n= i;
del:; while (n && i) {
mconout(BS);
mconout(' ');
mconout(BS);
--n; --i;
}
break;
case ACK: break; /* ignore Control-F */
default: /* insert character */
if ((c >= ' ') && (c <= '~') && (i < len-1)) {
buff[i++]= c;
mconout(c);
} else mconout(BEL);
break;
}
}
}
/* Output characters to the screen, building words and wrapping them
as necessary to fit within the current width. */
fmconout(c)
char c;
{
if (c >= ' ') { /* printable chars */
word[wordi++]= c; /* build a word */
if ((wordi + MARGIN < caller.cols) && !delim(c)) return;
}
word[wordi]= NUL; /* terminate it for output */
/* End of a word, word too large, or a control character. */
if (column + wordi + MARGIN > caller.cols) {/* if word is too long, */
mconout(CR); /* go to next line first */
mconout(LF);
}
if (*word) { /* if there is a word there, */
for (wordi= 0; word[wordi]; wordi++)
mconout(word[wordi]); /* output it */
wordi= 0; /* now its gone */
}
if ((c < ' ') && (c != SUB)) mconout(c); /* do control chars */
}
/* Output a character to the console, and poll for ^C, etc. */
mconout(c)
char c;
{
if (mconstat() == XOF) { /* if a pause, */
while (mconin() == XOF); /* wait for anything but XOF */
}
modout(c); /* to the modem, */
if (c && (c != BEL)) lconout(c); /* one to console if not bell */
if (c >= ' ') ++column; /* "cursor" motion */
else if (c == CR) column= 0;
else if (c == LF) { /* next line */
++line;
if (line >= caller.lines - 1) { /* screen pause, set abort */
line= 0; /* reset it, */
if (caller.lines) { /* if line pause enabled */
mconflush(); /* flush typeahead, */
abort= ! ques(" <- More\r");
}
}
}
}
/* Wait for a character from the keyboard. Check for activity, log the guy
off if he times out. */
#define ONEMIN (60L * 1000L) /* milliseconds in one minute */
#define EIGHTMIN (ONEMIN * 8L)
#define TENMIN (ONEMIN * 10L) /* ten minutes */
mconin() {
FLAG warned;
millisec= 0L; /* clear the timer, */
warned= 0; /* not warned yet */
while (! mconstat()) {
if ((millisec > EIGHTMIN) && (!warned)) {
mputs("\r\nPup says: \07\"I'll hang up on you if you sit idle too long!\"\r\n");
++warned;
}
if (millisec > TENMIN) {
mputs("\r\nPup says: \"You were warned!\"\r\n");
logoff(0,1);
}
}
tail= ++tail % sizeof(conbuf);
return( (int) conbuf[tail]);
}
/* Return 0 if no key available, else the key. */
mconstat() {
int t;
pollkbd(); /* poll for chars, */
t= (tail + 1) % sizeof(conbuf); /* wrap pointer, */
return((t == head) ? 0 : conbuf[t]); /* the char or 0 */
}
/* Poll both keyboards, stuff characters into the ring buffer. This handles
all background tasks, like type mode, etc. NOTE: This strips parity off
all incoming characters EXCEPT TSYNC! This lets us support Continuous Mail. */
pollkbd() {
char c;
limitusr(); /* maybe forced logoff */
if (! test) { /* if online, */
carrierchk(); /* watch carrier! */
if (_mconstat()) { /* if modem character there */
c= _mconin(); /* read it, */
if (c != TSYNC) c &= 0x7f;
put_c(c); /* use it */
}
} else {
c= keyhit();
if (c) put_c(c); /* else just local input */
}
}
/* Put this character into the ring buffer, process the special
control characters as necessary. */
put_c(c)
char c;
{
switch (c) {
case ETX: /* Control-C */
case VT: /* Control-K */
abort= 1; /* flag as an abort, */
case TSYNC: /* Continuous Mail ... */
case XOF: /* Control-S */
case ACK: /* Control-F */
mconflush(); /* flush typeahead, */
cmdflush(); /* flush command buffer, */
break;
case NUL: return; /* ignore NULs */
}
if (head != tail) { /* if room, install it */
conbuf[head]= c;
head= ++head % sizeof(conbuf);
}
}
/* Flush the console buffer. */
mconflush() {
while (mconstat()) mconin(); /* flush all characters, */
}
/* Formatted print to the modem. */
mprintf(f)
char *f;
{
char buf[500];
_spr(buf,&f);
mputs(buf);
}
/* Output a string to the modem. */
mputs(s)
char *s;
{
while (*s) fmconout(*s++);
fmconout(SUB); /* flush the word buffer */
}
/* Watch the time limits set, and give warnings when the limit comes
up. When the limit reaches 0, display a message and log the caller off. */
limitusr() {
int n;
n= limit - minutes; /* time left */
if (limit == 0) { /* if no limit, */
return; /* do nothing */
} else if (n < 1) { /* time is over */
if (lmtstate < 3) {
lmtstate= 3; /* only once */
limit= 0; /* DISABLE LIMITS */
mprintf("\r\n\r\n\07 Pup Says: \"Your time is up!\"\r\n\r\n");
logoff(0,1); /* disconnect */
}
} else if (n < 2) { /* final PUP SAYS: */
if (lmtstate < 2) {
lmtstate= 2; /* do only once */
mprintf("\r\n\r\n\07Pup Says: \"I'm gonna hang up on you in 60 seconds!\"r\n\r\n");
}
} else if (n < 5) { /* two minute PUP SAYS: */
if (lmtstate < 1) { /* if no PUP SAYS: yet, */
lmtstate= 1; /* remember this, BEFORE THE MSG */
mprintf("\r\n\07Pup Says: \"You have %d minutes left!\"\r\n",n);
}
}
}
/* Check the time limit, and if over, bump the guy off. */
limitchk() {
if (limit && (minutes >= limit)) logoff(0,1);
}
/* Output to the modem. */
modout(c)
char c;
{
if (test) return;
carrierchk(); /* always watch this */
_mconout(c); /* limit will get checked eventually */
}
/* Get a character from the modem, with a maximum wait time, in
centiseconds. Returns -1 if timeout. */
modin(n)
unsigned n;
{
long dly;
if (test) return(-1); /* cant in test mode */
if (_mconstat()) return(_mconin() & 0xff); /* why wait? */
dly= n * 10L; /* time, in milliseconds */
millis2= 0L; /* clear the ticker, */
while (millis2 < dly) {
carrierchk(); /* watch disconnect, */
if (_mconstat()) /* if a char avail, return it. */
return(_mconin() & 0xff);
}
return(-1); /* timeout */
}
/* Delay N centiseconds. Do nothing in the mean time. */
delay(n)
int n;
{
long dly;
dly= n * 10L;
millisec= 0L;
while (millisec < dly);
}
/* Logoff if carrier is lost. */
carrierchk() {
if (! cd()) logoff(0,1);
}
/* Return true if carrier detect is true or "ignore CD" is true. */
cd() {
if (cd_flag || test) return(1); /* fake on-line */
return(_cd()); /* real online */
}
/* Flush the modem until its quiet for N centiseconds or just flush
whatever is there if n == 0. */
flush(n)
int n;
{
if (test) return;
if (n) while (modin(n) != -1);
else while (_mconstat()) _mconin();
}