// File: /workspace/github_cpp_makefile_star2_O2/sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_param_comment(char*, char*, int)
int Interp::convert_param_comment(char *comment, char *expanded, int len)
{
    int i;
    char param[LINELEN+1];
    int paramNumber;
    int stat;
    double value;
    char valbuf[VAL_LEN]; // max double length + room
    char *v;
    int found;

    while(*comment)
    {
        if(*comment == '#')
        {
            found = 0;
            logDebug("a parameter");

            // skip over the '#'
            comment++;
            CHKS((0 == *comment), NCE_NAMED_PARAMETER_NOT_TERMINATED);

            if(isdigit(*comment))  // is this numeric param?
            {
                logDebug("numeric parameter");
                for(i=0; isdigit(*comment)&& (i<LINELEN); i++)
                {
                    param[i] = *comment++;
                }
                param[i] = 0;
                paramNumber = atoi(param);
                if((paramNumber >= 0) &&
                   (paramNumber < RS274NGC_MAX_PARAMETERS))
                {
                    value = _setup.parameters[paramNumber];
                    found = 1;
                }
            }
            else if(*comment == '<')
            {
                logDebug("name parameter");
                // this is a name parameter
                // skip over the '<'
                comment++;
                CHKS((0 == *comment), NCE_NAMED_PARAMETER_NOT_TERMINATED);

                for(i=0; (')' != *comment) &&
                        (i<LINELEN) && (0 != *comment);)
                {
                    if('>' == *comment)
                    {
                        break;     // done
                    }
                    if(isspace(*comment)) // skip space inside the param
                    {
                        comment++;
                        continue;
                    }
                    else
                    {
		        // if tolower is a macro, may need this int
		        int c = *comment++;
                        param[i] = tolower(c);
                        i++;
                    }
                }
                if('>' != *comment)
                {
                    ERS(NCE_NAMED_PARAMETER_NOT_TERMINATED);
                    logDebug("parameter not terminated");
                }
                else
                {
                    comment++;
                }

                // terminate the name
                param[i] = 0;

                // now lookup the name
                find_named_param(param, &stat, &value);
                if(stat)
                {
                    found = 1;
                }
            }
            else
            {
                // neither numeric or name
                logDebug("neither numeric nor name");
                // just store the '#'
                *expanded++ = '#';

                CHKS((*comment == 0), NCE_NAMED_PARAMETER_NOT_TERMINATED);
                continue;
            }

            // we have a parameter -- now insert it
            // we have the value
            if(found)
            {
                int n = snprintf(valbuf, VAL_LEN, "%lf", value);
                bool fail = (n >= VAL_LEN || n < 0);
                if(fail)
                    strcpy(valbuf, "######");

            }
            else
            {
                strcpy(valbuf, "######");
            }
            logDebug("found:%d value:|%s|", found, valbuf);

            v = valbuf;
            while(*v)
            {
                *expanded++ = *v++;
            }
        }
        else  // not a '#'
        {
            *expanded++ = *comment++;
        }
    }
    *expanded = 0; // the final nul
    
    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
movq 0x2c2e6(%rip), %rbx     # 0x3cf60
movb (%r13), %al
leaq 0x1(%r13), %rcx
addq $0x3, %r13
movq %r13, %r12
movq %rcx, %r13
cmpb $0x23, %al
jne 0x10d30
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x10cc4
callq 0xc0b0
movq %r15, %rdi
leaq 0x2074f(%rip), %rsi     # 0x313fb
xorl %edx, %edx
movl %eax, %ecx
leaq 0x20763(%rip), %r8      # 0x3141a
movl $0x4c2, %r9d            # imm = 0x4C2
xorl %eax, %eax
callq 0xc5e0
movsbl (%r13), %eax
testl %eax, %eax
je 0x10f4a
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
jbe 0x10d46
movl 0x1b6b4(%rbx), %ecx
cmpb $0x3c, %al
je 0x10ddd
testl %ecx, %ecx
jle 0x10d12
callq 0xc0b0
movq %r15, %rdi
leaq 0x20798(%rip), %rsi     # 0x31492
xorl %edx, %edx
movl %eax, %ecx
leaq 0x20715(%rip), %r8      # 0x3141a
movl $0x50b, %r9d            # imm = 0x50B
xorl %eax, %eax
callq 0xc5e0
movb $0x23, (%r14)
incq %r14
movb (%r13), %al
incq %r13
incq %r12
testb %al, %al
jne 0x10c8c
jmp 0x10f4a
movzbl %al, %ecx
testl %ecx, %ecx
je 0x10f90
movb %al, (%r14)
incq %r14
jmp 0x10c7a
movq %rbx, %r12
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x10d79
callq 0xc0b0
movq %r15, %rdi
leaq 0x206ea(%rip), %rsi     # 0x3144b
xorl %edx, %edx
movl %eax, %ecx
leaq 0x206ae(%rip), %r8      # 0x3141a
movl $0x4ca, %r9d            # imm = 0x4CA
xorl %eax, %eax
callq 0xc5e0
xorl %ebx, %ebx
movsbl (%r13,%rbx), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x10d9e
cmpq $0xfe, %rbx
ja 0x10d9e
movb %al, -0x160(%rbp,%rbx)
incq %rbx
jmp 0x10d7b
movb $0x0, -0x160(%rbp,%rbx)
leaq -0x160(%rbp), %rdi
callq 0xc1e0
addq %rbx, %r13
cmpl $0x1525, %eax           # imm = 0x1525
movq %r15, -0x30(%rbp)
ja 0x10e6e
movl %eax, %eax
movq %r12, %rbx
movsd 0x17c0(%r12,%rax,8), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x10eb3
testl %ecx, %ecx
jle 0x10e08
callq 0xc0b0
movq %r15, %rdi
leaq 0x20680(%rip), %rsi     # 0x31470
xorl %edx, %edx
movl %eax, %ecx
leaq 0x2061f(%rip), %r8      # 0x3141a
movl $0x4da, %r9d            # imm = 0x4DA
xorl %eax, %eax
callq 0xc5e0
movb 0x1(%r13), %al
testb %al, %al
je 0x10f4a
movq %r15, -0x30(%rbp)
xorl %r15d, %r15d
cmpb $0x29, %al
sete %cl
cmpl $0xff, %r15d
setge %dl
orb %cl, %dl
movslq %r15d, %rbx
jne 0x10e76
testb %al, %al
je 0x10fa8
movzbl %al, %ecx
cmpl $0x3e, %ecx
je 0x10e7e
movsbl %al, %r13d
movl %r13d, %edi
callq 0xb0c0
testl %eax, %eax
jne 0x10e65
movl %r13d, %edi
callq 0xc080
movb %al, -0x160(%rbp,%rbx)
incl %ebx
movl %ebx, %r15d
movb (%r12), %al
incq %r12
jmp 0x10e1b
xorl %r15d, %r15d
movq %r12, %rbx
jmp 0x10ee2
cmpb $0x3e, %al
jne 0x10fa8
movb $0x0, -0x160(%rbp,%rbx)
movq -0x30(%rbp), %rdi
leaq -0x160(%rbp), %rsi
leaq -0x34(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0xb570
cmpl $0x0, -0x34(%rbp)
je 0x10ed5
movsd -0x40(%rbp), %xmm0
movq %r12, %r13
movq 0x2c0ad(%rip), %rbx     # 0x3cf60
leaq -0x60(%rbp), %rdi
pushq $0x1e
popq %rsi
leaq 0x26069(%rip), %rdx     # 0x36f2a
movb $0x1, %al
callq 0xc280
pushq $0x1
popq %rcx
movl %ecx, %r15d
cmpl $0x1e, %eax
jae 0x10ee2
jmp 0x10ef0
xorl %r15d, %r15d
movq %r12, %r13
movq 0x2c07e(%rip), %rbx     # 0x3cf60
movl $0x232323, -0x5d(%rbp)  # imm = 0x232323
movl $0x23232323, -0x60(%rbp) # imm = 0x23232323
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x10f2b
callq 0xc0b0
movq -0x30(%rbp), %rdi
leaq 0x205bc(%rip), %rsi     # 0x314c5
xorl %edx, %edx
movl %eax, %ecx
leaq 0x20506(%rip), %r8      # 0x3141a
movl $0x521, %r9d            # imm = 0x521
xorl %eax, %eax
leaq -0x60(%rbp), %r10
pushq %r10
pushq %r15
callq 0xc5e0
popq %rax
popq %rcx
xorl %eax, %eax
movq -0x30(%rbp), %r15
movb -0x60(%rbp,%rax), %cl
testb %cl, %cl
je 0x10f42
movb %cl, (%r14,%rax)
incq %rax
jmp 0x10f31
addq %rax, %r14
jmp 0x10c7a
leaq 0x204db(%rip), %rdi     # 0x3142c
callq 0xb220
movq %r15, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
leaq 0xc6d0(%rbx), %rdi
leaq 0x1f313(%rip), %rsi     # 0x30284
pushq $0x50
popq %rdx
callq 0xb940
andw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
pushq $0x5
popq %rax
jmp 0x10f96
movb $0x0, (%r14)
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2047d(%rip), %rdi     # 0x3142c
callq 0xb220
movq -0x30(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
movq 0x2bf97(%rip), %rbx     # 0x3cf60
jmp 0x10f63
nop