Show textual regex error

main
cel 5 years ago
parent b5c2984c6c
commit 844672ad84
Signed by: cel
GPG Key ID: C28D95BB012367EA

@ -351,29 +351,27 @@ static int zet_matches_regex(const char *id, regex_t *regex) {
return found ? 1 : 0;
}
static int zet_search_init_query(struct zet_search *zs, const char *query) {
static int zet_search_init_query(struct zet_search *zs, const char *query, char *errbuf, size_t errbuf_size) {
int rc = regcomp(&zs->regex, query, REG_NOSUB | REG_EXTENDED | REG_ICASE);
if (rc != 0) {
char errbuf[LINE_MAX];
size_t sz = regerror(rc, &zs->regex, errbuf, sizeof(errbuf));
if (sz > sizeof(errbuf)-1) {
errbuf[sizeof(errbuf)-2] = '.';
errbuf[sizeof(errbuf)-3] = '.';
errbuf[sizeof(errbuf)-4] = '.';
}
warnx("regex: %s", errbuf);
size_t sz = regerror(rc, &zs->regex, errbuf, errbuf_size);
if (sz > errbuf_size-1 && errbuf_size >= 4) {
errbuf[errbuf_size-2] = '.';
errbuf[errbuf_size-3] = '.';
errbuf[errbuf_size-4] = '.';
}
return -1;
}
return 0;
}
static int zet_search_start(struct zet_search *zs, const char *id, char *query) {
static int zet_search_start(struct zet_search *zs, const char *id, const char *query, char *errbuf, size_t errbuf_size) {
zs->dir = opendir(zet_dir);
if (zs->dir == NULL) return -1;
zs->id = id;
if (query != NULL) {
zs->has_regex = true;
return zet_search_init_query(zs, query);
return zet_search_init_query(zs, query, errbuf, errbuf_size);
} else {
zs->has_regex = false;
return 0;
@ -413,7 +411,7 @@ static int zet_search_next(struct zet_search *zs, const char **idp) {
}
static int zet_search_close(struct zet_search *sz) {
regfree(&sz->regex);
if (sz->has_regex) regfree(&sz->regex);
return closedir(sz->dir);
}
@ -430,8 +428,9 @@ static int dpi_serve_zet_all(int fd) {
"<body style=\"margin:0\">");
rc |= write_topbar(fd, PAGE_ALL, NULL, NULL);
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");
char errbuf[LINE_MAX] = "";
rc = zet_search_start(&zs, NULL, NULL, errbuf, sizeof(errbuf));
if (rc < 0) return html_error(fd, "Unable to list: %s", errbuf);
rc = write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
@ -483,9 +482,10 @@ static int dpi_serve_zet_recent(int fd) {
"<body style=\"margin:0\">");
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, "<ul>");
char errbuf[LINE_MAX] = "";
rc = zet_search_start(&zs, NULL, NULL, errbuf, sizeof(errbuf));
if (rc < 0) return html_error(fd, "Unable to list: %s", errbuf);
rc = write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); goto cleanup; }
const char *id;
for (i = 0;; i++) {
@ -584,8 +584,9 @@ static int dpi_serve_zet_search(int fd, char *qs) {
);
rc |= write_topbar(fd, PAGE_SEARCH, query, NULL);
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");
char errbuf[LINE_MAX] = "";
rc = zet_search_start(&zs, NULL, query, errbuf, sizeof(errbuf));
if (rc < 0) return html_error(fd, "Unable to list: %s", errbuf);
rc = write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
@ -928,8 +929,9 @@ 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) return html_error(fd, "Unable to list");
char errbuf[LINE_MAX] = "";
rc = zet_search_start(&zs, id, NULL, errbuf, sizeof(errbuf));
if (rc < 0) return html_error(fd, "Unable to list: %s", errbuf);
do {
const char *result_id;
rc = zet_search_next(&zs, &result_id);

Loading…
Cancel
Save