// File: /workspace/github_cpp_makefile_star2_O3/jakubsvehla[P]hex/src/cmd_mode.cpp
// Function: CmdMode::Run()
void CmdMode::Run ()
{
  int   c;
  char  buffer [MAX_SIZE + 1];
  int   i = 0;

  m_Editor -> m_CmdLineView -> Erase();
  m_Editor -> m_CmdLineView -> Put(':');

  while (i < MAX_SIZE) {
    m_Editor -> m_CmdLineView -> Get(c);

    // Exit the command line mode
    if (c == ESC) {
      m_Editor -> m_CmdLineView -> Erase();
      Exit();
      return;
    }

    // Execute the command
    else if (c == '\n') {
      break;
    }
    else if (c == KEY_BACKSPACE && i > 0) {
      m_Editor -> m_CmdLineView -> Move(0, i--);
      m_Editor -> m_CmdLineView -> Del();
    }
    else if (isprint(c)) {
      buffer[i++] = c;
      m_Editor -> m_CmdLineView -> Put(c);
    }
  }

  buffer[i] = '\0';

  // quit cmd
  if (strcmp("quit", buffer) == 0 || strcmp("q", buffer) == 0) {
    if (m_Editor -> m_File -> IsSaved()) {
      m_Editor -> Exit();      
    }
    else {
      m_Editor -> m_CmdLineView -> Render("No write since last change (add ! to overwrite)");
      m_Editor -> m_FileView -> Refresh(); 
    }
  }

  // quit! cmd
  else if (strcmp("quit!", buffer) == 0 || strcmp("q!", buffer) == 0) {
    m_Editor -> Exit();
  }

  // write
  else if (strcmp("write", buffer) == 0 || strcmp("w", buffer) == 0) {
    m_Editor -> m_File -> Save();
  }
  else {
    m_Editor -> m_CmdLineView -> Erase();
    m_Editor -> m_CmdLineView -> Print("Not an editor command: %s", buffer);
    m_Editor -> m_CmdLineView -> Refresh();
    m_Editor -> m_FileView -> Refresh();
  }

  Exit();

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x20(%rax), %rdi
callq 0x40cc
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movl $0x3a, %esi
callq 0x3ede
xorl %r15d, %r15d
leaq 0x24(%rsp), %r14
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movq %r14, %rsi
callq 0x3f7a
movl 0x24(%rsp), %ebp
cmpl $0xa, %ebp
je 0x4b4b
cmpl $0x1b, %ebp
je 0x4ba6
cmpl $0x107, %ebp            # imm = 0x107
setne %al
testl %r15d, %r15d
setle %cl
orb %al, %cl
jne 0x4b1d
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
leal -0x1(%r15), %ebp
xorl %esi, %esi
movl %r15d, %edx
callq 0x40b8
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x3ee8
movl %ebp, %r15d
jmp 0x4b41
movl %ebp, %edi
callq 0x3450
testl %eax, %eax
je 0x4b41
movslq %r15d, %rax
incl %r15d
movb %bpl, (%rsp,%rax)
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movl %ebp, %esi
callq 0x3ede
cmpl $0x14, %r15d
jl 0x4ac0
movslq %r15d, %rax
movb $0x0, (%rsp,%rax)
movl $0x74697571, %ecx       # imm = 0x74697571
movl (%rsp), %eax
xorl %ecx, %eax
movzbl 0x4(%rsp), %edx
orl %eax, %edx
sete %dl
movzwl (%rsp), %eax
cmpw $0x71, %ax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x4bb8
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rdi
callq 0x3e3a
movq 0x8(%rbx), %rdi
testb %al, %al
jne 0x4c0f
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
leaq 0x16ea(%rip), %rsi      # 0x6288
callq *0x20(%rax)
jmp 0x4c45
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x40cc
jmp 0x4c52
xorl (%rsp), %ecx
movzwl 0x4(%rsp), %edx
xorl $0x21, %edx
orl %ecx, %edx
je 0x4c0b
movl $0x2171, %ecx           # imm = 0x2171
xorl (%rsp), %ecx
movzbl 0x2(%rsp), %edx
orw %cx, %dx
je 0x4c0b
movl $0x74697277, %ecx       # imm = 0x74697277
xorl (%rsp), %ecx
movzwl 0x4(%rsp), %edx
xorl $0x65, %edx
orl %ecx, %edx
sete %cl
cmpw $0x77, %ax
sete %dl
orb %cl, %dl
movq 0x8(%rbx), %rax
cmpb $0x1, %dl
jne 0x4c16
movq 0x10(%rax), %rdi
callq 0x3c62
jmp 0x4c52
movq 0x8(%rbx), %rdi
callq 0x380a
jmp 0x4c52
movq 0x20(%rax), %rdi
callq 0x40cc
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
leaq 0x1699(%rip), %rsi      # 0x62c7
movq %rsp, %rdx
xorl %eax, %eax
callq 0x3ef2
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x40c2
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
callq 0x40c2
movq 0x8(%rbx), %rax
movq 0x30(%rax), %rcx
movq %rcx, 0x28(%rax)
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop