commit 4085912
uint
·
2026-01-27 15:53:28 +0000 UTC
parent b54b867
add simple conf parser, move config.def.h->config.h .
4 files changed,
+193,
-21
+168,
-0
1@@ -0,0 +1,168 @@
2+#include <stdio.h>
3+#include <stdlib.h>
4+#include <string.h>
5+#include <strings.h>
6+
7+#include "config.h"
8+#include "log.h"
9+
10+static int load_path(const char* path);
11+static int parse_bool(const char* s, bool* out);
12+static int set_kv(const char* k, const char* v);
13+static char* trim(char* s);
14+
15+/* defaults */
16+char media_dir[4096] = "/mnt/sto/Media/";
17+char server_addr[64] = "127.0.0.1";
18+int server_port = 8088;
19+bool verbose_log = true;
20+char cors_origin[1024] = "";
21+
22+static int load_path(const char* path)
23+{
24+ FILE* f = fopen(path, "r");
25+ if (!f)
26+ return -1;
27+
28+ char* line = NULL;
29+ size_t cap = 0;
30+ ssize_t n;
31+
32+ while ((n = getline(&line, &cap, f)) >= 0) {
33+ (void)n;
34+
35+ char* s = trim(line);
36+ if (s[0] == '\0')
37+ continue;
38+ if (s[0] == '#')
39+ continue;
40+
41+ char* eq = strchr(s, '=');
42+ if (!eq)
43+ continue;
44+
45+ *eq = '\0';
46+ char* k = trim(s);
47+ char* v = trim(eq + 1);
48+
49+ if (k[0] == '\0')
50+ continue;
51+
52+ (void)set_kv(k, v);
53+ }
54+
55+ free(line);
56+ fclose(f);
57+ return 0;
58+}
59+
60+static int parse_bool(const char* s, bool* out)
61+{
62+ if (!s || !out)
63+ return -1;
64+
65+ if (strcmp(s, "1") == 0 || strcasecmp(s, "true") == 0 || strcasecmp(s, "yes") == 0)
66+ *out = true;
67+ else if (strcmp(s, "0") == 0 || strcasecmp(s, "false") == 0 || strcasecmp(s, "no") == 0)
68+ *out = false;
69+ else
70+ return -1;
71+
72+ return 0;
73+}
74+
75+static int set_kv(const char* k, const char* v)
76+{
77+ /* cmp keys */
78+ if (strcmp(k, "media_dir") == 0) {
79+ snprintf(media_dir, sizeof(media_dir), "%s", v);
80+ return 0;
81+ }
82+
83+ if (strcmp(k, "server_addr") == 0) {
84+ snprintf(server_addr, sizeof(server_addr), "%s", v);
85+ return 0;
86+ }
87+
88+ if (strcmp(k, "server_port") == 0) {
89+ char* end = NULL;
90+ long p = strtol(v, &end, 10);
91+ if (!end || *end != '\0')
92+ return -1;
93+ if (p < 1 || p > 65535)
94+ return -1;
95+ server_port = (int)p;
96+ return 0;
97+ }
98+
99+ if (strcmp(k, "verbose_log") == 0) {
100+ bool b;
101+ if (parse_bool(v, &b) < 0)
102+ return -1;
103+ verbose_log = b;
104+ return 0;
105+ }
106+
107+ if (strcmp(k, "cors_origin") == 0) {
108+ snprintf(cors_origin, sizeof(cors_origin), "%s", v);
109+ return 0;
110+ }
111+
112+ /* unknown key */
113+ return 0;
114+}
115+
116+static char* trim(char* s)
117+{
118+ while (*s && (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'))
119+ s++;
120+
121+ char* e = s + strlen(s);
122+ while (e > s) {
123+ char c = e[-1];
124+ if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
125+ break;
126+ e--;
127+ }
128+ *e = '\0';
129+ return s;
130+}
131+
132+void config_load(void)
133+{
134+ const char* e = getenv("PARADOS_CONFIG");
135+ const char* path = "";
136+ if (e && e[0]) {
137+ (void)load_path(e);
138+ path = e;
139+ goto log;
140+ }
141+
142+ if (load_path("/etc/parados.conf") == 0) {
143+ path = "/etc/parados.conf";
144+ goto log;
145+ }
146+
147+ if (load_path("./parados.conf") == 0) {
148+ path = "./parados.conf";
149+ goto log;
150+ }
151+
152+ LOG(true, "CONF", "unable to load configuration... using defaults", e);
153+ return;
154+
155+log:
156+ LOG(true, "CONF", "Config File: %s", path);
157+ LOG(true, "CONF", "Media Directory: %s", media_dir);
158+ LOG(true, "CONF", "Server Address: %s", server_addr);
159+
160+ char port[8];
161+ snprintf(port, sizeof(port), "%d", server_port);
162+ LOG(true, "CONF", "Server Port: %s", port);
163+
164+ LOG(true, "CONF", "Verbose Logging: %s", (verbose_log) ? "true" : "false");
165+ LOG(true, "CONF", "CORS origins: %s", cors_origin);
166+
167+ return;
168+}
169+
+0,
-19
1@@ -1,19 +0,0 @@
2-#ifndef CONFIG_H
3-#define CONFIG_H
4-
5-#include <stdbool.h>
6-
7-static const char* media_dir = "/path/to/media";
8-static const char* server_addr = "127.0.0.1";
9-static const int server_port = 8088;
10-static const bool verbose_log = true;
11-static const char* cors_origin = "";
12-
13-enum {
14- HTTP_REQ_MAX = 2048,
15- HTTP_RESP_MAX = 512,
16- LISTEN_BACKLOG = 64,
17-};
18-
19-#endif /* CONFIG_H */
20-
+21,
-0
1@@ -0,0 +1,21 @@
2+#ifndef CONFIG_H
3+#define CONFIG_H
4+
5+#include <stdbool.h>
6+
7+extern char media_dir[4096];
8+extern char server_addr[64];
9+extern int server_port;
10+extern bool verbose_log;
11+extern char cors_origin[1024];
12+
13+enum {
14+ HTTP_REQ_MAX = 2048,
15+ HTTP_RESP_MAX = 512,
16+ LISTEN_BACKLOG = 64,
17+};
18+
19+void config_load(void);
20+
21+#endif /* CONFIG_H */
22+
+4,
-2
1@@ -1,6 +1,6 @@
2 /* parados
3 the simple home media server
4-
5+
6 this software is licensed under ISC
7 check LICENCE for more details
8 */
9@@ -73,6 +73,8 @@ void setup(void)
10 signal(SIGCHLD, SIG_IGN); /* reap children to prevent
11 them to turn into zombies */
12
13+ config_load();
14+
15 int ret = 1;
16 sock = socket(AF_INET, SOCK_STREAM, 0);
17 if (sock < 0)
18@@ -112,7 +114,7 @@ int main(void)
19 #ifdef __OpenBSD__
20 if (pledge("stdio inet", NULL) < 0)
21 die("pledge", EXIT_FAILURE);
22-#endif
23+#endif /* __OpenBSD__ */
24 run();
25 return EXIT_SUCCESS;
26 }