This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Ellipsis use with C51 for 8051


I have some legacy code that was originally compiled with Franklin and it also compiles with my Keil compiler. The problem is that the source code does some things that make no sense and I cannot find any documentation in my Keil manuals or on the website about this topic. I want to ensure that 1) the code still operates the same using Keil vs Franklin compiler 2) I understand how it works. The manuals reference va_start,va_end macros. Can the code below be written to use those macros? How? If so, would this be a better option? Looking at the ouput src file, it appears that keil makes a undocumented library call.

I look forward to your inputs.

Thanks,

Paul Calvert
256-704-4134

Here is the code snipet:

int sscanf(char *inBfrPtr, char *fmtPtr, ...)
{
Byte scanResult = 0;
void *argPtr = &ellipsis_40; //THIS IS THE STATEMENT THAT I CAN FIND NO REFERNCE TO: ELLIPSIS_40. THERE ARE NO MACROS OR OTHER DECLARATIONS ANYWHERE IN MY CODE REFERENCING "ELLIPSIS_"
register char tmpDigit;

while(1){
if(*fmtPtr == '\0')
break;
else if(*fmtPtr == '%'){
fmtPtr++;
if(*fmtPtr == 'c'){
**(char **)argPtr = *inBfrPtr;
argPtr += 3;
fmtPtr++;
inBfrPtr++;
scanResult += 1;
continue;
}
else if(*fmtPtr == 'd'){
if(inBfrPtr[0] == '+' || inBfrPtr[0] == '-')
tmpDigit = inBfrPtr[1];
else
tmpDigit = inBfrPtr[0];
if(tmpDigit >= '0' && tmpDigit <= '9'){
**(int **)argPtr = atoi(inBfrPtr);
while(1){
inBfrPtr++;
if(inBfrPtr[0] < '0' || inBfrPtr[0] > '9')
break;
}
fmtPtr++;
argPtr += 3;
scanResult += 1;
continue;
}
else
break;
}
else if(*fmtPtr == '%'){
/* fall through to continue string comparison */
}
else{ /* unknown format specifier */
break;
}
}
if(*fmtPtr++ != *inBfrPtr++)
break;
}
return(scanResult);
}

Parents
  • Thanks for all your suggestions. Sorry about the <pre > problems with my posts.

    I tried the PREPRINT that was suggested. The resultant preprocessor file had no reference to ellipsis_40 at all. Worth a shot, but....

    Thank you Mark Odell for the last printf example you posted. I think I can figure it out from here. Did you see anything wrong with the way I implemented va_ macros? I realize I was half-*ssed doing it--in an effort to retain as much original code as possible, but it 'seemed' to work (I have yet to try on target hardware). Before this week I had never heard of or seen an ellipsis. Thanks for all your insights.....

    Paul

Reply
  • Thanks for all your suggestions. Sorry about the <pre > problems with my posts.

    I tried the PREPRINT that was suggested. The resultant preprocessor file had no reference to ellipsis_40 at all. Worth a shot, but....

    Thank you Mark Odell for the last printf example you posted. I think I can figure it out from here. Did you see anything wrong with the way I implemented va_ macros? I realize I was half-*ssed doing it--in an effort to retain as much original code as possible, but it 'seemed' to work (I have yet to try on target hardware). Before this week I had never heard of or seen an ellipsis. Thanks for all your insights.....

    Paul

Children
  • Paul,

    I did see something wrong with your use of va_* macros:

    argList[0] = va_arg(vargList, Byte);
    argPtr = &argList[0];
    
    if(*fmtPtr == 'c'){
    **(char **)argPtr = *inBfrPtr;
    
    // How you can add 3 to a void pointer is beyond me,
    // C doesn't know how to scale with void pointers.
    argPtr += 3;
    
    argPtr must be removed or I will become ill. All your funky lvalue casting not only looks weird but is just reproducing the magic of va_arg().

    You *must* pop things off the variadatic stack using va_arg() and no other means. It's really quite simple, if your format string (which you can define anyway you want) indicates that the caller has passed in two chars, a short, and a long (in that order) then you must pull them off this way in that order like this:
    char one, two;
    short three;
    long four;
    
    one   = va_arg(vargList, char);
    two   = va_arg(vargList, char);
    three = va_arg(vargList, short);
    four  = va_arg(vargList, long);
    
    va_arg() increments the variadatic stack for you and ensures that you get what you expect since you tell it the type.

    I hope this clarifies things. Your sscanf() needs a re-write, perhaps I'll give it shot in my spare time.

    - Mark

  • Paul,

    Have you ever used printf in your programs? If so, you have used a function with a variable length argument list. That's what the ...'s are.

    Jon