Beispiel #1
0
//****************************************************************************
static int print (char **out, int *varg)
{
    int post_decimal ;
    int width, pad ;
    unsigned dec_width = 6 ;
    int pc = 0;
    char *format = (char *) (*varg++);
    char scr[2];
    use_leading_plus = 0 ;  //  start out with this clear
    for (; *format != 0; ++format) {
        if (*format == '%') {
            dec_width = 6 ;
            ++format;
            width = pad = 0;
            if (*format == '\0')
                break;
            if (*format == '%')
                goto out_lbl;
            if (*format == '-') {
                ++format;
                pad = PAD_RIGHT;
            }
            if (*format == '+') {
                ++format;
                use_leading_plus = 1 ;
            }
            while (*format == '0') {
                ++format;
                pad |= PAD_ZERO;
            }
            post_decimal = 0 ;
            if (*format == '.'  ||
                    (*format >= '0' &&  *format <= '9')) {

                while (1) {
                    if (*format == '.') {
                        post_decimal = 1 ;
                        dec_width = 0 ;
                        format++ ;
                    } else if ((*format >= '0' &&  *format <= '9')) {
                        if (post_decimal) {
                            dec_width *= 10;
                            dec_width += *format - '0';
                        } else {
                            width *= 10;
                            width += *format - '0';
                        }
                        format++ ;
                    } else {
                        break;
                    }
                }
            }
            if (*format == 'l')
                ++format;
            switch (*format) {
            case 's':
            {
                // char *s = *((char **) varg++);   //lint !e740
                char *s = (char *) *varg++ ;  //lint !e740 !e826  convert to double pointer
                pc += prints (out, s ? s : "(null)", width, pad);
                use_leading_plus = 0 ;  //  reset this flag after printing one value
            }
            break;
            case 'd':
                pc += printi (out, *varg++, 10, 1, width, pad, 'a');
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;
            case 'x':
                pc += printi (out, *varg++, 16, 0, width, pad, 'a');
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;
            case 'X':
                pc += printi (out, *varg++, 16, 0, width, pad, 'A');
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;
            case 'u':
                pc += printi (out, *varg++, 10, 0, width, pad, 'a');
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;
            case 'c':
                /* char are converted to int then pushed on the stack */
                scr[0] = *varg++;
                scr[1] = '\0';
                pc += prints (out, scr, width, pad);
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;

            case 'f':
            {
                // http://wiki.debian.org/ArmEabiPort#Structpackingandalignment
                // Stack alignment
                //
                // The ARM EABI requires 8-byte stack alignment at public function entry points,
                // compared to the previous 4-byte alignment.
#ifdef USE_NEWLIB
                char *cptr = (char *) varg ;  //lint !e740 !e826  convert to double pointer
                uint caddr = (uint) cptr ;
                if ((caddr & 0xF) != 0) {
                    cptr += 4 ;
                }
                double *dblptr = (double *) cptr ;  //lint !e740 !e826  convert to double pointer
#else
                double *dblptr = (double *) varg ;  //lint !e740 !e826  convert to double pointer
#endif
                double dbl = *dblptr++ ;   //  increment double pointer
                varg = (int *) dblptr ;    //lint !e740  copy updated pointer back to base pointer
                char bfr[81] ;
                // unsigned slen =
                dbl2stri(bfr, dbl, dec_width) ;
                // stuff_talkf("[%s], width=%u, dec_width=%u\n", bfr, width, dec_width) ;
                pc += prints (out, bfr, width, pad);
                use_leading_plus = 0 ;  //  reset this flag after printing one value
            }
            break;

            default:
                printchar (out, '%');
                printchar (out, *format);
                use_leading_plus = 0 ;  //  reset this flag after printing one value
                break;
            }
        } else
            // if (*format == '\\') {
            //
            // } else
        {
out_lbl:
            printchar (out, *format);
            ++pc;
        }
    }  //  for each char in format string
    if (out) //lint !e850
        **out = '\0';
    return pc;
}
Beispiel #2
0
//****************************************************************************
static int print (char **out, int *varg)
{
   int post_decimal ;
   int width, pad ;
   unsigned dec_width = 6 ;
   int pc = 0;
   char *format = (char *) (*varg++);
	char scr[2];
	for (; *format != 0; ++format) {
		if (*format == '%') {
			++format;
			width = pad = 0;
			if (*format == '\0')
				break;
			if (*format == '%')
				goto out;
			if (*format == '-') {
				++format;
				pad = PAD_RIGHT;
			}
			while (*format == '0') {
				++format;
				pad |= PAD_ZERO;
			}
         post_decimal = 0 ;
         if (*format == '.'  ||
            (*format >= '0' &&  *format <= '9')) {

            while (1) {
               if (*format == '.') {
                  post_decimal = 1 ;
                  dec_width = 0 ;
                  format++ ;
               } else if ((*format >= '0' &&  *format <= '9')) {
                  if (post_decimal) {
                     dec_width *= 10;
                     dec_width += *format - '0';
                  } else {
                     width *= 10;
                     width += *format - '0';
                  }
                  format++ ;
               } else {
                  break;
               }
            }
         }
	 int ll = 0;
	 if (*format == 'l') {
            ++format;
	    if (*format == 'l') {
	       ++format;
	       ll = 1;
	    }
	 }
         switch (*format) {
         case 's':
            {
            char *s = *((char **) varg++);   //lint !e740
            // printf("[%s] w=%u\n", s, width) ;
            pc += prints (out, s ? s : "(null)", width, pad);
            }
            break;
         case 'd':
	    if (ll) {
	       if (((unsigned int)varg & 7) != 0) varg++; // MIPS requires 8 byte aligned long longs
	       long long *llptr = (long long *) varg;
	       long long llval = *llptr++;
	       varg = (int *) llptr;
	       pc += printl (out, llval, 10, 1, width, pad, 'a');
	    } else {
	       pc += printi (out, *varg++, 10, 1, width, pad, 'a');
	    }
            break;
         case 'x':
	    if (ll) {
	       if (((unsigned int)varg & 7) != 0) varg++; // MIPS requires 8 byte aligned long longs
	       long long *llptr = (long long *) varg;
	       long long llval = *llptr++;
	       varg = (int *) llptr;
	       pc += printl (out, llval, 16, 0, width, pad, 'a');
	    } else {
	       pc += printi (out, *varg++, 16, 0, width, pad, 'a');
	    }
            break;
         case 'p':
	    pc += prints (out, "0x", 0, 0);
            pc += printi (out, *varg++, 16, 0, width ? width : 8, pad ? pad : PAD_ZERO , 'a');
            break;
         case 'X':
	    if (ll) {
	       if (((unsigned int)varg & 7) != 0) varg++; // MIPS requires 8 byte aligned long longs
	       long long *llptr = (long long *) varg;
	       long long llval = *llptr++;
	       varg = (int *) llptr;
	       pc += printl (out, llval, 16, 0, width, pad, 'A');
	    } else {
	       pc += printi (out, *varg++, 16, 0, width, pad, 'A');
	    }
            break;
         case 'u':
	    if (ll) {
	       if (((unsigned int)varg & 7) != 0) varg++; // MIPS requires 8 byte aligned long longs
	       long long *llptr = (long long *) varg;
	       long long llval = *llptr++;
	       varg = (int *) llptr;
	       pc += printl (out, llval, 10, 0, width, pad, 'a');
	    } else {
	       pc += printi (out, *varg++, 10, 0, width, pad, 'a');
	    }
            break;
         case 'c':
            /* char are converted to int then pushed on the stack */
            scr[0] = *varg++;
            scr[1] = '\0';
            pc += prints (out, scr, width, pad);
            break;

         case 'f':
            {
	    //if (((unsigned int)varg & 7) != 0) varg++; // MIPS requires 8 byte aligned doubles
            double *dblptr = (double *) varg ;  //lint !e740 !e826  convert to double pointer
            double dbl = *dblptr++ ;   //  increment double pointer
            varg = (int *) dblptr ;    //lint !e740  copy updated pointer back to base pointer
            char bfr[81] ;
            // unsigned slen =
            dbl2stri(bfr, dbl, dec_width) ;
            // stuff_talkf("[%s], width=%u, dec_width=%u\n", bfr, width, dec_width) ;
            pc += prints (out, bfr, width, pad);
            }
            break;

         default:
            printchar (out, '%');
            printchar (out, *format);
            break;
         }
		}
		else {
		 out:
			printchar (out, *format);
			++pc;
		}
	}
	if (out)
		**out = '\0';
	return pc;
}