cgmnlm

gemini line mode browser - colorful & extended fork of https://git.sr.ht/~sircmpwn/gmni
git clone git://git.clttr.info/cgmnlm.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit b13f8a1d3d0c1c2fbb35523a6ca183b5d86e2718
parent 31ab29752c6bd7132df392353cfbe88ed97fd907
Author: René Wagner <rwa@clttr.info>
Date:   Mon, 15 Aug 2022 19:36:28 +0200

formatting more like upstream

Diffstat:
Msrc/gmnlm.c | 153+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 88 insertions(+), 65 deletions(-)

diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -646,23 +646,23 @@ do_prompts(const char *prompt, struct browser *browser) enum prompt_result result; fprintf(browser->tty, "%s", prompt); - struct link *link = browser->links; - char *endptr = NULL; - int linksel = 0; - int historyhops = 1; + size_t l = 0; + + char url[1024] = {0}; + char save_url[1024] = {0}; char *in = NULL; - size_t l = 0; ssize_t n = getline(&in, &l, browser->tty); if (n == -1 && feof(browser->tty)) { fputc('\n', browser->tty); result = PROMPT_QUIT; goto exit; } + in[n - 1] = 0; // Remove LF - - char url[1024] = {0}; - char save_url[1024] = {0}; + char *endptr; + + int historyhops = 1; int r; switch (in[0]) { case '\0': @@ -672,21 +672,6 @@ do_prompts(const char *prompt, struct browser *browser) if (in[1]) break; result = PROMPT_QUIT; goto exit; - case 's': - if (in[1]) break; - set_url(browser, "gemini://geminispace.info/search", &browser->history); - result = PROMPT_ANSWERED; - goto exit; - case 'a': - browser->alttext = !browser->alttext; - fprintf(browser->tty, "Alttext instead of preformatted block is now %s\n\n", browser->alttext ? "ENABLED" : "DISABLED"); - result = PROMPT_AGAIN; - goto exit; - case 'l': - snprintf(url, sizeof(url), "gemini://geminispace.info/backlinks?%s", browser->plain_url); - set_url(browser, url, &browser->history); - result = PROMPT_ANSWERED; - goto exit; case 'b': if (in[1] && isdigit(in[1])) { historyhops =(int)strtol(in+1, &endptr, 10); @@ -703,6 +688,21 @@ do_prompts(const char *prompt, struct browser *browser) set_url(browser, browser->history->url, NULL); result = PROMPT_ANSWERED; goto exit; + case 's': + if (in[1]) break; + set_url(browser, "gemini://geminispace.info/search", &browser->history); + result = PROMPT_ANSWERED; + goto exit; + case 'a': + browser->alttext = !browser->alttext; + fprintf(browser->tty, "Alttext instead of preformatted block is now %s\n\n", browser->alttext ? "ENABLED" : "DISABLED"); + result = PROMPT_AGAIN; + goto exit; + case 'l': + snprintf(url, sizeof(url), "gemini://geminispace.info/backlinks?%s", browser->plain_url); + set_url(browser, url, &browser->history); + result = PROMPT_ANSWERED; + goto exit; case 'f': if (in[1] && isdigit(in[1])) { historyhops =(int)strtol(in+1, &endptr, 10); @@ -761,47 +761,24 @@ do_prompts(const char *prompt, struct browser *browser) save_bookmark(browser, title ? trim_ws(title) : title); result = PROMPT_AGAIN; goto exit; - case 'k': - if (in[1]) break; - remove_bookmark(browser); - result = PROMPT_AGAIN; - goto exit; case 'M': if (in[1]) break; open_bookmarks(browser); result = PROMPT_ANSWERED; goto exit; - case '/': - if (!in[1]) break; - if ((r = regcomp(&browser->regex, &in[1], REG_EXTENDED)) != 0) { - static char buf[1024]; - r = regerror(r, &browser->regex, buf, sizeof(buf)); - assert(r < (int)sizeof(buf)); - fprintf(stderr, "Error: %s\n", buf); - result = PROMPT_AGAIN; - } else { - browser->searching = true; - result = PROMPT_ANSWERED; - } - goto exit_re; - case 'n': + case 'k': if (in[1]) break; - if (browser->searching) { - result = PROMPT_NEXT; - goto exit_re; - } else { - fprintf(stderr, "Cannot move to next result; we are not searching for anything\n"); - result = PROMPT_AGAIN; - goto exit; - } + remove_bookmark(browser); + result = PROMPT_AGAIN; + goto exit; case 'e': - case 'p': case 't': strncpy(&save_url[0], browser->plain_url, sizeof(url)-1); if (!in[1]) { strncpy(&url[0], browser->plain_url, sizeof(url)-1); } else { - linksel = (int)strtol(in+1, &endptr, 10); + struct link *link = browser->links; + int linksel = (int)strtol(in+1, &endptr, 10); if (!endptr[0] && linksel >= 0) { while (linksel > 0 && link) { link = link->next; @@ -840,6 +817,51 @@ do_prompts(const char *prompt, struct browser *browser) } result = PROMPT_AGAIN; goto exit; + case '/': + if (!in[1]) break; + if ((r = regcomp(&browser->regex, &in[1], REG_EXTENDED)) != 0) { + static char buf[1024]; + r = regerror(r, &browser->regex, buf, sizeof(buf)); + assert(r < (int)sizeof(buf)); + fprintf(stderr, "Error: %s\n", buf); + result = PROMPT_AGAIN; + } else { + browser->searching = true; + result = PROMPT_ANSWERED; + } + goto exit_re; + case 'n': + if (in[1]) break; + if (browser->searching) { + result = PROMPT_NEXT; + goto exit_re; + } else { + fprintf(stderr, "Cannot move to next result; we are not searching for anything\n"); + result = PROMPT_AGAIN; + goto exit; + } + case 'p': + if (!in[1]) break; + struct link *link = browser->links; + int linksel = (int)strtol(in+1, &endptr, 10); + if (!endptr[0] && linksel >= 0) { + while (linksel > 0 && link) { + link = link->next; + --linksel; + } + + if (!link) { + fprintf(stderr, "Error: no such link.\n"); + } else { + fprintf(browser->tty, "=> %s\n", link->url); + result = PROMPT_AGAIN; + goto exit; + } + } else { + fprintf(stderr, "Error: invalid argument.\n"); + } + result = PROMPT_AGAIN; + goto exit; case 'r': if (in[1]) break; result = PROMPT_ANSWERED; @@ -868,25 +890,25 @@ do_prompts(const char *prompt, struct browser *browser) d_url = link->url; } } - struct gemini_response resp; + struct gemini_response resp; char url[1024] = {0}, old_url[1024] = {0}; strncpy(&old_url[0], browser->plain_url, sizeof(url)-1); strncpy(&url[0], d_url, sizeof(url)-1); - // XXX: may affect history, do we care? + // XXX: may affect history, do we care? set_url(browser, url, NULL); - enum gemini_result res = do_requests(browser, &resp); - if (res != GEMINI_OK) { - fprintf(stderr, "Error: %s\n", - gemini_strerr(res, &resp)); - result = PROMPT_AGAIN; + enum gemini_result res = do_requests(browser, &resp); + if (res != GEMINI_OK) { + fprintf(stderr, "Error: %s\n", + gemini_strerr(res, &resp)); + result = PROMPT_AGAIN; set_url(browser, old_url, NULL); - goto exit; - } + goto exit; + } download_resp(browser->tty, resp, trim_ws(endptr), url); - gemini_response_finish(&resp); - result = PROMPT_AGAIN; + gemini_response_finish(&resp); + result = PROMPT_AGAIN; set_url(browser, old_url, NULL); - goto exit; + goto exit; case '|': strncpy(&url[0], browser->plain_url, sizeof(url)-1); res = do_requests(browser, &resp); @@ -908,7 +930,8 @@ do_prompts(const char *prompt, struct browser *browser) goto exit; } - linksel = (int)strtol(in, &endptr, 10); + struct link *link = browser->links; + int linksel = (int)strtol(in, &endptr, 10); if ((endptr[0] == '\0' || endptr[0] == '|') && linksel >= 0) { while (linksel > 0 && link) { link = link->next;