Example #1
0
File: gnotfix.c Project: kahrs/cda
int
getn(void)
{
	short k, n;

	n = rcvchar();
	k = rcvchar();
	k |= (n<<8);
	return(k);
}
Example #2
0
void* threadParent(void *arg) {
	/* Parent Thread */
	Byte *chck;
	
	/* Ketika belum diterima EOF, 
	   teruskan listening untuk penerimaan byte */
	while (parentExit == 0) {	
		chck = rcvchar(sockfd, &buffer);
		if ( chck != NULL ) {
			if ( *chck != 26 ) {
				receivedByte++;
				printf("Menerima byte ke-%d.\n", receivedByte);
			} else {
				printf("EOF diterima\n");
				parentExit = 1;
			}
		}
	}

	char state[1];
	sprintf(state, "%c",(char) XOFF);
	if (sendto(sockfd, state, 1, 0, (struct sockaddr *)&targetAddr, addrLen) == -1) {
		perror("sendto");
	}
	return NULL;
}
int main(int argc, char *argv[]) {

	if (argc!=2){
		printf("Argumen Salah\n");
		return 0;
	}
	char *port_number = argv[1];
	//Constructing local socket address
	struct addrinfo hints;
	memset(&hints,0,sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype=SOCK_DGRAM;
	hints.ai_protocol=IPPROTO_UDP;
	hints.ai_flags=AI_PASSIVE|AI_ADDRCONFIG;
	struct addrinfo* res;
	int err = getaddrinfo(NULL,port_number,&hints,&res);
	if (err!=0){
		printf("failed to resolve local socket address(err=%d)\n",err );
		return 0;
	}

	Byte C;

	//Creating Socket
	sockfd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
	if(sockfd<0){
		printf("%s\n",strerror(errno));
		return 0;
	}	

	// Insert code here to bind socket to the port number given in argv[1].
	if(bind(sockfd,res->ai_addr,res->ai_addrlen)<0){
		printf("%s\n",strerror(errno) );
		return 0;
	}
	struct sockaddr_in *addr;
	addr = (struct sockaddr_in *)res->ai_addr;
	printf("Binding pada %s:%s \n",inet_ntoa((struct  in_addr)addr->sin_addr),port_number);
	
	freeaddrinfo(res);
	
	/* Initialize XON/XOFF flags */
	send_xon = true;
	/* Create child process */
	pthread_t childt;
	pthread_create(&childt,NULL,childprocess,NULL);

	/*** If Parrent Process ***/
	while (true){
		C = *(rcvchar(sockfd, rxq));
		
		/* Quit on end of file */
		if (C == Endfile) {
			exit(0);
		}
	}

}
Example #4
0
waitrcvchar()
{
	int c;

	while( (c = rcvchar()) == -1 ){
		if( pendulum ) cursswitch( &C_cup );
		wait(RCV);
	}
	return c;
}
Example #5
0
File: io.c Project: pocket7878/sam
int
getch(void)
{
    int c;

    while((c = rcvchar()) == -1){
        block = ~Ehost;
        waitforio();
        block = 0;
    }
    return c;
}
Example #6
0
int main(int argc, char *argv[]){

  //check whether argument is valid or not
	if (argc != 2) {
		printf("./receiver <Service Port>\n");
		return 0;
	}

	// define the service port
	servicePort = atoi(argv[1]);

  // create connection with UDP socket
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("cannot create socket\n");
    return 0;
  }

  // bind the socket to a specific port and IP address
  memset((char *)&thisAddr, 0, sizeof(thisAddr));
  thisAddr.sin_family = AF_INET;
  thisAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  thisAddr.sin_port = htons(servicePort);

  if (bind(sockfd, (struct sockaddr *)&thisAddr, sizeof(thisAddr)) < 0) {
    perror("bind failed");
    return 0;
  }
	else {
		printf("Binding pada 127.0.0.1:%d ...\n",servicePort);
	}

	if (pthread_mutex_init(&lock, NULL) != 0) {
    printf("\n mutex init failed\n");
    return 1;
	}

	pthread_create(&tid, NULL, mainThread, NULL);

  while (1) {
    currentByte = *(rcvchar(sockfd, rxq));
    // Quit on end of file
    if (currentByte == Endfile) {
      exit(0);
    }
    usleep(DELAY*1000);
  }

	pthread_join(tid, NULL);
	pthread_mutex_destroy(&lock);

	return 0;
}
int main(int argc, char **argv)
/* Argv and argc will be used to define port number */
{	
	Byte c;
	struct ifreq ifr;

	/* Create a socket and bind it to a specified port according argument initialization */ 
		/* Create a UDP socket */
		if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
			perror("Cannot create socket\n");
			return 0;
		}

		/* Bind the socket to any valid IP address and a specific port */
		memset((char *)&myaddr, 0, sizeof(myaddr));
		myaddr.sin_family = AF_INET; // Address family used when set up the socket
		myaddr.sin_addr.s_addr = htonl(INADDR_ANY); // Address for socket
		myaddr.sin_port = htons(atoi(argv[1])); // Port number of socket
		
		if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
			perror("bind failed");
			return 0;
		}

	ifr.ifr_addr.sa_family = AF_INET; //Type of address to retrieve
	strncpy(ifr.ifr_name,"eth0", IFNAMSIZ-1); //Copy the interface name in the ifreq structure
	ioctl(sockfd, SIOCGIFADDR, &ifr); //Get the IP address
	printf("Binding pada %s:%s\n", inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr), argv[1]);
	
	/* Initialize XON/XOFF flags */
	xon_active = true; // Transmitter can send any message now

	/* Create child process */
	pthread_t consumerbuff;
	pthread_create(&consumerbuff, NULL, &consumeBuff, NULL);

	/*** IF PARENT PROCESS ***/
	while (true) {
		c = *(rcvchar(sockfd, rxq));
		/* Quit on end of file */
		if (c == Endfile) {
			while (rcvq.count>0) {
			}
			printf("End of file character has been received and the buffer is already empty.\n");
			close(sockfd);
			exit(0);
		}
	}
}
int main(int argc, char *argv[])
{
 	pthread_t thread[1];

 	if (argc<2) {
 		// case if arguments are less than specified
 		printf("Please use the program with arguments: %s <port>\n", argv[0]);
 		return 0;
 	}

 	printf("Creating socket to self in Port %s...\n", argv[1]);
 	if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
 		printf("ERROR: Create socket failed.\n");

 	bzero((char*) &adhost, sizeof(adhost));
 	adhost.sin_family = AF_INET;
 	adhost.sin_port = htons(atoi(argv[1]));
 	adhost.sin_addr.s_addr = INADDR_ANY;

 	if(bind(sockfd, (struct sockaddr*) &adhost, sizeof(adhost)) < 0)
 		error("ERROR: Binding failed.\n");

 	endFileReceived = 0;

 	memset(rxbuf, 0, sizeof(rxbuf));
 	
 	/* Create child process */
 	if(pthread_create(&thread[0], NULL, childRProcess, 0)) 
 		error("ERROR: Failed to create thread for child.\n");

	/* parent process: unlimited looping */
	Byte c;
	while (1) {
 		c = *(rcvchar(sockfd, rxq));

 		if (c == Endfile)
 			endFileReceived = 1;
	}

	printf("Receiving End of File... Receiver Complete!\n");
	printf("Waiting for the next transmission...\n");

	return 0;
}
Example #9
0
File: mesg.c Project: rennis250/sam
int
rcv(void){
	int c;
	static int state = 0;
	static int count = 0;
	static int i = 0;

	while((c=rcvchar()) != -1)
		switch(state){
		case 0:
			h.type = c;
			state++;
			break;

		case 1:
			h.count0 = c;
			state++;
			break;

		case 2:
			h.count1 = c;
			count = h.count0|(h.count1<<8);
			i = 0;
			if(count > DATASIZE)
				panic("count>DATASIZE");
			if(count == 0)
				goto zerocount;
			state++;
			break;

		case 3:
			indata[i++] = c;
			if(i == count){
		zerocount:
				indata[i] = 0;
				state = count = 0;
				return inmesg(h.type);
			}
			break;
		}
	return 0;
}
Example #10
0
void* threadParent(void *arg) {
	/* Parent Thread */
	char* chck;
	
	/* Ketika belum diterima End Of Frame, 
	   teruskan listening untuk penerimaan byte */
	while ( (parentExit != 1) ) {	
		chck = rcvchar(sockfd, &buffer);
		if ( chck != NULL ) {
			Frame F;
			F.GetDecompiled(chck);

			if ( (F.GetMessage()).at(0) != 26 ) {
				printf("Menerima Frame ke-%d\n", F.GetNumber());
			} else {
				printf("EOF diterima\n");
				getEOF = F.GetNumber();
			}		
		} 
	}

	printf("Exiting Child\n");
	return NULL;
}
Example #11
0
File: mesg.c Project: rennis250/sam
int
inmesg(Tmesg type)
{
	Rune buf[1025];
	char cbuf[64];
	int i, m;
	short s;
	long l, l1;
	vlong v;
	File *f;
	Posn p0, p1, p;
	Range r;
	String *str;
	char *c, *wdir;
	Rune *rp;
	Plumbmsg *pm;

	if(type > TMAX)
		panic("inmesg");

	journal(0, tname[type]);

	inp = indata;
	switch(type){
	case -1:
		panic("rcv error");

	default:
		fprint(2, "unknown type %d\n", type);
		panic("rcv unknown");

	case Tversion:
		tversion = inshort();
		journaln(0, tversion);
		break;

	case Tstartcmdfile:
		v = invlong();		/* for 64-bit pointers */
		journaln(0, v);
		Strdupl(&genstr, samname);
		cmd = newfile();
		cmd->unread = 0;
		outTsv(Hbindname, cmd->tag, v);
		outTs(Hcurrent, cmd->tag);
		logsetname(cmd, &genstr);
		cmd->rasp = listalloc('P');
		cmd->mod = 0;
		if(cmdstr.n){
			loginsert(cmd, 0L, cmdstr.s, cmdstr.n);
			Strdelete(&cmdstr, 0L, (Posn)cmdstr.n);
		}
		fileupdate(cmd, FALSE, TRUE);
		outT0(Hunlock);
		break;

	case Tcheck:
		/* go through whichfile to check the tag */
		outTs(Hcheck, whichfile(inshort())->tag);
		break;

	case Trequest:
		f = whichfile(inshort());
		p0 = inlong();
		p1 = p0+inshort();
		journaln(0, p0);
		journaln(0, p1-p0);
		if(f->unread)
			panic("Trequest: unread");
		if(p1>f->b.nc)
			p1 = f->b.nc;
		if(p0>f->b.nc) /* can happen e.g. scrolling during command */
			p0 = f->b.nc;
		if(p0 == p1){
			i = 0;
			r.p1 = r.p2 = p0;
		}else{
			r = rdata(f->rasp, p0, p1-p0);
			i = r.p2-r.p1;
			bufread(&f->b, r.p1, buf, i);
		}
		buf[i]=0;
		outTslS(Hdata, f->tag, r.p1, tmprstr(buf, i+1));
		break;

	case Torigin:
		s = inshort();
		l = inlong();
		l1 = inlong();
		journaln(0, l1);
		lookorigin(whichfile(s), l, l1);
		break;

	case Tstartfile:
		termlocked++;
		f = whichfile(inshort());
		if(!f->rasp)	/* this might be a duplicate message */
			f->rasp = listalloc('P');
		current(f);
		outTsv(Hbindname, f->tag, invlong());	/* for 64-bit pointers */
		outTs(Hcurrent, f->tag);
		journaln(0, f->tag);
		if(f->unread)
			load(f);
		else{
			if(f->b.nc>0){
				rgrow(f->rasp, 0L, f->b.nc);
				outTsll(Hgrow, f->tag, 0L, f->b.nc);
			}
			outTs(Hcheck0, f->tag);
			moveto(f, f->dot.r);
		}
		break;

	case Tworkfile:
		i = inshort();
		f = whichfile(i);
		current(f);
		f->dot.r.p1 = inlong();
		f->dot.r.p2 = inlong();
		f->tdot = f->dot.r;
		journaln(0, i);
		journaln(0, f->dot.r.p1);
		journaln(0, f->dot.r.p2);
		break;

	case Ttype:
		f = whichfile(inshort());
		p0 = inlong();
		journaln(0, p0);
		journal(0, (char*)inp);
		str = tmpcstr((char*)inp);
		i = str->n;
		loginsert(f, p0, str->s, str->n);
		if(fileupdate(f, FALSE, FALSE))
			seq++;
		if(f==cmd && p0==f->b.nc-i && i>0 && str->s[i-1]=='\n'){
			freetmpstr(str);
			termlocked++;
			termcommand();
		}else
			freetmpstr(str);
		f->dot.r.p1 = f->dot.r.p2 = p0+i; /* terminal knows this already */
		f->tdot = f->dot.r;
		break;

	case Tcut:
		f = whichfile(inshort());
		p0 = inlong();
		p1 = inlong();
		journaln(0, p0);
		journaln(0, p1);
		logdelete(f, p0, p1);
		if(fileupdate(f, FALSE, FALSE))
			seq++;
		f->dot.r.p1 = f->dot.r.p2 = p0;
		f->tdot = f->dot.r;   /* terminal knows the value of dot already */
		break;

	case Tpaste:
		f = whichfile(inshort());
		p0 = inlong();
		journaln(0, p0);
		for(l=0; l<snarfbuf.nc; l+=m){
			m = snarfbuf.nc-l;
			if(m>BLOCKSIZE)
				m = BLOCKSIZE;
			bufread(&snarfbuf, l, genbuf, m);
			loginsert(f, p0, tmprstr(genbuf, m)->s, m);
		}
		if(fileupdate(f, FALSE, TRUE))
			seq++;
		f->dot.r.p1 = p0;
		f->dot.r.p2 = p0+snarfbuf.nc;
		f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
		telldot(f);
		outTs(Hunlockfile, f->tag);
		break;

	case Tsnarf:
		i = inshort();
		p0 = inlong();
		p1 = inlong();
		snarf(whichfile(i), p0, p1, &snarfbuf, 0);
		break;

	case Tstartnewfile:
		v = invlong();
		Strdupl(&genstr, empty);
		f = newfile();
		f->rasp = listalloc('P');
		outTsv(Hbindname, f->tag, v);
		logsetname(f, &genstr);
		outTs(Hcurrent, f->tag);
		current(f);
		load(f);
		break;

	case Twrite:
		termlocked++;
		i = inshort();
		journaln(0, i);
		f = whichfile(i);
		addr.r.p1 = 0;
		addr.r.p2 = f->b.nc;
		if(f->name.s[0] == 0)
			error(Enoname);
		Strduplstr(&genstr, &f->name);
		writef(f);
		break;

	case Tclose:
		termlocked++;
		i = inshort();
		journaln(0, i);
		f = whichfile(i);
		current(f);
		trytoclose(f);
		/* if trytoclose fails, will error out */
		delete(f);
		break;

	case Tlook:
		f = whichfile(inshort());
		termlocked++;
		p0 = inlong();
		p1 = inlong();
		journaln(0, p0);
		journaln(0, p1);
		setgenstr(f, p0, p1);
		for(l = 0; l<genstr.n; l++){
			i = genstr.s[l];
			if(utfrune(".*+?(|)\\[]^$", i)){
				str = tmpcstr("\\");
				Strinsert(&genstr, str, l++);
				freetmpstr(str);
			}
		}
		Straddc(&genstr, '\0');
		nextmatch(f, &genstr, p1, 1);
		moveto(f, sel.p[0]);
		break;

	case Tsearch:
		termlocked++;
		if(curfile == 0)
			error(Enofile);
		if(lastpat.s[0] == 0)
			panic("Tsearch");
		nextmatch(curfile, &lastpat, curfile->dot.r.p2, 1);
		moveto(curfile, sel.p[0]);
		break;

	case Tsend:
		termlocked++;
		inshort();	/* ignored */
		p0 = inlong();
		p1 = inlong();
		setgenstr(cmd, p0, p1);
		bufreset(&snarfbuf);
		bufinsert(&snarfbuf, (Posn)0, genstr.s, genstr.n);
		outTl(Hsnarflen, genstr.n);
		if(genstr.s[genstr.n-1] != '\n')
			Straddc(&genstr, '\n');
		loginsert(cmd, cmd->b.nc, genstr.s, genstr.n);
		fileupdate(cmd, FALSE, TRUE);
		cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->b.nc;
		telldot(cmd);
		termcommand();
		break;

	case Tdclick:
		f = whichfile(inshort());
		p1 = inlong();
		doubleclick(f, p1);
		f->tdot.p1 = f->tdot.p2 = p1;
		telldot(f);
		outTs(Hunlockfile, f->tag);
		break;

	case Tstartsnarf:
		if (snarfbuf.nc <= 0) {	/* nothing to export */
			outTs(Hsetsnarf, 0);
			break;
		}
		c = 0;
		i = 0;
		m = snarfbuf.nc;
		if(m > SNARFSIZE) {
			m = SNARFSIZE;
			dprint("?warning: snarf buffer truncated\n");
		}
		rp = malloc(m*sizeof(Rune));
		if(rp){
			bufread(&snarfbuf, 0, rp, m);
			c = Strtoc(tmprstr(rp, m));
			free(rp);
			i = strlen(c);
		}
		outTs(Hsetsnarf, i);
		if(c){
			Write(1, c, i);
			free(c);
		} else
			dprint("snarf buffer too long\n");
		break;

	case Tsetsnarf:
		m = inshort();
		if(m > SNARFSIZE)
			error(Etoolong);
		c = malloc(m+1);
		if(c){
			for(i=0; i<m; i++)
				c[i] = rcvchar();
			c[m] = 0;
			str = tmpcstr(c);
			free(c);
			bufreset(&snarfbuf);
			bufinsert(&snarfbuf, (Posn)0, str->s, str->n);
			freetmpstr(str);
			outT0(Hunlock);
		}
		break;

	case Tack:
		waitack = 0;
		break;

	case Tplumb:
		f = whichfile(inshort());
		p0 = inlong();
		p1 = inlong();
		pm = emalloc(sizeof(Plumbmsg));
		pm->src = strdup("sam");
		pm->dst = 0;
		/* construct current directory */
		c = Strtoc(&f->name);
		if(c[0] == '/')
			pm->wdir = c;
		else{
			wdir = emalloc(1024);
			getwd(wdir, 1024);
			pm->wdir = emalloc(1024);
			snprint(pm->wdir, 1024, "%s/%s", wdir, c);
			cleanname(pm->wdir);
			free(wdir);
			free(c);
		}
		c = strrchr(pm->wdir, '/');
		if(c)
			*c = '\0';
		pm->type = strdup("text");
		if(p1 > p0)
			pm->attr = nil;
		else{
			p = p0;
			while(p0>0 && (i=filereadc(f, p0 - 1))!=' ' && i!='\t' && i!='\n')
				p0--;
			while(p1<f->b.nc && (i=filereadc(f, p1))!=' ' && i!='\t' && i!='\n')
				p1++;
			sprint(cbuf, "click=%ld", p-p0);
			pm->attr = plumbunpackattr(cbuf);
		}
		if(p0==p1 || p1-p0>=BLOCKSIZE){
			plumbfree(pm);
			break;
		}
		setgenstr(f, p0, p1);
		pm->data = Strtoc(&genstr);
		pm->ndata = strlen(pm->data);
		c = plumbpack(pm, &i);
		if(c != 0){
			outTs(Hplumb, i);
			Write(1, c, i);
			free(c);
		}
		plumbfree(pm);
		break;

	case Texit:
		exits(0);
	}
	return TRUE;
}
Example #12
0
File: gnotfix.c Project: kahrs/cda
int
mygetc(void)
{
	return(rcvchar());
}
Example #13
0
int
inmesg(Tmesg type)
{
    Rune buf[1025];
    int i, m;
    short s;
    long l, l1;
    File *f;
    Posn p0, p1;
    Range r;
    String *str;
    char *c;
    Rune *rp;

    if(type > TMAX)
        panic("inmesg");

    journal(0, tname[type]);

    inp = indata;
    switch(type) {
    case -1:
        panic("rcv error");

    default:
        fprint(2, "unknown type %d\n", type);
        panic("rcv unknown");

    case Tversion:
        tversion = inshort();
        journaln(0, tversion);
        break;

    case Tstartcmdfile:
        l = invlong();		/* for 64-bit pointers */
        journaln(0, l);
        Strdupl(&genstr, samname);
        cmd = newfile();
        outTsv(Hbindname, cmd->tag, l);
        outTs(Hcurrent, cmd->tag);
        Fsetname(cmd, &genstr);
        cmd->rasp = emalloc(sizeof(List));
        cmd->state = Clean;
        if(cmdstr.n) {
            Finsert(cmd, &cmdstr, 0L);
            Strdelete(&cmdstr, 0L, (Posn)cmdstr.n);
        }
        Fupdate(cmd, FALSE, TRUE);
        outT0(Hunlock);
        break;

    case Tcheck:
        /* go through whichfile to check the tag */
        outTs(Hcheck, whichfile(inshort())->tag);
        break;

    case Trequest:
        f = whichfile(inshort());
        p0 = inlong();
        p1 = p0+inshort();
        journaln(0, p0);
        journaln(0, p1-p0);
        if(f->state == Unread)
            panic("Trequest: unread");
        if(p1>f->nrunes)
            p1 = f->nrunes;
        if(p0>f->nrunes) /* can happen e.g. scrolling during command */
            p0 = f->nrunes;
        if(p0 == p1) {
            i = 0;
            r.p1 = r.p2 = p0;
        } else {
            r = rdata(f->rasp, p0, p1-p0);
            i = r.p2-r.p1;
            if(Fchars(f, buf, r.p1, r.p2)!=i)
                panic("Trequest 2");
        }
        buf[i]=0;
        outTslS(Hdata, f->tag, r.p1, tmprstr(buf, i+1));
        break;

    case Torigin:
        s = inshort();
        l = inlong();
        l1 = inlong();
        journaln(0, l1);
        lookorigin(whichfile(s), l, l1);
        break;

    case Tstartfile:
        termlocked++;
        f = whichfile(inshort());
        if(!f->rasp)	/* this might be a duplicate message */
            f->rasp = emalloc(sizeof(List));
        current(f);
        outTsv(Hbindname, f->tag, invlong());	/* for 64-bit pointers */
        outTs(Hcurrent, f->tag);
        journaln(0, f->tag);
        if(f->state == Unread)
            load(f);
        else {
            if(f->nrunes>0) {
                rgrow(f->rasp, 0L, f->nrunes);
                outTsll(Hgrow, f->tag, 0L, f->nrunes);
            }
            outTs(Hcheck0, f->tag);
            moveto(f, f->dot.r);
        }
        break;

    case Tworkfile:
        i = inshort();
        f = whichfile(i);
        current(f);
        f->dot.r.p1 = inlong();
        f->dot.r.p2 = inlong();
        f->tdot = f->dot.r;
        journaln(0, i);
        journaln(0, f->dot.r.p1);
        journaln(0, f->dot.r.p2);
        break;

    case Ttype:
        f = whichfile(inshort());
        p0 = inlong();
        journaln(0, p0);
        journal(0, (char*)inp);
        str = tmpcstr((char*)inp);
        i = str->n;
        Finsert(f, str, p0);
        if(Fupdate(f, FALSE, FALSE))
            modnum++;
        if(f==cmd && p0==f->nrunes-i && i>0 && str->s[i-1]=='\n') {
            freetmpstr(str);
            termlocked++;
            termcommand();
        } else
            freetmpstr(str);
        f->dot.r.p1 = f->dot.r.p2 = p0+i; /* terminal knows this already */
        f->tdot = f->dot.r;
        break;

    case Tcut:
        f = whichfile(inshort());
        p0 = inlong();
        p1 = inlong();
        journaln(0, p0);
        journaln(0, p1);
        Fdelete(f, p0, p1);
        if(Fupdate(f, FALSE, FALSE))
            modnum++;
        f->dot.r.p1 = f->dot.r.p2 = p0;
        f->tdot = f->dot.r;   /* terminal knows the value of dot already */
        break;

    case Tpaste:
        f = whichfile(inshort());
        p0 = inlong();
        journaln(0, p0);
        for(l=0; l<snarfbuf->nrunes; l+=m) {
            m = snarfbuf->nrunes-l;
            if(m>BLOCKSIZE)
                m = BLOCKSIZE;
            Bread(snarfbuf, genbuf, m, l);
            Finsert(f, tmprstr(genbuf, m), p0);
        }
        if(Fupdate(f, FALSE, TRUE))
            modnum++;
        f->dot.r.p1 = p0;
        f->dot.r.p2 = p0+snarfbuf->nrunes;
        f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
        telldot(f);
        outTs(Hunlockfile, f->tag);
        break;

    case Tsnarf:
        i = inshort();
        p0 = inlong();
        p1 = inlong();
        snarf(whichfile(i), p0, p1, snarfbuf, 0);
        break;

    case Tstartnewfile:
        l = invlong();
        Strdupl(&genstr, empty);
        f = newfile();
        f->rasp = emalloc(sizeof(List));
        outTsv(Hbindname, f->tag, l);
        Fsetname(f, &genstr);
        outTs(Hcurrent, f->tag);
        current(f);
        load(f);
        break;

    case Twrite:
        termlocked++;
        i = inshort();
        journaln(0, i);
        f = whichfile(i);
        addr.r.p1 = 0;
        addr.r.p2 = f->nrunes;
        if(f->name.s[0] == 0)
            error(Enoname);
        Strduplstr(&genstr, &f->name);
        writef(f);
        break;

    case Tclose:
        termlocked++;
        i = inshort();
        journaln(0, i);
        f = whichfile(i);
        current(f);
        trytoclose(f);
        /* if trytoclose fails, will error out */
        delete(f);
        break;

    case Tlook:
        f = whichfile(inshort());
        termlocked++;
        p0 = inlong();
        p1 = inlong();
        journaln(0, p0);
        journaln(0, p1);
        setgenstr(f, p0, p1);
        for(l = 0; l<genstr.n; l++) {
            i = genstr.s[l];
            if(utfrune(".*+?(|)\\[]^$", i))
                Strinsert(&genstr, tmpcstr("\\"), l++);
        }
        Straddc(&genstr, '\0');
        nextmatch(f, &genstr, p1, 1);
        moveto(f, sel.p[0]);
        break;

    case Tsearch:
        termlocked++;
        if(curfile == 0)
            error(Enofile);
        if(lastpat.s[0] == 0)
            panic("Tsearch");
        nextmatch(curfile, &lastpat, curfile->dot.r.p2, 1);
        moveto(curfile, sel.p[0]);
        break;

    case Tsend:
        termlocked++;
        inshort();	/* ignored */
        p0 = inlong();
        p1 = inlong();
        setgenstr(cmd, p0, p1);
        Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
        Binsert(snarfbuf, &genstr, (Posn)0);
        outTl(Hsnarflen, genstr.n);
        if(genstr.s[genstr.n-1] != '\n')
            Straddc(&genstr, '\n');
        Finsert(cmd, &genstr, cmd->nrunes);
        Fupdate(cmd, FALSE, TRUE);
        cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->nrunes;
        telldot(cmd);
        termcommand();
        break;

    case Tdclick:
        f = whichfile(inshort());
        p1 = inlong();
        doubleclick(f, p1);
        f->tdot.p1 = f->tdot.p2 = p1;
        telldot(f);
        outTs(Hunlockfile, f->tag);
        break;

    case Tstartsnarf:
        if (snarfbuf->nrunes <= 0) {	/* nothing to export */
            outTs(Hsetsnarf, 0);
            break;
        }
        c = 0;
        i = 0;
        m = snarfbuf->nrunes;
        if(m > 32000) {		/* tmprstr stores len in a short */
            m = 32000;
            dprint("?warning: snarf buffer truncated\n");
        }
        rp = malloc(m*sizeof(Rune));
        if(rp) {
            Bread(snarfbuf, rp, m, 0);
            c = Strtoc(tmprstr(rp, m));
            free(rp);
            i = strlen(c);
        }
        outTs(Hsetsnarf, i);
        if(c) {
            Write(1, c, i);
            free(c);
        } else
            dprint("snarf buffer too long\n");
        break;

    case Tsetsnarf:
        m = inshort();
        if(m > SNARFSIZE)
            error(Etoolong);
        c = malloc(m+1);
        if(c) {
            for(i=0; i<m; i++)
                c[i] = rcvchar();
            c[m] = 0;
            str = tmpcstr(c);
            free(c);
            Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
            Binsert(snarfbuf, str, (Posn)0);
            freetmpstr(str);
            outT0(Hunlock);
        }
        break;

    case Tack:
        waitack = 0;
        break;

    case Texit:
        exits(0);
    }
    return TRUE;
}
Example #14
0
int
waitchar(void)
{
	Event e;
	int c;
	char c2;
	int newmouse;
	int wasblocked;
	int kbdchar = -1;
	char echobuf[3*BSIZE];
	static int lastc = -1;


	for(;;) {
		if(resize_flag)
			resize();
		wasblocked = blocked;
		if(backp)
			return(0);
		if(ecanmouse() && (button2() || button3()))
			readmenu();
		if(snarffp) {
			if((c = Bgetc(snarffp)) < 0) {
				if(lastc != '\n')
					write(outfd,"\n",1);
				Bterm(snarffp);
				snarffp = 0;
				if(lastc != '\n') {
					lastc = -1;
					return('\n');
				}
				lastc = -1;
				continue;
			}
			lastc = c;
			c2 = c;
			write(outfd, &c2, 1);
			return(c);
		}
		if(!blocked && host_avail())
			return(rcvchar());
		if(kbdchar > 0) {
			if(blocked)
				resize();
			if(cs->raw) {
				switch(kbdchar){
				case Kup:
					sendfk("up key");
					break;
				case Kdown:
					sendfk("down key");
					break;
				case Kleft:
					sendfk("left key");
					break;
				case Kright:
					sendfk("right key");
					break;
				case Kpgup:
					sendfk("page up");
					break;
				case Kpgdown:
					sendfk("page down");
					break;
				case KF|1:
					sendfk("F1");
					break;
				case KF|2:
					sendfk("F2");
					break;
				case KF|3:
					sendfk("F3");
					break;
				case KF|4:
					sendfk("F4");
					break;
				case KF|5:
					sendfk("F5");
					break;
				case KF|6:
					sendfk("F6");
					break;
				case KF|7:
					sendfk("F7");
					break;
				case KF|8:
					sendfk("F8");
					break;
				case KF|9:
					sendfk("F9");
					break;
				case KF|10:
					sendfk("F10");
					break;
				case KF|11:
					sendfk("F11");
					break;
				case KF|12:
					sendfk("F12");
					break;
				case '\n':
					echobuf[0] = '\r';
					sendnchars(1, echobuf);
					break;
				case '\r':
					echobuf[0] = '\n';
					sendnchars(1, echobuf);
					break;
				default:
					echobuf[0] = kbdchar;
					sendnchars(1, echobuf);
					break;
				}
			} else if(canon(echobuf,kbdchar) == SCROLL) {
				if(!blocked)
					bigscroll();
			} else
				strcat(echo_input,echobuf);
			blocked = 0;
			kbdchar = -1;
			continue;
		}
		curson(wasblocked);	/* turn on cursor while we're waiting */
		do {
			newmouse = 0;
			switch(eread(blocked ? Emouse|Ekeyboard :
					       Emouse|Ekeyboard|Ehost, &e)) {
			case Emouse:
				mouse = e.mouse;
				if(button2() || button3())
					readmenu();
				else if(resize_flag == 0) {
					/* eresized() is triggered by special mouse event */
					newmouse = 1;
				}
				break;
			case Ekeyboard:
				kbdchar = e.kbdc;
				break;
			case Ehost:
				set_host(&e);
				break;
			default:
				perror("protocol violation");
				exits("protocol violation");
			}
		} while(newmouse == 1);
		cursoff();	/* turn cursor back off */
	}
}