download asyn_os2.cpp
Language: C++
Copyright: Copyright 1987-96, Bit Bucket Software Co.
LOC: 1390
Project Info
BinkleyTerm XE(btxe)
Server: SourceForge
Type: cvs
...e\b\btxe\btxe\btnt\sources\
   Addr.cpp
   Addr.h
   aglcrc.cpp
   aglcrc.h
   asyn_lnx.cpp
   asyn_os2.cpp
   asyn_w32.cpp
   async.h
   asyos2v1.h
   b_banner.cpp
   b_bbs.cpp
   b_faxrcv.cpp
   b_flofil.cpp
   b_frproc.cpp
   b_help.cpp
   b_initva.cpp
   b_logs.cpp
   b_passwo.cpp
   b_proto.cpp
   b_rspfil.cpp
   b_sbinit.cpp
   b_script.cpp
   b_search.cpp
   b_sessio.cpp
   b_spawn.cpp
   b_term.cpp
   b_termov.cpp
   b_today.cpp
   b_wazoo.cpp
   b_whack.cpp
   b_wzsend.cpp
   banner.cpp
   banner.h
   BasicString.h
   bbs_ctra.cpp
   bbs_data.cpp
   bbs_incl.h
   bbs_io.cpp
   bink.h
   bink_asm.cpp
   binkdlg.cpp
   binkpipe.h
   box.h
   brec.cpp
   bsend.cpp
   bt.cpp
   btconfig.cpp
   btutil.cpp
   buffer.h
   cache.cpp
   cache.h
   callback.cpp
   callerid.cpp
   callwin.cpp
   callwin.h
   capi.cpp
   cfosline.cpp
   chat.cpp
   com.h
   com_dos.h
   com_lnx.h
   com_os2.h
   com_w32.h
   common.cpp
   cpuload.cpp
   css.cpp
   css.h
   data.cpp
   debug.cpp
   debug.h
   defines.h
   dosfuncs.cpp
   emsi.cpp
   emxmxcom.cpp
   evtparse.cpp
   evtsetup.cpp
   exceptio.cpp
   exceptio.h
   Exceptions.cpp
   Exceptions.h
   externs.h
   faxproto.h
   fcfgread.cpp
   fidomail.h
   file_all.cpp
   file_dos.cpp
   file_io.h
   file_lnx.cpp
   file_os2.cpp
   file_w32.cpp
   foffsets.h
   fossil.cpp
   freepoll.cpp
   ftsc.cpp
   gethcomm.h
   heap.cpp
   history.cpp
   history.h
   hydra.cpp
   hydra.h
   includes.cpp
   includes.h
   janus.cpp
   janus.h
   keybd.h
   keyfncs.h
   keymap.cpp
   langload.cpp
   language.h
   lngmagic.h
   m7rec.cpp
   m7send.cpp
   mailer.cpp
   mailovly.cpp
   mailroot.cpp
   maxcomm.h
   maxmcp.cpp
   maxmcp.h
   maxprm.cpp
   mdm_proc.cpp
   misc.cpp
   msgs.h
   Node.cpp
   Node.h
   NodeList.cpp
   NodeList.h
   nodeproc.cpp
   os2_pm.cpp
   outbound.cpp
   pipe.cpp
   pktmsgs.cpp
   pktmsgs.h
   protcomm.cpp
   prototyp.h
   recvbark.cpp
   recvsync.cpp
   sbuf.cpp
   sbuf.h
   sched.cpp
   sched.h
   script2.cpp
   SelfTest.cpp
   SelfTest.h
   sendbark.cpp
   sendsync.cpp
   squish.cpp
   squish.h
   srif.cpp
   statetbl.cpp
   stats.cpp
   stdafx.cpp
   stdafx.h
   timer.cpp
   timer.h
   times.cpp
   types.h
   v7debug.cpp
   version.cpp
   version7.cpp
   version7.h
   vfos_dos.cpp
   vfos_lnx.cpp
   vfos_os2.cpp
   vfos_qnx.cpp
   vfos_w32.cpp
   vfossil.h
   video.h
   vt100.cpp
   vt100.h
   xfer.h
   xmrec.cpp
   xmsend.cpp
   xstring.h
   yoohoo.cpp
   zmodem.cpp
   zmodem.h

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
/*---------------------------------------------------------------------------*
 *
 *                               BinkleyTerm
 *
 *              (C) Copyright 1987-96, Bit Bucket Software Co.
 *     For license and contact information see /doc/orig_260/license.260.
 *
 *           This version was modified by the BinkleyTerm XE Team.
 *        For contact information see /doc/team.lst and /doc/join.us.
 *  For a complete list of changes see /doc/xe_user.doc and /doc/xe_hist.doc.
 *
 * Filename    : $Source: /cvsroot/btxe/btnt/sources/asyn_os2.cpp,v $
 * Revision    : $Revision: 1.1 $
 * Tagname     : $Name:  $
 * Last updated: $Date: 2005/01/01 15:18:10 $
 * State       : $State: Exp $
 * Orig. Author: Peter Fitzsimmons
 *
 * Description : BinkleyTerm OS/2 Async Comm I/O Routines
 *
 *---------------------------------------------------------------------------*/

/* Include this file before any other includes or defines! */

#include "includes.h"

/*
 * code snippet from Harald Pollack, 2:310/14.59
 * intializes com port to use fifo, CTS/RTS, 8n1
 *  
 * APIRET APIENTRY comport_init(BOOL fifo)
 * {
 *   APIRET      rc;
 *   LINECONTROL LineCntrl;
 * 
 *   if (fifo)
 *   {
 *     if (dcbCom.fbTimeout & 0x18) // DISABLE, ENABLE or AUTO
 *     {
 *       status_line (">PortInit Enable Fifo Rx=8 Tx=16");
 *       dcbCom.fbTimeout &= ~0x18; // DISABLE
 *       dcbCom.fbTimeout |= 0x10; // ENABLE
 *       dcbCom.fbTimeout &= ~0x60; // Rx=1
 *       dcbCom.fbTimeout |= 0x40; // Rx=8
 *       dcbCom.fbTimeout |= 0x80; // Tx=16
 *     }
 *     else
 *       status_line (">No Fifo detected!");
 *   }
 * 
 *   status_line (">PortInit CTS/RTS-Handshake DTR-Control XON/XOFF disabled");
 *   dcbCom.fbCtlHndShake = MODE_CTS_HANDSHAKE | MODE_DTR_CONTROL;
 *   dcbCom.fbFlowReplace = MODE_RTS_HANDSHAKE;
 *   rc = DevIOCtl(NULL,0,&dcbCom,sizeof(dcbCom),ASYNC_SETDCBINFO,IOCTL_ASYNC,hTTY);
 *   if (rc) return rc;
 * 
 *   status_line (">PortInit DataBits 8n1");
 *   LineCntrl.bDataBits  = 8;     // 8 Databits
 *   LineCntrl.bParity    = 0;     // no Parity
 *   LineCntrl.bStopBits  = 0;     // 1 Stopbit
 *   rc = DevIOCtl(NULL,0,&LineCntrl,sizeof(LineCntrl),ASYNC_SETLINECTRL,IOCTL_ASYNC,hTTY);
 * 
 *   rc = raise_dtr();
 *   if (rc) return rc;
 * 
 *   return(0);
 * }
 */

