32 #include <sys/param.h>
33 #include <sys/socket.h>
35 #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)
115 logprintf(LIRC_NOTICE,
"Message too big: %s", ctx->
packet);
136 (
const void*)&CMD_TIMEOUT,
137 sizeof(CMD_TIMEOUT));
140 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
141 logprintf(LIRC_NOTICE,
"fill_string: timeout\n");
153 static int read_string(
lirc_cmd_ctx* cmd,
int fd,
const char**
string)
167 if (cmd->
next == NULL || strchr(cmd->
next,
'\n') == NULL) {
168 r = fill_string(fd, cmd);
176 cmd->
next = strchr(cmd->
next,
'\n');
177 if (cmd->
next != NULL) {
188 const char*
string = NULL;
195 todo = strlen(ctx->
packet);
197 logprintf(LIRC_DEBUG,
"lirc_command_run: Sending: %s", data);
199 done = write(fd, (
void*)data, todo);
201 logprintf(LIRC_WARNING,
202 "%s: could not send packet\n", prog);
216 r = read_string(ctx, fd, &
string);
218 if (!
string || strlen(
string) == 0)
220 logprintf(LIRC_DEBUG,
221 "lirc_command_run, state: %d, input: \"%s\"\n",
222 state,
string ?
string :
"(Null)");
225 if (strcasecmp(
string,
"BEGIN") != 0)
230 if (strncasecmp(
string, ctx->
packet,
232 || strlen(
string) + 1 != strlen(ctx->
packet)) {
239 if (strcasecmp(
string,
"SUCCESS") == 0) {
241 }
else if (strcasecmp(
string,
"END") == 0) {
242 logprintf(LIRC_NOTICE,
243 "lirc_command_run: status:END");
245 }
else if (strcasecmp(
string,
"ERROR") == 0) {
246 logprintf(LIRC_WARNING,
247 "%s: command failed: %s",
256 if (strcasecmp(
string,
"END") == 0) {
257 logprintf(LIRC_NOTICE,
258 "lirc_command_run: data:END, status:%d",
261 }
else if (strcasecmp(
string,
"DATA") == 0) {
265 logprintf(LIRC_DEBUG,
266 "data: bad packet: %s\n",
271 data_n = (__u32)strtoul(
string, &endptr, 0);
272 if (!*
string || *endptr)
284 strcpy(ctx->
reply,
"");
287 chk_write(STDOUT_FILENO,
string, strlen(
string),
289 chk_write(STDOUT_FILENO,
"\n", 1,
"reply (2)");
300 if (strcasecmp(
string,
"END") == 0) {
301 logprintf(LIRC_NOTICE,
302 "lirc_command_run: status:END, status:%d",
310 logprintf(LIRC_WARNING,
"%s: bad return packet\n", prog);
311 logprintf(LIRC_DEBUG,
"State %d: bad packet: %s\n", status,
string);
316 static void lirc_printf(
const char* format_str, ...)
323 va_start(ap, format_str);
324 vfprintf(stderr, format_str, ap);
329 static void lirc_perror(
const char* s)
340 if (prog == NULL || lirc_prog != NULL)
343 if (lirc_lircd >= 0) {
344 lirc_verbose = verbose;
345 lirc_prog = strdup(prog);
346 if (lirc_prog == NULL) {
347 lirc_printf(
"%s: out of memory\n", prog);
352 lirc_printf(
"%s: could not open socket: %s\n",
354 strerror(-lirc_lircd));
361 if (lirc_prog != NULL) {
365 if (lirc_buffer != NULL) {
369 return close(lirc_lircd);
373 static int lirc_readline(
char** line, FILE* f)
380 newline = (
char*)malloc(LIRC_READ + 1);
381 if (newline == NULL) {
382 lirc_printf(
"%s: out of memory\n", lirc_prog);
387 ret = fgets(newline + len, LIRC_READ + 1, f);
389 if (feof(f) && len > 0) {
397 len = strlen(newline);
398 if (newline[len - 1] ==
'\n') {
399 newline[len - 1] = 0;
404 enlargeline = (
char*)realloc(newline, len + 1 + LIRC_READ);
405 if (enlargeline == NULL) {
407 lirc_printf(
"%s: out of memory\n", lirc_prog);
410 newline = enlargeline;
415 static char* lirc_trim(
char* s)
419 while (s[0] ==
' ' || s[0] ==
'\t')
424 if (s[len] ==
' ' || s[len] ==
'\t')
434 static char lirc_parse_escape(
char** s,
const char* name,
int line)
437 unsigned int i, overflow, count;
438 int digits_found, digit;
478 while (++count < 3) {
480 if (c >=
'0' && c <=
'7') {
481 i = (i << 3) + c -
'0';
487 if (i > (1 << CHAR_BIT) - 1) {
488 i &= (1 << CHAR_BIT) - 1;
490 "%s: octal escape sequence out of range in %s:%d\n",
491 lirc_prog, name, line);
501 if (c >=
'0' && c <=
'9') {
503 }
else if (c >=
'a' && c <=
'f') {
504 digit = c -
'a' + 10;
505 }
else if (c >=
'A' && c <=
'F') {
506 digit = c -
'A' + 10;
511 overflow |= i ^ (i << 4 >> 4);
512 i = (i << 4) + digit;
516 lirc_printf(
"%s: \\x used with no "
517 "following hex digits in %s:%d\n",
518 lirc_prog, name, line);
519 if (overflow || i > (1 << CHAR_BIT) - 1) {
520 i &= (1 << CHAR_BIT) - 1;
521 lirc_printf(
"%s: hex escape sequence out "
522 "of range in %s:%d\n", lirc_prog, name,
528 if (c >=
'@' && c <=
'Z')
535 static void lirc_parse_string(
char* s,
const char* name,
int line)
543 *t = lirc_parse_escape(&s, name, line);
555 static void lirc_parse_include(
char* s,
const char* name,
int line)
564 if (*s !=
'"' && *s !=
'<')
566 if (*s ==
'"' && last !=
'"')
568 else if (*s ==
'<' && last !=
'>')
571 memmove(s, s + 1, len - 2 + 1);
575 int lirc_mode(
char* token,
char* token2,
char** mode,
579 int (check) (
char* s),
585 new_entry = *new_config;
586 if (strcasecmp(token,
"begin") == 0) {
587 if (token2 == NULL) {
588 if (new_entry == NULL) {
591 if (new_entry == NULL) {
592 lirc_printf(
"%s: out of memory\n",
596 new_entry->prog = NULL;
597 new_entry->code = NULL;
598 new_entry->rep_delay = 0;
599 new_entry->ign_first_events = 0;
601 new_entry->config = NULL;
602 new_entry->change_mode = NULL;
603 new_entry->flags = none;
604 new_entry->mode = NULL;
605 new_entry->next_config = NULL;
606 new_entry->next_code = NULL;
607 new_entry->next = NULL;
608 *new_config = new_entry;
610 lirc_printf(
"%s: bad file format, %s:%d\n",
611 lirc_prog, name, line);
615 if (new_entry == NULL && *mode == NULL) {
616 *mode = strdup(token2);
620 lirc_printf(
"%s: bad file format, %s:%d\n",
621 lirc_prog, name, line);
625 }
else if (strcasecmp(token,
"end") == 0) {
626 if (token2 == NULL) {
627 if (new_entry != NULL) {
629 if (new_entry->prog == NULL) {
631 "%s: prog missing in config before line %d\n", lirc_prog,
633 lirc_freeconfigentries(new_entry);
637 if (strcasecmp(new_entry->prog,
639 lirc_freeconfigentries(new_entry);
644 new_entry->next_code = new_entry->code;
645 new_entry->next_config = new_entry->config;
646 if (*last_config == NULL) {
647 *first_config = new_entry;
648 *last_config = new_entry;
650 (*last_config)->next = new_entry;
651 *last_config = new_entry;
656 new_entry->mode = strdup(*mode);
657 if (new_entry->mode == NULL) {
659 "%s: out of memory\n",
666 new_entry->prog != NULL &&
667 strcasecmp(new_entry->prog,
671 list = new_entry->config;
672 while (list != NULL) {
673 if (check(list->string) == -1)
679 if (new_entry->rep_delay == 0 &&
681 new_entry->rep_delay = new_entry->rep -
685 "%s: %s:%d: 'end' without 'begin'\n",
686 lirc_prog, name, line);
691 if (new_entry != NULL) {
693 "%s: %s:%d: missing 'end' token\n",
694 lirc_prog, name, line);
697 if (strcasecmp(*mode, token2) == 0) {
701 lirc_printf(
"%s: \"%s\" doesn't "
702 "match mode \"%s\"\n",
703 lirc_prog, token2, *mode);
708 "%s: %s:%d: 'end %s' without 'begin'\n",
709 lirc_prog, name, line, token2);
714 lirc_printf(
"%s: unknown token \"%s\" in %s:%d ignored\n",
715 lirc_prog, token, name, line);
721 unsigned int lirc_flags(
char*
string)
727 s = strtok(
string,
" \t|");
729 if (strcasecmp(s,
"once") == 0)
731 else if (strcasecmp(s,
"quit") == 0)
733 else if (strcasecmp(s,
"mode") == 0)
735 else if (strcasecmp(s,
"startup_mode") == 0)
736 flags |= startup_mode;
737 else if (strcasecmp(s,
"toggle_reset") == 0)
738 flags |= toggle_reset;
740 lirc_printf(
"%s: unknown flag \"%s\"\n", lirc_prog, s);
741 s = strtok(NULL,
" \t");
756 static char* get_homepath(
void)
761 filename = malloc(MAXPATHLEN);
762 if (filename == NULL) {
763 lirc_printf(
"%s: out of memory\n", lirc_prog);
766 home = getenv(
"HOME");
767 home = home == NULL ?
"/" : home;
768 strncpy(filename, home, MAXPATHLEN);
769 if (filename[strlen(filename) - 1] ==
'/')
770 filename[strlen(filename) - 1] =
'\0';
780 static char* get_freedesktop_path(
void)
784 if (getenv(
"XDG_CONFIG_HOME") != NULL) {
785 path = malloc(MAXPATHLEN);
786 strncpy(path, getenv(
"XDG_CONFIG_HOME"), MAXPATHLEN);
787 strncat(path,
"/", MAXPATHLEN - strlen(path));
788 strncat(path,
CFG_LIRCRC, MAXPATHLEN - strlen(path));
790 path = get_homepath();
793 strncat(path,
"/.config/lircrc", MAXPATHLEN - strlen(path) - 1);
795 if (access(path, R_OK) != 0)
801 static char* lirc_getfilename(
const char* file,
const char* current_file)
806 filename = get_freedesktop_path();
807 if (filename == NULL) {
809 }
else if (strlen(filename) == 0) {
811 filename = get_homepath();
812 if (filename == NULL)
816 filename = realloc(filename, strlen(filename) + 1);
817 }
else if (strncmp(file,
"~/", 2) == 0) {
818 filename = get_homepath();
819 if (filename == NULL)
821 strcat(filename, file + 1);
822 filename = realloc(filename, strlen(filename) + 1);
823 }
else if (file[0] ==
'/' || current_file == NULL) {
825 filename = strdup(file);
826 if (filename == NULL) {
827 lirc_printf(
"%s: out of memory\n", lirc_prog);
832 int pathlen = strlen(current_file);
834 while (pathlen > 0 && current_file[pathlen - 1] !=
'/')
836 filename = (
char*)malloc(pathlen + strlen(file) + 1);
837 if (filename == NULL) {
838 lirc_printf(
"%s: out of memory\n", lirc_prog);
841 memcpy(filename, current_file, pathlen);
842 filename[pathlen] = 0;
843 strcat(filename, file);
849 static FILE* lirc_open(
const char* file,
850 const char* current_file,
856 filename = lirc_getfilename(file, current_file);
857 if (filename == NULL)
860 fin = fopen(filename,
"r");
861 if (fin == NULL && (file != NULL || errno != ENOENT)) {
862 lirc_printf(
"%s: could not open config file %s\n", lirc_prog,
864 lirc_perror(lirc_prog);
865 }
else if (fin == NULL) {
868 fin = fopen(root_file,
"r");
869 if (fin == NULL && errno == ENOENT) {
870 int save_errno = errno;
873 fin = fopen(root_file,
"r");
876 if (fin == NULL && errno != ENOENT) {
877 lirc_printf(
"%s: could not open config file %s\n",
879 lirc_perror(lirc_prog);
880 }
else if (fin == NULL) {
881 lirc_printf(
"%s: could not open config files "
882 "%s and %s\n", lirc_prog, filename,
884 lirc_perror(lirc_prog);
887 filename = strdup(root_file);
888 if (filename == NULL) {
890 lirc_printf(
"%s: out of memory\n", lirc_prog);
895 if (full_name && fin != NULL)
896 *full_name = filename;
903 static struct filestack_t* stack_push(
struct filestack_t* parent)
905 struct filestack_t* entry;
907 entry = malloc(
sizeof(
struct filestack_t));
909 lirc_printf(
"%s: out of memory\n", lirc_prog);
915 entry->parent = parent;
920 static struct filestack_t* stack_pop(
struct filestack_t* entry)
922 struct filestack_t* parent = NULL;
925 parent = entry->parent;
934 static void stack_free(
struct filestack_t* entry)
937 entry = stack_pop(entry);
949 while (scan != NULL) {
950 if (scan->flags & startup_mode) {
951 if (scan->change_mode != NULL) {
952 startupmode = scan->change_mode;
954 scan->change_mode = NULL;
957 lirc_printf(
"%s: startup_mode flags requires 'mode ='\n", lirc_prog);
963 if (startupmode == NULL) {
965 while (scan != NULL) {
966 if (scan->mode != NULL
967 && strcasecmp(lirc_prog, scan->mode) == 0) {
968 startupmode = lirc_prog;
975 if (startupmode == NULL)
978 while (scan != NULL) {
979 if (scan->change_mode != NULL
980 && scan->flags & once
981 && strcasecmp(startupmode, scan->change_mode) == 0)
1003 free(c->change_mode);
1008 while (code != NULL) {
1009 if (code->remote != NULL && code->remote != LIRC_ALL)
1011 if (code->button != NULL && code->button != LIRC_ALL)
1013 code_temp = code->next;
1019 while (list != NULL) {
1022 list_temp = list->next;
1026 config_temp = c->next;
1034 parse_shebang(
char* line,
int depth,
const char* path,
char* buff,
size_t size)
1038 const char*
const SHEBANG_MSG =
1039 "Warning: Use of deprecated lircrc shebang."
1040 " Use lircrc_class instead.\n";
1042 token = strtok(line,
"#! ");
1045 lirc_printf(
"Warning: ignoring shebang in included file.");
1048 if (strcmp(token,
"lircrc") == 0) {
1049 strncpy(my_path, path,
sizeof(my_path) - 1);
1050 strncat(buff, basename(my_path), size - 1);
1051 lirc_printf(SHEBANG_MSG);
1053 lirc_printf(
"Warning: bad shebang (ignored)");
1058 static int lirc_readconfig_only_internal(
const char* file,
1060 int (check)(
char* s),
1063 const char*
const INCLUDED_LIRCRC_CLASS =
1064 "Warning: lirc_class in included file (ignored)";
1070 struct filestack_t* filestack;
1071 struct filestack_t* stack_tmp;
1073 char lircrc_class[128] = {
'\0' };
1081 char* save_full_name = NULL;
1083 filestack = stack_push(NULL);
1084 if (filestack == NULL)
1086 filestack->file = lirc_open(file, NULL, &(filestack->name));
1087 if (filestack->file == NULL) {
1088 stack_free(filestack);
1091 filestack->line = 0;
1094 first = new_entry = last = NULL;
1098 ret = lirc_readline(&
string, filestack->file);
1099 if (ret == -1 ||
string == NULL) {
1100 fclose(filestack->file);
1101 if (open_files == 1 && full_name != NULL) {
1102 save_full_name = filestack->name;
1103 filestack->name = NULL;
1105 filestack = stack_pop(filestack);
1112 if (strncmp(
string,
"#!", 2) == 0) {
1113 parse_shebang(
string,
1117 sizeof(lircrc_class));
1121 eq = strchr(
string,
'=');
1123 token = strtok(
string,
" \t");
1124 if (token == NULL) {
1126 }
else if (token[0] ==
'#') {
1128 }
else if (strcasecmp(token,
"lircrc_class") == 0) {
1129 token2 = lirc_trim(strtok(NULL,
""));
1130 if (strlen(token2) == 0) {
1132 "Warning: no lircrc_class");
1133 }
else if (open_files == 1) {
1134 strncpy(lircrc_class,
1136 sizeof(lircrc_class) - 1);
1138 lirc_printf(INCLUDED_LIRCRC_CLASS);
1140 }
else if (strcasecmp(token,
"include") == 0) {
1141 if (open_files >= MAX_INCLUDES) {
1142 lirc_printf(
"%s: too many files "
1143 "included at %s:%d\n",
1144 lirc_prog, filestack->name,
1148 token2 = strtok(NULL,
"");
1149 token2 = lirc_trim(token2);
1150 lirc_parse_include(token2,
1153 stack_tmp = stack_push(filestack);
1154 if (stack_tmp == NULL) {
1162 stack_tmp->line = 0;
1163 if (stack_tmp->file) {
1165 filestack = stack_tmp;
1167 stack_pop(stack_tmp);
1173 token2 = strtok(NULL,
" \t");
1175 token3 = strtok(NULL,
" \t");
1176 if (token2 != NULL && token3 != NULL) {
1177 lirc_printf(
"%s: unexpected token in line %s:%d\n",
1178 lirc_prog, filestack->name, filestack->line);
1180 ret = lirc_mode(token, token2, &mode,
1183 check, filestack->name,
1186 if (remote != LIRC_ALL)
1194 if (new_entry != NULL) {
1195 lirc_freeconfigentries(
1204 token = lirc_trim(
string);
1205 token2 = lirc_trim(eq + 1);
1206 if (token[0] ==
'#') {
1208 }
else if (new_entry == NULL) {
1209 lirc_printf(
"%s: bad file format, %s:%d\n",
1210 lirc_prog, filestack->name,
1214 token2 = strdup(token2);
1215 if (token2 == NULL) {
1216 lirc_printf(
"%s: out of memory\n",
1219 }
else if (strcasecmp(token,
"prog") == 0) {
1220 if (new_entry->prog != NULL)
1221 free(new_entry->prog);
1222 new_entry->prog = token2;
1223 }
else if (strcasecmp(token,
"remote") == 0) {
1224 if (remote != LIRC_ALL)
1227 if (strcasecmp(
"*", token2) == 0) {
1233 }
else if (strcasecmp(token,
"button") == 0) {
1241 "%s: out of memory\n",
1245 code->remote = remote;
1248 code->button = LIRC_ALL;
1251 code->button = token2;
1255 if (new_entry->code == NULL)
1256 new_entry->code = code;
1258 new_entry->next_code->
1260 new_entry->next_code = code;
1261 if (remote != LIRC_ALL) {
1262 remote = strdup(remote);
1263 if (remote == NULL) {
1265 "%s: out of memory\n",
1271 }
else if (strcasecmp(token,
"delay") == 0) {
1275 new_entry->rep_delay = strtoul(token2,
1277 if ((new_entry->rep_delay ==
1278 ULONG_MAX && errno == ERANGE)
1279 || end[0] != 0 || strlen(token2) ==
1281 lirc_printf(
"%s: \"%s\" not"
1282 " a valid number for delay\n", lirc_prog,
1285 }
else if (strcasecmp(token,
"ignore_first_events") == 0) {
1289 new_entry->ign_first_events = strtoul(
1291 if ((new_entry->ign_first_events ==
1292 ULONG_MAX && errno == ERANGE)
1293 || end[0] != 0 || strlen(token2) ==
1295 lirc_printf(
"%s: \"%s\" not"
1296 " a valid number for ignore_first_events\n",
1299 }
else if (strcasecmp(token,
"repeat") == 0) {
1304 strtoul(token2, &end, 0);
1305 if ((new_entry->rep == ULONG_MAX &&
1307 || end[0] != 0 || strlen(token2) ==
1309 lirc_printf(
"%s: \"%s\" not"
1310 " a valid number for repeat\n", lirc_prog,
1313 }
else if (strcasecmp(token,
"config") == 0) {
1318 if (new_list == NULL) {
1321 "%s: out of memory\n",
1325 lirc_parse_string(token2,
1328 new_list->string = token2;
1329 new_list->next = NULL;
1330 if (new_entry->config == NULL)
1334 new_entry->next_config->
1336 new_entry->next_config =
1339 }
else if (strcasecmp(token,
"mode") == 0) {
1340 if (new_entry->change_mode != NULL)
1341 free(new_entry->change_mode);
1342 new_entry->change_mode = token2;
1343 }
else if (strcasecmp(token,
"flags") == 0) {
1344 new_entry->flags = lirc_flags(token2);
1349 "%s: unknown token \"%s\" in %s:%d ignored\n",
1350 lirc_prog, token, filestack->name,
1359 if (remote != LIRC_ALL)
1361 if (new_entry != NULL) {
1363 ret = lirc_mode(
"end", NULL, &mode, &new_entry, &first,
1364 &last, check,
"", 0);
1366 "%s: warning: end token missing at end of file\n",
1369 lirc_freeconfigentries(new_entry);
1376 "%s: warning: no end token found for mode \"%s\"\n", lirc_prog,
1385 if (*config == NULL) {
1386 lirc_printf(
"%s: out of memory\n", lirc_prog);
1387 lirc_freeconfigentries(first);
1390 (*config)->first = first;
1391 (*config)->next = first;
1392 startupmode = lirc_startupmode((*config)->first);
1393 (*config)->current_mode =
1394 startupmode ? strdup(startupmode) : NULL;
1395 if (lircrc_class[0] !=
'\0')
1396 (*config)->lircrc_class = strdup(lircrc_class);
1398 (*config)->lircrc_class = NULL;
1399 (*config)->sockfd = -1;
1400 if (full_name != NULL) {
1401 *full_name = save_full_name;
1402 save_full_name = NULL;
1406 lirc_freeconfigentries(first);
1409 stack_free(filestack);
1411 free(save_full_name);
1416 int lirc_identify(
int sockfd)
1426 while (ret == EAGAIN || ret == EWOULDBLOCK);
1434 int (check)(
char* s))
1436 struct sockaddr_un addr;
1443 if (lirc_readconfig_only_internal(file, config, check, &filename) == -1)
1446 if ((*config)->lircrc_class == NULL)
1447 goto lirc_readconfig_compat;
1451 addr.sun_family = AF_UNIX;
1454 sizeof(addr.sun_path)) >
sizeof(addr.sun_path)) {
1455 lirc_printf(
"%s: WARNING: file name too long\n", lirc_prog);
1456 goto lirc_readconfig_compat;
1458 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1460 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1461 lirc_perror(lirc_prog);
1462 goto lirc_readconfig_compat;
1464 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1465 (*config)->sockfd = sockfd;
1469 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS)
1480 snprintf(command,
sizeof(command),
1481 "lircrcd %s", (*config)->lircrc_class);
1482 ret = system(command);
1483 if (ret == -1 || WEXITSTATUS(ret) != EXIT_SUCCESS)
1484 goto lirc_readconfig_compat;
1487 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1489 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1490 lirc_perror(lirc_prog);
1491 goto lirc_readconfig_compat;
1493 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1494 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS) {
1495 (*config)->sockfd = sockfd;
1503 lirc_readconfig_compat:
1513 int (check) (
char* s))
1515 return lirc_readconfig_only_internal(file, config, check, NULL);
1521 if (config != NULL) {
1522 if (config->sockfd != -1) {
1523 (void)close(config->sockfd);
1524 config->sockfd = -1;
1528 lirc_freeconfigentries(config->first);
1529 free(config->current_mode);
1535 static void lirc_clearmode(
struct lirc_config* config)
1539 if (config->current_mode == NULL)
1541 scan = config->first;
1542 while (scan != NULL) {
1543 if (scan->change_mode != NULL)
1544 if (strcasecmp(scan->change_mode,
1545 config->current_mode) == 0)
1546 scan->flags &= ~ecno;
1549 free(config->current_mode);
1550 config->current_mode = NULL;
1554 static char* lirc_execute(
struct lirc_config* config,
1560 if (scan->flags & mode)
1561 lirc_clearmode(config);
1562 if (scan->change_mode != NULL) {
1563 free(config->current_mode);
1564 config->current_mode = strdup(scan->change_mode);
1565 if (scan->flags & once) {
1566 if (scan->flags & ecno)
1569 scan->flags |= ecno;
1572 if (scan->next_config != NULL
1573 && scan->prog != NULL
1574 && (lirc_prog == NULL || strcasecmp(scan->prog, lirc_prog) == 0)
1576 s = scan->next_config->string;
1577 scan->next_config = scan->next_config->next;
1578 if (scan->next_config == NULL)
1579 scan->next_config = scan->config;
1595 int delay_start, rep_delay;
1597 if (scan->ign_first_events) {
1598 if (scan->rep_delay && rep == 0)
1600 "%s: ignoring \"delay\" because \"ignore_first_events\" is also set\n",
1602 rep_delay = scan->ign_first_events;
1605 rep_delay = scan->rep_delay;
1609 if (rep < delay_start)
1612 if (scan->rep == 0 && rep_delay > 0 && rep == rep_delay + delay_start)
1615 if (scan->rep > 0 && rep >= rep_delay + delay_start) {
1616 rep -= rep_delay + delay_start;
1617 return (rep % scan->rep) == 0;
1630 if (scan->code == NULL)
1631 return rep_filter(scan, rep);
1634 if (scan->next_code->remote == LIRC_ALL
1635 || strcasecmp(scan->next_code->remote, remote) == 0) {
1636 if (scan->next_code->button == LIRC_ALL
1637 || strcasecmp(scan->next_code->button, button) == 0) {
1640 if (scan->code->next == NULL || rep == 0) {
1641 scan->next_code = scan->next_code->next;
1642 if (scan->code->next != NULL)
1646 if (scan->next_code == NULL) {
1647 scan->next_code = scan->code;
1648 if (scan->code->next != NULL ||
1649 rep_filter(scan, rep))
1660 if (scan->flags & toggle_reset)
1661 scan->next_config = scan->config;
1664 if (codes == scan->next_code)
1666 codes = codes->next;
1668 while (codes != scan->next_code->next) {
1675 while (next != scan->next_code) {
1676 if (prev->remote == LIRC_ALL
1677 || strcasecmp(prev->remote, next->remote) == 0) {
1678 if (prev->button == LIRC_ALL
1679 || strcasecmp(prev->button,
1680 next->button) == 0) {
1693 if (prev->remote == LIRC_ALL
1694 || strcasecmp(prev->remote, remote) == 0) {
1695 if (prev->button == LIRC_ALL
1696 || strcasecmp(prev->button, button) == 0) {
1698 scan->next_code = prev->next;
1704 codes = codes->next;
1706 scan->next_code = scan->code;
1713 static int warning = 1;
1717 fprintf(stderr,
"%s: warning: lirc_ir2char() is obsolete\n",
1727 static int lirc_code2char_internal(
struct lirc_config* config,
1742 if (sscanf(code,
"%*x %x %*s %*s\n", &rep) == 1) {
1743 backup = strdup(code);
1747 strtok(backup,
" ");
1749 button = strtok(NULL,
" ");
1750 remote = strtok(NULL,
"\n");
1752 if (button == NULL || remote == NULL) {
1757 scan = config->next;
1759 while (scan != NULL) {
1760 exec_level = lirc_iscode(scan, remote, button, rep);
1761 if (exec_level > 0 &&
1762 (scan->mode == NULL ||
1763 (scan->mode != NULL &&
1764 config->current_mode != NULL &&
1765 strcasecmp(scan->mode,
1766 config->current_mode) == 0)) &&
1767 quit_happened == 0) {
1768 if (exec_level > 1) {
1769 s = lirc_execute(config, scan);
1770 if (s != NULL && prog != NULL)
1775 if (scan->flags & quit) {
1777 config->next = NULL;
1780 }
else if (s != NULL) {
1781 config->next = scan->next;
1793 config->next = config->first;
1807 if (config->sockfd != -1) {
1810 while (ret == EAGAIN || ret == EWOULDBLOCK);
1813 *
string = static_buff;
1815 return ret == 0 ? 0 : -1;
1817 return lirc_code2char_internal(config, code,
string, NULL);
1821 int lirc_code2charprog(
struct lirc_config* config,
1832 ret = lirc_code2char_internal(config, code,
string, prog);
1841 static int warning = 1;
1846 fprintf(stderr,
"%s: warning: lirc_nextir() is obsolete\n",
1860 static int end_len = 0;
1866 if (lirc_buffer == NULL) {
1867 lirc_buffer = (
char*)malloc(packet_size + 1);
1868 if (lirc_buffer == NULL) {
1869 lirc_printf(
"%s: out of memory\n", lirc_prog);
1874 while ((end = strchr(lirc_buffer,
'\n')) == NULL) {
1875 if (end_len >= packet_size) {
1880 (
char*)realloc(lirc_buffer, packet_size + 1);
1881 if (new_buffer == NULL)
1883 lirc_buffer = new_buffer;
1885 len = read(lirc_lircd, lirc_buffer + end_len,
1886 packet_size - end_len);
1888 if (len == -1 && errno == EAGAIN)
1894 lirc_buffer[end_len] = 0;
1896 end = strchr(lirc_buffer,
'\n');
1903 end_len = strlen(end);
1906 *code = strdup(lirc_buffer);
1908 memmove(lirc_buffer, end, end_len + 1);
1917 id =
id != NULL ?
id :
"default";
1918 snprintf(buf, size, VARRUNDIR
"/%d-%s-lircrcd.socket", getuid(),
id);
1930 if (config->sockfd != -1) {
1934 while (ret == EAGAIN || ret == EWOULDBLOCK);
1941 return config->current_mode;
1951 if (config->sockfd != -1) {
1960 while (r == EAGAIN || r == EWOULDBLOCK);
1967 free(config->current_mode);
1968 config->current_mode = mode ? strdup(mode) : NULL;
1969 return config->current_mode;
1983 while (r == EAGAIN);
1998 scancode, repeat, keysym, remote);
2003 while (r == EAGAIN);
2010 do_connect(
int domain,
struct sockaddr* addr,
size_t size,
int quiet)
2014 fd = socket(domain, SOCK_STREAM, 0);
2017 fprintf(stderr,
"do_connect: could not open socket\n");
2022 if (connect(fd, addr, size) == -1) {
2025 "do_connect: could not connect to socket\n");
2036 const char* socket_path;
2037 struct sockaddr_un addr_un;
2039 socket_path = path ? path : getenv(
"LIRC_SOCKET_PATH");
2040 socket_path = socket_path ? socket_path :
LIRCD;
2041 if (strlen(socket_path) + 1 >
sizeof(addr_un.sun_path)) {
2044 fprintf(stderr,
"%s: socket name is too long\n", prog);
2045 return -ENAMETOOLONG;
2047 addr_un.sun_family = AF_UNIX;
2048 strcpy(addr_un.sun_path, socket_path);
2049 return do_connect(AF_UNIX,
2050 (
struct sockaddr*)&addr_un,
2058 struct addrinfo* addrinfos;
2063 snprintf(service,
sizeof(service),
2065 r = getaddrinfo(address, service, NULL, &addrinfos);
2068 fprintf(stderr,
"get_remote_socket: host %s unknown\n",
2070 return -EADDRNOTAVAIL;
2072 for (a = addrinfos; a != NULL; a = a->ai_next) {
2073 r = do_connect(a->ai_family, a->ai_addr, a->ai_addrlen, quiet);
2077 freeaddrinfo(addrinfos);
#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)