reddit: gopher: output valid directory on error, fail hard on pledge/unveil error - frontends - front-ends for some sites (experiment) (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 9f0f2a73c85c3fc233e5ad5e806c4e42c72e838f (DIR) parent 0919d051729baad9fd8adb9d1bfd090a86088149 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Sat, 2 May 2020 16:40:41 +0200 reddit: gopher: output valid directory on error, fail hard on pledge/unveil error Diffstat: M reddit/gopher.c | 44 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 15 deletions(-) --- (DIR) diff --git a/reddit/gopher.c b/reddit/gopher.c @@ -35,6 +35,12 @@ line(int _type, const char *username, const char *selector) } void +error(const char *s) +{ + line('3', s, ""); +} + +void info(const char *s) { line('i', s, ""); @@ -63,6 +69,16 @@ page(int _type, const char *username, const char *page) } void +header(void) +{ +} + +void footer(void) +{ + printf(".\r\n"); +} + +void printitem(struct item *item) { if (!item || !item->title[0]) @@ -161,10 +177,10 @@ render(struct list_response *r) { size_t i; - if (pledge("stdio", NULL) == -1) { - fprintf(stderr, "pledge: %s\n", strerror(errno)); + header(); + + if (pledge("stdio", NULL) == -1) exit(1); - } if (render_pagination(r)) info(""); @@ -174,6 +190,8 @@ render(struct list_response *r) render_pagination(r); + footer(); + return 0; } @@ -191,18 +209,11 @@ main(int argc, char *argv[]) struct list_response *r; char *querystring, *p, *search; - if (pledge("stdio dns inet rpath unveil", NULL) == -1) { - fprintf(stderr, "pledge: %s\n", strerror(errno)); + if (pledge("stdio dns inet rpath unveil", NULL) == -1) exit(1); - } - if (unveil(TLS_CA_CERT_FILE, "r") == -1) { - fprintf(stderr, "unveil: %s\n", strerror(errno)); - exit(1); - } - if (unveil(NULL, NULL) == -1) { - fprintf(stderr, "unveil: %s\n", strerror(errno)); + if (unveil(TLS_CA_CERT_FILE, "r") == -1 || + unveil(NULL, NULL) == -1) exit(1); - } if ((p = getenv("SERVER_NAME"))) host = p; @@ -216,7 +227,9 @@ main(int argc, char *argv[]) if (decodeparam(subreddit, sizeof(subreddit), p) == -1) subreddit[0] = '\0'; } else { - search = getenv("X_GOPHER_SEARCH"); + if (!(search = getenv("X_GOPHER_SEARCH"))) /* geomyidae */ + search = getenv("SEARCHREQUEST"); /* gophernicus */ + if (search && !uriencode(search, subreddit, sizeof(subreddit))) usage(); } @@ -237,7 +250,8 @@ main(int argc, char *argv[]) r = reddit_list(subreddit, 100, before, after); if (!r || r->nitems == 0) { - printf("iNo items found\t\t%s\t%s\r\n", host, port); + error("No items found"); + printf(".\r\n"); exit(1); }