aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c9
-rw-r--r--page.c74
-rw-r--r--page.h2
3 files changed, 82 insertions, 3 deletions
diff --git a/main.c b/main.c
index 19178f4..762db88 100644
--- a/main.c
+++ b/main.c
@@ -621,6 +621,7 @@ struct search_args
static int search_fn(const char *const fpath, const struct stat *const sb,
bool *const done, void *const user)
{
+ static const size_t limit = 200;
const struct search_args *const sa = user;
const char *rel = fpath + strlen(sa->root);
struct page_search *const res = sa->s;
@@ -650,7 +651,13 @@ static int search_fn(const char *const fpath, const struct stat *const sb,
}
res->results = results;
- res->n++;
+
+ if (++res->n >= limit)
+ {
+ sa->s->limit_exceeded = true;
+ *done = true;
+ }
+
return 0;
failure:
diff --git a/page.c b/page.c
index 6093d7f..32cc071 100644
--- a/page.c
+++ b/page.c
@@ -1766,6 +1766,76 @@ static int add_search_results(struct html_node *const n,
return 0;
}
+static int prepare_search_limit_exceeded(struct html_node *const n,
+ const struct page_search *const s)
+{
+ static const char msg[] = "Too many results were found. Please "
+ "set up a more specific search term in order to provide "
+ "fewer and more relevant results";
+ struct html_node *const p = html_node_add_child(n, "p");
+
+ if (!p)
+ {
+ fprintf(stderr, "%s: html_node_add_child p failed\n", __func__);
+ return -1;
+ }
+ else if (html_node_set_value(p, msg))
+ {
+ fprintf(stderr, "%s: html_node_set_value failed\n", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int prepare_search_result_count(struct html_node *const n,
+ const struct page_search *const s)
+{
+ int ret = -1;
+ struct html_node *const p = html_node_add_child(n, "p");
+ struct dynstr d;
+
+ dynstr_init(&d);
+
+ if (!p)
+ {
+ fprintf(stderr, "%s: html_node_add_child p failed\n", __func__);
+ goto end;
+ }
+ else if (s->limit_exceeded && prepare_search_limit_exceeded(n, s))
+ {
+ fprintf(stderr, "%s: prepare_search_limit_exceeded msg failed\n",
+ __func__);
+ goto end;
+ }
+ else if (!s->n && html_node_set_value(p, "No results found"))
+ {
+ fprintf(stderr, "%s: html_node_set_value msg failed\n", __func__);
+ goto end;
+ }
+ else if (s->n)
+ {
+ const char *const results = s->n == 1 ? "result" : "results";
+
+ if (dynstr_append(&d, "%zu %s found", s->n, results))
+ {
+ fprintf(stderr, "%s: dynstr_append failed\n", __func__);
+ goto end;
+ }
+ else if (html_node_set_value(p, d.str))
+ {
+ fprintf(stderr, "%s: html_node_set_value p failed\n", __func__);
+ goto end;
+ }
+ }
+
+ ret = 0;
+
+end:
+ dynstr_free(&d);
+ return ret;
+}
+
int page_search(struct http_response *const r,
const struct page_search *const s)
{
@@ -1800,9 +1870,9 @@ int page_search(struct http_response *const r,
fprintf(stderr, "%s: add_search_results failed\n", __func__);
goto end;
}
- else if (!s->n && html_node_set_value(body, "No results found"))
+ else if (prepare_search_result_count(body, s))
{
- fprintf(stderr, "%s: html_node_set_value msg failed\n", __func__);
+ fprintf(stderr, "%s: prepare_limit_exceeded failed\n", __func__);
goto end;
}
else if (prepare_footer(body))
diff --git a/page.h b/page.h
index 749ebd0..7d477ff 100644
--- a/page.h
+++ b/page.h
@@ -2,6 +2,7 @@
#define PAGE_H
#include "http.h"
+#include <stdbool.h>
#include <stddef.h>
struct page_quota
@@ -27,6 +28,7 @@ struct page_search
const char *root;
size_t n;
+ bool limit_exceeded;
};
struct page_rm