#ifndef NEED_OS2COMMS

/* These must be in "includes.h" for C7 precompiled headers to work (as if
   we'll ever get any MS support for OS/2-targetted C7!!!)

   #define INCL_DOS
   #define INCL_DOSERRORS
   #define INCL_DOSDEVIOCTL
   #include "includes.h"   (And this I put up top to avoid confusion) */

/* This module by Peter Fitzsimmons */
/* Modified 6/13/92 for exit (3) WRA */

char ascii_comport[] = "COM";

HCOMM hcModem = 0;              /* comm.dll handle */

#define RBSIZE  8200            /* MB */
#define TBSIZE  8200            /* MB */

USHORT rBufsize = RBSIZE;
USHORT tBufsize = TBSIZE;

/* CFS 961120 - an attempt to make BT-XE work without CFOS's -kx */
/* (SIO compatibility). */

void
com_DTR_on (void)
{
    MODEMSTATUS ModemStatus;
    USHORT usComError;
    DCBINFO sDCB;
    USHORT rc;

    if (NoModem)
        return;

    if (SIOMode)
    {
        ModemStatus.fbModemOn = DTR_ON;
        ModemStatus.fbModemOff = 0;
        DosDevIOCtl (ComGetFH (hcModem), IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
                     (PVOID) & ModemStatus, sizeof (MODEMSTATUS), NULL,
                     (PVOID) & usComError, sizeof (USHORT), NULL);
    }
    else
    {
        if (!(rc = ComGetDCB (hcModem, &sDCB)))
        {
            sDCB.fbCtlHndShake |= MODE_DTR_CONTROL;  /* raise DTR */
            ComSetDCB (hcModem, &sDCB);
        }
        else
        {
            status_line ("!SYS%04hu: ComGetDCB()", rc);
            exit (3);
        }
    }
}

void
com_DTR_off (void)
{
    MODEMSTATUS ModemStatus;
    USHORT usComError;
    DCBINFO sDCB;
    USHORT rc;

    if (NoModem)
        return;

    if (SIOMode)
    {
        ModemStatus.fbModemOn = 0;
        ModemStatus.fbModemOff = DTR_OFF;
        DosDevIOCtl (ComGetFH (hcModem), IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
                     (PVOID) & ModemStatus, sizeof (MODEMSTATUS), NULL,
                     (PVOID) & usComError, sizeof (USHORT), NULL);
    }
    else
    {
        if (!(rc = ComGetDCB (hcModem, &sDCB)))
        {
            sDCB.fbCtlHndShake &= ~MODE_DTR_CONTROL;  /* lower DTR */
            ComSetDCB (hcModem, &sDCB);
            com_kick ();
        }
        else
        {
            status_line ("!SYS%04hu: ComGetDCB()", rc);
            exit (3);
        }
    }
}

void
com_XON_disable (void)
{
    DCBINFO sDCB;
    USHORT rc;

    if (NoModem)
        return;

    if (!(rc = ComGetDCB (hcModem, &sDCB)))
    {
        /* disable auto Xmit and recv flow control */

        sDCB.fbFlowReplace &= ~(MODE_AUTO_TRANSMIT | MODE_AUTO_RECEIVE);
        ComSetDCB (hcModem, &sDCB);
        com_kick ();
    }
    else
    {
        status_line ("!SYS%04hu: ComGetDCB()", rc);
        exit (3);
    }
}

void
com_XON_enable (void)
{
    DCBINFO sDCB;
    USHORT rc;

    if (NoModem)
        return;

    if (!(rc = ComGetDCB (hcModem, &sDCB)))
    {
        /* enable auto Xmit and recv flow control */

        sDCB.fbFlowReplace |= MODE_AUTO_TRANSMIT;  /*PLF Wed  04-04-1990  02:35:41 */
        ComSetDCB (hcModem, &sDCB);
    }
    else
    {
        status_line ("!SYS%04hu: ComGetDCB()", rc);
        exit (3);
    }
}

int
com_getc (int t)
{
    if (NoModem)
        return (0);

    if (t)
        ComRxWait (hcModem, t * 1000L);
    return ComGetc (hcModem);
}

/* com_break() : start break if on==TRUE, stop break if on==FALSE */

void
com_break (int on)
{
    ULONG cmd;
    HFILE hf;
    PVOID ParmList;
    ULONG ParmLengthMax;
    ULONG ParmLengthInOut;
    UCHAR DataArea[2];
    ULONG DataLengthMax;
    ULONG DataLengthInOut;
    APIRET rc;

    if (NoModem)
        return;

    ParmList = 0;
    ParmLengthInOut = 0;
    ParmLengthMax = 0;

    DataLengthInOut = 0;
    DataLengthMax = 2;

    cmd = (on) ? ASYNC_SETBREAKON : ASYNC_SETBREAKOFF;
    hf = ComGetFH (hcModem);
    if (hf)
    {
        /* rc = DosDevIOCtl(&comerr, 0L, cmd, IOCTL_ASYNC, hf); */
        rc = DosDevIOCtl (hf, IOCTL_ASYNC, cmd, ParmList, ParmLengthMax,
                          &ParmLengthInOut, DataArea, DataLengthMax,
                          &DataLengthInOut);
        if (rc)
            status_line ("!SYS%04ld: ASYNC_SETBREAK", rc);
    }
}

void
MDM_ENABLE (unsigned long rate)
{
    char _parity;
    short databits;
    short stopbits;

    if (NoModem)
        return;

    if (!hcModem)
        return;

    if (lock_baud && rate != max_baud.rate_value)
    {
        status_line (">Pretending baud is %lu (locked at %lu)", rate, max_baud.rate_value);
        rate = max_baud.rate_value;
    }

    databits = 7 + (comm_bits == BITS_8);
    stopbits = 1 + (stop_bits == STOP_2);

    switch (parity)
    {
    case NO_PARITY:
        _parity = 'N';
        break;
    case ODD_PARITY:
        _parity = 'O';
        break;
    case EVEN_PARITY:
        _parity = 'E';
        break;
    default:
        _parity = 'N';
        status_line ("!Invalid parity");
    }

    ComSetBaudRate (hcModem, (long) rate, _parity, (USHORT) databits, (USHORT) stopbits);
}

