~sircmpwn/gmni

eb5c5470e8110b67e56226cdcdc497defac8c64d — Sebastiano Tronto 2 years ago 8c62393
Add d[N] to download Nth link

I wanted to add an optional [N] to 'd' and '|' to download/pipe the
Nth link instead of the current page. After getting the first one done,
I found out that '|' already has this option, so I fixed the relative
help line without changing that command.

All in all I don't know how useful this patch is, considering that
'[N]|' was already there. Feel free to discard it and just keep the
documentation change.

Cheers!
1 files changed, 26 insertions(+), 7 deletions(-)

M src/gmnlm.c
M src/gmnlm.c => src/gmnlm.c +26 -7
@@ 83,8 83,8 @@ const char *help_msg =
	"m [title]\tSave bookmark\n"
	"M\tBrowse bookmarks\n"
	"r\tReload the page\n"
	"d [path]\tDownload page to path\n"
	"|<prog>\tPipe page into program\n"
	"d[N] [path]\tDownload page, or Nth link, to path\n"
	"[N]|<prog>\tPipe page, or Nth link, into program\n"
	"\n"
	"Other commands include:\n\n"
	"<Enter>\tread more lines\n"


@@ 679,22 679,41 @@ do_prompts(const char *prompt, struct browser *browser)
		result = PROMPT_AGAIN;
		goto exit;
	case 'd':
		if (in[1] != '\0' && !isspace(in[1])) break;
		endptr = &in[1];
		char *d_url = browser->plain_url;
		if (in[1] != '\0' && !isspace(in[1])) {
			struct link *link = browser->links;
			int linksel = (int)strtol(in+1, &endptr, 10);
			while (linksel > 0 && link) {
				link = link->next;
				--linksel;
			}

			if (!link) {
				fprintf(stderr, "Error: no such link.\n");
				break;
			} else {
				d_url = link->url;
			}
		}
		struct gemini_response resp;
		char url[1024] = {0};
		strncpy(&url[0], browser->plain_url, sizeof(url)-1);
		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?
		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;
			set_url(browser, old_url, NULL);
			goto exit;
		}
		set_url(browser, url, NULL);
		download_resp(browser->tty, resp, trim_ws(&in[1]), url);
		download_resp(browser->tty, resp, trim_ws(endptr), url);
		gemini_response_finish(&resp);
		result = PROMPT_AGAIN;
		set_url(browser, old_url, NULL);
		goto exit;
	case '|':
		strncpy(&url[0], browser->plain_url, sizeof(url)-1);