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)
164 if (cmd->
next == NULL || strchr(cmd->
next,
'\n') == NULL) {
165 r = fill_string(fd, cmd);
171 cmd->
next = strchr(cmd->
next,
'\n');
172 if (cmd->
next != NULL) {
183 const char*
string = NULL;
190 todo = strlen(ctx->
packet);
192 logprintf(LIRC_DEBUG,
"lirc_command_run: Sending: %s", data);
194 done = write(fd, (
void*)data, todo);
196 logprintf(LIRC_WARNING,
197 "%s: could not send packet\n", prog);
211 r = read_string(ctx, fd, &
string);
213 if (!
string || strlen(
string) == 0)
215 logprintf(LIRC_DEBUG,
216 "lirc_command_run, state: %d, input: \"%s\"\n",
217 state,
string ?
string :
"(Null)");
220 if (strcasecmp(
string,
"BEGIN") != 0)
225 if (strncasecmp(
string, ctx->
packet,
227 || strlen(
string) + 1 != strlen(ctx->
packet)) {
234 if (strcasecmp(
string,
"SUCCESS") == 0) {
236 }
else if (strcasecmp(
string,
"END") == 0) {
237 logprintf(LIRC_NOTICE,
238 "lirc_command_run: status:END");
240 }
else if (strcasecmp(
string,
"ERROR") == 0) {
241 logprintf(LIRC_WARNING,
242 "%s: command failed: %s",
251 if (strcasecmp(
string,
"END") == 0) {
252 logprintf(LIRC_NOTICE,
253 "lirc_command_run: data:END, status:%d",
256 }
else if (strcasecmp(
string,
"DATA") == 0) {
260 logprintf(LIRC_DEBUG,
261 "data: bad packet: %s\n",
266 data_n = (__u32)strtoul(
string, &endptr, 0);
267 if (!*
string || *endptr)
279 strcpy(ctx->
reply,
"");
295 if (strcasecmp(
string,
"END") == 0) {
296 logprintf(LIRC_NOTICE,
297 "lirc_command_run: status:END, status:%d",
305 logprintf(LIRC_WARNING,
"%s: bad return packet\n", prog);
306 logprintf(LIRC_DEBUG,
"State %d: bad packet: %s\n", status,
string);
311 static void lirc_printf(
const char* format_str, ...)
318 va_start(ap, format_str);
319 vfprintf(stderr, format_str, ap);
324 static void lirc_perror(
const char* s)
335 if (prog == NULL || lirc_prog != NULL)
338 if (lirc_lircd >= 0) {
339 lirc_verbose = verbose;
340 lirc_prog = strdup(prog);
341 if (lirc_prog == NULL) {
342 lirc_printf(
"%s: out of memory\n", prog);
347 lirc_printf(
"%s: could not open socket: %s\n",
349 strerror(-lirc_lircd));
356 if (lirc_prog != NULL) {
360 if (lirc_buffer != NULL) {
364 return close(lirc_lircd);
368 static int lirc_readline(
char** line, FILE* f)
375 newline = (
char*)malloc(LIRC_READ + 1);
376 if (newline == NULL) {
377 lirc_printf(
"%s: out of memory\n", lirc_prog);
382 ret = fgets(newline + len, LIRC_READ + 1, f);
384 if (feof(f) && len > 0) {
392 len = strlen(newline);
393 if (newline[len - 1] ==
'\n') {
394 newline[len - 1] = 0;
399 enlargeline = (
char*)realloc(newline, len + 1 + LIRC_READ);
400 if (enlargeline == NULL) {
402 lirc_printf(
"%s: out of memory\n", lirc_prog);
405 newline = enlargeline;
410 static char* lirc_trim(
char* s)
414 while (s[0] ==
' ' || s[0] ==
'\t')
419 if (s[len] ==
' ' || s[len] ==
'\t')
429 static char lirc_parse_escape(
char** s,
const char* name,
int line)
432 unsigned int i, overflow, count;
433 int digits_found, digit;
473 while (++count < 3) {
475 if (c >=
'0' && c <=
'7') {
476 i = (i << 3) + c -
'0';
482 if (i > (1 << CHAR_BIT) - 1) {
483 i &= (1 << CHAR_BIT) - 1;
485 "%s: octal escape sequence out of range in %s:%d\n",
486 lirc_prog, name, line);
496 if (c >=
'0' && c <=
'9') {
498 }
else if (c >=
'a' && c <=
'f') {
499 digit = c -
'a' + 10;
500 }
else if (c >=
'A' && c <=
'F') {
501 digit = c -
'A' + 10;
506 overflow |= i ^ (i << 4 >> 4);
507 i = (i << 4) + digit;
511 lirc_printf(
"%s: \\x used with no "
512 "following hex digits in %s:%d\n",
513 lirc_prog, name, line);
514 if (overflow || i > (1 << CHAR_BIT) - 1) {
515 i &= (1 << CHAR_BIT) - 1;
516 lirc_printf(
"%s: hex escape sequence out "
517 "of range in %s:%d\n", lirc_prog, name,
523 if (c >=
'@' && c <=
'Z')
530 static void lirc_parse_string(
char* s,
const char* name,
int line)
538 *t = lirc_parse_escape(&s, name, line);
550 static void lirc_parse_include(
char* s,
const char* name,
int line)
559 if (*s !=
'"' && *s !=
'<')
561 if (*s ==
'"' && last !=
'"')
563 else if (*s ==
'<' && last !=
'>')
566 memmove(s, s + 1, len - 2 + 1);
570 int lirc_mode(
char* token,
char* token2,
char** mode,
574 int (check) (
char* s),
580 new_entry = *new_config;
581 if (strcasecmp(token,
"begin") == 0) {
582 if (token2 == NULL) {
583 if (new_entry == NULL) {
586 if (new_entry == NULL) {
587 lirc_printf(
"%s: out of memory\n",
591 new_entry->prog = NULL;
592 new_entry->code = NULL;
593 new_entry->rep_delay = 0;
594 new_entry->ign_first_events = 0;
596 new_entry->config = NULL;
597 new_entry->change_mode = NULL;
598 new_entry->flags = none;
599 new_entry->mode = NULL;
600 new_entry->next_config = NULL;
601 new_entry->next_code = NULL;
602 new_entry->next = NULL;
603 *new_config = new_entry;
605 lirc_printf(
"%s: bad file format, %s:%d\n",
606 lirc_prog, name, line);
610 if (new_entry == NULL && *mode == NULL) {
611 *mode = strdup(token2);
615 lirc_printf(
"%s: bad file format, %s:%d\n",
616 lirc_prog, name, line);
620 }
else if (strcasecmp(token,
"end") == 0) {
621 if (token2 == NULL) {
622 if (new_entry != NULL) {
624 if (new_entry->prog == NULL) {
626 "%s: prog missing in config before line %d\n", lirc_prog,
628 lirc_freeconfigentries(new_entry);
632 if (strcasecmp(new_entry->prog,
634 lirc_freeconfigentries(new_entry);
639 new_entry->next_code = new_entry->code;
640 new_entry->next_config = new_entry->config;
641 if (*last_config == NULL) {
642 *first_config = new_entry;
643 *last_config = new_entry;
645 (*last_config)->next = new_entry;
646 *last_config = new_entry;
651 new_entry->mode = strdup(*mode);
652 if (new_entry->mode == NULL) {
654 "%s: out of memory\n",
661 new_entry->prog != NULL &&
662 strcasecmp(new_entry->prog,
666 list = new_entry->config;
667 while (list != NULL) {
668 if (check(list->string) == -1)
674 if (new_entry->rep_delay == 0 &&
676 new_entry->rep_delay = new_entry->rep -
680 "%s: %s:%d: 'end' without 'begin'\n",
681 lirc_prog, name, line);
686 if (new_entry != NULL) {
688 "%s: %s:%d: missing 'end' token\n",
689 lirc_prog, name, line);
692 if (strcasecmp(*mode, token2) == 0) {
696 lirc_printf(
"%s: \"%s\" doesn't "
697 "match mode \"%s\"\n",
698 lirc_prog, token2, *mode);
703 "%s: %s:%d: 'end %s' without 'begin'\n",
704 lirc_prog, name, line, token2);
709 lirc_printf(
"%s: unknown token \"%s\" in %s:%d ignored\n",
710 lirc_prog, token, name, line);
716 unsigned int lirc_flags(
char*
string)
722 s = strtok(
string,
" \t|");
724 if (strcasecmp(s,
"once") == 0)
726 else if (strcasecmp(s,
"quit") == 0)
728 else if (strcasecmp(s,
"mode") == 0)
730 else if (strcasecmp(s,
"startup_mode") == 0)
731 flags |= startup_mode;
732 else if (strcasecmp(s,
"toggle_reset") == 0)
733 flags |= toggle_reset;
735 lirc_printf(
"%s: unknown flag \"%s\"\n", lirc_prog, s);
736 s = strtok(NULL,
" \t");
751 static char* get_homepath(
void)
756 filename = malloc(MAXPATHLEN);
757 if (filename == NULL) {
758 lirc_printf(
"%s: out of memory\n", lirc_prog);
761 home = getenv(
"HOME");
762 home = home == NULL ?
"/" : home;
763 strncpy(filename, home, MAXPATHLEN);
764 if (filename[strlen(filename) - 1] ==
'/')
765 filename[strlen(filename) - 1] =
'\0';
775 static char* get_freedesktop_path(
void)
779 if (getenv(
"XDG_CONFIG_HOME") != NULL) {
780 path = malloc(MAXPATHLEN);
781 strncpy(path, getenv(
"XDG_CONFIG_HOME"), MAXPATHLEN);
782 strncat(path,
"/", MAXPATHLEN - strlen(path));
783 strncat(path,
CFG_LIRCRC, MAXPATHLEN - strlen(path));
785 path = get_homepath();
788 strncat(path,
"/.config/lircrc", MAXPATHLEN - strlen(path) - 1);
790 if (access(path, R_OK) != 0)
796 static char* lirc_getfilename(
const char* file,
const char* current_file)
801 filename = get_freedesktop_path();
802 if (filename == NULL) {
804 }
else if (strlen(filename) == 0) {
806 filename = get_homepath();
807 if (filename == NULL)
811 filename = realloc(filename, strlen(filename) + 1);
812 }
else if (strncmp(file,
"~/", 2) == 0) {
813 filename = get_homepath();
814 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);
829 while (pathlen > 0 && current_file[pathlen - 1] !=
'/')
831 filename = (
char*)malloc(pathlen + strlen(file) + 1);
832 if (filename == NULL) {
833 lirc_printf(
"%s: out of memory\n", lirc_prog);
836 memcpy(filename, current_file, pathlen);
837 filename[pathlen] = 0;
838 strcat(filename, file);
844 static FILE* lirc_open(
const char* file,
845 const char* current_file,
851 filename = lirc_getfilename(file, current_file);
852 if (filename == NULL)
855 fin = fopen(filename,
"r");
856 if (fin == NULL && (file != NULL || errno != ENOENT)) {
857 lirc_printf(
"%s: could not open config file %s\n", lirc_prog,
859 lirc_perror(lirc_prog);
860 }
else if (fin == NULL) {
863 fin = fopen(root_file,
"r");
864 if (fin == NULL && errno == ENOENT) {
865 int save_errno = errno;
868 fin = fopen(root_file,
"r");
871 if (fin == NULL && errno != ENOENT) {
872 lirc_printf(
"%s: could not open config file %s\n",
874 lirc_perror(lirc_prog);
875 }
else if (fin == NULL) {
876 lirc_printf(
"%s: could not open config files "
877 "%s and %s\n", lirc_prog, filename,
879 lirc_perror(lirc_prog);
882 filename = strdup(root_file);
883 if (filename == NULL) {
885 lirc_printf(
"%s: out of memory\n", lirc_prog);
890 if (full_name && fin != NULL)
891 *full_name = filename;
898 static struct filestack_t* stack_push(
struct filestack_t* parent)
900 struct filestack_t* entry;
902 entry = malloc(
sizeof(
struct filestack_t));
904 lirc_printf(
"%s: out of memory\n", lirc_prog);
910 entry->parent = parent;
915 static struct filestack_t* stack_pop(
struct filestack_t* entry)
917 struct filestack_t* parent = NULL;
920 parent = entry->parent;
929 static void stack_free(
struct filestack_t* entry)
932 entry = stack_pop(entry);
944 while (scan != NULL) {
945 if (scan->flags & startup_mode) {
946 if (scan->change_mode != NULL) {
947 startupmode = scan->change_mode;
949 scan->change_mode = NULL;
952 lirc_printf(
"%s: startup_mode flags requires 'mode ='\n", lirc_prog);
958 if (startupmode == NULL) {
960 while (scan != NULL) {
961 if (scan->mode != NULL
962 && strcasecmp(lirc_prog, scan->mode) == 0) {
963 startupmode = lirc_prog;
970 if (startupmode == NULL)
973 while (scan != NULL) {
974 if (scan->change_mode != NULL
975 && scan->flags & once
976 && strcasecmp(startupmode, scan->change_mode) == 0)
998 free(c->change_mode);
1003 while (code != NULL) {
1004 if (code->remote != NULL && code->remote != LIRC_ALL)
1006 if (code->button != NULL && code->button != LIRC_ALL)
1008 code_temp = code->next;
1014 while (list != NULL) {
1017 list_temp = list->next;
1021 config_temp = c->next;
1029 parse_shebang(
char* line,
int depth,
const char* path,
char* buff,
size_t size)
1033 const char*
const SHEBANG_MSG =
1034 "Warning: Use of deprecated lircrc shebang."
1035 " Use lircrc_class instead.\n";
1037 token = strtok(line,
"#! ");
1040 lirc_printf(
"Warning: ignoring shebang in included file.");
1043 if (strcmp(token,
"lircrc") == 0) {
1044 strncpy(my_path, path,
sizeof(my_path) - 1);
1045 strncat(buff, basename(my_path), size - 1);
1046 lirc_printf(SHEBANG_MSG);
1048 lirc_printf(
"Warning: bad shebang (ignored)");
1053 static int lirc_readconfig_only_internal(
const char* file,
1055 int (check)(
char* s),
1058 const char*
const INCLUDED_LIRCRC_CLASS =
1059 "Warning: lirc_class in included file (ignored)";
1065 struct filestack_t* filestack;
1066 struct filestack_t* stack_tmp;
1068 char lircrc_class[128] = {
'\0' };
1076 char* save_full_name = NULL;
1078 filestack = stack_push(NULL);
1079 if (filestack == NULL)
1081 filestack->file = lirc_open(file, NULL, &(filestack->name));
1082 if (filestack->file == NULL) {
1083 stack_free(filestack);
1086 filestack->line = 0;
1089 first = new_entry = last = NULL;
1093 ret = lirc_readline(&
string, filestack->file);
1094 if (ret == -1 ||
string == NULL) {
1095 fclose(filestack->file);
1096 if (open_files == 1 && full_name != NULL) {
1097 save_full_name = filestack->name;
1098 filestack->name = NULL;
1100 filestack = stack_pop(filestack);
1107 if (strncmp(
string,
"#!", 2) == 0) {
1108 parse_shebang(
string,
1112 sizeof(lircrc_class));
1116 eq = strchr(
string,
'=');
1118 token = strtok(
string,
" \t");
1119 if (token == NULL) {
1121 }
else if (token[0] ==
'#') {
1123 }
else if (strcasecmp(token,
"lircrc_class") == 0) {
1124 token2 = lirc_trim(strtok(NULL,
""));
1125 if (strlen(token2) == 0) {
1127 "Warning: no lircrc_class");
1128 }
else if (open_files == 1) {
1129 strncpy(lircrc_class,
1131 sizeof(lircrc_class) - 1);
1133 lirc_printf(INCLUDED_LIRCRC_CLASS);
1135 }
else if (strcasecmp(token,
"include") == 0) {
1136 if (open_files >= MAX_INCLUDES) {
1137 lirc_printf(
"%s: too many files "
1138 "included at %s:%d\n",
1139 lirc_prog, filestack->name,
1143 token2 = strtok(NULL,
"");
1144 token2 = lirc_trim(token2);
1145 lirc_parse_include(token2,
1148 stack_tmp = stack_push(filestack);
1149 if (stack_tmp == NULL) {
1157 stack_tmp->line = 0;
1158 if (stack_tmp->file) {
1160 filestack = stack_tmp;
1162 stack_pop(stack_tmp);
1168 token2 = strtok(NULL,
" \t");
1170 token3 = strtok(NULL,
" \t");
1171 if (token2 != NULL && token3 != NULL) {
1172 lirc_printf(
"%s: unexpected token in line %s:%d\n",
1173 lirc_prog, filestack->name, filestack->line);
1175 ret = lirc_mode(token, token2, &mode,
1178 check, filestack->name,
1181 if (remote != LIRC_ALL)
1189 if (new_entry != NULL) {
1190 lirc_freeconfigentries(
1199 token = lirc_trim(
string);
1200 token2 = lirc_trim(eq + 1);
1201 if (token[0] ==
'#') {
1203 }
else if (new_entry == NULL) {
1204 lirc_printf(
"%s: bad file format, %s:%d\n",
1205 lirc_prog, filestack->name,
1209 token2 = strdup(token2);
1210 if (token2 == NULL) {
1211 lirc_printf(
"%s: out of memory\n",
1214 }
else if (strcasecmp(token,
"prog") == 0) {
1215 if (new_entry->prog != NULL)
1216 free(new_entry->prog);
1217 new_entry->prog = token2;
1218 }
else if (strcasecmp(token,
"remote") == 0) {
1219 if (remote != LIRC_ALL)
1222 if (strcasecmp(
"*", token2) == 0) {
1228 }
else if (strcasecmp(token,
"button") == 0) {
1236 "%s: out of memory\n",
1240 code->remote = remote;
1243 code->button = LIRC_ALL;
1246 code->button = token2;
1250 if (new_entry->code == NULL)
1251 new_entry->code = code;
1253 new_entry->next_code->
1255 new_entry->next_code = code;
1256 if (remote != LIRC_ALL) {
1257 remote = strdup(remote);
1258 if (remote == NULL) {
1260 "%s: out of memory\n",
1266 }
else if (strcasecmp(token,
"delay") == 0) {
1270 new_entry->rep_delay = strtoul(token2,
1272 if ((new_entry->rep_delay ==
1273 ULONG_MAX && errno == ERANGE)
1274 || end[0] != 0 || strlen(token2) ==
1276 lirc_printf(
"%s: \"%s\" not"
1277 " a valid number for delay\n", lirc_prog,
1280 }
else if (strcasecmp(token,
"ignore_first_events") == 0) {
1284 new_entry->ign_first_events = strtoul(
1286 if ((new_entry->ign_first_events ==
1287 ULONG_MAX && errno == ERANGE)
1288 || end[0] != 0 || strlen(token2) ==
1290 lirc_printf(
"%s: \"%s\" not"
1291 " a valid number for ignore_first_events\n",
1294 }
else if (strcasecmp(token,
"repeat") == 0) {
1299 strtoul(token2, &end, 0);
1300 if ((new_entry->rep == ULONG_MAX &&
1302 || end[0] != 0 || strlen(token2) ==
1304 lirc_printf(
"%s: \"%s\" not"
1305 " a valid number for repeat\n", lirc_prog,
1308 }
else if (strcasecmp(token,
"config") == 0) {
1313 if (new_list == NULL) {
1316 "%s: out of memory\n",
1320 lirc_parse_string(token2,
1323 new_list->string = token2;
1324 new_list->next = NULL;
1325 if (new_entry->config == NULL)
1329 new_entry->next_config->
1331 new_entry->next_config =
1334 }
else if (strcasecmp(token,
"mode") == 0) {
1335 if (new_entry->change_mode != NULL)
1336 free(new_entry->change_mode);
1337 new_entry->change_mode = token2;
1338 }
else if (strcasecmp(token,
"flags") == 0) {
1339 new_entry->flags = lirc_flags(token2);
1344 "%s: unknown token \"%s\" in %s:%d ignored\n",
1345 lirc_prog, token, filestack->name,
1354 if (remote != LIRC_ALL)
1356 if (new_entry != NULL) {
1358 ret = lirc_mode(
"end", NULL, &mode, &new_entry, &first,
1359 &last, check,
"", 0);
1361 "%s: warning: end token missing at end of file\n",
1364 lirc_freeconfigentries(new_entry);
1371 "%s: warning: no end token found for mode \"%s\"\n", lirc_prog,
1380 if (*config == NULL) {
1381 lirc_printf(
"%s: out of memory\n", lirc_prog);
1382 lirc_freeconfigentries(first);
1385 (*config)->first = first;
1386 (*config)->next = first;
1387 startupmode = lirc_startupmode((*config)->first);
1388 (*config)->current_mode =
1389 startupmode ? strdup(startupmode) : NULL;
1390 if (lircrc_class[0] !=
'\0')
1391 (*config)->lircrc_class = strdup(lircrc_class);
1393 (*config)->lircrc_class = NULL;
1394 (*config)->sockfd = -1;
1395 if (full_name != NULL) {
1396 *full_name = save_full_name;
1397 save_full_name = NULL;
1401 lirc_freeconfigentries(first);
1404 stack_free(filestack);
1406 free(save_full_name);
1411 int lirc_identify(
int sockfd)
1421 while (ret == EAGAIN || ret == EWOULDBLOCK);
1429 int (check)(
char* s))
1431 struct sockaddr_un addr;
1438 if (lirc_readconfig_only_internal(file, config, check, &filename) == -1)
1441 if ((*config)->lircrc_class == NULL)
1442 goto lirc_readconfig_compat;
1446 addr.sun_family = AF_UNIX;
1449 sizeof(addr.sun_path)) >
sizeof(addr.sun_path)) {
1450 lirc_printf(
"%s: WARNING: file name too long\n", lirc_prog);
1451 goto lirc_readconfig_compat;
1453 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1455 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1456 lirc_perror(lirc_prog);
1457 goto lirc_readconfig_compat;
1459 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1460 (*config)->sockfd = sockfd;
1464 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS)
1475 snprintf(command,
sizeof(command),
1476 "lircrcd %s", (*config)->lircrc_class);
1477 ret = system(command);
1478 if (ret == -1 || WEXITSTATUS(ret) != EXIT_SUCCESS)
1479 goto lirc_readconfig_compat;
1482 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1484 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1485 lirc_perror(lirc_prog);
1486 goto lirc_readconfig_compat;
1488 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1489 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS) {
1490 (*config)->sockfd = sockfd;
1498 lirc_readconfig_compat:
1508 int (check) (
char* s))
1510 return lirc_readconfig_only_internal(file, config, check, NULL);
1516 if (config != NULL) {
1517 if (config->sockfd != -1) {
1518 (void)close(config->sockfd);
1519 config->sockfd = -1;
1523 lirc_freeconfigentries(config->first);
1524 free(config->current_mode);
1530 static void lirc_clearmode(
struct lirc_config* config)
1534 if (config->current_mode == NULL)
1536 scan = config->first;
1537 while (scan != NULL) {
1538 if (scan->change_mode != NULL)
1539 if (strcasecmp(scan->change_mode,
1540 config->current_mode) == 0)
1541 scan->flags &= ~ecno;
1544 free(config->current_mode);
1545 config->current_mode = NULL;
1549 static char* lirc_execute(
struct lirc_config* config,
1555 if (scan->flags & mode)
1556 lirc_clearmode(config);
1557 if (scan->change_mode != NULL) {
1558 free(config->current_mode);
1559 config->current_mode = strdup(scan->change_mode);
1560 if (scan->flags & once) {
1561 if (scan->flags & ecno)
1564 scan->flags |= ecno;
1567 if (scan->next_config != NULL
1568 && scan->prog != NULL
1569 && (lirc_prog == NULL || strcasecmp(scan->prog, lirc_prog) == 0)
1571 s = scan->next_config->string;
1572 scan->next_config = scan->next_config->next;
1573 if (scan->next_config == NULL)
1574 scan->next_config = scan->config;
1590 int delay_start, rep_delay;
1592 if (scan->ign_first_events) {
1593 if (scan->rep_delay && rep == 0)
1595 "%s: ignoring \"delay\" because \"ignore_first_events\" is also set\n",
1597 rep_delay = scan->ign_first_events;
1600 rep_delay = scan->rep_delay;
1604 if (rep < delay_start)
1607 if (scan->rep == 0 && rep_delay > 0 && rep == rep_delay + delay_start)
1610 if (scan->rep > 0 && rep >= rep_delay + delay_start) {
1611 rep -= rep_delay + delay_start;
1612 return (rep % scan->rep) == 0;
1625 if (scan->code == NULL)
1626 return rep_filter(scan, rep);
1629 if (scan->next_code->remote == LIRC_ALL
1630 || strcasecmp(scan->next_code->remote, remote) == 0) {
1631 if (scan->next_code->button == LIRC_ALL
1632 || strcasecmp(scan->next_code->button, button) == 0) {
1635 if (scan->code->next == NULL || rep == 0) {
1636 scan->next_code = scan->next_code->next;
1637 if (scan->code->next != NULL)
1641 if (scan->next_code == NULL) {
1642 scan->next_code = scan->code;
1643 if (scan->code->next != NULL ||
1644 rep_filter(scan, rep))
1655 if (scan->flags & toggle_reset)
1656 scan->next_config = scan->config;
1659 if (codes == scan->next_code)
1661 codes = codes->next;
1663 while (codes != scan->next_code->next) {
1670 while (next != scan->next_code) {
1671 if (prev->remote == LIRC_ALL
1672 || strcasecmp(prev->remote, next->remote) == 0) {
1673 if (prev->button == LIRC_ALL
1674 || strcasecmp(prev->button,
1675 next->button) == 0) {
1688 if (prev->remote == LIRC_ALL
1689 || strcasecmp(prev->remote, remote) == 0) {
1690 if (prev->button == LIRC_ALL
1691 || strcasecmp(prev->button, button) == 0) {
1693 scan->next_code = prev->next;
1699 codes = codes->next;
1701 scan->next_code = scan->code;
1708 static int warning = 1;
1712 fprintf(stderr,
"%s: warning: lirc_ir2char() is obsolete\n",
1722 static int lirc_code2char_internal(
struct lirc_config* config,
1737 if (sscanf(code,
"%*x %x %*s %*s\n", &rep) == 1) {
1738 backup = strdup(code);
1742 strtok(backup,
" ");
1744 button = strtok(NULL,
" ");
1745 remote = strtok(NULL,
"\n");
1747 if (button == NULL || remote == NULL) {
1752 scan = config->next;
1754 while (scan != NULL) {
1755 exec_level = lirc_iscode(scan, remote, button, rep);
1756 if (exec_level > 0 &&
1757 (scan->mode == NULL ||
1758 (scan->mode != NULL &&
1759 config->current_mode != NULL &&
1760 strcasecmp(scan->mode,
1761 config->current_mode) == 0)) &&
1762 quit_happened == 0) {
1763 if (exec_level > 1) {
1764 s = lirc_execute(config, scan);
1765 if (s != NULL && prog != NULL)
1770 if (scan->flags & quit) {
1772 config->next = NULL;
1775 }
else if (s != NULL) {
1776 config->next = scan->next;
1788 config->next = config->first;
1802 if (config->sockfd != -1) {
1805 while (ret == EAGAIN || ret == EWOULDBLOCK);
1808 *
string = static_buff;
1810 return ret == 0 ? 0 : -1;
1812 return lirc_code2char_internal(config, code,
string, NULL);
1816 int lirc_code2charprog(
struct lirc_config* config,
1827 ret = lirc_code2char_internal(config, code,
string, prog);
1836 static int warning = 1;
1841 fprintf(stderr,
"%s: warning: lirc_nextir() is obsolete\n",
1855 static int end_len = 0;
1861 if (lirc_buffer == NULL) {
1862 lirc_buffer = (
char*)malloc(packet_size + 1);
1863 if (lirc_buffer == NULL) {
1864 lirc_printf(
"%s: out of memory\n", lirc_prog);
1869 while ((end = strchr(lirc_buffer,
'\n')) == NULL) {
1870 if (end_len >= packet_size) {
1875 (
char*)realloc(lirc_buffer, packet_size + 1);
1876 if (new_buffer == NULL)
1878 lirc_buffer = new_buffer;
1880 len = read(lirc_lircd, lirc_buffer + end_len,
1881 packet_size - end_len);
1883 if (len == -1 && errno == EAGAIN)
1889 lirc_buffer[end_len] = 0;
1891 end = strchr(lirc_buffer,
'\n');
1898 end_len = strlen(end);
1901 *code = strdup(lirc_buffer);
1903 memmove(lirc_buffer, end, end_len + 1);
1912 id =
id != NULL ?
id :
"default";
1913 snprintf(buf, size, VARRUNDIR
"/%d-%s-lircrcd.socket", getuid(),
id);
1925 if (config->sockfd != -1) {
1929 while (ret == EAGAIN || ret == EWOULDBLOCK);
1936 return config->current_mode;
1946 if (config->sockfd != -1) {
1955 while (r == EAGAIN || r == EWOULDBLOCK);
1962 free(config->current_mode);
1963 config->current_mode = mode ? strdup(mode) : NULL;
1964 return config->current_mode;
1978 while (r == EAGAIN);
1993 scancode, repeat, keysym, remote);
1998 while (r == EAGAIN);
2005 do_connect(
int domain,
struct sockaddr* addr,
size_t size,
int quiet)
2009 fd = socket(domain, SOCK_STREAM, 0);
2012 fprintf(stderr,
"do_connect: could not open socket\n");
2017 if (connect(fd, addr, size) == -1) {
2020 "do_connect: could not connect to socket\n");
2031 const char* socket_path;
2032 struct sockaddr_un addr_un;
2034 socket_path = path ? path : getenv(
"LIRC_SOCKET_PATH");
2035 socket_path = socket_path ? socket_path :
LIRCD;
2036 if (strlen(socket_path) + 1 >
sizeof(addr_un.sun_path)) {
2039 fprintf(stderr,
"%s: socket name is too long\n", prog);
2040 return -ENAMETOOLONG;
2042 addr_un.sun_family = AF_UNIX;
2043 strcpy(addr_un.sun_path, socket_path);
2044 return do_connect(AF_UNIX,
2045 (
struct sockaddr*)&addr_un,
2053 struct addrinfo* addrinfos;
2058 snprintf(service,
sizeof(service),
2060 r = getaddrinfo(address, service, NULL, &addrinfos);
2063 fprintf(stderr,
"get_remote_socket: host %s unknown\n",
2065 return -EADDRNOTAVAIL;
2067 for (a = addrinfos; a != NULL; a = a->ai_next) {
2068 r = do_connect(a->ai_family, a->ai_addr, a->ai_addrlen, quiet);
2072 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)