diff --git a/zet.dpi.c b/zet.dpi.c
index c63e94c..af16b35 100644
--- a/zet.dpi.c
+++ b/zet.dpi.c
@@ -234,6 +234,20 @@ static int dpi_respond_err(int fd, const char *fmt, ...) {
return rc;
}
+static int html_error(int fd, const char *fmt, ...) {
+ int rc;
+ va_list ap;
+ int err = errno;
+ va_start(ap, fmt);
+ rc = write_buf(fd, "\nError:\n");
+ rc |= vdprintf(fd, fmt, ap);
+ if (err != 0) rc |= dprintf(fd, ": %s", strerror(err));
+ if (rc < 0) warn("dprintf");
+ va_end(ap);
+ if (close(fd) < 0) warn("close");
+ return 0;
+}
+
static char *zet_get_title_fd(int fd, char *buf, size_t len) {
if (fd < 0) return NULL;
int rc = read_some(fd, (unsigned char *)buf, &len);
@@ -383,8 +397,6 @@ static int zet_search_close(struct zet_search *sz) {
static int dpi_serve_zet_all(int fd) {
int rc;
struct zet_search zs;
- rc = zet_search_start(&zs, NULL, NULL);
- if (rc < 0) return dpi_respond_err(fd, "Unable to list");
rc = dpi_send_header(fd, "text/html");
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
rc = dprintf(fd,
@@ -394,19 +406,15 @@ static int dpi_serve_zet_all(int fd) {
""
"
");
rc |= write_topbar(fd, PAGE_ALL, NULL, NULL);
- rc |= write_buf(fd, "");
+ if (rc < 0) { warn("write"); close(fd); return 0; }
+ rc = zet_search_start(&zs, NULL, NULL);
+ if (rc < 0) return html_error(fd, "Unable to list");
+ rc = write_buf(fd, "");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
while (1) {
rc = zet_search_next(&zs, &id);
- if (rc < 0) {
- rc = dprintf(fd,
- "\nError: %s\n",
- strerror(errno));
- if (rc < 0) warnx("dprintf");
- close(fd);
- return 0;
- }
+ if (rc < 0) return html_error(fd, "Unable to search");
if (id == NULL) break;
char title_buf[128];
char *title = zet_get_title(id, title_buf, sizeof(title_buf));
@@ -442,8 +450,6 @@ static int dpi_serve_zet_recent(int fd) {
int capacity = 0;
int count = 0;
struct zet_mtime *zets = NULL;
- rc = zet_search_start(&zs, NULL, NULL);
- if (rc < 0) return dpi_respond_err(fd, "Unable to list");
rc = dpi_send_header(fd, "text/html");
if (rc < 0) { warn("dpi_send_header"); goto cleanup; }
rc = dprintf(fd,
@@ -453,6 +459,9 @@ static int dpi_serve_zet_recent(int fd) {
""
"");
rc |= write_topbar(fd, PAGE_RECENT, NULL, NULL);
+ if (rc < 0) { warn("write"); goto cleanup; }
+ rc = zet_search_start(&zs, NULL, NULL);
+ if (rc < 0) return html_error(fd, "Unable to list");
rc |= write_buf(fd, "");
if (rc < 0) { warn("write"); goto cleanup; }
const char *id;
@@ -541,9 +550,6 @@ static int dpi_serve_zet_search(int fd, char *qs) {
query = query_unescaped;
}
- rc = zet_search_start(&zs, NULL, query);
- if (rc < 0) return dpi_respond_err(fd, "Unable to list");
-
rc = dpi_send_header(fd, "text/html");
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
rc = dprintf(fd,
@@ -554,19 +560,15 @@ static int dpi_serve_zet_search(int fd, char *qs) {
""
);
rc |= write_topbar(fd, PAGE_SEARCH, query, NULL);
- rc |= write_buf(fd, "");
+ if (rc < 0) { warn("write"); close(fd); return 0; }
+ rc = zet_search_start(&zs, NULL, query);
+ if (rc < 0) return html_error(fd, "Unable to list");
+ rc = write_buf(fd, "");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
while (1) {
rc = zet_search_next(&zs, &id);
- if (rc < 0) {
- rc = dprintf(fd,
- "\nError: %s\n",
- strerror(errno));
- if (rc < 0) warnx("dprintf");
- close(fd);
- return 0;
- }
+ if (rc < 0) return html_error(fd, "Unable to search");
if (id == NULL) break;
char title_buf[128];
char *title = zet_get_title(id, title_buf, sizeof(title_buf));
@@ -859,21 +861,13 @@ static int dpi_serve_zet(int fd, char *path) {
);
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET);
- if (rc < 0) {
- rc = dprintf(fd, "\nError: %s", strerror(errno));
- if (rc < 0) warnx("dprintf");
- close(fd);
- }
+ if (rc < 0) return html_error(fd, "Unable to read file");
} else {
rc |= write_buf(fd, "");
}
char *text = note_fd >= 0 ? read_full(note_fd) : NULL;
- if (text == NULL) {
- rc = dprintf(fd, "\nError: %s", strerror(errno));
- if (rc < 0) warnx("dprintf");
- close(fd);
- }
+ if (text == NULL) return html_error(fd, "Unable to read file");
struct sd_callbacks callbacks;
struct zet_renderopt options;
@@ -912,7 +906,7 @@ static int dpi_serve_zet(int fd, char *path) {
struct zet_search zs;
bool first = true;
rc = zet_search_start(&zs, id, NULL);
- if (rc < 0) warn("zet_search_start");
+ if (rc < 0) return html_error(fd, "Unable to list");
do {
const char *result_id;
rc = zet_search_next(&zs, &result_id);