33 #include <sys/param.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
44 static const struct timeval CMD_TIMEOUT = {.tv_sec = 1, .tv_usec = 0};
48 #define logprintf(level,fmt,args...) syslog(level, fmt, ## args)
49 #define LIRC_WARNING LOG_WARNING
50 #define LIRC_DEBUG LOG_DEBUG
51 #define LIRC_NOTICE LOG_NOTICE
52 #define LIRC_ERROR LOG_ERR
55 #define MAX_INCLUDES 10
57 #define LIRC_PACKET_SIZE 255
59 #define LIRC_TIMEOUT 3
66 struct filestack_t *parent;
87 unsigned int lirc_flags(
char *
string);
89 static int lirc_lircd;
90 static int lirc_verbose = 0;
91 static char *lirc_prog = NULL;
92 static char *lirc_buffer = NULL;
98 chk_write(
int fd,
const void *buf,
size_t count,
const char* msg)
100 if (write(fd, buf, count) == -1) {
116 logprintf(LIRC_NOTICE,
"Message too big: %s", ctx->
packet);
137 (
const void *)&CMD_TIMEOUT,
138 sizeof(CMD_TIMEOUT));
141 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR){
142 logprintf(LIRC_NOTICE,
"fill_string: timeout\n");
155 static int read_string(
lirc_cmd_ctx* cmd,
int fd,
const char**
string)
166 if (cmd->
next == NULL || strchr(cmd->
next,
'\n') == NULL) {
167 r = fill_string(fd, cmd);
173 cmd->
next = strchr(cmd->
next,
'\n');
174 if (cmd->
next != NULL) {
185 const char*
string = NULL;
192 todo = strlen(ctx->
packet);
194 logprintf(LIRC_DEBUG,
"lirc_command_run: Sending: %s", data);
196 done = write(fd, (
void *)data, todo);
198 logprintf(LIRC_WARNING,
199 "%s: could not send packet\n", prog);
213 r = read_string(ctx, fd, &
string);
214 }
while (r == EAGAIN);
215 if (strlen(
string) == 0) {
218 logprintf(LIRC_DEBUG,
219 "lirc_command_run, state: %d, input: \"%s\"\n",
220 state,
string ?
string :
"(Null)");
223 if (strcasecmp(
string,
"BEGIN") != 0) {
229 if (strncasecmp(
string, ctx->
packet, strlen(
string)) != 0
230 || strlen(
string) + 1 != strlen(ctx->
packet))
238 if (strcasecmp(
string,
"SUCCESS") == 0) {
240 }
else if (strcasecmp(
string,
"END") == 0) {
241 logprintf(LIRC_NOTICE,
242 "lirc_command_run: status:END");
244 }
else if (strcasecmp(
string,
"ERROR") == 0) {
245 logprintf(LIRC_WARNING,
"%s: command failed: %s",
254 if (strcasecmp(
string,
"END") == 0) {
255 logprintf(LIRC_NOTICE,
256 "lirc_command_run: data:END, status:%d",
259 }
else if (strcasecmp(
string,
"DATA") == 0) {
263 logprintf(LIRC_DEBUG,
264 "data: bad packet: %s\n",
269 data_n = (__u32) strtoul(
string, &endptr, 0);
270 if (!*
string || *endptr) {
284 strcpy(ctx->
reply,
"");
301 if (strcasecmp(
string,
"END") == 0) {
302 logprintf(LIRC_NOTICE,
303 "lirc_command_run: status:END, status:%d",
312 logprintf(LIRC_WARNING,
"%s: bad return packet\n", prog);
313 logprintf(LIRC_DEBUG,
"State %d: bad packet: %s\n", status,
string);
318 static void lirc_printf(
const char *format_str, ...)
325 va_start(ap, format_str);
326 vfprintf(stderr, format_str, ap);
331 static void lirc_perror(
const char *s)
342 if (prog == NULL || lirc_prog != NULL){
346 if (lirc_lircd >= 0) {
347 lirc_verbose = verbose;
348 lirc_prog = strdup(prog);
349 if (lirc_prog == NULL) {
350 lirc_printf(
"%s: out of memory\n", prog);
355 lirc_printf(
"%s: could not open socket: %s\n",
357 strerror(-lirc_lircd));
366 if (lirc_prog != NULL) {
370 if (lirc_buffer != NULL) {
374 return (close(lirc_lircd));
378 static int lirc_readline(
char **line, FILE * f)
380 char *newline, *ret, *enlargeline;
383 newline = (
char *)malloc(LIRC_READ + 1);
384 if (newline == NULL) {
385 lirc_printf(
"%s: out of memory\n", lirc_prog);
390 ret = fgets(newline + len, LIRC_READ + 1, f);
392 if (feof(f) && len > 0) {
400 len = strlen(newline);
401 if (newline[len - 1] ==
'\n') {
402 newline[len - 1] = 0;
407 enlargeline = (
char *)realloc(newline, len + 1 + LIRC_READ);
408 if (enlargeline == NULL) {
410 lirc_printf(
"%s: out of memory\n", lirc_prog);
413 newline = enlargeline;
418 static char *lirc_trim(
char *s)
422 while (s[0] ==
' ' || s[0] ==
'\t')
427 if (s[len] ==
' ' || s[len] ==
'\t')
437 static char lirc_parse_escape(
char **s,
const char *name,
int line)
441 unsigned int i, overflow, count;
442 int digits_found, digit;
482 while (++count < 3) {
484 if (c >=
'0' && c <=
'7') {
485 i = (i << 3) + c -
'0';
491 if (i > (1 << CHAR_BIT) - 1) {
492 i &= (1 << CHAR_BIT) - 1;
493 lirc_printf(
"%s: octal escape sequence out of range in %s:%d\n", lirc_prog, name, line);
503 if (c >=
'0' && c <=
'9')
505 else if (c >=
'a' && c <=
'f')
506 digit = c -
'a' + 10;
507 else if (c >=
'A' && c <=
'F')
508 digit = c -
'A' + 10;
513 overflow |= i ^ (i << 4 >> 4);
514 i = (i << 4) + digit;
518 lirc_printf(
"%s: \\x used with no "
519 "following hex digits in %s:%d\n", lirc_prog, name, line);
521 if (overflow || i > (1 << CHAR_BIT) - 1) {
522 i &= (1 << CHAR_BIT) - 1;
523 lirc_printf(
"%s: hex escape sequence out "
524 "of range in %s:%d\n", lirc_prog, name, line);
529 if (c >=
'@' && c <=
'Z')
536 static void lirc_parse_string(
char *s,
const char *name,
int line)
544 *t = lirc_parse_escape(&s, name, line);
556 static void lirc_parse_include(
char *s,
const char *name,
int line)
566 if (*s !=
'"' && *s !=
'<') {
569 if (*s ==
'"' && last !=
'"') {
571 }
else if (*s ==
'<' && last !=
'>') {
575 memmove(s, s + 1, len - 2 + 1);
579 int lirc_mode(
char *token,
char *token2,
char **mode,
583 int (check) (
char *s),
589 new_entry = *new_config;
590 if (strcasecmp(token,
"begin") == 0) {
591 if (token2 == NULL) {
592 if (new_entry == NULL) {
595 if (new_entry == NULL) {
596 lirc_printf(
"%s: out of memory\n", lirc_prog);
599 new_entry->prog = NULL;
600 new_entry->code = NULL;
601 new_entry->rep_delay = 0;
602 new_entry->ign_first_events = 0;
604 new_entry->config = NULL;
605 new_entry->change_mode = NULL;
606 new_entry->flags = none;
607 new_entry->mode = NULL;
608 new_entry->next_config = NULL;
609 new_entry->next_code = NULL;
610 new_entry->next = NULL;
612 *new_config = new_entry;
615 lirc_printf(
"%s: bad file format, %s:%d\n", lirc_prog, name, line);
619 if (new_entry == NULL && *mode == NULL) {
620 *mode = strdup(token2);
625 lirc_printf(
"%s: bad file format, %s:%d\n", lirc_prog, name, line);
629 }
else if (strcasecmp(token,
"end") == 0) {
630 if (token2 == NULL) {
631 if (new_entry != NULL) {
633 if (new_entry->prog == NULL) {
634 lirc_printf(
"%s: prog missing in config before line %d\n", lirc_prog, line);
635 lirc_freeconfigentries(new_entry);
639 if (strcasecmp(new_entry->prog, lirc_prog) != 0) {
640 lirc_freeconfigentries(new_entry);
645 new_entry->next_code = new_entry->code;
646 new_entry->next_config = new_entry->config;
647 if (*last_config == NULL) {
648 *first_config = new_entry;
649 *last_config = new_entry;
651 (*last_config)->next = new_entry;
652 *last_config = new_entry;
657 new_entry->mode = strdup(*mode);
658 if (new_entry->mode == NULL) {
659 lirc_printf(
"%s: out of memory\n", lirc_prog);
665 new_entry->prog != NULL && strcasecmp(new_entry->prog, lirc_prog) == 0) {
668 list = new_entry->config;
669 while (list != NULL) {
670 if (check(list->string) == -1) {
677 if (new_entry->rep_delay == 0 && new_entry->rep > 0) {
678 new_entry->rep_delay = new_entry->rep - 1;
681 lirc_printf(
"%s: %s:%d: 'end' without 'begin'\n", lirc_prog, name, line);
686 if (new_entry != NULL) {
687 lirc_printf(
"%s: %s:%d: missing 'end' token\n", lirc_prog, name, line);
690 if (strcasecmp(*mode, token2) == 0) {
694 lirc_printf(
"%s: \"%s\" doesn't "
695 "match mode \"%s\"\n", lirc_prog, token2, *mode);
699 lirc_printf(
"%s: %s:%d: 'end %s' without 'begin'\n", lirc_prog, name, line, token2);
704 lirc_printf(
"%s: unknown token \"%s\" in %s:%d ignored\n", lirc_prog, token, name, line);
710 unsigned int lirc_flags(
char *
string)
716 s = strtok(
string,
" \t|");
718 if (strcasecmp(s,
"once") == 0) {
720 }
else if (strcasecmp(s,
"quit") == 0) {
722 }
else if (strcasecmp(s,
"mode") == 0) {
724 }
else if (strcasecmp(s,
"startup_mode") == 0) {
725 flags |= startup_mode;
726 }
else if (strcasecmp(s,
"toggle_reset") == 0) {
727 flags |= toggle_reset;
729 lirc_printf(
"%s: unknown flag \"%s\"\n", lirc_prog, s);
731 s = strtok(NULL,
" \t");
746 static char* get_homepath(
void)
751 filename = malloc(MAXPATHLEN);
752 if (filename == NULL) {
753 lirc_printf(
"%s: out of memory\n", lirc_prog);
756 home = getenv(
"HOME");
757 home = home == NULL ?
"/" : home;
758 strncpy(filename, home, MAXPATHLEN);
759 if (filename[strlen(filename) - 1] ==
'/') {
760 filename[strlen(filename) - 1] =
'\0';
771 static char* get_freedesktop_path()
775 if (getenv(
"XDG_CONFIG_HOME") != NULL) {
776 path = malloc(MAXPATHLEN);
777 strncpy(path, getenv(
"XDG_CONFIG_HOME"), MAXPATHLEN);
778 strncat(path,
"/", MAXPATHLEN - strlen(path));
779 strncat(path,
CFG_LIRCRC, MAXPATHLEN - strlen(path));
781 path = get_homepath();
785 strncat(path,
"/.config/lircrc", MAXPATHLEN - strlen(path));
787 if (access(path, R_OK) != 0) {
794 static char *lirc_getfilename(
const char *file,
const char *current_file)
799 filename = get_freedesktop_path();
800 if (filename == NULL) {
802 }
else if (strlen(filename) == 0) {
804 filename = get_homepath();
805 if (filename == NULL) {
810 filename = realloc(filename, strlen(filename) + 1);
811 }
else if (strncmp(file,
"~/", 2) == 0) {
812 filename = get_homepath();
813 if (filename == NULL) {
816 strcat(filename, file + 1);
817 filename = realloc(filename, strlen(filename) + 1);
818 }
else if (file[0] ==
'/' || current_file == NULL) {
820 filename = strdup(file);
821 if (filename == NULL) {
822 lirc_printf(
"%s: out of memory\n", lirc_prog);
827 int pathlen = strlen(current_file);
828 while (pathlen > 0 && current_file[pathlen - 1] !=
'/')
830 filename = (
char *)malloc(pathlen + strlen(file) + 1);
831 if (filename == NULL) {
832 lirc_printf(
"%s: out of memory\n", lirc_prog);
835 memcpy(filename, current_file, pathlen);
836 filename[pathlen] = 0;
837 strcat(filename, file);
843 static FILE *lirc_open(
const char *file,
const char *current_file,
char **full_name)
848 filename = lirc_getfilename(file, current_file);
849 if (filename == NULL) {
853 fin = fopen(filename,
"r");
854 if (fin == NULL && (file != NULL || errno != ENOENT)) {
855 lirc_printf(
"%s: could not open config file %s\n", lirc_prog, filename);
856 lirc_perror(lirc_prog);
857 }
else if (fin == NULL) {
859 fin = fopen(root_file,
"r");
860 if (fin == NULL && errno == ENOENT) {
861 int save_errno = errno;
863 fin = fopen(root_file,
"r");
866 if (fin == NULL && errno != ENOENT) {
867 lirc_printf(
"%s: could not open config file %s\n", lirc_prog,
LIRCRC_ROOT_FILE);
868 lirc_perror(lirc_prog);
869 }
else if (fin == NULL) {
870 lirc_printf(
"%s: could not open config files "
872 lirc_perror(lirc_prog);
875 filename = strdup(root_file);
876 if (filename == NULL) {
878 lirc_printf(
"%s: out of memory\n", lirc_prog);
883 if (full_name && fin != NULL) {
884 *full_name = filename;
892 static struct filestack_t *stack_push(
struct filestack_t *parent)
894 struct filestack_t *entry;
895 entry = malloc(
sizeof(
struct filestack_t));
897 lirc_printf(
"%s: out of memory\n", lirc_prog);
903 entry->parent = parent;
908 static struct filestack_t *stack_pop(
struct filestack_t *entry)
910 struct filestack_t *parent = NULL;
912 parent = entry->parent;
921 static void stack_free(
struct filestack_t *entry)
924 entry = stack_pop(entry);
937 while (scan != NULL) {
938 if (scan->flags & startup_mode) {
939 if (scan->change_mode != NULL) {
940 startupmode = scan->change_mode;
942 scan->change_mode = NULL;
945 lirc_printf(
"%s: startup_mode flags requires 'mode ='\n", lirc_prog);
952 if (startupmode == NULL) {
954 while (scan != NULL) {
955 if (scan->mode != NULL && strcasecmp(lirc_prog, scan->mode) == 0) {
956 startupmode = lirc_prog;
963 if (startupmode == NULL)
966 while (scan != NULL) {
967 if (scan->change_mode != NULL && scan->flags & once && strcasecmp(startupmode, scan->change_mode) == 0) {
972 return (startupmode);
987 free(c->change_mode);
992 while (code != NULL) {
993 if (code->remote != NULL && code->remote != LIRC_ALL)
995 if (code->button != NULL && code->button != LIRC_ALL)
997 code_temp = code->next;
1003 while (list != NULL) {
1006 list_temp = list->next;
1010 config_temp = c->next;
1018 parse_shebang(
char* line,
int depth,
const char* path,
char* buff,
size_t size)
1022 const char*
const SHEBANG_MSG =
1023 "Warning: Use of deprecated lircrc shebang."
1024 " Use lircrc_class instead.\n";
1026 token = strtok(line,
"#! ");
1029 lirc_printf(
"Warning: ignoring shebang in included file.");
1032 if (strcmp(token,
"lircrc") == 0) {
1033 strncpy(my_path, path,
sizeof(my_path) - 1);
1034 strncat(buff, basename(my_path), size - 1);
1035 lirc_printf(SHEBANG_MSG);
1037 lirc_printf(
"Warning: bad shebang (ignored)");
1042 static int lirc_readconfig_only_internal(
const char *file,
1044 int (check) (
char *s),
char **full_name)
1046 const char*
const INCLUDED_LIRCRC_CLASS =
1047 "Warning: lirc_class in included file (ignored)";
1048 char *string, *eq, *token, *token2, *token3;
1049 struct filestack_t *filestack, *stack_tmp;
1051 char lircrc_class[128] = {
'\0'};
1053 char *mode, *remote;
1056 char *save_full_name = NULL;
1058 filestack = stack_push(NULL);
1059 if (filestack == NULL) {
1062 filestack->file = lirc_open(file, NULL, &(filestack->name));
1063 if (filestack->file == NULL) {
1064 stack_free(filestack);
1067 filestack->line = 0;
1070 first = new_entry = last = NULL;
1074 if ((ret = lirc_readline(&
string, filestack->file)) == -1 ||
string == NULL) {
1075 fclose(filestack->file);
1076 if (open_files == 1 && full_name != NULL) {
1077 save_full_name = filestack->name;
1078 filestack->name = NULL;
1080 filestack = stack_pop(filestack);
1087 if (strncmp(
string,
"#!", 2) == 0) {
1088 parse_shebang(
string,
1092 sizeof(lircrc_class));
1096 eq = strchr(
string,
'=');
1098 token = strtok(
string,
" \t");
1099 if (token == NULL) {
1101 }
else if (token[0] ==
'#') {
1103 }
else if (strcasecmp(token,
"lircrc_class") == 0) {
1104 token2 = lirc_trim(strtok(NULL,
""));
1105 if (strlen(token2) == 0) {
1107 "Warning: no lircrc_class");
1108 }
else if (open_files == 1) {
1109 strncat(lircrc_class,
1111 sizeof(lircrc_class) - 1);
1113 lirc_printf(INCLUDED_LIRCRC_CLASS);
1115 }
else if (strcasecmp(token,
"include") == 0) {
1116 if (open_files >= MAX_INCLUDES) {
1117 lirc_printf(
"%s: too many files "
1118 "included at %s:%d\n", lirc_prog, filestack->name, filestack->line);
1121 token2 = strtok(NULL,
"");
1122 token2 = lirc_trim(token2);
1123 lirc_parse_include(token2, filestack->name, filestack->line);
1124 stack_tmp = stack_push(filestack);
1125 if (stack_tmp == NULL) {
1129 lirc_open(token2, filestack->name, &(stack_tmp->name));
1130 stack_tmp->line = 0;
1131 if (stack_tmp->file) {
1133 filestack = stack_tmp;
1135 stack_pop(stack_tmp);
1141 token2 = strtok(NULL,
" \t");
1142 if (token2 != NULL && (token3 = strtok(NULL,
" \t")) != NULL) {
1143 lirc_printf(
"%s: unexpected token in line %s:%d\n",
1144 lirc_prog, filestack->name, filestack->line);
1146 ret = lirc_mode(token, token2, &mode,
1147 &new_entry, &first, &last,
1148 check, filestack->name, filestack->line);
1150 if (remote != LIRC_ALL)
1158 if (new_entry != NULL) {
1159 lirc_freeconfigentries(new_entry);
1167 token = lirc_trim(
string);
1168 token2 = lirc_trim(eq + 1);
1169 if (token[0] ==
'#') {
1171 }
else if (new_entry == NULL) {
1172 lirc_printf(
"%s: bad file format, %s:%d\n",
1173 lirc_prog, filestack->name, filestack->line);
1176 token2 = strdup(token2);
1177 if (token2 == NULL) {
1178 lirc_printf(
"%s: out of memory\n", lirc_prog);
1180 }
else if (strcasecmp(token,
"prog") == 0) {
1181 if (new_entry->prog != NULL)
1182 free(new_entry->prog);
1183 new_entry->prog = token2;
1184 }
else if (strcasecmp(token,
"remote") == 0) {
1185 if (remote != LIRC_ALL)
1188 if (strcasecmp(
"*", token2) == 0) {
1194 }
else if (strcasecmp(token,
"button") == 0) {
1201 lirc_printf(
"%s: out of memory\n", lirc_prog);
1204 code->remote = remote;
1205 if (strcasecmp(
"*", token2) == 0) {
1206 code->button = LIRC_ALL;
1209 code->button = token2;
1213 if (new_entry->code == NULL) {
1214 new_entry->code = code;
1216 new_entry->next_code->next = code;
1218 new_entry->next_code = code;
1219 if (remote != LIRC_ALL) {
1220 remote = strdup(remote);
1221 if (remote == NULL) {
1222 lirc_printf(
"%s: out of memory\n", lirc_prog);
1227 }
else if (strcasecmp(token,
"delay") == 0) {
1231 new_entry->rep_delay = strtoul(token2, &end, 0);
1232 if ((new_entry->rep_delay == ULONG_MAX && errno == ERANGE)
1233 || end[0] != 0 || strlen(token2) == 0) {
1234 lirc_printf(
"%s: \"%s\" not"
1235 " a valid number for delay\n", lirc_prog, token2);
1238 }
else if (strcasecmp(token,
"ignore_first_events") == 0) {
1242 new_entry->ign_first_events = strtoul(token2, &end, 0);
1243 if ((new_entry->ign_first_events == ULONG_MAX && errno == ERANGE)
1244 || end[0] != 0 || strlen(token2) == 0) {
1245 lirc_printf(
"%s: \"%s\" not"
1246 " a valid number for ignore_first_events\n", lirc_prog, token2);
1249 }
else if (strcasecmp(token,
"repeat") == 0) {
1253 new_entry->rep = strtoul(token2, &end, 0);
1254 if ((new_entry->rep == ULONG_MAX && errno == ERANGE)
1255 || end[0] != 0 || strlen(token2) == 0) {
1256 lirc_printf(
"%s: \"%s\" not"
1257 " a valid number for repeat\n", lirc_prog, token2);
1260 }
else if (strcasecmp(token,
"config") == 0) {
1265 if (new_list == NULL) {
1267 lirc_printf(
"%s: out of memory\n", lirc_prog);
1270 lirc_parse_string(token2, filestack->name, filestack->line);
1271 new_list->string = token2;
1272 new_list->next = NULL;
1273 if (new_entry->config == NULL) {
1274 new_entry->config = new_list;
1276 new_entry->next_config->next = new_list;
1278 new_entry->next_config = new_list;
1280 }
else if (strcasecmp(token,
"mode") == 0) {
1281 if (new_entry->change_mode != NULL)
1282 free(new_entry->change_mode);
1283 new_entry->change_mode = token2;
1284 }
else if (strcasecmp(token,
"flags") == 0) {
1285 new_entry->flags = lirc_flags(token2);
1289 lirc_printf(
"%s: unknown token \"%s\" in %s:%d ignored\n",
1290 lirc_prog, token, filestack->name, filestack->line);
1298 if (remote != LIRC_ALL)
1300 if (new_entry != NULL) {
1302 ret = lirc_mode(
"end", NULL, &mode, &new_entry, &first, &last, check,
"", 0);
1303 lirc_printf(
"%s: warning: end token missing at end of file\n", lirc_prog);
1305 lirc_freeconfigentries(new_entry);
1311 lirc_printf(
"%s: warning: no end token found for mode \"%s\"\n", lirc_prog, mode);
1320 if (*config == NULL) {
1321 lirc_printf(
"%s: out of memory\n", lirc_prog);
1322 lirc_freeconfigentries(first);
1325 (*config)->first = first;
1326 (*config)->next = first;
1327 startupmode = lirc_startupmode((*config)->first);
1328 (*config)->current_mode = startupmode ? strdup(startupmode) : NULL;
1329 if (lircrc_class[0] !=
'\0') {
1330 (*config)->lircrc_class = strdup(lircrc_class);
1332 (*config)->lircrc_class = NULL;
1334 (*config)->sockfd = -1;
1335 if (full_name != NULL) {
1336 *full_name = save_full_name;
1337 save_full_name = NULL;
1341 lirc_freeconfigentries(first);
1344 stack_free(filestack);
1346 if (save_full_name) {
1347 free(save_full_name);
1353 int lirc_identify(
int sockfd)
1363 }
while (ret == EAGAIN || ret == EWOULDBLOCK);
1371 struct sockaddr_un addr;
1378 if (lirc_readconfig_only_internal(file, config, check, &filename) == -1) {
1382 if ((*config)->lircrc_class == NULL) {
1383 goto lirc_readconfig_compat;
1388 addr.sun_family = AF_UNIX;
1391 sizeof(addr.sun_path)) >
sizeof(addr.sun_path))
1393 lirc_printf(
"%s: WARNING: file name too long\n", lirc_prog);
1394 goto lirc_readconfig_compat;
1396 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1398 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1399 lirc_perror(lirc_prog);
1400 goto lirc_readconfig_compat;
1402 if (connect(sockfd, (
struct sockaddr *)&addr,
sizeof(addr)) != -1) {
1403 (*config)->sockfd = sockfd;
1407 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS) {
1419 snprintf(command,
sizeof(command),
1420 "lircrcd %s", (*config)->lircrc_class);
1421 ret = system(command);
1422 if (ret == -1 || WEXITSTATUS(ret) != EXIT_SUCCESS) {
1423 goto lirc_readconfig_compat;
1427 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1429 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1430 lirc_perror(lirc_prog);
1431 goto lirc_readconfig_compat;
1433 if (connect(sockfd, (
struct sockaddr *)&addr,
sizeof(addr)) != -1) {
1434 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS) {
1435 (*config)->sockfd = sockfd;
1443 lirc_readconfig_compat:
1454 return lirc_readconfig_only_internal(file, config, check, NULL);
1460 if (config != NULL) {
1461 if (config->sockfd != -1) {
1462 (void)close(config->sockfd);
1463 config->sockfd = -1;
1467 lirc_freeconfigentries(config->first);
1468 free(config->current_mode);
1474 static void lirc_clearmode(
struct lirc_config *config)
1478 if (config->current_mode == NULL) {
1481 scan = config->first;
1482 while (scan != NULL) {
1483 if (scan->change_mode != NULL) {
1484 if (strcasecmp(scan->change_mode, config->current_mode) == 0) {
1485 scan->flags &= ~ecno;
1490 free(config->current_mode);
1491 config->current_mode = NULL;
1500 if (scan->flags & mode) {
1501 lirc_clearmode(config);
1503 if (scan->change_mode != NULL) {
1504 free(config->current_mode);
1505 config->current_mode = strdup(scan->change_mode);
1506 if (scan->flags & once) {
1507 if (scan->flags & ecno) {
1510 scan->flags |= ecno;
1514 if (scan->next_config != NULL &&
1515 scan->prog != NULL && (lirc_prog == NULL || strcasecmp(scan->prog, lirc_prog) == 0) && do_once == 1) {
1516 s = scan->next_config->string;
1517 scan->next_config = scan->next_config->next;
1518 if (scan->next_config == NULL)
1519 scan->next_config = scan->config;
1535 int delay_start, rep_delay;
1536 if (scan->ign_first_events) {
1537 if (scan->rep_delay && rep == 0)
1538 lirc_printf(
"%s: ignoring \"delay\" because \"ignore_first_events\" is also set\n",
1540 rep_delay = scan->ign_first_events;
1543 rep_delay = scan->rep_delay;
1547 if (rep < delay_start)
1550 if (scan->rep == 0 && rep_delay > 0 && rep == rep_delay + delay_start)
1553 if (scan->rep > 0 && rep >= rep_delay + delay_start) {
1554 rep -= rep_delay + delay_start;
1555 return ((rep % scan->rep) == 0);
1560 static int lirc_iscode(
struct lirc_config_entry *scan,
char *remote,
char *button,
int rep)
1565 if (scan->code == NULL) {
1566 return rep_filter(scan, rep);
1570 if (scan->next_code->remote == LIRC_ALL || strcasecmp(scan->next_code->remote, remote) == 0) {
1571 if (scan->next_code->button == LIRC_ALL || strcasecmp(scan->next_code->button, button) == 0) {
1574 if (scan->code->next == NULL || rep == 0) {
1575 scan->next_code = scan->next_code->next;
1576 if (scan->code->next != NULL) {
1581 if (scan->next_code == NULL) {
1582 scan->next_code = scan->code;
1583 if (scan->code->next != NULL || rep_filter(scan, rep))
1594 if (scan->flags & toggle_reset) {
1595 scan->next_config = scan->config;
1599 if (codes == scan->next_code)
1601 codes = codes->next;
1603 while (codes != scan->next_code->next) {
1609 while (next != scan->next_code) {
1610 if (prev->remote == LIRC_ALL || strcasecmp(prev->remote, next->remote) == 0) {
1611 if (prev->button == LIRC_ALL || strcasecmp(prev->button, next->button) == 0) {
1624 if (prev->remote == LIRC_ALL || strcasecmp(prev->remote, remote) == 0) {
1625 if (prev->button == LIRC_ALL || strcasecmp(prev->button, button) == 0) {
1627 scan->next_code = prev->next;
1633 codes = codes->next;
1635 scan->next_code = scan->code;
1642 static int warning = 1;
1646 fprintf(stderr,
"%s: warning: lirc_ir2char() is obsolete\n", lirc_prog);
1655 static int lirc_code2char_internal(
struct lirc_config *config,
char *code,
char **
string,
char **prog)
1659 char *remote, *button;
1666 if (sscanf(code,
"%*x %x %*s %*s\n", &rep) == 1) {
1667 backup = strdup(code);
1671 strtok(backup,
" ");
1673 button = strtok(NULL,
" ");
1674 remote = strtok(NULL,
"\n");
1676 if (button == NULL || remote == NULL) {
1681 scan = config->next;
1683 while (scan != NULL) {
1684 exec_level = lirc_iscode(scan, remote, button, rep);
1685 if (exec_level > 0 &&
1686 (scan->mode == NULL ||
1687 (scan->mode != NULL &&
1688 config->current_mode != NULL &&
1689 strcasecmp(scan->mode, config->current_mode) == 0)) && quit_happened == 0) {
1690 if (exec_level > 1) {
1691 s = lirc_execute(config, scan);
1692 if (s != NULL && prog != NULL) {
1698 if (scan->flags & quit) {
1700 config->next = NULL;
1703 }
else if (s != NULL) {
1704 config->next = scan->next;
1716 config->next = config->first;
1730 if (config->sockfd != -1) {
1733 }
while (ret == EAGAIN || ret == EWOULDBLOCK);
1736 *
string = static_buff;
1738 return ret == 0 ? 0 : -1;
1740 return lirc_code2char_internal(config, code,
string, NULL);
1744 int lirc_code2charprog(
struct lirc_config *config,
char *code,
char **
string,
char **prog)
1752 ret = lirc_code2char_internal(config, code,
string, prog);
1761 static int warning = 1;
1766 fprintf(stderr,
"%s: warning: lirc_nextir() is obsolete\n", lirc_prog);
1779 static int end_len = 0;
1784 if (lirc_buffer == NULL) {
1785 lirc_buffer = (
char *)malloc(packet_size + 1);
1786 if (lirc_buffer == NULL) {
1787 lirc_printf(
"%s: out of memory\n", lirc_prog);
1792 while ((end = strchr(lirc_buffer,
'\n')) == NULL) {
1793 if (end_len >= packet_size) {
1797 new_buffer = (
char *)realloc(lirc_buffer, packet_size + 1);
1798 if (new_buffer == NULL) {
1801 lirc_buffer = new_buffer;
1803 len = read(lirc_lircd, lirc_buffer + end_len, packet_size - end_len);
1805 if (len == -1 && errno == EAGAIN)
1811 lirc_buffer[end_len] = 0;
1813 if ((end = strchr(lirc_buffer,
'\n')) == NULL) {
1820 end_len = strlen(end);
1823 *code = strdup(lirc_buffer);
1825 memmove(lirc_buffer, end, end_len + 1);
1834 id =
id != NULL ?
id :
"default";
1835 snprintf(buf, size, VARRUNDIR
"/%d-%s-lircrcd.socket", getuid(),
id);
1847 if (config->sockfd != -1) {
1851 }
while (ret == EAGAIN || ret == EWOULDBLOCK);
1858 return config->current_mode;
1868 if (config->sockfd != -1) {
1879 }
while (r == EAGAIN || r == EWOULDBLOCK);
1886 free(config->current_mode);
1887 config->current_mode = mode ? strdup(mode) : NULL;
1888 return config->current_mode;
1904 }
while (r == EAGAIN);
1919 scancode, repeat, keysym, remote);
1925 }
while (r == EAGAIN);
1932 do_connect(
int domain,
struct sockaddr* addr,
size_t size,
int quiet)
1936 fd = socket(domain, SOCK_STREAM, 0);
1939 fprintf(stderr,
"do_connect: could not open socket\n");
1944 if (connect(fd, addr, size) == -1) {
1947 "do_connect: could not connect to socket\n");
1958 const char* socket_path;
1959 struct sockaddr_un addr_un;
1961 socket_path = path ? path : getenv(
"LIRC_SOCKET_PATH");
1962 socket_path = socket_path ? socket_path :
LIRCD;
1963 if (strlen(socket_path) + 1 >
sizeof(addr_un.sun_path)) {
1966 fprintf(stderr,
"%s: socket name is too long\n", prog);
1968 return -ENAMETOOLONG;
1970 addr_un.sun_family = AF_UNIX;
1971 strcpy(addr_un.sun_path, socket_path);
1972 return do_connect(AF_UNIX,
1973 (
struct sockaddr *) &addr_un,
1981 struct sockaddr_in addr_in;
1982 struct hostent* hostInfo;
1984 hostInfo = gethostbyname(address);
1985 if (hostInfo == NULL) {
1987 fprintf(stderr,
"get_remote_socket: host %s unknown\n",
1990 return -EADDRNOTAVAIL;
1992 addr_in.sin_family = hostInfo->h_addrtype;
1993 memcpy((
char *) &addr_in.sin_addr.s_addr,
1994 hostInfo->h_addr_list[0],
1995 hostInfo->h_length);
1997 return do_connect(hostInfo->h_addrtype,
1998 (
struct sockaddr *)&addr_in,
#define chk_write(fd, buf, count)
void lirc_command_reply_to_stdout(lirc_cmd_ctx *ctx)
int lirc_init(const char *prog, int verbose)
const char * lirc_setmode(struct lirc_config *config, const char *mode)
char reply[PACKET_SIZE+1]
int lirc_get_local_socket(const char *path, int quiet)
char buffer[PACKET_SIZE+1]
int lirc_command_run(lirc_cmd_ctx *ctx, int fd)
#define LIRCRC_OLD_ROOT_FILE
const char * lirc_getmode(struct lirc_config *config)
int lirc_simulate(int fd, const char *remote, const char *keysym, int scancode, int repeat)
size_t lirc_getsocketname(const char *id, char *buf, size_t size)
int lirc_command_init(lirc_cmd_ctx *ctx, const char *fmt,...)
int lirc_nextcode(char **code)
int lirc_get_remote_socket(const char *address, int port, int quiet)
int lirc_code2char(struct lirc_config *config, char *code, char **string)
int lirc_readconfig_only(const char *file, struct lirc_config **config, int(check)(char *s))
char packet[PACKET_SIZE+1]
int lirc_readconfig(const char *file, struct lirc_config **config, int(check)(char *s))
int lirc_send_one(int fd, const char *remote, const char *keysym)
void lirc_freeconfig(struct lirc_config *config)
3-rd party application interface.
char * lirc_ir2char(struct lirc_config *config, char *code)