// File: 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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1ec17
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x1ebf0
movl $0x0, -0x16c(%rbp)
xorl %eax, %eax
movq 0x68b6b(%rip), %rcx     # 0x86f60
cmpl 0x1b6b4(%rcx), %eax
jge 0x1e428
callq 0x11950
movq -0x180(%rbp), %rdi
movl %eax, %ecx
leaq 0x5b6c5(%rip), %rsi     # 0x79ad7
xorl %edx, %edx
leaq 0x5b6db(%rip), %r8      # 0x79af6
movl $0x4c2, %r9d            # imm = 0x4C2
movb $0x0, %al
callq 0x12050
jmp 0x1e42a
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x1e51f
jmp 0x1e449
leaq 0x5b6b8(%rip), %rdi     # 0x79b08
callq 0x10380
movq -0x180(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x68af3(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x68ae2(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x68ad4(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5b686(%rip), %rsi     # 0x79b27
movl $0x50, %edx
callq 0x10840
movq 0x68aae(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x68aa0(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x68a88(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x68a78(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x68a6b(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x68a5d(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x1ec25
jmp 0x1e521
movq -0x18(%rbp), %rax
movsbl (%rax), %edi
callq 0x10f80
cmpl $0x0, %eax
je 0x1e63c
jmp 0x1e538
xorl %eax, %eax
movq 0x68a1f(%rip), %rcx     # 0x86f60
cmpl 0x1b6b4(%rcx), %eax
jge 0x1e574
callq 0x11950
movq -0x180(%rbp), %rdi
movl %eax, %ecx
leaq 0x5b600(%rip), %rsi     # 0x79b5e
xorl %edx, %edx
leaq 0x5b58f(%rip), %r8      # 0x79af6
movl $0x4ca, %r9d            # imm = 0x4CA
movb $0x0, %al
callq 0x12050
jmp 0x1e576
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %edi
callq 0x10f80
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x181(%rbp)
je 0x1e5a8
cmpl $0xff, -0x28(%rbp)
setl %al
movb %al, -0x181(%rbp)
movb -0x181(%rbp), %al
testb $0x1, %al
jne 0x1e5b4
jmp 0x1e5db
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %cl
movslq -0x28(%rbp), %rax
movb %cl, -0x130(%rbp,%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1e57d
movslq -0x28(%rbp), %rax
movb $0x0, -0x130(%rbp,%rax)
leaq -0x130(%rbp), %rdi
callq 0x11b00
movl %eax, -0x134(%rbp)
cmpl $0x0, -0x134(%rbp)
jl 0x1e637
cmpl $0x1526, -0x134(%rbp)   # imm = 0x1526
jge 0x1e637
movslq -0x134(%rbp), %rcx
movq 0x68944(%rip), %rax     # 0x86f60
movsd 0x17c0(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x140(%rbp)
movl $0x1, -0x16c(%rbp)
jmp 0x1eac2
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3c, %eax
jne 0x1e97f
jmp 0x1e64e
xorl %eax, %eax
movq 0x68909(%rip), %rcx     # 0x86f60
cmpl 0x1b6b4(%rcx), %eax
jge 0x1e68a
callq 0x11950
movq -0x180(%rbp), %rdi
movl %eax, %ecx
leaq 0x5b50f(%rip), %rsi     # 0x79b83
xorl %edx, %edx
leaq 0x5b479(%rip), %r8      # 0x79af6
movl $0x4da, %r9d            # imm = 0x4DA
movb $0x0, %al
callq 0x12050
jmp 0x1e68c
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x1e781
jmp 0x1e6ab
leaq 0x5b456(%rip), %rdi     # 0x79b08
callq 0x10380
movq -0x180(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x68891(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x68880(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x68872(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5b424(%rip), %rsi     # 0x79b27
movl $0x50, %edx
callq 0x10840
movq 0x6884c(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x6883e(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x68826(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x68816(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x68809(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x687fb(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x1ec25
jmp 0x1e783
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %edx
xorl %eax, %eax
movl $0x29, %ecx
cmpl %edx, %ecx
movb %al, -0x182(%rbp)
je 0x1e7c7
xorl %eax, %eax
cmpl $0xff, -0x28(%rbp)
movb %al, -0x182(%rbp)
jge 0x1e7c7
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
setne %al
movb %al, -0x182(%rbp)
movb -0x182(%rbp), %al
testb $0x1, %al
jne 0x1e7d3
jmp 0x1e842
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
movl $0x3e, %eax
cmpl %ecx, %eax
jne 0x1e7e5
jmp 0x1e842
movq -0x18(%rbp), %rax
movsbl (%rax), %edi
callq 0x10120
cmpl $0x0, %eax
je 0x1e804
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1e78a
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movsbl (%rax), %eax
movl %eax, -0x170(%rbp)
movl -0x170(%rbp), %edi
callq 0x11910
movb %al, %cl
movslq -0x28(%rbp), %rax
movb %cl, -0x130(%rbp,%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1e78a
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
movl $0x3e, %eax
cmpl %ecx, %eax
je 0x1e92e
jmp 0x1e858
leaq 0x5b2a9(%rip), %rdi     # 0x79b08
callq 0x10380
movq -0x180(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x686e4(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x686d3(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x686c5(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5b277(%rip), %rsi     # 0x79b27
movl $0x50, %edx
callq 0x10840
movq 0x6869f(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x68691(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x68679(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x68669(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x6865c(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x6864e(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x1ec25
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x180(%rbp), %rdi
movslq -0x28(%rbp), %rax
movb $0x0, -0x130(%rbp,%rax)
leaq -0x130(%rbp), %rsi
leaq -0x138(%rbp), %rdx
leaq -0x140(%rbp), %rcx
callq 0x10830
cmpl $0x0, -0x138(%rbp)
je 0x1e97a
movl $0x1, -0x16c(%rbp)
jmp 0x1eac0
jmp 0x1e981
xorl %eax, %eax
movq 0x685d6(%rip), %rcx     # 0x86f60
cmpl 0x1b6b4(%rcx), %eax
jge 0x1e9bd
callq 0x11950
movq -0x180(%rbp), %rdi
movl %eax, %ecx
leaq 0x5b22a(%rip), %rsi     # 0x79bd1
xorl %edx, %edx
leaq 0x5b146(%rip), %r8      # 0x79af6
movl $0x50b, %r9d            # imm = 0x50B
movb $0x0, %al
callq 0x12050
jmp 0x1e9bf
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x23, (%rax)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x1eab9
jmp 0x1e9e3
leaq 0x5b11e(%rip), %rdi     # 0x79b08
callq 0x10380
movq -0x180(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x68559(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x68548(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x6853a(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5b0ec(%rip), %rsi     # 0x79b27
movl $0x50, %edx
callq 0x10840
movq 0x68514(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x68506(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x684ee(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x684de(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x684d1(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x684c3(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x1ec25
jmp 0x1eabb
jmp 0x1e3c5
jmp 0x1eac2
cmpl $0x0, -0x16c(%rbp)
je 0x1eb40
leaq -0x160(%rbp), %rdi
movsd -0x140(%rbp), %xmm0
movl $0x1e, %esi
leaq 0x613dd(%rip), %rdx     # 0x7fec3
movb $0x1, %al
callq 0x11c00
movl %eax, -0x174(%rbp)
movb $0x1, %al
cmpl $0x1e, -0x174(%rbp)
movb %al, -0x183(%rbp)
jge 0x1eb14
cmpl $0x0, -0x174(%rbp)
setl %al
movb %al, -0x183(%rbp)
movb -0x183(%rbp), %al
andb $0x1, %al
movb %al, -0x175(%rbp)
testb $0x1, -0x175(%rbp)
je 0x1eb3e
leaq -0x160(%rbp), %rdi
leaq 0x5b0c4(%rip), %rsi     # 0x79bfd
callq 0x11080
jmp 0x1eb53
leaq -0x160(%rbp), %rdi
leaq 0x5b0af(%rip), %rsi     # 0x79bfd
callq 0x11080
jmp 0x1eb55
xorl %eax, %eax
movq 0x68402(%rip), %rcx     # 0x86f60
cmpl 0x1b6b4(%rcx), %eax
jge 0x1eba8
callq 0x11950
movq -0x180(%rbp), %rdi
movl %eax, %ecx
movl -0x16c(%rbp), %r10d
leaq -0x160(%rbp), %rax
leaq 0x5b07b(%rip), %rsi     # 0x79c04
xorl %edx, %edx
leaq 0x5af64(%rip), %r8      # 0x79af6
movl $0x521, %r9d            # imm = 0x521
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x12050
jmp 0x1ebaa
leaq -0x160(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1ebee
movq -0x168(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x168(%rbp)
movb (%rax), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
jmp 0x1ebb8
jmp 0x1ec12
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
jmp 0x1e3c5
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nopw %cs:(%rax,%rax)