// File: /workspace/github_c_makefile_star2_O2/mrdotx[P]st/st.c
// Function: copyurl
void
copyurl(const Arg *arg) {
	/* () and [] can appear in urls, but excluding them here will reduce false
	 * positives when figuring out where a given url ends.
	 */
	static char URLCHARS[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789-._~:/?#@!$&'*+,;=%";

	static const char* URLSTRINGS[] = {"http://", "https://"};

	/* remove highlighting from previous selection if any */
	if(sel.ob.x >= 0 && sel.oe.x >= 0)
		tsetcolor(sel.nb.y, sel.ob.x, sel.oe.x + 1, defaultfg, defaultbg);

	int i = 0,
		row = 0, /* row of current URL */
		col = 0, /* column of current URL start */
		startrow = 0, /* row of last occurrence */
		colend = 0, /* column of last occurrence */
		passes = 0; /* how many rows have been scanned */

	char *linestr = calloc(term.col+1, sizeof(Rune));
	char *c = NULL,
		 *match = NULL;

	row = (sel.ob.x >= 0 && sel.nb.y > 0) ? sel.nb.y : term.bot;
	LIMIT(row, term.top, term.bot);
	startrow = row;

	colend = (sel.ob.x >= 0 && sel.nb.y > 0) ? sel.nb.x : term.col;
	LIMIT(colend, 0, term.col);

	/*
 	** Scan from (term.bot,term.col) to (0,0) and find
	** next occurrance of a URL
	*/
	while(passes !=term.bot + 2) {
		/* Read in each column of every row until
 		** we hit previous occurrence of URL
		*/
		for (col = 0, i = 0; col < colend; ++col,++i) {
			linestr[i] = term.line[row][col].u;
		}
		linestr[term.col] = '\0';

		if ((match = findlastany(linestr, URLSTRINGS,
						sizeof(URLSTRINGS)/sizeof(URLSTRINGS[0]))))
			break;

		if (--row < term.top)
			row = term.bot;

		colend = term.col;
		passes++;
	};

	if (match) {
		/* must happen before trim */
		selclear();
		sel.ob.x = strlen(linestr) - strlen(match);

		/* trim the rest of the line from the url match */
		for (c = match; *c != '\0'; ++c)
			if (!strchr(URLCHARS, *c)) {
				*c = '\0';
				break;
			}

		/* highlight selection by inverting terminal colors */
		tsetcolor(row, sel.ob.x, sel.ob.x + strlen( match ), defaultbg, defaultfg);

		/* select and copy */
		sel.mode = 1;
		sel.type = SEL_REGULAR;
		sel.oe.x = sel.ob.x + strlen(match)-1;
		sel.ob.y = sel.oe.y = row;
		selnormalize();
		tsetdirt(sel.nb.y, sel.ne.y);
		xsetsel(getsel());
		xclipcopy();
	}

	free(linestr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0x10573(%rip), %ebx     # 0x194f4
movl 0x10575(%rip), %edx     # 0x194fc
movl %ebx, %eax
orl %edx, %eax
js 0x8fb5
movl 0x10555(%rip), %edi     # 0x194e8
incl %edx
leaq 0xf5f4(%rip), %rax      # 0x18590
movl (%rax), %ecx
leaq 0xf5ef(%rip), %rax      # 0x18594
movl (%rax), %r8d
movl %ebx, %esi
callq 0x91fa
movl 0x1053f(%rip), %ebx     # 0x194f4
movl 0x10551(%rip), %eax     # 0x1950c
movl %eax, 0x20(%rsp)
movslq %eax, %r15
leaq 0x1(%r15), %rdi
pushq $0x4
popq %rsi
callq 0x6770
testl %ebx, %ebx
setns %cl
movl 0x1050f(%rip), %edx     # 0x194e8
testl %edx, %edx
setg %sil
testb %sil, %cl
movl 0x11510(%rip), %esi     # 0x1a4f8
cmovel %esi, %edx
movq %rax, %rbx
movl 0x104f0(%rip), %ecx     # 0x194e4
cmovel %r15d, %ecx
cmpl %esi, %edx
movl %esi, %edi
cmovll %edx, %edi
movl 0x114ef(%rip), %eax     # 0x1a4f4
cmpl %eax, %edx
movl %eax, 0x1c(%rsp)
cmovll %eax, %edi
movq %rdi, 0x8(%rsp)
cmpl %r15d, %ecx
movl %r15d, %eax
cmovll %ecx, %eax
xorl %edx, %edx
testl %ecx, %ecx
cmovsl %edx, %eax
movq %rsi, 0x28(%rsp)
leal 0x2(%rsi), %r12d
movq 0x104dd(%rip), %rbp     # 0x19510
leaq 0xcff6(%rip), %r13      # 0x16030
xorl %r14d, %r14d
movq %rbx, 0x10(%rsp)
movq %r15, 0x30(%rsp)
cmpl %r12d, %r14d
je 0x91e4
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
movslq 0x8(%rsp), %rcx
shlq $0x4, %rax
movq %rbx, %rdx
xorl %esi, %esi
cmpq %rsi, %rax
je 0x9082
movq (%rbp,%rcx,8), %rdi
movb (%rdi,%rsi), %dil
movb %dil, (%rdx)
addq $0x10, %rsi
incq %rdx
jmp 0x9068
movb $0x0, (%rbx,%r15)
movq %rbx, %rdi
callq 0x6390
movq %rax, %r15
addq %rbx, %r15
movl %r14d, 0x24(%rsp)
decq %r15
cmpq %rbx, %r15
jb 0x90e0
movl %r12d, %r14d
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x90d1
movq (%rbx,%r13), %r12
movq %r12, %rdi
callq 0x6390
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x61f0
addq $0x8, %rbx
testl %eax, %eax
jne 0x90a7
jmp 0x9107
movq 0x10(%rsp), %rbx
movl %r14d, %r12d
movl 0x24(%rsp), %r14d
jmp 0x909a
movq 0x8(%rsp), %rax
cmpl 0x1c(%rsp), %eax
leal -0x1(%rax), %eax
cmovlel 0x28(%rsp), %eax
incl %r14d
movq %rax, 0x8(%rsp)
movl 0x20(%rsp), %eax
movq 0x30(%rsp), %r15
jmp 0x9047
callq 0x6d9a
movq 0x10(%rsp), %rdi
callq 0x6390
movq %rax, %r12
movq %r15, %rdi
callq 0x6390
subl %eax, %r12d
movl %r12d, 0x103c9(%rip)    # 0x194f4
leaq 0xf3ce(%rip), %r13      # 0x18500
movq %r15, %rbx
movsbl (%rbx), %esi
testl %esi, %esi
je 0x9151
movq %r13, %rdi
callq 0x6540
testq %rax, %rax
je 0x914e
incq %rbx
jmp 0x9135
movb $0x0, (%rbx)
movq %r15, %rdi
callq 0x6390
leal (%rax,%r12), %edx
leaq 0xf430(%rip), %rax      # 0x18594
movl (%rax), %ecx
leaq 0xf423(%rip), %rax      # 0x18590
movl (%rax), %r8d
movq 0x8(%rsp), %r14
movl %r14d, %edi
movl %r12d, %esi
callq 0x91fa
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x10347(%rip)     # 0x194d8
movl 0x1035d(%rip), %ebx     # 0x194f4
movq %r15, %rdi
callq 0x6390
addl %ebx, %eax
decl %eax
movl %eax, 0x10353(%rip)     # 0x194fc
movl %r14d, 0x10350(%rip)    # 0x19500
movl %r14d, 0x10341(%rip)    # 0x194f8
callq 0x6dc3
movl 0x10326(%rip), %edi     # 0x194e8
movl 0x10328(%rip), %esi     # 0x194f0
callq 0x6ebb
callq 0x706f
movq %rax, %rdi
callq 0xbcdf
callq 0xbc80
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6360