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 || strcspn(
string,
"\n")
233 != strcspn(ctx->
packet,
"\n")) {
240 if (strcasecmp(
string,
"SUCCESS") == 0) {
242 }
else if (strcasecmp(
string,
"END") == 0) {
243 logprintf(LIRC_NOTICE,
244 "lirc_command_run: status:END");
246 }
else if (strcasecmp(
string,
"ERROR") == 0) {
247 logprintf(LIRC_WARNING,
248 "%s: command failed: %s",
257 if (strcasecmp(
string,
"END") == 0) {
258 logprintf(LIRC_NOTICE,
259 "lirc_command_run: data:END, status:%d",
262 }
else if (strcasecmp(
string,
"DATA") == 0) {
266 logprintf(LIRC_DEBUG,
267 "data: bad packet: %s\n",
272 data_n = (__u32)strtoul(
string, &endptr, 0);
273 if (!*
string || *endptr)
285 strcpy(ctx->
reply,
"");
288 chk_write(STDOUT_FILENO,
string, strlen(
string),
290 chk_write(STDOUT_FILENO,
"\n", 1,
"reply (2)");
301 if (strcasecmp(
string,
"END") == 0) {
302 logprintf(LIRC_NOTICE,
303 "lirc_command_run: status:END, status:%d",
311 logprintf(LIRC_WARNING,
"%s: bad return packet\n", prog);
312 logprintf(LIRC_DEBUG,
"State %d: bad packet: %s\n", status,
string);
317 static void lirc_printf(
const char* format_str, ...)
324 va_start(ap, format_str);
325 vfprintf(stderr, format_str, ap);
330 static void lirc_perror(
const char* s)
341 if (prog == NULL || lirc_prog != NULL)
344 if (lirc_lircd >= 0) {
345 lirc_verbose = verbose;
346 lirc_prog = strdup(prog);
347 if (lirc_prog == NULL) {
348 lirc_printf(
"%s: out of memory\n", prog);
353 lirc_printf(
"%s: could not open socket: %s\n",
355 strerror(-lirc_lircd));
362 if (lirc_prog != NULL) {
366 if (lirc_buffer != NULL) {
370 return close(lirc_lircd);
374 static int lirc_readline(
char** line, FILE* f)
381 newline = (
char*)malloc(LIRC_READ + 1);
382 if (newline == NULL) {
383 lirc_printf(
"%s: out of memory\n", lirc_prog);
388 ret = fgets(newline + len, LIRC_READ + 1, f);
390 if (feof(f) && len > 0) {
398 len = strlen(newline);
399 if (newline[len - 1] ==
'\n') {
400 newline[len - 1] = 0;
405 enlargeline = (
char*)realloc(newline, len + 1 + LIRC_READ);
406 if (enlargeline == NULL) {
408 lirc_printf(
"%s: out of memory\n", lirc_prog);
411 newline = enlargeline;
416 static char* lirc_trim(
char* s)
420 while (s[0] ==
' ' || s[0] ==
'\t')
425 if (s[len] ==
' ' || s[len] ==
'\t')
435 static char lirc_parse_escape(
char** s,
const char* name,
int line)
438 unsigned int i, overflow, count;
439 int digits_found, digit;
479 while (++count < 3) {
481 if (c >=
'0' && c <=
'7') {
482 i = (i << 3) + c -
'0';
488 if (i > (1 << CHAR_BIT) - 1) {
489 i &= (1 << CHAR_BIT) - 1;
491 "%s: octal escape sequence out of range in %s:%d\n",
492 lirc_prog, name, line);
502 if (c >=
'0' && c <=
'9') {
504 }
else if (c >=
'a' && c <=
'f') {
505 digit = c -
'a' + 10;
506 }
else if (c >=
'A' && c <=
'F') {
507 digit = c -
'A' + 10;
512 overflow |= i ^ (i << 4 >> 4);
513 i = (i << 4) + digit;
517 lirc_printf(
"%s: \\x used with no " 518 "following hex digits in %s:%d\n",
519 lirc_prog, name, line);
520 if (overflow || i > (1 << CHAR_BIT) - 1) {
521 i &= (1 << CHAR_BIT) - 1;
522 lirc_printf(
"%s: hex escape sequence out " 523 "of range in %s:%d\n", lirc_prog, name,
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)
565 if (*s !=
'"' && *s !=
'<')
567 if (*s ==
'"' && last !=
'"')
569 else if (*s ==
'<' && last !=
'>')
572 memmove(s, s + 1, len - 2 + 1);
576 int lirc_mode(
char* token,
char* token2,
char** mode,
580 int (check) (
char* s),
586 new_entry = *new_config;
587 if (strcasecmp(token,
"begin") == 0) {
588 if (token2 == NULL) {
589 if (new_entry == NULL) {
592 if (new_entry == NULL) {
593 lirc_printf(
"%s: out of memory\n",
597 new_entry->prog = NULL;
598 new_entry->code = NULL;
599 new_entry->rep_delay = 0;
600 new_entry->ign_first_events = 0;
602 new_entry->config = NULL;
603 new_entry->change_mode = NULL;
604 new_entry->flags = none;
605 new_entry->mode = NULL;
606 new_entry->next_config = NULL;
607 new_entry->next_code = NULL;
608 new_entry->next = NULL;
609 *new_config = new_entry;
611 lirc_printf(
"%s: bad file format, %s:%d\n",
612 lirc_prog, name, line);
616 if (new_entry == NULL && *mode == NULL) {
617 *mode = strdup(token2);
621 lirc_printf(
"%s: bad file format, %s:%d\n",
622 lirc_prog, name, line);
626 }
else if (strcasecmp(token,
"end") == 0) {
627 if (token2 == NULL) {
628 if (new_entry != NULL) {
630 if (new_entry->prog == NULL) {
632 "%s: prog missing in config before line %d\n", lirc_prog,
634 lirc_freeconfigentries(new_entry);
638 if (strcasecmp(new_entry->prog,
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) {
660 "%s: out of memory\n",
667 new_entry->prog != NULL &&
668 strcasecmp(new_entry->prog,
672 list = new_entry->config;
673 while (list != NULL) {
674 if (check(list->string) == -1)
680 if (new_entry->rep_delay == 0 &&
682 new_entry->rep_delay = new_entry->rep -
686 "%s: %s:%d: 'end' without 'begin'\n",
687 lirc_prog, name, line);
692 if (new_entry != NULL) {
694 "%s: %s:%d: missing 'end' token\n",
695 lirc_prog, name, line);
698 if (strcasecmp(*mode, token2) == 0) {
702 lirc_printf(
"%s: \"%s\" doesn't " 703 "match mode \"%s\"\n",
704 lirc_prog, token2, *mode);
709 "%s: %s:%d: 'end %s' without 'begin'\n",
710 lirc_prog, name, line, token2);
715 lirc_printf(
"%s: unknown token \"%s\" in %s:%d ignored\n",
716 lirc_prog, token, name, line);
722 unsigned int lirc_flags(
char*
string)
728 s = strtok(
string,
" \t|");
730 if (strcasecmp(s,
"once") == 0)
732 else if (strcasecmp(s,
"quit") == 0)
734 else if (strcasecmp(s,
"mode") == 0)
736 else if (strcasecmp(s,
"startup_mode") == 0)
737 flags |= startup_mode;
738 else if (strcasecmp(s,
"toggle_reset") == 0)
739 flags |= toggle_reset;
741 lirc_printf(
"%s: unknown flag \"%s\"\n", lirc_prog, s);
742 s = strtok(NULL,
" \t");
757 static char* get_homepath(
void)
762 filename = malloc(MAXPATHLEN);
763 if (filename == NULL) {
764 lirc_printf(
"%s: out of memory\n", lirc_prog);
767 home = getenv(
"HOME");
768 home = home == NULL ?
"/" : home;
769 strncpy(filename, home, MAXPATHLEN);
770 if (filename[strlen(filename) - 1] ==
'/')
771 filename[strlen(filename) - 1] =
'\0';
781 static char* get_freedesktop_path(
void)
785 if (getenv(
"XDG_CONFIG_HOME") != NULL) {
786 path = malloc(MAXPATHLEN);
787 strncpy(path, getenv(
"XDG_CONFIG_HOME"), MAXPATHLEN);
788 strncat(path,
"/", MAXPATHLEN - strlen(path));
789 strncat(path,
CFG_LIRCRC, MAXPATHLEN - strlen(path));
791 path = get_homepath();
794 strncat(path,
"/.config/lircrc", MAXPATHLEN - strlen(path) - 1);
796 if (access(path, R_OK) != 0)
802 static char* lirc_getfilename(
const char* file,
const char* current_file)
807 filename = get_freedesktop_path();
808 if (filename == NULL) {
810 }
else if (strlen(filename) == 0) {
812 filename = get_homepath();
813 if (filename == NULL)
817 filename = realloc(filename, strlen(filename) + 1);
818 }
else if (strncmp(file,
"~/", 2) == 0) {
819 filename = get_homepath();
820 if (filename == NULL)
822 strcat(filename, file + 1);
823 filename = realloc(filename, strlen(filename) + 1);
824 }
else if (file[0] ==
'/' || current_file == NULL) {
826 filename = strdup(file);
827 if (filename == NULL) {
828 lirc_printf(
"%s: out of memory\n", lirc_prog);
833 int pathlen = strlen(current_file);
835 while (pathlen > 0 && current_file[pathlen - 1] !=
'/')
837 filename = (
char*)malloc(pathlen + strlen(file) + 1);
838 if (filename == NULL) {
839 lirc_printf(
"%s: out of memory\n", lirc_prog);
842 memcpy(filename, current_file, pathlen);
843 filename[pathlen] = 0;
844 strcat(filename, file);
850 static FILE* lirc_open(
const char* file,
851 const char* current_file,
857 filename = lirc_getfilename(file, current_file);
858 if (filename == NULL)
861 fin = fopen(filename,
"r");
862 if (fin == NULL && (file != NULL || errno != ENOENT)) {
863 lirc_printf(
"%s: could not open config file %s\n", lirc_prog,
865 lirc_perror(lirc_prog);
866 }
else if (fin == NULL) {
869 fin = fopen(root_file,
"r");
870 if (fin == NULL && errno == ENOENT) {
871 int save_errno = errno;
874 fin = fopen(root_file,
"r");
877 if (fin == NULL && errno != ENOENT) {
878 lirc_printf(
"%s: could not open config file %s\n",
880 lirc_perror(lirc_prog);
881 }
else if (fin == NULL) {
882 lirc_printf(
"%s: could not open config files " 883 "%s and %s\n", lirc_prog, filename,
885 lirc_perror(lirc_prog);
888 filename = strdup(root_file);
889 if (filename == NULL) {
891 lirc_printf(
"%s: out of memory\n", lirc_prog);
896 if (full_name && fin != NULL)
897 *full_name = filename;
904 static struct filestack_t* stack_push(
struct filestack_t* parent)
906 struct filestack_t* entry;
908 entry = malloc(
sizeof(
struct filestack_t));
910 lirc_printf(
"%s: out of memory\n", lirc_prog);
916 entry->parent = parent;
921 static struct filestack_t* stack_pop(
struct filestack_t* entry)
923 struct filestack_t* parent = NULL;
926 parent = entry->parent;
935 static void stack_free(
struct filestack_t* entry)
938 entry = stack_pop(entry);
950 while (scan != NULL) {
951 if (scan->flags & startup_mode) {
952 if (scan->change_mode != NULL) {
953 startupmode = scan->change_mode;
955 scan->change_mode = NULL;
958 lirc_printf(
"%s: startup_mode flags requires 'mode ='\n", lirc_prog);
964 if (startupmode == NULL) {
966 while (scan != NULL) {
967 if (scan->mode != NULL
968 && strcasecmp(lirc_prog, scan->mode) == 0) {
969 startupmode = lirc_prog;
976 if (startupmode == NULL)
979 while (scan != NULL) {
980 if (scan->change_mode != NULL
981 && scan->flags & once
982 && strcasecmp(startupmode, scan->change_mode) == 0)
1004 free(c->change_mode);
1009 while (code != NULL) {
1010 if (code->remote != NULL && code->remote != LIRC_ALL)
1012 if (code->button != NULL && code->button != LIRC_ALL)
1014 code_temp = code->next;
1020 while (list != NULL) {
1023 list_temp = list->next;
1027 config_temp = c->next;
1035 parse_shebang(
char* line,
int depth,
const char* path,
char* buff,
size_t size)
1039 const char*
const SHEBANG_MSG =
1040 "Warning: Use of deprecated lircrc shebang." 1041 " Use lircrc_class instead.\n";
1043 token = strtok(line,
"#! ");
1046 lirc_printf(
"Warning: ignoring shebang in included file.");
1049 if (strcmp(token,
"lircrc") == 0) {
1050 strncpy(my_path, path,
sizeof(my_path) - 1);
1051 strncat(buff, basename(my_path), size - 1);
1052 lirc_printf(SHEBANG_MSG);
1054 lirc_printf(
"Warning: bad shebang (ignored)");
1059 static int lirc_readconfig_only_internal(
const char* file,
1061 int (check)(
char* s),
1064 const char*
const INCLUDED_LIRCRC_CLASS =
1065 "Warning: lirc_class in included file (ignored)";
1071 struct filestack_t* filestack;
1072 struct filestack_t* stack_tmp;
1074 char lircrc_class[128] = {
'\0' };
1082 char* save_full_name = NULL;
1084 filestack = stack_push(NULL);
1085 if (filestack == NULL)
1087 filestack->file = lirc_open(file, NULL, &(filestack->name));
1088 if (filestack->file == NULL) {
1089 stack_free(filestack);
1092 filestack->line = 0;
1095 first = new_entry = last = NULL;
1099 ret = lirc_readline(&
string, filestack->file);
1100 if (ret == -1 ||
string == NULL) {
1101 fclose(filestack->file);
1102 if (open_files == 1 && full_name != NULL) {
1103 save_full_name = filestack->name;
1104 filestack->name = NULL;
1106 filestack = stack_pop(filestack);
1113 if (strncmp(
string,
"#!", 2) == 0) {
1114 parse_shebang(
string,
1118 sizeof(lircrc_class));
1122 eq = strchr(
string,
'=');
1124 token = strtok(
string,
" \t");
1125 if (token == NULL) {
1127 }
else if (token[0] ==
'#') {
1129 }
else if (strcasecmp(token,
"lircrc_class") == 0) {
1130 token2 = lirc_trim(strtok(NULL,
""));
1131 if (strlen(token2) == 0) {
1133 "Warning: no lircrc_class");
1134 }
else if (open_files == 1) {
1135 strncpy(lircrc_class,
1137 sizeof(lircrc_class) - 1);
1139 lirc_printf(INCLUDED_LIRCRC_CLASS);
1141 }
else if (strcasecmp(token,
"include") == 0) {
1142 if (open_files >= MAX_INCLUDES) {
1143 lirc_printf(
"%s: too many files " 1144 "included at %s:%d\n",
1145 lirc_prog, filestack->name,
1149 token2 = strtok(NULL,
"");
1150 token2 = lirc_trim(token2);
1151 lirc_parse_include(token2,
1154 stack_tmp = stack_push(filestack);
1155 if (stack_tmp == NULL) {
1163 stack_tmp->line = 0;
1164 if (stack_tmp->file) {
1166 filestack = stack_tmp;
1168 stack_pop(stack_tmp);
1174 token2 = strtok(NULL,
" \t");
1176 token3 = strtok(NULL,
" \t");
1177 if (token2 != NULL && token3 != NULL) {
1178 lirc_printf(
"%s: unexpected token in line %s:%d\n",
1179 lirc_prog, filestack->name, filestack->line);
1181 ret = lirc_mode(token, token2, &mode,
1184 check, filestack->name,
1187 if (remote != LIRC_ALL)
1195 if (new_entry != NULL) {
1196 lirc_freeconfigentries(
1205 token = lirc_trim(
string);
1206 token2 = lirc_trim(eq + 1);
1207 if (token[0] ==
'#') {
1209 }
else if (new_entry == NULL) {
1210 lirc_printf(
"%s: bad file format, %s:%d\n",
1211 lirc_prog, filestack->name,
1215 token2 = strdup(token2);
1216 if (token2 == NULL) {
1217 lirc_printf(
"%s: out of memory\n",
1220 }
else if (strcasecmp(token,
"prog") == 0) {
1221 if (new_entry->prog != NULL)
1222 free(new_entry->prog);
1223 new_entry->prog = token2;
1224 }
else if (strcasecmp(token,
"remote") == 0) {
1225 if (remote != LIRC_ALL)
1228 if (strcasecmp(
"*", token2) == 0) {
1234 }
else if (strcasecmp(token,
"button") == 0) {
1242 "%s: out of memory\n",
1246 code->remote = remote;
1249 code->button = LIRC_ALL;
1252 code->button = token2;
1256 if (new_entry->code == NULL)
1257 new_entry->code = code;
1259 new_entry->next_code->
1261 new_entry->next_code = code;
1262 if (remote != LIRC_ALL) {
1263 remote = strdup(remote);
1264 if (remote == NULL) {
1266 "%s: out of memory\n",
1272 }
else if (strcasecmp(token,
"delay") == 0) {
1276 new_entry->rep_delay = strtoul(token2,
1278 if ((new_entry->rep_delay ==
1279 ULONG_MAX && errno == ERANGE)
1280 || end[0] != 0 || strlen(token2) ==
1282 lirc_printf(
"%s: \"%s\" not" 1283 " a valid number for delay\n", lirc_prog,
1286 }
else if (strcasecmp(token,
"ignore_first_events") == 0) {
1290 new_entry->ign_first_events = strtoul(
1292 if ((new_entry->ign_first_events ==
1293 ULONG_MAX && errno == ERANGE)
1294 || end[0] != 0 || strlen(token2) ==
1296 lirc_printf(
"%s: \"%s\" not" 1297 " a valid number for ignore_first_events\n",
1300 }
else if (strcasecmp(token,
"repeat") == 0) {
1305 strtoul(token2, &end, 0);
1306 if ((new_entry->rep == ULONG_MAX &&
1308 || end[0] != 0 || strlen(token2) ==
1310 lirc_printf(
"%s: \"%s\" not" 1311 " a valid number for repeat\n", lirc_prog,
1314 }
else if (strcasecmp(token,
"config") == 0) {
1319 if (new_list == NULL) {
1322 "%s: out of memory\n",
1326 lirc_parse_string(token2,
1329 new_list->string = token2;
1330 new_list->next = NULL;
1331 if (new_entry->config == NULL)
1335 new_entry->next_config->
1337 new_entry->next_config =
1340 }
else if (strcasecmp(token,
"mode") == 0) {
1341 if (new_entry->change_mode != NULL)
1342 free(new_entry->change_mode);
1343 new_entry->change_mode = token2;
1344 }
else if (strcasecmp(token,
"flags") == 0) {
1345 new_entry->flags = lirc_flags(token2);
1350 "%s: unknown token \"%s\" in %s:%d ignored\n",
1351 lirc_prog, token, filestack->name,
1360 if (remote != LIRC_ALL)
1362 if (new_entry != NULL) {
1364 ret = lirc_mode(
"end", NULL, &mode, &new_entry, &first,
1365 &last, check,
"", 0);
1367 "%s: warning: end token missing at end of file\n",
1370 lirc_freeconfigentries(new_entry);
1377 "%s: warning: no end token found for mode \"%s\"\n", lirc_prog,
1386 if (*config == NULL) {
1387 lirc_printf(
"%s: out of memory\n", lirc_prog);
1388 lirc_freeconfigentries(first);
1391 (*config)->first = first;
1392 (*config)->next = first;
1393 startupmode = lirc_startupmode((*config)->first);
1394 (*config)->current_mode =
1395 startupmode ? strdup(startupmode) : NULL;
1396 if (lircrc_class[0] !=
'\0')
1397 (*config)->lircrc_class = strdup(lircrc_class);
1399 (*config)->lircrc_class = NULL;
1400 (*config)->sockfd = -1;
1401 if (full_name != NULL) {
1402 *full_name = save_full_name;
1403 save_full_name = NULL;
1407 lirc_freeconfigentries(first);
1410 stack_free(filestack);
1412 free(save_full_name);
1417 int lirc_identify(
int sockfd)
1427 while (ret == EAGAIN || ret == EWOULDBLOCK);
1428 return ret == 0 ? LIRC_RET_SUCCESS : -1;
1435 int (check)(
char* s))
1437 struct sockaddr_un addr;
1444 if (lirc_readconfig_only_internal(file, config, check, &filename) == -1)
1447 if ((*config)->lircrc_class == NULL)
1448 goto lirc_readconfig_compat;
1452 addr.sun_family = AF_UNIX;
1455 sizeof(addr.sun_path)) >
sizeof(addr.sun_path)) {
1456 lirc_printf(
"%s: WARNING: file name too long\n", lirc_prog);
1457 goto lirc_readconfig_compat;
1459 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1461 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1462 lirc_perror(lirc_prog);
1463 goto lirc_readconfig_compat;
1465 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1466 (*config)->sockfd = sockfd;
1470 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS)
1481 snprintf(command,
sizeof(command),
1482 "lircrcd %s", (*config)->lircrc_class);
1483 ret = system(command);
1484 if (ret == -1 || WEXITSTATUS(ret) != EXIT_SUCCESS)
1485 goto lirc_readconfig_compat;
1488 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1490 lirc_printf(
"%s: WARNING: could not open socket\n", lirc_prog);
1491 lirc_perror(lirc_prog);
1492 goto lirc_readconfig_compat;
1494 if (connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr)) != -1) {
1495 if (lirc_identify(sockfd) == LIRC_RET_SUCCESS) {
1496 (*config)->sockfd = sockfd;
1504 lirc_readconfig_compat:
1514 int (check) (
char* s))
1516 return lirc_readconfig_only_internal(file, config, check, NULL);
1522 if (config != NULL) {
1523 if (config->sockfd != -1) {
1524 (void)close(config->sockfd);
1525 config->sockfd = -1;
1529 lirc_freeconfigentries(config->first);
1530 free(config->current_mode);
1536 static void lirc_clearmode(
struct lirc_config* config)
1540 if (config->current_mode == NULL)
1542 scan = config->first;
1543 while (scan != NULL) {
1544 if (scan->change_mode != NULL)
1545 if (strcasecmp(scan->change_mode,
1546 config->current_mode) == 0)
1547 scan->flags &= ~ecno;
1550 free(config->current_mode);
1551 config->current_mode = NULL;
1555 static char* lirc_execute(
struct lirc_config* config,
1561 if (scan->flags & mode)
1562 lirc_clearmode(config);
1563 if (scan->change_mode != NULL) {
1564 free(config->current_mode);
1565 config->current_mode = strdup(scan->change_mode);
1566 if (scan->flags & once) {
1567 if (scan->flags & ecno)
1570 scan->flags |= ecno;
1573 if (scan->next_config != NULL
1574 && scan->prog != NULL
1575 && (lirc_prog == NULL || strcasecmp(scan->prog, lirc_prog) == 0)
1577 s = scan->next_config->string;
1578 scan->next_config = scan->next_config->next;
1579 if (scan->next_config == NULL)
1580 scan->next_config = scan->config;
1596 int delay_start, rep_delay;
1598 if (scan->ign_first_events) {
1599 if (scan->rep_delay && rep == 0)
1601 "%s: ignoring \"delay\" because \"ignore_first_events\" is also set\n",
1603 rep_delay = scan->ign_first_events;
1606 rep_delay = scan->rep_delay;
1610 if (rep < delay_start)
1613 if (scan->rep == 0 && rep_delay > 0 && rep == rep_delay + delay_start)
1616 if (scan->rep > 0 && rep >= rep_delay + delay_start) {
1617 rep -= rep_delay + delay_start;
1618 return (rep % scan->rep) == 0;
1631 if (scan->code == NULL)
1632 return rep_filter(scan, rep);
1635 if (scan->next_code->remote == LIRC_ALL
1636 || strcasecmp(scan->next_code->remote, remote) == 0) {
1637 if (scan->next_code->button == LIRC_ALL
1638 || strcasecmp(scan->next_code->button, button) == 0) {
1641 if (scan->code->next == NULL || rep == 0) {
1642 scan->next_code = scan->next_code->next;
1643 if (scan->code->next != NULL)
1647 if (scan->next_code == NULL) {
1648 scan->next_code = scan->code;
1649 if (scan->code->next != NULL ||
1650 rep_filter(scan, rep))
1661 if (scan->flags & toggle_reset)
1662 scan->next_config = scan->config;
1665 if (codes == scan->next_code)
1667 codes = codes->next;
1669 while (codes != scan->next_code->next) {
1676 while (next != scan->next_code) {
1677 if (prev->remote == LIRC_ALL
1678 || strcasecmp(prev->remote, next->remote) == 0) {
1679 if (prev->button == LIRC_ALL
1680 || strcasecmp(prev->button,
1681 next->button) == 0) {
1694 if (prev->remote == LIRC_ALL
1695 || strcasecmp(prev->remote, remote) == 0) {
1696 if (prev->button == LIRC_ALL
1697 || strcasecmp(prev->button, button) == 0) {
1699 scan->next_code = prev->next;
1705 codes = codes->next;
1707 scan->next_code = scan->code;
1714 static int warning = 1;
1718 fprintf(stderr,
"%s: warning: lirc_ir2char() is obsolete\n",
1728 static int lirc_code2char_internal(
struct lirc_config* config,
1743 if (sscanf(code,
"%*x %x %*s %*s\n", &rep) == 1) {
1744 backup = strdup(code);
1748 strtok(backup,
" ");
1750 button = strtok(NULL,
" ");
1751 remote = strtok(NULL,
"\n");
1753 if (button == NULL || remote == NULL) {
1758 scan = config->next;
1760 while (scan != NULL) {
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,
1767 config->current_mode) == 0)) &&
1768 quit_happened == 0) {
1769 if (exec_level > 1) {
1770 s = lirc_execute(config, scan);
1771 if (s != NULL && prog != NULL)
1776 if (scan->flags & quit) {
1778 config->next = NULL;
1781 }
else if (s != NULL) {
1782 config->next = scan->next;
1794 config->next = config->first;
1807 my_code = strdup(code);
1808 pos = rindex(my_code,
'\n');
1815 if (config->sockfd != -1) {
1818 while (ret == EAGAIN || ret == EWOULDBLOCK);
1821 *
string = static_buff;
1823 return ret == 0 ? 0 : -1;
1825 return lirc_code2char_internal(config, code,
string, NULL);
1829 int lirc_code2charprog(
struct lirc_config* config,
1840 ret = lirc_code2char_internal(config, code,
string, prog);
1849 static int warning = 1;
1854 fprintf(stderr,
"%s: warning: lirc_nextir() is obsolete\n",
1868 static int end_len = 0;
1874 if (lirc_buffer == NULL) {
1875 lirc_buffer = (
char*)malloc(packet_size + 1);
1876 if (lirc_buffer == NULL) {
1877 lirc_printf(
"%s: out of memory\n", lirc_prog);
1882 while ((end = strchr(lirc_buffer,
'\n')) == NULL) {
1883 if (end_len >= packet_size) {
1888 (
char*)realloc(lirc_buffer, packet_size + 1);
1889 if (new_buffer == NULL)
1891 lirc_buffer = new_buffer;
1893 len = read(lirc_lircd, lirc_buffer + end_len,
1894 packet_size - end_len);
1896 if (len == -1 && errno == EAGAIN)
1902 lirc_buffer[end_len] = 0;
1904 end = strchr(lirc_buffer,
'\n');
1911 end_len = strlen(end);
1914 *code = strdup(lirc_buffer);
1916 memmove(lirc_buffer, end, end_len + 1);
1925 id =
id != NULL ?
id :
"default";
1926 snprintf(buf, size, VARRUNDIR
"/%d-%s-lircrcd.socket", getuid(),
id);
1938 if (config->sockfd != -1) {
1942 while (ret == EAGAIN || ret == EWOULDBLOCK);
1949 return config->current_mode;
1959 if (config->sockfd != -1) {
1968 while (r == EAGAIN || r == EWOULDBLOCK);
1975 free(config->current_mode);
1976 config->current_mode = mode ? strdup(mode) : NULL;
1977 return config->current_mode;
1991 while (r == EAGAIN);
2006 scancode, repeat, keysym, remote);
2011 while (r == EAGAIN);
2018 do_connect(
int domain,
struct sockaddr* addr,
size_t size,
int quiet)
2022 fd = socket(domain, SOCK_STREAM, 0);
2025 fprintf(stderr,
"do_connect: could not open socket\n");
2030 if (connect(fd, addr, size) == -1) {
2033 "do_connect: could not connect to socket\n");
2044 const char* socket_path;
2045 struct sockaddr_un addr_un;
2047 socket_path = path ? path : getenv(
"LIRC_SOCKET_PATH");
2048 socket_path = socket_path ? socket_path :
LIRCD;
2049 if (strlen(socket_path) + 1 >
sizeof(addr_un.sun_path)) {
2052 fprintf(stderr,
"%s: socket name is too long\n", prog);
2053 return -ENAMETOOLONG;
2055 addr_un.sun_family = AF_UNIX;
2056 strcpy(addr_un.sun_path, socket_path);
2057 return do_connect(AF_UNIX,
2058 (
struct sockaddr*)&addr_un,
2066 struct addrinfo* addrinfos;
2071 snprintf(service,
sizeof(service),
2073 r = getaddrinfo(address, service, NULL, &addrinfos);
2076 fprintf(stderr,
"get_remote_socket: host %s unknown\n",
2078 return -EADDRNOTAVAIL;
2080 for (a = addrinfos; a != NULL; a = a->ai_next) {
2081 r = do_connect(a->ai_family, a->ai_addr, a->ai_addrlen, quiet);
2085 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)