home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume4
/
bridge
/
part01
/
master_io.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-05-31
|
4KB
|
173 lines
#ifndef lint
static char sccsid[] = "@(#)io.c 1.1 86/02/05 SMI"; /* from UCB 1.3 83/07/06 */
#endif
/* this file contains the I/O handling and the exchange of
edit characters. This connection itself is established in
ctl.c
*/
#include "talk.h"
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#define A_LONG_TIME 10000000
#define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard
input */
extern int errno;
extern int INPORT, OUTPORT, OUTEND;
/*
* The routine to do the actual talking
*/
talk()
{
register int read_template, sockt_mask, inportmask;
BMSG buf;
char string[10],ch;
int read_set, nb,flag,nbuf=sizeof buf;
struct timeval wait;
current_line = 0;
sockt_mask = (1<<sockt);
inportmask = (1<<INPORT);
/*
* wait on both the other process (sockt_mask) and
* standard input ( STDIN_MASK )
*/
read_template = sockt_mask | inportmask;
forever {
read_set = read_template;
wait.tv_sec = 100000;
wait.tv_usec = 0;
select(32, &read_set, 0, 0, &wait);
if ( read_set & sockt_mask ) {
/* There is data on sockt */
nb=read(sockt,&buf,nbuf);
if(nb==nbuf)write(OUTPORT, &buf, nbuf);
}
if ( read_set & inportmask ) {
/* we can't make the tty non_blocking, because
curses's output routines would screw up */
nb=read(INPORT,&buf,nbuf);
if(nb==nbuf)write(sockt, &buf, nbuf);
}
}
}
extern int errno;
extern int sys_nerr;
extern char *sys_errlist[];
/* p_error prints the system error message on the standard location
on the screen and then exits. (i.e. a curses version of perror)
*/
p_error(string)
char *string;
{
char *sys;
sys = "Unknown error";
if(errno < sys_nerr) {
sys = sys_errlist[errno];
}
printf("[%s : %s (%d)]\n", string, sys, errno);
quit();
}
/* display string in the standard location */
message(string)
char *string;
{
}
talk1()
{ char ch,ins[2][40];
static char *im[2]={"Conversation","Input "};
int rtn, i, ityp,iwn,wn, row, col, readmask,read_template;
int inport_mask,read_set;
BMSG buf;
int ki[2],nb,nbuf=sizeof buf;
struct timeval wait;
ityp=1;iwn=12;ki[0]=ki[1]=0;
wmove(comwin[18].x_win,0,0);
wprintw(comwin[18].x_win,"%s",im[ityp]);
wrefresh(comwin[18].x_win);
inport_mask=(1<<INPORT);
read_template=inport_mask | STDIN_MASK;
forever {
read_set=read_template;
wait.tv_sec=100000;
wait.tv_usec=0;
select(32,&read_set,0,0,&wait);
if(read_set&inport_mask){
nb=read(INPORT,&buf,nbuf);
if(nb==nbuf){
wn=WN;row=ROW;col=COL;
if(row<80) wmove(comwin[wn].x_win,row,col);
if(wn<14&&STR[0]=='\\'){
TYPE=64;write(OUTPORT,&buf,nbuf);
endwin();exit();}
if(STR[0]=='Z'|wn==12) wclear(comwin[wn].x_win);
if(STR[0]!='Z') wprintw(comwin[wn].x_win,"%s",STR);
wrefresh(comwin[wn].x_win);
}
}
if(read_set&STDIN_MASK){
read(0,&ch,1);
if(ityp==1&&ch=='\\') {
TYPE=64;write(OUTPORT,&buf,nbuf);
write(OUTEND,&ch,1);endwin();exit();}
if(ityp==1&&ch<='z'&&ch>='a') ch&=223;
switch(ch){
case 27: ityp=1-ityp;iwn=28-iwn;
wmove(comwin[18].x_win,0,0);
wprintw(comwin[18].x_win,"%s",im[ityp]);
wrefresh(comwin[18].x_win);
break;
case '\b': if(ki[ityp]>0){
ki[ityp]--;
waddch(comwin[iwn].x_win,ch);
wrefresh(comwin[iwn].x_win);
}
break;
default: ins[ityp][ki[ityp]++]=ch;
waddch(comwin[iwn].x_win,ch);
wrefresh(comwin[iwn].x_win);
}
if(ki[ityp]>=comwin[iwn].x_ncols-1&&ch!='\n'){
ch='\n';ins[ityp][ki[ityp]++]=ch;
waddch(comwin[iwn].x_win,ch);
wrefresh(comwin[iwn].x_win);
}
if(ch=='\n'){
if(ityp){wclear(comwin[iwn].x_win);
wrefresh(comwin[iwn].x_win);}
TYPE=ityp;ins[ityp][ki[ityp]]='\0';
ROW=81;ki[ityp]=0;
strcpy(STR,ins[ityp]);
write(OUTPORT,&buf,nbuf);
}
}
}
}