// File: /workspace/github_c_makefile_star2_O0/moefh[P]hed/src/editor.c
// Function: prompt_get_text
static int prompt_get_text(struct hed_editor *editor, const char *prompt, char *str, size_t max_str_len)
{
  struct hed_screen *scr = &editor->screen;

  size_t prompt_len = strlen(prompt);
  size_t str_len = strlen(str);
  size_t cursor_pos = str_len;
  char key_err[64];

  clear_msg();
  scr->redraw_needed = true;
  while (! editor->quit) {
    show_cursor(false);
    if (scr->redraw_needed)
      draw_main_screen(editor);
    reset_color();
    set_color(FG_BLACK, BG_GRAY);
    move_cursor(1, scr->h - EDITOR_FOOTER_LINES + 1);
    out(" %s: %s", prompt, str);
    clear_eol();
    size_t scr_cursor_pos = utf8_len_upto(str, str + cursor_pos);
    move_cursor(4 + prompt_len + scr_cursor_pos, scr->h - EDITOR_FOOTER_LINES + 1);
    set_color(FG_BLACK, BG_GRAY);
    show_cursor(true);
    hed_scr_flush();
    
    int k = read_key(scr->term_fd, key_err, sizeof(key_err));
    switch (k) {
    case KEY_REDRAW:
      show_cursor(false);
      reset_color();
      clear_screen();
      show_cursor(true);
      scr->redraw_needed = true;
      break;
      
    case CTRL_KEY('c'):
    case '\r':
      editor->mode = HED_MODE_DEFAULT;
      show_cursor(false);
      clear_msg();
      return (k == '\r') ? 0 : -1;

    case CTRL_KEY('a'):
    case KEY_HOME:
      cursor_pos = 0;
      break;
      
    case CTRL_KEY('e'):
    case KEY_END:
      cursor_pos = str_len;
      break;
      
    case CTRL_KEY('b'):
    case KEY_ARROW_LEFT:
      if (cursor_pos > 0) {
        const char *pos = utf8_prev(str, str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;
      
    case CTRL_KEY('f'):
    case KEY_ARROW_RIGHT:
      if (cursor_pos < str_len) {
        const char *pos = utf8_next(str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;

    case CTRL_KEY('t'):
      if (editor->mode == HED_MODE_READ_FILENAME) {
        show_cursor(false);
        char filename[256];
        if (hed_select_file(editor, filename, sizeof(filename)) >= 0) {
          size_t filename_len = strlen(filename) + 1;
          if (filename_len > max_str_len) {
            filename_len = max_str_len;
          }
          memcpy(str, filename, filename_len-1);
          str[filename_len-1] = '\0';
          editor->mode = HED_MODE_DEFAULT;
          return 0;
        }
        show_cursor(true);
        scr->redraw_needed = true;
      }
      break;
      
    case 8:
    case 127:
      if (cursor_pos > 0) {
        //memmove(str + cursor_pos - 1, str + cursor_pos, str_len - cursor_pos + 1);
        const char *prev = utf8_prev(str, str + cursor_pos);
        size_t n_erase = str + cursor_pos - prev;
        memmove(str + cursor_pos - n_erase, str + cursor_pos, str_len - cursor_pos + n_erase);
        cursor_pos -= n_erase;
        str_len -= n_erase;
      }
      break;

    case KEY_DEL:
      if (cursor_pos < str_len) {
        memmove(str + cursor_pos, str + cursor_pos + 1, str_len - cursor_pos + 1);
        str_len--;
      }
      break;
      
    default:
      if (k >= 32 && k < 127 && (str_len + 2 <= max_str_len)) {
        memmove(str + cursor_pos + 1, str + cursor_pos, str_len - cursor_pos + 1);
        str[cursor_pos++] = k;
        str_len++;
      }
    }
  }
  
  editor->mode = HED_MODE_DEFAULT;
  clear_msg();
  return 0;
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0x1c8(%rsp)
movq %rsi, 0x1c0(%rsp)
movq %rdx, 0x1b8(%rsp)
movq %rcx, 0x1b0(%rsp)
movq 0x1c8(%rsp), %rax
addq $0x108, %rax            # imm = 0x108
movq %rax, 0x1a8(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x20d0
movq %rax, 0x1a0(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x20d0
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x190(%rsp)
callq 0x3740
movq 0x1a8(%rsp), %rax
movb $0x1, 0xd(%rax)
movq 0x1c8(%rsp), %rax
movb (%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x926d
jmp 0x9852
xorl %edi, %edi
callq 0x3e00
movq 0x1a8(%rsp), %rax
testb $0x1, 0xd(%rax)
je 0x928f
movq 0x1c8(%rsp), %rdi
callq 0x67b0
callq 0x3d40
movl $0x1e, %edi
movl %edi, 0x8(%rsp)
movl $0x2f, %esi
movl %esi, 0xc(%rsp)
callq 0x3c80
movq 0x1a8(%rsp), %rax
movl 0x8(%rax), %esi
addl $-0x2, %esi
movl $0x1, %edi
movl %edi, 0x10(%rsp)
callq 0x3d80
movq 0x1c0(%rsp), %rsi
movq 0x1b8(%rsp), %rdx
leaq 0x2482(%rip), %rdi      # 0xb760
xorl %eax, %eax
callq 0x3850
callq 0x3d60
movq 0x1b8(%rsp), %rdi
movq 0x190(%rsp), %rax
movq %rdi, %rsi
addq %rax, %rsi
callq 0x4df0
movq %rax, 0x148(%rsp)
movl 0x1a0(%rsp), %edx
movl 0x148(%rsp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x4(%rax,%rcx), %edi
movq 0x1a8(%rsp), %rax
movl 0x8(%rax), %esi
addl $-0x2, %esi
callq 0x3d80
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
callq 0x3c80
movl 0x10(%rsp), %edi
callq 0x3e00
callq 0x3790
movq 0x1a8(%rsp), %rax
movl (%rax), %edi
leaq 0x150(%rsp), %rsi
movl $0x40, %edx
callq 0x2a80
movl %eax, 0x144(%rsp)
movl 0x144(%rsp), %eax
movl %eax, 0x14(%rsp)
subl $0x1, %eax
je 0x94d1
jmp 0x938a
movl 0x14(%rsp), %eax
subl $0x2, %eax
je 0x94f7
jmp 0x9399
movl 0x14(%rsp), %eax
subl $0x3, %eax
je 0x9493
jmp 0x93a8
movl 0x14(%rsp), %eax
subl $0x5, %eax
je 0x94e2
jmp 0x93b7
movl 0x14(%rsp), %eax
subl $0x6, %eax
je 0x9547
jmp 0x93c6
movl 0x14(%rsp), %eax
subl $0x8, %eax
je 0x966e
jmp 0x93d5
movl 0x14(%rsp), %eax
subl $0xd, %eax
je 0x9493
jmp 0x93e4
movl 0x14(%rsp), %eax
subl $0x14, %eax
je 0x9596
jmp 0x93f3
movl 0x14(%rsp), %eax
subl $0x7f, %eax
je 0x966e
jmp 0x9402
movl 0x14(%rsp), %eax
subl $0x110002, %eax         # imm = 0x110002
je 0x9467
jmp 0x940f
movl 0x14(%rsp), %eax
subl $0x111000, %eax         # imm = 0x111000
je 0x94f7
jmp 0x9420
movl 0x14(%rsp), %eax
subl $0x111001, %eax         # imm = 0x111001
je 0x9547
jmp 0x9431
movl 0x14(%rsp), %eax
subl $0x111005, %eax         # imm = 0x111005
je 0x9735
jmp 0x9442
movl 0x14(%rsp), %eax
subl $0x111006, %eax         # imm = 0x111006
je 0x94d1
jmp 0x9453
movl 0x14(%rsp), %eax
subl $0x111007, %eax         # imm = 0x111007
je 0x94e2
jmp 0x979d
xorl %edi, %edi
callq 0x3e00
callq 0x3d40
callq 0x3e40
movl $0x1, %edi
callq 0x3e00
movq 0x1a8(%rsp), %rax
movb $0x1, 0xd(%rax)
jmp 0x984d
movq 0x1c8(%rsp), %rax
movl $0x0, 0x104(%rax)
xorl %edi, %edi
callq 0x3e00
callq 0x3740
movl 0x144(%rsp), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $0xd, %edx
cmovel %ecx, %eax
movl %eax, 0x1d4(%rsp)
jmp 0x9874
movq $0x0, 0x190(%rsp)
jmp 0x984d
movq 0x198(%rsp), %rax
movq %rax, 0x190(%rsp)
jmp 0x984d
cmpq $0x0, 0x190(%rsp)
jbe 0x9542
movq 0x1b8(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
addq 0x190(%rsp), %rsi
callq 0x4a40
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rax
movq 0x1b8(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x190(%rsp)
jmp 0x984d
movq 0x190(%rsp), %rax
cmpq 0x198(%rsp), %rax
jae 0x9591
movq 0x1b8(%rsp), %rdi
addq 0x190(%rsp), %rdi
callq 0x4b10
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq 0x1b8(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x190(%rsp)
jmp 0x984d
movq 0x1c8(%rsp), %rax
cmpl $0x1, 0x104(%rax)
jne 0x9669
xorl %edi, %edi
callq 0x3e00
movq 0x1c8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x4e60
cmpl $0x0, %eax
jl 0x9653
leaq 0x30(%rsp), %rdi
callq 0x20d0
addq $0x1, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x1b0(%rsp), %rax
jbe 0x9601
movq 0x1b0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1b8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
subq $0x1, %rdx
callq 0x21a0
movq 0x1b8(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq 0x1c8(%rsp), %rax
movl $0x0, 0x104(%rax)
movl $0x0, 0x1d4(%rsp)
jmp 0x9874
movl $0x1, %edi
callq 0x3e00
movq 0x1a8(%rsp), %rax
movb $0x1, 0xd(%rax)
jmp 0x984d
cmpq $0x0, 0x190(%rsp)
jbe 0x9730
movq 0x1b8(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
addq 0x190(%rsp), %rsi
callq 0x4a40
movq %rax, 0x20(%rsp)
movq 0x1b8(%rsp), %rax
addq 0x190(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x1b8(%rsp), %rdi
addq 0x190(%rsp), %rdi
xorl %eax, %eax
subq 0x18(%rsp), %rax
addq %rax, %rdi
movq 0x1b8(%rsp), %rsi
addq 0x190(%rsp), %rsi
movq 0x198(%rsp), %rdx
subq 0x190(%rsp), %rdx
addq 0x18(%rsp), %rdx
callq 0x21f0
movq 0x18(%rsp), %rcx
movq 0x190(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x190(%rsp)
movq 0x18(%rsp), %rcx
movq 0x198(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x198(%rsp)
jmp 0x984d
movq 0x190(%rsp), %rax
cmpq 0x198(%rsp), %rax
jae 0x9798
movq 0x1b8(%rsp), %rdi
addq 0x190(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
addq 0x190(%rsp), %rsi
addq $0x1, %rsi
movq 0x198(%rsp), %rdx
subq 0x190(%rsp), %rdx
addq $0x1, %rdx
callq 0x21f0
movq 0x198(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x198(%rsp)
jmp 0x984d
cmpl $0x20, 0x144(%rsp)
jl 0x984b
cmpl $0x7f, 0x144(%rsp)
jge 0x984b
movq 0x198(%rsp), %rax
addq $0x2, %rax
cmpq 0x1b0(%rsp), %rax
ja 0x984b
movq 0x1b8(%rsp), %rdi
addq 0x190(%rsp), %rdi
addq $0x1, %rdi
movq 0x1b8(%rsp), %rsi
addq 0x190(%rsp), %rsi
movq 0x198(%rsp), %rdx
subq 0x190(%rsp), %rdx
addq $0x1, %rdx
callq 0x21f0
movl 0x144(%rsp), %eax
movb %al, %dl
movq 0x1b8(%rsp), %rax
movq 0x190(%rsp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x190(%rsp)
movb %dl, (%rax,%rcx)
movq 0x198(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x198(%rsp)
jmp 0x984d
jmp 0x9258
movq 0x1c8(%rsp), %rax
movl $0x0, 0x104(%rax)
callq 0x3740
movl $0x0, 0x1d4(%rsp)
movl 0x1d4(%rsp), %eax
addq $0x1d8, %rsp            # imm = 0x1D8
retq
nopw %cs:(%rax,%rax)