// File: /workspace/github_c_makefile_star2_O2/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 $0x468, %rsp            # imm = 0x468
movq %rdi, %rbx
leaq -0x480(%rbp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
callq 0x21b0
movl $0x218, %edi            # imm = 0x218
callq 0x22d0
testq %rax, %rax
je 0x300f
movq %rax, %r14
movabsq $0x2000000040, %rax     # imm = 0x2000000040
movq %rax, (%r14)
leaq 0x8(%r14), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rdi, -0x70(%rbp)
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 0x422d(%rip), %rsi      # 0x6caf
movq %rbx, %rdi
callq 0x2390
movq %rax, -0x60(%rbp)
testq %rax, %rax
je 0x3035
movq %r14, -0x50(%rbp)
addq $0x108, %r14            # imm = 0x108
movq %r14, -0x68(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r13d, %r13d
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq -0x60(%rbp), %rdx
callq 0x2210
testq %rax, %rax
je 0x3000
incl -0x2c(%rbp)
movq %r15, %rsi
leaq 0x37e9(%rip), %rcx      # 0x62c0
movl %ebx, %edi
movsbl (%rsi), %ebx
movslq %ebx, %r15
testl %ebx, %ebx
je 0x2ff2
cmpl $0x6, %r13d
ja 0x2c0a
movl %r13d, %eax
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %r12d, %r12d
cmpb $0x9, %bl
je 0x2ccf
movzbl %bl, %eax
cmpl $0x23, %eax
je 0x2b23
cmpl $0x20, %eax
je 0x2ccf
cmpl $0xa, %eax
jne 0x2d3b
decq %rsi
xorl %r14d, %r14d
jmp 0x2c81
cmpb $0x9, %r15b
je 0x2c85
movzbl %bl, %eax
cmpl $0x20, %eax
je 0x2c85
cmpl $0x3d, %eax
je 0x2c51
movl %edi, %r13d
movq %rsi, %r12
callq 0x2490
movq (%rax), %rax
testb $0x2, (%rax,%r15,2)
jne 0x2e02
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x3785(%rip), %rsi      # 0x62f8
leaq 0x3790(%rip), %rdx      # 0x630a
movl $0x94, %ecx
leaq 0x37d4(%rip), %r8       # 0x635a
jmp 0x2e22
movb $0x0, 0x1(%rsi)
xorl %r13d, %r13d
cmpl %edi, %r14d
jne 0x2c8f
testl %r14d, %r14d
je 0x2d66
movq %rsi, -0x38(%rbp)
movq -0x78(%rbp), %r15
testq %r15, %r15
sete %al
cmpq $0x0, -0x58(%rbp)
sete %cl
orb %al, %cl
cmpq $0x0, -0x40(%rbp)
sete %al
cmpq $0x0, -0x48(%rbp)
sete %dl
orb %al, %dl
orb %cl, %dl
je 0x2d6e
xorl %r13d, %r13d
pushq $0x3
popq %rdi
leaq 0x3715(%rip), %rsi      # 0x62f8
leaq 0x3720(%rip), %rdx      # 0x630a
movl $0xbc, %ecx
leaq 0x3827(%rip), %r8       # 0x641d
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x4c60
pushq $0x1
popq %r12
jmp 0x2d2e
movl %r14d, %r12d
movl %edi, %r14d
jmp 0x2cf8
cmpb $0x9, %r15b
je 0x2c31
movzbl %r15b, %eax
cmpl $0x3d, %eax
je 0x2ce5
cmpl $0x20, %eax
jne 0x2d06
leaq -0x1(%rsi), %rax
movq %rax, -0x58(%rbp)
pushq $0x1
popq %r12
jmp 0x2c88
cmpb $0x9, %r15b
je 0x2c51
movzbl %bl, %eax
cmpl $0x20, %eax
jne 0x2cd7
movl %r14d, %r12d
jmp 0x2cf1
cmpb $0x23, %r15b
ja 0x2cdb
movzbl %r15b, %eax
movabsq $0x900000600, %rdx      # imm = 0x900000600
btq %rax, %rdx
jae 0x2cdb
decq %rsi
movq %rsi, -0x48(%rbp)
movl %r14d, %r12d
pushq $0x1
popq %r14
pushq $0x6
jmp 0x2cf6
movl %r14d, %r12d
movl %edi, %r14d
pushq $0x2
jmp 0x2cf6
movl %edi, %r15d
pushq $0x3
popq %rdi
movq %rsi, %rbx
leaq 0x3659(%rip), %rsi      # 0x62f8
leaq 0x3664(%rip), %rdx      # 0x630a
movl $0xb6, %ecx
leaq 0x3736(%rip), %r8       # 0x63e8
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x4c60
movq %rbx, %rsi
leaq 0x35f9(%rip), %rcx      # 0x62c0
movl %r14d, %r12d
movl %r15d, %r14d
jmp 0x2cf8
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0x2cf8
movq %rsi, -0x40(%rbp)
movl %r14d, %r12d
movl %edi, %r14d
pushq $0x5
jmp 0x2cf6
leaq -0x1(%rsi), %rax
movq %rax, -0x58(%rbp)
pushq $0x1
popq %r12
movl %edi, %r14d
pushq $0x4
popq %r13
movl %r14d, %edi
incq %rsi
movl %r12d, %r14d
jmp 0x2ad9
movl %edi, %r13d
movq %rsi, -0x38(%rbp)
callq 0x2490
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
testw $0xc00, %ax             # imm = 0xC00
je 0x2e32
movl %r14d, %r12d
movl %r13d, %r14d
pushq $0x1
popq %r13
leaq 0x358b(%rip), %rcx      # 0x62c0
movq -0x38(%rbp), %rsi
jmp 0x2cf8
movq %rsi, %r14
callq 0x2490
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
testw $0xc00, %ax             # imm = 0xC00
je 0x2e56
movq %r14, %rsi
movq %r14, -0x78(%rbp)
xorl %r14d, %r14d
pushq $0x1
jmp 0x2eb4
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x2cf8
xorl %eax, %eax
movq -0x58(%rbp), %rcx
movb %al, 0x1(%rcx)
movq -0x48(%rbp), %rdx
movb %al, 0x1(%rdx)
movq %rcx, %rax
subq %r15, %rax
addq $-0x3, %rax
cmpq $0x6, %rax
ja 0x2bd6
leaq 0x3543(%rip), %rcx      # 0x62dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x36b5(%rip), %rdi      # 0x645e
movq %r15, %rsi
pushq $0x4
popq %rbx
movq %rbx, %rdx
callq 0x2070
testl %eax, %eax
je 0x2fdb
leaq 0x369d(%rip), %rdi      # 0x6463
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2070
testl %eax, %eax
jne 0x2bd6
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3083
cmpq $0xffff, %rax           # imm = 0xFFFF
ja 0x2bd6
movq -0x50(%rbp), %rcx
movw %ax, 0x210(%rcx)
jmp 0x2c01
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x34e9(%rip), %rsi      # 0x62f8
leaq 0x34f4(%rip), %rdx      # 0x630a
movl $0x96, %ecx
leaq 0x357e(%rip), %r8       # 0x63a0
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x4c60
movq %r12, %rsi
jmp 0x2ea9
testb $0x2, %al
jne 0x2e7a
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x34b5(%rip), %rsi      # 0x62f8
leaq 0x34c0(%rip), %rdx      # 0x630a
pushq $0x7e
popq %rcx
leaq 0x3506(%rip), %r8       # 0x635a
jmp 0x2e9a
testb $0x2, %al
jne 0x2ec2
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x3491(%rip), %rsi      # 0x62f8
leaq 0x349c(%rip), %rdx      # 0x630a
pushq $0x5f
popq %rcx
leaq 0x34e2(%rip), %r8       # 0x635a
jmp 0x2ee0
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x3471(%rip), %rsi      # 0x62f8
leaq 0x347c(%rip), %rdx      # 0x630a
movl $0x80, %ecx
leaq 0x3506(%rip), %r8       # 0x63a0
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x4c60
movq -0x38(%rbp), %rsi
decq %rsi
movl %r14d, %r12d
movl %r13d, %r14d
pushq $0x6
popq %r13
leaq 0x3403(%rip), %rcx      # 0x62c0
jmp 0x2cf8
movl %ebx, (%rsp)
pushq $0x3
popq %rdi
leaq 0x3429(%rip), %rsi      # 0x62f8
leaq 0x3434(%rip), %rdx      # 0x630a
pushq $0x61
popq %rcx
leaq 0x34c0(%rip), %r8       # 0x63a0
movl -0x2c(%rbp), %r9d
xorl %eax, %eax
callq 0x4c60
movq %r14, %rsi
decq %rsi
xorl %r14d, %r14d
jmp 0x2eb2
leaq 0x3572(%rip), %rdi      # 0x646f
movq %r15, %rsi
pushq $0x7
popq %rdx
callq 0x2070
testl %eax, %eax
jne 0x2bd6
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3083
testq %rax, %rax
js 0x2bd6
movq -0x50(%rbp), %rcx
movq %rax, 0x208(%rcx)
jmp 0x2c01
leaq 0x352b(%rip), %rdi      # 0x6468
movq %r15, %rsi
pushq $0x6
popq %rdx
callq 0x2070
testl %eax, %eax
jne 0x2bd6
movl $0x100, %edx            # imm = 0x100
movq -0x68(%rbp), %rdi
jmp 0x2fe4
leaq 0x3512(%rip), %rdi      # 0x6477
movq %r15, %rsi
pushq $0x9
popq %rdx
callq 0x2070
testl %eax, %eax
jne 0x2bd6
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3083
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2bd6
movq -0x50(%rbp), %rcx
movl %eax, 0x4(%rcx)
jmp 0x2c01
leaq 0x34dd(%rip), %rdi      # 0x6481
movq %r15, %rsi
pushq $0xa
popq %rdx
callq 0x2070
testl %eax, %eax
jne 0x2bd6
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3083
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2bd6
movq -0x50(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x2c01
movl $0x100, %edx            # imm = 0x100
movq -0x70(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2060
jmp 0x2c01
movl %edi, %ebx
leaq -0x480(%rbp), %r15
jmp 0x2ab0
movq -0x60(%rbp), %rdi
callq 0x2120
movq -0x50(%rbp), %rbx
jmp 0x3061
leaq 0x32e2(%rip), %rsi      # 0x62f8
leaq 0x32ed(%rip), %rdx      # 0x630a
leaq 0x32fa(%rip), %r8       # 0x631e
xorl %ebx, %ebx
pushq $0x4
popq %rdi
pushq $0x31
popq %rcx
xorl %eax, %eax
callq 0x4c60
jmp 0x3061
leaq 0x32bc(%rip), %rsi      # 0x62f8
leaq 0x32c7(%rip), %rdx      # 0x630a
leaq 0x32f7(%rip), %r8       # 0x6341
pushq $0x4
popq %rdi
pushq $0x41
popq %rcx
xorl %eax, %eax
callq 0x4c60
movq %r14, %rdi
callq 0x2030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq