// File: skyshore2001[P]symfind/symfind.cpp
// Function: GrepSymbol(char*)
void GrepSymbol(char *arg)
{
	char gcmd[2048], *p = gcmd, *p1;
	p += sprintf(p, "grep --line-buffered -Hn -R");
	char *pat = strtok(arg, " ");
	// begin with "-": as grep option. e.g. "g -w hello"
	while (pat[0] == '-') { 
		p += sprintf(p, " %s", pat);
		pat = strtok(NULL, " ");
	}
	// ignore case if contains upper letter
	if (!ContainsUpper(pat))
		p += sprintf(p, " -i");
	p += sprintf(p, " -e \"%s\"", pat);
	vector<string> incs;
	while (p1 = strtok(NULL, " ")) {
		if (p1[0] == '-') {
			++ p1;
			p += sprintf(p, " --exclude \"%s\"", p1);
		}
		else {
			incs.push_back(p1);
// 			p += sprintf(p, " --include \"%s\"", p1);
		}
	}
	if (incs.size() == 0) { // use the default patterns when scanning
		GetTagscanPats(incs);
	}
	for (const string &inc: incs) {
		p += sprintf(p, " --include \"%s\"", inc.c_str());
	}

	SfRepoIter it(g_repos);
	while (it.Next()) {
		const char *root = g_rootsubs.Substitue(it.Value()->root);
		p += sprintf(p, " \"%s\"", root);
	}
	if (g_forsvr) {
		printf("%s\n", gcmd);
	}
	else {
		p += sprintf(p, " | tee %s", TMP_OUT);
		printf("%s\n", gcmd);
		system(gcmd);
		struct stat st;
		if (stat(TMP_OUT, &st) == 0 && st.st_size > 0)
		{
			printf("(Search results are saved in %s)\n", TMP_OUT);

			const char *ed = "vi";
			if (strstr(EDITOR, "vi")) { // vi/vim/gvim
				ed = EDITOR;
			}
			sprintf(gcmd, "%s -q %s -c copen", ed, TMP_OUT);
			system(gcmd);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x9c0, %rsp            # imm = 0x9C0
movq %rdi, -0x8(%rbp)
leaq -0x810(%rbp), %rax
movq %rax, -0x818(%rbp)
movq -0x818(%rbp), %rdi
leaq 0x6fac(%rip), %rsi      # 0xb497
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x7178(%rip), %rsi      # 0xb68b
callq 0x23a0
movq %rax, -0x828(%rbp)
movq -0x828(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x4579
movq -0x818(%rbp), %rdi
movq -0x828(%rbp), %rdx
leaq 0x6fa3(%rip), %rsi      # 0xb4e6
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x7120(%rip), %rsi      # 0xb68b
callq 0x23a0
movq %rax, -0x828(%rbp)
jmp 0x451f
movq -0x828(%rbp), %rdi
callq 0x6110
testb $0x1, %al
jne 0x45b4
movq -0x818(%rbp), %rdi
leaq 0x6f1c(%rip), %rsi      # 0xb4b3
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
movq -0x818(%rbp), %rdi
movq -0x828(%rbp), %rdx
leaq 0x6eee(%rip), %rsi      # 0xb4b7
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
leaq -0x840(%rbp), %rdi
callq 0x6160
xorl %eax, %eax
movl %eax, %edi
leaq 0x708e(%rip), %rsi      # 0xb68b
callq 0x23a0
movq %rax, -0x820(%rbp)
cmpq $0x0, %rax
je 0x471f
movq -0x820(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x466b
movq -0x820(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x820(%rbp)
movq -0x818(%rbp), %rdi
movq -0x820(%rbp), %rdx
leaq 0x6e77(%rip), %rsi      # 0xb4c0
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
jmp 0x471a
movq -0x820(%rbp), %rax
movq %rax, -0x988(%rbp)
leaq -0x861(%rbp), %rdi
movq %rdi, -0x980(%rbp)
callq 0x23f0
movq -0x988(%rbp), %rsi
movq -0x980(%rbp), %rdx
leaq -0x860(%rbp), %rdi
callq 0x6180
jmp 0x46a8
leaq -0x840(%rbp), %rdi
leaq -0x860(%rbp), %rsi
callq 0x6060
jmp 0x46bd
leaq -0x860(%rbp), %rdi
callq 0x2140
leaq -0x861(%rbp), %rdi
callq 0x2270
jmp 0x471a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x870(%rbp)
movl %eax, -0x874(%rbp)
jmp 0x4709
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x870(%rbp)
movl %eax, -0x874(%rbp)
leaq -0x860(%rbp), %rdi
callq 0x2140
leaq -0x861(%rbp), %rdi
callq 0x2270
jmp 0x4a26
jmp 0x45f2
leaq -0x840(%rbp), %rdi
callq 0x6230
cmpq $0x0, %rax
jne 0x4758
leaq -0x840(%rbp), %rdi
callq 0x4270
jmp 0x473f
jmp 0x4758
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x870(%rbp)
movl %eax, -0x874(%rbp)
jmp 0x4a26
leaq -0x840(%rbp), %rax
movq %rax, -0x880(%rbp)
movq -0x880(%rbp), %rdi
callq 0x6250
movq %rax, -0x888(%rbp)
movq -0x880(%rbp), %rdi
callq 0x6280
movq %rax, -0x890(%rbp)
leaq -0x888(%rbp), %rdi
leaq -0x890(%rbp), %rsi
callq 0x62b0
testb $0x1, %al
jne 0x47a5
jmp 0x4811
leaq -0x888(%rbp), %rdi
callq 0x62f0
movq %rax, -0x898(%rbp)
movq -0x818(%rbp), %rax
movq %rax, -0x990(%rbp)
movq -0x898(%rbp), %rdi
callq 0x20d0
movq -0x990(%rbp), %rdi
movq %rax, %rdx
leaq 0x6ced(%rip), %rsi      # 0xb4d0
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
leaq -0x888(%rbp), %rdi
callq 0x6310
jmp 0x478c
movq 0xcac8(%rip), %rsi      # 0x112e0
leaq -0x8d8(%rbp), %rdi
callq 0x58e0
jmp 0x4826
jmp 0x4828
leaq -0x8d8(%rbp), %rdi
callq 0x5920
movq %rax, -0x998(%rbp)
jmp 0x483d
movq -0x998(%rbp), %rax
cmpq $0x0, %rax
je 0x48d9
movq 0xcac3(%rip), %rax      # 0x11318
addq $0x8, %rax
movq %rax, -0x9a8(%rbp)
leaq -0x8d8(%rbp), %rdi
callq 0x5960
movq %rax, -0x9a0(%rbp)
jmp 0x4875
movq -0x9a8(%rbp), %rdi
movq -0x9a0(%rbp), %rax
movq (%rax), %rsi
callq 0x3380
movq %rax, -0x9b0(%rbp)
jmp 0x4894
movq -0x9b0(%rbp), %rax
movq %rax, -0x8e0(%rbp)
movq -0x818(%rbp), %rdi
movq -0x8e0(%rbp), %rdx
leaq 0x6b22(%rip), %rsi      # 0xb3d9
movb $0x0, %al
callq 0x2050
movl %eax, %ecx
movq -0x818(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x818(%rbp)
jmp 0x4828
testb $0x1, 0xca30(%rip)      # 0x11310
je 0x48fe
leaq 0x6b52(%rip), %rdi      # 0xb43b
xorl %eax, %eax
leaq -0x810(%rbp), %rsi
callq 0x2030
jmp 0x48f9
jmp 0x4a11
movq -0x818(%rbp), %rdi
movq 0xc9ac(%rip), %rdx      # 0x112b8
leaq 0x6bcd(%rip), %rsi      # 0xb4e0
xorl %eax, %eax
movb %al, -0x9b1(%rbp)
callq 0x2050
movl %eax, %edx
movb -0x9b1(%rbp), %al
movq -0x818(%rbp), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, -0x818(%rbp)
leaq 0x6af8(%rip), %rdi      # 0xb43b
leaq -0x810(%rbp), %rsi
callq 0x2030
jmp 0x4951
leaq -0x810(%rbp), %rdi
callq 0x21e0
jmp 0x495f
movq 0xc952(%rip), %rdi      # 0x112b8
leaq -0x970(%rbp), %rsi
callq 0x2210
cmpl $0x0, %eax
jne 0x4a0f
cmpq $0x0, -0x940(%rbp)
jle 0x4a0f
movq 0xc928(%rip), %rsi      # 0x112b8
leaq 0x6b53(%rip), %rdi      # 0xb4ea
xorl %eax, %eax
callq 0x2030
jmp 0x49a0
leaq 0x6b65(%rip), %rax      # 0xb50c
movq %rax, -0x978(%rbp)
leaq 0xc89b(%rip), %rdi      # 0x11250
leaq 0x6b50(%rip), %rsi      # 0xb50c
callq 0x2060
cmpq $0x0, %rax
je 0x49d5
leaq 0xc882(%rip), %rax      # 0x11250
movq %rax, -0x978(%rbp)
movq -0x978(%rbp), %rdx
movq 0xc8d5(%rip), %rcx      # 0x112b8
leaq 0x6b25(%rip), %rsi      # 0xb50f
xorl %eax, %eax
leaq -0x810(%rbp), %rdi
movq %rdi, -0x9c0(%rbp)
callq 0x2050
movq -0x9c0(%rbp), %rdi
callq 0x21e0
jmp 0x4a0d
jmp 0x4a0f
jmp 0x4a11
leaq -0x840(%rbp), %rdi
callq 0x6330
addq $0x9c0, %rsp            # imm = 0x9C0
popq %rbp
retq
leaq -0x840(%rbp), %rdi
callq 0x6330
movq -0x870(%rbp), %rdi
callq 0x23e0
nop