// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x20(%rax), %rdi
callq 0x3f7c
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
pushq $0x3a
popq %rsi
callq 0x3dc2
xorl %r15d, %r15d
leaq 0xc(%rsp), %r14
movslq %r15d, %r12
cmpl $0x13, %r15d
jg 0x4947
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movq %r14, %rsi
callq 0x3e5e
movl 0xc(%rsp), %ebp
cmpl $0xa, %ebp
je 0x4947
cmpl $0x1b, %ebp
je 0x4a10
testl %r15d, %r15d
setle %al
cmpl $0x107, %ebp            # imm = 0x107
setne %cl
orb %al, %cl
je 0x491c
movl %ebp, %edi
callq 0x3430
testl %eax, %eax
je 0x48b6
incl %r15d
movb %bpl, 0x10(%rsp,%r12)
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movl %ebp, %esi
callq 0x3dc2
jmp 0x48b6
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
leal -0x1(%r15), %ebp
xorl %esi, %esi
movl %r15d, %edx
callq 0x3f68
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x3dcc
movl %ebp, %r15d
jmp 0x48b6
movb $0x0, 0x10(%rsp,%r12)
leaq 0x190f(%rip), %rdi      # 0x6263
leaq 0x10(%rsp), %r14
pushq $0x5
popq %rdx
movq %r14, %rsi
callq 0x3260
testl %eax, %eax
sete %al
movzwl (%r14), %ebp
cmpw $0x71, %bp
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x49aa
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rdi
callq 0x3d22
movq 0x8(%rbx), %rdi
testb %al, %al
jne 0x4a23
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
leaq 0x18c6(%rip), %rsi      # 0x6268
callq *0x20(%rax)
jmp 0x4a5b
leaq 0x18e7(%rip), %rdi      # 0x6298
leaq 0x10(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x3260
testl %eax, %eax
je 0x4a1f
leaq 0x18d5(%rip), %rdi      # 0x629e
leaq 0x10(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x3260
testl %eax, %eax
je 0x4a1f
leaq 0x18c0(%rip), %rdi      # 0x62a1
leaq 0x10(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x3260
testl %eax, %eax
sete %al
cmpw $0x77, %bp
sete %cl
orb %al, %cl
movq 0x8(%rbx), %rax
cmpb $0x1, %cl
jne 0x4a2a
movq 0x10(%rax), %rdi
callq 0x3b54
jmp 0x4a68
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x3f7c
jmp 0x4a68
movq 0x8(%rbx), %rdi
callq 0x37d2
jmp 0x4a68
movq 0x20(%rax), %rdi
callq 0x3f7c
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
leaq 0x1865(%rip), %rsi      # 0x62a7
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3dd6
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
callq 0x3f72
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
callq 0x3f72
movq 0x8(%rbx), %rax
movq 0x30(%rax), %rcx
movq %rcx, 0x28(%rax)
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop