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

📄 eculine.c

📁 一个通讯程序源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*+-----------------------------------------------------------------------	eculine.c -- ECU line handler	wht@n4hgf.Mt-Park.GA.US  Defined functions:	display_hw_flow_config()	lCLOCAL(flag)	lRTSCTS_control(flag)	lbreak()	lclear_xmtr_xoff()	lclose()	lclose_failed(sig)	ldcdwatch(flag)	ldcdwatch_str(flagstr)	ldraino(inflush_flag)	lflash_dtr()	lflush(flush_type)	lget_xon_xoff(ixon,ixoff)	lgetc_timeout(msec)	lgetc_xmtr()	lgets_timeout(lrwt)	llookfor(lookfor,msecs,echo_flag)	lnew_baud_rate(new_baud)	lopen()	lopen_failed(sig)	lputc(lchar)	lputc_paced(pace_msec,lchar)	lputs(string)	lputs_paced(pace_msec,string)	lquiet(msecs,echo_flag)	lrdchk_xmtr()	lreset_ksr()	lset_baud_rate(ioctl_flag)	lset_parity(ioctl_flag)	linst_err_text(lerr)	lxon_xoff(flag)	lzero_length_read_detected()	process_xmtr_rcvd_char(rchar,echo)	set_xon_xoff_by_arg(arg)	valid_baud_rate(baud)	xon_status()Note: On the Sun or with -DUSE_TERMIOS, termios is used in lieu oftermio.  See ecu.h and search for USE_TERMIOS for vague illumination.------------------------------------------------------------------------*//*+:EDITS:*//*:09-10-1992-13:58-wht@n4hgf-ECU release 3.20 *//*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA *//*:08-21-1992-13:39-wht@n4hgf-rewire direct/modem device use *//*:08-19-1992-14:03-wht@n4hgf-2nd open in lflash_dtr needs O_NDELAY on SVR4 *//*:08-16-1992-02:52-wht@n4hgf-some vendors use SCO naming but ttyaa/ttyaA *//*:07-27-1992-05:49-wht@n4hgf-lopen SCO modem line to make CLOCAL effective *//*:07-24-1992-14:25-wht@n4hgf-more valiant efforts on lclose failure *//*:07-19-1992-09:19-wht@n4hgf-lopen validation for char special *//*:05-11-1992-16:35-wht@gyro-speed up lflash_DTR on sun *//*:05-04-1992-04:43-kortcs!tim-fix EAGAIN on line open with SVR4 *//*:04-27-1992-19:57-wht@n4hgf-add LINST_ECUUNGETTY error text *//*:04-24-1992-21:59-wht@n4hgf-more SCO tty name normalizing *//*:04-24-1992-21:44-wht@n4hgf-add SCO_direct_tty *//*:04-12-1992-06:31-wht@gyro-was not canceling alarm on lopen error *//*:03-29-1992-16:27-wht@n4hgf-put three second timer on lopen *//*:03-17-1992-18:26-wht@n4hgf-optimize parameter 1 to select() *//*:12-12-1991-05:14-wht@n4hgf-lgetc_timeout can now return a null character *//*:11-26-1991-19:47-wht@n4hgf-add ldcdwatch_str *//*:11-11-1991-22:28-wht@n4hgf-ldcdwatch and lCLOCAL code *//*:11-11-1991-14:38-wht@n4hgf-lzero_length_read_detected code *//*:09-01-1991-14:18-wht@n4hgf2-on sun, use termios and improve ldraino *//*:09-01-1991-02:51-wht@n4hgf2-sun CRTSCTS turn on bug fixed *//*:08-25-1991-14:39-wht@n4hgf-SVR4 port thanks to aega84!lh *//*:08-11-1991-18:06-wht@n4hgf-SCO_TTY_NAMING considerations *//*:08-06-1991-14:18-wht@n4hgf-baud rates below 300 get two stop bits *//*:07-29-1991-01:55-wht@n4hgf-remove unused externs *//*:07-25-1991-12:56-wht@n4hgf-ECU release 3.10 *//*:07-17-1991-07:04-wht@n4hgf-avoid SCO UNIX nap bug *//*:04-09-1991-16:11-wht@n4hgf-use B0 in lflash_DTR *//*:02-07-1991-01:00-wht@n4hgf-fix code in for lclose retry on remote XOFF *//*:01-29-1991-14:54-wht@n4hgf-put code in for lclose retry on remote XOFF *//*:01-25-1991-05:57-wht@n4hgf-cringe - lflush was flushing console not line *//*:01-09-1991-22:31-wht@n4hgf-ISC port *//*:01-09-1991-21:26-wht@n4hgf-don't prototype nap() (ISC port) *//*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */#define DECLARE_LINEVARS_PUBLIC#include "ecu.h"#include "ecukey.h"#include "termecu.h"#include <setjmp.h>#include <pwd.h>extern int errno;#if defined(SVR4)# include <sys/termios.h># include <sys/termiox.h>int hx_flag;#endifvoid lzero_length_read_detected();void lCLOCAL();int lgetc_count;char lopen_err_str[64] = "";int zero_length_read_detected = 0;int dcdwatch_set = 0;	/* if true, ldcdwatch() has been called */static jmp_buf _insurance_jmpbuf;/* * with SCO UNIX, nap doesn't work as advertized; param MUST be > granularity * or nap will return immediately; not a problem with XENIX */#define LPUTS_NAP_COUNT	(min((hzmsec * 2),20L))/*+-------------------------------------------------------------------------	process_xmtr_rcvd_char(rchar,echo) - feed xmtr-rcvd char to rcvr codeecho: 0 no echo      1 echo literally      2 "make printable"--------------------------------------------------------------------------*/voidprocess_xmtr_rcvd_char(rchar,echo)uint rchar;register int echo;{	if(process_rcvd_char(rchar))		return;	if(echo == 1)	{		if(rchar == NL)			fputc(CRET,se);		fputc(rchar,se);		if(rchar != CRET)			plogc(rchar);	}	else if(echo == 2)	{	char *make_char_graphic();		pputs(make_char_graphic(rchar,0));		if(rchar == 0x0A)			pputs("\n");	}}	/* end of process_xmtr_rcvd_char *//*+-------------------------------------------------------------------------	lgetc_xmtr() -- xmtr version of get char from linealso called by rcvr code when lgetc_buf empty and vmin == 1zero_length_read_detected is a public that will set if theDCD watcher is turned on and DCD is lost--------------------------------------------------------------------------*/ucharlgetc_xmtr(){	int itmp;	uchar char_rtnd;READ_AGAIN:	if((itmp = read(shm->Liofd,(char *)&char_rtnd,1)) < 1)	{		if(!itmp)		{			if(shm->Ldcdwatch)			{				lzero_length_read_detected();				return(0);			}			errno = EIO;	/* for termecu processing */			termecu(TERMECU_LINE_READ_ERROR);		}		if(errno == EINTR)			/* if signal interrupted, ... */		{			if(sigint)				return(0);			goto READ_AGAIN;		}		termecu(TERMECU_LINE_READ_ERROR);	}	shm->rcvd_chars++;	shm->rcvd_chars_this_connect++;	if(shm->Lparity)		char_rtnd &= 0x7F;	return(char_rtnd);}	/* end of lgetc_xmtr *//*+-------------------------------------------------------------------------	lrdchk_xmtr() -- rdchk(shm->Liofd) for xmtr--------------------------------------------------------------------------*/intlrdchk_xmtr(){	return(rdchk(shm->Liofd));}	/* end of lrdchk_xmtr *//*+-------------------------------------------------------------------------	char *lgets_timeout(LRWT *) - may be called by xmtr onlyto1 and to2 are unsigned long values in milliseconds (notcurrently supported well under BSD4); to1 is the time to waitfor the first character, to2 the time to wait for subsequentcharacters.if raw_flag 0,     non-printables are stripped from beginning                   and end of received characters (i.e., modem                   response reads); NULs discarded, parity strippedif raw_flag 1,     full raw read buffer returned0x80 in raw_flag indicates console interrupts should be enabled.if interrupt thus detected, the procedure returns "!Interrupted"without reseting variable 'interrupt'buffer is address to read chars intobufsize is buffer max size (allowing room for terminating null)which should be at least 2 if raw_size includes 0x80 bit,else at least 12 characters if 0x80 omitted.count is a int which, at return, receives the actual count readzero_length_read_detected is a public that will set if theDCD watcher is turned on and DCD is lost--------------------------------------------------------------------------*/char *lgets_timeout(lrwt)LRWT *lrwt;{/**************************/#if !defined(WORKING_SELECT)/**************************/	register actual_count = 0;	register char *cptr = lrwt->buffer;	register echo_flag = lrwt->echo_flag;	int max_count = lrwt->bufsize;	char *rtn_val;	int timeout_counter;	int qc1;	int qc2;	int raw_mode = lrwt->raw_flag & 0x0F;	int check_sigint = (lrwt->raw_flag & 0x80);	int old_ttymode = get_ttymode();	/* save original tty mode */	int delim_len;	long quantum;	long ltmp;	delim_len = (lrwt->delim) ? strlen(lrwt->delim) : 0;	if((shm->Lbaud < 300) && lrwt->to2)		if(lrwt->to2 < 300L) lrwt->to2 = 300L;	else if((shm->Lbaud < 1200) && lrwt->to2)		if(lrwt->to2 < 200L) lrwt->to2 = 100L;/* shortest interval */	ltmp = (lrwt->to1 < lrwt->to2) ? lrwt->to1 : lrwt->to2;/* calculate wait quantum */	quantum = ltmp / 10L;				/* try for ten ticks */#if defined(M_I386)	if(quantum < 40L)		quantum = 40L;#else	if(quantum < 20L)		quantum = 20L;#endif	qc1 = lrwt->to1 / quantum;	if(!qc1) qc1 = 1L;	qc2 = lrwt->to2 / quantum;	if(!qc2) qc2 = 1L;/* perform the lrtw function using nap() and rdchk()   input: qc1 is first nap count (for first charcters)           qc2 is 2nd nap count (for subsequent characters)           quantum is the nap period in milliseconds          cptr is char* to receive read string          max_count is max number of characters incl null          lrwt->raw_flag as described above  output: lrwt->count is actual count of return result          lrwt->buffer is return read buffer*/	max_count--;				/* leave room for null */	if(check_sigint)		ttymode(2);				/* let console interrupt long timeouts */	timeout_counter = qc1;		/* first timeout */ 	*cptr = 0;					/* init result string */	while(timeout_counter--)	{		Nap(quantum);		if(check_sigint && sigint)			goto INTERRUPTED;		while(lrdchk_xmtr())		{			zero_length_read_detected = 0;			*cptr = lgetc_xmtr();			if(zero_length_read_detected)				goto BOTTOM;			if(check_sigint && sigint)				goto INTERRUPTED;			if(*cptr == 0)				continue;			process_xmtr_rcvd_char(*cptr,echo_flag);			if(!raw_mode && (*cptr == CRET))					continue;			*++cptr = 0;			if(++actual_count == 1)			{				if(!lrwt->to2)					break;				timeout_counter = qc2;			}			if(--max_count == 0)				goto BOTTOM;			if(delim_len && (actual_count >= delim_len) &&					!strncmp(lrwt->delim,cptr - delim_len,delim_len))				goto BOTTOM;		}	}/********************************/#else /* do have WORKING_SELECT *//********************************//* --- use select --- */	register actual_count = 0;	register char *cptr = lrwt->buffer;	register max_count = lrwt->bufsize;	register raw_mode = lrwt->raw_flag & 0x0F;	register echo_flag = lrwt->echo_flag;	int check_sigint = (lrwt->raw_flag & 0x80);	int old_ttymode = get_ttymode();	/* save original tty mode */	int fdmask;	int delim_len;	struct timeval tval;	char *rtn_val;	delim_len = (lrwt->delim) ? strlen(lrwt->delim) : 0;	if((shm->Lbaud < 300) && lrwt->to2)		if(lrwt->to2 < 300L) lrwt->to2 = 300L;	else if((shm->Lbaud < 1200) && lrwt->to2)		if(lrwt->to2 < 200L) lrwt->to2 = 100L;/* perform the lrtw function  output: lrwt->count is actual count of return result          lrwt->buffer is return read buffer*/	max_count--;				/* leave room for null */	if(check_sigint)		ttymode(2);				/* let console interrupt long timeouts */	*cptr = 0;					/* init result string */	while(1)	{		if(check_sigint && sigint)			goto INTERRUPTED;		errno = 0;		fdmask = 1 << shm->Liofd; /* Liofd will always be <= 31, right? */		if(actual_count)		{			tval.tv_sec = lrwt->to2 / 1000L;			tval.tv_usec = (lrwt->to2 % 1000L) * 1000L;		}		else		{			tval.tv_sec = lrwt->to1 / 1000L;			tval.tv_usec = (lrwt->to1 % 1000L) * 1000L;		}		if(select(shm->Liofd + 1,&fdmask,NULL,NULL,&tval) != 1)		{			if(errno == EINTR)				continue;			break;		}		while(rdchk(shm->Liofd))		{			zero_length_read_detected = 0;			*cptr = lgetc_xmtr();			if(zero_length_read_detected)				goto BOTTOM;			if(check_sigint && sigint)				goto INTERRUPTED;			if(*cptr == 0)				continue;			process_xmtr_rcvd_char(*cptr,!!echo_flag);			if(!raw_mode && (*cptr == CRET))					continue;			*++cptr = 0;			actual_count++;			if(--max_count == 0)				goto BOTTOM;			if(delim_len && (actual_count >= delim_len) &&					!strncmp(lrwt->delim,cptr - delim_len,delim_len))				goto BOTTOM;		}		if(!lrwt->to2)			break;	}#endif	/* WORKING_SELECT *//********* common post processing for select() / no select() ************/BOTTOM:	if(check_sigint)		ttymode(old_ttymode);	if(raw_mode)	{		lrwt->count = actual_count;		return(lrwt->buffer);	}	cptr = lrwt->buffer;	while(((*cptr > 0) && (*cptr < SPACE)) || (*cptr >= DEL))		cptr++;	rtn_val = cptr;	actual_count = 0;	while(((*cptr &= 0x7F) >= SPACE) && (*cptr < DEL))	{		cptr++;		actual_count++;	}	*cptr = 0;	strcpy(lrwt->buffer,rtn_val);	lrwt->count = actual_count;	return(lrwt->buffer);INTERRUPTED:	ttymode(old_ttymode);	strcpy(lrwt->buffer,"!Interrupted");	lrwt->count = strlen(lrwt->buffer);	return((char *)0);}	/* end of lgets_timeout *//*+-------------------------------------------------------------------------	lgetc_timeout(msec) - may be called by xmtr only reads one character from line unless msec passes with no receipt. return char if received, else -1 if timeout--------------------------------------------------------------------------*/intlgetc_timeout(msec)long msec;{	uchar rtn_char;#if !defined(WORKING_SELECT)	long timeout;	timeout = msec;	while(!lrdchk_xmtr())	{		if(sigint)			return(-1);		if((timeout -= Nap(hzmsec)) <= 0)			return(-1);	}#else	int fdmask;	struct timeval tval;	tval.tv_sec = msec / 1000L;	tval.tv_usec = (msec % 1000L) * 1000L;	fdmask = 1 << shm->Liofd; /* Liofd will always be <= 31, right? */	if(select(shm->Liofd + 1,&fdmask,NULL,NULL,&tval) < 1)		return(-1);	if(!lrdchk_xmtr())		return(-1);	if(sigint)		return(-1);#endif	rtn_char = lgetc_xmtr();	return(rtn_char);}	/* end of lgetc_timeout *//*+-------------------------------------------------------------------------	llookfor(lookfor,msecs,echo_flag)return 1 if successful, else 0 if no matchecho_flag: 0 no echo           1 echo literally           2 "make printable"--------------------------------------------------------------------------*/intllookfor(lookfor,msecs,echo_flag)char *lookfor;ulong msecs;int echo_flag;{	register lookfor_len = strlen(lookfor);

⌨️ 快捷键说明

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