// File: /workspace/github_cpp_makefile_star2_O2/AndreiBarsan[P]uShell/shell.cc
// Function: microshell::core::Shell::parse_command(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::shared_ptr<microshell::core::Command>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*) const
bool Shell::parse_command(const string& command_text,
                          shared_ptr<Command> &command,
                          string *error) const {
  // TODO(andrei) YACC this.
  // TODO(andrei) Customizable IFS variable.
  vector<string> argv = util::split(command_text, ' ');

  // Perform expansion for every parameter.
  for(string& arg : argv) {
    arg = expand(arg);
  }

  const string& program_name = argv[0];

  if(is_builtin(argv[0])) {
    command = construct_builtin(argv);
  }
  else {
    // If the path actually points to a directory, we want to catch that.
    string full_path = resolve_path(program_name);
    if (util::is_directory(full_path)) {
      // TODO(andrei) zsh-like auto-cd functionality could go here.
      *error = "Cannot execute [" + argv[0] + "]. It's a directory.";
      return false;
    }

    string binary_path;
    if(resolve_binary_name(argv[0], &binary_path)) {
      argv[0] = binary_path;
      command = make_shared<DiskCommand>(new DiskCommand(argv));
    } else {
      *error = "Command not found: [" + argv[0] + "]";
      return false;
    }
  }

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, -0x48(%rbp)
movq %rdx, %rbx
movq %rdi, %r15
leaq -0x40(%rbp), %r13
pushq $0x20
popq %rdx
movq %r13, %rdi
callq 0xc248
movq (%r13), %r12
movq 0x8(%r13), %r14
leaq -0x88(%rbp), %r13
cmpq %r14, %r12
je 0xac02
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xae54
movq %r12, %rdi
movq %r13, %rsi
callq 0x5290
movq %r13, %rdi
callq 0x5100
addq $0x20, %r12
jmp 0xabd6
movq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0xb14c
testb %al, %al
je 0xac41
leaq -0x88(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0xb16c
leaq -0x88(%rbp), %rsi
movq %rbx, %rdi
callq 0xb982
leaq -0x80(%rbp), %rdi
callq 0x6480
movb $0x1, %bl
jmp 0xadb6
leaq -0x88(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaee4
leaq -0x88(%rbp), %rdi
callq 0xc505
testb %al, %al
je 0xacc3
movq -0x40(%rbp), %rdx
leaq 0x38b9(%rip), %rsi      # 0xe527
leaq -0x68(%rbp), %rdi
callq 0x5fd1
leaq 0x38ba(%rip), %rdx      # 0xe538
leaq -0xa8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x5f7f
leaq -0xa8(%rbp), %rbx
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
callq 0x5290
movq %rbx, %rdi
callq 0x5100
leaq -0x68(%rbp), %rdi
callq 0x5100
leaq -0x88(%rbp), %rdi
callq 0x5100
jmp 0xadb4
leaq -0x98(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x40(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
movq %r15, %rdi
callq 0xb194
movl %eax, %r15d
testb %al, %al
je 0xad41
movq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x5150
pushq $0x20
popq %rdi
callq 0x5210
movq %rax, %r14
leaq -0x40(%rbp), %rsi
movq %rax, %rdi
callq 0xb5f6
leaq -0xc8(%rbp), %rsi
movq %r14, (%rsi)
leaq -0x68(%rbp), %rdi
callq 0xb5d7
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0xbb9c
leaq -0x60(%rbp), %rdi
callq 0x6480
jmp 0xad93
movq -0x40(%rbp), %rdx
leaq 0x3801(%rip), %rsi      # 0xe54d
leaq -0xc8(%rbp), %rdi
callq 0x5fd1
leaq 0x22dd(%rip), %rdx      # 0xd03c
leaq -0x68(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5f7f
leaq -0x68(%rbp), %rbx
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
callq 0x5290
movq %rbx, %rdi
callq 0x5100
leaq -0xc8(%rbp), %rdi
callq 0x5100
leaq -0xa8(%rbp), %rdi
callq 0x5100
leaq -0x88(%rbp), %rdi
callq 0x5100
testb %r15b, %r15b
jne 0xac3a
xorl %ebx, %ebx
leaq -0x40(%rbp), %rdi
callq 0x678c
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
callq 0x51f0
jmp 0xae03
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x5100
jmp 0xae03
jmp 0xae00
jmp 0xae00
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0xae0a
jmp 0xae11
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x5100
jmp 0xae14
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x5100
jmp 0xae2b
jmp 0xae28
jmp 0xae28
jmp 0xae28
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x678c
movq %rbx, %rdi
callq 0x53a0