// File: jkingan[P]ladyheather/heathui.cpp
// Function: rpn_calc()
int rpn_calc()
{
   // rpn calculator
   if(last_touch_key == 999) {  // ? command sent from touch keyboard
      show_rpn_help = 1;
   }

   rpn_break = 0;
   if(edit_buffer[0] == 0) {  // empty line, exit calculator
      if(zoom_screen) remove_zoom();
      rpn_mode = 0;
      show_version_header();
      rpn_unnest();
      rpn_err_flag = 0;
      return 0;
   }

// rpn_err_flag = 0;
   rpn_mode = 1;
   show_version_header();
   strlwr(edit_buffer);
   strcpy(nmea_msg, edit_buffer);  // prepare to parse the line
   msg_col = 0;

if(strstr(edit_buffer, "z`")) goto poop;

   // process the line of calculator commands (separated by spaces)
   rpn_again:
   msg_field[0] = 0;
   while(get_msg_field()) {
//      if(!strcmp(msg_field, "?") == 0) {
      if(strcmp(msg_field, "?")) {
         if(last_touch_key == 999) { last_touch_key = 0; show_rpn_help = 0;}
      }

      if(rpn_break) {   // stop parsing line on any errors or "break" command
         rpn_break = 0;
         skip_next_rpn = 0;
         break;
      }

      if(skip_next_rpn > 1) {  // conditional was true skip second operand
         --skip_next_rpn;
      }
      else if(skip_next_rpn == 1) {  // skip next command
         skip_next_rpn = 0;
         continue;
      }
      else if(skip_next_rpn < 0) {  // skip rest of line
         skip_next_rpn = 0;
         break;
      }

      // see what we just got
      if((msg_field[0] == '.') && msg_field[1]) {  // floating point number
         if(valid_num(msg_field)) get_rpn_num();
         else {
            sprintf(out, "Invalid number: %s", msg_field);
            rpn_error(out);
            break;
         }
      }
      else if((msg_field[0] == '+') && msg_field[1]) { // signed number
         if(valid_num(msg_field)) get_rpn_num();
         else {
            sprintf(out, "Invalid number: %s", msg_field);
            rpn_error(out);
            break;
         }
      }
      else if((msg_field[0] == '-') && msg_field[1]) { // signed number
         if(valid_num(msg_field)) get_rpn_num();
         else {
            sprintf(out, "Invalid number: %s", msg_field);
            rpn_error(out);
            break;
         }
      }
      else if(isdigit(msg_field[0])) { // possible number
         if((msg_field[0] == '0') && (msg_field[1] == 'x')) {  // hex number
            if(valid_hex(&msg_field[2])) get_rpn_num();
            else {
               sprintf(out, "Invalid hex constant: %s", msg_field);
               rpn_error(out);
               break;
            }
         }
         else if((msg_field[0] == '0') && (msg_field[1] == 'o')) {  // octal number
            if(valid_octal(&msg_field[2])) get_rpn_num();
            else {
               sprintf(out, "Invalid octal constant: %s", msg_field);
               rpn_error(out);
               break;
            }
         }
         else if((msg_field[0] == '0') && (msg_field[1] == 'b')) {  // binary number
            if(valid_binary(&msg_field[2])) get_rpn_num();
            else {
               sprintf(out, "Invalid binary constant: %s", msg_field);
               rpn_error(out);
               break;
            }
         }
         else if(valid_num(msg_field)) {  // decimal number
            get_rpn_num();
         }
         else {
            sprintf(out, "Invalid number: %s", msg_field);
            rpn_error(out);
            break;
         }
      }
      else if(rpn_const()) ;   // calculator operations (constants, etc - rpn_const() must be called before rpn_math)
      else if(rpn_math()) ;    // calculator operations (math, etc)
      else if(!strcmp(msg_field, "?"))    { show_rpn_help = 1; }
      else if(!strcmp(msg_field, "help")) { show_rpn_help = 1; }
      else if(!strcmp(msg_field, "showdefs")) { show_rpn_help = 2; }
      else if(!strcmp(msg_field, "zoom")) {
         add_kbd(ESC_CHAR);
         start_calc_zoom(10);
      }
      else if(msg_field[0]) {
         sprintf(out, "Unknown operation: (%s)", msg_field);
         rpn_error(out);
         break;
      }
      else break;  // end of line
   }

poop:
   skip_next_rpn = 0;
   if(rpn_unnest()) {  // we got to the end of a user defined command, go back to where we were
      goto rpn_again;
   }

   rpn_show(); // show the rpn stack
   rpn_mode = (-1);

   return rpn_err_flag;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
leaq 0x2490f9(%rip), %rax    # 0x27a408
cmpl $0x3e7, (%rax)          # imm = 0x3E7
jne 0x31324
leaq 0x3696ee(%rip), %rax    # 0x39aa0c
movl $0x1, (%rax)
movl $0x0, 0x370c02(%rip)    # 0x3a1f30
leaq 0x36d18b(%rip), %rax    # 0x39e4c0
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x3137b
leaq 0x33a644(%rip), %rax    # 0x36b988
cmpb $0x0, (%rax)
je 0x3134e
callq 0x319b0
leaq 0x3696b3(%rip), %rax    # 0x39aa08
movl $0x0, (%rax)
callq 0x9c6d0
callq 0x31ac0
movl $0x0, 0x372589(%rip)    # 0x3a38f8
movl $0x0, -0x4(%rbp)
jmp 0x318ce
leaq 0x369686(%rip), %rax    # 0x39aa08
movl $0x1, (%rax)
callq 0x9c6d0
leaq 0x36d12c(%rip), %rdi    # 0x39e4c0
callq 0x9850
leaq 0x369670(%rip), %rdi    # 0x39aa10
leaq 0x36d119(%rip), %rsi    # 0x39e4c0
callq 0x9350
leaq 0x369a71(%rip), %rax    # 0x39ae24
movl $0x0, (%rax)
leaq 0x36d100(%rip), %rdi    # 0x39e4c0
leaq 0x140fad(%rip), %rsi    # 0x172374
callq 0x9090
cmpq $0x0, %rax
je 0x313d7
jmp 0x3189a
jmp 0x313d9
leaq 0x369840(%rip), %rax    # 0x39ac20
movb $0x0, (%rax)
callq 0xb0bd0
cmpl $0x0, %eax
je 0x31898
leaq 0x369828(%rip), %rdi    # 0x39ac20
leaq 0x1559ad(%rip), %rsi    # 0x186dac
callq 0x9510
cmpl $0x0, %eax
je 0x31434
leaq 0x248ff8(%rip), %rax    # 0x27a408
cmpl $0x3e7, (%rax)          # imm = 0x3E7
jne 0x31432
leaq 0x248fe9(%rip), %rax    # 0x27a408
movl $0x0, (%rax)
leaq 0x3695e0(%rip), %rax    # 0x39aa0c
movl $0x0, (%rax)
jmp 0x31434
cmpl $0x0, 0x370af5(%rip)    # 0x3a1f30
je 0x31456
movl $0x0, 0x370ae9(%rip)    # 0x3a1f30
movl $0x0, 0x370adb(%rip)    # 0x3a1f2c
jmp 0x31898
cmpl $0x1, 0x370acf(%rip)    # 0x3a1f2c
jle 0x31470
movl 0x370ac7(%rip), %eax    # 0x3a1f2c
addl $-0x1, %eax
movl %eax, 0x370abe(%rip)    # 0x3a1f2c
jmp 0x314a4
cmpl $0x1, 0x370ab5(%rip)    # 0x3a1f2c
jne 0x31488
movl $0x0, 0x370aa9(%rip)    # 0x3a1f2c
jmp 0x313e3
cmpl $0x0, 0x370a9d(%rip)    # 0x3a1f2c
jge 0x314a0
movl $0x0, 0x370a91(%rip)    # 0x3a1f2c
jmp 0x31898
jmp 0x314a2
jmp 0x314a4
leaq 0x369775(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x3150a
leaq 0x369766(%rip), %rax    # 0x39ac20
cmpb $0x0, 0x1(%rax)
je 0x3150a
leaq 0x369759(%rip), %rdi    # 0x39ac20
callq 0x33400
cmpl $0x0, %eax
je 0x314d8
callq 0x335a0
jmp 0x31505
leaq 0x369f21(%rip), %rdi    # 0x39b400
leaq 0x140e91(%rip), %rsi    # 0x172377
leaq 0x369733(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369f05(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x31893
leaq 0x36970f(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x2b, %eax
jne 0x31570
leaq 0x369700(%rip), %rax    # 0x39ac20
cmpb $0x0, 0x1(%rax)
je 0x31570
leaq 0x3696f3(%rip), %rdi    # 0x39ac20
callq 0x33400
cmpl $0x0, %eax
je 0x3153e
callq 0x335a0
jmp 0x3156b
leaq 0x369ebb(%rip), %rdi    # 0x39b400
leaq 0x140e2b(%rip), %rsi    # 0x172377
leaq 0x3696cd(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369e9f(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x31891
leaq 0x3696a9(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x315d6
leaq 0x36969a(%rip), %rax    # 0x39ac20
cmpb $0x0, 0x1(%rax)
je 0x315d6
leaq 0x36968d(%rip), %rdi    # 0x39ac20
callq 0x33400
cmpl $0x0, %eax
je 0x315a4
callq 0x335a0
jmp 0x315d1
leaq 0x369e55(%rip), %rdi    # 0x39b400
leaq 0x140dc5(%rip), %rsi    # 0x172377
leaq 0x369667(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369e39(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x3188f
leaq 0x369643(%rip), %rax    # 0x39ac20
movsbl (%rax), %edi
callq 0x9330
cmpl $0x0, %eax
je 0x31782
leaq 0x36962b(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x3165b
leaq 0x36961c(%rip), %rax    # 0x39ac20
movsbl 0x1(%rax), %eax
cmpl $0x78, %eax
jne 0x3165b
leaq 0x36960c(%rip), %rdi    # 0x39ac20
addq $0x2, %rdi
callq 0x33200
cmpl $0x0, %eax
je 0x31629
callq 0x335a0
jmp 0x31656
leaq 0x369dd0(%rip), %rdi    # 0x39b400
leaq 0x140d53(%rip), %rsi    # 0x17238a
leaq 0x3695e2(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369db4(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x3177d
leaq 0x3695be(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x316c8
leaq 0x3695af(%rip), %rax    # 0x39ac20
movsbl 0x1(%rax), %eax
cmpl $0x6f, %eax
jne 0x316c8
leaq 0x36959f(%rip), %rdi    # 0x39ac20
addq $0x2, %rdi
callq 0x332c0
cmpl $0x0, %eax
je 0x31696
callq 0x335a0
jmp 0x316c3
leaq 0x369d63(%rip), %rdi    # 0x39b400
leaq 0x140cff(%rip), %rsi    # 0x1723a3
leaq 0x369575(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369d47(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x3177b
leaq 0x369551(%rip), %rax    # 0x39ac20
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x31732
leaq 0x369542(%rip), %rax    # 0x39ac20
movsbl 0x1(%rax), %eax
cmpl $0x62, %eax
jne 0x31732
leaq 0x369532(%rip), %rdi    # 0x39ac20
addq $0x2, %rdi
callq 0x33360
cmpl $0x0, %eax
je 0x31703
callq 0x335a0
jmp 0x31730
leaq 0x369cf6(%rip), %rdi    # 0x39b400
leaq 0x140cad(%rip), %rsi    # 0x1723be
leaq 0x369508(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369cda(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x31779
leaq 0x3694e7(%rip), %rdi    # 0x39ac20
callq 0x33400
cmpl $0x0, %eax
je 0x3174a
callq 0x335a0
jmp 0x31777
leaq 0x369caf(%rip), %rdi    # 0x39b400
leaq 0x140c1f(%rip), %rsi    # 0x172377
leaq 0x3694c1(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369c93(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x31779
jmp 0x3177b
jmp 0x3177d
jmp 0x3188d
callq 0x33820
cmpl $0x0, %eax
je 0x31791
jmp 0x3188b
callq 0x34ac0
cmpl $0x0, %eax
je 0x317a0
jmp 0x31889
leaq 0x369479(%rip), %rdi    # 0x39ac20
leaq 0x1555fe(%rip), %rsi    # 0x186dac
callq 0x9510
cmpl $0x0, %eax
jne 0x317ca
leaq 0x36924d(%rip), %rax    # 0x39aa0c
movl $0x1, (%rax)
jmp 0x31887
leaq 0x36944f(%rip), %rdi    # 0x39ac20
leaq 0x1413e7(%rip), %rsi    # 0x172bbf
callq 0x9510
cmpl $0x0, %eax
jne 0x317f4
leaq 0x369223(%rip), %rax    # 0x39aa0c
movl $0x1, (%rax)
jmp 0x31885
leaq 0x369425(%rip), %rdi    # 0x39ac20
leaq 0x140bd8(%rip), %rsi    # 0x1723da
callq 0x9510
cmpl $0x0, %eax
jne 0x3181b
leaq 0x3691f9(%rip), %rax    # 0x39aa0c
movl $0x2, (%rax)
jmp 0x31883
leaq 0x3693fe(%rip), %rdi    # 0x39ac20
leaq 0x140bba(%rip), %rsi    # 0x1723e3
callq 0x9510
cmpl $0x0, %eax
jne 0x31849
movl $0x1b, %edi
callq 0xf320
movl $0xa, %edi
callq 0x2e280
jmp 0x31881
leaq 0x3693d0(%rip), %rax    # 0x39ac20
cmpb $0x0, (%rax)
je 0x3187f
leaq 0x369ba4(%rip), %rdi    # 0x39b400
leaq 0x140b85(%rip), %rsi    # 0x1723e8
leaq 0x3693b6(%rip), %rdx    # 0x39ac20
movb $0x0, %al
callq 0x9060
leaq 0x369b88(%rip), %rdi    # 0x39b400
callq 0x2e640
jmp 0x31898
jmp 0x31898
jmp 0x31883
jmp 0x31885
jmp 0x31887
jmp 0x31889
jmp 0x3188b
jmp 0x3188d
jmp 0x3188f
jmp 0x31891
jmp 0x31893
jmp 0x313e3
jmp 0x3189a
movl $0x0, 0x370688(%rip)    # 0x3a1f2c
callq 0x31ac0
cmpl $0x0, %eax
je 0x318b3
jmp 0x313d9
callq 0x32c60
leaq 0x369149(%rip), %rax    # 0x39aa08
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl 0x37202d(%rip), %eax    # 0x3a38f8
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10, %rsp
popq %rbp
retq
nopw (%rax,%rax)