// File: /workspace/github_cpp_makefile_star2_O1/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 0x1d45b
movl %esi, %ebx
movq %rdi, %r12
movzbl %bl, %r15d
movzbl %dl, %r14d
leaq 0xeda7e(%rip), %rdi     # 0x10ac49
leaq 0x153abe(%rip), %r8     # 0x170c90
movq %r12, %rsi
movl %r15d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x9030
movb $0x0, -0x130(%rbp)
testb %r14b, %r14b
je 0x1d201
leaq 0x153a9b(%rip), %rsi    # 0x170c90
leaq -0x130(%rbp), %rdi
callq 0x96c0
leaq -0x130(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x96c0
leaq -0x12f(%rbp), %rdi
movl $0x2e, %esi
callq 0x9100
testq %rax, %rax
je 0x1d236
cmpb $0x2, %bl
je 0x1d25c
cmpl $0x1, %r15d
je 0x1d250
jmp 0x1d266
leaq -0x130(%rbp), %rdi
callq 0x9120
addq %rbp, %rax
addq $-0x130, %rax           # imm = 0xFED0
cmpb $0x1, %bl
jne 0x1d25c
movb $0x0, 0x4(%rax)
movl $0x6766632e, (%rax)     # imm = 0x6766632E
jmp 0x1d266
movb $0x0, 0x4(%rax)
movl $0x6c61632e, (%rax)     # imm = 0x6C61632E
testb %bl, %bl
je 0x1d2e6
movq %r14, %rdi
movl $0x2e, %esi
callq 0x9100
movq %rax, %r15
cmpq %r14, %rax
jb 0x1d28d
cmpb $0x2f, (%r15)
je 0x1d28d
decq %r15
cmpq %r14, %r15
jae 0x1d27f
incq %r15
leaq 0xed9ed(%rip), %rdi     # 0x10ac84
xorl %r14d, %r14d
leaq -0x130(%rbp), %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x9030
leaq 0x1039fb(%rip), %rsi    # 0x120cad
movq %r15, %rdi
callq 0x9a4d
testq %rax, %rax
je 0x1d2e9
movq %rax, %r12
cmpb $0x2, %bl
leaq 0xed9e1(%rip), %rax     # 0x10acad
leaq 0xed9f4(%rip), %rdi     # 0x10acc7
cmoveq %rax, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x9030
movq %r12, %r14
jmp 0x1d2e9
xorl %r14d, %r14d
testq %r14, %r14
jne 0x1d336
leaq 0x1039b8(%rip), %rsi    # 0x120cad
leaq -0x130(%rbp), %r15
movq %r15, %rdi
callq 0x9a4d
movq %rax, %r14
leaq 0xed9d1(%rip), %rdi     # 0x10acdf
movq %r15, %rsi
xorl %eax, %eax
callq 0x9030
testq %r14, %r14
je 0x1d473
leaq 0xed9f6(%rip), %rdi     # 0x10ad1e
leaq -0x130(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
cmpb $0x2, %bl
jne 0x1d359
movq %r14, %rdi
callq 0x9370
leaq -0x130(%rbp), %rdi
movl $0x1, %esi
callq 0xda774
jmp 0x1d45b
leaq -0x230(%rbp), %rdi
movl $0xfb, %esi
movq %r14, %rdx
callq 0x9600
testq %rax, %rax
je 0x1d453
leaq -0x22f(%rbp), %rbx
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 0x1d3eb
btl %eax, %r12d
jae 0x1d402
movq %r15, %rdi
callq 0x9120
decl %eax
js 0x1d3db
movl %eax, %eax
incq %rax
movzbl -0x231(%rbp,%rax), %ecx
cmpq $0x20, %rcx
ja 0x1d3db
btq %rcx, %r13
jae 0x1d3db
movb $0x0, -0x231(%rbp,%rax)
decq %rax
jg 0x1d3ba
movq %r15, %rdi
callq 0x3251d
testl %eax, %eax
jne 0x1d4b2
movq %r15, %rdi
movl $0xfb, %esi
movq %r14, %rdx
callq 0x9600
testq %rax, %rax
jne 0x1d394
jmp 0x1d453
cmpl $0x1c, %eax
jne 0x1d3eb
cmpq $0x0, 0x220fa1(%rip)    # 0x23e3b0
jne 0x1d42b
leaq 0xed91f(%rip), %rdi     # 0x10ad37
leaq 0x103ebc(%rip), %rsi    # 0x1212db
callq 0x9a4d
movq %rax, 0x220f85(%rip)    # 0x23e3b0
movq 0x220f7e(%rip), %rsi    # 0x23e3b0
testq %rsi, %rsi
je 0x1d3eb
cmpb $0x0, -0x230(%rbp)
je 0x1d3eb
cmpb $0x0, -0x22f(%rbp)
je 0x1d3eb
movq %rbx, %rdi
callq 0x9400
jmp 0x1d3eb
movq %r14, %rdi
callq 0x9370
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r14d
testb %bl, %bl
jne 0x1d45e
leaq 0xed87f(%rip), %rsi     # 0x10ad03
leaq -0x390(%rbp), %rbx
leaq -0x130(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq -0x230(%rbp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1d4ed
jmp 0x1d45e
leaq 0x31eb57(%rip), %rbx    # 0x33c010
leaq 0xed884(%rip), %rsi     # 0x10ad44
leaq -0x130(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq -0x230(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1d4ed
movl $0x1, %r14d
jmp 0x1d45e