// File: skyshore2001[P]symfind/symfind.cpp
// Function: ExecCmd(char*, char const*)
int ExecCmd(char *buf, const char *prompt = "> ")
{
	char defaultCmd[20] = "?\n";
	if (buf[0] == '\n' || buf[0] == '\r')
		buf = defaultCmd;
	// shell cmd
	if (buf[0] == '!') {
		system(buf+1);
		goto nx;
	}

	char *cmd, *arg;
	int ses;
	cmd = strtok(buf, " \r\n");
	arg = strtok(NULL, "\r\n");
	ses = 0;
	while (*cmd && isdigit(*cmd)) {
		ses = ses * 10 + (*cmd - '0');
		++ cmd;
	}
// 		printf("### ses=%d, cmd='%s'\n", ses, cmd);
	InitSession(ses);
	if (STR_EQ(cmd, "f") || STR_EQ(cmd, "s")) {
		clock_t t0 = clock();
		if (cmd[0] == 'f')
			QueryFile(arg);
		else // 's'
			QuerySymbol(arg);
		printf("(Total %d result(s) in %.3fs.)\n", g_result.items.size(), (double)(clock()-t0)/CLOCKS_PER_SEC);
	}
	else if (STR_EQ(cmd, "g")) { // grep
		if (arg) {
			GrepSymbol(arg);
		}
	}
	else if (STR_EQ(cmd, "q")) {
		return 1;
	}
	else if (STR_EQ(cmd, "?")) {
		Help();
	}
	else if (!g_forsvr && (STR_EQ(cmd, "go") || STR_EQ(cmd, "n") || STR_EQ(cmd, "N"))) {
		GotoResult(cmd, arg);
	}
	else if (!g_forsvr && STR_EQ(cmd, "editor")) {
		if (arg) {
			strcpy(EDITOR, arg);
		}
		printf("editor %s\n", EDITOR);
	}
	else if (STR_EQ(cmd, "max")) {
		if (arg) {
			int n = atoi(arg);
			if (n > 0) {
				MAX_FOUND = n;
			}
		}
		printf("max %d\n", MAX_FOUND);
	}
	else if (STR_EQ(cmd, "root")) {
		if (arg) {
			g_rootsubs.Set(arg);
		}
		printf("root %s\n", g_rootsubs.pattern);
	}
	else if (STR_EQ(cmd, "add")) {
		if (arg) {
			int cnt = 0;
			const char *args[10];
			args[cnt++] = strtok(arg, " ");
			while ((args[cnt] = strtok(NULL, " ")) != NULL) {
				++ cnt;
			}
			LoadRepofiles(cnt, args);
		}
	}
	else {
		printf("*** unknown command: '%s'. Type '?' for help.\n", cmd);
	}
nx:
	if (ses != 0) {
		printf("(session %d)\n", ses);
	}
// 	printf("> ");
	printf("%s", prompt);
	if (g_forsvr)
		fputs("\n", stdout);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq 0x6676(%rip), %rax      # 0xb730
movq %rax, -0x30(%rbp)
movq 0x6673(%rip), %rax      # 0xb738
movq %rax, -0x28(%rbp)
movl 0x6671(%rip), %eax      # 0xb740
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x50ea
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x50f2
leaq -0x30(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x21, %eax
jne 0x5110
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
callq 0x21e0
jmp 0x54a3
movq -0x10(%rbp), %rdi
leaq 0x61cf(%rip), %rsi      # 0xb2ea
callq 0x23a0
movq %rax, -0x38(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x61b2(%rip), %rsi      # 0xb2e1
callq 0x23a0
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, -0xb1(%rbp)
je 0x5168
movq -0x38(%rbp), %rax
movsbl (%rax), %edi
callq 0x2200
cmpl $0x0, %eax
setne %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x5174
jmp 0x5195
imull $0xa, -0x44(%rbp), %eax
movq -0x38(%rbp), %rcx
movsbl (%rcx), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x513f
movl -0x44(%rbp), %edi
callq 0x4ed0
movq -0x38(%rbp), %rdi
leaq 0x63c6(%rip), %rsi      # 0xb56e
callq 0x2330
cmpl $0x0, %eax
je 0x51c7
movq -0x38(%rbp), %rdi
leaq 0x632b(%rip), %rsi      # 0xb4e8
callq 0x2330
cmpl $0x0, %eax
jne 0x523b
callq 0x2160
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x66, %eax
jne 0x51e7
movq -0x40(%rbp), %rdi
callq 0x3490
jmp 0x51f0
movq -0x40(%rbp), %rdi
callq 0x38a0
leaq 0xc0f1(%rip), %rdi      # 0x112e8
addq $0x8, %rdi
callq 0x5e70
movq %rax, -0xc0(%rbp)
callq 0x2160
movq -0xc0(%rbp), %rsi
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x5de4(%rip), %xmm1     # 0xb008
divsd %xmm1, %xmm0
leaq 0x6341(%rip), %rdi      # 0xb570
movb $0x1, %al
callq 0x2030
jmp 0x54a1
movq -0x38(%rbp), %rdi
leaq 0x634a(%rip), %rsi      # 0xb590
callq 0x2330
cmpl $0x0, %eax
jne 0x5265
cmpq $0x0, -0x40(%rbp)
je 0x5260
movq -0x40(%rbp), %rdi
callq 0x44c0
jmp 0x549f
movq -0x38(%rbp), %rdi
leaq 0x6322(%rip), %rsi      # 0xb592
callq 0x2330
cmpl $0x0, %eax
jne 0x5286
movl $0x1, -0x4(%rbp)
jmp 0x54f2
movq -0x38(%rbp), %rdi
leaq 0x6303(%rip), %rsi      # 0xb594
callq 0x2330
cmpl $0x0, %eax
jne 0x52a5
callq 0x2740
jmp 0x549b
testb $0x1, 0xc064(%rip)      # 0x11310
jne 0x52ff
movq -0x38(%rbp), %rdi
leaq 0x62dd(%rip), %rsi      # 0xb596
callq 0x2330
cmpl $0x0, %eax
je 0x52ed
movq -0x38(%rbp), %rdi
leaq 0x6251(%rip), %rsi      # 0xb51f
callq 0x2330
cmpl $0x0, %eax
je 0x52ed
movq -0x38(%rbp), %rdi
leaq 0x62b6(%rip), %rsi      # 0xb599
callq 0x2330
cmpl $0x0, %eax
jne 0x52ff
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x40a0
jmp 0x5499
testb $0x1, 0xc00a(%rip)      # 0x11310
jne 0x534e
movq -0x38(%rbp), %rdi
leaq 0x6288(%rip), %rsi      # 0xb59b
callq 0x2330
cmpl $0x0, %eax
jne 0x534e
cmpq $0x0, -0x40(%rbp)
je 0x5334
movq -0x40(%rbp), %rsi
leaq 0xbf21(%rip), %rdi      # 0x11250
callq 0x2220
leaq 0x6267(%rip), %rdi      # 0xb5a2
leaq 0xbf0e(%rip), %rsi      # 0x11250
movb $0x0, %al
callq 0x2030
jmp 0x5497
movq -0x38(%rbp), %rdi
leaq 0x6254(%rip), %rsi      # 0xb5ad
callq 0x2330
cmpl $0x0, %eax
jne 0x53a8
cmpq $0x0, -0x40(%rbp)
je 0x538b
movq -0x40(%rbp), %rdi
callq 0x2390
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jle 0x5389
movl -0x54(%rbp), %ecx
movq 0xbf92(%rip), %rax      # 0x11318
movl %ecx, 0x4(%rax)
jmp 0x538b
movq 0xbf86(%rip), %rax      # 0x11318
movl 0x4(%rax), %esi
leaq 0x6215(%rip), %rdi      # 0xb5b1
movb $0x0, %al
callq 0x2030
jmp 0x5495
movq -0x38(%rbp), %rdi
leaq 0x6206(%rip), %rsi      # 0xb5b9
callq 0x2330
cmpl $0x0, %eax
jne 0x53f6
cmpq $0x0, -0x40(%rbp)
je 0x53d8
movq 0xbf4d(%rip), %rdi      # 0x11318
addq $0x8, %rdi
movq -0x40(%rbp), %rsi
callq 0x4d60
movq 0xbf39(%rip), %rsi      # 0x11318
addq $0x8, %rsi
leaq 0x61d4(%rip), %rdi      # 0xb5be
movb $0x0, %al
callq 0x2030
jmp 0x5493
movq -0x38(%rbp), %rdi
leaq 0x61c6(%rip), %rsi      # 0xb5c7
callq 0x2330
cmpl $0x0, %eax
jne 0x547f
cmpq $0x0, -0x40(%rbp)
je 0x547d
movl $0x0, -0x58(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x6267(%rip), %rsi      # 0xb68b
callq 0x23a0
movq %rax, %rcx
movl -0x58(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x58(%rbp)
cltq
movq %rcx, -0xb0(%rbp,%rax,8)
xorl %eax, %eax
movl %eax, %edi
leaq 0x623f(%rip), %rsi      # 0xb68b
callq 0x23a0
movslq -0x58(%rbp), %rcx
movq %rax, -0xb0(%rbp,%rcx,8)
cmpq $0x0, %rax
je 0x546e
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5441
movl -0x58(%rbp), %edi
leaq -0xb0(%rbp), %rsi
callq 0x2f40
jmp 0x5491
movq -0x38(%rbp), %rsi
leaq 0x6141(%rip), %rdi      # 0xb5cb
movb $0x0, %al
callq 0x2030
jmp 0x5493
jmp 0x5495
jmp 0x5497
jmp 0x5499
jmp 0x549b
jmp 0x549d
jmp 0x549f
jmp 0x54a1
jmp 0x54a3
cmpl $0x0, -0x44(%rbp)
je 0x54ba
movl -0x44(%rbp), %esi
leaq 0x6147(%rip), %rdi      # 0xb5fa
movb $0x0, %al
callq 0x2030
movq -0x18(%rbp), %rsi
leaq 0x6022(%rip), %rdi      # 0xb4e7
movb $0x0, %al
callq 0x2030
testb $0x1, 0xbe3d(%rip)      # 0x11310
je 0x54eb
movq 0xbae4(%rip), %rax      # 0x10fc0
movq (%rax), %rsi
leaq 0x5dfc(%rip), %rdi      # 0xb2e2
callq 0x22b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nop