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

📄 hdbintf.c

📁 一个通讯程序源码
💻 C
📖 第 1 页 / 共 3 页
字号:
	if(!already && !tty_not_char_special)	{		pputs("\7\n");		pputs(s512);		pputs("\nFurther Devices errors will not be displayed,\n");		pputs("but are logged in ~/.ecu/log.  Press any key to continue.\7");		ttyflush(0);		ttygetc(1);		pputs("\n");	}	already = 1;	memcpy(s512,"MALFORMED",9);		/* mod for log file */	*nlptr = ' ';	ecu_log_event(xmtr_pid,s512);}	/* end of malformed_Devices_entry *//*+-------------------------------------------------------------------------	getdvent() - get first or next Devices entry (a la getpwent)--------------------------------------------------------------------------*/DVE *getdvent(){	int itmp;#define MAX_DV_TOKENS 9	char *tokens[MAX_DV_TOKENS];	int ntokens;#if 0	int itokens;#endif	char *cptr;	static DVE dve;	static char dvstr[256];	char *strchr();	char *skip_ld_break();	if(!there_is_hdb_on_this_machine)		goto RETURN_NULL;	if(!fpdv)	{		if(!(fpdv = fopen(Devices_file,"r")))		{			pperror(Devices_file);			goto RETURN_NULL;		}	}	while(1)	{		/*		 * read a Devices line		 */		if(!fgets(dvstr,sizeof(dvstr),fpdv))		{RETURN_NULL:#ifdef CHOOSE_DEBUG			ecu_log_event(xmtr_pid,"getdvent returning NULL");#endif			return((DVE *)0);		}		/*		 * weed out comments and blank lines		 */		if(strlen(dvstr) <= 1)					/* blank line */			continue;		cptr = skip_ld_break(dvstr);			/* first non-blank */		if(!*cptr || strchr("#\n",*cptr))		/* comment or all white space */			continue;		/*		 * tokenize		 */		build_arg_array(dvstr,tokens,MAX_DV_TOKENS,&ntokens);		/*		 * honor '#' whever it occurs		 */#if 0		for(itokens = 0; itokens < ntokens; itokens++)		{			if(!tokens[itokens])				tokens[itokens] = "";			if(cptr = strchr(tokens[itokens],'#'))			{				*cptr = 0;				ntokens = itokens + 1;				break;			}		}#endif		/*		 * a bit of validation		 */		if(ntokens < 4)		{			malformed_Devices_entry("too few fields",ntokens,tokens);			continue;		}		/*		 * TCP,et - - Any TCP - 		 * found in Sun Devices (UUCP over TCP)		 */#if 0	/* leave this generic; handle in getdvbaud and getdvtype */		if(!strcmp(tokens[1],"-"))		{			malformed_Devices_entry("bad tty specified",ntokens,tokens);			continue;		}#endif		break;	}	/*	 * we have a good entry	 */	dve.type = tokens[0];	dve.line = tokens[1];	/*	 * get rid of possible SVR4 ",M" modem control suffix	 */	itmp = strlen(dve.line);	if((itmp > 2) && !strcmp(dve.line + itmp - 2,",M"))	  dve.line[itmp - 2] = 0;	dve.dialer = tokens[2];	if(!strcmpi(tokens[3],"Any"))	{		dve.low_baud = 1;		dve.high_baud = 65535;	}	else	{		dve.low_baud = atoi(tokens[3]);		if(!(cptr = strchr(tokens[3],'-')))			dve.high_baud = dve.low_baud;		else			dve.high_baud = atoi(cptr + 1);	}	dve.dialprog = tokens[4];	dve.token = tokens[5];#ifdef CHOOSE_DEBUG	{		char s256[256];		sprintf(s256,"getdvent returning '%s' type='%s'",dve.line,dve.type);		ecu_log_event(xmtr_pid,s256);	}#endif	return(&dve);}	/* end of getdvent *//*+-------------------------------------------------------------------------	enddvent() - close Devices file--------------------------------------------------------------------------*/voidenddvent(){	if(fpdv)	{		fclose(fpdv);		fpdv = (FILE *)0;	}}	/* end of enddvent *//*+-------------------------------------------------------------------------	getdvbaud(baud) - get Devices entry matching baud rate--------------------------------------------------------------------------*/DVE *getdvbaud(baud)uint baud;{	DVE *tdve;#ifdef CHOOSE_DEBUG	char s128[128];	sprintf(s128,"getdvbaud looking for %u baud",baud);	ecu_log_event(getpid(),s128);#endif	while(1)	{		if((tdve = getdvent()) == (DVE *)0)			return(tdve);#ifdef CHOOSE_DEBUG		sprintf(s128,"getdvbaud found %s type '%s' baud lo=%d hi=%d",			tdve->line,tdve->type,tdve->low_baud,tdve->high_baud);				ecu_log_event(getpid(),s128);#endif		if(!strcmp(tdve->line,"-"))	/* neo-entries like TCP have "-" line */			continue;		if((tdve->low_baud <= baud) && (baud <= tdve->high_baud))		{#ifdef CHOOSE_DEBUG			sprintf(s128,"getdvbaud returning %s",tdve->line);			ecu_log_event(getpid(),s128);#endif			return(tdve);		}	}	/*NOTREACHED*/}	/* end of getdvbaud *//*+-------------------------------------------------------------------------	getdvline(line) - get Devices entry matching linecalling argument 'line's is string AFTER "/dev/"--------------------------------------------------------------------------*/DVE *getdvline(line)char *line;{	DVE *tdve;#ifdef CHOOSE_DEBUG	char s128[128];	sprintf(s128,"getdvline looking for %s",line);	ecu_log_event(getpid(),s128);#endif	while(1)	{		if((tdve = getdvent()) == (DVE *)0)			return(tdve);#ifdef CHOOSE_DEBUG		sprintf(s128,"getdvline %s found baud lo=%d hi=%d",tdve->line,			tdve->low_baud, tdve->high_baud);		ecu_log_event(getpid(),s128);#endif		if(!TTYNAME_STRCMP(tdve->line,line))			return(tdve);	}	/*NOTREACHED*/}	/* end of getdvline *//*+-------------------------------------------------------------------------	dvtype_match(typespec,dvtype) - match between pde typespec and dvtypereturns 1 if pde type specification 'typespec' matches Devices device 'type'--------------------------------------------------------------------------*/intdvtype_match(typespec,dvtype)char *typespec;char *dvtype;{	char *emsg;	char *match;	int matchlen;	int re_match = 1;	char cmpbuf[128];	if(*typespec == '=')		typespec++;	else if(*typespec == '/')	{		re_match = 0;		typespec++;	}	if(re_match)	{		if(!strcmp(dvtype,typespec))			return(1);	}	else	{		if(regexp_compile(typespec,cmpbuf,sizeof(cmpbuf),&emsg))			return(0);		if(regexp_scan(cmpbuf,dvtype,&match,&matchlen))			return(1);	}	return(0);}	/* end of dvtype_match *//*+-------------------------------------------------------------------------	getdvtype(type) - get Devices entry matching typetype is either 'Device_type'   search for exact match on Device_type               '=Device_type'  search for exact match on Device_type               '/regexp'       search for match with regular expressionyou must make sure any supplied regexp is a valid one, for regexpcompilation errors are indistinguishable from other seach failuresuses optimized implementation of dvtype_match functionality--------------------------------------------------------------------------*/DVE *getdvtype(type)char *type;{	DVE *tdve;	char *emsg;	char *match;	int matchlen;	int re_match = 0;	/* regular expression match */	char cmpbuf[128];	if(*type == '=')		type++;	else if(*type == '/')	{		re_match = 1;		type++;		if(regexp_compile(type,cmpbuf,sizeof(cmpbuf),&emsg))			return((DVE *)0);	}	while(tdve = getdvent())	{		if(!strcmp(tdve->line,"-"))	/* neo-entries like TCP have "-" line */			continue;		if(re_match)		{			if(regexp_scan(cmpbuf,tdve->type,&match,&matchlen))				break;		}		else		{			if(!strcmp(tdve->type,type))				break;		}	}	return(tdve);}	/* end of getdvtype *//*+-------------------------------------------------------------------------	dialstr_translate(translate_list,to_translate) - translate dial strings--------------------------------------------------------------------------*/#if 0voiddialstr_translate(translate_list,to_translate)register char *translate_list;char *to_translate;{	register char *cptr;	while(*translate_list && *(translate_list + 1))	{		for(cptr=to_translate; *cptr; cptr++)		{			if(*translate_list == *cptr)				*cptr = *(translate_list + 1);		}		translate_list += 2;	}}	/* end of dialstr_translate */#endif/*+-------------------------------------------------------------------------	getdlent() - get first or next Dialers entry (a la getpwent)--------------------------------------------------------------------------*/struct dlent *getdlent(){	int itmp;#define MAX_DL_TOKENS 3	char *tokens[MAX_DL_TOKENS];	static struct dlent dle;	static char dlstr[128];	char *strchr();	if(!there_is_hdb_on_this_machine)		return((struct dlent *)0);	if(!fpdl)	{		if(!(fpdl = fopen(Dialers_file,"r")))		{			pperror(Dialers_file);			return((struct dlent *)0);		}	}	while(1)	{		if(!fgets(dlstr,sizeof(dlstr),fpdl))			return((struct dlent *)0);		if(((itmp = strlen(dlstr)) <= 1) || (dlstr[0] == '#') ||			(dlstr[0] == ' '))		{			continue;		}		dlstr[--itmp] = 0;		for(itmp = 0; itmp < MAX_DL_TOKENS; itmp++)			tokens[itmp] = "";		if(tokens[0] = arg_token(dlstr," \t\r\n"))		{			if(tokens[1] = arg_token((char *)0," \t\r\n"))			{			extern char *str_token_static;				tokens[2] = skip_ld_break(str_token_static);			}		}		break;	}	dle.name = tokens[0];	dle.tlate = tokens[1];	dle.script = tokens[2];	return(&dle);}	/* end of getdlent *//*+-------------------------------------------------------------------------	enddlent() - close Dialers file--------------------------------------------------------------------------*/voidenddlent(){	if(fpdl)	{		fclose(fpdl);		fpdl = (FILE *)0;	}}	/* end of enddlent *//*+-------------------------------------------------------------------------	getdlentname(name) - get Dialers entry by name--------------------------------------------------------------------------*/struct dlent *getdlentname(name)char *name;{	register DLE *tdle;	while(tdle = getdlent())	{		if(!strcmp(name,tdle->name))			break;	}	return(tdle);}	/* end of getdlentname *//*+-------------------------------------------------------------------------	hdb_choose_Any(baud) - user will take 'Any' linegive preference to current line--------------------------------------------------------------------------*/DVE *hdb_choose_Any(baud)uint baud;{	DVE *tdve = (DVE *)0;	char newtty[sizeof(shm->Lline)];	int lerr = 0;	int utmpst = 0;#if defined(LOG_HDBDIAL) || defined(CHOOSE_DEBUG)	char s128[128];#endif#ifdef CHOOSE_DEBUG	sprintf(s128,"hdb_choose_Any baud=%u current line='%s'",baud,shm->Lline);	ecu_log_event(getpid(),s128);#endif	enddvent();	/* krock but safe *//* * see if shm->Lline in use by someone else; if not and baud rate ok, no further */	if(shm->Lline[0])	{		while(tdve = getdvline(shm->Lline + 5))		{			if((tdve->low_baud <= baud) && (baud <= tdve->high_baud))				break;		}		if(tdve)		{			switch(utmpst = utmp_status(shm->Lline))			{			case US_WEGOTIT:				goto RETURN;			case US_NOTFOUND:	/* not in utmp, or getty dead */#if 0				if(access(shm->Lline,6))					break;#endif				if((lerr = line_lock_status(shm->Lline)) &&					(lerr != LINST_WEGOTIT))				{					break;				}				goto RETURN;			case US_LOGIN:		/* enabled for login, idle */				goto RETURN;			}		}		enddvent();	}	/*	 * we've got to pick a new line	 */#ifdef CHOOSE_DEBUG	sprintf(s128,"must pick new line utmpst=%u lerr=%u",utmpst,lerr);	ecu_log_event(getpid(),s128);#endif	lerr = 0;	while(1)	{		if(!(tdve = getdvbaud(baud)))			break;		/* by now, we know shm->Lline wont work */		if(!TTYNAME_STRCMP(tdve->line,shm->Lline + 5))			continue;		/* if not acu, dont use it */		if(ulindex(tdve->type,"ACU") < 0)			continue;		sprintf(newtty,"/dev/%s",tdve->line);		switch(utmpst = utmp_status(newtty))		{			case US_NOTFOUND:	/* not in utmp, or getty dead */#if 0				if(access(newtty,6)) /* ecuungetty won't be able to help us */					break;#endif				if((lerr = line_lock_status(newtty)) &&					(lerr != LINST_WEGOTIT))				{					break;				}				goto RETURN;			case US_LOGIN:		/* enabled for login, idle */				goto RETURN;			case US_WEGOTIT:				goto RETURN;		}	}RETURN:	enddvent();#ifdef LOG_HDBDIAL	sprintf(s128,"CHOOSEANY lerr=%d chose %s",		lerr,(tdve) ? tdve->line : "<none>");	ecu_log_event(getpid(),s128);#endif	return(tdve);}	/* end of hdb_choose_Any *//*+-------------------------------------------------------------------------	hdb_choose_Device(type,baud) - need line with 'type' and 'baud'return DVE pointer if line chosen, 0 if failed to find a linePriority is given to retaining the current line--------------------------------------------------------------------------*/DVE *hdb_choose_Device(type,baud)char *type;uint baud;{	DVE *tdve = (DVE *)0;	char s32[32];	int itmp = 0;	int lerr = 0;	int utmpst = 0;	int done;#ifdef CHOOSE_DEBUG	char s128[128];	sprintf(s128,"hdb_choose_Device type='%s' baud=%u curr line='%s'",	    type,baud,shm->Lline);	ecu_log_event(getpid(),s128);#endif	/*	 * check current line	 */	if(shm->Lline[0])	{		while(tdve = getdvline(shm->Lline + 5))		{			if(dvtype_match(type,tdve->type) &&				(tdve->low_baud <= baud) && (baud <= tdve->high_baud))			{				break;			}		}		enddvent();

⌨️ 快捷键说明

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