⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffers.c

📁 Gcomm is a serial communications program similar to seyon, but more modern, and easier to use. Works
💻 C
字号:
#ifndef lintstatic	const char rcsid[] = "$Id: buffers.c,v 1.1.1.1 2001/03/08 00:01:48 efalk Exp $" ;#endif/* main program for gcomm */#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <unistd.h>#include <stdarg.h>#include <malloc.h>#include <termios.h>#include <string.h>#include <ctype.h>#include <errno.h>#include <sys/types.h>#include <sys/time.h>#include <sys/param.h>#include <gtk/gtk.h>#include "params.h"#include "gcomm.h"#include "buffers.h"#include "serial.h"#include "network.h"#include "pipe.h"#include "callbacks.h"#ifdef	IODEBUG#include "Xmodem/seriallog.h"#else#define	SerialLogFlush()#define	SerialLog(a,b,c)#endifstatic	ssize_t	bufRead(int fd, void *buf, size_t count) ;voidclear_buffers(){	termBuf.cnt = termBuf.ptr = 0 ;	inputBuf.cnt = inputBuf.ptr = 0 ;	outputBuf.cnt = outputBuf.ptr = 0 ;}	/* Unload as many characters as we can from the buffer.	 * Return:	 *  0	success	 *  -1	I/O error	 *  +n	partial success, n characters remaining in buffer	 */inthandle_output(int fd, Buffer *buf, char *what){	int	len ;	int	i ;	while( buf->cnt > 0 )	{	  len = buf->cnt ;	  if( buf->ptr > 0 ) {		/* wrapped buffer, common case */	    if( buf->ptr + len > BufLen )	      len = BufLen - buf->ptr ;	  }	  i = bufWrite(fd, buf->buffer+buf->ptr, len) ;	  if( i == -1 && errno == EWOULDBLOCK )	    i = 0 ;	  if( i == -1 ) {	    perror(what) ;	    return -1 ;	  }	  if( (buf->ptr += i) >= BufLen )	    buf->ptr -= BufLen ;	  buf->cnt -= i ;	  if( i < len )	    return buf->cnt ;	}	/* lucky us, we emptied the buffer.  Reset start pointer too,	 * to reduce wrapped buffers.	 */	buf->ptr = 0 ;	return 0 ;}	/* Read as many characters from fd as it will provide, or until	 * the buffer is full.	 * Return values:	 *  +n	success, n characters in buffer	 *  -1	I/O error	 */inthandle_input(int fd, Buffer *buf, char *what){	int	ptr,len,i ;	while( buf->cnt < BufLen )	{	  ptr = buf->ptr + buf->cnt ;	/* ptr is pointer to where we read */	  if( ptr >= BufLen )	    ptr -= BufLen ;	  len = BufLen - buf->cnt ;	  if( ptr + len > BufLen )	    len = BufLen - ptr ;	  i = bufRead(fd, buf->buffer+ptr, len) ;	  if( i == 0 )	    return buf->cnt ;			/* EOF */	  if( i == -1 ) {	    if( errno == EIO ) {	      SerialLogFlush() ;	      sleep(1) ;	      return -1 ;		/* error */	    }	    if( errno != EWOULDBLOCK )	      perror(what) ;	    return buf->cnt ;		/* data available */	  }	  buf->cnt += i ;	}	return buf->cnt ;}	/* Write out as much data as we can to the terminal. */intterminalOutput(){	int	len ;	len = handle_output(1, terminalBuffer, "Terminal output") ;	/* If the buffer isn't empty, then that means we couldn't write	 * it all out.  Install a handler for when we can.	 */	if( len > 0 && termoHandler == -1 )	  termoHandler = gdk_input_add(1, GDK_INPUT_WRITE, termoCB, NULL) ;	/* In addition, if input was turned off, turn it back on */	if( len < BufLen && inputHandler == -1 )	  inputHandler = gdk_input_add( ifd, GDK_INPUT_READ, inputCB, NULL ) ;	return len ;}	/* Write out as much data as we can to the remote. */intremoteOutput(){	int	len ;	len = handle_output(ofd, &outputBuf, "Remote output") ;	/* If the buffer isn't empty, then that means we couldn't write	 * it all out.  Install a handler for when we can.	 */	if( len > 0 && outputHandler == -1 )	  outputHandler = gdk_input_add(ofd, GDK_INPUT_WRITE, outputCB, NULL) ;	/* In addition, if input was turned off, turn it back on */	if( len < BufLen && termiHandler == -1 )	  termiHandler = gdk_input_add( 0, GDK_INPUT_READ, termiCB, NULL ) ;	return len ;}	/* append to a buffer, wait for enough room if needed */voidappend_buffer(Buffer *buf, char *str, int len, int fd){	int	i,j ;	int	ptr ;	while( len > 0 )	{	  /* if buffer full, get rid of some */	  if( buf->cnt >= BufLen )	  {	    fd_set ofds ;	    FD_ZERO(&ofds) ;	    FD_SET(fd, &ofds) ;	    i = select(32, NULL, &ofds, NULL, NULL ) ;	    if( i <= 0 )	      perror("append_buffer: select") ;	    else if( handle_output(fd, buf, "append_buffer") )	      return ;	  }	  /* put as many characters as will fit into buffer */	  ptr = buf->ptr + buf->cnt ;	  if( ptr >= BufLen ) ptr -= BufLen ;	  i = len ;	  if( (j = BufLen - buf->cnt) < i )	    i = j ;	  if( (j = BufLen - ptr) < i )	    i = j ;	  bcopy(str, buf->buffer + ptr, i) ;	  buf->cnt += i ;	  len -= i ;	  str += i ;	}}	/* wait until buffer empty */voidflush_buffer(Buffer *buf, int fd){	int	i ;	fd_set	ofds ;	/* if buffer full, get rid of some */	while( buf->cnt > 0 )	{	  FD_ZERO(&ofds) ;	  FD_SET(fd, &ofds) ;	  i = select(32, NULL, &ofds, NULL, NULL ) ;	  if( i <= 0 )	    perror("append_buffer: select") ;	  else if( handle_output(fd, buf, "append_buffer") )	    return ;	}}voidappend_buffer1(Buffer *buf, int c, int fd){	char	ch = c ;	append_buffer(buf, &ch,1, fd) ;}	/* read one character from buffer.  Assume non-empty */intget_buffer(Buffer *buf){	int	c ;	if( buf->cnt == 0 )	  return -1 ;	c = buf->buffer[buf->ptr] & 0377 ;	if( buf->ptr++ >= sizeof(buf->buffer) ) buf->ptr = 0 ;	if( --buf->cnt <= 0 )	  buf->ptr = 0 ;	return c ;}voiddumpData(char *what, int fd, const char *buf, size_t count){	int	len = 22 ;;	printf("%s %d bytes fd %d: ", what, count, fd) ;	for(; count-- > 0; ++buf ) {	  if( len == 0 ) {	    putchar('\n') ;	    len = 8 ;	  }	  if( *buf < ' ' ) {	    printf("^%c", *buf+0100) ;	    len += 1 ;	  }	  else if( *buf >= 0177 ) {	    printf("=%2.2x", *buf) ;	    len += 2 ;	  }	  else	    putchar(*buf) ;	  if( ++len >= 72 ) {	    putchar('\n') ;	    len = 0 ;	  }	}	if( len > 0 )	  putchar('\n') ;}static	ssize_tbufRead(int fd, void *buf, size_t count){	int	rval = read(fd, buf, count) ;#ifdef	IODEBUG	if( fd == ifd )	  SerialLog(buf, rval, 1) ;#endif	return rval ;}ssize_tbufWrite(int fd, const void *buf, size_t count){#ifdef	IODEBUG	if( fd == ofd )	  SerialLog(buf, count, 0) ;#endif	return write(fd, buf, count) ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -