// File: jkingan[P]ladyheather/heathui.cpp
// Function: rpn_define(int)
int rpn_define(int skip_first)
{
int i, j;
int def_col;
char *s;
int old_mode;

   // create a user define command

   if(num_rpn_defs >= MAX_RPN_DEFS) {
      edit_error("Too many DEFINE commands seen!");
      return 1;
   }
   old_mode = rpn_mode;
   rpn_mode = 1;   // make sure get_msg_field() works

   def_col = msg_col;
   rpn_break = 1;
   if(skip_first) {
      get_msg_field();
      def_col = msg_col;
   }

   if(get_msg_field()) {
      if(!isalpha(msg_field[0])) {
         edit_error("DEFINE name must start with a letter!");
         rpn_mode = old_mode;
         return 2;
      }
      if(!strcmp(msg_field, "define")) {
         edit_error("Cannot re-define the DEFINE command!");
         rpn_mode = old_mode;
         return 22;
      }

      i = find_rpn_def(msg_field);
      if(i >= 0) {  // redefine
         if(rpn_defs[i]) {      // delete old define
            free(rpn_defs[i]);
            rpn_defs[i] = 0;
         }
         if(num_rpn_defs) {
            for(j=i; j<num_rpn_defs; j++) {
               rpn_defs[j] = rpn_defs[j+1];
            }
            --num_rpn_defs;
         }
         if(nmea_msg[msg_col]) {
            goto do_define;
         }
      }
      else if(i == (-1)) {  // new define
         do_define:
         if(nmea_msg[msg_col]) {
            s = (char *) (void *) calloc(strlen(&nmea_msg[def_col])+1, sizeof(char));
            if(s) {
               strcpy(s, &nmea_msg[def_col]);
               if(s[0] == 0) strcat(s, " ");
               rpn_defs[num_rpn_defs] = s;
               ++num_rpn_defs;
            }
            else {
               edit_error("Could not allocate DEFINE memory!");
               rpn_mode = old_mode;
               return 4;
            }
         }
         else {
            edit_error("No operations specified for DEFINE!");
            rpn_mode = old_mode;
            return 7;
         }
      }
      else {
         rpn_mode = old_mode;
         return 5;  // should never happen
      }
   }
   else {
      edit_error("DEFINE name missing!");
      rpn_mode = old_mode;
      return 6;
   }

   rpn_mode = old_mode;
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x64, 0x371806(%rip)   # 0x3a2268
jl 0x30a7c
leaq 0x141475(%rip), %rdi    # 0x171ee0
callq 0x30d60
movl $0x1, -0x4(%rbp)
jmp 0x30d4e
leaq 0x369f85(%rip), %rax    # 0x39aa08
movl (%rax), %eax
movl %eax, -0x24(%rbp)
leaq 0x369f79(%rip), %rax    # 0x39aa08
movl $0x1, (%rax)
leaq 0x36a388(%rip), %rax    # 0x39ae24
movl (%rax), %eax
movl %eax, -0x14(%rbp)
movl $0x1, 0x371485(%rip)    # 0x3a1f30
cmpl $0x0, -0x8(%rbp)
je 0x30ac2
callq 0xb0bd0
leaq 0x36a367(%rip), %rax    # 0x39ae24
movl (%rax), %eax
movl %eax, -0x14(%rbp)
callq 0xb0bd0
cmpl $0x0, %eax
je 0x30d1a
leaq 0x36a149(%rip), %rax    # 0x39ac20
movsbl (%rax), %edi
callq 0x9620
cmpl $0x0, %eax
jne 0x30b08
leaq 0x141414(%rip), %rdi    # 0x171eff
callq 0x30d60
movl -0x24(%rbp), %ecx
leaq 0x369f0e(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x30d4e
leaq 0x36a111(%rip), %rdi    # 0x39ac20
leaq 0x14140f(%rip), %rsi    # 0x171f25
callq 0x9510
cmpl $0x0, %eax
jne 0x30b44
leaq 0x141405(%rip), %rdi    # 0x171f2c
callq 0x30d60
movl -0x24(%rbp), %ecx
leaq 0x369ed2(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x16, -0x4(%rbp)
jmp 0x30d4e
leaq 0x36a0d5(%rip), %rdi    # 0x39ac20
callq 0x309c0
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jl 0x30c0b
movslq -0xc(%rbp), %rcx
leaq 0x3713d8(%rip), %rax    # 0x3a1f40
cmpq $0x0, (%rax,%rcx,8)
je 0x30b96
movslq -0xc(%rbp), %rcx
leaq 0x3713c6(%rip), %rax    # 0x3a1f40
movq (%rax,%rcx,8), %rdi
callq 0x9450
movslq -0xc(%rbp), %rcx
leaq 0x3713b2(%rip), %rax    # 0x3a1f40
movq $0x0, (%rax,%rcx,8)
cmpl $0x0, 0x3716cb(%rip)    # 0x3a2268
je 0x30bed
movl -0xc(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl 0x3716ba(%rip), %eax    # 0x3a2268
jge 0x30bde
movl -0x10(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x371380(%rip), %rax    # 0x3a1f40
movq (%rax,%rcx,8), %rdx
movslq -0x10(%rbp), %rcx
leaq 0x371371(%rip), %rax    # 0x3a1f40
movq %rdx, (%rax,%rcx,8)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x30ba5
movl 0x371684(%rip), %eax    # 0x3a2268
addl $-0x1, %eax
movl %eax, 0x37167b(%rip)    # 0x3a2268
leaq 0x36a230(%rip), %rax    # 0x39ae24
movslq (%rax), %rcx
leaq 0x369e12(%rip), %rax    # 0x39aa10
cmpb $0x0, (%rax,%rcx)
je 0x30c06
jmp 0x30c17
jmp 0x30d18
cmpl $-0x1, -0xc(%rbp)
jne 0x30d01
jmp 0x30c17
leaq 0x36a206(%rip), %rax    # 0x39ae24
movslq (%rax), %rcx
leaq 0x369de8(%rip), %rax    # 0x39aa10
cmpb $0x0, (%rax,%rcx)
je 0x30cde
movslq -0x14(%rbp), %rax
leaq 0x369dd3(%rip), %rdi    # 0x39aa10
addq %rax, %rdi
callq 0x9120
movq %rax, %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x9230
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x30cbb
movq -0x20(%rbp), %rdi
movslq -0x14(%rbp), %rax
leaq 0x369da0(%rip), %rsi    # 0x39aa10
addq %rax, %rsi
callq 0x9350
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x30c94
movq -0x20(%rbp), %rdi
leaq 0x155155(%rip), %rsi    # 0x185de4
callq 0x96a0
movq -0x20(%rbp), %rdx
movslq 0x3715c9(%rip), %rcx    # 0x3a2268
leaq 0x37129a(%rip), %rax    # 0x3a1f40
movq %rdx, (%rax,%rcx,8)
movl 0x3715b8(%rip), %eax    # 0x3a2268
addl $0x1, %eax
movl %eax, 0x3715af(%rip)    # 0x3a2268
jmp 0x30cdc
leaq 0x14128f(%rip), %rdi    # 0x171f51
callq 0x30d60
movl -0x24(%rbp), %ecx
leaq 0x369d37(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x30d4e
jmp 0x30cff
leaq 0x14128e(%rip), %rdi    # 0x171f73
callq 0x30d60
movl -0x24(%rbp), %ecx
leaq 0x369d14(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x7, -0x4(%rbp)
jmp 0x30d4e
jmp 0x30d16
movl -0x24(%rbp), %ecx
leaq 0x369cfd(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x30d4e
jmp 0x30d18
jmp 0x30d3b
leaq 0x141276(%rip), %rdi    # 0x171f97
callq 0x30d60
movl -0x24(%rbp), %ecx
leaq 0x369cd8(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x6, -0x4(%rbp)
jmp 0x30d4e
movl -0x24(%rbp), %ecx
leaq 0x369cc3(%rip), %rax    # 0x39aa08
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)