1From 9a51dbca603267fd65a64b38e30a356affba38e3 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Thu, 7 Mar 2019 18:20:44 +0800
4Subject: [PATCH 2/2] Support parsing <configfile>.d/*.conf
5
6Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
7---
8 input-event-daemon.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++------
9 1 file changed, 61 insertions(+), 8 deletions(-)
10
11diff --git a/input-event-daemon.c b/input-event-daemon.c
12index 2a7b527..a324382 100644
13--- a/input-event-daemon.c
14+++ b/input-event-daemon.c
15@@ -4,6 +4,7 @@
16 #include <unistd.h>
17
18 #include <ctype.h>
19+#include <dirent.h>
20 #include <getopt.h>
21 #include <fcntl.h>
22 #include <errno.h>
23@@ -11,12 +12,15 @@
24
25 #include <sys/wait.h>
26 #include <sys/select.h>
27+#include <sys/stat.h>
28+#include <sys/types.h>
29
30 #include <linux/input.h>
31
32 #include "input-event-daemon.h"
33 #include "input-event-table.h"
34
35+#define DEFAULT_CONFIGURE_FILE "/etc/input-event-daemon.conf";
36
37 static int key_event_compare(const key_event_t *a, const key_event_t *b) {
38     int i, r_cmp;
39@@ -385,7 +389,7 @@ static void input_parse_event(struct input_event *event, const char *src) {
40 }
41
42
43-void config_parse_file() {
44+void config_parse_file(const char *configfile) {
45     FILE *config_fd;
46     char buffer[512], *line;
47     char *section = NULL;
48@@ -394,9 +398,13 @@ void config_parse_file() {
49     int line_num = 0;
50     int listen_len = 0;
51
52-    if((config_fd = fopen(conf.configfile, "r")) == NULL) {
53+    if(conf.verbose) {
54+        fprintf(stderr, PROGRAM": Start parsing %s...\n", configfile);
55+    }
56+
57+    if((config_fd = fopen(configfile, "r")) == NULL) {
58         fprintf(stderr, PROGRAM": fopen(%s): %s\n",
59-            conf.configfile, strerror(errno));
60+            configfile, strerror(errno));
61         exit(EXIT_FAILURE);
62     }
63
64@@ -461,7 +469,7 @@ void config_parse_file() {
65         print_error:
66         if(error != NULL) {
67             fprintf(stderr, PROGRAM": %s (%s:%d)\n",
68-                error, conf.configfile, line_num);
69+                error, configfile, line_num);
70         }
71
72
73@@ -483,6 +491,46 @@ void config_parse_file() {
74     fclose(config_fd);
75 }
76
77+//Base on triggerhappy's trigger.c
78+static int accept_configure_file(const struct dirent *entry) {
79+    const char *suffix = ".conf";
80+    const char *name = entry->d_name;
81+    char *end = strstr( name, suffix );
82+    if ( end && end[ strlen(suffix) ] == '\0' ) {
83+        return 1;
84+    } else {
85+        return 0;
86+    }
87+}
88+
89+//Base on triggerhappy's trigger.c
90+void config_parse_dir(const char *path) {
91+    struct stat sb;
92+    struct dirent **namelist;
93+    int n;
94+
95+    if (stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode))
96+        return;
97+
98+    n = scandir(path, &namelist, accept_configure_file, alphasort);
99+    if ( n < 0)
100+        return;
101+
102+    while (n--) {
103+        struct stat sf;
104+        char *file = namelist[n]->d_name;
105+        char *sep = "/";
106+        char fpath[strlen(path)+strlen(sep)+strlen(file) + 1];
107+        strcpy(fpath, path);
108+        strcat(fpath, sep);
109+        strcat(fpath, file);
110+        if (stat(fpath, &sf) != -1 && S_ISREG(sf.st_mode))
111+            config_parse_file(fpath);
112+        free(namelist[n]);
113+    }
114+    free(namelist);
115+}
116+
117 static const char *config_key_event(char *shortcut, char *exec) {
118     int i;
119     char *code, *modifier;
120@@ -619,8 +667,6 @@ static char *config_trim_string(char *str) {
121 void daemon_init() {
122     int i;
123
124-    conf.configfile  = "/etc/input-event-daemon.conf";
125-
126     conf.monitor     = 0;
127     conf.verbose     = 0;
128     conf.daemon      = 1;
129@@ -814,6 +860,7 @@ static void daemon_print_version() {
130 }
131
132 int main(int argc, char *argv[]) {
133+    const char *configfile = DEFAULT_CONFIGURE_FILE;
134     int result, arguments = 0, listen_len = 0;
135     static const struct option long_options[] = {
136         { "monitor",   no_argument,       0, 'm' },
137@@ -859,7 +906,7 @@ int main(int argc, char *argv[]) {
138                 return EXIT_SUCCESS;
139                 break;
140             case 'c': /* config */
141-                conf.configfile = optarg;
142+                configfile = optarg;
143                 break;
144             case 'v': /* verbose */
145                 conf.verbose = 1;
146@@ -891,7 +938,13 @@ int main(int argc, char *argv[]) {
147         if (!conf.listen[0])
148             input_open_all_listener();
149     } else {
150-        config_parse_file();
151+        char path[strlen(configfile) + 3];
152+
153+        config_parse_file(configfile);
154+
155+        strcpy(path, configfile);
156+        strcat(path, ".d");
157+        config_parse_dir(path);
158     }
159
160     daemon_start_listener();
161--
1622.11.0
163
164