void
MDM_DISABLE (void)
{
    if (NoModem)
        return;

    if (hcModem)
    {
        ComClose (hcModem);
        hcModem = 0;
    }
}

void
ShowMdmSettings (void)
{
    DCBINFO dcb;
    USHORT rc;
    char *On = "On";
    char *Off = "Off";
    char *dtr;
    char *rts;
    char *buffer;
    short Rx = 0, Tx;

    if (NoModem)
        return;

    if (!(rc = ComGetDCB (hcModem, &dcb)))
    {
        status_line (">Modem: TO=%s,XON(Rx)=%s,XON(Tx)=%s",
                     (dcb.fbTimeout & MODE_NO_WRITE_TIMEOUT) ? On : Off,
                     (dcb.fbFlowReplace & MODE_AUTO_RECEIVE) ? On : Off,
                     (dcb.fbFlowReplace & MODE_AUTO_TRANSMIT) ? On : Off);
        status_line (">Modem: IDSR=%s,ODSR=%s,OCTS=%s",
                     (dcb.fbCtlHndShake & MODE_DSR_SENSITIVITY) ? On : Off,
                     (dcb.fbCtlHndShake & MODE_DSR_HANDSHAKE) ? On : Off,
                     (dcb.fbCtlHndShake & MODE_CTS_HANDSHAKE) ? On : Off);
        switch (dcb.fbCtlHndShake & (MODE_DTR_CONTROL | MODE_DTR_HANDSHAKE))
        {
        case 0:
            dtr = Off;
            break;
        case MODE_DTR_CONTROL:
            dtr = On;
            break;
        case MODE_DTR_HANDSHAKE:
            dtr = "IHS";
            break;                    /* input handshaking */
        default:
            dtr = "??";
            break;
        }

        switch (dcb.fbFlowReplace & (MODE_RTS_CONTROL | MODE_RTS_HANDSHAKE | MODE_TRANSMIT_TOGGLE))
        {
        case 0:
            rts = Off;
            break;
        case MODE_RTS_CONTROL:
            rts = On;
            break;
        case MODE_RTS_HANDSHAKE:
            rts = "IHS";
            break;
        case MODE_TRANSMIT_TOGGLE:
            rts = "TOG";
            break;
        default:
            rts = "??";
            break;
        }

        switch (dcb.fbTimeout & 0x18)
        {
        case 0x08:
            buffer = Off;
            break;
        case 0x10:
            buffer = On;
            break;
        case 0x18:
            buffer = "Auto";
            break;
        default:
            buffer = "??";
            break;
        }

        switch (dcb.fbTimeout & 0x60)
        {
        case 0:
            Rx = 1;
            break;
        case 0x20:
            Rx = 4;
            break;
        case 0x40:
            Rx = 8;
            break;
        case 0x60:
            Rx = 14;
            break;
        }

        Tx = (dcb.fbTimeout & 0x80) ? 16 : 1;
        status_line (">Modem: DTR=%s,RTS=%s,BUFFER=%s (Rx=%hd, Tx=%hd)",
                     dtr,
                     rts,
                     buffer,
                     Rx, Tx);
        status_line (">Modem: Buffers R: %hu T: %hu", rBufsize, tBufsize);  /* MB 94-01-02 */
    }
    else
    {
        status_line ("!SYS%04hu: ComGetDCB()", rc);
        exit (3);
    }
}

unsigned
Com_init (int port)
{
    USHORT rc;
    char *s;
    char dev[256];

    if (NoModem)
        return (0);


    if (port_device == NULL)
        port_device = ascii_comport;

    sprintf (dev, "%s%d", port_device, port + 1);

    printf ("Async device %s\r\n", dev);

    s = getenv ("RBUF");          /* MB 94-01-02 */
    if (s)
        rBufsize = (USHORT) min (32000, atoi (s));
    else
        rBufsize = RBSIZE;

    s = getenv ("TBUF");
    if (s)
        tBufsize = (USHORT) min (32000, atoi (s));
    else
        tBufsize = TBSIZE;

    rc = ComOpen ((PSZ) dev, &hcModem, rBufsize, tBufsize);
    if (rc)
    {
        status_line ("!SYS%04hu:  ComOpen(%s)", rc, dev);
        return (rc);
    }
    else
        ShowMdmSettings ();

    return (rc);
}

unsigned
Cominit (int port, int failsafe)
{
    int ret = 0x1954;

    failsafe = failsafe;

    if (NoModem)
        return (ret);

    if (!hcModem)
    {
        if (Com_init (port))
            ret = 0;
    }
    else
        ComResume (hcModem);

    return ret;
}

/* force transmitter to go */
void
com_kick (void)
{
    HFILE hf;
    PVOID ParmList;
    ULONG ParmLengthMax;
    ULONG ParmLengthInOut;
    PVOID DataArea;
    ULONG DataLengthMax;
    ULONG DataLengthInOut;
    APIRET rc;

    if (NoModem)
        return;

    ParmList = 0;
    ParmLengthInOut = 0;
    ParmLengthMax = 0;

    DataArea = 0;
    DataLengthInOut = 0;
    DataLengthMax = 0;

    if (hcModem)
    {
        hf = ComGetFH (hcModem);
        if (hf)
        {
            rc = DosDevIOCtl (hf, IOCTL_ASYNC, ASYNC_STARTTRANSMIT, ParmList, ParmLengthMax,
                              &ParmLengthInOut, DataArea, DataLengthMax,
                              &DataLengthInOut);
            if (rc && rc != 31)
            {
                status_line ("!SYS%04ld: ASYNC_STARTTRANSMIT", rc);
                /* exit (3); */
            }
        }
    }
}

/* Currently only used by HYDRA */

/*
 * This is a lot like ComWrite, but will return the sent-character
 * count if either a timeout occurs or carrier is lost. The timer
 * is specified by passing in a time_t for end-of-time. If zero is
 * passed for the timer, we'll just check for carrier loss.
 *
 */

