// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
testq %rdi, %rdi
je 0x1d4b2
movl %esi, %r14d
movq %rdi, %r12
movzbl %r14b, %r15d
movzbl %dl, %ebx
leaq 0xeda50(%rip), %rdi     # 0x10ac59
leaq 0x153a80(%rip), %r8     # 0x170c90
movq %r12, %rsi
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x9030
movb $0x0, -0x130(%rbp)
testb %bl, %bl
je 0x1d23d
leaq 0x153a5f(%rip), %rsi    # 0x170c90
leaq -0x130(%rbp), %rdi
callq 0x96c0
leaq -0x130(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x96c0
leaq -0x12f(%rbp), %rdi
movl $0x2e, %esi
callq 0x9100
testq %rax, %rax
je 0x1d278
testb %r14b, %r14b
je 0x1d2ae
cmpl $0x2, %r15d
je 0x1d2b3
cmpl $0x1, %r15d
je 0x1d293
jmp 0x1d2bd
leaq -0x130(%rbp), %rdi
callq 0x9120
addq %rbp, %rax
addq $-0x130, %rax           # imm = 0xFED0
cmpb $0x1, %r14b
jne 0x1d29f
movb $0x0, 0x4(%rax)
movl $0x6766632e, (%rax)     # imm = 0x6766632E
jmp 0x1d2bd
movb $0x0, 0x4(%rax)
movl $0x6c61632e, (%rax)     # imm = 0x6C61632E
testb %r14b, %r14b
jne 0x1d2bd
movb $0x1, %r12b
jmp 0x1d32e
movb $0x0, 0x4(%rax)
movl $0x6c61632e, (%rax)     # imm = 0x6C61632E
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x9100
movq %rax, %r15
cmpq %rbx, %rax
jb 0x1d2e0
cmpb $0x2f, (%r15)
je 0x1d2e0
decq %r15
cmpq %rbx, %r15
jae 0x1d2d2
incq %r15
leaq 0xed9aa(%rip), %rdi     # 0x10ac94
xorl %r12d, %r12d
leaq -0x130(%rbp), %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x9030
leaq 0x103a88(%rip), %rsi    # 0x120d8d
movq %r15, %rdi
callq 0x9a47
testq %rax, %rax
je 0x1d32e
movq %rax, %rbx
cmpb $0x2, %r14b
jne 0x1d39a
leaq 0xed99b(%rip), %rdi     # 0x10acbd
movq %r15, %rsi
xorl %eax, %eax
callq 0x9030
jmp 0x1d37c
leaq 0x103a58(%rip), %rsi    # 0x120d8d
leaq -0x130(%rbp), %r15
movq %r15, %rdi
callq 0x9a47
movq %rax, %rbx
leaq 0xed9a1(%rip), %rdi     # 0x10acef
movq %r15, %rsi
xorl %eax, %eax
callq 0x9030
testq %rbx, %rbx
je 0x1d4c8
leaq 0xed9c6(%rip), %rdi     # 0x10ad2e
leaq -0x130(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
cmpb $0x2, %r14b
jne 0x1d3ab
movq %rbx, %rdi
callq 0x9370
leaq -0x130(%rbp), %rdi
movl $0x1, %esi
callq 0xdae0e
jmp 0x1d4b2
leaq 0xed936(%rip), %rdi     # 0x10acd7
movq %r15, %rsi
xorl %eax, %eax
callq 0x9030
leaq -0x230(%rbp), %rdi
movl $0xfb, %esi
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
je 0x1d4aa
leaq -0x22f(%rbp), %r14
leaq -0x230(%rbp), %r15
movl $0x2000a01, %r12d       # imm = 0x2000A01
movabsq $0x100002600, %r13      # imm = 0x100002600
movzbl -0x230(%rbp), %eax
addl $-0x24, %eax
cmpl $0x1c, %eax
ja 0x1d43d
btl %eax, %r12d
jae 0x1d454
movq %r15, %rdi
callq 0x9120
decl %eax
js 0x1d42d
movl %eax, %eax
incq %rax
movzbl -0x231(%rbp,%rax), %ecx
cmpq $0x20, %rcx
ja 0x1d42d
btq %rcx, %r13
jae 0x1d42d
movb $0x0, -0x231(%rbp,%rax)
decq %rax
jg 0x1d40c
movq %r15, %rdi
callq 0x326cf
testl %eax, %eax
jne 0x1d507
movq %r15, %rdi
movl $0xfb, %esi
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
jne 0x1d3e6
jmp 0x1d4aa
cmpl $0x1c, %eax
jne 0x1d43d
movq 0x220f50(%rip), %rsi    # 0x23e3b0
movb $0x1, %al
testq %rsi, %rsi
jne 0x1d48e
leaq 0xed8d9(%rip), %rdi     # 0x10ad47
leaq 0x103f46(%rip), %rsi    # 0x1213bb
callq 0x9a47
movq %rax, %rsi
movq %rax, 0x220f2c(%rip)    # 0x23e3b0
cmpb $0x0, -0x230(%rbp)
setne %al
testq %rsi, %rsi
je 0x1d43d
testb %al, %al
je 0x1d43d
cmpb $0x0, -0x22f(%rbp)
je 0x1d43d
movq %r14, %rdi
callq 0x9400
jmp 0x1d43d
movq %rbx, %rdi
callq 0x9370
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ebx
testb %r12b, %r12b
je 0x1d4b4
leaq 0xed83a(%rip), %rsi     # 0x10ad13
leaq -0x390(%rbp), %r14
leaq -0x130(%rbp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
leaq -0x230(%rbp), %rsi
movb $0x0, (%rsi)
movq %r14, %rdi
xorl %edx, %edx
callq 0x1d541
jmp 0x1d4b4
leaq 0x31eb02(%rip), %rbx    # 0x33c010
leaq 0xed83f(%rip), %rsi     # 0x10ad54
leaq -0x130(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq -0x230(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1d541
movl $0x1, %ebx
jmp 0x1d4b4