home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MTERM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  137 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* MTERM.C - Minimal example PC terminal program.
  4.    Released to public domain by author, David Harmon, June 1992.
  5.    Intended for use with a FOSSIL driver, but will run with BIOS alone.
  6.    I expect you'll want to add something for practical purposes. ;-)
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>            /* kbhit(), getch(), putch(), etc. */
  12. #include <dos.h>              /* int86(), etc. */
  13.  
  14. #ifdef __ZTC__
  15.  #define cputs(s) fputs((s),stderr)
  16. #endif
  17.  
  18. int port = 0;                 /* 0 = COM1:, 1 = COM2: etc. */
  19. int local_echo = 0;
  20. int cr_add_lf = 0;
  21. int exiting = 0;
  22.  
  23. int init_comm(int flags)
  24. {
  25.       union REGS regs;
  26.  
  27.       regs.h.ah = 0x04;         /* initialize driver (port) */
  28.       regs.x.bx = 0x4f50;
  29.       regs.x.dx = port;
  30.       int86( 0x14, ®s, ®s);
  31.  
  32.       regs.h.ah = 0x00;         /* set baud rate * port attrs */
  33.       regs.h.al = (unsigned char)flags;
  34.       regs.x.dx = port;
  35.       int86( 0x14, ®s, ®s);
  36.       return regs.h.ah;
  37. }
  38.  
  39. void send_char(char ch)
  40. {
  41.       union REGS regs;
  42.  
  43.       regs.h.ah = 0x01;         /* Send char (wait until ready)*/
  44.       regs.h.al = ch;
  45.       regs.x.dx = port;
  46.       int86( 0x14, ®s, ®s);
  47. }
  48.  
  49. int   input_ready(void)
  50. {
  51.       union REGS regs;
  52.  
  53.       regs.h.ah = 0x03;         /* Get port status */
  54.       regs.x.dx = port;
  55.       int86( 0x14, ®s, ®s);
  56.       return ((regs.h.ah & 0x01) != 0);   /* input ready */
  57. }
  58.  
  59. int   get_char(void)
  60. {
  61.       union REGS regs;
  62.  
  63.       regs.h.ah = 0x02;         /* receive char (wait if necessary)*/
  64.       regs.x.dx = port;
  65.       int86( 0x14, ®s, ®s);
  66.       return regs.h.al;
  67. }
  68.  
  69. void deinit_comm(void)
  70. {
  71.       union REGS regs;
  72.  
  73.       regs.h.ah = 0x05;         /* deinitialize port (pseudo close) */
  74.       regs.h.al = 0x00;         /* (lower DTR) */
  75.       regs.x.dx = port;
  76.       int86( 0x14, ®s, ®s);
  77. }
  78.  
  79. main()
  80. {
  81.       int ch;
  82.  
  83.       init_comm(0xE3);          /* hard coded 0xB3 = 2400,N,8,1 */
  84.       cputs("MTERM ready!   Press F1 to exit.\r\n");
  85.       while (!exiting)
  86.       {
  87.             if (kbhit())        /* key was hit */
  88.             {
  89.                   ch = getch();     /* Regular ASCII keys are returned as the
  90.                                        ASCII code; function keys, arrows, etc.
  91.                                        as zero followed by a special code
  92.                                        (on next getch.)  */
  93.                   if (ch != 0)
  94.                   {
  95.                         send_char((char)ch);    /* to com port */
  96.                         if (local_echo)
  97.                         {
  98.                               putch(ch);        /* to screen */
  99.  
  100.                               /* add LF to CR? */
  101.  
  102.                               if (cr_add_lf && ch == '\r')
  103.                                     putch('\n');
  104.                         }
  105.                   }
  106.                   else
  107.                   {
  108.                         ch = getch();       /* get the special key code */
  109.                         switch (ch)
  110.                         {
  111.                         case 0x3B: /* F1 */
  112.                               exiting = 1;               /* quit now */
  113.                               break;
  114.  
  115.                         case 0x3C: /* F2 */
  116.                               local_echo = !local_echo;  /* toggle echo */
  117.                               break;
  118.  
  119.                         case 0x3D: /* F3 */
  120.                               cr_add_lf = !cr_add_lf;    /* toggle LF */
  121.                               break;
  122.                         }
  123.                   }
  124.             } /* end if kbhit */
  125.  
  126.             if (input_ready())       /* com port */
  127.             {
  128.                   ch = get_char();
  129.                   putch(ch);
  130.                   if (cr_add_lf && ch == '\r')  /* add LF to CR? */
  131.                         putch('\n');
  132.             }
  133.       } /* end while not exiting */
  134.       deinit_comm();
  135.       return 0;
  136. }    
  137.