USHORT
ComTXBlockTimeout (BYTE * lpBuf, USHORT cbBuf, ULONG ulTimer)
{
    USHORT cbRemaining = cbBuf;
    USHORT cbToSend;
    int fTimedOut;

    if (NoModem)
        return (0);

    fTimedOut = (ulTimer != 0) ? (ulTimer < (ULONG) unix_time (NULL)) : FALSE;
    while (!fTimedOut && CARRIER && cbRemaining)
    {
        cbToSend = ComOutSpace (hcModem);
        if ((--cbToSend) > 0)
        {
            if (cbToSend > cbRemaining)
                cbToSend = cbRemaining;
            ComWrite (hcModem, lpBuf, cbToSend);
            lpBuf += cbToSend;
            cbRemaining -= cbToSend;
        }
        else
            time_release ();
        fTimedOut = (ulTimer != 0) ? (ulTimer < (ULONG) unix_time (NULL)) : FALSE;
    }
    return (cbBuf - cbRemaining);
}

USHORT
ComTXRemain (void)              /* MB 93-12-03 */
{
    if (NoModem)
        return (0);

    return (tBufsize - ComOutSpace (hcModem));
}

#else // NEED_OS2COMMS

#define __BTPE_DEBUG
#define __IOCTL_DEBUG
#define __LOG_TO_FILE

#include "async.h"

/* Private data */
HFILE hfComHandle = NULLHANDLE; /* handle from DosOpen() */
char ascii_comport[] = "COM";

/* transmitter stuff */
#define TSIZE 8192
static unsigned char *tBuf = NULL;
static unsigned char *zTxBuf = NULL;
static int zpos = 0;
static int tBufsize = 0;
static ULONG tpos = 0;
static ULONG tlen = 0;
static BOOL32 thread = FALSE;
HEV buffer_empty, buffer_full;

/* receiver stuff */
#define RSIZE 32768
static unsigned char *rbuf = NULL;
static ULONG rpos = 0;
static int rbufsize = 0;
static ULONG Rbytes = 0;

/* prototypes of local functions */

static APIRET IOCTL (HFILE, ULONG, ULONG, PVOID, ULONG, PULONG, PVOID,
                     ULONG, PULONG, PCHAR);
static int com_getDCB (DCBINFO * dcb);
static int com_setDCB (DCBINFO * dcb);
static void ShowMdmSettings (void);
static void setup_port (void);
static void HydraWrite (PVOID);

#ifdef  BTPE_DEBUG
#define debug_msg(m,c)  status_line(">" m, c)
#else
#define debug_msg(m,c)
#endif

#ifdef LOG_TO_FILE
static FILE *every = NULL;
static char *dbgfnm;
static void logAll (int ch, int type);
static int peeking = 0;

#endif

/* com_init() : Intialize communications port. Baud rate is preserved.
*            int port  : Hardware port (0 based) to init
*    -OR-  char *unc   : Full UNC \\networkId\modemId to init.
*
* if unc==NULL, port is used. if unc != NULL, unc is used
*/
int
com_init (int port, char *unc)
{
    static char did_it = 0;
    char str[30];
    ULONG ActionTaken, share_mode;
    APIRET ulrc;

    if (!did_it)
    {
        printf ("\033[1;33mOS/2 FOSSIL emulator for BinkleyTerm, Peter Fitzsimmons  (1:250/628)\033[0m\r\n");
        printf ("\033[1;33mSupport for Hydra added and bugfixes, Steffen Motzer     (2:2471/1071.3)\033[0m\r\n");
        did_it = 1;
    }

    if (port_device == NULL)
        port_device = ascii_comport;

    sprintf (str, "%s%d", port_device, port + 1);

    if (!unc)
        unc = str;

    share_mode = share_port ? OPEN_SHARE_DENYNONE : OPEN_SHARE_DENYREADWRITE;
#ifdef BTPE_DEBUG
    ulrc = DosOpen ((PSZ) unc, &hfComHandle, &ActionTaken, 0L,
                    FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
                    OPEN_FLAGS_WRITE_THROUGH | share_mode |
                    OPEN_ACCESS_READWRITE, 0L);
#else
ulrc = DosOpen ((PSZ) unc, &hfComHandle, &ActionTaken, 0L,
FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
OPEN_FLAGS_WRITE_THROUGH | share_mode |
OPEN_ACCESS_READWRITE | OPEN_FLAGS_FAIL_ON_ERROR, 0L);
#endif
    if (ulrc != NO_ERROR)
    {
        hfComHandle = NULLHANDLE;
        status_line ("!com_init() : DosOpen() error 0x%x on '%s'\n", ulrc, unc);
        return ulrc;
    }

    if (rbuf == NULL)
    {
        char *s = getenv ("RBUF");

        if (s)
            rbufsize = max (H_MAXBLKLEN * 3, atoi (s));
        else
            rbufsize = RSIZE;
        rbuf = malloc (rbufsize + 1);
    }
    if (zTxBuf == NULL)
    {
        char *s = getenv ("TBUF");

        if (s)
            tBufsize = min (TSIZE, atoi (s));
        else
            tBufsize = TSIZE;
        zTxBuf = malloc (tBufsize + 1);
    }
    setup_port ();
    ShowMdmSettings ();
    return (NO_ERROR);
}

void
com_DTR_on (void)
{
    DCBINFO sDCB;
    MODEMSTATUS ModemStatus;
    ULONG Data;

    if (SIOMode)
    {
        ModemStatus.fbModemOn = DTR_ON;
        ModemStatus.fbModemOff = 0;
        IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
               (PVOID) & ModemStatus, sizeof (MODEMSTATUS), NULL,
               (PVOID) & Data, sizeof (ULONG), NULL, "com_DTR_on()");
    }
    else
    {
        com_getDCB (&sDCB);
        sDCB.fbCtlHndShake |= MODE_DTR_CONTROL;  /* raise DTR */
        com_setDCB (&sDCB);
    }
    debug_msg ("DTR %s", "on");
}

void
com_DTR_off (void)
{
    DCBINFO sDCB;
    MODEMSTATUS ModemStatus;
    ULONG Data;

    if (SIOMode)
    {
        ModemStatus.fbModemOn = 0;
        ModemStatus.fbModemOff = DTR_OFF;
        IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
               (PVOID) & ModemStatus, sizeof (MODEMSTATUS), NULL,
               (PVOID) & Data, sizeof (ULONG), NULL, "com_DTR_off()");
    }
    else
    {
        com_getDCB (&sDCB);
        sDCB.fbCtlHndShake &= ~MODE_DTR_CONTROL;  /* lower DTR */
        com_setDCB (&sDCB);
    }

    debug_msg ("DTR %s", "off");

#ifdef LOG_TO_FILE
    if (every != NULL)
    {
        fflush (every);
    }
#endif
    zpos = 0;                     /* "remove" the characters from the buffer */
}

