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

📄 lirc_client.c

📁 很少见的linux下的红外口的工具
💻 C
📖 第 1 页 / 共 2 页
字号:
			token=lirc_trim(string);			token2=lirc_trim(eq+1);			if(token[0]=='#')			{				/* ignore comment */			}			else if(new_entry==NULL)			{				lirc_printf("%s: bad file format, "					    "line %d\n",lirc_prog,line);				ret=-1;			}			else			{				token2=strdup(token2);				if(token2==NULL)				{					lirc_printf("%s: out of memory\n",						    lirc_prog);					ret=-1;				}				else if(strcasecmp(token,"prog")==0)				{					if(new_entry->prog!=NULL) free(new_entry->prog);					new_entry->prog=token2;				}				else if(strcasecmp(token,"remote")==0)				{					if(remote!=LIRC_ALL)						free(remote);										if(strcasecmp("*",token2)==0)					{						remote=LIRC_ALL;						free(token2);					}					else					{						remote=token2;					}				}				else if(strcasecmp(token,"button")==0)				{					struct lirc_code *code;										code=(struct lirc_code *)					malloc(sizeof(struct lirc_code));					if(code==NULL)					{						free(token2);						lirc_printf("%s: out of "							    "memory\n",							    lirc_prog);						ret=-1;					}					else					{						code->remote=remote;						if(strcasecmp("*",token2)==0)						{							code->button=LIRC_ALL;							free(token2);						}						else						{							code->button=token2;						}						code->next=NULL;						if(new_entry->code==NULL)						{							new_entry->code=code;						}						else						{							new_entry->next_code->next							=code;						}						new_entry->next_code=code;						if(remote!=LIRC_ALL)						{							remote=strdup(remote);							if(remote==NULL)							{								lirc_printf("%s: out of memory\n",lirc_prog);								ret=-1;							}						}					}				}				else if(strcasecmp(token,"repeat")==0)				{					char *end;					errno=ERANGE+1;					new_entry->rep=strtoul(token2,&end,0);					if((new_entry->rep==ULONG_MAX 					    && errno==ERANGE)					   || end[0]!=0					   || strlen(token2)==0)					{						lirc_printf("%s: \"%s\" not"							    " a  valid number for "							    "repeat\n",lirc_prog,							    token2);					}					free(token2);				}				else if(strcasecmp(token,"config")==0)				{					struct lirc_list *new_list;					new_list=(struct lirc_list *) 					malloc(sizeof(struct lirc_list));					if(new_list==NULL)					{						free(token2);						lirc_printf("%s: out of "							    "memory\n",							    lirc_prog);						ret=-1;					}					else					{						lirc_parse_string(token2,line);						new_list->string=token2;						new_list->next=NULL;						if(new_entry->config==NULL)						{							new_entry->config=new_list;						}						else						{							new_entry->next_config->next							=new_list;						}						new_entry->next_config=new_list;					}				}				else if(strcasecmp(token,"mode")==0)				{					if(new_entry->change_mode!=NULL) free(new_entry->change_mode);					new_entry->change_mode=token2;				}				else if(strcasecmp(token,"flags")==0)				{					new_entry->flags=lirc_flags(token2);					free(token2);				}				else				{					free(token2);					lirc_printf("%s: unknown token "						    "\"%s\" in line %d ignored\n",						    lirc_prog,token,line);				}			}		}		free(string);		line++;		if(ret==-1) break;	}	if(remote!=LIRC_ALL)		free(remote);	if(new_entry!=NULL)	{		if(ret==0)		{			ret=lirc_mode("end",NULL,&mode,&new_entry,				      &first,&last,check,line);			lirc_printf("%s: warning: end token missing at end "				    "of file\n",lirc_prog);		}		else		{			lirc_freeconfigentries(new_entry);			new_entry=NULL;		}	}	if(mode!=NULL)	{		if(ret==0)		{			lirc_printf("%s: warning: no end token found for mode "				    "\"%s\"\n",lirc_prog,mode);		}		free(mode);	}	fclose(fin);	if(ret==0)	{		*config=(struct lirc_config *)			malloc(sizeof(struct lirc_config));		if(*config==NULL)		{			lirc_freeconfigentries(first);			return(-1);		}		(*config)->first=first;		(*config)->next=first;		(*config)->current_mode=lirc_startupmode((*config)->first);	}	else	{		*config=NULL;		lirc_freeconfigentries(first);	}	return(ret);}char *lirc_startupmode(struct lirc_config_entry *first){	struct lirc_config_entry *scan;	char *startupmode;	startupmode=NULL;	scan=first;	/* Set a startup mode based on flags=startup_mode */	while(scan!=NULL)	{		if(scan->flags&startup_mode) {			if(scan->change_mode!=NULL) {				startupmode=scan->change_mode;				/* Remove the startup mode or it confuses lirc mode system */				scan->change_mode=NULL;				break;			}			else {				lirc_printf("%s: startup_mode flags requires 'mode ='\n",					    lirc_prog);			}		}		scan=scan->next;	}	/* Set a default mode if we find a mode = client app name */	if(startupmode==NULL) {		scan=first;		while(scan!=NULL)		{			if(scan->mode!=NULL &&strcasecmp(lirc_prog,scan->mode)==0)			{				startupmode=lirc_prog;				break;			}			scan=scan->next;		}	}	if(startupmode==NULL) return(NULL);	scan=first;	while(scan!=NULL)	{		if(scan->change_mode!=NULL && scan->flags&once &&		   strcasecmp(startupmode,scan->change_mode)==0)		{			scan->flags|=ecno;		}		scan=scan->next;	}	return(startupmode);}void lirc_freeconfig(struct lirc_config *config){	if(config!=NULL)	{		lirc_freeconfigentries(config->first);		free(config);	}}void lirc_freeconfigentries(struct lirc_config_entry *first){	struct lirc_config_entry *c,*config_temp;	struct lirc_list *list,*list_temp;	struct lirc_code *code,*code_temp;	c=first;	while(c!=NULL)	{		if(c->prog) free(c->prog);		if(c->change_mode) free(c->change_mode);		if(c->mode) free(c->mode);		code=c->code;		while(code!=NULL)		{			if(code->remote!=NULL && code->remote!=LIRC_ALL)				free(code->remote);			if(code->button!=NULL && code->button!=LIRC_ALL)				free(code->button);			code_temp=code->next;			free(code);			code=code_temp;		}		list=c->config;		while(list!=NULL)		{			if(list->string) free(list->string);			list_temp=list->next;			free(list);			list=list_temp;		}		config_temp=c->next;		free(c);		c=config_temp;	}}void lirc_clearmode(struct lirc_config *config){	struct lirc_config_entry *scan;	if(config->current_mode==NULL)	{		return;	}	scan=config->first;	while(scan!=NULL)	{		if(scan->change_mode!=NULL)		{			if(strcasecmp(scan->change_mode,config->current_mode)==0)			{				scan->flags&=~ecno;			}		}		scan=scan->next;	}	config->current_mode=NULL;}char *lirc_execute(struct lirc_config *config,struct lirc_config_entry *scan){	char *s;	int do_once=1;		if(scan->flags&quit)	{		config->next=NULL;	}	else	{		config->next=scan->next;	}	if(scan->flags&mode)	{		lirc_clearmode(config);	}	if(scan->change_mode!=NULL)	{		config->current_mode		=scan->change_mode;		if(scan->flags&once)		{			if(scan->flags&ecno)			{				do_once=0;			}			else			{				scan->flags|=ecno;			}		}	}	if(scan->next_config!=NULL &&	   scan->prog!=NULL &&	   strcasecmp(scan->prog,lirc_prog)==0 &&	   do_once==1)	{		s=scan->next_config->string;		scan->next_config		=scan->next_config->next;		if(scan->next_config==NULL)			scan->next_config			=scan->config;		return(s);	}	return(NULL);}int lirc_iscode(struct lirc_config_entry *scan,char *remote,char *button,int rep){	struct lirc_code *codes;		if(scan->code==NULL)		return(1);	if(scan->next_code->remote==LIRC_ALL || 	   strcasecmp(scan->next_code->remote,remote)==0)	{		if(scan->next_code->button==LIRC_ALL || 		   strcasecmp(scan->next_code->button,button)==0)		{			if(scan->code->next==NULL || rep==0)			{				scan->next_code=scan->next_code->next;			}			if(scan->next_code==NULL)			{				scan->next_code=scan->code;                                if(scan->code->next!=NULL ||                                    (scan->rep==0 ? rep==0:(rep%scan->rep)==0))                                {                                        return(1);                                }                                else                                {                                        return(0);                                }                        }			else			{				return(0);			}		}	}        if(rep!=0) return(0);	codes=scan->code;        if(codes==scan->next_code) return(0);	codes=codes->next;	while(codes!=scan->next_code->next)	{                struct lirc_code *prev,*next;                int flag=1;                prev=scan->code;                next=codes;                while(next!=scan->next_code)                {                        if(prev->remote==LIRC_ALL ||                           strcasecmp(prev->remote,next->remote)==0)                        {                                if(prev->button==LIRC_ALL ||                                   strcasecmp(prev->button,next->button)==0)                                {                                        prev=prev->next;                                        next=next->next;                                }                                else                                {                                        flag=0;break;                                }                        }                        else                        {                                flag=0;break;                        }                }                if(flag==1)                {                        if(prev->remote==LIRC_ALL ||                           strcasecmp(prev->remote,remote)==0)                        {                                if(prev->button==LIRC_ALL ||                                   strcasecmp(prev->button,button)==0)                                {                                        if(rep==0)                                        {                                                scan->next_code=prev->next;                                                return(0);                                        }                                }                        }                }                codes=codes->next;	}	scan->next_code=scan->code;	return(0);}char *lirc_ir2char(struct lirc_config *config,char *code){	static int warning=1;	char *string;		if(warning)	{		fprintf(stderr,"%s: warning: lirc_ir2char() is obsolete\n",			lirc_prog);		warning=0;	}	if(lirc_code2char(config,code,&string)==-1) return(NULL);	return(string);}int lirc_code2char(struct lirc_config *config,char *code,char **string){	int rep;	char *backup;	char *remote,*button;	struct lirc_config_entry *scan;	*string=NULL;	if(sscanf(code,"%*llx %x %*s %*s\n",&rep)==1)	{		backup=strdup(code);		if(backup==NULL) return(-1);		strtok(backup," ");		strtok(NULL," ");		button=strtok(NULL," ");		remote=strtok(NULL,"\n");		if(button==NULL || remote==NULL)		{			free(backup);			return(0);		}				scan=config->next;		while(scan!=NULL)		{			if(lirc_iscode(scan,remote,button,rep) &&			   (scan->mode==NULL ||			    (scan->mode!=NULL && 			     config->current_mode!=NULL &&			     strcasecmp(scan->mode,config->current_mode)==0))			   )			{				char *s;				s=lirc_execute(config,scan);				if(s!=NULL)				{					free(backup);					*string=s;					return(0);				}			}			if(config->next!=NULL)			{				scan=scan->next;			}			else			{				scan=NULL;			}		}		free(backup);	}	config->next=config->first;	return(0);}#define PACKET_SIZE 100char *lirc_nextir(void){	static int warning=1;	char *code;	int ret;		if(warning)	{		fprintf(stderr,"%s: warning: lirc_nextir() is obsolete\n",			lirc_prog);		warning=0;	}	ret=lirc_nextcode(&code);	if(ret==-1) return(NULL);	return(code);}int lirc_nextcode(char **code){	static int packet_size=PACKET_SIZE;	static int end_len=0;	ssize_t len=0;	char *end,c;	*code=NULL;	if(lirc_buffer==NULL)	{		lirc_buffer=(char *) malloc(packet_size+1);		if(lirc_buffer==NULL)		{			return(-1);		}		lirc_buffer[0]=0;	}	while((end=strchr(lirc_buffer,'\n'))==NULL)	{		if(end_len>=packet_size)		{			char *new_buffer;			packet_size+=PACKET_SIZE;			new_buffer=(char *) realloc(lirc_buffer,packet_size);			if(new_buffer==NULL)			{				return(-1);			}			lirc_buffer=new_buffer;		}		len=read(lirc_lircd,lirc_buffer+end_len,packet_size-end_len);		if(len<=0)		{			if(len==-1 && errno==EAGAIN) return(0);			else return(-1);		}		end_len+=len;		lirc_buffer[end_len]=0;		/* return if next code not yet available completely */		if((end=strchr(lirc_buffer,'\n'))==NULL)		{			return(0);		}	}	/* copy first line to buffer (code) and move remaining chars to	   lirc_buffers start */	end++;	end_len=strlen(end);	c=end[0];	end[0]=0;	*code=strdup(lirc_buffer);	end[0]=c;	memmove(lirc_buffer,end,end_len+1);	if(*code==NULL) return(-1);	return(0);}

⌨️ 快捷键说明

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