-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsrio.c
1661 lines (1422 loc) · 30 KB
/
tsrio.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (C) Cyclades Corporation, 1999-1999. All rights reserved.
*
*
* tsrio.c
* Tsrsock I/O Scheduler
*
* History
* 08/17/1999 V.1.0.0 Initial revision
*
* Oct-27-2001 V.1.0.1
* Read is now allowed when PTY is on WAITUSRWR* states
* Less syslog activity on unexpected events
* Hangup message is now of Warning type
*/
#include <sys/types.h>
#include <stdio.h>
#define _TSR_TSRIO_
#include "inc/cyclades-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/telnet.h"
#include "inc/misc.h"
#include "inc/dev.h"
#include <string.h>
/*
* Internal Variables
*/
struct buffer Inbuf; /* Buffer RAS => USER */
struct buffer Outbuf; /* Buffer USER => RAS */
int Hang_up = FALSE;
struct statenames
{
const char *const stname;
char **subnames;
};
struct statenames Pty_states[] = {
{"PTY_INITIAL", 0},
{"PTY_CLOSING", 0},
{"PTY_CLOSED", 0},
{"PTY_OPENING", 0},
{"PTY_WAITDCD", 0},
{"PTY_OPER", 0},
{"PTY_CONFIG", 0},
{"PTY_WAITNVTWR", 0},
{"PTY_WAITUSRWR", 0},
{"PTY_WAITNRUW", 0},
{"PTY_WAITNVTCM", 0},
{"PTY_WAITCLOCAL", 0},
{"PTY_WAITCLOSE", 0},
{"PTY_OPERRONLY", 0},
{"PTY_WAITUSRWR0", 0},
{"PTY_WAITUSRWR2", 0},
};
struct statenames Nvt_states[] = {
{"NVT_INITIAL", 0},
{"NVT_OPER", 0},
{"NVT_WAITPTYWR", 0},
{"NVT_WAITRASWR", 0},
{"NVT_WAITRWPW", 0},
{"NVT_WAITRASCM", 0},
{"NVT_WAITRCPW", 0},
};
const char *const Pty_events[] = {
"EV_UPOPEN",
"EV_UPCLOSE",
"EV_UPDATA",
"EV_UPCONTROL",
"EV_UPFLUSH",
"EV_UPWROK",
};
const char *const Nvt_events[] = {
"EV_RNDNTFY",
"EV_RNDATA",
"EV_RNHANG",
"EV_RNCMOK",
"EV_RNWROK",
};
/*
* Main Scheduler Routines
*/
void all_hangup();
int check_states(void);
void all_wakeup(void);
void all_readonly(void);
/*
* PTY External events handlers (up_*)
*/
void up_nop(struct event *evp);
void up_open(struct event *unused);
void up_op01(struct event *unused);
void up_close(struct event *unused);
void up_data(struct event *evp);
void up_ioctl(struct event *unused);
void up_flush(struct event *evp);
void up_wrok(struct event *unused);
/*
* PTY Internal events handlers (np_*)
*/
void np_nop(const char *const hand);
void np_wrok(void);
void np_wok01(void);
void np_wok02(void);
void np_woker(void);
void np_cmdok(void);
void np_cok01(void);
void np_cok02(void);
void np_cok03(void);
void np_cok04(void);
void np_coker(void);
void np_dcdon(void);
void np_don01(void);
void np_don02(void);
void np_doner(void);
void np_dcdoff(void);
void np_dof01(void);
void np_dof02(void);
void np_dofer(void);
void np_parity(void);
void np_par01(void);
void np_parer(void);
void np_break(void);
void np_brk01(void);
void np_brker(void);
/*
* PTY Operations (do_pty*)
*/
void do_ptyerr(char *action);
int do_ptydata(void);
void do_ptyopen(void);
void do_ptyopen1(void);
void do_ptyclose(void);
void do_ptyclose1(void);
void do_ptywrite(void);
void do_ptyioctl(void);
void do_ptyconfig(int mode, struct portconfig *pcp);
void do_ptyconfig1(void);
void do_ptysendbreak(int interval);
void do_ptyflush(int mode);
void do_ptyhangup(void);
void do_ptyinterrupt(void);
/*
* NVT External events handlers (rn_*)
*/
void rn_nop(struct event *evp);
void rn_ntfy(struct event *evp);
void rn_data(struct event *unused);
void rn_cmdok(struct event *unused);
void rn_wrok(struct event *unused);
/*
* NVT Internal events handlers (pn_*)
*/
void pn_nop(const char *const hand);
void pn_wrok(void);
void pn_wok01(void);
void pn_wok02(void);
void pn_woker(void);
/*
* NVT Operations (do_nvt*)
*/
void do_nvterror(const char *const action);
int do_nvtdata(void);
void do_nvtdtron(void);
void do_nvtdtroff(void);
void do_nvtconfig(struct portconfig *pcp);
void do_nvtsendbreak(int interval);
void do_nvtflush(int mode);
/*
* Debug routines
*/
char *tsr_states(const char *const unused);
void print_ptystate(const char *const hand);
void print_nvtstate(const char *const hand);
void print_action(const char *const hand);
void print_event(struct event *evp);
/*
* Matrix for PTY External events
*/
void (*(Pty_upevents[PTY_STATES][EV_UPEVENTS])) (struct event * evp) = {
/*{UPOPEN ,UPCLOSE ,UPDATA ,UPCONTROL,UPFLUSH ,UPWROK } */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* INITIAL */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* CLOSING */
{
up_open, up_nop, up_nop, up_nop, up_nop, up_nop}, /* CLOSED */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* OPENING */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* WAITDCD */
{
up_nop, up_close, up_data, up_ioctl, up_flush, up_nop}, /* OPER */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* CONFIG */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* WAITNVTWR */
{
up_nop, up_close, up_data, up_nop, up_nop, up_wrok}, /* WAITUSRWR */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_wrok}, /* WAITNWUW */
{
up_nop, up_nop, up_nop, up_nop, up_nop, up_nop}, /* WAITNVTCM */
{
up_nop, up_close, up_nop, up_nop, up_nop, up_nop}, /* WAITCLOCAL */
{
up_nop, up_close, up_nop, up_nop, up_nop, up_nop}, /* WAITCLOSE */
{
up_op01, up_nop, up_nop, up_nop, up_nop, up_nop}, /* OPERRONLY */
{
up_nop, up_close, up_nop, up_nop, up_nop, up_wrok}, /* WAITUSRWR0 */
{
up_nop, up_close, up_nop, up_nop, up_nop, up_wrok}, /* WAITUSRWR2 */
};
/*
* Matrix for PTY Internal events
*/
void (*(Pty_npevents[PTY_STATES][EV_NPEVENTS])) (void) = {
/*{NPWROK ,NPCMDOK ,NPDCDON ,NPDCDOFF,NPPARITY,NPBREAK } */
{
np_woker, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* INITIAL */
{
np_woker, np_cok01, np_doner, np_dofer, np_parer, np_brker}, /* CLOSING */
{
np_woker, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* CLOSED */
{
np_woker, np_cok02, np_doner, np_dofer, np_parer, np_brker}, /* OPENING */
{
np_woker, np_coker, np_don01, np_dofer, np_parer, np_brker}, /* WAITDCD */
{
np_woker, np_coker, np_doner, np_dof01, np_par01, np_brk01}, /* OPER */
{
np_woker, np_cok03, np_doner, np_dofer, np_parer, np_brker}, /* CONFIG */
{
np_wok01, np_coker, np_doner, np_dofer, np_par01, np_brk01}, /* WAITNVTWR */
{
np_woker, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* WAITUSRWR */
{
np_wok02, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* WAITNWUW */
{
np_woker, np_cok04, np_doner, np_dofer, np_parer, np_brker}, /* WAITNVTCM */
{
np_woker, np_coker, np_don02, np_dofer, np_parer, np_brker}, /* WAITCLOCAL */
{
np_woker, np_coker, np_don01, np_dofer, np_parer, np_brker}, /* WAITCLOSE */
{
np_woker, np_coker, np_doner, np_dof02, np_parer, np_brk01}, /* OPERRONLY */
{
np_woker, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* WAITUSRWR0 */
{
np_woker, np_coker, np_doner, np_dofer, np_parer, np_brker}, /* WAITUSRWR2 */
};
/*
* Matrix for NVT External events
*/
void (*(Nvt_rnevents[NVT_STATES][EV_RNEVENTS])) (struct event * evp) = {
/*{RNNTFY ,RNDATA ,RNHANG ,RNCMDOK ,RNWROK } */
{
rn_ntfy, rn_nop, rn_nop, rn_nop, rn_nop}, /* INITIAL */
{
rn_ntfy, rn_data, rn_nop, rn_nop, rn_nop}, /* OPER */
{
rn_nop, rn_nop, rn_nop, rn_nop, rn_nop}, /* WAITPTYWR */
{
rn_ntfy, rn_data, rn_nop, rn_nop, rn_wrok}, /* WAITRASWR */
{
rn_nop, rn_nop, rn_nop, rn_nop, rn_wrok}, /* WAITRWPW */
{
rn_ntfy, rn_nop, rn_nop, rn_cmdok, rn_nop}, /* WAITRASCM */
{
rn_nop, rn_nop, rn_nop, rn_cmdok, rn_nop}, /* WAITRCPW */
};
/*
* Matrix for NVT Internal events
*/
void (*(Nvt_pnevents[NVT_STATES][EV_PNEVENTS])) (void) = {
/*{PNWROK } */
{
pn_woker}, /* INITIAL */
{
pn_woker}, /* OPER */
{
pn_wok01}, /* WAITPTYWR */
{
pn_woker}, /* WAITRASWR */
{
pn_wok02}, /* WAITRWPW */
{
pn_woker}, /* WAITRASCM */
{
pn_woker}, /* WAITRCPW */
};
/*
* Startup
*/
int
tsr_init(int netsize, int devsize, int devmodem, int closemode)
{
int ret;
/* Initialize buffers (warn: these macros can return) */
INIT_BUFFER(Inbuf, 2 * netsize);
INIT_BUFFER(Outbuf, 2 * devsize);
/* Initialize event queue */
INIT_EVENTS();
/* Connection status */
Hang_up = FALSE;
/* Initialize NVT */
if ((ret = tel_init(netsize, &Inbuf, &Outbuf)) != E_NORMAL)
return (ret);
NVT_SETSTATE("tsr_init", NVT_OPER); /* NVT starts at operational state */
/* Initialize PTY */
if ((ret = dev_init(devsize, devmodem, closemode, &Outbuf, &Inbuf,
Nvt.comport)) == E_NORMAL) {
PTY_SETSTATE("tsr_init", PTY_INITIAL);
do_ptyclose(); /* PTY starts at closed state */
}
return (ret);
}
/*
* Main Scheduler Routines
*/
void
tsr_io(void)
{
struct event *ev;
int mask;
while (Hang_up == FALSE) {
mask = check_states();
external_poll(mask, SCHED_DELAY);
while (Hang_up == FALSE) {
ev = GET_EVENT();
if (!ev)
break;
print_event(ev);
switch (ev->ev_type) {
case EV_UP:
(*Pty_upevents[Pty.state][ev->ev_code]) (ev);
break;
case EV_RN:
(*Nvt_rnevents[Nvt.state][ev->ev_code]) (ev);
break;
}
CLR_EVENT(ev);
}
}
all_hangup();
}
void
all_hangup()
{
dev_hangup();
tel_free();
dev_free();
FREE_BUFFER(Inbuf);
FREE_BUFFER(Outbuf);
FREE_EVENTS();
Hang_up = FALSE;
}
int
check_states(void)
{
int mask = 0;
switch (Pty.state) {
case PTY_OPER:
case PTY_OPERRONLY:
mask = DEV_READ | DEV_EXCEPT;
break;
case PTY_WAITCLOCAL:
case PTY_WAITCLOSE:
mask = DEV_PROBE | DEV_EXCEPT;
break;
case PTY_WAITUSRWR0:
mask = DEV_READ | DEV_EXCEPT | DEV_WRITE;
break;
case PTY_WAITUSRWR:
mask = DEV_READ | DEV_EXCEPT | DEV_WRITE;
break;
case PTY_WAITNWUW: /* don't read pty */
case PTY_WAITUSRWR2:
mask = DEV_WRITE;
break;
}
switch (Nvt.state) {
case NVT_OPER:
case NVT_WAITRASCM:
case NVT_WAITRCPW: /*WARN Pty Blocked, buffer may overrun */
mask |= SOCK_READ;
break;
case NVT_WAITRASWR:
mask |= SOCK_READ | SOCK_WRITE;
break;
case NVT_WAITRWPW: /* don't read socket */
mask |= SOCK_WRITE;
break;
}
return (mask);
}
void
imminent_event(int msgtype)
{
switch (Pty.state) {
case PTY_WAITCLOSE:
if (msgtype != PROBE_EOF) { /* ! MDATA == 0 =>any except CLOSE, BLOCK */
PTY_SETSTATE("probe", PTY_WAITDCD);
}
else {
dev_getdata(); /* CLOSE, GET IT */
}
break;
case PTY_WAITUSRWR0:
PTY_SETSTATE("probe", PTY_WAITUSRWR);
if (msgtype != PROBE_EOF && msgtype != PROBE_DATA) { /* ! MDATA, BLOCK */
PTY_SETSTATE("probe", PTY_WAITUSRWR2);
}
else { /* MDATA/CLOSE => GET IT */
dev_getdata();
}
break;
case PTY_WAITUSRWR:
if (msgtype != PROBE_EOF && msgtype != PROBE_DATA) { /* ! MDATA, BLOCK */
PTY_SETSTATE("probe", PTY_WAITUSRWR2);
}
else { /* MDATA/CLOSE => GET IT */
dev_getdata();
}
break;
case PTY_WAITCLOCAL:
if (msgtype == PROBE_CLOCAL) {
PTY_SETSTATE("probe", PTY_OPER);
Pty.portmodes |= PORT_CLOCAL;
dev_getdata();
}
else if (msgtype == PROBE_EOF) { /* CLOSE ?? */
PTY_SETSTATE("probe", PTY_OPER);
dev_getdata();
}
else {
PTY_SETSTATE("probe", PTY_WAITDCD);
}
break;
}
}
/* GAMB => Invoked in Pty.state == OPER, check for missed events */
void
all_wakeup(void)
{
/* First check for NP_DCDOFF */
if (!(Pty.portmodes & PORT_CLOCAL) &&
!(Pty.comport->portstate.modemstate & MODEM_DCD)) {
PTY_SETSTATE("all_wakeup", PTY_WAITCLOSE);
do_ptyhangup();
return;
}
/* Check for NVT data */
if (Pty.outbuff->b_hold) {
if (do_ptydata() != E_NORMAL) { /* local action */
return;
}
pn_wrok(); /* Notify NVT */
}
/* Finally, check for user data */
if (Pty.inbuff->b_hold) {
do_ptywrite();
}
}
void
all_readonly(void)
{
/* First check for NP_DCDOFF */
if (!(Pty.portmodes & PORT_CLOCAL) &&
!(Pty.comport->portstate.modemstate & MODEM_DCD)) {
PTY_SETSTATE("all_readonly", PTY_WAITCLOCAL);
do_ptyhangup();
return;
}
/* Check for NVT data */
if (Pty.outbuff->b_hold) {
if (do_ptydata() != E_NORMAL) { /* local action */
return;
}
pn_wrok(); /* Notify NVT */
}
}
/*
* PTY External events handlers (up_*)
*/
void
up_nop(struct event *evp)
{
const char *const event = Pty_events[evp->ev_code];
sysmessage(MSG_DEBUG, "PTY state (EVENT %s): %s\n", event,
tsr_states(event));
}
void
up_open(struct event *unused)
{
unused = unused;
do_ptyopen();
}
void
up_op01(struct event *unused)
{
unused = unused;
/* Port already open (read_only) => only change to read-write states */
switch (Pty.state) {
case PTY_OPERRONLY:
PTY_SETSTATE("up_op01", PTY_OPER);
all_wakeup(); /* XXXX */
break;
case PTY_WAITUSRWR0:
PTY_SETSTATE("up_op01", PTY_WAITUSRWR);
break;
}
}
void
up_close(struct event *unused)
{
unused = unused;
do_ptyclose();
}
void
up_ioctl(struct event *unused)
{
unused = unused;
do_ptyioctl();
}
void
up_flush(struct event *evp)
{
int mode = *(int *) evp->ev_param;
do_ptyflush(mode);
}
void
up_data(struct event *unused)
{
unused = unused;
do_ptywrite();
}
void
up_wrok(struct event *unused)
{
unused = unused;
switch (Pty.state) {
case PTY_WAITUSRWR0:
PTY_SETSTATE("up_wrok", PTY_OPERRONLY);
break;
case PTY_WAITUSRWR:
case PTY_WAITUSRWR2:
PTY_SETSTATE("up_wrok", PTY_OPER);
break;
case PTY_WAITNWUW:
PTY_SETSTATE("up_wrok", PTY_WAITNVTWR);
break;
}
if (do_ptydata() == E_NORMAL) { /* XXXX local action */
pn_wrok();
}
}
/*
* PTY Internal events handlers (np_*)
*/
void
np_nop(const char *const evhand)
{
sysmessage(MSG_DEBUG, "PTY state (INT EVENT %s): %s\n", evhand,
tsr_states(evhand));
}
/* EV_NPWROK */
void
np_wrok(void)
{
(*Pty_npevents[Pty.state][EV_NPWROK]) ();
}
void
np_wok01(void)
{
PTY_SETSTATE("np_wrok", PTY_OPER);
all_wakeup(); /* XXXX */
}
void
np_wok02(void)
{
PTY_SETSTATE("np_wrok", PTY_WAITUSRWR);
}
void
np_woker(void)
{
np_nop("np_wrok");
}
/* EV_NPCMDOK */
void
np_cmdok(void)
{
(*Pty_npevents[Pty.state][EV_NPCMDOK]) ();
}
void
np_cok01(void)
{
do_ptyclose1();
}
void
np_cok02(void)
{
do_ptyopen1();
}
void
np_cok03(void)
{
do_ptyconfig1();
}
void
np_cok04(void)
{
PTY_SETSTATE("np_cmdok", PTY_OPER);
all_wakeup(); /* XXXX */
}
void
np_coker(void)
{
np_nop("np_cmdok");
}
/* EV_NPDCDON */
void
np_dcdon(void)
{
(*Pty_npevents[Pty.state][EV_NPDCDON]) ();
}
void
np_don01(void)
{
PTY_SETSTATE("np_dcdon", PTY_OPER);
all_wakeup(); /* XXXX */
}
void
np_don02(void)
{
PTY_SETSTATE("np_dcdon", PTY_OPERRONLY);
all_readonly();
}
void
np_doner(void)
{
np_nop("ev_npdcdon");
}
/* EV_NPDCDOFF */
void
np_dcdoff(void)
{
(*Pty_npevents[Pty.state][EV_NPDCDOFF]) ();
}
void
np_dof01(void)
{
if (!(Pty.portmodes & PORT_CLOCAL)) {
PTY_SETSTATE("np_dcdoff", PTY_WAITCLOSE);
do_ptyhangup();
}
}
void
np_dof02(void)
{
if (!(Pty.portmodes & PORT_CLOCAL)) {
PTY_SETSTATE("np_dcdoff", PTY_WAITCLOCAL);
do_ptyhangup();
}
}
void
np_dofer(void)
{
np_nop("np_dcdoff");
}
/* EV_NPPARITY */
void
np_parity(void)
{
(*Pty_npevents[Pty.state][EV_NPPARITY]) ();
}
void
np_par01(void)
{
int portmodes;
struct buffer *bp;
portmodes = Pty.portmodes;
bp = Pty.outbuff;
if (!(portmodes & PORT_IGNPAR)) {
if (portmodes & PORT_PARMRK) {
PUT_BUFFER(bp, 0xff);
PUT_BUFFER(bp, 0x00);
}
else {
PUT_BUFFER(bp, 0x00);
}
}
}
void
np_parer(void)
{
np_nop("np_parity");
}
/* EV_NPBREAK */
void
np_break(void)
{
(*Pty_npevents[Pty.state][EV_NPBREAK]) ();
}
void
np_brk01(void)
{
int portmodes;
struct buffer *bp1, *bp2;
portmodes = Pty.portmodes;
bp1 = Pty.outbuff;
bp2 = Pty.inbuff;
if (!(portmodes & PORT_IGNBRK)) {
if (portmodes & PORT_BRKINT) {
RESET_BUFFER(bp1);
RESET_BUFFER(bp2);
do_ptyinterrupt();
}
else if (portmodes & PORT_PARMRK) {
PUT_BUFFER(bp1, 0xff);
PUT_BUFFER(bp1, 0x00);
}
else {
PUT_BUFFER(bp1, 0x00);
}
}
}
void
np_brker(void)
{
np_nop("np_break");
}
/*
* PTY Operations (do_pty*)
*/
void
do_ptyerror(const char *const action)
{
sysmessage(MSG_ERR, "Undesirable PTY state for ACTION %s: %s\n",
action, tsr_states(action));
}
int
do_ptydata()
{
int ret;
struct buffer *bp = Pty.outbuff;
print_action("do_ptydata");
switch (Pty.state) {
case PTY_OPER:
case PTY_OPERRONLY:
case PTY_WAITNVTWR:
if ((ret = dev_putdata(bp)) == 0) {
ret = ret;
if (bp->b_hold != 0) {
switch (Pty.state) {
case PTY_OPER:
PTY_SETSTATE("do_ptydata", PTY_WAITUSRWR);
break;
case PTY_WAITNVTWR:
PTY_SETSTATE("do_ptydata", PTY_WAITNWUW);
break;
case PTY_OPERRONLY:
PTY_SETSTATE("do_ptydata", PTY_WAITUSRWR0);
break;
}
ret = E_BLOCKED;
}
else {
ret = E_NORMAL;
}
}
else {
Hang_up = TRUE;
if (Debug > 0) {
sysmessage(MSG_DEBUG, "HANGUP DO_PTYDATA\n");
}
ret = E_FILEIO;
}
break;
case PTY_WAITCLOSE:
case PTY_CLOSING:
case PTY_CLOSED:
case PTY_OPENING:
case PTY_WAITCLOCAL:
case PTY_WAITDCD:
RESET_BUFFER(Pty.outbuff); /* Discard it */
pn_wrok(); /* Notify NVT */
ret = E_NORMAL;
break;
default:
do_ptyerror("do_ptydata");
ret = E_BLOCKED; /* Transition to PTY_OPER must wakeup NVT */
break;
}
return (ret);
}
void
do_ptyclose(void)
{
print_action("do_ptyclose");
/* Wait output data to drain */
if (Pty.inbuff->b_hold) {
if (do_nvtdata() != E_NORMAL) {
sysmessage(MSG_WARNING,
"Can't flush output buffer before close\n");
}
}
/* Flush input data */
if (Pty.outbuff->b_hold) {
RESET_BUFFER(Pty.outbuff);
pn_wrok(); /* Notify NVT */
}
/* Set DTR off if applicable */
if (Pty.portmodes & PORT_HUPCL) {
PTY_SETSTATE("do_ptyclose", PTY_CLOSING);
do_nvtdtroff(); /* Sched */
}
else {
do_ptyclose1();
}
}
void
do_ptyclose1(void)
{
print_action("do_ptyclose1");
dev_config();
PTY_SETSTATE("do_ptyclose1", PTY_CLOSED);
/* Simulate a user open */
/* DO a DTR OFF->ON delay */
sysdelay(1000);
do_ptyopen();
}
void
do_ptyopen(void)
{
print_action("do_ptyopen");
PTY_SETSTATE("do_ptyopen", PTY_OPENING);