home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / sep91.zip / 9N09076A < prev    next >
Text File  |  1991-07-10  |  12KB  |  352 lines

  1. /******************************************************
  2.  * Listing 3  hostcom.c
  3.  *
  4.  * HOSTCOM  PC - Host Communication Program
  5.  *
  6.  * written by Douglas Connolly
  7.  * 419 Hart Senate Office Building
  8.  * Washington, D.C. 20510
  9.  *
  10.  *****************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <stdlib.h>
  16. #include <conio.h>
  17. #include <ctype.h>
  18. #include "hc.h"
  19.  
  20. /* screen attributes */
  21. int norm_attr = ((BLACK<<4)+LIGHTGRAY); /* text */
  22. int stat_attr = ((BLUE<<4)+WHITE);    /* status line */
  23.  
  24. /* attributes for activity field */
  25. int wait_attr  = ((RED<<4) + WHITE);    /* waiting */
  26. int time_attr  = ((GREEN<<4)+ WHITE);   /* TIME */
  27. int send_attr  = ((CYAN<<4) + WHITE);   /* sending */
  28. int oia_attr   = ((GREEN<<4)+ WHITE);   /* OIA */
  29.  
  30. static char far *vid_mem; /* video memory pointer */
  31.  
  32. /* hllapi_level indicates the emulation software used:
  33.  *  0 = PC/3270 Ver. 1.0    PSID = A
  34.  *  1 = IBM Entry Level     PSID = E
  35.  *  2 = Attachmate Extra    PSID = B
  36.  *  3 = RabbitGATE          PSID = 1
  37.  *  4 = simulated connection
  38.  *  5 = simulated connection with test data
  39.  */
  40.  
  41. int hllapi_level;
  42.  
  43. /* clear_flag prevents 2 consecutive clear commands
  44.  * being sent to host (prevent Attachmate problems)
  45.  */
  46. int clear_flag = 0;
  47.  
  48.  
  49. void main (int argc, char **argv)
  50. {
  51.         int x, y, chr, err;
  52.         char api_ps[1921]; /* pres. space + 1 */
  53.         int k;             /* char read from kbd */
  54.         char c[2] = " ";   /* intput string */
  55.         int n = 0;         /* cursor position */
  56.  
  57.         /* power on/off string for RabbitGATE */
  58.         char rabbit_power[4] = {'@', 'A', 0x80, '\0'};
  59.  
  60.         /* select emulation software */
  61.         if (argc < 2 ||
  62.             (hllapi_level = atoi(argv[1])) > 5) {
  63.                 fprintf(stderr, HOSTCOM_MSG);
  64.                 return;
  65.         }
  66.  
  67.         if (hllapi_level == 4)
  68.                 memset(api_ps, ' ', 1920);
  69.  
  70.         /* copy test data to pres space */
  71.         else if (hllapi_level == 5)
  72.                 for (x=0; x<24*80; x+=80) {
  73.                         chr = 0x28;
  74.                         for (y=0; y<80; y++)
  75.                                 api_ps[x+y] = chr++;
  76.                 }
  77.  
  78.         /* initialize video pointer */
  79.         if (set_vid() != 0) {
  80.                 fprintf(stderr, VID_ERROR);
  81.                 return;
  82.         }
  83.  
  84.         /* connect presentation space */
  85.         if ((err = connect_ps_space()) != 0) {
  86.                 fprintf(stderr,  CONNECT_ERROR);
  87.                 return;
  88.         }
  89.  
  90.         clrscr();
  91.         write_string(1, 25, STAT_LINE, stat_attr);
  92.  
  93.         /* RabbitGATE:  Power on session */
  94.         if (hllapi_level == 3)  {
  95.             write_string(1, 1," Powering on session"
  96.                 " One moment ...", stat_attr);
  97.  
  98.                 send_key(rabbit_power);
  99.                 delay(500);
  100.                 if (!check_power()) {
  101.                   puts("HOSTOCM: power on failed\n");
  102.                   exit(1);
  103.                 }
  104.         }
  105.  
  106.         status_msg("Ready", stat_attr);
  107.  
  108.         while (1) {
  109.                 /* Wait for a key.  While no key is
  110.                  * waiting, continuously update PS.
  111.                  */
  112.  
  113.                 while (!kbhit()) {
  114.                     if ((err=update_ps(api_ps))!=0) {
  115.                         if (process_error(err) != 0) {
  116.                             reset_connection();
  117.                             return;
  118.                         }
  119.                     }
  120.                     display_cursor();
  121.                 }
  122.                 k = get_key();
  123.  
  124.                 switch (k) {
  125.  
  126.                 case K_F3:      /* clear */
  127.                     /* Send clear key only if
  128.                      * clear_flag is set.
  129.                      */
  130.                         if (clear_flag) {
  131.                             clear_flag = 0;
  132.                             err = input_to_host("@C");
  133.                             n = 0;
  134.                             delay(500);
  135.                         }
  136.                         break;
  137.                 case K_F4:              /* reset  */
  138.                         err = input_to_host("@R");
  139.                         n = 0;
  140.                         break;
  141.                 case K_F7:              /* OIA */
  142.                         if ((err = dspy_oia()) == 0) {
  143.                                 write_string(1,1,
  144.                                  OIA_MSG, stat_attr);
  145.                                 while (!receive_esc());
  146.                         }
  147.                         break;
  148.                 case K_CF10: /* unconditional exit */
  149.                                 reset_connection();
  150.                                 clrscr();
  151.                                 return;
  152.                 case K_F10:     /* exit program */
  153.  
  154.                         if (check_signon()) {
  155.                            /* warn if in application */
  156.                            write_string(1, 1,
  157.                              SIGNOFF_MSG, stat_attr);
  158.                            while(!receive_esc());
  159.                         }
  160.                         else {
  161.                            if (hllapi_level == 3)
  162.                                 /* power off */
  163.                                 send_key(rabbit_power);
  164.                            if ((err =
  165.                                    reset_connection())
  166.                                    == 0)  {
  167.                                   clrscr();
  168.                                   return;
  169.                            }
  170.                         }
  171.                         break;
  172.                 case K_HOME:    /* home  */
  173.                         err = keys_to_host("@0");
  174.                         n = 0;
  175.                         break;
  176.                 case K_CTRLR:   /* return (newline) */
  177.                         err = keys_to_host("@N");
  178.                         n = 0;
  179.                         break;
  180.                 case K_ENTER:   /* enter key */
  181.                         /* set clear key flag */
  182.                         clear_flag = 1;
  183.                         err = input_to_host("@E");
  184.                         n = 0;
  185.                         break;
  186.                 case K_LEFT:    /* cursor left */
  187.                   if (n>0) {
  188.                      if ((err=keys_to_host("@L"))==0) {
  189.                         n--;
  190.                         err = display_cursor();
  191.                      }
  192.                   }
  193.                 break;
  194.                 case K_RIGHT:   /* cursor right */
  195.                      if ((err=keys_to_host("@Z"))==0) {
  196.                           n++;
  197.                           err = display_cursor();
  198.                      }
  199.                      break;
  200.                 case K_TAB:             /* tab */
  201.                         err = keys_to_host("@T");
  202.                         n = 0;
  203.                         break;
  204.                 case K_INS:             /* insert */
  205.                         err = keys_to_host("@I");
  206.                         break;
  207.                 case K_BACKTAB:         /* backtab */
  208.                         err = keys_to_host("@B");
  209.                         n = 0;
  210.                         break;
  211.                 case K_DEL:     /* delete right */
  212.                         err = keys_to_host("@D");
  213.                         break;
  214.                 case K_BACKSPACE: /* backspace */
  215.                         if (n>0) {
  216.                             n--;
  217.                             err = keys_to_host("@L");
  218.                             if (err == 0)
  219.                                err=keys_to_host("@D");
  220.                             break;
  221.                         }
  222.                         else {  /* at start of line */
  223.                                 putchar('\a');
  224.                                 continue;
  225.                         }
  226.                 default:
  227.                         if (isascii(k)) {
  228.                                 putchar(k);
  229.                                 *c = (char) k;
  230.                                 err = keys_to_host(c);
  231.                                 n++;
  232.                         }
  233.                         else {
  234.                                 putchar('\a');
  235.                                 continue;
  236.                         }
  237.                 }               /* end of switch */
  238.  
  239.                 if (err > 0)
  240.                         if (process_error(err) == 1) {
  241.                                 reset_connection();
  242.                                 return;
  243.                         }
  244.  
  245.                 /* Make sure comm. line is clear
  246.                  * Since this check can result in an
  247.                  * error, another call to is
  248.                  * necessary to process_error().
  249.                  */
  250.  
  251.                 if ((err = host_wait()) > 0)
  252.                         if (process_error(err) == 1) {
  253.                                 reset_connection();
  254.                                 return;
  255.                         }
  256.  
  257.         }       /* end of while loop */
  258. }
  259.  
  260. /*****************************************************
  261.  *
  262.  * set_vid() Initialize global pointer vid_mem to
  263.  *           color or monochrome video memory.
  264.  *
  265.  * return 0: successful, 25/80 video mode detected
  266.  *        1: incorrect video mode detected
  267.  *
  268.  *****************************************************/
  269.  
  270. int set_vid(void)
  271. {
  272.         union REGS inregs, outregs;
  273.         int err_flag = 0;
  274.         inregs.h.ah  = 15;   /* BIOS vid mode func */
  275.  
  276.         /* BIOS int 0x10 */
  277.         int86(0x10, &inregs, &outregs);
  278.  
  279.         switch (outregs.h.al) {
  280.         case 2:                 /* 25/80 color */
  281.         case 3:                 /* 25/80 color */
  282.                 vid_mem   = (char far *) 0xB8000000L;
  283.                 break;
  284.         case 7:                 /* monochrome */
  285.                 vid_mem   = (char far *) 0xB0000000L;
  286.                 stat_attr = wait_attr = time_attr =
  287.                 send_attr = oia_attr =
  288.                 ((LIGHTGRAY<<4) + BLACK);
  289.                 break;
  290.         default:                /* wrong mode */
  291.                 err_flag = 1;
  292.                 break;
  293.         }
  294.         return (err_flag);
  295. }
  296.  
  297. /*****************************************************
  298.  *
  299.  * write_string() Write string directly to vid memory.
  300.  *
  301.  * Coordinates accepted in 1-25/1-80 range for
  302.  * consistency with Turbo C library functions.
  303.  *
  304.  *  x   = 1-80
  305.  *  y   = 1-25
  306.  * *s   = string to display
  307.  * attr = attribute to use for display -- use constants
  308.  *        in conio.h
  309.  ******************************************************/
  310.  
  311. void write_string(int x, int y, char *s, int attr)
  312. {
  313.         char far *v;
  314.         x--; y--;
  315.         v = vid_mem + (y*160) + (x*2);
  316.         while(*s) {
  317.                 *v++ = *s++;    /* write character */
  318.                 *v++ = attr;    /* write attribute */
  319.         }
  320. }
  321.  
  322. /*****************************************************
  323.  *
  324.  * get_key() Read a single key and return normal ASCII
  325.  *           and extended ASCII characeters
  326.  *
  327.  ******************************************************/
  328.  
  329. int get_key(void)
  330. {
  331.         int ch;
  332.         if ((ch = bdos(0x07, 0, 0) & 0x0ff) != '\0')
  333.                 return(ch);
  334.         return ((bdos(0x07, 0, 0) & 0x0ff) | 0x100);
  335. }
  336.  
  337. /*****************************************************
  338.  *
  339.  * receive_esc() Return 1 if escape depressed and 0
  340.  *               if no key depressed, or key not
  341.  *               escape.
  342.  *
  343.  *****************************************************/
  344.  
  345. int receive_esc (void)
  346. {
  347.         if (kbhit())
  348.                 if (get_key() == K_ESC)
  349.                         return (1);
  350.         return (0);
  351. }
  352.