// File: /workspace/github_c_makefile_star2_O1/ttoobne[P]bohttpd/src/core/config.c
// Function: parse_configuration
config_t* parse_configuration(char* filename) {
    config_t* config;
    char buf[CONFBUF_SIZE] = { '\0' };
    FILE* fp;
    char* p;
    char* name_st;
    char* name_ed;
    char* value_st;
    char* value_ed;
    char ch;
    int have_name;
    int have_value;
    int line;

    /* 维护状态机来解析配置文件 */
    /* 按行读取，每行格式 [<name> = <value>] [#[...]] */
    enum {
        start,
        name,
        spaces_before_equal_sign,
        equal_sign,
        spaces_before_value,
        value,
        end,
    } state;

    do {
        if ((config = (config_t*)malloc(sizeof(config_t))) == NULL) {
            log_error("config_t malloc failed.");
            return NULL;
        }

        /* 设置配置默认值 */
        config->threadpool = THREADPOOL_DEF;
        config->taskqueue = TASKQUEUE_DEF;
        memset(config->root, 0, sizeof(config->root));
        memset(config->defile, 0, sizeof(config->defile));
        strncpy(config->root, ROOT_DEF, 2);
        strncpy(config->defile, DEFILE_DEF, 10);
        config->timeout = TIMEOUT_DEF;
        config->port = PORT_DEF;

        /* 只读打开配置文件 */
        if ((fp = fopen(filename, "r")) == NULL) {
            log_error("config file open failed.");
            break;
        }

        state = start;
        line = 0;
        
        while (fgets(buf, sizeof(buf), fp) != NULL) {
            line ++ ;

            for (p = buf; *p != '\0'; ++ p) {
                ch = *p;

                switch (state) {
                case start:
                    have_name = 0;
                    have_value = 0;

                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    if (ch == '#' || ch == '\n') {
                        state = end;
                        p -- ;
                        break;
                    }

                    if (!isalpha(ch) && !isdigit(ch)) {
                        if (!iscntrl(ch)) {
                            log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                        } else {
                            log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                        }
                        state = end;
                        p -- ;
                        break;
                    }

                    name_st = p;
                    state = name;

                    break;
                
                case name:
                    if (ch == ' ' || ch == '\t') {
                        name_ed = p - 1;
                        have_name = 1;
                        state = spaces_before_equal_sign;
                        break;
                    }

                    if (ch == '=') {
                        name_ed = p - 1;
                        have_name = 1;
                        state = spaces_before_value;
                        break;
                    }

                    if (!isalpha(ch) && !isdigit(ch)) {
                        if (!iscntrl(ch)) {
                            log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                        } else {
                            log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                        }
                        state = end;
                        p -- ;
                        break;
                    }

                    break;

                case spaces_before_equal_sign:
                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    if (ch == '=') {
                        state = spaces_before_value;
                        break;
                    }
                    
                    if (!iscntrl(ch)) {
                        log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                    } else {
                        log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                    }
                    state = end;
                    p -- ;
                    break;

                case spaces_before_value:
                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    value_st = p;
                    state = value;

                    break;

                case value:
                    if (ch == ' ' || ch == '\t' || ch == '#' || ch == '\n') {
                        value_ed = p - 1;
                        have_value = 1;
                        state = end;
                        p -- ;
                        break;
                    }

                    break;

                case end:
                    *(p + 1) = '\0';
                    state = start;

                    if (have_name != have_value) {
                        log_warn("line %d in configuration file: unrecognized syntax .", line);
                        break;
                    }

                    if (have_name) {
                        if (check_name_value(config, name_st, name_ed, value_st, value_ed) != 0) {
                            log_warn("line %d in configuration file: unrecognized identifier or value.", line);
                            break;
                        }
                    }

                    break;

                default:
                    break;
                }
            }
        }

        fclose(fp);
        fp = NULL;

        return config;

    } while(0);
    
    if (fp != NULL) {
        fclose(fp);
        fp = NULL;
    }

    config_destroy(config);

    return NULL;
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, %rbx
leaq -0x470(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x21b0
movl $0x218, %edi            # imm = 0x218
callq 0x22d0
testq %rax, %rax
je 0x3251
movq %rax, %r14
movabsq $0x2000000040, %rax     # imm = 0x2000000040
movq %rax, (%r14)
leaq 0x8(%r14), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x21b0
movw $0x2f2e, 0x8(%r14)      # imm = 0x2F2E
movabsq $0x74682e7865646e69, %rax # imm = 0x74682E7865646E69
movq %rax, 0x108(%r14)
movw $0x6c6d, 0x110(%r14)    # imm = 0x6C6D
movq $0x3e8, 0x208(%r14) # imm = 0x3E8
movw $0x50, 0x210(%r14)
leaq 0x4ffd(%rip), %rsi      # 0x7caf
movq %rbx, %rdi
callq 0x2390
testq %rax, %rax
je 0x327b
movq %rax, %rbx
movq %r15, -0x60(%rbp)
movq %r14, -0x40(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x2210
testq %rax, %rax
je 0x3243
movq -0x40(%rbp), %rax
addq $0x108, %rax            # imm = 0x108
movq %rax, -0x58(%rbp)
leaq 0x45c0(%rip), %r14      # 0x72c0
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movq %rbx, -0x68(%rbp)
incl -0x2c(%rbp)
movb -0x470(%rbp), %al
testb %al, %al
je 0x3222
leaq -0x470(%rbp), %r13
cmpl $0x6, %r15d
ja 0x310a
movsbl %al, %ebx
movl %r15d, %ecx
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
jmpq *%rcx
movzbl %al, %eax
xorl %r12d, %r12d
cmpl $0x1f, %eax
jg 0x2ed3
cmpl $0x9, %eax
je 0x2f0a
cmpl $0xa, %eax
je 0x30fa
jmp 0x2ee1
movl $0x2, %r15d
cmpb $0x9, %al
je 0x310a
movzbl %al, %eax
cmpl $0x20, %eax
je 0x310a
cmpl $0x3d, %eax
jne 0x2f19
movl $0x4, %r15d
jmp 0x310a
movb $0x0, 0x1(%r13)
cmpl -0x30(%rbp), %r12d
jne 0x2ea1
xorl %r15d, %r15d
testl %r12d, %r12d
je 0x2fb3
movq -0x70(%rbp), %rbx
testq %rbx, %rbx
sete %al
cmpq $0x0, -0x50(%rbp)
sete %cl
orb %al, %cl
cmpq $0x0, -0x38(%rbp)
sete %al
cmpq $0x0, -0x48(%rbp)
sete %dl
orb %al, %dl
orb %cl, %dl
je 0x2fbb
xorl %r15d, %r15d
movl $0x3, %edi
leaq 0x450d(%rip), %rsi      # 0x72f8
leaq 0x4518(%rip), %rdx      # 0x730a
movl $0xbc, %ecx
leaq 0x461f(%rip), %r8       # 0x741d
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
jmp 0x2f59
cmpb $0x9, %al
je 0x2e27
movzbl %al, %eax
cmpl $0x3d, %eax
je 0x2f4b
cmpl $0x20, %eax
jne 0x2f64
leaq -0x1(%r13), %rax
movq %rax, -0x50(%rbp)
movl $0x2, %r15d
jmp 0x2f59
movl $0x4, %r15d
cmpb $0x9, %al
je 0x310a
movzbl %al, %eax
cmpl $0x20, %eax
je 0x310a
movl $0x5, %r15d
movq %r13, -0x38(%rbp)
jmp 0x310a
movl $0x5, %r15d
cmpb $0x23, %al
ja 0x310a
movzbl %al, %eax
movabsq $0x900000600, %rcx      # imm = 0x900000600
btq %rax, %rcx
jae 0x310a
decq %r13
movl $0x6, %r15d
movl $0x1, -0x30(%rbp)
movq %r13, -0x48(%rbp)
jmp 0x310a
xorl %r15d, %r15d
movl $0x3, %edi
leaq 0x4448(%rip), %rsi      # 0x72f8
leaq 0x4453(%rip), %rdx      # 0x730a
movl $0xb6, %ecx
leaq 0x4525(%rip), %r8       # 0x73e8
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
jmp 0x310a
cmpl $0x20, %eax
je 0x2f0a
cmpl $0x23, %eax
je 0x30fa
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
movzwl (%rax,%rcx,2), %eax
movl $0x1, %r15d
testl $0xc00, %eax            # imm = 0xC00
je 0x306d
movq %r13, -0x70(%rbp)
jmp 0x3103
movl $0x0, -0x30(%rbp)
xorl %r15d, %r15d
jmp 0x310a
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
testb $0x2, (%rax,%rcx,2)
jne 0x3050
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x43bb(%rip), %rsi      # 0x72f8
leaq 0x43c6(%rip), %rdx      # 0x730a
movl $0x94, %ecx
jmp 0x2fa7
leaq -0x1(%r13), %rax
movq %rax, -0x50(%rbp)
movl $0x4, %r15d
movl $0x1, %r12d
jmp 0x310a
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
movzwl (%rax,%rcx,2), %eax
movl $0x1, %r15d
testl $0xc00, %eax            # imm = 0xC00
jne 0x310a
testb $0x2, %al
jne 0x3095
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x435d(%rip), %rsi      # 0x72f8
leaq 0x4368(%rip), %rdx      # 0x730a
movl $0x7e, %ecx
leaq 0x43ac(%rip), %r8       # 0x735a
jmp 0x30b7
xorl %r12d, %r12d
jmp 0x310a
xorl %eax, %eax
movq -0x50(%rbp), %rcx
movb %al, 0x1(%rcx)
movq -0x48(%rbp), %rdx
movb %al, 0x1(%rdx)
movq %rcx, %rax
subq %rbx, %rax
addq $-0x3, %rax
cmpq $0x6, %rax
ja 0x2ddc
leaq 0x42f6(%rip), %rcx      # 0x72dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %edx
leaq 0x4463(%rip), %rdi      # 0x745e
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
je 0x320b
movl $0x4, %edx
leaq 0x444c(%rip), %rdi      # 0x7463
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2ddc
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x32d2
cmpq $0xffff, %rax           # imm = 0xFFFF
ja 0x2ddc
movq -0x40(%rbp), %rcx
movw %ax, 0x210(%rcx)
jmp 0x2f59
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x4299(%rip), %rsi      # 0x72f8
leaq 0x42a4(%rip), %rdx      # 0x730a
movl $0x96, %ecx
jmp 0x30b0
testb $0x2, %al
jne 0x30cd
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x4278(%rip), %rsi      # 0x72f8
leaq 0x4283(%rip), %rdx      # 0x730a
movl $0x5f, %ecx
leaq 0x42c7(%rip), %r8       # 0x735a
jmp 0x30ef
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x4254(%rip), %rsi      # 0x72f8
leaq 0x425f(%rip), %rdx      # 0x730a
movl $0x80, %ecx
leaq 0x42e9(%rip), %r8       # 0x73a0
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
decq %r13
movl $0x6, %r15d
jmp 0x310a
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x421c(%rip), %rsi      # 0x72f8
leaq 0x4227(%rip), %rdx      # 0x730a
movl $0x61, %ecx
leaq 0x42b1(%rip), %r8       # 0x73a0
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
decq %r13
movl $0x6, %r15d
movl $0x0, -0x30(%rbp)
movb 0x1(%r13), %al
incq %r13
testb %al, %al
jne 0x2d26
jmp 0x3222
movl $0x7, %edx
leaq 0x4345(%rip), %rdi      # 0x746f
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2ddc
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x32d2
testq %rax, %rax
js 0x2ddc
movq -0x40(%rbp), %rcx
movq %rax, 0x208(%rcx)
jmp 0x2f59
movl $0x6, %edx
leaq 0x42fc(%rip), %rdi      # 0x7468
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2ddc
movl $0x100, %edx            # imm = 0x100
movq -0x58(%rbp), %rdi
jmp 0x3214
movl $0x9, %edx
leaq 0x42e1(%rip), %rdi      # 0x7477
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2ddc
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x32d2
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2ddc
movq -0x40(%rbp), %rcx
movl %eax, 0x4(%rcx)
jmp 0x2f59
movl $0xa, %edx
leaq 0x42aa(%rip), %rdi      # 0x7481
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2ddc
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x32d2
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2ddc
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x2f59
movl $0x100, %edx            # imm = 0x100
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2060
jmp 0x2f59
leaq -0x470(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
movq -0x68(%rbp), %rbx
movq %rbx, %rdx
callq 0x2210
testq %rax, %rax
jne 0x2d0e
movq %rbx, %rdi
callq 0x2120
movq -0x40(%rbp), %rbx
jmp 0x32ab
leaq 0x40a0(%rip), %rsi      # 0x72f8
leaq 0x40ab(%rip), %rdx      # 0x730a
leaq 0x40b8(%rip), %r8       # 0x731e
xorl %ebx, %ebx
movl $0x4, %edi
movl $0x31, %ecx
xorl %eax, %eax
callq 0x252a
jmp 0x32ab
leaq 0x4076(%rip), %rsi      # 0x72f8
leaq 0x4081(%rip), %rdx      # 0x730a
leaq 0x40b1(%rip), %r8       # 0x7341
movl $0x4, %edi
movl $0x41, %ecx
xorl %eax, %eax
callq 0x252a
movq %r14, %rdi
callq 0x2030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq