cgmnlm

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/cgmnlm.git
Log | Files | Refs | README | LICENSE

commit e84d5c5ed7c675c97c8bb4d6c6e4d43e02d5e81b
parent 1798cac710b18d0f05cfb69b93e7c80fc73b3eef
Author: René Wagner <rwa@clttr.info>
Date:   Sun, 31 Oct 2021 19:41:51 +0100

unify behavior of p, e and t commands in cgmnlm

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

diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -86,12 +86,12 @@ const char *help_msg = "The following commands are available:\n\n" "<Enter>\t\tread more lines (if available)\n" "<url>\t\tgo to url\n" - "[N]\t\tFollow Nth link (where N is a number)\n" - "p[N]\t\tPrint URL of Nth link (where N is a number)\n" - "e[N]\t\tSend URL of Nth link or current URL when N is ommited to external default program\n" - "t[N]\t\tDownload content of Nth link to a temporary file\n" - "b[N]\t\tJump back N entries in history, N is optional, default 1\n" - "f[N]\t\tJump forward N entries in history, N is optional, default 1\n" + "[N]\t\tFollow Nth link\n" + "p[N]\t\tPrint URL of Nth link\n" + "e[N]\t\tSend URL of current page or Nth link to external default program (N is optional)\n" + "t[N]\t\tDownload content of current page or Nth link to a temporary file (N is optional)\n" + "b[N]\t\tJump back N entries in history (N is optional, default 1)\n" + "f[N]\t\tJump forward N entries in history (N is optional, default 1)\n" "u\t\tone path element up\n" "H\t\tView all page history\n" "m\t\tSave bookmark\n" @@ -107,6 +107,8 @@ const char *help_msg = "a\t\ttoggle usage of alt text instead of preformatted text\n" "q\t\tQuit\n" "\n" + "[N] must be replaced with a number > 0\n" + "\n" ; static void @@ -668,7 +670,7 @@ do_prompts(const char *prompt, struct browser *browser) goto exit; case 'u':; int keep = 0; - int len = strlen(browser->plain_url); + int len = strlen(browser->plain_url); for (int i=0; i<len; i++) { // ignore trailing / on uri path @@ -744,50 +746,44 @@ do_prompts(const char *prompt, struct browser *browser) case 'p': case 't': if (!in[1]) { - if (in[0] == 'e') { - snprintf(url, sizeof(url), "xdg-open %s", browser->plain_url); - if ( !system(url) ) fprintf(browser->tty, "Link send to xdg-open\n"); - goto exit; - } else { - break; + strncpy(&url[0], browser->plain_url, sizeof(url)-1); + } else { + 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 { + strncpy(&url[0], link->url, sizeof(url)-1); + } } } - linksel = (int)strtol(in+1, &endptr, 10); - if (!endptr[0] && linksel >= 0) { - while (linksel > 0 && link) { - link = link->next; - --linksel; + if (url[0]) { + fprintf(browser->tty, "=> %s\n", url); + if (in[0] == 'e') { + char target[2048] = {0}; + snprintf(target, sizeof(target), "xdg-open %s", url); + if ( !system(target) ) fprintf(browser->tty, "Link send to xdg-open\n"); } - - if (!link) { - fprintf(stderr, "Error: no such link.\n"); - } else { - fprintf(browser->tty, "=> %s\n", link->url); - if (in[0] == 'e') { - snprintf(url, sizeof(url), "xdg-open %s", link->url); - if ( !system(url) ) fprintf(browser->tty, "Link send to xdg-open\n"); - } - if (in[0] == 't') { - struct gemini_response resp; - strncpy(&url[0], browser->plain_url, sizeof(link->url)-1); - set_url(browser, link->url, &browser->history); - // XXX: may affect history, do we care? - enum gemini_result res = do_requests(browser, &resp); - if (res != GEMINI_OK) { - fprintf(stderr, "Error: %s\n", - gemini_strerr(res, &resp)); - } else { - char tempfile[] = "/tmp/cgmnlm_XXXXXX"; - mkstemp(tempfile); - download_resp(browser->tty, resp, tempfile, link->url); - } - gemini_response_finish(&resp); - set_url(browser, url, NULL); + if (in[0] == 't') { + struct gemini_response resp; + set_url(browser, url, &browser->history); + enum gemini_result res = do_requests(browser, &resp); + if (res != GEMINI_OK) { + fprintf(stderr, "Error: %s\n", + gemini_strerr(res, &resp)); + } else { + char tempfile[] = "/tmp/cgmnlm_XXXXXXXX"; + mkstemp(tempfile); + download_resp(browser->tty, resp, tempfile, url); } - fprintf(browser->tty, "\n"); + gemini_response_finish(&resp); + set_url(browser, url, NULL); } - } else { - fprintf(stderr, "Error: invalid argument.\n"); + fprintf(browser->tty, "\n"); } result = PROMPT_AGAIN; goto exit;