/* close communications channel. Baud rate is preserved. */
int
com_fini (void)
{
    int stat;

    if (hfComHandle != NULLHANDLE)
    {
        if (thread)
        {
            thread = FALSE;
            DosPostEventSem (buffer_full);
            DosSleep (500);
        }
        com_wait ();
        com_clear_in ();
        com_clear_out ();
        stat = DosClose (hfComHandle);
        hfComHandle = NULLHANDLE;
#ifdef LOG_TO_FILE
        logAll (0, '-');
#endif
        if (stat)
        {
            debug_msg ("Async: DosClose() error 0x%x", stat);
            return (FALSE);
        }
    }
    return (TRUE);
}

long
com_cur_baud (void)
{
    EXTBAUDRATE extrate;

    if (hfComHandle == NULLHANDLE)
        return (0);

    IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_EXTGETBAUDRATE,
           NULL, 0, NULL, &extrate, sizeof extrate, NULL, "com_cur_baud()");
    return ((long) extrate.cur_bps);
}

/* com_set_baud() :

*  rate = 110..19200
*  parity = N, E, O, M, S (none,even, odd, mark, space)
*  databits = 5..8
*  stopbits = 1..2
*
*/
int
com_set_baud (unsigned long ratei, char parity, int databits, int stopbits)
{
    APIRET stat;
    ULONG ratel = ratei;
    struct _LINECONTROL
    {
        BYTE bDataBits;
        BYTE bParity;
        BYTE bStopBits;
        BYTE fbTransBreak;
    }
    lc;

#ifdef BTPE_DEBUG
    static long cnt = 0;

    cnt++;
#endif

    if (hfComHandle == NULLHANDLE)
        return (0);

    stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_EXTSETBAUDRATE,
                  &ratel, sizeof ratel, NULL, NULL, 0, NULL, "com_set_baud()");
    if (stat)
    {
#ifdef BTPE_DEBUG
        if (cnt <= 2)
            printf (">status from setbaudrate was %d\n", stat);
        else
            status_line (" status from setbaudrate was %d", stat);
#endif
        return (FALSE);
    }

    lc.bDataBits = (BYTE) databits;

    switch (stopbits)
    {
    case 1:
        lc.bStopBits = 0;
        break;
    case 2:
        lc.bStopBits = 2;
        break;
    default:
        if (databits == 5)
            lc.bStopBits = 1;
    }

    lc.fbTransBreak = 0;

    switch (toupper (parity))
    {
    case 'N':
        lc.bParity = 0;
        break;
    case 'O':
        lc.bParity = 1;
        break;
    case 'E':
        lc.bParity = 2;
        break;
    case 'M':
        lc.bParity = 3;
        break;
    case 'S':
        lc.bParity = 4;
        break;
    default:
        debug_msg ("Bad parity '%c'", parity);
        return (FALSE);
    }

    stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_SETLINECTRL,
                  &lc, sizeof lc, NULL, NULL, 0, NULL, "com_set_baud()");

    return (!stat);
}

static int
com_getDCB (DCBINFO * dcb)
{
    APIRET stat;

    if (hfComHandle == NULLHANDLE)
        return (0);

    stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETDCBINFO,
                  NULL, 0, NULL, dcb, sizeof *dcb, NULL, "com_getDCB()");

    return (!stat);
}

static int
com_setDCB (DCBINFO * dcb)
{
    APIRET stat;

    if (hfComHandle == NULLHANDLE)
        return (0);

    stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_SETDCBINFO,
                  dcb, sizeof *dcb, NULL, NULL, 0, NULL, "com_setDCB()");

    return (!stat);
}

void
com_XON_disable (void)
{
    DCBINFO sDCB;

    if (com_getDCB (&sDCB))
    {
        /* disable auto Xmit and recv flow control */
        sDCB.fbFlowReplace &= ~(MODE_AUTO_TRANSMIT | MODE_AUTO_RECEIVE);
        com_setDCB (&sDCB);
    }
    com_kick ();

}

void
com_XON_enable (void)
{
    DCBINFO sDCB;

    if (com_getDCB (&sDCB))
    {
        /* enable auto Xmit and recv flow control */
        sDCB.fbFlowReplace |= (MODE_AUTO_TRANSMIT | MODE_AUTO_RECEIVE);
        com_setDCB (&sDCB);
    }
}

/* nuke receive buffer */
void
com_clear_in (void)
{
    char FlushParm = 0;           /* param to flush IOCTL function */

    if (hfComHandle == NULLHANDLE)
        return;

    Rbytes = rpos = 0;
    IOCTL (hfComHandle, IOCTL_GENERAL, DEV_FLUSHINPUT,
           (PVOID) & FlushParm, sizeof FlushParm, NULL,
           NULL, 0, NULL, "com_clear_in()");
}

/* com_getbuf() : return negative value if error */
int
com_getbuf (void)
{
    APIRET stat = 0;
    ULONG bytes;
    RXQUEUE q;

    if (hfComHandle == NULLHANDLE)
        return (-1);

    if (rpos > 1024)
    {
        memmove (rbuf, rbuf + rpos, Rbytes - rpos);  /* SM 961127 */
        Rbytes -= rpos;
        rpos = 0;
    }

    if (!IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETINQUECOUNT,
                NULL, 0, NULL, &q, sizeof q, NULL, "com_getbuf()"))
    {
        if (q.cch > 0)
        {
            stat = DosRead (hfComHandle, rbuf + Rbytes, (USHORT) min (rbufsize -
                            Rbytes,
                            q.cch), &bytes);
        }
        else
            return (-1);
    }
    else
        return (-1);

    if (stat && !bytes && (!(stat == ERROR_MORE_DATA)))
    {
        debug_msg ("DosRead() error 0x%x", stat);
        return (-1);
    }

    Rbytes += bytes;
    return (TRUE);
}

/* com_getchar() : return negative value if error */
int
com_getchar (void)
{
    int ret;

    if (rpos == Rbytes)
        com_getbuf ();

    if (rpos < Rbytes)
        ret = rbuf[rpos++];
    else
        ret = -1;

#ifdef LOG_TO_FILE
    if ((ret != -1) && !peeking)
    {
        logAll (ret, 'R');
    }
    peeking = 0;
#endif
    return (ret);
}

/*non destructive read ahead; no wait */
int
com_peek (void)
{

#ifdef LOG_TO_FILE
    peeking = 1;
#endif

    if (rpos == Rbytes)
        com_getbuf ();
    return (rpos == Rbytes) ? -1 : rbuf[rpos];
}

