// File: skyshore2001[P]symfind/symfind.cpp
// Function: QuerySymbol(char*)
void QuerySymbol(char *arg)
{
	SfSymbolIter it(g_repos);

	char pat_kind[20] = {0};
	char kindlen = 0;
	vector<SfPattern> pats, pats_val, pats_path;

	char *p;
	while (p = strtok(arg, " ")) {
		arg = NULL; // make strtok continue

		if (*(p+1) == 0 && islower(*p)) { // only 1 char for pat_kind
			if (kindlen < sizeof(pat_kind))
				pat_kind[kindlen++] = *p;
			continue;
		}

		SfPatterns *ppats = &pats;
		if (*p == '#') {
			++ p;
			ppats = &pats_val;
		}
		else {
			char *p1 = strchr(p, 0)-1;
			if (*p1 == '/' || *p1 == '\\') {
				*p1 -- = 0;
				ppats = &pats_path;
			}
		}
		ppats->push_back(SfPattern(p));
	}

	g_result.Init('s');
	while (it.Next()) {
		SfSymbol *symbol = it.Value();

		char *name = symbol->name();
		bool ok = true;

		char *kind = symbol->kind();
		if (kindlen && strncmp(kind, pat_kind, kindlen) != 0)
			continue;

		for (SfPattern &pat: pats) {
			if (! pat.Match(name)) {
				ok = false;
				break;
			}
		}
		if (ok && pats_val.size() > 0) {
			ok = strcmp(kind, "macro") == 0 || strcmp(kind, "variable") == 0;
			if (ok) {
				char *extra = symbol->extra();
				for (SfPattern &pat: pats_val) {
					if (! pat.Match(extra)) {
						ok = false;
						break;
					}
				}
			}
		}
		if (ok && pats_path.size() > 0) {
			const SfContext &ctx = it.GetContext();
			for (SfPattern &pat: pats_path) {
				if (! pat.Match(ctx.file->name, ctx.folder->name)) {
					ok = false;
					break;
				}
			}
		}
		if (ok) {
			auto &items = g_result.items;
			const SfContext &ctx = it.GetContext();
			items.push_back(ctx);
			int cnt = items.size();
			char buf[1024];
			if (!g_forsvr)
				printf("%d:\t%s\t%s\t%s\t%s:%s\n", cnt, kind, symbol->name(), symbol->extra(), ctx.file->name, symbol->line());
			else
				printf("%d:\t%s\t%s\t%s\t%s:%s\t%s\n", cnt, kind, symbol->name(), symbol->extra(), ctx.file->name, symbol->line(), 
						GetFullName(buf, ctx.repo->root, ctx.folder->name));
			if (cnt >= MAX_FOUND) {
				printf("... (max %d)\n", MAX_FOUND);
				break;
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x670, %rsp            # imm = 0x670
movq %rdi, -0x8(%rbp)
movq 0xda2a(%rip), %rsi      # 0x112e0
leaq -0x48(%rbp), %rdi
callq 0x5ef0
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x2120
movb $0x0, -0x61(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x59b0
leaq -0x98(%rbp), %rdi
callq 0x59b0
leaq -0xb0(%rbp), %rdi
callq 0x59b0
movq -0x8(%rbp), %rdi
leaq 0x7d8c(%rip), %rsi      # 0xb68b
callq 0x23a0
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0x3a7a
movq $0x0, -0x8(%rbp)
movq -0xb8(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x396a
movq -0xb8(%rbp), %rax
movsbl (%rax), %edi
callq 0x2310
cmpl $0x0, %eax
je 0x396a
movsbq -0x61(%rbp), %rax
cmpq $0x14, %rax
jae 0x3968
movq -0xb8(%rbp), %rax
movb (%rax), %cl
movb -0x61(%rbp), %al
movb %al, %dl
addb $0x1, %dl
movb %dl, -0x61(%rbp)
movsbq %al, %rax
movb %cl, -0x60(%rbp,%rax)
jmp 0x38f4
leaq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x39a6
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x3a05
movq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x20a0
addq $-0x1, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x39dd
movq -0xc8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5c, %eax
jne 0x3a03
movq -0xc8(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0xc8(%rbp)
movb $0x0, (%rax)
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x3a05
movq -0xc0(%rbp), %rax
movq %rax, -0x5a8(%rbp)
movq -0xb8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x5a00
jmp 0x3a28
movq -0x5a8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x59d0
jmp 0x3a3d
jmp 0x38f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf0(%rbp)
movl %eax, -0xf4(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x5e90
leaq -0x98(%rbp), %rdi
callq 0x5e90
leaq -0x80(%rbp), %rdi
callq 0x5e90
jmp 0x408d
leaq 0xd867(%rip), %rdi      # 0x112e8
movl $0x73, %esi
callq 0x5b30
jmp 0x3a8d
jmp 0x3a8f
leaq -0x48(%rbp), %rdi
callq 0x5f20
movq %rax, -0x5b0(%rbp)
jmp 0x3aa1
movq -0x5b0(%rbp), %rax
cmpq $0x0, %rax
je 0x4063
leaq -0x48(%rbp), %rdi
callq 0x5f80
movq %rax, -0x5b8(%rbp)
jmp 0x3ac4
movq -0x5b8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x5fa0
movq %rax, -0x5c0(%rbp)
jmp 0x3ae7
movq -0x5c0(%rbp), %rax
movq %rax, -0x108(%rbp)
movb $0x1, -0x109(%rbp)
movq -0x100(%rbp), %rdi
callq 0x5fc0
movq %rax, -0x5c8(%rbp)
jmp 0x3b11
movq -0x5c8(%rbp), %rax
movq %rax, -0x118(%rbp)
cmpb $0x0, -0x61(%rbp)
je 0x3b44
movq -0x118(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movsbq -0x61(%rbp), %rdx
callq 0x2100
cmpl $0x0, %eax
je 0x3b44
jmp 0x3a8f
leaq -0x80(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x5bf0
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x5c20
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x5c50
testb $0x1, %al
jne 0x3b8e
jmp 0x3be5
leaq -0x128(%rbp), %rdi
callq 0x5c90
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
movq -0x108(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x5cb0
movb %al, -0x5c9(%rbp)
jmp 0x3bc0
movb -0x5c9(%rbp), %al
testb $0x1, %al
jne 0x3bd5
jmp 0x3bcc
movb $0x0, -0x109(%rbp)
jmp 0x3be5
jmp 0x3bd7
leaq -0x128(%rbp), %rdi
callq 0x5dc0
jmp 0x3b75
testb $0x1, -0x109(%rbp)
je 0x3d2f
leaq -0x98(%rbp), %rdi
callq 0x5de0
cmpq $0x0, %rax
jbe 0x3d2f
movq -0x118(%rbp), %rdi
leaq 0x7837(%rip), %rsi      # 0xb44d
callq 0x2330
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x5ca(%rbp)
je 0x3c49
movq -0x118(%rbp), %rdi
leaq 0x781b(%rip), %rsi      # 0xb453
callq 0x2330
cmpl $0x0, %eax
sete %al
movb %al, -0x5ca(%rbp)
movb -0x5ca(%rbp), %al
andb $0x1, %al
movb %al, -0x109(%rbp)
testb $0x1, -0x109(%rbp)
je 0x3d2d
movq -0x100(%rbp), %rdi
callq 0x5fe0
movq %rax, -0x5d8(%rbp)
jmp 0x3c79
movq -0x5d8(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rdi
callq 0x5bf0
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rdi
callq 0x5c20
movq %rax, -0x158(%rbp)
leaq -0x150(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x5c50
testb $0x1, %al
jne 0x3cd4
jmp 0x3d2b
leaq -0x150(%rbp), %rdi
callq 0x5c90
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rdi
movq -0x140(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x5cb0
movb %al, -0x5d9(%rbp)
jmp 0x3d06
movb -0x5d9(%rbp), %al
testb $0x1, %al
jne 0x3d1b
jmp 0x3d12
movb $0x0, -0x109(%rbp)
jmp 0x3d2b
jmp 0x3d1d
leaq -0x150(%rbp), %rdi
callq 0x5dc0
jmp 0x3cbb
jmp 0x3d2d
jmp 0x3d2f
testb $0x1, -0x109(%rbp)
je 0x3e22
leaq -0xb0(%rbp), %rdi
callq 0x5de0
cmpq $0x0, %rax
jbe 0x3e22
leaq -0x48(%rbp), %rdi
callq 0x5bd0
movq %rax, -0x5e8(%rbp)
jmp 0x3d64
movq -0x5e8(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rdi
callq 0x5bf0
movq %rax, -0x178(%rbp)
movq -0x170(%rbp), %rdi
callq 0x5c20
movq %rax, -0x180(%rbp)
leaq -0x178(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x5c50
testb $0x1, %al
jne 0x3dbf
jmp 0x3e20
leaq -0x178(%rbp), %rdi
callq 0x5c90
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
movq -0x168(%rbp), %rcx
movq 0x8(%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rcx), %rsi
movq (%rax), %rdx
callq 0x5cb0
movb %al, -0x5e9(%rbp)
jmp 0x3dfb
movb -0x5e9(%rbp), %al
testb $0x1, %al
jne 0x3e10
jmp 0x3e07
movb $0x0, -0x109(%rbp)
jmp 0x3e20
jmp 0x3e12
leaq -0x178(%rbp), %rdi
callq 0x5dc0
jmp 0x3da6
jmp 0x3e22
testb $0x1, -0x109(%rbp)
je 0x405e
leaq 0xd4ba(%rip), %rax      # 0x112f0
movq %rax, -0x190(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x5bd0
movq %rax, -0x5f8(%rbp)
jmp 0x3e4f
movq -0x5f8(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x198(%rbp), %rsi
leaq 0xd485(%rip), %rdi      # 0x112f0
callq 0x5e00
jmp 0x3e72
leaq 0xd46f(%rip), %rdi      # 0x112e8
addq $0x8, %rdi
callq 0x5e70
movl %eax, -0x19c(%rbp)
testb $0x1, 0xd481(%rip)      # 0x11310
jne 0x3f47
movl -0x19c(%rbp), %eax
movl %eax, -0x60c(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x608(%rbp)
movq -0x100(%rbp), %rdi
callq 0x5fa0
movq %rax, -0x600(%rbp)
jmp 0x3ec4
movq -0x100(%rbp), %rdi
callq 0x5fe0
movq %rax, -0x618(%rbp)
jmp 0x3ed9
movq -0x198(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x628(%rbp)
movq -0x100(%rbp), %rdi
callq 0x6000
movq %rax, -0x620(%rbp)
jmp 0x3f03
movq -0x628(%rbp), %r9
movq -0x618(%rbp), %r8
movq -0x600(%rbp), %rcx
movq -0x608(%rbp), %rdx
movl -0x60c(%rbp), %esi
movq -0x620(%rbp), %rdi
movq %rsp, %rax
movq %rdi, (%rax)
leaq 0x7523(%rip), %rdi      # 0xb45c
xorl %eax, %eax
callq 0x2030
jmp 0x3f42
jmp 0x402e
movl -0x19c(%rbp), %eax
movl %eax, -0x63c(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x638(%rbp)
movq -0x100(%rbp), %rdi
callq 0x5fa0
movq %rax, -0x630(%rbp)
jmp 0x3f76
movq -0x100(%rbp), %rdi
callq 0x5fe0
movq %rax, -0x648(%rbp)
jmp 0x3f8b
movq -0x198(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x658(%rbp)
movq -0x100(%rbp), %rdi
callq 0x6000
movq %rax, -0x650(%rbp)
jmp 0x3fb5
movq -0x198(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq (%rcx), %rsi
movq (%rax), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x5a0(%rbp), %rdi
callq 0x31a0
movq %rax, -0x660(%rbp)
jmp 0x3fe2
movq -0x658(%rbp), %r9
movq -0x648(%rbp), %r8
movq -0x630(%rbp), %rcx
movq -0x638(%rbp), %rdx
movl -0x63c(%rbp), %esi
movq -0x650(%rbp), %rdi
movq -0x660(%rbp), %r10
movq %rsp, %rax
movq %r10, 0x8(%rax)
movq %rdi, (%rax)
leaq 0x744d(%rip), %rdi      # 0xb470
xorl %eax, %eax
callq 0x2030
jmp 0x402c
jmp 0x402e
movl -0x19c(%rbp), %eax
movq 0xd2dd(%rip), %rcx      # 0x11318
cmpl 0x4(%rcx), %eax
jl 0x405c
movq 0xd2d1(%rip), %rax      # 0x11318
movl 0x4(%rax), %esi
leaq 0x73ee(%rip), %rdi      # 0xb43f
xorl %eax, %eax
callq 0x2030
jmp 0x405a
jmp 0x4063
jmp 0x405e
jmp 0x3a8f
leaq -0xb0(%rbp), %rdi
callq 0x5e90
leaq -0x98(%rbp), %rdi
callq 0x5e90
leaq -0x80(%rbp), %rdi
callq 0x5e90
addq $0x670, %rsp            # imm = 0x670
popq %rbp
retq
movq -0xf0(%rbp), %rdi
callq 0x23e0
nopl (%rax)