home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR10 / GT1800_3.ZIP / GTBIMOD2.LZH / GTBIMOD.C < prev    next >
C/C++ Source or Header  |  1992-10-12  |  15KB  |  613 lines

  1. /*
  2. **
  3. *
  4.          Copyright (C)  P&M Software Co 1989.  All rights reserved
  5.  
  6.       THIS PROGRAM BELONGS TO P&M SOFTWARE CO.  IT IS CONSIDERED A TRADE
  7.       SECRET AND IS NOT TO BE DIVULGED OR USED BY PARTIES WHO HAVE NOT
  8.       RECEIVED WRITTEN AUTHORIZATION FROM THE OWNER.
  9.  
  10. *
  11. **
  12. */
  13.  
  14. /*
  15. **
  16. *
  17.               Uses TINY memory model.
  18. *
  19. **
  20. */
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <io.h>
  26. #include <errno.h>
  27. #include <sys\stat.h>
  28. #include <dos.h>
  29. #include <conio.h>
  30. #include <bios.h>
  31.  
  32. extern unsigned _heaplen  = 4096;
  33. extern unsigned _stklen   = 6144;
  34. extern int directvideo    = 0;
  35.  
  36. #define SIN_BUF_SIZE   1024
  37. #define SOUT_BUF_SIZE  1024
  38.  
  39. typedef struct bimod_time_lim {
  40.     char bi_hundreds;
  41.     char bi_seconds;
  42.     char bi_minutes;
  43.     char bi_hours;
  44. } BI_TIME_RECORD;
  45.  
  46. void prt_error(char *,char *);
  47. int o_mode(int);
  48. int skipper(char *,int);
  49. int sfopen(char *,int);
  50. int setup_bimodem(void);
  51. void main(int,char *[]);
  52.  
  53. static char bipath[80];
  54. static char bi_cfg[80];
  55. static char bi_mode[32];
  56. static char bi_xmit_pth[80];
  57. static char bi_recv_pth[80];
  58. static char bi_name[80];
  59. static char twoway_bbs[32];
  60. static char gws[4096];
  61. static unsigned int ramt;
  62. static BI_TIME_RECORD bi_time;
  63. static unsigned int pc_com_addr[] = { 0x03f8, 0x02f8, 0x03e8, 0x02e8 };
  64. static unsigned int pc_irq_asg[]  = {      4,      3,      4,      3 };
  65. static int com_port;
  66. static unsigned long baud_rate;
  67. static unsigned char sin_buffer[SIN_BUF_SIZE];
  68. static int sin_store_ptr;
  69. static int sin_read_ptr;
  70. static unsigned char sout_buffer[SOUT_BUF_SIZE];
  71. static int sout_store_ptr;
  72. static int sout_read_ptr;
  73. static unsigned char turn_irq_on;
  74. static unsigned char turn_irq_off;
  75. static int xmit_busy;
  76. static unsigned long irq_vector_addr;
  77. static unsigned long hold_vector;
  78. static int carrier_status = 1;
  79. static unsigned int base_com_addr;
  80. static unsigned int int_enable_reg;
  81. static unsigned int int_enable;
  82. static unsigned int int_ident_reg;
  83. static unsigned int line_control_reg;
  84. static unsigned int modem_control_reg;
  85. static unsigned int modem_control;
  86. static unsigned int line_status_reg;
  87. static unsigned int modem_status_reg;
  88.  
  89. char read_sin_buffer(void)
  90. {
  91.     register char function;
  92.  
  93.     function = sin_buffer[sin_read_ptr];
  94.     if (++sin_read_ptr == SIN_BUF_SIZE)
  95.     sin_read_ptr = 0;
  96.     return(function);
  97. }
  98.  
  99. char *incom(void)
  100. {
  101.     static char incom_return[2];
  102.  
  103.     if (sin_store_ptr == sin_read_ptr)
  104.     return(NULL);
  105.     incom_return[0] = read_sin_buffer();
  106.     incom_return[1] = 0;
  107.     return(incom_return);
  108. }
  109.  
  110. int isDCD(void)
  111. {
  112.     return((inportb(modem_status_reg) & 0x0080));
  113. }
  114.  
  115. int iscarrier(void)
  116. {
  117.     register int res;
  118.  
  119.     res = 1;
  120.     if (! isDCD()) {
  121.         if (carrier_status)
  122.             delay(10);
  123.         if (! isDCD())
  124.             res = 0;
  125.     }
  126.     return((carrier_status = res));
  127. }
  128.  
  129. void store_sout_buffer(char ch)
  130. {
  131.     sout_buffer[sout_store_ptr++] = ch;
  132.     if (sout_store_ptr == SOUT_BUF_SIZE)
  133.     sout_store_ptr = 0;
  134.     if (xmit_busy)
  135.     return;
  136.     disable();
  137.     if (xmit_busy)
  138.     goto L1soutx;
  139.     xmit_busy = 1;
  140.     outportb(base_com_addr,(int) sout_buffer[sout_read_ptr]);
  141.     if (++sout_read_ptr == SOUT_BUF_SIZE)
  142.     sout_read_ptr = 0;
  143. L1soutx:
  144.     enable();
  145. }
  146.  
  147. void send_str(char *s)
  148. {
  149.     register int i, j;
  150.  
  151.     i = 0;
  152.     while (j = s[i++]) {
  153.         store_sout_buffer(j);
  154.     putch(j);
  155.     iscarrier();
  156.     if (! carrier_status)
  157.         return;
  158.         delay(5);
  159.     }
  160. }
  161.  
  162. void set_int_vector(unsigned long vaddr,unsigned long value)
  163. {
  164.     disable();
  165.     *((unsigned long far *) vaddr) = value;
  166.     enable();
  167. }
  168.  
  169. unsigned long get_int_vector(unsigned long vaddr)
  170. {
  171.     unsigned long result;
  172.  
  173.     disable();
  174.     result = *((unsigned long far *) vaddr);
  175.     enable();
  176.     return(result);
  177. }
  178.  
  179. void reset_modem_vectors(void)
  180. {
  181.     disable();
  182.     int_enable = inportb(int_enable_reg);
  183.     outportb(int_enable_reg,(int_enable & 0xf0));
  184.     outportb(0x21,(inportb(0x21) | turn_irq_off));
  185.     enable();
  186.     set_int_vector(irq_vector_addr,hold_vector);
  187. }
  188.  
  189. void interrupt far async_intr_handler()
  190. {
  191. L1loop1:
  192.     switch (inportb(int_ident_reg)) {
  193.         case 1:
  194.             goto L1iret;
  195.         case 2:
  196. L1loop2:
  197.             if (sout_store_ptr == sout_read_ptr)
  198.                 xmit_busy = 0;
  199.             else {
  200.                 outportb(base_com_addr,(int) sout_buffer[sout_read_ptr]);
  201.                 sout_read_ptr++;
  202.                 if (sout_read_ptr == SOUT_BUF_SIZE)
  203.             sout_read_ptr = 0;
  204.                 xmit_busy = 1;
  205.             }
  206.             goto L1loop1;
  207.     case 12:
  208.         case  4:
  209.             sin_buffer[sin_store_ptr] = (char) inportb(base_com_addr);
  210.             sin_store_ptr++;
  211.             if (sin_store_ptr == SIN_BUF_SIZE)
  212.         sin_store_ptr = 0;
  213.             goto L1loop1;
  214.         case 0:
  215.             inportb(modem_status_reg);
  216.             goto L1rsx;
  217.         case 6:
  218.             inportb(line_status_reg);
  219.             break;
  220.         default:
  221.             inportb(modem_status_reg);
  222.             inportb(line_status_reg);
  223.             break;
  224.     }
  225.     goto L1loop1;
  226. L1rsx:
  227.     if (xmit_busy)
  228.         goto L1loop1;
  229.     goto L1loop2;
  230. L1iret:
  231.     outportb(0x20,0x20);
  232. }
  233.  
  234. struct baud_record {
  235.     unsigned long bdt_baud;
  236.     int bdt_divs;
  237. };
  238. struct baud_record baud_table[] = {
  239.     {    300L,384 },
  240.     {    600L,192 },
  241.     {   1200L, 96 },
  242.     {   2400L, 48 },
  243.     {   4800L, 24 },
  244.     {   7200L, 16 },
  245.     {   9600L, 12 },
  246.     {  12000L, 10 },
  247.     {  14400L,  8 },
  248.     {  16800L,  7 },
  249.     {  19200L,  6 },
  250.     {  38400L,  3 },
  251.     {  57600L,  2 },
  252.     { 115200L,  1 },
  253.     {      0L,  0 }
  254. };
  255.  
  256. void setserial(unsigned long baud)
  257. {
  258.     int j;
  259.     int parameter;
  260.     int baudrate;
  261.     int stopbits, databits;
  262.     union REGS regs;
  263.  
  264.     baudrate = 48;
  265.     j = 0;
  266.     while (baud_table[j].bdt_baud > 0L) {
  267.     if (baud == baud_table[j].bdt_baud) {
  268.         baudrate = baud_table[j].bdt_divs;
  269.         break;
  270.     }
  271.     j++;
  272.     }
  273.     stopbits = 0;                               // Default to 1 stop bit
  274.     databits = 3;                               // Default to 8 data bits
  275.     parameter = (stopbits << 2) + databits;
  276.     int_enable = inportb(int_enable_reg);
  277.     outportb(int_enable_reg,(int_enable & 0xf0));
  278. //
  279.     outportb(line_control_reg,0x80);
  280.     outportb(int_enable_reg,((baudrate >> 8) & 0x00ff));
  281.     outportb(base_com_addr,(baudrate & 0x00ff));
  282.     outportb(line_control_reg,parameter);
  283.     inportb(modem_status_reg);
  284.     inportb(int_ident_reg);
  285.     inportb(base_com_addr);
  286.     inportb(line_status_reg);
  287. //
  288.     outportb(int_ident_reg,0x0);
  289.     modem_control = inportb(modem_control_reg) & 0xef;
  290.     outportb(modem_control_reg,(modem_control | 0x0b));
  291.     disable();
  292.     outportb(0x21,(inportb(0x21) & turn_irq_on));
  293.     enable();
  294.     int_enable = inportb(int_enable_reg);
  295.     outportb(int_enable_reg,(int_enable | 0x0f));
  296.     delay(10);
  297. }
  298.  
  299. void initialize_com_port(void)
  300. {
  301. //    typedef int com_tab[4];
  302.     register int cp, irq;
  303.  
  304.     cp = com_port - 1;
  305. //    machine_id = (char far *) 0xF000FFFEL;
  306. //    if (*machine_id == pcjr)
  307. //        base_com_addr = (*((com_tab *) 0x00000400))[cp];
  308. //    else
  309.     base_com_addr = pc_com_addr[cp];
  310.     cp = 0;
  311.     while (base_com_addr != pc_com_addr[cp])
  312.     cp++;
  313.     irq = pc_irq_asg[cp];
  314.     turn_irq_off = (0x01 << irq);
  315.     turn_irq_on  = (turn_irq_off ^ 0xff);
  316.     irq_vector_addr = (unsigned long) (0x20 + (4 * irq));
  317.     sin_store_ptr =
  318.      sin_read_ptr =
  319.       sout_store_ptr =
  320.        sout_read_ptr = 0;
  321.     int_enable_reg = base_com_addr + 1;
  322.     int_ident_reg = int_enable_reg + 1;
  323.     line_control_reg = int_ident_reg + 1;
  324.     modem_control_reg = line_control_reg + 1;
  325.     line_status_reg = modem_control_reg + 1;
  326.     modem_status_reg = line_status_reg + 1;
  327.  
  328.     hold_vector = get_int_vector(irq_vector_addr);
  329.     set_int_vector(irq_vector_addr,(unsigned long) async_intr_handler);
  330.     setserial(baud_rate);
  331. }
  332.  
  333. void prt_error(fs,ns)
  334. char *fs;
  335. char *ns;
  336. {
  337.     perror("\nGTBIMOD");
  338.     printf(fs,ns);
  339.     sleep(2);
  340. }
  341.  
  342. int o_mode(omd)
  343. int omd;
  344. {
  345.     register int md;
  346.  
  347.     md = omd;
  348.     if (_osmajor > 2)
  349.     md = (md | O_DENYNONE);
  350.     return(md);
  351. }
  352.  
  353. int skipper(jstr,jo)
  354. char *jstr;
  355. int jo;
  356. {
  357.    while ((jo < ramt) && (jstr[jo]) && (jstr[jo] <= ' '))
  358.        jo++;
  359.    return(jo);
  360. }
  361.  
  362. int sfopen(fname,omode)
  363. char *fname;
  364. int omode;
  365. {
  366.     register int handle, loop;
  367.     char buf[80];
  368.  
  369.     loop = handle = (-1);
  370.     while (handle < 0) {
  371.     if ((handle = _open(fname,o_mode(omode))) < 0) {
  372.             if ((++loop >= 10) || (errno != EACCES)) {
  373.             sprintf(buf,"%s%s","Unable to open: ",fname);
  374.         prt_error("SFOPEN: %s",buf);
  375.             return(-1);
  376.         }
  377.             sleep(1);
  378.     }
  379.     }
  380.     return(handle);
  381. }
  382.  
  383. void extract(char *ds,int *j)
  384. {
  385.     register int k, jp;
  386.  
  387.     jp = *j;
  388.     k  = 0;
  389.     while ((jp < ramt) && (gws[jp] > ' '))
  390.         ds[k++] = gws[jp++];
  391.     *j = skipper(gws,jp);
  392. }
  393.  
  394. int setup_bimodem()
  395. {
  396.     int jp, cfg, twoway, tim, result, lpth;
  397.     char logpath[96], ws[128];
  398.  
  399.     result = 0;
  400.     sprintf(bi_cfg,"%sBIMODEM.CFG",bipath);
  401.     strupr(bi_cfg);
  402.     if ((cfg = sfopen(bi_cfg,O_RDWR)) < 0)
  403.         return(1);
  404.     strcpy(twoway_bbs,"TWOWAY.BBS");
  405.     if ((twoway = sfopen(twoway_bbs,O_RDWR)) < 0) {
  406.     _close(cfg);
  407.         return(1);
  408.     }
  409.     memset((void *) gws,0,sizeof(gws));
  410.     lseek(twoway,0L,SEEK_SET);
  411.     ramt = _read(twoway,(void *) gws,(sizeof(gws)-16));
  412.     memset((void *) logpath,0x20,sizeof(logpath));
  413.     lseek(twoway,0L,SEEK_SET);
  414.     lpth = sprintf(logpath,"L %sBIMODEM.LOG\r\n",bipath);
  415.     _write(twoway,(void *) logpath,lpth);
  416.     _close(twoway);
  417.     memset((void *) bi_mode,0,sizeof(bi_mode));
  418.     memset((void *) bi_xmit_pth,0x20,sizeof(bi_xmit_pth));
  419.     memset((void *) bi_recv_pth,0x20,sizeof(bi_recv_pth));
  420.     jp = 0;
  421.     extract(bi_mode,    &jp);
  422.     extract(bi_xmit_pth,&jp);
  423.     extract(bi_recv_pth,&jp);
  424.     bi_mode[4] = 0;
  425.     tim = 120;
  426.     if (! strcmp(bi_mode,"TERM")) {
  427.         result = 2;
  428.     tim = 720;
  429.     }
  430.     if (! strcmp(bi_mode,"MAIL"))
  431.     result = 4;
  432.     if (! strcmp(bi_mode,"HOST")) {
  433.         result = 3;
  434.         tim = atoi(&bi_mode[5]);
  435.     }
  436.     printf("\nMODE = %s",bi_mode);
  437.     memmove((void *) ws,(void *) bi_xmit_pth,50);
  438.     ws[50] = 0;
  439.     printf("\nXMIT = %s",ws);
  440.     memmove((void *) ws,(void *) bi_recv_pth,50);
  441.     ws[50] = 0;
  442.     printf("\nRECV = %s\n",ws);
  443.     printf("\nTIME = %d",tim);
  444.     bi_time.bi_hundreds = 0;
  445.     bi_time.bi_seconds  = 0;
  446.     bi_time.bi_minutes  = (char) (tim % 60);
  447.     bi_time.bi_hours    = (char) (tim / 60);
  448.     lseek(cfg,0L,SEEK_SET);
  449.     _write(cfg,(void *) &bi_time,4);
  450.     lseek(cfg,37L,SEEK_SET);
  451.     _read(cfg,(void *) ws,16);
  452.     ws[0] |= 0x60;
  453.     lseek(cfg,37L,SEEK_SET);
  454.     _write(cfg,(void *) ws,16);
  455.     lseek(cfg,41L,SEEK_SET);
  456.     _write(cfg,(void *) bi_xmit_pth,80);
  457.     _write(cfg,(void *) bi_recv_pth,80);
  458.     logpath[lpth--] = 0x20;
  459.     logpath[lpth--] = 0x20;
  460.     logpath[lpth]   = 0x20;
  461.     _write(cfg,(void *) &logpath[2],80);
  462.     _close(cfg);
  463.     sprintf(ws,"DEL %sBIMODEM.LOG",bipath);
  464.     system(ws);
  465.  
  466.     sprintf(bi_cfg,"%sBIMODEM.PTH",bipath);
  467.     strupr(bi_cfg);
  468.     if (result == 3) {
  469.         sprintf(ws,"DEL %s",bi_cfg);
  470.         system(ws);
  471.     }
  472.     if (result == 4) {
  473.     memset((void *) bi_recv_pth,0x20,sizeof(bi_recv_pth));
  474.         if ((cfg = _creat(bi_cfg,0)) < 0)
  475.             return(1);
  476.         while (jp < ramt) {
  477.             memset((void *) bi_name,0x20,sizeof(bi_name));
  478.         extract(bi_name,&jp);
  479.         _write(cfg,(void *) "U       ",8);
  480.         _write(cfg,(void *) bi_name,80);
  481.         _write(cfg,(void *) bi_recv_pth,80);
  482.         _write(cfg,(void *) bi_recv_pth,80);
  483.     }
  484.     _close(cfg);
  485.     }
  486.     return(result);
  487. }
  488.  
  489. int keytest(void)
  490. {
  491.     return(bioskey(1));
  492. }
  493.  
  494. int keyread(void)
  495. {
  496.     return((bioskey(0) & 0x00FF));
  497. }
  498.  
  499. void main(argc,argv)
  500. int argc;
  501. char *argv[];
  502. {
  503.     register int j, loop;
  504.     unsigned int lcnt, ticks;
  505.     int rescode, pcnt, pa;
  506.     char *p;
  507.     char prior;
  508.  
  509.     directvideo = 0;
  510.     rescode = 1;
  511.     delay(0);
  512.     if (argc < 4) {
  513.         printf(  "\nGTBIMOD usage: gtbimod port baud bipath\\");
  514.         printf("\n\n    Where: 'port' ...... x:yyyy:z");
  515.     printf(  "\n                                 x = COM port #");
  516.     printf(  "\n                              yyyy = COM port addr");
  517.     printf(  "\n                                 z = IRQ #");
  518.     printf(  "\n           'baud' ...... is the current baud rate");
  519.     printf(  "\n           'bipath\\' ... points to a directory containing these files:");
  520.     printf("\n\n                 1.  BIMODEM.CFG");
  521.     printf(  "\n                 2.  BIMODEM.LOG");
  522.     printf(  "\n                 3.  BIMODEM.PTH");
  523.     printf(  "\n");
  524.         exit(1);
  525.     }
  526.     printf("PORT = %s\nBAUD = %s\nPATH = %s\n",argv[1],argv[2],argv[3]);
  527.     com_port = argv[1][0] - '0';
  528.     j = com_port - 1;
  529.     if (argv[1][1] == ':') {
  530.         sscanf(&argv[1][2],"%x",&pa);
  531.         pc_com_addr[j] = pa;
  532.     if (argv[1][6] == ':')
  533.             pc_irq_asg[j] = argv[1][7] - '0';
  534.     }
  535.     baud_rate = atol(argv[2]);
  536.     initialize_com_port();
  537.     strcpy(bipath,argv[3]);
  538.     j = strlen(bipath);
  539.     if (bipath[(j - 1)] != '\\') {
  540.         bipath[j++] = '\\';
  541.         bipath[j] = 0;
  542.     }
  543.     strupr(bipath);
  544.     iscarrier();
  545.     if (carrier_status) {
  546.         if ((rescode = setup_bimodem()) == 3) {
  547.         loop = iscarrier();
  548.         if (! loop)
  549.         rescode = 1;
  550.         send_str("\r\n\nThe HOST is ready.   Press ENTER when you are ready: ");
  551.         pcnt =
  552.          prior =
  553.           lcnt =
  554.            ticks = 0;
  555.             while (loop) {
  556.         if (++lcnt > 100) {
  557.             lcnt = 0;
  558.             if (++ticks > 55000U) {
  559.             send_str("\r\n\nTimeout...\r\n\n");
  560.             loop = 0;
  561.             rescode = 1;
  562.             }
  563.             delay(10);
  564.         }
  565.                 p = incom();
  566.         if (p != NULL) {
  567.             store_sout_buffer(p[0]);
  568.             putch(p[0]);
  569.             if (p[0] == 13) {
  570.             send_str("\n\n");
  571.             loop = 0;
  572.             }
  573.             else {
  574.             if (((p[0] == ' ') && (prior == 8)) || ((p[0] == 8) && (prior == ' '))) {
  575.                     if (pcnt++ > 1)
  576.                 loop = 0;
  577.             }
  578.             }
  579.             prior = p[0];
  580.         }
  581.         else {
  582.             if (keytest()) {
  583.             if ((keyread()) == 13) {
  584.                 send_str("\n\n");
  585.                 loop = 0;
  586.             }
  587.             }
  588.         }
  589.         iscarrier();
  590.         if (! carrier_status) {
  591.             loop = 0;
  592.             rescode = 1;
  593.         }
  594.         }
  595.     }
  596.     }
  597.     reset_modem_vectors();
  598.     exit(rescode);
  599. }
  600.  
  601. /*
  602. **
  603. *
  604.          Copyright (C)  P&M Software Co 1989.  All rights reserved
  605.  
  606.       THIS PROGRAM BELONGS TO P&M SOFTWARE CO.  IT IS CONSIDERED A TRADE
  607.       SECRET AND IS NOT TO BE DIVULGED OR USED BY PARTIES WHO HAVE NOT
  608.       RECEIVED WRITTEN AUTHORIZATION FROM THE OWNER.
  609.  
  610. *
  611. **
  612. */
  613.