home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume4 / bridge / part01 / master_io.c < prev    next >
C/C++ Source or Header  |  1988-05-31  |  4KB  |  173 lines

  1. #ifndef lint
  2. static    char sccsid[] = "@(#)io.c 1.1 86/02/05 SMI"; /* from UCB 1.3 83/07/06 */
  3. #endif
  4.  
  5. /* this file contains the I/O handling and the exchange of 
  6.    edit characters. This connection itself is established in
  7.    ctl.c
  8.  */
  9.  
  10. #include "talk.h"
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <sys/time.h>
  14.  
  15. #define A_LONG_TIME 10000000
  16. #define STDIN_MASK (1<<fileno(stdin))    /* the bit mask for standard
  17.                        input */
  18. extern int errno;
  19. extern int INPORT, OUTPORT, OUTEND;
  20.  
  21. /*
  22.  * The routine to do the actual talking
  23.  */
  24.  
  25. talk()
  26. {
  27.     register int read_template, sockt_mask, inportmask;
  28.     BMSG    buf;
  29.     char string[10],ch;
  30.     int read_set, nb,flag,nbuf=sizeof buf;
  31.     struct timeval wait;
  32.  
  33.     current_line = 0;
  34.  
  35.     sockt_mask = (1<<sockt);
  36.     inportmask = (1<<INPORT);
  37.  
  38.     /*
  39.      * wait on both the other process (sockt_mask) and 
  40.      * standard input ( STDIN_MASK )
  41.      */
  42.  
  43.     read_template = sockt_mask | inportmask;
  44.  
  45.     forever {
  46.  
  47.     read_set = read_template;
  48.  
  49.     wait.tv_sec = 100000;
  50.     wait.tv_usec = 0;
  51.  
  52.     select(32, &read_set, 0, 0, &wait);
  53.  
  54.     if ( read_set & sockt_mask ) { 
  55.  
  56.         /* There is data on sockt */
  57.         nb=read(sockt,&buf,nbuf);
  58.         if(nb==nbuf)write(OUTPORT, &buf, nbuf);
  59.     }
  60.     
  61.     if ( read_set & inportmask ) {
  62.  
  63.         /* we can't make the tty non_blocking, because
  64.            curses's output routines would screw up */
  65.         nb=read(INPORT,&buf,nbuf);
  66.         if(nb==nbuf)write(sockt, &buf, nbuf);
  67.     }
  68.     }
  69. }
  70.  
  71. extern int    errno;
  72. extern int    sys_nerr;
  73. extern char    *sys_errlist[];
  74.  
  75.     /* p_error prints the system error message on the standard location
  76.        on the screen and then exits. (i.e. a curses version of perror)
  77.      */
  78.  
  79. p_error(string) 
  80. char *string;
  81. {
  82.     char *sys;
  83.  
  84.     sys = "Unknown error";
  85.     if(errno < sys_nerr) {
  86.     sys = sys_errlist[errno];
  87.     }
  88.  
  89.  
  90.     printf("[%s : %s (%d)]\n", string, sys, errno);
  91.     quit();
  92. }
  93.  
  94.     /* display string in the standard location */
  95.  
  96. message(string)
  97. char *string;
  98. {
  99. }
  100. talk1()
  101. {    char    ch,ins[2][40];
  102.     static char *im[2]={"Conversation","Input       "};
  103.     int    rtn, i, ityp,iwn,wn, row, col, readmask,read_template;
  104.     int    inport_mask,read_set;
  105.     BMSG    buf;
  106.     int    ki[2],nb,nbuf=sizeof buf;
  107.     struct    timeval wait;
  108.  
  109.     ityp=1;iwn=12;ki[0]=ki[1]=0;
  110.     wmove(comwin[18].x_win,0,0);
  111.     wprintw(comwin[18].x_win,"%s",im[ityp]);
  112.     wrefresh(comwin[18].x_win);
  113.     inport_mask=(1<<INPORT);
  114.     read_template=inport_mask | STDIN_MASK;
  115.     forever {
  116.         read_set=read_template;
  117.         wait.tv_sec=100000;
  118.         wait.tv_usec=0;
  119.         select(32,&read_set,0,0,&wait);
  120.         if(read_set&inport_mask){
  121.             nb=read(INPORT,&buf,nbuf);
  122.             if(nb==nbuf){
  123.             wn=WN;row=ROW;col=COL;
  124.             if(row<80) wmove(comwin[wn].x_win,row,col);
  125.                         if(wn<14&&STR[0]=='\\'){
  126.                              TYPE=64;write(OUTPORT,&buf,nbuf);
  127.                              endwin();exit();}
  128.             if(STR[0]=='Z'|wn==12) wclear(comwin[wn].x_win);
  129.                         if(STR[0]!='Z') wprintw(comwin[wn].x_win,"%s",STR);
  130.             wrefresh(comwin[wn].x_win);
  131.             }
  132.         }
  133.         if(read_set&STDIN_MASK){
  134.             read(0,&ch,1);
  135.                         if(ityp==1&&ch=='\\') {
  136.                              TYPE=64;write(OUTPORT,&buf,nbuf);
  137.                              write(OUTEND,&ch,1);endwin();exit();}
  138.             if(ityp==1&&ch<='z'&&ch>='a') ch&=223;
  139.             switch(ch){
  140.                  case 27:   ityp=1-ityp;iwn=28-iwn;
  141.                     wmove(comwin[18].x_win,0,0);
  142.                     wprintw(comwin[18].x_win,"%s",im[ityp]);
  143.                     wrefresh(comwin[18].x_win);
  144.                     break;
  145.                  case '\b': if(ki[ityp]>0){
  146.                         ki[ityp]--;
  147.                         waddch(comwin[iwn].x_win,ch);
  148.                         wrefresh(comwin[iwn].x_win);
  149.                     }
  150.                     break;
  151.                  default:   ins[ityp][ki[ityp]++]=ch;
  152.                     waddch(comwin[iwn].x_win,ch);
  153.                     wrefresh(comwin[iwn].x_win);
  154.                     
  155.             }
  156.             if(ki[ityp]>=comwin[iwn].x_ncols-1&&ch!='\n'){
  157.                 ch='\n';ins[ityp][ki[ityp]++]=ch;
  158.                 waddch(comwin[iwn].x_win,ch);
  159.                 wrefresh(comwin[iwn].x_win);
  160.             }
  161.             if(ch=='\n'){
  162.                 if(ityp){wclear(comwin[iwn].x_win);
  163.                      wrefresh(comwin[iwn].x_win);}
  164.                 TYPE=ityp;ins[ityp][ki[ityp]]='\0';
  165.                 ROW=81;ki[ityp]=0;
  166.                 strcpy(STR,ins[ityp]);
  167.                 write(OUTPORT,&buf,nbuf);
  168.             }
  169.         }
  170.     }
  171. }
  172.  
  173.