/* if there are enough characters in RXQueue , return TRUE */
bool
com_in_check (void)
{
    RXQUEUE q;
    APIRET stat;

    if (hfComHandle == NULLHANDLE)
        return (FALSE);

    stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETINQUECOUNT,
                  NULL, 0, NULL, &q, sizeof q, NULL, "com_in_check()");
    if (stat)
        return (FALSE);
    return (q.cch > (q.cb / 2)) ? TRUE : FALSE;
}

/* return number of chars in input buffer */
int
com_char_avail (void)
{
    RXQUEUE q;

    if (rpos < Rbytes)
        return (Rbytes - rpos);

    if (hfComHandle == NULLHANDLE)
        return (0);

    if (IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETINQUECOUNT,
               NULL, 0, NULL, &q, sizeof q, NULL, "com_char_avail()"))
        return (0);

    return (q.cch);
}

/* return non 0 value if carrier detected */
bool
com_online (void)
{
    BYTE msr;
    APIRET rc;

    if (hfComHandle == NULLHANDLE)
        return (FALSE);

    rc = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETMODEMINPUT,
                NULL, 0, NULL, &msr, sizeof msr, NULL, "com_online()");

    if (rc)
        return (FALSE);

    return ((msr & DCD_ON) == DCD_ON);
}

/* com_break() : start break if on==TRUE, stop break if on==FALSE */
void
com_break (int on)
{
    int cmd;
    USHORT comerr;

    cmd = (on) ? ASYNC_SETBREAKON : ASYNC_SETBREAKOFF;
    IOCTL (hfComHandle, IOCTL_ASYNC, cmd,
           NULL, 0, NULL, &comerr, sizeof comerr, NULL, "com_break()");
}

/* com_out_empty() : return TRUE if output buffer is empty */
bool
com_out_empty (void)
{
    RXQUEUE q;

    if (hfComHandle == NULLHANDLE)
        return (TRUE);

    /* Service Inbound side if necessary */
    if (com_in_check ())
        com_getbuf ();

    if (zpos != 0)
        UNBUFFER_BYTES ();

    if (IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETOUTQUECOUNT,
               NULL, 0, NULL, &q, sizeof q, NULL, "com_out_empty()"))

        return (TRUE);

    return ((bool) (q.cch == 0));
}

/* nuke transmit buffer */
void
com_clear_out (void)
{
    char FlushParm = 0;           /* param to flush IOCTL function */

    zpos = 0;

    if (hfComHandle == NULLHANDLE)
        return;
    IOCTL (hfComHandle, IOCTL_GENERAL, DEV_FLUSHOUTPUT,
           (PVOID) & FlushParm, sizeof FlushParm, NULL,
           NULL, 0, NULL, "com_clear_out()");
    com_kick ();
}

/* com_putc() : output to com port */
/* This function is very slow..where possile, write to com port in blocks
* using com_write() instead...especially above 2400 bps
*/
void
com_putc (byte c)
{
    ULONG bytes;
    APIRET stat;
    RXQUEUE q;

    if (hfComHandle == NULLHANDLE)
        return;

#ifdef LOG_TO_FILE
    logAll (c, 'S');
#endif
    if (com_in_check ())
        com_getbuf ();
    do
    {
        if (com_in_check ())
            com_getbuf ();
        stat = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETOUTQUECOUNT,
                      NULL, 0, NULL, &q, sizeof q, NULL, "com_putc()");
        if (stat != NO_ERROR)
            return;

        if (q.cch >= q.cb)
        {
            com_kick ();
            DosSleep (1L);
        }
    }
    while ((q.cch >= q.cb) && com_online ());

    stat = DosWrite (hfComHandle, &c, 1L, &bytes);
    if (stat)
    {
        stat = stat;                /* to prevent a compiler warning in case of no DEBUG */
        debug_msg ("DosWrite() err 0x%x", stat);
    }
}

/* com_write() : buffered block write */
void
com_write (char *buf, unsigned int num, int carcheck)
{
    ULONG bytes;
    APIRET rc;

#ifdef LOG_TO_FILE
    int x;

#endif

    if (hfComHandle == NULLHANDLE)
        return;

#ifdef LOG_TO_FILE
    logAll ((unsigned char) buf[0], 'S');
    if (every != NULL)
    {
        for (x = 1; x < num; x++)
        {
            logAll ((unsigned char) buf[x], 'S');
        }
    }
#endif

    if ((carcheck) && !com_online ())
        return;

    if (com_in_check ())
        com_getbuf ();

    rc = DosWrite (hfComHandle, buf, (ULONG) num, &bytes);
    if (rc)
    {
        rc = rc;                    /* to prevent a compiler warning in case of no DEBUG */
        debug_msg ("DosWrite() err 0x%x", rc);
    }
    rc = rc;
}

/* wait for output buffer to empty */
void
com_wait (void)
{
    while ((!com_out_empty ()) && com_online ())
        DosSleep (1L);
}

/* force transmitter to go */
void
com_kick (void)
{
    if (hfComHandle == NULLHANDLE)
        return;

    IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_STARTTRANSMIT,
           NULL, 0, NULL, NULL, 0, NULL, "com_kick()");
}

extern unsigned int comm_bits;
extern unsigned int parity;
extern unsigned int stop_bits;
extern struct baud_str btypes[];

void
MDM_ENABLE (unsigned long rate)
{
    char _parity;
    int databits;
    int stopbits;

    if (lock_baud && rate != max_baud.rate_value)
    {
        status_line (">Pretending baud is %lu (locked at %lu)", rate, max_baud.rate_value);
        rate = max_baud.rate_value;
    }

    databits = 7 + (comm_bits == BITS_8);
    stopbits = 1 + (stop_bits == STOP_2);

    switch (parity)
    {
    case NO_PARITY:
        _parity = 'N';
        break;
    case ODD_PARITY:
        _parity = 'O';
        break;
    case EVEN_PARITY:
        _parity = 'E';
        break;
    default:
        _parity = 'N';
        status_line ("!Invalid parity");
    }

    com_set_baud (rate, _parity, databits, stopbits);
}

void
MDM_DISABLE (void)
{
    if (hfComHandle == NULLHANDLE)
        return;

    com_clear_out ();
    com_clear_in ();
    com_fini ();
}

/* zsend.c uses BUFFER_BYTE and UNBUFFER_BYTES...good idea. */
void
BUFFER_BYTE (unsigned char ch)
{
    if (zpos == tBufsize)
        UNBUFFER_BYTES ();
    zTxBuf[zpos++] = ch;
}

