From f982fb95044581435e31c765487b45d5ee963519 Mon Sep 17 00:00:00 2001 From: arf20 Date: Tue, 2 Dec 2025 00:00:12 +0100 Subject: search actually working :D --- index.c | 21 +++++++++++++++++---- index.h | 3 ++- index.htm.tmpl | 20 ++++++++++++++++---- main.c | 37 ++++++++++++++++++++++++++++--------- search | Bin 46312 -> 47088 bytes 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/index.c b/index.c index 44864bb..57c05d7 100644 --- a/index.c +++ b/index.c @@ -115,6 +115,13 @@ results_insert(results_t *results, const node_data_t *result) results->results[results->size++] = result; } +void +results_destroy(results_t *results) +{ + free(results->results); + free(results); +} + int index_init() { @@ -138,7 +145,7 @@ index_deinit() } map_t * -index_new(size_t icapacity, const char *dir, int examine) +index_recurse(size_t size, const char *dir, int examine, size_t rootlen) { DIR *dirp = opendir(dir); if (!dirp) { @@ -147,7 +154,7 @@ index_new(size_t icapacity, const char *dir, int examine) return NULL; } - map_t *map = map_new(icapacity); + map_t *map = map_new(size); char path[4096]; struct dirent *de = NULL; @@ -165,6 +172,7 @@ index_new(size_t icapacity, const char *dir, int examine) /* stat it */ node_data_t *data = malloc(sizeof(node_data_t)); data->name = strdup(de->d_name); + data->path = strdup(&path[rootlen]); if (stat(path, &data->stat) < 0) { fprintf(stderr, "[index] error stat() %s: %s\n", path, strerror(errno)); @@ -183,7 +191,7 @@ index_new(size_t icapacity, const char *dir, int examine) /* recurse */ map_t *child = NULL; if (de->d_type == DT_DIR) { - index_new(icapacity, path, examine); + child = index_recurse(size, path, examine, rootlen); } map_insert(map, de->d_name, data, child); @@ -192,6 +200,11 @@ index_new(size_t icapacity, const char *dir, int examine) return map; } +map_t * +index_new(size_t size, const char *dir, int examine) { + return index_recurse(size, dir, examine, strlen(dir) + 1); +} + void index_lookup_substr(map_t *index, const char *query, results_t *results) @@ -200,7 +213,7 @@ index_lookup_substr(map_t *index, const char *query, if (!index->map[i].data) continue; - for (struct node_s *node = &index->map[i]; node->next; node = node->next) { + for (struct node_s *node = &index->map[i]; node; node = node->next) { if (strstr(node->data->name, query)) results_insert(results, node->data); if (node->child) diff --git a/index.h b/index.h index d31fd9b..187de94 100644 --- a/index.h +++ b/index.h @@ -33,7 +33,7 @@ typedef enum { } lookup_type_t; typedef struct { - char *name; + const char *name, *path; struct stat stat; const char *mime; } node_data_t; @@ -50,6 +50,7 @@ void index_deinit(); index_t index_new(size_t icapacity, const char *root, int examine); results_t *index_lookup(index_t index, lookup_type_t type, const char *query); void index_destroy(index_t index); +void results_destroy(results_t *results); #endif /* _INDEX_H */ diff --git a/index.htm.tmpl b/index.htm.tmpl index 1443aa0..fd21578 100644 --- a/index.htm.tmpl +++ b/index.htm.tmpl @@ -13,19 +13,16 @@ display: block; } -/* Style the form - display items horizontally */ .form-inline { display: flex; flex-flow: row wrap; align-items: center; } -/* Add some margins for each label */ .form-inline label { } -/* Style the input fields */ .form-inline input { flex-grow: 1; vertical-align: middle; @@ -37,6 +34,20 @@ border: none; } +.result { + margin-left: 1em; + margin-bottom: 1em; +} + +.name { + font-weight: bold; + font-size: 16pt; + margin: 0; +} + +.path { + +} @@ -50,10 +61,11 @@

Search all of the ARFNET content fast

- +
+ %s diff --git a/main.c b/main.c index 19d7730..9aa7e76 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,27 @@ static char *index_format_template = NULL; static index_t g_index = NULL; +static const char *result_html_template = + "

%s

%s
\n"; + + +static const char * +generate_results_html(results_t *results) +{ + static char buff[65535]; + + char *pos = buff; + + for (int i = 0; i < results->size; i++) { + pos += snprintf(pos, 65535 - (pos - buff), + result_html_template, + results->results[i]->name, + results->results[i]->path + ); + } + + return buff; +} enum MHD_Result answer_to_connection( void *cls, struct MHD_Connection *connection, @@ -69,8 +90,7 @@ enum MHD_Result answer_to_connection( int ret; if (strcmp(method, "GET") == 0 && strcmp(url, "/") == 0) { - snprintf(buff, BUFF_SIZE, - index_format_template); + snprintf(buff, BUFF_SIZE, index_format_template, "", ""); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); @@ -80,20 +100,19 @@ enum MHD_Result answer_to_connection( MHD_destroy_response(response); } else if (strcmp(method, "GET") == 0 && strcmp(url, "/query") == 0) { - snprintf(buff, BUFF_SIZE, - index_format_template); - const char *query = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "query"); results_t *results = index_lookup(g_index, LOOKUP_SUBSTR, query); - printf("results\n"); - for (size_t i = 0; i < results->size; i++) - printf("\t%s\n", results->results[i]->name); + + snprintf(buff, BUFF_SIZE, index_format_template, query, + generate_results_html(results)); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); + results_destroy(results); + printf("%d\n", 200); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); @@ -123,7 +142,7 @@ int main() { fseek(tf, 0, SEEK_END); size_t tfs = ftell(tf); rewind(tf); - index_format_template = malloc(tfs); + index_format_template = malloc(tfs + 1); fread(index_format_template, 1, tfs, tf); fclose(tf); index_format_template[tfs] = '\0'; diff --git a/search b/search index 9f8c51c..e7e9576 100755 Binary files a/search and b/search differ -- cgit v1.2.3