commit 5e6122e453f81b9ad5c1691a2982b50cd19c14eb
parent 520bdd531eb6136d69c95778ea4eb56dbba30f41
Author: libredev <libredev@ircforever.org>
Date: Wed, 15 Mar 2023 17:42:50 +0530
fix parsing multiple sources
Diffstat:
M | config.mk | | | 4 | +++- |
M | main.c | | | 76 | ++++++++++++++++++++++++++++++++++++---------------------------------------- |
2 files changed, 39 insertions(+), 41 deletions(-)
diff --git a/config.mk b/config.mk
@@ -6,9 +6,11 @@ ICECAST_URL=http://radio.example.com
INSTALL_DIR=/var/www/htdocs/radio.example.com
HTTPD_USER=httpd
HTTPD_GROUP=httpd
+MAX_SOURCES=10
CFLAGS = -g -std=c99 -Wall -Wextra -Wpedantic -Wfatal-errors\
-Wstrict-prototypes -Wold-style-definition\
- -D_DEFAULT_SOURCE -DICECAST_URL=\"$(ICECAST_URL)\" -DNAME=\""$(NAME)"\"
+ -D_DEFAULT_SOURCE -DMAX_SOURCES=$(MAX_SOURCES)\
+ -DICECAST_URL=\"$(ICECAST_URL)\" -DNAME=\""$(NAME)"\"
LDFLAGS =
diff --git a/main.c b/main.c
@@ -57,10 +57,10 @@ struct source {
char *url;
};
-struct icestats icestats;
-struct source *sources = NULL;
-int source_len = 0;
-int header_sent = FALSE;
+struct icestats icestats;
+struct source srces[MAX_SOURCES];
+int srclen;
+int header_sent;
void
not_found(void)
@@ -287,6 +287,7 @@ json_process(json_stream *json)
{
enum scope scope = SCOPE_NONE;
char *str;
+ int multisrc = FALSE;
while(1) {
enum json_type type = json_next(json);
switch (type) {
@@ -297,44 +298,48 @@ json_process(json_stream *json)
case JSON_STRING:
str = json_get_string(json, NULL);
switch (scope) {
- case SCOPE_NONE:
- fatal("%s: string in none scope");
- break;
case SCOPE_ROOT:
- if (strcmp(str, "icestats") == 0) {
- if (json_next(json) != JSON_OBJECT)
- fatal("icestats doesn't have any scope");
+ if (strcmp(str, "icestats") == 0)
scope = SCOPE_ICESTATES;
- } else {
- fatal("%s: undefined string in icestats scope");
- }
+ else
+ fatal("%s: undefined string in icestats scope", str);
break;
case SCOPE_ICESTATES:
- if (strcmp(str, "source") == 0) {
- if (json_next(json) != JSON_OBJECT)
- fatal("source doesn't have any scope");
+ if (strcmp(str, "source") == 0)
scope = SCOPE_SOURCE;
- source_len++;
- sources = erealloc(sources, (source_len) * sizeof(struct source));
- memset(&sources[source_len-1], 0, sizeof(struct source));
- } else {
+ else
icestats_set(&icestats, json, str);
- }
break;
case SCOPE_SOURCE:
- source_set(&sources[source_len-1], json, str);
+ source_set(&srces[srclen-1], json, str);
break;
+ default:
+ fatal("%s: string in undefined scope", str);
}
break;
case JSON_ARRAY:
+ if (scope != SCOPE_SOURCE)
+ fatal("undefined array");
+ multisrc = TRUE;
break;
case JSON_ARRAY_END:
+ if (scope != SCOPE_SOURCE)
+ fatal("undefined array");
+ scope = SCOPE_ICESTATES;
break;
case JSON_OBJECT:
- if (scope == SCOPE_NONE)
+ switch (scope) {
+ case SCOPE_NONE:
scope = SCOPE_ROOT;
- else
- fatal("unknown scope");
+ break;
+ case SCOPE_ICESTATES:
+ break;
+ case SCOPE_SOURCE:
+ srclen++;
+ break;
+ default:
+ fatal("undefined object");
+ }
break;
case JSON_OBJECT_END:
switch (scope) {
@@ -348,7 +353,8 @@ json_process(json_stream *json)
scope = SCOPE_ROOT;
break;
case SCOPE_SOURCE:
- scope = SCOPE_ICESTATES;
+ if (!multisrc)
+ scope = SCOPE_ICESTATES;
break;
}
break;
@@ -434,8 +440,8 @@ main(int argc, char *argv[])
if (strcmp(request_url, "/stream") == 0
|| *request_url == '\0') {
print_navigation_bar(0);
- for (int i = 0; i < source_len; i++)
- source_print(&sources[i]);
+ for (int i = 0; i < srclen; i++)
+ source_print(&srces[i]);
puts("<h2> Comments </h2>");
/* comment box */
@@ -488,15 +494,6 @@ main(int argc, char *argv[])
print_navigation_bar(3);
icestats_print(&icestats);
} else {
- /* for (int i = 0; i <= source_index; i++) {
- if (sources[i].url == NULL)
- continue;
- if (strcmp(request_url, sources[i].url) == 0) {
- print_navigation_bar(0);
- source_print(&sources[i]);
- goto out;
- }
- } */
not_found();
}
@@ -507,9 +504,8 @@ out:
file_close(f);
/* cleanup */
- for (int i = 0; i < source_len; i++)
- source_free(&sources[i]);
- free(sources);
+ for (int i = 0; i < srclen; i++)
+ source_free(&srces[i]);
icestats_free(&icestats);
return 0;