home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / dip337d-uri / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-01  |  2.1 KB  |  88 lines

  1. /*
  2.  * dip        A program for handling dialup IP connecions.
  3.  *        This file implements a very rudimentary terminal
  4.  *        emulator, which can be used to log into some host
  5.  *        manually, or to debug the connection.
  6.  *
  7.  * Version:    @(#)term.c    3.3.3    08/16/93
  8.  *
  9.  * Author:      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10.  *        Copyright 1988-1993 MicroWalt Corporation
  11.  *
  12.  *        This program is free software; you can redistribute it
  13.  *        and/or  modify it under  the terms of  the GNU General
  14.  *        Public  License as  published  by  the  Free  Software
  15.  *        Foundation;  either  version 2 of the License, or  (at
  16.  *        your option) any later version.
  17.  */
  18. #include "dip.h"
  19.  
  20.  
  21. void
  22. do_terminal(void)
  23. {
  24.   struct termios otty, ntty;
  25.   char buff[1024];
  26.   void (*old_sigs[32])(int);
  27.   fd_set mask, rmask;
  28.   register int i;
  29.   int tty;
  30.  
  31.   /* First off, ask the SERIAL driver for its fd. */
  32.   tty = tty_askfd();
  33.  
  34.   printf("[ Entering TERMINAL mode.  Use CTRL-] to get back ]\n");
  35.  
  36.   /* Set up the keyboard. */
  37.   if (ioctl(0, TCGETS, &otty)  < 0) {
  38.     perror("term ioctl TCGETS");
  39.     return;
  40.   }
  41.   ntty = otty;
  42.   ntty.c_lflag &= ~(ECHO  | ICANON | ISIG );
  43.   ntty.c_iflag &= ~(ICRNL | IXON   | IXANY);
  44.   ntty.c_oflag &= ~(ONLCR | OPOST);
  45.   if (ioctl(0, TCSETS, &ntty)  < 0) {
  46.     perror("term ioctl TCSETS");
  47.     return;
  48.   }
  49.   
  50.   /* Set up the correct descriptor masks for select(2). */
  51.   FD_ZERO(&mask);
  52.   FD_SET(0, &mask);
  53.   FD_SET(tty, &mask);
  54.  
  55.   /* Disable all signals, saving them for later. */
  56.   for (i = 0; i < 32; i++) old_sigs[i] = signal(i + 1, SIG_IGN);
  57.  
  58.   /* Go into an endless terminal loop. */
  59.   while(1) {
  60.     rmask = mask;
  61.     i = select(32, &rmask, (fd_set *)NULL, (fd_set *)NULL,
  62.                     (struct timeval *)NULL);
  63.     if (i <= 0) break;
  64.     if (FD_ISSET(0, &rmask)) {
  65.         i = read(0, buff, 1);
  66.         if (i > 0) {
  67.             if (buff[0] == (']' & 037)) break;
  68.             (void) write(tty, buff, i);
  69.         }
  70.     }
  71.  
  72.     if (FD_ISSET(tty, &rmask)) {
  73.         i = read(tty, buff, 1024);
  74.         if (i > 0) (void) write(1, buff, i);
  75.     };
  76.   }
  77.  
  78.   if (ioctl(0, TCSETS, &otty)  < 0) {
  79.     perror("term ioctl TCSETS");
  80.     return;
  81.   }
  82.  
  83.   /* Restore all signals. */
  84.   for (i = 0; i < 32; i++) (void) signal(i + 1, old_sigs[i]);
  85.  
  86.   printf("\n[ Back to LOCAL mode. ]\n");
  87. }
  88.