void
UNBUFFER_BYTES (void)
{
    if (com_online () && zpos)
        (zpos == 1) ? (com_putc (zTxBuf[0])) :
        (com_write ((char *) zTxBuf, zpos, 1));
    zpos = 0;
}

unsigned
Cominit (int port, int failsafe)
{
    failsafe = failsafe;

    if (NoModem)
        return (0x1954);

    if (hfComHandle == NULLHANDLE)
    {
        if (com_init (port, NULL))
            return 0;
    }
    else
        setup_port ();

    return (0x1954);
}

int
com_getc (int t)
{
    long t1;

    if (NoModem)
        return (0);

    if (!CHAR_AVAIL ())
    {
        t1 = timerset ((unsigned int) (t * PER_SECOND));
        while (!CHAR_AVAIL ())
        {
            if (timeup (t1))
            {
                return (EOF);
            }

            /* This should work because we only do TIMED_READ when we have
            * carrier */
            if (!CARRIER)
            {
                return (EOF);
            }
            time_release ();
        }
    }
    return ((unsigned int) (MODEM_IN ()) & 0x00ff);
}

#ifdef LOG_TO_FILE

static void
logAll (int ch, int type)
{
    static int lastType = 0;
    static int lastchar = 0;
    static time_t lastTime = 0;
    time_t tt;
    struct tm *tms;

    if (debugging_log && (every == NULL))
    {
        dbgfnm = getenv ("BINKDBG");
        if (dbgfnm != NULL)
        {
            every = fopen (dbgfnm, append_ascii);
            if (every != NULL)
            {
                setvbuf (every, NULL, _IOFBF, 1000000);
                logAll (0, 'X');        /* REKURSIVE! */
            }
        }
    }

    if (every == NULL)
        return;

    if ((ch != 0x0d) && (ch != 0x0a))
    {
        if ((lastchar == 0x0d) || (lastchar == 0x0a))
        {
            lastType = 0;
        }
    }

    time (&tt);
    if (tt != lastTime)
    {
        tms = localtime (&tt);
        fprintf (every, "\n\nTime: %.2d:%.2d:%.2d\n",
                 tms->tm_hour, tms->tm_min, tms->tm_sec);
        lastTime = tt;
    }

    if (type != lastType)
    {
        if (type == 'S')
        {
            fprintf (every, "\nSEND: ");
        }
        else if (type == 'R')
        {
            fprintf (every, "\nRECV: ");
        }
        else if (type == 'X')
        {
            fprintf (every, "\nSTART\n");
        }
        else if (type == '-')
        {
            fprintf (every, "\nFINISHED\n");
        }
        lastType = type;
    }

    if (type != 'X')
    {
        if ((ch > 0x1f) && (ch < 0x255))
        {
            fprintf (every, "%c", (char) ch);
        }
        else
        {
            if ((ch == 0x0d) || (ch == 0x0a))
            {
                fprintf (every, " (CR)");
            }
            else
            {
                fprintf (every, " 0x%02x,", ch);
            }
        }
        lastchar = ch;
    }
    if (type == '-')
    {
        fclose (every);
        every = NULL;
    }

    return;
}

#endif

/* Currently only used by HYDRA */

/*
* This is a lot like ComWrite, but will return the sent-character
* count if either a timeout occurs or carrier is lost. The timer
* is specified by passing in a time_t for end-of-time. If zero is
* passed for the timer, we'll just check for carrier loss.
*
*/

USHORT
ComTXBlockTimeout (BYTE * lpBuf, USHORT cbBuf, ULONG ulTimer)
{
    ULONG ulPostCt = 0;
    APIRET ulrc;
    ULONG bytes, tpos2;

    if (hfComHandle == NULLHANDLE)
        return (0);

    if (cbBuf == 0 || ulTimer == 0)
        return 0;                   /* nothing to do */

#ifdef LOG_TO_FILE
    logAll ((unsigned char) lpBuf[0], 'S');
    if (every != NULL)
    {
        int x;

        for (x = 1; x < cbBuf; x++)
        {
            logAll ((unsigned char) lpBuf[x], 'S');
        }
    }
#endif

    tpos2 = 0;

    if (!thread)
    {
        DosCreateEventSem (NULL, &buffer_empty, 0, FALSE);
        DosCreateEventSem (NULL, &buffer_full, 0, FALSE);
        thread = TRUE;
        ulrc = _beginthread (HydraWrite, NULL, 32768L, NULL);
        if (ulrc == -1)
        {
            status_line ("!could not create thread HydraWrite()");
            thread = FALSE;
            return 0;
        }
    }

    if (cbBuf < 1024 && tpos == 0)  /* small chunks of data should be send */
    {
        /* as fast as possible                 */
        do
        {
            if (com_in_check ())
                com_getbuf ();
            ulrc = DosWrite (hfComHandle, lpBuf + tpos2, cbBuf - tpos2, &bytes);
            if (ulrc != NO_ERROR || bytes == 0)
                DosSleep (1);
            if (ulrc == NO_ERROR)
                tpos2 += bytes;
        }
        while ((ulTimer > unix_time (NULL)) && (tpos2 != cbBuf) && com_online ());
    }
    else
    {
        BOOL32 abort = FALSE;
        BOOL32 timeout = FALSE;

        DosWaitEventSem (buffer_empty, SEM_INDEFINITE_WAIT);
        DosResetEventSem (buffer_empty, &ulPostCt);
        tBuf = lpBuf;
        tlen = cbBuf;
        DosPostEventSem (buffer_full);

        do
        {
            DosQueryEventSem (buffer_empty, &ulPostCt);
            if (com_in_check ())
                com_getbuf ();
            abort = keyabort ();
            timeout = ulTimer <= unix_time (NULL);
            time_release ();
        }
        while (ulPostCt < 1 && com_online () && !abort && !timeout);

        if (!com_online () || abort || timeout)
        {
            thread = FALSE;
            DosPostEventSem (buffer_full);
            DosSleep (500);
            if (abort)
                _ungetch (27);
        }
    }

    return cbBuf;
}

void
HydraWrite (PVOID ignore_me)
{
    ULONG bytes, ulPostCt;
    APIRET ulrc;

    ignore_me = ignore_me;        /* make the compiler happy */
    DosPostEventSem (buffer_empty);

    while (thread)                /* if we can write to the port */
    {
        DosWaitEventSem (buffer_full, SEM_INDEFINITE_WAIT);
        DosResetEventSem (buffer_full, &ulPostCt);

        if (tBuf != NULL)
        {
            while (tpos < tlen && thread)  /* buffer not empty */
            {
                ulrc = DosWrite (hfComHandle, tBuf + tpos, tlen - tpos, &bytes);
                if (ulrc != NO_ERROR || bytes == 0)
                    DosSleep (1);
                if (ulrc == NO_ERROR)
                    tpos += bytes;
            }
        }

        tpos = 0;
        tlen = 0;
        DosPostEventSem (buffer_empty);
    }

    DosCloseEventSem (buffer_empty);
    DosCloseEventSem (buffer_full);
    thread = FALSE;               /* comTXBlockTimeout can start an other */
    /* incarnation of this thread later     */
    _endthread ();
}

USHORT
ComTXRemain (void)              /* SM 961109 */
{
    return (Rbytes - rpos) < (H_MAXBLKLEN * 2) ? 0 : H_MAXBLKLEN + 1;
}

USHORT
ComOutSpace (HFILE hfComHandle)    /* SM 961109 */
{
    RXQUEUE q;
    APIRET rc;

    if (hfComHandle == NULLHANDLE)
        return (0);

    rc = IOCTL (hfComHandle, IOCTL_ASYNC, ASYNC_GETOUTQUECOUNT,
                NULL, 0, NULL, &q, sizeof q, NULL, "ComOutSpace()");
    if (rc)
        return (0);
    return (q.cb - q.cch);
}

APIRET
IOCTL (HFILE hDevice, ULONG category, ULONG function,
       PVOID pParams, ULONG cbParmLenMax, PULONG pcbParmLen,
       PVOID pData, ULONG cbDataLenMax, PULONG pcbDataLen,
       PCHAR errtext)
{
    APIRET ulrc;
    ULONG parmlen = cbParmLenMax;
    ULONG datalen = cbDataLenMax;

    if (pParams != NULL && pcbParmLen == NULL)
        pcbParmLen = &parmlen;
    if (pData != NULL && pcbDataLen == NULL)
        pcbDataLen = &datalen;

    ulrc = DosDevIOCtl (hDevice, category, function,
                        pParams, cbParmLenMax, pcbParmLen,
                        pData, cbDataLenMax, pcbDataLen);
#ifdef IOCTL_DEBUG
    if (ulrc != NO_ERROR)
    {
        status_line ("!%s", errtext);
        status_line ("!=================================");
        status_line ("!DosDevIOCtl() error    : %u", ulrc);
        status_line ("!DosDevIOCtl() category : %u", category);
        status_line ("!DosDevIOCtl() function : %u", function);
        status_line ("!=================================");
    }
#else
errtext = errtext;
#endif
    return ulrc;
}

void
ShowMdmSettings (void)
{
    DCBINFO sDCB;
    char *On = "On";
    char *Off = "Off";
    char *dtr;
    char *rts;
    char *buffer;
    short Rx = 0, Tx;

    if (NoModem)
        return;

    if (com_getDCB (&sDCB))
    {
        status_line (">Modem: TO=%s,XON(Rx)=%s,XON(Tx)=%s",
                     (sDCB.fbTimeout & MODE_NO_WRITE_TIMEOUT) ? On : Off,
                     (sDCB.fbFlowReplace & MODE_AUTO_RECEIVE) ? On : Off,
                     (sDCB.fbFlowReplace & MODE_AUTO_TRANSMIT) ? On : Off);
        status_line (">Modem: IDSR=%s,ODSR=%s,OCTS=%s",
                     (sDCB.fbCtlHndShake & MODE_DSR_SENSITIVITY) ? On : Off,
                     (sDCB.fbCtlHndShake & MODE_DSR_HANDSHAKE) ? On : Off,
                     (sDCB.fbCtlHndShake & MODE_CTS_HANDSHAKE) ? On : Off);

        switch (sDCB.fbCtlHndShake & (MODE_DTR_CONTROL | MODE_DTR_HANDSHAKE))
        {
        case 0:
            dtr = Off;
            break;
        case MODE_DTR_CONTROL:
            dtr = On;
            break;
        case MODE_DTR_HANDSHAKE:
            dtr = "IHS";
            break;                    /* input handshaking */
        default:
            dtr = "??";
            break;
        }

        switch (sDCB.fbFlowReplace & (MODE_RTS_CONTROL | MODE_RTS_HANDSHAKE | MODE_TRANSMIT_TOGGLE))
        {
        case 0:
            rts = Off;
            break;
        case MODE_RTS_CONTROL:
            rts = On;
            break;
        case MODE_RTS_HANDSHAKE:
            rts = "IHS";
            break;
        case MODE_TRANSMIT_TOGGLE:
            rts = "TOG";
            break;
        default:
            rts = "??";
            break;
        }

        switch (sDCB.fbTimeout & 0x18)
        {
        case 0x08:
            buffer = Off;
            break;
        case 0x10:
            buffer = On;
            break;
        case 0x18:
            buffer = "Auto";
            break;
        default:
            buffer = "??";
            break;
        }

        switch (sDCB.fbTimeout & 0x60)
        {
        case 0:
            Rx = 1;
            break;
        case 0x20:
            Rx = 4;
            break;
        case 0x40:
            Rx = 8;
            break;
        case 0x60:
            Rx = 14;
            break;
        }

        Tx = (sDCB.fbTimeout & 0x80) ? 16 : 1;
        status_line (">Modem: DTR=%s,RTS=%s,BUFFER=%s (Rx=%hd, Tx=%hd)",
                     dtr,
                     rts,
                     buffer,
                     Rx, Tx);
        status_line (">Modem: Buffers R: %hu T: %hu", rbufsize, tBufsize);
    }
}

void
setup_port ()
{
    DCBINFO sDCB;

    if (NoModem)
        return;

    com_getDCB (&sDCB);
    sDCB.usWriteTimeout = 1000;
    sDCB.usReadTimeout = 1000;
    sDCB.fbCtlHndShake |= (MODE_DTR_CONTROL | MODE_CTS_HANDSHAKE);
    sDCB.fbCtlHndShake &= ~(MODE_DSR_SENSITIVITY | MODE_DSR_HANDSHAKE | MODE_DTR_HANDSHAKE);
    sDCB.fbFlowReplace |= (MODE_RTS_HANDSHAKE);
    sDCB.fbFlowReplace &= ~(MODE_AUTO_TRANSMIT | MODE_AUTO_RECEIVE | MODE_RTS_CONTROL);
    sDCB.fbTimeout = (MODE_NO_WRITE_TIMEOUT | MODE_READ_TIMEOUT | 0x10 | 0x40
                      | 0x80);
    com_setDCB (&sDCB);
}

# endif                         /* NEED_OS2COMMS */

/* $Id: asyn_os2.cpp,v 1.1 2005/01/01 15:18:10 vildanov Exp $ */

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us