// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b20
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
movl $0x3a, %esi
callq 0x4830
cmpl $0x14, -0x34(%rbp)
jge 0x6230
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
leaq -0xc(%rbp), %rsi
callq 0x4920
cmpl $0x1b, -0xc(%rbp)
jne 0x61a2
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b20
movq -0x40(%rbp), %rdi
callq 0x63a0
jmp 0x638f
cmpl $0xa, -0xc(%rbp)
jne 0x61ad
jmp 0x6230
cmpl $0x107, -0xc(%rbp)      # imm = 0x107
jne 0x61ed
cmpl $0x0, -0x34(%rbp)
jle 0x61ed
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
movl -0x34(%rbp), %edx
movl %edx, %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
xorl %esi, %esi
callq 0x4ad0
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4860
jmp 0x6227
movl -0xc(%rbp), %edi
callq 0x3550
cmpl $0x0, %eax
je 0x6225
movq -0x40(%rbp), %rax
movl -0xc(%rbp), %ecx
movb %cl, %dl
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movb %dl, -0x30(%rbp,%rcx)
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
movl -0xc(%rbp), %esi
callq 0x4830
jmp 0x6227
jmp 0x6229
jmp 0x622b
jmp 0x615e
movslq -0x34(%rbp), %rax
movb $0x0, -0x30(%rbp,%rax)
leaq -0x30(%rbp), %rsi
leaq 0x1fd1(%rip), %rdi      # 0x8215
callq 0x3460
cmpl $0x0, %eax
je 0x6263
leaq -0x30(%rbp), %rsi
leaq 0x1fc1(%rip), %rdi      # 0x821a
callq 0x3460
cmpl $0x0, %eax
jne 0x62b8
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x45d0
testb $0x1, %al
jne 0x627a
jmp 0x6289
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3b60
jmp 0x62b3
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
movq (%rdi), %rax
leaq 0x1f7d(%rip), %rsi      # 0x821c
callq *0x20(%rax)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x4b00
jmp 0x6386
leaq -0x30(%rbp), %rsi
leaq 0x1f89(%rip), %rdi      # 0x824c
callq 0x3460
cmpl $0x0, %eax
je 0x62e2
leaq -0x30(%rbp), %rsi
leaq 0x1f7a(%rip), %rdi      # 0x8252
callq 0x3460
cmpl $0x0, %eax
jne 0x62f4
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3b60
jmp 0x6384
leaq -0x30(%rbp), %rsi
leaq 0x1f56(%rip), %rdi      # 0x8255
callq 0x3460
cmpl $0x0, %eax
je 0x631e
leaq -0x30(%rbp), %rsi
leaq 0x1f47(%rip), %rdi      # 0x825b
callq 0x3460
cmpl $0x0, %eax
jne 0x6331
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x41d0
jmp 0x6382
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b20
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
leaq -0x30(%rbp), %rdx
leaq 0x1f04(%rip), %rsi      # 0x825d
movb $0x0, %al
callq 0x4880
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b00
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x4b00
jmp 0x6384
jmp 0x6386
movq -0x40(%rbp), %rdi
callq 0x63a0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)