MythTV  master
lirc_client.c
Go to the documentation of this file.
1 /* NOTE: Extracted from LIRC release 0.8.4a -- dtk */
2 /* Updated to LIRC release 0.8.6 */
3 
4 /****************************************************************************
5  ** lirc_client.c ***********************************************************
6  ****************************************************************************
7  *
8  * lirc_client - common routines for lircd clients
9  *
10  * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
11  * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
12  *
13  * System wide LIRCRC support by Michal Svec <rebel@atrey.karlin.mff.cuni.cz>
14  */
15 
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19 
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 
33 #include "lirc_client.h"
34 
35 
36 /* internal defines */
37 #define MAX_INCLUDES 10
38 #define LIRC_READ 255
39 #define LIRC_PACKET_SIZE 255
40 /* three seconds */
41 #define LIRC_TIMEOUT 3
42 
43 /* internal data structures */
44 struct filestack_t {
46  char *name;
47  int line;
49 };
50 
52 {
57  P_N,
60 };
61 
62 /* internal functions */
63 static void lirc_printf(const struct lirc_state*, const char *format_str, ...);
64 static void lirc_perror(const struct lirc_state*, const char *s);
65 static int lirc_readline(const struct lirc_state *state, char **line,FILE *f);
66 static char *lirc_trim(char *s);
67 static char lirc_parse_escape(const struct lirc_state *state, char **s,const char *name,int line);
68 static void lirc_parse_string(const struct lirc_state *state, char *s,const char *name,int line);
69 static void lirc_parse_include(char *s,const char *name,int line);
70 static int lirc_mode(
71  const struct lirc_state *state,
72  const char *token,const char *token2,char **mode,
73  struct lirc_config_entry **new_config,
74  struct lirc_config_entry **first_config,
75  struct lirc_config_entry **last_config,
76  int (check)(char *s),
77  const char *name,int line);
78 /*
79  lircrc_config relies on this function, hence don't make it static
80  but it's not part of the official interface, so there's no guarantee
81  that it will stay available in the future
82 */
83 static unsigned int lirc_flags(const struct lirc_state *state, char *string);
84 static char *lirc_getfilename(const struct lirc_state *state,
85  const char *file,
86  const char *current_file);
87 static FILE *lirc_open(const struct lirc_state *state,
88  const char *file, const char *current_file,
89  char **full_name);
90 static struct filestack_t *stack_push(const struct lirc_state *state, struct filestack_t *parent);
91 static struct filestack_t *stack_pop(struct filestack_t *entry);
92 static void stack_free(struct filestack_t *entry);
93 static int lirc_readconfig_only_internal(const struct lirc_state *state,
94  const char *file,
95  struct lirc_config **config,
96  int (check)(char *s),
97  char **full_name,
98  char **sha_bang);
99 static char *lirc_startupmode(const struct lirc_state *state,
100  struct lirc_config_entry *first);
101 static void lirc_freeconfigentries(struct lirc_config_entry *first);
102 static void lirc_clearmode(struct lirc_config *config);
103 static char *lirc_execute(const struct lirc_state *state,
104  struct lirc_config *config,
105  struct lirc_config_entry *scan);
106 static int lirc_iscode(struct lirc_config_entry *scan, char *remote,
107  char *button,unsigned int rep);
108 static int lirc_code2char_internal(const struct lirc_state *state,
109  struct lirc_config *config,char *code,
110  char **string, char **prog);
111 static const char *lirc_read_string(const struct lirc_state *state, int fd);
112 static int lirc_identify(const struct lirc_state *state, int sockfd);
113 
114 static int lirc_send_command(const struct lirc_state *state, int sockfd, const char *command, char *buf, size_t *buf_len, int *ret_status);
115 
116 static void lirc_printf(const struct lirc_state *state, const char *format_str, ...)
117 {
118  va_list ap;
119 
120  if(state && !state->lirc_verbose) return;
121 
122  va_start(ap,format_str);
123  vfprintf(stderr,format_str,ap);
124  va_end(ap);
125 }
126 
127 static void lirc_perror(const struct lirc_state *state, const char *s)
128 {
129  if(!state->lirc_verbose) return;
130 
131  perror(s);
132 }
133 
135  const char *lircrc_user_file,
136  const char *prog,
137  const char *lircd,
138  int verbose)
139 {
140  struct sockaddr_un addr;
141 
142  /* connect to lircd */
143 
144  if(lircrc_root_file==NULL || lircrc_user_file == NULL || prog==NULL)
145  {
146  lirc_printf(NULL, "%s: lirc_init invalid params\n",prog);
147  return(NULL);
148  }
149 
150  struct lirc_state *state = (struct lirc_state *) calloc(1, sizeof(struct lirc_state));
151  if(state==NULL)
152  {
153  lirc_printf(NULL, "%s: out of memory\n",prog);
154  return(NULL);
155  }
156  state->lirc_lircd=-1;
157  state->lirc_verbose=verbose;
158 
159  state->lircrc_root_file=strdup(lircrc_root_file);
160  if(state->lircrc_root_file==NULL)
161  {
162  lirc_printf(state, "%s: out of memory\n",prog);
163  lirc_deinit(state);
164  return(NULL);
165  }
166 
167  state->lircrc_user_file=strdup(lircrc_user_file);
168  if(state->lircrc_user_file==NULL)
169  {
170  lirc_printf(state, "%s: out of memory\n",prog);
171  lirc_deinit(state);
172  return(NULL);
173  }
174 
175  state->lirc_prog=strdup(prog);
176  if(state->lirc_prog==NULL)
177  {
178  lirc_printf(state, "%s: out of memory\n",prog);
179  lirc_deinit(state);
180  return(NULL);
181  }
182 
183  if (lircd)
184  {
185  addr.sun_family=AF_UNIX;
186  strncpy(addr.sun_path,lircd,sizeof(addr.sun_path)-1);
187  state->lirc_lircd=socket(AF_UNIX,SOCK_STREAM,0);
188  if(state->lirc_lircd==-1)
189  {
190  lirc_printf(state, "%s: could not open socket\n",state->lirc_prog);
191  lirc_perror(state, state->lirc_prog);
192  lirc_deinit(state);
193  return(NULL);
194  }
195  if(connect(state->lirc_lircd,(struct sockaddr *)&addr,sizeof(addr))==-1)
196  {
197  close(state->lirc_lircd);
198  lirc_printf(state, "%s: could not connect to socket\n",state->lirc_prog);
199  lirc_perror(state, state->lirc_prog);
200  lirc_deinit(state);
201  return(NULL);
202  }
203  }
204 
205  return(state);
206 }
207 
208 int lirc_deinit(struct lirc_state *state)
209 {
210  int ret = LIRC_RET_SUCCESS;
211  if (state==NULL)
212  return ret;
213  if(state->lircrc_root_file!=NULL)
214  {
215  free(state->lircrc_root_file);
216  state->lircrc_root_file=NULL;
217  }
218  if(state->lircrc_user_file!=NULL)
219  {
220  free(state->lircrc_user_file);
221  state->lircrc_user_file=NULL;
222  }
223  if(state->lirc_prog!=NULL)
224  {
225  free(state->lirc_prog);
226  state->lirc_prog=NULL;
227  }
228  if(state->lirc_buffer!=NULL)
229  {
230  free(state->lirc_buffer);
231  state->lirc_buffer=NULL;
232  }
233  if (state->lirc_lircd!=-1)
234  ret = close(state->lirc_lircd);
235  free(state);
236  return ret;
237 }
238 
239 static int lirc_readline(const struct lirc_state *state, char **line,FILE *f)
240 {
241  char *newline,*ret,*enlargeline;
242  int len;
243 
244  newline=(char *) malloc(LIRC_READ+1);
245  if(newline==NULL)
246  {
247  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
248  return(-1);
249  }
250  len=0;
251  while(1)
252  {
253  ret=fgets(newline+len,LIRC_READ+1,f);
254  if(ret==NULL)
255  {
256  if(feof(f) && len>0)
257  {
258  *line=newline;
259  }
260  else
261  {
262  free(newline);
263  *line=NULL;
264  }
265  return(0);
266  }
267  len=strlen(newline);
268  if(newline[len-1]=='\n')
269  {
270  newline[len-1]=0;
271  *line=newline;
272  return(0);
273  }
274 
275  enlargeline=(char *) realloc(newline,len+1+LIRC_READ);
276  if(enlargeline==NULL)
277  {
278  free(newline);
279  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
280  return(-1);
281  }
282  newline=enlargeline;
283  }
284 }
285 
286 static char *lirc_trim(char *s)
287 {
288  int len;
289 
290  while(s[0]==' ' || s[0]=='\t') s++;
291  len=strlen(s);
292  while(len>0)
293  {
294  len--;
295  if(s[len]==' ' || s[len]=='\t') s[len]=0;
296  else break;
297  }
298  return(s);
299 }
300 
301 /* parse standard C escape sequences + \@,\A-\Z is ^@,^A-^Z */
302 
303 static char lirc_parse_escape(const struct lirc_state *state, char **s,const char *name,int line)
304 {
305 
306  char c;
307  unsigned int i,overflow,count;
308  int digits_found,digit;
309 
310  c=**s;
311  (*s)++;
312  switch(c)
313  {
314  case 'a':
315  return('\a');
316  case 'b':
317  return('\b');
318  case 'e':
319 #if 0
320  case 'E': /* this should become ^E */
321 #endif
322  return(033);
323  case 'f':
324  return('\f');
325  case 'n':
326  return('\n');
327  case 'r':
328  return('\r');
329  case 't':
330  return('\t');
331  case 'v':
332  return('\v');
333  case '\n':
334  return(0);
335  case 0:
336  (*s)--;
337  return 0;
338  case '0':
339  case '1':
340  case '2':
341  case '3':
342  case '4':
343  case '5':
344  case '6':
345  case '7':
346  i=c-'0';
347  count=0;
348 
349  while(++count<3)
350  {
351  c=*(*s)++;
352  if(c>='0' && c<='7')
353  {
354  i=(i << 3)+c-'0';
355  }
356  else
357  {
358  (*s)--;
359  break;
360  }
361  }
362  if(i>(1<<CHAR_BIT)-1)
363  {
364  i&=(1<<CHAR_BIT)-1;
365  lirc_printf(state, "%s: octal escape sequence "
366  "out of range in %s:%d\n",state->lirc_prog,
367  name,line);
368  }
369  return((char) i);
370  case 'x':
371  {
372  i=0;
373  overflow=0;
374  digits_found=0;
375  for (;;)
376  {
377  c = *(*s)++;
378  if(c>='0' && c<='9')
379  digit=c-'0';
380  else if(c>='a' && c<='f')
381  digit=c-'a'+10;
382  else if(c>='A' && c<='F')
383  digit=c-'A'+10;
384  else
385  {
386  (*s)--;
387  break;
388  }
389  overflow|=i^(i<<4>>4);
390  i=(i<<4)+digit;
391  digits_found=1;
392  }
393  if(!digits_found)
394  {
395  lirc_printf(state, "%s: \\x used with no "
396  "following hex digits in %s:%d\n",
397  state->lirc_prog,name,line);
398  }
399  if(overflow || i>(1<<CHAR_BIT)-1)
400  {
401  i&=(1<<CHAR_BIT)-1;
402  lirc_printf(state, "%s: hex escape sequence out "
403  "of range in %s:%d\n",
404  state->lirc_prog,name,line);
405  }
406  return((char) i);
407  }
408  default:
409  if(c>='@' && c<='Z') return(c-'@');
410  return(c);
411  }
412 }
413 
414 static void lirc_parse_string(const struct lirc_state *state, char *s,const char *name,int line)
415 {
416  char *t;
417 
418  t=s;
419  while(*s!=0)
420  {
421  if(*s=='\\')
422  {
423  s++;
424  *t=lirc_parse_escape(state, &s,name,line);
425  t++;
426  }
427  else
428  {
429  *t=*s;
430  s++;
431  t++;
432  }
433  }
434  *t=0;
435 }
436 
437 static void lirc_parse_include(char *s,const char *name,int line)
438 {
439  char last;
440  size_t len;
441 
442  (void)name;
443  (void)line;
444  len=strlen(s);
445  if(len<2)
446  {
447  return;
448  }
449  last=s[len-1];
450  if(*s!='"' && *s!='<')
451  {
452  return;
453  }
454  if(*s=='"' && last!='"')
455  {
456  return;
457  }
458  else if(*s=='<' && last!='>')
459  {
460  return;
461  }
462  s[len-1]=0;
463  memmove(s, s+1, len-2+1); /* terminating 0 is copied */
464 }
465 
466 int lirc_mode(const struct lirc_state *state,
467  const char *token,const char *token2,char **mode,
468  struct lirc_config_entry **new_config,
469  struct lirc_config_entry **first_config,
470  struct lirc_config_entry **last_config,
471  int (check)(char *s),
472  const char *name,int line)
473 {
474  struct lirc_config_entry *new_entry;
475 
476  new_entry=*new_config;
477  if(strcasecmp(token,"begin")==0)
478  {
479  if(token2==NULL)
480  {
481  if(new_entry==NULL)
482  {
483  new_entry=(struct lirc_config_entry *)
484  malloc(sizeof(struct lirc_config_entry));
485  if(new_entry==NULL)
486  {
487  lirc_printf(state, "%s: out of memory\n",
488  state->lirc_prog);
489  return(-1);
490  }
491  else
492  {
493  new_entry->prog=NULL;
494  new_entry->code=NULL;
495  new_entry->rep_delay=0;
496  new_entry->rep=0;
497  new_entry->config=NULL;
498  new_entry->change_mode=NULL;
499  new_entry->flags=none;
500  new_entry->mode=NULL;
501  new_entry->next_config=NULL;
502  new_entry->next_code=NULL;
503  new_entry->next=NULL;
504 
505  *new_config=new_entry;
506  }
507  }
508  else
509  {
510  lirc_printf(state, "%s: bad file format, "
511  "%s:%d\n",state->lirc_prog,name,line);
512  return(-1);
513  }
514  }
515  else
516  {
517  if(new_entry==NULL && *mode==NULL)
518  {
519  *mode=strdup(token2);
520  if(*mode==NULL)
521  {
522  return(-1);
523  }
524  }
525  else
526  {
527  lirc_printf(state, "%s: bad file format, "
528  "%s:%d\n",state->lirc_prog,name,line);
529  return(-1);
530  }
531  }
532  }
533  else if(strcasecmp(token,"end")==0)
534  {
535  if(token2==NULL)
536  {
537  if(new_entry!=NULL)
538  {
539 #if 0
540  if(new_entry->prog==NULL)
541  {
542  lirc_printf(state, "%s: prog missing in "
543  "config before line %d\n",
544  state->lirc_prog,line);
545  lirc_freeconfigentries(new_entry);
546  *new_config=NULL;
547  return(-1);
548  }
549  if(strcasecmp(new_entry->prog,state->lirc_prog)!=0)
550  {
551  lirc_freeconfigentries(new_entry);
552  *new_config=NULL;
553  return(0);
554  }
555 #endif
556  new_entry->next_code=new_entry->code;
557  new_entry->next_config=new_entry->config;
558  if(*last_config==NULL)
559  {
560  *first_config=new_entry;
561  *last_config=new_entry;
562  }
563  else
564  {
565  (*last_config)->next=new_entry;
566  *last_config=new_entry;
567  }
568  *new_config=NULL;
569 
570  if(*mode!=NULL)
571  {
572  new_entry->mode=strdup(*mode);
573  if(new_entry->mode==NULL)
574  {
575  lirc_printf(state, "%s: out of "
576  "memory\n",
577  state->lirc_prog);
578  return(-1);
579  }
580  }
581 
582  if(check!=NULL &&
583  new_entry->prog!=NULL &&
584  strcasecmp(new_entry->prog,state->lirc_prog)==0)
585  {
586  struct lirc_list *list;
587 
588  list=new_entry->config;
589  while(list!=NULL)
590  {
591  if(check(list->string)==-1)
592  {
593  return(-1);
594  }
595  list=list->next;
596  }
597  }
598 
599  if (new_entry->rep_delay==0 &&
600  new_entry->rep>0)
601  {
602  new_entry->rep_delay=new_entry->rep-1;
603  }
604  }
605  else
606  {
607  lirc_printf(state, "%s: %s:%d: 'end' without "
608  "'begin'\n",state->lirc_prog,name,line);
609  return(-1);
610  }
611  }
612  else
613  {
614  if(*mode!=NULL)
615  {
616  if(new_entry!=NULL)
617  {
618  lirc_printf(state, "%s: %s:%d: missing "
619  "'end' token\n",state->lirc_prog,
620  name,line);
621  return(-1);
622  }
623  if(strcasecmp(*mode,token2)==0)
624  {
625  free(*mode);
626  *mode=NULL;
627  }
628  else
629  {
630  lirc_printf(state, "%s: \"%s\" doesn't "
631  "match mode \"%s\"\n",
632  state->lirc_prog,token2,*mode);
633  return(-1);
634  }
635  }
636  else
637  {
638  lirc_printf(state, "%s: %s:%d: 'end %s' without "
639  "'begin'\n",state->lirc_prog,name,line,
640  token2);
641  return(-1);
642  }
643  }
644  }
645  else
646  {
647  lirc_printf(state, "%s: unknown token \"%s\" in %s:%d ignored\n",
648  state->lirc_prog,token,name,line);
649  }
650  return(0);
651 }
652 
653 unsigned int lirc_flags(const struct lirc_state *state, char *string)
654 {
655  char *s, *strtok_state = NULL;
656  unsigned int flags;
657 
658  flags=none;
659  s=strtok_r(string," \t|",&strtok_state);
660  while(s)
661  {
662  if(strcasecmp(s,"once")==0)
663  {
664  flags|=once;
665  }
666  else if(strcasecmp(s,"quit")==0)
667  {
668  flags|=quit;
669  }
670  else if(strcasecmp(s,"mode")==0)
671  {
672  flags|=modex;
673  }
674  else if(strcasecmp(s,"startup_mode")==0)
675  {
676  flags|=startup_mode;
677  }
678  else if(strcasecmp(s,"toggle_reset")==0)
679  {
680  flags|=toggle_reset;
681  }
682  else
683  {
684  lirc_printf(state, "%s: unknown flag \"%s\"\n",state->lirc_prog,s);
685  }
686  s=strtok_r(NULL," \t",&strtok_state);
687  }
688  return(flags);
689 }
690 
691 static char *lirc_getfilename(const struct lirc_state *state,
692  const char *file,
693  const char *current_file)
694 {
695  const char *home;
696  char *filename;
697 
698  if(file==NULL)
699  {
700  home=getenv("HOME");
701  if(home==NULL)
702  {
703  home="/";
704  }
705  filename=(char *) malloc(strlen(home)+1+
706  strlen(state->lircrc_user_file)+1);
707  if(filename==NULL)
708  {
709  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
710  return NULL;
711  }
712  strcpy(filename,home);
713  if(strlen(home)>0 && filename[strlen(filename)-1]!='/')
714  {
715  strcat(filename,"/");
716  }
717  strcat(filename,state->lircrc_user_file);
718  }
719  else if(strncmp(file, "~/", 2)==0)
720  {
721  home=getenv("HOME");
722  if(home==NULL)
723  {
724  home="/";
725  }
726  filename=(char *) malloc(strlen(home)+strlen(file)+1);
727  if(filename==NULL)
728  {
729  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
730  return NULL;
731  }
732  strcpy(filename,home);
733  strcat(filename,file+1);
734  }
735  else if(file[0]=='/' || current_file==NULL)
736  {
737  /* absulute path or root */
738  filename=strdup(file);
739  if(filename==NULL)
740  {
741  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
742  return NULL;
743  }
744  }
745  else
746  {
747  /* get path from parent filename */
748  int pathlen = strlen(current_file);
749  while (pathlen>0 && current_file[pathlen-1]!='/')
750  pathlen--;
751  filename=(char *) malloc(pathlen+strlen(file)+1);
752  if(filename==NULL)
753  {
754  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
755  return NULL;
756  }
757  memcpy(filename,current_file,pathlen);
758  filename[pathlen]=0;
759  strcat(filename,file);
760  }
761  return filename;
762 }
763 
764 static FILE *lirc_open(const struct lirc_state *state,
765  const char *file, const char *current_file,
766  char **full_name)
767 {
768  FILE *fin;
769  char *filename;
770 
771  filename=lirc_getfilename(state, file, current_file);
772  if(filename==NULL)
773  {
774  return NULL;
775  }
776 
777  fin=fopen(filename,"r");
778  if(fin==NULL && (file!=NULL || errno!=ENOENT))
779  {
780  lirc_printf(state, "%s: could not open config file %s\n",
781  state->lirc_prog,filename);
782  lirc_perror(state, state->lirc_prog);
783  }
784  else if(fin==NULL)
785  {
786  fin=fopen(state->lircrc_root_file,"r");
787  if(fin==NULL && errno!=ENOENT)
788  {
789  lirc_printf(state, "%s: could not open config file %s\n",
790  state->lirc_prog,state->lircrc_root_file);
791  lirc_perror(state, state->lirc_prog);
792  }
793  else if(fin==NULL)
794  {
795  lirc_printf(state, "%s: could not open config files "
796  "%s and %s\n",
797  state->lirc_prog,filename,state->lircrc_root_file);
798  lirc_perror(state, state->lirc_prog);
799  }
800  else
801  {
802  free(filename);
803  filename = strdup(state->lircrc_root_file);
804  if(filename==NULL)
805  {
806  fclose(fin);
807  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
808  return NULL;
809  }
810  }
811  }
812  if(full_name && fin!=NULL)
813  {
814  *full_name = filename;
815  }
816  else
817  {
818  free(filename);
819  }
820  return fin;
821 }
822 
823 static struct filestack_t *stack_push(const struct lirc_state *state, struct filestack_t *parent)
824 {
825  struct filestack_t *entry;
826  entry = malloc(sizeof(struct filestack_t));
827  if (entry == NULL)
828  {
829  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
830  return NULL;
831  }
832  entry->file = NULL;
833  entry->name = NULL;
834  entry->line = 0;
835  entry->parent = parent;
836  return entry;
837 }
838 
839 static struct filestack_t *stack_pop(struct filestack_t *entry)
840 {
841  struct filestack_t *parent = NULL;
842  if (entry)
843  {
844  parent = entry->parent;
845  if (entry->name)
846  free(entry->name);
847  free(entry);
848  }
849  return parent;
850 }
851 
852 static void stack_free(struct filestack_t *entry)
853 {
854  while (entry)
855  {
856  entry = stack_pop(entry);
857  }
858 }
859 
860 int lirc_readconfig(const struct lirc_state *state,
861  const char *file,
862  struct lirc_config **config,
863  int (check)(char *s))
864 {
865  struct sockaddr_un addr;
866  int sockfd = -1;
867  char *sha_bang, *filename;
868  const char *sha_bang2;
869  char *command;
870  unsigned int ret;
871 
872  filename = NULL;
873  sha_bang = NULL;
874  if(lirc_readconfig_only_internal(state,file,config,check,&filename,&sha_bang)==-1)
875  {
876  return -1;
877  }
878 
879  if(sha_bang == NULL)
880  {
881  goto lirc_readconfig_compat;
882  }
883 
884  /* connect to lircrcd */
885 
886  addr.sun_family=AF_UNIX;
887  if(lirc_getsocketname(filename, addr.sun_path, sizeof(addr.sun_path))>sizeof(addr.sun_path))
888  {
889  lirc_printf(state, "%s: WARNING: file name too long\n", state->lirc_prog);
890  goto lirc_readconfig_compat;
891  }
892  sockfd=socket(AF_UNIX,SOCK_STREAM,0);
893  if(sockfd==-1)
894  {
895  lirc_printf(state, "%s: WARNING: could not open socket\n",state->lirc_prog);
896  lirc_perror(state, state->lirc_prog);
897  goto lirc_readconfig_compat;
898  }
899  if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr))!=-1)
900  {
901  if(sha_bang!=NULL) free(sha_bang);
902  (*config)->sockfd=sockfd;
903  free(filename);
904 
905  /* tell daemon state->lirc_prog */
906  if(lirc_identify(state, sockfd) == LIRC_RET_SUCCESS)
907  {
908  /* we're connected */
909  return 0;
910  }
911  close(sockfd);
912  lirc_freeconfig(*config);
913  return -1;
914  }
915  close(sockfd);
916  sockfd = -1;
917 
918  /* launch lircrcd */
919  sha_bang2=sha_bang!=NULL ? sha_bang:"lircrcd";
920 
921  command=malloc(strlen(sha_bang2)+1+strlen(filename)+1);
922  if(command==NULL)
923  {
924  goto lirc_readconfig_compat;
925  }
926  strcpy(command, sha_bang2);
927  strcat(command, " ");
928  strcat(command, filename);
929 
930  ret = system(command);
931  free(command);
932 
933  if(ret!=EXIT_SUCCESS)
934  {
935  goto lirc_readconfig_compat;
936  }
937 
938  if(sha_bang!=NULL) { free(sha_bang); sha_bang = NULL; }
939  free(filename); filename = NULL;
940 
941  sockfd=socket(AF_UNIX,SOCK_STREAM,0);
942  if(sockfd==-1)
943  {
944  lirc_printf(state, "%s: WARNING: could not open socket\n",state->lirc_prog);
945  lirc_perror(state, state->lirc_prog);
946  goto lirc_readconfig_compat;
947  }
948  if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr))!=-1)
949  {
950  if(lirc_identify(state, sockfd) == LIRC_RET_SUCCESS)
951  {
952  (*config)->sockfd=sockfd;
953  return 0;
954  }
955  }
956  close(sockfd);
957  lirc_freeconfig(*config);
958  return -1;
959 
960  lirc_readconfig_compat:
961  /* compat fallback */
962  if(sockfd != -1) close(sockfd);
963  if(sha_bang!=NULL) free(sha_bang);
964  free(filename);
965  return 0;
966 }
967 
968 int lirc_readconfig_only(const struct lirc_state *state,
969  const char *file,
970  struct lirc_config **config,
971  int (check)(char *s))
972 {
973  return lirc_readconfig_only_internal(state, file, config, check, NULL, NULL);
974 }
975 
976 static int lirc_readconfig_only_internal(const struct lirc_state *state,
977  const char *file,
978  struct lirc_config **config,
979  int (check)(char *s),
980  char **full_name,
981  char **sha_bang)
982 {
983  char *string,*eq,*token,*token2,*token3,*strtok_state = NULL;
984  struct filestack_t *filestack, *stack_tmp;
985  int open_files;
986  struct lirc_config_entry *new_entry,*first,*last;
987  char *mode,*remote;
988  int ret=0;
989  int firstline=1;
990  char *save_full_name = NULL;
991 
992  filestack = stack_push(state, NULL);
993  if (filestack == NULL)
994  {
995  return -1;
996  }
997  filestack->file = lirc_open(state, file, NULL, &(filestack->name));
998  if (filestack->file == NULL)
999  {
1000  stack_free(filestack);
1001  return -1;
1002  }
1003  filestack->line = 0;
1004  open_files = 1;
1005 
1006  first=new_entry=last=NULL;
1007  mode=NULL;
1008  remote=LIRC_ALL;
1009  while (filestack)
1010  {
1011  if((ret=lirc_readline(state,&string,filestack->file))==-1 ||
1012  string==NULL)
1013  {
1014  fclose(filestack->file);
1015  if(open_files == 1 && full_name != NULL)
1016  {
1017  save_full_name = filestack->name;
1018  filestack->name = NULL;
1019  }
1020  filestack = stack_pop(filestack);
1021  open_files--;
1022  continue;
1023  }
1024  /* check for sha-bang */
1025  if(firstline && sha_bang)
1026  {
1027  firstline = 0;
1028  if(strncmp(string, "#!", 2)==0)
1029  {
1030  *sha_bang=strdup(string+2);
1031  if(*sha_bang==NULL)
1032  {
1033  lirc_printf(state, "%s: out of memory\n",
1034  state->lirc_prog);
1035  ret=-1;
1036  free(string);
1037  break;
1038  }
1039  }
1040  }
1041  filestack->line++;
1042  eq=strchr(string,'=');
1043  if(eq==NULL)
1044  {
1045  token=strtok_r(string," \t",&strtok_state);
1046  if(token==NULL)
1047  {
1048  /* ignore empty line */
1049  }
1050  else if(token[0]=='#')
1051  {
1052  /* ignore comment */
1053  }
1054  else if(strcasecmp(token, "include") == 0)
1055  {
1056  if (open_files >= MAX_INCLUDES)
1057  {
1058  lirc_printf(state, "%s: too many files "
1059  "included at %s:%d\n",
1060  state->lirc_prog,
1061  filestack->name,
1062  filestack->line);
1063  ret=-1;
1064  }
1065  else
1066  {
1067  token2 = strtok_r(NULL, "", &strtok_state);
1068  token2 = lirc_trim(token2);
1070  (token2, filestack->name,
1071  filestack->line);
1072  stack_tmp = stack_push(state, filestack);
1073  if (stack_tmp == NULL)
1074  {
1075  ret=-1;
1076  }
1077  else
1078  {
1079  stack_tmp->file = lirc_open(state, token2, filestack->name, &(stack_tmp->name));
1080  stack_tmp->line = 0;
1081  if (stack_tmp->file)
1082  {
1083  open_files++;
1084  filestack = stack_tmp;
1085  }
1086  else
1087  {
1088  stack_pop(stack_tmp);
1089  ret=-1;
1090  }
1091  }
1092  }
1093  }
1094  else
1095  {
1096  token2=strtok_r(NULL," \t",&strtok_state);
1097  if(token2!=NULL &&
1098  (token3=strtok_r(NULL," \t",&strtok_state))!=NULL)
1099  {
1100  lirc_printf(state, "%s: unexpected token in line %s:%d\n",
1101  state->lirc_prog,filestack->name,filestack->line);
1102  }
1103  else
1104  {
1105  ret=lirc_mode(state, token,token2,&mode,
1106  &new_entry,&first,&last,
1107  check,
1108  filestack->name,
1109  filestack->line);
1110  if(ret==0)
1111  {
1112  if(remote!=LIRC_ALL)
1113  free(remote);
1114  remote=LIRC_ALL;
1115  }
1116  else
1117  {
1118  if(mode!=NULL)
1119  {
1120  free(mode);
1121  mode=NULL;
1122  }
1123  if(new_entry!=NULL)
1124  {
1126  (new_entry);
1127  new_entry=NULL;
1128  }
1129  }
1130  }
1131  }
1132  }
1133  else
1134  {
1135  eq[0]=0;
1136  token=lirc_trim(string);
1137  token2=lirc_trim(eq+1);
1138  if(token[0]=='#')
1139  {
1140  /* ignore comment */
1141  }
1142  else if(new_entry==NULL)
1143  {
1144  lirc_printf(state, "%s: bad file format, %s:%d\n",
1145  state->lirc_prog,filestack->name,filestack->line);
1146  ret=-1;
1147  }
1148  else
1149  {
1150  token2=strdup(token2);
1151  if(token2==NULL)
1152  {
1153  lirc_printf(state, "%s: out of memory\n",
1154  state->lirc_prog);
1155  ret=-1;
1156  }
1157  else if(strcasecmp(token,"prog")==0)
1158  {
1159  if(new_entry->prog!=NULL) free(new_entry->prog);
1160  new_entry->prog=token2;
1161  }
1162  else if(strcasecmp(token,"remote")==0)
1163  {
1164  if(remote!=LIRC_ALL)
1165  free(remote);
1166 
1167  if(strcasecmp("*",token2)==0)
1168  {
1169  remote=LIRC_ALL;
1170  free(token2);
1171  }
1172  else
1173  {
1174  remote=token2;
1175  }
1176  }
1177  else if(strcasecmp(token,"button")==0)
1178  {
1179  struct lirc_code *code;
1180 
1181  code=(struct lirc_code *)
1182  malloc(sizeof(struct lirc_code));
1183  if(code==NULL)
1184  {
1185  free(token2);
1186  lirc_printf(state, "%s: out of "
1187  "memory\n",
1188  state->lirc_prog);
1189  ret=-1;
1190  }
1191  else
1192  {
1193  code->remote=remote;
1194  if(strcasecmp("*",token2)==0)
1195  {
1196  code->button=LIRC_ALL;
1197  free(token2);
1198  }
1199  else
1200  {
1201  code->button=token2;
1202  }
1203  code->next=NULL;
1204 
1205  if(new_entry->code==NULL)
1206  {
1207  new_entry->code=code;
1208  }
1209  else
1210  {
1211  new_entry->next_code->next
1212  =code;
1213  }
1214  new_entry->next_code=code;
1215  if(remote!=LIRC_ALL)
1216  {
1217  remote=strdup(remote);
1218  if(remote==NULL)
1219  {
1220  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
1221  ret=-1;
1222  }
1223  }
1224  }
1225  }
1226  else if(strcasecmp(token,"delay")==0)
1227  {
1228  char *end;
1229 
1230  errno=ERANGE+1;
1231  new_entry->rep_delay=strtoul(token2,&end,0);
1232  if((new_entry->rep_delay==UINT_MAX
1233  && errno==ERANGE)
1234  || end[0]!=0
1235  || strlen(token2)==0)
1236  {
1237  lirc_printf(state, "%s: \"%s\" not"
1238  " a valid number for "
1239  "delay\n",state->lirc_prog,
1240  token2);
1241  }
1242  free(token2);
1243  }
1244  else if(strcasecmp(token,"repeat")==0)
1245  {
1246  char *end;
1247 
1248  errno=ERANGE+1;
1249  new_entry->rep=strtoul(token2,&end,0);
1250  if((new_entry->rep==UINT_MAX
1251  && errno==ERANGE)
1252  || end[0]!=0
1253  || strlen(token2)==0)
1254  {
1255  lirc_printf(state, "%s: \"%s\" not"
1256  " a valid number for "
1257  "repeat\n",state->lirc_prog,
1258  token2);
1259  }
1260  free(token2);
1261  }
1262  else if(strcasecmp(token,"config")==0)
1263  {
1264  struct lirc_list *new_list;
1265 
1266  new_list=(struct lirc_list *)
1267  malloc(sizeof(struct lirc_list));
1268  if(new_list==NULL)
1269  {
1270  free(token2);
1271  lirc_printf(state, "%s: out of "
1272  "memory\n",
1273  state->lirc_prog);
1274  ret=-1;
1275  }
1276  else
1277  {
1278  lirc_parse_string(state,token2,filestack->name,filestack->line);
1279  new_list->string=token2;
1280  new_list->next=NULL;
1281  if(new_entry->config==NULL)
1282  {
1283  new_entry->config=new_list;
1284  }
1285  else
1286  {
1287  new_entry->next_config->next
1288  =new_list;
1289  }
1290  new_entry->next_config=new_list;
1291  }
1292  }
1293  else if(strcasecmp(token,"mode")==0)
1294  {
1295  if(new_entry->change_mode!=NULL) free(new_entry->change_mode);
1296  new_entry->change_mode=token2;
1297  }
1298  else if(strcasecmp(token,"flags")==0)
1299  {
1300  new_entry->flags=lirc_flags(state, token2);
1301  free(token2);
1302  }
1303  else
1304  {
1305  free(token2);
1306  lirc_printf(state, "%s: unknown token \"%s\" in %s:%d ignored\n",
1307  state->lirc_prog,token,filestack->name,filestack->line);
1308  }
1309  }
1310  }
1311  free(string);
1312  if(ret==-1) break;
1313  }
1314  if(remote!=LIRC_ALL)
1315  free(remote);
1316  if(new_entry!=NULL)
1317  {
1318  if(ret==0)
1319  {
1320  ret=lirc_mode(state, "end",NULL,&mode,&new_entry,
1321  &first,&last,check,"",0);
1322  lirc_printf(state, "%s: warning: end token missing at end "
1323  "of file\n",state->lirc_prog);
1324  }
1325  else
1326  {
1327  lirc_freeconfigentries(new_entry);
1328  new_entry=NULL;
1329  }
1330  }
1331  if(mode!=NULL)
1332  {
1333  if(ret==0)
1334  {
1335  lirc_printf(state, "%s: warning: no end token found for mode "
1336  "\"%s\"\n",state->lirc_prog,mode);
1337  }
1338  free(mode);
1339  }
1340  if(ret==0)
1341  {
1342  char *startupmode;
1343 
1344  *config=(struct lirc_config *)
1345  malloc(sizeof(struct lirc_config));
1346  if(*config==NULL)
1347  {
1348  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
1350  return(-1);
1351  }
1352  (*config)->first=first;
1353  (*config)->next=first;
1354  startupmode = lirc_startupmode(state, (*config)->first);
1355  (*config)->current_mode=startupmode ? strdup(startupmode):NULL;
1356  (*config)->sockfd=-1;
1357  if(full_name != NULL)
1358  {
1359  *full_name = save_full_name;
1360  save_full_name = NULL;
1361  }
1362  }
1363  else
1364  {
1365  *config=NULL;
1367  if(sha_bang && *sha_bang!=NULL)
1368  {
1369  free(*sha_bang);
1370  *sha_bang=NULL;
1371  }
1372  }
1373  if(filestack)
1374  {
1375  stack_free(filestack);
1376  }
1377  if(save_full_name)
1378  {
1379  free(save_full_name);
1380  }
1381  return(ret);
1382 }
1383 
1384 static char *lirc_startupmode(const struct lirc_state *state, struct lirc_config_entry *first)
1385 {
1386  struct lirc_config_entry *scan;
1387  char *startupmode;
1388 
1389  startupmode=NULL;
1390  scan=first;
1391  /* Set a startup mode based on flags=startup_mode */
1392  while(scan!=NULL)
1393  {
1394  if(scan->flags&startup_mode) {
1395  if(scan->change_mode!=NULL) {
1396  startupmode=scan->change_mode;
1397  /* Remove the startup mode or it confuses lirc mode system */
1398  scan->change_mode=NULL;
1399  break;
1400  }
1401  else {
1402  lirc_printf(state, "%s: startup_mode flags requires 'mode ='\n",
1403  state->lirc_prog);
1404  }
1405  }
1406  scan=scan->next;
1407  }
1408 
1409  /* Set a default mode if we find a mode = client app name */
1410  if(startupmode==NULL) {
1411  scan=first;
1412  while(scan!=NULL)
1413  {
1414  if(scan->mode!=NULL && strcasecmp(state->lirc_prog,scan->mode)==0)
1415  {
1416  startupmode=state->lirc_prog;
1417  break;
1418  }
1419  scan=scan->next;
1420  }
1421  }
1422 
1423  if(startupmode==NULL) return(NULL);
1424  scan=first;
1425  while(scan!=NULL)
1426  {
1427  if(scan->change_mode!=NULL && scan->flags&once &&
1428  strcasecmp(startupmode,scan->change_mode)==0)
1429  {
1430  scan->flags|=ecno;
1431  }
1432  scan=scan->next;
1433  }
1434  return(startupmode);
1435 }
1436 
1438 {
1439  if(config!=NULL)
1440  {
1441  if(config->sockfd!=-1)
1442  {
1443  (void) close(config->sockfd);
1444  config->sockfd=-1;
1445  }
1447  free(config->current_mode);
1448  free(config);
1449  }
1450 }
1451 
1452 static void lirc_freeconfigentries(struct lirc_config_entry *first)
1453 {
1454  struct lirc_config_entry *c,*config_temp;
1455  struct lirc_list *list,*list_temp;
1456  struct lirc_code *code,*code_temp;
1457 
1458  c=first;
1459  while(c!=NULL)
1460  {
1461  if(c->prog) free(c->prog);
1462  if(c->change_mode) free(c->change_mode);
1463  if(c->mode) free(c->mode);
1464 
1465  code=c->code;
1466  while(code!=NULL)
1467  {
1468  if(code->remote!=NULL && code->remote!=LIRC_ALL)
1469  free(code->remote);
1470  if(code->button!=NULL && code->button!=LIRC_ALL)
1471  free(code->button);
1472  code_temp=code->next;
1473  free(code);
1474  code=code_temp;
1475  }
1476 
1477  list=c->config;
1478  while(list!=NULL)
1479  {
1480  if(list->string) free(list->string);
1481  list_temp=list->next;
1482  free(list);
1483  list=list_temp;
1484  }
1485  config_temp=c->next;
1486  free(c);
1487  c=config_temp;
1488  }
1489 }
1490 
1491 static void lirc_clearmode(struct lirc_config *config)
1492 {
1493  struct lirc_config_entry *scan;
1494 
1495  if(config->current_mode==NULL)
1496  {
1497  return;
1498  }
1499  scan=config->first;
1500  while(scan!=NULL)
1501  {
1502  if(scan->change_mode!=NULL)
1503  {
1504  if(strcasecmp(scan->change_mode,config->current_mode)==0)
1505  {
1506  scan->flags&=~ecno;
1507  }
1508  }
1509  scan=scan->next;
1510  }
1511  free(config->current_mode);
1512  config->current_mode=NULL;
1513 }
1514 
1515 static char *lirc_execute(const struct lirc_state *state,
1516  struct lirc_config *config,
1517  struct lirc_config_entry *scan)
1518 {
1519  char *s;
1520  int do_once=1;
1521 
1522  if(scan->flags&modex)
1523  {
1525  }
1526  if(scan->change_mode!=NULL)
1527  {
1528  free(config->current_mode);
1529  config->current_mode=strdup(scan->change_mode);
1530  if(scan->flags&once)
1531  {
1532  if(scan->flags&ecno)
1533  {
1534  do_once=0;
1535  }
1536  else
1537  {
1538  scan->flags|=ecno;
1539  }
1540  }
1541  }
1542  if(scan->next_config!=NULL &&
1543  scan->prog!=NULL &&
1544  (state->lirc_prog == NULL || strcasecmp(scan->prog,state->lirc_prog)==0) &&
1545  do_once==1)
1546  {
1547  s=scan->next_config->string;
1548  scan->next_config=scan->next_config->next;
1549  if(scan->next_config==NULL)
1550  scan->next_config=scan->config;
1551  return(s);
1552  }
1553  return(NULL);
1554 }
1555 
1556 static int lirc_iscode(struct lirc_config_entry *scan, char *remote,
1557  char *button,unsigned int rep)
1558 {
1559  struct lirc_code *codes;
1560 
1561  /* no remote/button specified */
1562  if(scan->code==NULL)
1563  {
1564  return rep==0 ||
1565  (scan->rep>0 && rep>scan->rep_delay &&
1566  ((rep-scan->rep_delay-1)%scan->rep)==0);
1567  }
1568 
1569  /* remote/button match? */
1570  if(scan->next_code->remote==LIRC_ALL ||
1571  strcasecmp(scan->next_code->remote,remote)==0)
1572  {
1573  if(scan->next_code->button==LIRC_ALL ||
1574  strcasecmp(scan->next_code->button,button)==0)
1575  {
1576  int iscode=0;
1577  /* button sequence? */
1578  if(scan->code->next==NULL || rep==0)
1579  {
1580  scan->next_code=scan->next_code->next;
1581  if(scan->code->next != NULL)
1582  {
1583  iscode=1;
1584  }
1585  }
1586  /* sequence completed? */
1587  if(scan->next_code==NULL)
1588  {
1589  scan->next_code=scan->code;
1590  if(scan->code->next!=NULL || rep==0 ||
1591  (scan->rep>0 && rep>scan->rep_delay &&
1592  ((rep-scan->rep_delay-1)%scan->rep)==0))
1593  iscode=2;
1594  }
1595  return iscode;
1596  }
1597  }
1598 
1599  if(rep!=0) return(0);
1600 
1601  /* handle toggle_reset */
1602  if(scan->flags & toggle_reset)
1603  {
1604  scan->next_config = scan->config;
1605  }
1606 
1607  codes=scan->code;
1608  if(codes==scan->next_code) return(0);
1609  codes=codes->next;
1610  /* rebase code sequence */
1611  while(codes!=scan->next_code->next)
1612  {
1613  struct lirc_code *prev,*next;
1614  int flag=1;
1615 
1616  prev=scan->code;
1617  next=codes;
1618  while(next!=scan->next_code)
1619  {
1620  if(prev->remote==LIRC_ALL ||
1621  strcasecmp(prev->remote,next->remote)==0)
1622  {
1623  if(prev->button==LIRC_ALL ||
1624  strcasecmp(prev->button,next->button)==0)
1625  {
1626  prev=prev->next;
1627  next=next->next;
1628  }
1629  else
1630  {
1631  flag=0;break;
1632  }
1633  }
1634  else
1635  {
1636  flag=0;break;
1637  }
1638  }
1639  if(flag==1)
1640  {
1641  if(prev->remote==LIRC_ALL ||
1642  strcasecmp(prev->remote,remote)==0)
1643  {
1644  if(prev->button==LIRC_ALL ||
1645  strcasecmp(prev->button,button)==0)
1646  {
1647  if(rep==0)
1648  {
1649  scan->next_code=prev->next;
1650  return(0);
1651  }
1652  }
1653  }
1654  }
1655  codes=codes->next;
1656  }
1657  scan->next_code=scan->code;
1658  return(0);
1659 }
1660 
1661 #if 0
1662 char *lirc_ir2char(const struct lirc_state *state,struct lirc_config *config,char *code)
1663 {
1664  static int warning=1;
1665  char *string;
1666 
1667  if(warning)
1668  {
1669  fprintf(stderr,"%s: warning: lirc_ir2char() is obsolete\n",
1670  state->lirc_prog);
1671  warning=0;
1672  }
1673  if(lirc_code2char(state,config,code,&string)==-1) return(NULL);
1674  return(string);
1675 }
1676 #endif
1677 
1678 int lirc_code2char(const struct lirc_state *state, struct lirc_config *config,char *code,char **string)
1679 {
1680  if(config->sockfd!=-1)
1681  {
1682  char* command = malloc((10+strlen(code)+1+1) * sizeof(char));
1683  if (command == NULL)
1684  return LIRC_RET_ERROR;
1685  static char buf[LIRC_PACKET_SIZE];
1686  size_t buf_len = LIRC_PACKET_SIZE;
1687  int success;
1688  int ret;
1689 
1690  sprintf(command, "CODE %s\n", code);
1691 
1692  ret = lirc_send_command(state, config->sockfd, command,
1693  buf, &buf_len, &success);
1694  if(success == LIRC_RET_SUCCESS)
1695  {
1696  if(ret > 0)
1697  {
1698  *string = buf;
1699  }
1700  else
1701  {
1702  *string = NULL;
1703  }
1704  free(command);
1705  return LIRC_RET_SUCCESS;
1706  }
1707  free(command);
1708  return LIRC_RET_ERROR;
1709  }
1710  return lirc_code2char_internal(state, config, code, string, NULL);
1711 }
1712 
1713 int lirc_code2charprog(struct lirc_state *state,struct lirc_config *config,char *code,char **string,
1714  char **prog)
1715 {
1716  char *backup;
1717  int ret;
1718 
1719  backup = state->lirc_prog;
1720  state->lirc_prog = NULL;
1721 
1722  ret = lirc_code2char_internal(state,config, code, string, prog);
1723 
1724  state->lirc_prog = backup;
1725  return ret;
1726 }
1727 
1728 static int lirc_code2char_internal(const struct lirc_state *state,
1729  struct lirc_config *config,char *code,
1730  char **string, char **prog)
1731 {
1732  unsigned int rep;
1733  char *backup, *strtok_state = NULL;
1734  char *remote,*button;
1735  char *s=NULL;
1736  struct lirc_config_entry *scan;
1737  int exec_level;
1738  int quit_happened;
1739 
1740  *string=NULL;
1741  if(sscanf(code,"%*20x %20x %*5000s %*5000s\n",&rep)==1)
1742  {
1743  backup=strdup(code);
1744  if(backup==NULL) return(-1);
1745 
1746  strtok_r(backup," ",&strtok_state);
1747  strtok_r(NULL," ",&strtok_state);
1748  button=strtok_r(NULL," ",&strtok_state);
1749  remote=strtok_r(NULL,"\n",&strtok_state);
1750 
1751  if(button==NULL || remote==NULL)
1752  {
1753  free(backup);
1754  return(0);
1755  }
1756 
1757  scan=config->next;
1758  quit_happened=0;
1759  while(scan!=NULL)
1760  {
1761  exec_level = lirc_iscode(scan,remote,button,rep);
1762  if(exec_level > 0 &&
1763  (scan->mode==NULL ||
1764  (scan->mode!=NULL &&
1765  config->current_mode!=NULL &&
1766  strcasecmp(scan->mode,config->current_mode)==0)) &&
1767  quit_happened==0
1768  )
1769  {
1770  if(exec_level > 1)
1771  {
1772  s=lirc_execute(state,config,scan);
1773  if(s != NULL && prog != NULL)
1774  {
1775  *prog = scan->prog;
1776  }
1777  }
1778  else
1779  {
1780  s = NULL;
1781  }
1782  if(scan->flags&quit)
1783  {
1784  quit_happened=1;
1785  config->next=NULL;
1786  scan=scan->next;
1787  continue;
1788  }
1789  else if(s!=NULL)
1790  {
1791  config->next=scan->next;
1792  break;
1793  }
1794  }
1795  scan=scan->next;
1796  }
1797  free(backup);
1798  if(s!=NULL)
1799  {
1800  *string=s;
1801  return(0);
1802  }
1803  }
1804  config->next=config->first;
1805  return(0);
1806 }
1807 
1808 #define PACKET_SIZE 100
1809 
1810 #if 0
1811 char *lirc_nextir(struct lirc_state *state)
1812 {
1813  static int warning=1;
1814  char *code;
1815  int ret;
1816 
1817  if(warning)
1818  {
1819  fprintf(stderr,"%s: warning: lirc_nextir() is obsolete\n",
1820  state->lirc_prog);
1821  warning=0;
1822  }
1823  ret=lirc_nextcode(state, &code);
1824  if(ret==-1) return(NULL);
1825  return(code);
1826 }
1827 #endif
1828 
1829 int lirc_nextcode(struct lirc_state *state, char **code)
1830 {
1831  static int packet_size=PACKET_SIZE;
1832  static int end_len=0;
1833  ssize_t len=0;
1834  char *end,c;
1835 
1836  *code=NULL;
1837  if(state->lirc_buffer==NULL)
1838  {
1839  state->lirc_buffer=(char *) malloc(packet_size+1);
1840  if(state->lirc_buffer==NULL)
1841  {
1842  lirc_printf(state, "%s: out of memory\n",state->lirc_prog);
1843  return(-1);
1844  }
1845  state->lirc_buffer[0]=0;
1846  }
1847  while((end=strchr(state->lirc_buffer,'\n'))==NULL)
1848  {
1849  if(end_len>=packet_size)
1850  {
1851  char *new_buffer;
1852 
1853  packet_size+=PACKET_SIZE;
1854  new_buffer=(char *) realloc(state->lirc_buffer,packet_size+1);
1855  if(new_buffer==NULL)
1856  {
1857  return(-1);
1858  }
1859  state->lirc_buffer=new_buffer;
1860  }
1861  len=read(state->lirc_lircd,state->lirc_buffer+end_len,packet_size-end_len);
1862  if(len<=0)
1863  {
1864  if(len==-1 && errno==EAGAIN) return(0);
1865  else return(-1);
1866  }
1867  end_len+=len;
1868  state->lirc_buffer[end_len]=0;
1869  /* return if next code not yet available completely */
1870  if(strchr(state->lirc_buffer,'\n')==NULL)
1871  {
1872  return(0);
1873  }
1874  }
1875  /* copy first line to buffer (code) and move remaining chars to
1876  state->lirc_buffers start */
1877  end++;
1878  end_len=strlen(end);
1879  c=end[0];
1880  end[0]=0;
1881  *code=strdup(state->lirc_buffer);
1882  end[0]=c;
1883  memmove(state->lirc_buffer,end,end_len+1);
1884  if(*code==NULL) return(-1);
1885  return(0);
1886 }
1887 
1888 size_t lirc_getsocketname(const char *filename, char *buf, size_t size)
1889 {
1890  if(strlen(filename)+2<=size)
1891  {
1892  strcpy(buf, filename);
1893  strcat(buf, "d");
1894  }
1895  return strlen(filename)+2;
1896 }
1897 
1898 const char *lirc_getmode(const struct lirc_state *state, struct lirc_config *config)
1899 {
1900  if(config->sockfd!=-1)
1901  {
1902  static char buf[LIRC_PACKET_SIZE];
1903  size_t buf_len = LIRC_PACKET_SIZE;
1904  int success;
1905  int ret;
1906 
1907  ret = lirc_send_command(state, config->sockfd, "GETMODE\n",
1908  buf, &buf_len, &success);
1909  if(success == LIRC_RET_SUCCESS)
1910  {
1911  if(ret > 0)
1912  {
1913  return buf;
1914  }
1915  else
1916  {
1917  return NULL;
1918  }
1919  }
1920  return NULL;
1921  }
1922  return config->current_mode;
1923 }
1924 
1925 const char *lirc_setmode(const struct lirc_state *state, struct lirc_config *config, const char *mode)
1926 {
1927  if(config->sockfd!=-1)
1928  {
1929  static char buf[LIRC_PACKET_SIZE];
1930  size_t buf_len = LIRC_PACKET_SIZE;
1931  int success;
1932  int ret;
1933  char cmd[LIRC_PACKET_SIZE];
1934  if(snprintf(cmd, LIRC_PACKET_SIZE, "SETMODE%s%s\n",
1935  mode ? " ":"",
1936  mode ? mode:"")
1937  >= LIRC_PACKET_SIZE)
1938  {
1939  return NULL;
1940  }
1941 
1942  ret = lirc_send_command(state, config->sockfd, cmd,
1943  buf, &buf_len, &success);
1944  if(success == LIRC_RET_SUCCESS)
1945  {
1946  if(ret > 0)
1947  {
1948  return buf;
1949  }
1950  else
1951  {
1952  return NULL;
1953  }
1954  }
1955  return NULL;
1956  }
1957 
1958  free(config->current_mode);
1959  config->current_mode = mode ? strdup(mode) : NULL;
1960  return config->current_mode;
1961 }
1962 
1963 static const char *lirc_read_string(const struct lirc_state *state, int fd)
1964 {
1965  static char buffer[LIRC_PACKET_SIZE+1]="";
1966  char *end;
1967  static size_t head=0, tail=0;
1968  int ret;
1969  ssize_t n;
1970  fd_set fds;
1971  struct timeval tv;
1972 
1973  if(head>0)
1974  {
1975  memmove(buffer,buffer+head,tail-head+1);
1976  tail-=head;
1977  head=0;
1978  end=strchr(buffer,'\n');
1979  }
1980  else
1981  {
1982  end=NULL;
1983  }
1984  if(strlen(buffer)!=tail)
1985  {
1986  lirc_printf(state, "%s: protocol error\n", state->lirc_prog);
1987  goto lirc_read_string_error;
1988  }
1989 
1990  while(end==NULL)
1991  {
1992  if(LIRC_PACKET_SIZE<=tail)
1993  {
1994  lirc_printf(state, "%s: bad packet\n", state->lirc_prog);
1995  goto lirc_read_string_error;
1996  }
1997 
1998  FD_ZERO(&fds);
1999  FD_SET(fd,&fds);
2000  tv.tv_sec=LIRC_TIMEOUT;
2001  tv.tv_usec=0;
2002  do
2003  {
2004  ret=select(fd+1,&fds,NULL,NULL,&tv);
2005  }
2006  while(ret==-1 && errno==EINTR);
2007  if(ret==-1)
2008  {
2009  lirc_printf(state, "%s: select() failed\n", state->lirc_prog);
2010  lirc_perror(state, state->lirc_prog);
2011  goto lirc_read_string_error;
2012  }
2013  else if(ret==0)
2014  {
2015  lirc_printf(state, "%s: timeout\n", state->lirc_prog);
2016  goto lirc_read_string_error;
2017  }
2018 
2019  n=read(fd, buffer+tail, LIRC_PACKET_SIZE-tail);
2020  if(n<=0)
2021  {
2022  lirc_printf(state, "%s: read() failed\n", state->lirc_prog);
2023  lirc_perror(state, state->lirc_prog);
2024  goto lirc_read_string_error;
2025  }
2026  buffer[tail+n]=0;
2027  tail+=n;
2028  end=strchr(buffer,'\n');
2029  }
2030 
2031  end[0]=0;
2032  head=strlen(buffer)+1;
2033  return(buffer);
2034 
2035  lirc_read_string_error:
2036  head=tail=0;
2037  buffer[0]=0;
2038  return(NULL);
2039 }
2040 
2041 int lirc_send_command(const struct lirc_state *lstate, int sockfd, const char *command, char *buf, size_t *buf_len, int *ret_status)
2042 {
2043  int done,todo;
2044  const char *string,*data;
2045  char *endptr;
2046  enum packet_state state;
2047  int status;
2048  unsigned long n, data_n=0;
2049  size_t written=0, max=0, len;
2050 
2051  if(buf_len!=NULL)
2052  {
2053  max=*buf_len;
2054  }
2055  todo=strlen(command);
2056  data=command;
2057  while(todo>0)
2058  {
2059  done=write(sockfd,(const void *) data,todo);
2060  if(done<0)
2061  {
2062  lirc_printf(lstate, "%s: could not send packet\n",
2063  lstate->lirc_prog);
2064  lirc_perror(lstate, lstate->lirc_prog);
2065  return(-1);
2066  }
2067  data+=done;
2068  todo-=done;
2069  }
2070 
2071  /* get response */
2072  status=LIRC_RET_SUCCESS;
2073  state=P_BEGIN;
2074  n=0;
2075  while(1)
2076  {
2077  string=lirc_read_string(lstate, sockfd);
2078  if(string==NULL) return(-1);
2079  switch(state)
2080  {
2081  case P_BEGIN:
2082  if(strcasecmp(string,"BEGIN")!=0)
2083  {
2084  continue;
2085  }
2086  state=P_MESSAGE;
2087  break;
2088  case P_MESSAGE:
2089  if(strncasecmp(string,command,strlen(string))!=0 ||
2090  strlen(string)+1!=strlen(command))
2091  {
2092  state=P_BEGIN;
2093  continue;
2094  }
2095  state=P_STATUS;
2096  break;
2097  case P_STATUS:
2098  if(strcasecmp(string,"SUCCESS")==0)
2099  {
2100  status=LIRC_RET_SUCCESS;
2101  }
2102  else if(strcasecmp(string,"END")==0)
2103  {
2104  status=LIRC_RET_SUCCESS;
2105  goto good_packet;
2106  }
2107  else if(strcasecmp(string,"ERROR")==0)
2108  {
2109  lirc_printf(lstate, "%s: command failed: %s",
2110  lstate->lirc_prog, command);
2111  status=LIRC_RET_ERROR;
2112  }
2113  else
2114  {
2115  goto bad_packet;
2116  }
2117  state=P_DATA;
2118  break;
2119  case P_DATA:
2120  if(strcasecmp(string,"END")==0)
2121  {
2122  goto good_packet;
2123  }
2124  else if(strcasecmp(string,"DATA")==0)
2125  {
2126  state=P_N;
2127  break;
2128  }
2129  goto bad_packet;
2130  case P_N:
2131  errno=0;
2132  data_n=strtoul(string,&endptr,0);
2133  if(!*string || *endptr)
2134  {
2135  goto bad_packet;
2136  }
2137  if(data_n==0)
2138  {
2139  state=P_END;
2140  }
2141  else
2142  {
2143  state=P_DATA_N;
2144  }
2145  break;
2146  case P_DATA_N:
2147  len=strlen(string);
2148  if(buf!=NULL && written+len+1<max)
2149  {
2150  memcpy(buf+written, string, len+1);
2151  }
2152  written+=len+1;
2153  n++;
2154  if(n==data_n) state=P_END;
2155  break;
2156  case P_END:
2157  if(strcasecmp(string,"END")==0)
2158  {
2159  goto good_packet;
2160  }
2161  goto bad_packet;
2162  break;
2163  }
2164  }
2165 
2166  /* never reached */
2167 
2168  bad_packet:
2169  lirc_printf(lstate, "%s: bad return packet\n", lstate->lirc_prog);
2170  return(-1);
2171 
2172  good_packet:
2173  if(ret_status!=NULL)
2174  {
2175  *ret_status=status;
2176  }
2177  if(buf_len!=NULL)
2178  {
2179  *buf_len=written;
2180  }
2181  return (int) data_n;
2182 }
2183 
2184 int lirc_identify(const struct lirc_state *state, int sockfd)
2185 {
2186  char* command = malloc((10+strlen(state->lirc_prog)+1+1) * sizeof(char));
2187  if (command == NULL)
2188  return LIRC_RET_ERROR;
2189  int success;
2190 
2191  sprintf(command, "IDENT %s\n", state->lirc_prog);
2192 
2193  (void) lirc_send_command(state, sockfd, command, NULL, NULL, &success);
2194  free(command);
2195  return success;
2196 }
static void lirc_parse_include(char *s, const char *name, int line)
Definition: lirc_client.c:437
static FILE * lirc_open(const struct lirc_state *state, const char *file, const char *current_file, char **full_name)
Definition: lirc_client.c:764
char * lircrc_user_file
Definition: lirc_client.h:48
def write(text, progress=True)
Definition: mythburn.py:279
int lirc_deinit(struct lirc_state *state)
Definition: lirc_client.c:208
struct lirc_code * next_code
Definition: lirc_client.h:85
struct lirc_state * lirc_init(const char *lircrc_root_file, const char *lircrc_user_file, const char *prog, const char *lircd, int verbose)
Definition: lirc_client.c:134
Definition: lirc_client.h:73
stderr
Definition: ttvdb.py:1426
struct exc__state * last
Definition: pxsup2dast.c:98
const char * lirc_getmode(const struct lirc_state *state, struct lirc_config *config)
Definition: lirc_client.c:1898
def scan(profile, smoonURL, gate)
Definition: scan.py:43
#define MAX_INCLUDES
Definition: lirc_client.c:37
int lirc_nextcode(struct lirc_state *state, char **code)
Definition: lirc_client.c:1829
char * name
Definition: lirc_client.c:46
#define NULL
Definition: H264Parser.h:62
static int lirc_send_command(const struct lirc_state *state, int sockfd, const char *command, char *buf, size_t *buf_len, int *ret_status)
Definition: lirc_client.c:2041
struct lirc_config_entry * next
Definition: lirc_client.h:87
static void lirc_perror(const struct lirc_state *, const char *s)
Definition: lirc_client.c:127
static char * lirc_startupmode(const struct lirc_state *state, struct lirc_config_entry *first)
Definition: lirc_client.c:1384
char * change_mode
Definition: lirc_client.h:80
static int lirc_readline(const struct lirc_state *state, char **line, FILE *f)
Definition: lirc_client.c:239
static char * lirc_getfilename(const struct lirc_state *state, const char *file, const char *current_file)
Definition: lirc_client.c:691
int lirc_code2char(const struct lirc_state *state, struct lirc_config *config, char *code, char **string)
Definition: lirc_client.c:1678
static char lirc_parse_escape(const struct lirc_state *state, char **s, const char *name, int line)
Definition: lirc_client.c:303
struct filestack_t * parent
Definition: lirc_client.c:48
static struct filestack_t * stack_push(const struct lirc_state *state, struct filestack_t *parent)
Definition: lirc_client.c:823
int lirc_readconfig_only(const struct lirc_state *state, const char *file, struct lirc_config **config, int(check)(char *s))
Definition: lirc_client.c:968
#define LIRC_ALL
Definition: lirc_client.h:30
struct lirc_code * next
Definition: lirc_client.h:61
char * string
Definition: lirc_client.h:53
size_t lirc_getsocketname(const char *filename, char *buf, size_t size)
Definition: lirc_client.c:1888
def read(device=None, features=[])
Definition: disc.py:35
static char * lirc_trim(char *s)
Definition: lirc_client.c:286
packet_state
Definition: lirc_client.c:51
struct lirc_list * next
Definition: lirc_client.h:54
static int lirc_readconfig_only_internal(const struct lirc_state *state, const char *file, struct lirc_config **config, int(check)(char *s), char **full_name, char **sha_bang)
Definition: lirc_client.c:976
#define LIRC_TIMEOUT
Definition: lirc_client.c:41
int lirc_verbose
Definition: lirc_client.h:44
#define close
Definition: compat.h:16
struct lirc_config_entry * first
Definition: lirc_client.h:68
static void lirc_parse_string(const struct lirc_state *state, char *s, const char *name, int line)
Definition: lirc_client.c:414
unsigned int rep
Definition: lirc_client.h:78
unsigned char t
Definition: ParseText.cpp:340
struct lirc_list * next_config
Definition: lirc_client.h:84
static int lirc_code2char_internal(const struct lirc_state *state, struct lirc_config *config, char *code, char **string, char **prog)
Definition: lirc_client.c:1728
int lirc_readconfig(const struct lirc_state *state, const char *file, struct lirc_config **config, int(check)(char *s))
Definition: lirc_client.c:860
const char * name
Definition: ParseText.cpp:339
int FILE
Definition: mythburn.py:110
char * lirc_prog
Definition: lirc_client.h:45
#define LIRC_RET_SUCCESS
Definition: lirc_client.h:27
static unsigned int lirc_flags(const struct lirc_state *state, char *string)
Definition: lirc_client.c:653
static void lirc_freeconfigentries(struct lirc_config_entry *first)
Definition: lirc_client.c:1452
#define LIRC_PACKET_SIZE
Definition: lirc_client.c:39
#define LIRC_READ
Definition: lirc_client.c:38
static char * lirc_execute(const struct lirc_state *state, struct lirc_config *config, struct lirc_config_entry *scan)
Definition: lirc_client.c:1515
PictureAttribute next(PictureAttributeSupported supported, PictureAttribute attribute)
char * lirc_buffer
Definition: lirc_client.h:46
struct lirc_code * code
Definition: lirc_client.h:76
static void lirc_clearmode(struct lirc_config *config)
Definition: lirc_client.c:1491
void lirc_freeconfig(struct lirc_config *config)
Definition: lirc_client.c:1437
char * lircrc_root_file
Definition: lirc_client.h:47
#define LIRC_RET_ERROR
Definition: lirc_client.h:28
int lirc_code2charprog(struct lirc_state *state, struct lirc_config *config, char *code, char **string, char **prog)
Definition: lirc_client.c:1713
static void stack_free(struct filestack_t *entry)
Definition: lirc_client.c:852
int lirc_lircd
Definition: lirc_client.h:43
#define PACKET_SIZE
Definition: lirc_client.c:1808
static int lirc_identify(const struct lirc_state *state, int sockfd)
Definition: lirc_client.c:2184
static const char * lirc_read_string(const struct lirc_state *state, int fd)
Definition: lirc_client.c:1963
static int lirc_mode(const struct lirc_state *state, const char *token, const char *token2, char **mode, struct lirc_config_entry **new_config, struct lirc_config_entry **first_config, struct lirc_config_entry **last_config, int(check)(char *s), const char *name, int line)
Definition: lirc_client.c:466
char * prog
Definition: lirc_client.h:75
char * button
Definition: lirc_client.h:60
FILE * file
Definition: lirc_client.c:45
char * mode
Definition: lirc_client.h:83
static void lirc_printf(const struct lirc_state *, const char *format_str,...)
Definition: lirc_client.c:116
static struct filestack_t * stack_pop(struct filestack_t *entry)
Definition: lirc_client.c:839
unsigned int rep_delay
Definition: lirc_client.h:77
static int lirc_iscode(struct lirc_config_entry *scan, char *remote, char *button, unsigned int rep)
Definition: lirc_client.c:1556
unsigned int flags
Definition: lirc_client.h:81
const char * lirc_setmode(const struct lirc_state *state, struct lirc_config *config, const char *mode)
Definition: lirc_client.c:1925
char * remote
Definition: lirc_client.h:59
struct lirc_list * config
Definition: lirc_client.h:79