// File: jkingan[P]ladyheather/heather.cpp
// Function: read_config_file(char*, unsigned char, unsigned char)
int read_config_file(char *name, u08 local_flag, u08 add_path)
{
char fn[MAX_PATH+1];
char msg[MAX_PATH+80];
char buf[SLEN+1];
char *dot;
FILE *file;
int error;
int i;

   // read command line options from a .CFG file
   if(name == 0) return 0;

printf("read_config_file(name:%s  local:%d  add:%d)  path:%s\n", name,(int) local_flag, (int) add_path, heather_path);
   fn[0] = 0;
   if(add_path) {
      strcat(fn, heather_path);
   }
   strcat(fn, name);

   dot = strstr(&fn[1], ".");
   if(dot) {   // file name has a .EXTension
               // replace with .CFG,  unless user gave the name on the command line
      if(local_flag == 1) {  // cfg file name is based upon the .EXE file name
         strcpy(dot, ".cfg");
      }
      else if(local_flag == 2) {
         strcpy(dot, ".cal");
      }
   }
   else {
      if(local_flag == 1) strcat(fn, ".cfg");  // no extension given,  use .CFG
      else                strcat(fn, ".cal");  // no extension given,  use .CAL
   }

   if(local_flag) {  // cfg file name is based upon the .EXE file name
      dot = strstr(fn, ".");
      while(dot >= &fn[0]) {  // look in current directory first
#ifdef WINDOWS
         if(*dot == '\\') break;
#else  // __linux__  __MACH__  __FreeBSD__
         if(*dot == '/') break;
#endif
         --dot;
      }
printf("Attempting to open config file(%s):(%s)\n", fn, dot+1);
      file = topen(dot+1, "r");
      if(file) {
         if(local_flag == 2) printf("Reading calendar file:%s\n", dot+1);
         else                printf("Reading config file:%s\n", dot+1);
      }
   }
   else file = 0;

   if(file == 0) {  // now look in .EXE file directory
      file = topen(fn, "r");
printf("Attempting to open config file::%s\n", fn);
      if(file == 0) { // config file not found
         if(local_flag) return 1;
         sprintf(msg, "\nConfig file %s not found!", fn);
         buf[0] = 0;
         command_help(msg, buf, 0);
         return 1;
//qqq    exit(10);
      }
      printf("Reading config file: %s\n", fn);
   }

   if(local_flag == 2) {
      fclose(file);
      #ifdef GREET_STUFF
         read_calendar(fn, 1);
      #endif
      return 0;
   }
#ifdef WINDOWS
   else
   {
   memset(heather_cfg_path, 0, sizeof(heather_cfg_path));
   _snprintf(heather_cfg_path,sizeof(heather_cfg_path)-1,"%s", fn);
   }
#endif

   while(fgets(buf, sizeof buf, file) != NULL) {
      if(buf[0] == '@') {       // @keyboard command - copy to temp script file
         if(local_flag != 2) {  // it's not from a calendar file
            if(temp_script == 0) {
               temp_script = topen(TEMP_SCRIPT, "w");
            }
            if(temp_script && buf[0] && buf[1]) {  // copy line to temporary script file for later execution
               fputs(&buf[1], temp_script);
            }
         }
      }
      else {
         if((buf[0] != '/') && (buf[0] != '-') && (buf[0] != '=') && (buf[0] != '$')) continue;
         for(i=strlen(buf)-1; i>=0; i--) {       // trim trailing garbage from line
            if     (buf[i] == 0x0A) buf[i] = 0;
            else if(buf[i] == 0x0D) buf[i] = 0;
            else if(buf[i] == ' ')  buf[i] = 0;
            else if(buf[i] == '\t') buf[i] = 0;
            else break;
         }

         error = option_switch(buf);

         if(error) {
            sprintf(blanks, "in config file %s: ", fn);
            command_help(blanks, buf, 0);
            return 1;
//qqq       exit(10);
         }
      }
   }

   fclose(file);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3a0, %rsp            # imm = 0x3A0
movb %dl, %al
movb %sil, %cl
movq %rdi, -0x10(%rbp)
movb %cl, -0x11(%rbp)
movb %al, -0x12(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2994d
movl $0x0, -0x4(%rbp)
jmp 0x29e1f
movq -0x10(%rbp), %rsi
movzbl -0x11(%rbp), %edx
movzbl -0x12(%rbp), %ecx
leaq 0x1472e6(%rip), %rdi    # 0x170c46
leaq 0x1a6509(%rip), %r8     # 0x1cfe70
movb $0x0, %al
callq 0x9030
movb $0x0, -0x120(%rbp)
cmpb $0x0, -0x12(%rbp)
je 0x2998e
leaq -0x120(%rbp), %rdi
leaq 0x1a64e7(%rip), %rsi    # 0x1cfe70
callq 0x96a0
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x96a0
leaq -0x120(%rbp), %rdi
addq $0x1, %rdi
leaq 0x16c418(%rip), %rsi    # 0x195dc8
callq 0x9090
movq %rax, -0x388(%rbp)
cmpq $0x0, -0x388(%rbp)
je 0x29a04
movzbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x299e4
movq -0x388(%rbp), %rdi
leaq 0x155713(%rip), %rsi    # 0x17f0f0
callq 0x9350
jmp 0x29a02
movzbl -0x11(%rbp), %eax
cmpl $0x2, %eax
jne 0x29a00
movq -0x388(%rbp), %rdi
leaq 0x147281(%rip), %rsi    # 0x170c7c
callq 0x9350
jmp 0x29a02
jmp 0x29a37
movzbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x29a22
leaq -0x120(%rbp), %rdi
leaq 0x1556d5(%rip), %rsi    # 0x17f0f0
callq 0x96a0
jmp 0x29a35
leaq -0x120(%rbp), %rdi
leaq 0x14724c(%rip), %rsi    # 0x170c7c
callq 0x96a0
jmp 0x29a37
cmpb $0x0, -0x11(%rbp)
je 0x29b1c
leaq -0x120(%rbp), %rdi
leaq 0x16c379(%rip), %rsi    # 0x195dc8
callq 0x9090
movq %rax, -0x388(%rbp)
movq -0x388(%rbp), %rax
leaq -0x120(%rbp), %rcx
cmpq %rcx, %rax
jb 0x29a93
movq -0x388(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x29a7f
jmp 0x29a93
movq -0x388(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x388(%rbp)
jmp 0x29a5b
leaq -0x120(%rbp), %rsi
movq -0x388(%rbp), %rdx
addq $0x1, %rdx
leaq 0x1471d5(%rip), %rdi    # 0x170c81
movb $0x0, %al
callq 0x9030
movq -0x388(%rbp), %rdi
addq $0x1, %rdi
leaq 0x15a548(%rip), %rsi    # 0x18400d
callq 0x9b20
movq %rax, -0x390(%rbp)
cmpq $0x0, -0x390(%rbp)
je 0x29b1a
movzbl -0x11(%rbp), %eax
cmpl $0x2, %eax
jne 0x29aff
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq 0x1471b4(%rip), %rdi    # 0x170caa
movb $0x0, %al
callq 0x9030
jmp 0x29b18
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq 0x1471b3(%rip), %rdi    # 0x170cc4
movb $0x0, %al
callq 0x9030
jmp 0x29b1a
jmp 0x29b27
movq $0x0, -0x390(%rbp)
cmpq $0x0, -0x390(%rbp)
jne 0x29bdb
leaq -0x120(%rbp), %rdi
leaq 0x15a4ca(%rip), %rsi    # 0x18400d
callq 0x9b20
movq %rax, -0x390(%rbp)
leaq -0x120(%rbp), %rsi
leaq 0x14717f(%rip), %rdi    # 0x170cdc
movb $0x0, %al
callq 0x9030
cmpq $0x0, -0x390(%rbp)
jne 0x29bc6
cmpb $0x0, -0x11(%rbp)
je 0x29b80
movl $0x1, -0x4(%rbp)
jmp 0x29e1f
leaq -0x280(%rbp), %rdi
leaq -0x120(%rbp), %rdx
leaq 0x14716b(%rip), %rsi    # 0x170d00
movb $0x0, %al
callq 0x9060
movb $0x0, -0x380(%rbp)
leaq -0x280(%rbp), %rdi
leaq -0x380(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x29e30
movl $0x1, -0x4(%rbp)
jmp 0x29e1f
leaq -0x120(%rbp), %rsi
leaq 0x147147(%rip), %rdi    # 0x170d1b
movb $0x0, %al
callq 0x9030
movzbl -0x11(%rbp), %eax
cmpl $0x2, %eax
jne 0x29c0d
movq -0x390(%rbp), %rdi
callq 0x9380
leaq -0x120(%rbp), %rdi
movl $0x1, %esi
callq 0x12b680
movl $0x0, -0x4(%rbp)
jmp 0x29e1f
jmp 0x29c0f
leaq -0x380(%rbp), %rdi
movq -0x390(%rbp), %rdx
movl $0xfb, %esi
callq 0x95e0
cmpq $0x0, %rax
je 0x29e0c
movsbl -0x380(%rbp), %eax
cmpl $0x40, %eax
jne 0x29ca4
movzbl -0x11(%rbp), %eax
cmpl $0x2, %eax
je 0x29c9f
cmpq $0x0, 0x273942(%rip)    # 0x29d590
jne 0x29c6a
leaq 0x1470dd(%rip), %rdi    # 0x170d34
leaq 0x15a9ed(%rip), %rsi    # 0x18464b
callq 0x9b20
movq %rax, 0x273926(%rip)    # 0x29d590
cmpq $0x0, 0x27391e(%rip)    # 0x29d590
je 0x29c9d
cmpb $0x0, -0x380(%rbp)
je 0x29c9d
cmpb $0x0, -0x37f(%rbp)
je 0x29c9d
leaq -0x380(%rbp), %rdi
addq $0x1, %rdi
movq 0x2738f8(%rip), %rsi    # 0x29d590
callq 0x9410
jmp 0x29c9f
jmp 0x29e07
movsbl -0x380(%rbp), %eax
cmpl $0x2f, %eax
je 0x29cd9
movsbl -0x380(%rbp), %eax
cmpl $0x2d, %eax
je 0x29cd9
movsbl -0x380(%rbp), %eax
cmpl $0x3d, %eax
je 0x29cd9
movsbl -0x380(%rbp), %eax
cmpl $0x24, %eax
je 0x29cd9
jmp 0x29c0f
leaq -0x380(%rbp), %rdi
callq 0x9120
subq $0x1, %rax
movl %eax, -0x398(%rbp)
cmpl $0x0, -0x398(%rbp)
jl 0x29dae
movslq -0x398(%rbp), %rax
movsbl -0x380(%rbp,%rax), %eax
cmpl $0xa, %eax
jne 0x29d21
movslq -0x398(%rbp), %rax
movb $0x0, -0x380(%rbp,%rax)
jmp 0x29d98
movslq -0x398(%rbp), %rax
movsbl -0x380(%rbp,%rax), %eax
cmpl $0xd, %eax
jne 0x29d46
movslq -0x398(%rbp), %rax
movb $0x0, -0x380(%rbp,%rax)
jmp 0x29d96
movslq -0x398(%rbp), %rax
movsbl -0x380(%rbp,%rax), %eax
cmpl $0x20, %eax
jne 0x29d6b
movslq -0x398(%rbp), %rax
movb $0x0, -0x380(%rbp,%rax)
jmp 0x29d94
movslq -0x398(%rbp), %rax
movsbl -0x380(%rbp,%rax), %eax
cmpl $0x9, %eax
jne 0x29d90
movslq -0x398(%rbp), %rax
movb $0x0, -0x380(%rbp,%rax)
jmp 0x29d92
jmp 0x29dae
jmp 0x29d94
jmp 0x29d96
jmp 0x29d98
jmp 0x29d9a
movl -0x398(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x398(%rbp)
jmp 0x29cef
leaq -0x380(%rbp), %rdi
callq 0x46ed0
movl %eax, -0x394(%rbp)
cmpl $0x0, -0x394(%rbp)
je 0x29e05
leaq -0x120(%rbp), %rdx
leaq 0x371419(%rip), %rdi    # 0x39b1f0
leaq 0x146f63(%rip), %rsi    # 0x170d41
movb $0x0, %al
callq 0x9060
leaq -0x380(%rbp), %rsi
leaq 0x3713fd(%rip), %rdi    # 0x39b1f0
xorl %eax, %eax
movl %eax, %edx
callq 0x29e30
movl $0x1, -0x4(%rbp)
jmp 0x29e1f
jmp 0x29e07
jmp 0x29c0f
movq -0x390(%rbp), %rdi
callq 0x9380
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x3a0, %rsp            # imm = 0x3A0
popq %rbp
retq
nopl (%rax,%rax)