Remove warning during test.
[bertos.git] / bertos / mware / formatwr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005, 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \brief Basic "printf", "sprintf" and "fprintf" formatter.
36  *
37  * This module is 100% reentrant and can be adapted to user-defined routines
38  * that needs formatters with special properties like different output
39  * channels or new format specifiers.
40  *
41  * To reduce size in applications not using real numbers or long integers
42  * the formatter may be compiled to exclude certain parts.  This is
43  * controlled by giving a -D option a compilation time:
44  *
45  * \code
46  *  -D CONFIG_PRINTF=PRINTF_FULL         Full ANSI printf formatter, with some C99 extensions
47  *  -D CONFIG_PRINTF=PRINTF_NOFLOAT      Exclude support for floats
48  *  -D CONFIG_PRINTF=PRINTF_REDUCED      Simplified formatter (see below)
49  *  -D CONFIG_PRINTF=PRINTF_NOMODIFIERS  Exclude 'l', 'z' and 'h' modifiers in reduced version
50  *  -D CONFIG_PRINTF=PRINTF_DISABLED     No formatter at all
51  * \endcode
52  *
53  * Code size on AVR4 with GCC 3.4.1 (-O2):
54  * \li  PRINTF_FULL        2912byte (0xB60)
55  * \li  PRINTF_NOFLOAT     1684byte (0x694)
56  * \li  PRINTF_REDUCED      924byte (0x39C)
57  * \li  PRINTF_NOMODIFIERS  416byte (0x1A0)
58  *
59  * Code/data size in words on DSP56K with CodeWarrior 6.0:
60  * \li  PRINTF_FULL         1493/45
61  * \li  PRINTF_NOFLOAT      795/45
62  * \li  PRINTF_REDUCED      482/0
63  * \li  PRINTF_NOMODIFIERS  301/0
64  *
65  * The reduced version of formatter is suitable when program size is critical
66  * rather than formatting power.  This routine uses less than 20 bytes of
67  * stack space which makes it practical even in systems with less than 256
68  * bytes of user RAM.
69  *
70  * The only formatting specifiers supported by the reduced formatter are:
71  * \code
72  *    %% %c %s %d %o %x %X and %hd %ho %hx %hX %ld %lo %lx %lX
73  * \endcode
74  *
75  * It means that real variables are not supported as well as field
76  * width and precision arguments.
77  */
78
79
80 #include "formatwr.h"
81
82 #include "cfg/cfg_formatwr.h"  /* CONFIG_ macros */
83 #include <cfg/debug.h>         /* ASSERT */
84
85 #include <cpu/pgm.h>
86 #include <mware/hex.h>
87
88 #ifndef CONFIG_PRINTF_N_FORMATTER
89         /** Disable the arcane %n formatter. */
90         #define CONFIG_PRINTF_N_FORMATTER 0
91 #endif
92
93 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
94         /** Disable the %o formatter. */
95         #define CONFIG_PRINTF_OCTAL_FORMATTER 0
96 #endif
97
98 /* True if we must keep a count of the number of characters we print. */
99 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
100
101 #if CONFIG_PRINTF
102
103 #if CONFIG_PRINTF > PRINTF_NOFLOAT
104         #include <float.h>
105
106         /* Maximum precision for floating point values */
107         typedef long double max_float_t;
108
109         /*bernie: save some memory, who cares about floats with lots of decimals? */
110         #define FRMWRI_BUFSIZE 134
111         #if !(ARCH & ARCH_NIGHTTEST)
112                 #warning 134 is too much, the code must be fixed to have a lower precision limit
113         #endif
114 #else
115         /*
116          * Conservative estimate. Should be (probably) 12 (which is the size necessary
117          * to represent (2^32-1) in octal plus the sign bit.
118          */
119         #define FRMWRI_BUFSIZE 16
120 #endif
121
122 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
123 #ifndef MEM_ATTRIBUTE
124 #define MEM_ATTRIBUTE
125 #endif
126
127 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
128         #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
129 #else
130         #define IS_SHORT (sizeof(int) == 2)
131 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
132
133
134 #if CONFIG_PRINTF > PRINTF_NOFLOAT
135
136 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
137                 MEM_ATTRIBUTE short nr_of_digits,
138                 MEM_ATTRIBUTE char *buf,
139                 MEM_ATTRIBUTE char format_flag,
140                 MEM_ATTRIBUTE char g_flag,
141                 MEM_ATTRIBUTE bool alternate_flag)
142 {
143         MEM_ATTRIBUTE char *cp;
144         MEM_ATTRIBUTE char *buf_pointer;
145         MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
146
147         buf_pointer = buf;
148         integral_10_log = 0;
149
150         if (value >= 1)
151         {
152                 while (value >= 1e11) /* To speed up things a bit */
153                 {
154                         value /= 1e10;
155                         integral_10_log += 10;
156                 }
157                 while (value >= 10)
158                 {
159                         value /= 10;
160                         integral_10_log++;
161                 }
162         }
163         else if (value) /* Not just 0.0 */
164         {
165                 while (value <= 1e-10) /* To speed up things a bit */
166                 {
167                         value *= 1e10;
168                         integral_10_log -= 10;
169                 }
170                 while (value < 1)
171                 {
172                         value *= 10;
173                         integral_10_log--;
174                 }
175         }
176         if (g_flag)
177         {
178                 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
179                 {
180                         format_flag = 0;
181                         nr_of_digits -= integral_10_log;
182                 }
183                 nr_of_digits--;
184                 if (alternate_flag)
185                         /* %#G - No removal of trailing zeros */
186                         g_flag = 0;
187                 else
188                         /* %G - Removal of trailing zeros */
189                         alternate_flag = true;
190         }
191
192         /* %e or %E */
193         if (format_flag)
194         {
195                 dec_point_pos = 0;
196         }
197         else
198         {
199                 /* Less than one... */
200                 if (integral_10_log < 0)
201                 {
202                         *buf_pointer++ = '0';
203                         if ((n = nr_of_digits) || alternate_flag)
204                                 *buf_pointer++ = '.';
205                         i = 0;
206                         while (--i > integral_10_log && nr_of_digits)
207                         {
208                                 *buf_pointer++ = '0';
209                                 nr_of_digits--;
210                         }
211                         if (integral_10_log < (-n - 1))
212                                 /* Nothing more to do */
213                                 goto CLEAN_UP;
214                         dec_point_pos = 1;
215                 }
216                 else
217                 {
218                         dec_point_pos = - integral_10_log;
219                 }
220         }
221
222         i = dec_point_pos;
223         while (i <= nr_of_digits )
224         {
225                 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
226                 value *= 10; /* Prepare for next shot */
227                 *buf_pointer++ = n + '0';
228                 if ( ! i++ && (nr_of_digits || alternate_flag))
229                         *buf_pointer++ = '.';
230         }
231
232         /* Rounding possible */
233         if (value >= 5)
234         {
235                 n = 1; /* Carry */
236                 cp = buf_pointer - 1;
237                 do
238                 {
239                         if (*cp != '.')
240                         {
241                                 if ( (*cp += n) == ('9' + 1) )
242                                 {
243                                         *cp = '0';
244                                         n = 1;
245                                 }
246                                 else
247                                         n = 0;
248                         }
249                 } while (cp-- > buf);
250                 if (n)
251                 {
252                         /* %e or %E */
253                         if (format_flag)
254                         {
255                                 cp = buf_pointer;
256                                 while (cp > buf)
257                                 {
258                                         if (*(cp - 1) == '.')
259                                         {
260                                                 *cp = *(cp - 2);
261                                                 cp--;
262                                         }
263                                         else
264                                                 *cp = *(cp - 1);
265                                         cp--;
266                                 }
267                                 integral_10_log++;
268                         }
269                         else
270                         {
271                                 cp = ++buf_pointer;
272                                 while (cp > buf)
273                                 {
274                                         *cp = *(cp - 1);
275                                         cp--;
276                                 }
277                         }
278                         *buf = '1';
279                 }
280         }
281
282 CLEAN_UP:
283         /* %G - Remove trailing zeros */
284         if (g_flag)
285         {
286                 while (*(buf_pointer - 1) == '0')
287                         buf_pointer--;
288                 if (*(buf_pointer - 1) == '.')
289                         buf_pointer--;
290         }
291
292         /* %e or %E */
293         if (format_flag)
294         {
295                 *buf_pointer++ = format_flag;
296                 if (integral_10_log < 0)
297                 {
298                         *buf_pointer++ = '-';
299                         integral_10_log = -integral_10_log;
300                 }
301                 else
302                         *buf_pointer++ = '+';
303                 n = 0;
304                 buf_pointer +=10;
305                 do
306                 {
307                         n++;
308                         *buf_pointer++ = (integral_10_log % 10) + '0';
309                         integral_10_log /= 10;
310                 } while ( integral_10_log || n < 2 );
311                 for ( i = n ; n > 0 ; n-- )
312                         *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
313                 buf_pointer -= 10;
314         }
315         return (buf_pointer);
316 }
317
318 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
319
320 /**
321  * This routine forms the core and entry of the formatter.
322  *
323  * The conversion performed conforms to the ANSI specification for "printf".
324  */
325 int
326 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
327                 void put_one_char(char, void *),
328                 void *secret_pointer,
329                 va_list ap)
330 {
331 #if CONFIG_PRINTF > PRINTF_REDUCED
332         MEM_ATTRIBUTE static char bad_conversion[] = "???";
333         MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
334
335         MEM_ATTRIBUTE int precision;
336         MEM_ATTRIBUTE int n;
337 #if CONFIG_PRINTF_COUNT_CHARS
338         MEM_ATTRIBUTE int nr_of_chars;
339 #endif
340         MEM_ATTRIBUTE int field_width;
341         MEM_ATTRIBUTE char format_flag;
342         enum PLUS_SPACE_FLAGS {
343                 PSF_NONE, PSF_PLUS, PSF_MINUS
344         };
345         enum DIV_FACTOR {
346                 DIV_DEC, DIV_HEX,
347 #if CONFIG_PRINTF_OCTAL_FORMATTER
348                 DIV_OCT,
349 #endif
350         };
351         MEM_ATTRIBUTE struct {
352                 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
353 #if CONFIG_PRINTF_OCTAL_FORMATTER
354                 enum DIV_FACTOR div_factor : 2;
355 #else
356                 enum DIV_FACTOR div_factor : 1;
357 #endif
358                 bool left_adjust : 1;
359                 bool l_L_modifier : 1;
360                 bool h_modifier : 1;
361                 bool alternate_flag : 1;
362                 bool nonzero_value : 1;
363                 bool zeropad : 1;
364 #if CPU_HARVARD
365                 bool progmem : 1;
366 #endif
367         } flags;
368         MEM_ATTRIBUTE unsigned long ulong;
369
370 #if CONFIG_PRINTF >  PRINTF_NOFLOAT
371         MEM_ATTRIBUTE max_float_t fvalue;
372 #endif
373
374         MEM_ATTRIBUTE char *buf_pointer;
375         MEM_ATTRIBUTE char *ptr;
376         MEM_ATTRIBUTE const char *hex;
377         MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
378
379 #if CONFIG_PRINTF_COUNT_CHARS
380         nr_of_chars = 0;
381 #endif
382         for (;;)    /* Until full format string read */
383         {
384                 while ((format_flag = PGM_READ_CHAR(format++)) != '%')    /* Until '%' or '\0' */
385                 {
386                         if (!format_flag)
387 #if CONFIG_PRINTF_RETURN_COUNT
388                                 return (nr_of_chars);
389 #else
390                                 return 0;
391 #endif
392                         put_one_char(format_flag, secret_pointer);
393 #if CONFIG_PRINTF_COUNT_CHARS
394                         nr_of_chars++;
395 #endif
396                 }
397                 if (PGM_READ_CHAR(format) == '%')    /* %% prints as % */
398                 {
399                         format++;
400                         put_one_char('%', secret_pointer);
401 #if CONFIG_PRINTF_COUNT_CHARS
402                         nr_of_chars++;
403 #endif
404                         continue;
405                 }
406
407                 flags.left_adjust = false;
408                 flags.alternate_flag = false;
409                 flags.plus_space_flag = PSF_NONE;
410                 flags.zeropad = false;
411 #if CPU_HARVARD
412                 flags.progmem = false;
413 #endif
414                 ptr = buf_pointer = &buf[0];
415                 hex = HEX_tab;
416
417                 /* check for leading '-', '+', ' ','#' or '0' flags  */
418                 for (;;)
419                 {
420                         switch (PGM_READ_CHAR(format))
421                         {
422                                 case ' ':
423                                         if (flags.plus_space_flag)
424                                                 goto NEXT_FLAG;
425                                 case '+':
426                                         flags.plus_space_flag = PSF_PLUS;
427                                         goto NEXT_FLAG;
428                                 case '-':
429                                         flags.left_adjust = true;
430                                         goto NEXT_FLAG;
431                                 case '#':
432                                         flags.alternate_flag = true;
433                                         goto NEXT_FLAG;
434                                 case '0':
435                                         flags.zeropad = true;
436                                         goto NEXT_FLAG;
437                         }
438                         break;
439 NEXT_FLAG:
440                         format++;
441                 }
442
443                 /* Optional field width (may be '*') */
444                 if (PGM_READ_CHAR(format) == '*')
445                 {
446                         field_width = va_arg(ap, int);
447                         if (field_width < 0)
448                         {
449                                 field_width = -field_width;
450                                 flags.left_adjust = true;
451                         }
452                         format++;
453                 }
454                 else
455                 {
456                         field_width = 0;
457                         while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
458                                 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
459                 }
460
461                 if (flags.left_adjust)
462                         flags.zeropad = false;
463
464                 /* Optional precision (or '*') */
465                 if (PGM_READ_CHAR(format) == '.')
466                 {
467                         if (PGM_READ_CHAR(++format) == '*')
468                         {
469                                 precision = va_arg(ap, int);
470                                 format++;
471                         }
472                         else
473                         {
474                                 precision = 0;
475                                 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
476                                         precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
477                         }
478                 }
479                 else
480                         precision = -1;
481
482                 /* At this point, "left_adjust" is nonzero if there was
483                  * a sign, "zeropad" is 1 if there was a leading zero
484                  * and 0 otherwise, "field_width" and "precision"
485                  * contain numbers corresponding to the digit strings
486                  * before and after the decimal point, respectively,
487                  * and "plus_space_flag" is either 0 (no flag) or
488                  * contains a plus or space character. If there was no
489                  * decimal point, "precision" will be -1.
490                  */
491
492                 flags.l_L_modifier = false;
493                 flags.h_modifier = false;
494
495                 /* Optional 'l','L','z' or 'h' modifier? */
496                 switch (PGM_READ_CHAR(format))
497                 {
498                         case 'l':
499                         case 'L':
500                         case 'z':
501                                 flags.l_L_modifier = true;
502                                 format++;
503                                 break;
504                         case 'h':
505                                 flags.h_modifier = true;
506                                 format++;
507                                 break;
508                 }
509
510                 /*
511                  * At exit from the following switch, we will emit
512                  * the characters starting at "buf_pointer" and
513                  * ending at "ptr"-1
514                  */
515                 switch (format_flag = PGM_READ_CHAR(format++))
516                 {
517 #if CONFIG_PRINTF_N_FORMATTER
518                         case 'n':
519                                 if (sizeof(short) != sizeof(int))
520                                 {
521                                         if (sizeof(int) != sizeof(long))
522                                         {
523                                                 if (h_modifier)
524                                                         *va_arg(ap, short *) = nr_of_chars;
525                                                 else if (flags.l_L_modifier)
526                                                         *va_arg(ap, long *) = nr_of_chars;
527                                                 else
528                                                         *va_arg(ap, int *) = nr_of_chars;
529                                         }
530                                         else
531                                         {
532                                                 if (h_modifier)
533                                                         *va_arg(ap, short *) = nr_of_chars;
534                                                 else
535                                                         *va_arg(ap, int *) = nr_of_chars;
536                                         }
537                                 }
538                                 else
539                                 {
540                                         if (flags.l_L_modifier)
541                                                 *va_arg(ap, long *) = nr_of_chars;
542                                         else
543                                                 *va_arg(ap, int *) = nr_of_chars;
544                                 }
545                                 continue;
546 #endif
547                         case 'c':
548                                 buf[0] = va_arg(ap, int);
549                                 ptr++;
550                                 break;
551
552                         /* Custom formatter for strings in program memory. */
553                         case 'S':
554 #if CPU_HARVARD
555                                 flags.progmem = true;
556 #endif
557                                 /* Fall trough */
558
559                         case 's':
560                                 if ( !(buf_pointer = va_arg(ap, char *)) )
561                                         buf_pointer = null_pointer;
562                                 if (precision < 0)
563                                         precision = 10000;
564
565                                 /*
566                                  * Move `ptr' to the last character of the
567                                  * string that will be actually printed.
568                                  */
569                                 ptr = buf_pointer;
570 #if CPU_HARVARD
571                                 if (flags.progmem)
572                                 {
573                                         for (n=0; pgm_read_char(ptr) && n < precision; n++)
574                                                 ++ptr;
575                                 }
576                                 else
577 #endif
578                                 for (n=0; *ptr && n < precision; n++)
579                                         ++ptr;
580                                 break;
581
582 #if CONFIG_PRINTF_OCTAL_FORMATTER
583                         case 'o':
584                                 if (flags.alternate_flag && !precision)
585                                         precision++;
586 #endif
587                         case 'x':
588                                 hex = hex_tab;
589                         case 'u':
590                         case 'p':
591                         case 'X':
592                                 if (format_flag == 'p')
593 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
594                                         ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
595 #else /* 32bit pointers */
596                                         ulong = (unsigned long)va_arg(ap, char *);
597 #endif /* 32bit pointers */
598                                 else if (flags.l_L_modifier)
599                                         ulong = va_arg(ap, unsigned long);
600                                 else if (flags.h_modifier)
601                                         ulong = (unsigned long)(unsigned short)va_arg(ap, unsigned int);
602                                 else
603                                         ulong = va_arg(ap, unsigned int);
604
605                                 flags.div_factor =
606 #if CONFIG_PRINTF_OCTAL_FORMATTER
607                                         (format_flag == 'o') ? DIV_OCT :
608 #endif
609                                         (format_flag == 'u') ? DIV_DEC : DIV_HEX;
610                                 flags.plus_space_flag = PSF_NONE;
611                                 goto INTEGRAL_CONVERSION;
612
613                         case 'd':
614                         case 'i':
615                                 if (flags.l_L_modifier)
616                                         ulong = (unsigned long)(long)va_arg(ap, long);
617                                 else
618                                         ulong = (unsigned long)(long)va_arg(ap, int);
619
620                                 /* Extract sign */
621                                 if ((signed long)ulong < 0)
622                                 {
623                                         flags.plus_space_flag = PSF_MINUS;
624                                         ulong = (unsigned long)(-((signed long)ulong));
625                                 }
626
627                                 flags.div_factor = DIV_DEC;
628
629                                 /* Now convert to digits */
630 INTEGRAL_CONVERSION:
631                                 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
632                                 flags.nonzero_value = (ulong != 0);
633
634                                 /* No char if zero and zero precision */
635                                 if (precision != 0 || flags.nonzero_value)
636                                 {
637                                         switch (flags.div_factor)
638                                         {
639                                         case DIV_DEC:
640                                                 do
641                                                         *--buf_pointer = hex[ulong % 10];
642                                                 while (ulong /= 10);
643                                                 break;
644
645                                         case DIV_HEX:
646                                                 do
647                                                         *--buf_pointer = hex[ulong % 16];
648                                                 while (ulong /= 16);
649                                                 break;
650 #if CONFIG_PRINTF_OCTAL_FORMATTER
651                                         case DIV_OCT:
652                                                 do
653                                                         *--buf_pointer = hex[ulong % 8];
654                                                 while (ulong /= 8);
655                                                 break;
656 #endif
657                                         }
658                                 }
659
660                                 /* "precision" takes precedence */
661                                 if (precision < 0)
662                                         if (flags.zeropad)
663                                                 precision = field_width - (flags.plus_space_flag != PSF_NONE);
664                                 while (precision > (int)(ptr - buf_pointer))
665                                         *--buf_pointer = '0';
666
667                                 if (flags.alternate_flag && flags.nonzero_value)
668                                 {
669                                         if (format_flag == 'x' || format_flag == 'X')
670                                         {
671                                                 *--buf_pointer = format_flag;
672                                                 *--buf_pointer = '0';
673                                         }
674 #if CONFIG_PRINTF_OCTAL_FORMATTER
675                                         else if ((format_flag == 'o') && (*buf_pointer != '0'))
676                                         {
677                                                 *--buf_pointer = '0';
678                                         }
679 #endif
680                                 }
681                                 ASSERT(buf_pointer >= buf);
682                                 break;
683
684 #if CONFIG_PRINTF > PRINTF_NOFLOAT
685                         case 'g':
686                         case 'G':
687                                 n = 1;
688                                 format_flag -= 2;
689                                 if (! precision)
690                                 {
691                                         precision = 1;
692                                 }
693                                 goto FLOATING_CONVERSION;
694                         case 'f':
695                                 format_flag = 0;
696                         case 'e':
697                         case 'E':
698                                 n = 0;
699 FLOATING_CONVERSION:
700                                 if (precision < 0)
701                                 {
702                                         precision = 6;
703                                 }
704
705                                 if (sizeof(double) != sizeof(max_float_t))
706                                 {
707                                         fvalue = flags.l_L_modifier ?
708                                                 va_arg(ap,max_float_t) : va_arg(ap,double);
709                                 }
710                                 else
711                                         fvalue = va_arg(ap,max_float_t);
712
713                                 if (fvalue < 0)
714                                 {
715                                         flags.plus_space_flag = PSF_MINUS;
716                                         fvalue = -fvalue;
717                                 }
718                                 ptr = float_conversion (fvalue,
719                                                 (short)precision,
720                                                 buf_pointer += field_width,
721                                                 format_flag,
722                                                 (char)n,
723                                                 flags.alternate_flag);
724                                 if (flags.zeropad)
725                                 {
726                                         precision = field_width - (flags.plus_space_flag != PSF_NONE);
727                                         while (precision > ptr - buf_pointer)
728                                                 *--buf_pointer = '0';
729                                 }
730                                 break;
731
732 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
733
734                         case '\0': /* Really bad place to find NUL in */
735                                 format--;
736
737                         default:
738                                 /* Undefined conversion! */
739                                 ptr = buf_pointer = bad_conversion;
740                                 ptr += sizeof(bad_conversion) - 1;
741                                 break;
742
743                 }
744
745                 /*
746                  * This part emittes the formatted string to "put_one_char".
747                  */
748
749                 /* If field_width == 0 then nothing should be written. */
750                 precision = ptr - buf_pointer;
751
752                 if ( precision > field_width)
753                 {
754                         n = 0;
755                 }
756                 else
757                 {
758                         n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
759                 }
760
761                 /* emit any leading pad characters */
762                 if (!flags.left_adjust)
763                         while (--n >= 0)
764                         {
765                                 put_one_char(' ', secret_pointer);
766 #if CONFIG_PRINTF_COUNT_CHARS
767                                 nr_of_chars++;
768 #endif
769                         }
770
771                 /* emit flag characters (if any) */
772                 if (flags.plus_space_flag)
773                 {
774                         put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
775 #if CONFIG_PRINTF_COUNT_CHARS
776                         nr_of_chars++;
777 #endif
778                 }
779
780 #if CPU_HARVARD
781                 if (flags.progmem)
782                 {
783                         while (--precision >= 0)
784                         {
785                                 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
786 #if CONFIG_PRINTF_COUNT_CHARS
787                                 nr_of_chars++;
788 #endif
789                         }
790                 }
791                 else
792 #endif /* CPU_HARVARD */
793                 {
794                         /* emit the string itself */
795                         while (--precision >= 0)
796                         {
797                                 put_one_char(*buf_pointer++, secret_pointer);
798 #if CONFIG_PRINTF_COUNT_CHARS
799                                 nr_of_chars++;
800 #endif
801                         }
802                 }
803
804                 /* emit trailing space characters */
805                 if (flags.left_adjust)
806                         while (--n >= 0)
807                         {
808                                 put_one_char(' ', secret_pointer);
809 #if CONFIG_PRINTF_COUNT_CHARS
810                                 nr_of_chars++;
811 #endif
812                         }
813         }
814
815 #else /* PRINTF_REDUCED starts here */
816
817 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
818         bool l_modifier, h_modifier;
819         unsigned long u_val, div_val;
820 #else
821         unsigned int u_val, div_val;
822 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
823
824         char format_flag;
825         unsigned int nr_of_chars, base;
826         char outChar;
827         char *ptr;
828
829         nr_of_chars = 0;
830         for (;;)    /* Until full format string read */
831         {
832                 while ((format_flag = PGM_READ_CHAR(format++)) != '%')    /* Until '%' or '\0' */
833                 {
834                         if (!format_flag)
835                                 return (nr_of_chars);
836                         put_one_char(format_flag, secret_pointer);
837                         nr_of_chars++;
838                 }
839
840 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
841                 /*
842                  * Optional 'l', 'z' or 'h' modifiers?
843                  */
844                 l_modifier = h_modifier = false;
845                 switch (PGM_READ_CHAR(format))
846                 {
847                         case 'l':
848                         case 'z':
849                                 /* for the 'z' modifier, we make this assumption */
850                                 STATIC_ASSERT(sizeof(size_t) == sizeof(long));
851                                 l_modifier = true;
852                                 format++;
853                                 break;
854
855                         case 'h':
856                                 h_modifier = true;
857                                 format++;
858                                 break;
859                 }
860 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
861
862                 switch (format_flag = PGM_READ_CHAR(format++))
863                 {
864                         case 'c':
865                                 format_flag = va_arg(ap, int);
866                         default:
867                                 put_one_char(format_flag, secret_pointer);
868                                 nr_of_chars++;
869                                 continue;
870
871                         case 's':
872                                 ptr = va_arg(ap, char *);
873                                 while ((format_flag = *ptr++))
874                                 {
875                                         put_one_char(format_flag, secret_pointer);
876                                         nr_of_chars++;
877                                 }
878                                 continue;
879
880                         case 'o':
881                                 base = 8;
882                                 if (IS_SHORT)
883                                         div_val = 0x8000;
884                                 else
885                                         div_val = 0x40000000;
886                                 goto CONVERSION_LOOP;
887
888                         case 'd':
889                                 base = 10;
890                                 if (IS_SHORT)
891                                         div_val = 10000;
892                                 else
893                                         div_val = 1000000000;
894                                 goto CONVERSION_LOOP;
895
896                         case 'X':
897                         case 'x':
898                                 base = 16;
899                                 if (IS_SHORT)
900                                         div_val = 0x1000;
901                                 else
902                                         div_val = 0x10000000;
903
904 CONVERSION_LOOP:
905 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
906                                 if (h_modifier)
907                                 {
908                                         if (format_flag == 'd')
909                                                 u_val = (short)va_arg(ap, int);
910                                         else
911                                                 u_val = (unsigned short)va_arg(ap, int);
912                                 }
913                                 else if (l_modifier)
914                                         u_val = va_arg(ap, long);
915                                 else
916                                 {
917                                         if (format_flag == 'd')
918                                                 u_val = va_arg(ap, int);
919                                         else
920                                                 u_val = va_arg(ap, unsigned int);
921                                 }
922
923 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
924                                 u_val = va_arg(ap,int);
925 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
926                                 if (format_flag == 'd')
927                                 {
928                                         if (((int)u_val) < 0)
929                                         {
930                                                 u_val = - u_val;
931                                                 put_one_char('-', secret_pointer);
932                                                 nr_of_chars++;
933                                         }
934                                 }
935                                 while (div_val > 1 && div_val > u_val)
936                                 {
937                                         div_val /= base;
938                                 }
939                                 do
940                                 {
941                                         outChar = (u_val / div_val) + '0';
942                                         if (outChar > '9')
943                                         {
944                                                 if (format_flag == 'x')
945                                                         outChar += 'a'-'9'-1;
946                                                 else
947                                                         outChar += 'A'-'9'-1;
948                                         }
949                                         put_one_char(outChar, secret_pointer);
950                                         nr_of_chars++;
951                                         u_val %= div_val;
952                                         div_val /= base;
953                                 }
954                                 while (div_val);
955
956                 } /* end switch(format_flag...) */
957         }
958 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
959 }
960
961 #endif /* CONFIG_PRINTF */