Subversion Repositories NaviCtrl

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*
2
Copyright (C) 1993 Free Software Foundation
3
 
4
This file is part of the GNU IO Library.  This library is free
5
software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the
7
Free Software Foundation; either version 2, or (at your option)
8
any later version.
9
 
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
 
15
You should have received a copy of the GNU General Public License
16
along with this library; see the file COPYING.  If not, write to the Free
17
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
19
As a special exception, if you link this library with files
20
compiled with a GNU compiler to produce an executable, this does not cause
21
the resulting executable to be covered by the GNU General Public License.
22
This exception does not however invalidate any other reasons why
23
the executable file might be covered by the GNU General Public License. */
24
 
25
/*
26
 * Copyright (c) 1990 Regents of the University of California.
27
 * All rights reserved.
28
 *
29
 * Redistribution and use in source and binary forms, with or without
30
 * modification, are permitted provided that the following conditions
31
 * are met:
32
 * 1. Redistributions of source code must retain the above copyright
33
 *    notice, this list of conditions and the following disclaimer.
34
 * 2. Redistributions in binary form must reproduce the above copyright
35
 *    notice, this list of conditions and the following disclaimer in the
36
 *    documentation and/or other materials provided with the distribution.
37
 * 3. [rescinded 22 July 1999]
38
 * 4. Neither the name of the University nor the names of its contributors
39
 *    may be used to endorse or promote products derived from this software
40
 *    without specific prior written permission.
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
 * SUCH DAMAGE.
53
 */
54
 
55
/* * ****************************************************************************
56
 This file is a patched version of printf called _printf_P
57
 It is made to work with avr-gcc for Atmel AVR MCUs.
58
 There are some differences from standard printf:
59
        1. There is no floating point support (with fp the code is about 8K!)
60
        2. Return type is void
61
        3. Format string must be in program memory (by using macro printf this is
62
           done automaticaly)
63
        4. %n is not implemented (just remove the comment around it if you need it)
64
        5. If LIGHTPRINTF is defined, the code is about 550 bytes smaller and the
65
           folowing specifiers are disabled :
66
                space # * . - + p s o O
67
        6. A function void uart_sendchar(char c) is used for output. The UART must
68
                be initialized before using printf.
69
 
70
 Alexander Popov
71
 sasho@vip.orbitel.bg
72
******************************************************************************
73
 Changed file to work with LPC - ARM.
74
 Ingo Busker
75
 busker@mikrocontroller.com   02.2005
76
******************************************************************************/
77
 
78
 
79
/*
80
 * Actual printf innards.
81
 *
82
 * This code is large and complicated...
83
 */
84
 
85
#include <string.h>
86
#ifdef __STDC__
87
#include <stdarg.h>
88
#else
89
#include <varargs.h>
90
#endif
91
 
92
#include "main.h"
93
 
94
 
95
//#define LIGHTPRINTF
96
char Putchar(char zeichen)
97
{
98
   DisplayBuff[DispPtr++] = zeichen;
99
   return(1);
100
}
101
 
102
 
103
void PRINT(const char * ptr, unsigned int len) {
104
        for(;len;len--)
105
                Putchar(*ptr++);
106
}
107
 
108
void PRINTP(const char * ptr, unsigned int len) {
109
        for(;len;len--)
110
                Putchar(*ptr++);
111
}
112
 
113
void PAD_SP(signed char howmany) {
114
        for(;howmany>0;howmany--)
115
                Putchar(' ');
116
}
117
 
118
void PAD_0(signed char howmany) {
119
        for(;howmany>0;howmany--)
120
                Putchar('0');
121
}
122
 
123
#define BUF             40
124
 
125
/*
126
 * Macros for converting digits to letters and vice versa
127
 */
128
#define to_digit(c)     ((c) - '0')
129
#define  is_digit(c)    ((c)<='9' && (c)>='0')
130
#define to_char(n)      ((n) + '0')
131
 
132
/*
133
 * Flags used during conversion.
134
 */
135
#define LONGINT         0x01            /* long integer */
136
#define LONGDBL         0x02            /* long double; unimplemented */
137
#define SHORTINT                0x04            /* short integer */
138
#define ALT                     0x08            /* alternate form */
139
#define LADJUST         0x10            /* left adjustment */
140
#define ZEROPAD         0x20            /* zero (as opposed to blank) pad */
141
#define HEXPREFIX       0x40            /* add 0x or 0X prefix */
142
 
143
void _printf_P (char const *fmt0, ...)      /* Works with string from FLASH */
144
{
145
        va_list ap;
146
        register const char *fmt; /* format string */
147
        register char ch;       /* character from fmt */
148
        register int n;         /* handy integer (short term usage) */
149
        register char *cp;      /* handy char pointer (short term usage) */
150
        const char *fmark;      /* for remembering a place in fmt */
151
        register unsigned char flags;   /* flags as above */
152
        signed char width;              /* width from format (%8d), or 0 */
153
        signed char prec;               /* precision from format (%.3d), or -1 */
154
        char sign;                              /* sign prefix (' ', '+', '-', or \0) */
155
        unsigned long _ulong=0; /* integer arguments %[diouxX] */
156
#define OCT 8
157
#define DEC 10
158
#define HEX 16
159
        unsigned char base;             /* base for [diouxX] conversion */
160
        signed char dprec;              /* a copy of prec if [diouxX], 0 otherwise */
161
        signed char dpad;                       /* extra 0 padding needed for integers */
162
        signed char fieldsz;            /* field size expanded by sign, dpad etc */
163
        /* The initialization of 'size' is to suppress a warning that
164
           'size' might be used unitialized.  It seems gcc can't
165
           quite grok this spaghetti code ... */
166
        signed char size = 0;           /* size of converted field or string */
167
        char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
168
        char ox[2];                     /* space for 0x hex-prefix */
169
 
170
        va_start(ap, fmt0);
171
 
172
        fmt = fmt0;
173
 
174
        /*
175
         * Scan the format for conversions (`%' character).
176
         */
177
        for (;;) {
178
                for (fmark = fmt; (ch = (*fmt)) != '\0' && ch != '%'; fmt++)
179
                        /* void */;
180
                if ((n = fmt - fmark) != 0) {
181
                        PRINTP(fmark, n);
182
                }
183
                if (ch == '\0')
184
                        goto done;
185
                fmt++;          /* skip over '%' */
186
 
187
                flags = 0;
188
                dprec = 0;
189
                width = 0;
190
                prec = -1;
191
                sign = '\0';
192
 
193
rflag:          ch = (*fmt++);
194
reswitch:
195
#ifdef LIGHTPRINTF
196
        if (ch=='o' || ch=='u' || (ch|0x20)=='x') {
197
#else
198
        if (ch=='u' || (ch|0x20)=='x') {
199
#endif
200
                if (flags&LONGINT) {
201
                        _ulong=va_arg(ap, unsigned long);
202
                } else {
203
                        register unsigned int _d;
204
                        _d=va_arg(ap, unsigned int);
205
                        _ulong = flags&SHORTINT ? (unsigned long)(unsigned short)_d : (unsigned long)_d;
206
                }
207
        }
208
 
209
#ifndef LIGHTPRINTF
210
                if(ch==' ') {
211
                        /*
212
                         * ``If the space and + flags both appear, the space
213
                         * flag will be ignored.''
214
                         *      -- ANSI X3J11
215
                         */
216
                        if (!sign)
217
                                sign = ' ';
218
                        goto rflag;
219
                } else if (ch=='#') {
220
                        flags |= ALT;
221
                        goto rflag;
222
                } else if (ch=='*'||ch=='-') {
223
                        if (ch=='*') {
224
                                /*
225
                                 * ``A negative field width argument is taken as a
226
                                 * - flag followed by a positive field width.''
227
                                 *      -- ANSI X3J11
228
                                 * They don't exclude field widths read from args.
229
                                 */
230
                                if ((width = va_arg(ap, int)) >= 0)
231
                                        goto rflag;
232
                                width = -width;
233
                        }
234
                        flags |= LADJUST;
235
                        flags &= ~ZEROPAD; /* '-' disables '0' */
236
                        goto rflag;
237
                } else if (ch=='+') {
238
                        sign = '+';
239
                        goto rflag;
240
                } else if (ch=='.') {
241
                        if ((ch = (*fmt++)) == '*') {
242
                                n = va_arg(ap, int);
243
                                prec = n < 0 ? -1 : n;
244
                                goto rflag;
245
                        }
246
                        n = 0;
247
                        while (is_digit(ch)) {
248
                                n = n*10 + to_digit(ch);
249
                                ch = (*fmt++);
250
                        }
251
                        prec = n < 0 ? -1 : n;
252
                        goto reswitch;
253
                } else
254
#endif /* LIGHTPRINTF */
255
                if (ch=='0') {
256
                        /*
257
                         * ``Note that 0 is taken as a flag, not as the
258
                         * beginning of a field width.''
259
                         *      -- ANSI X3J11
260
                         */
261
                        if (!(flags & LADJUST))
262
                            flags |= ZEROPAD; /* '-' disables '0' */
263
                        goto rflag;
264
                } else if (ch>='1' && ch<='9') {
265
                        n = 0;
266
                        do {
267
                                n = 10 * n + to_digit(ch);
268
                                ch = *fmt++;
269
                        } while (is_digit(ch));
270
                        width = n;
271
                        goto reswitch;
272
                } else if (ch=='h') {
273
                        flags |= SHORTINT;
274
                        goto rflag;
275
                } else if (ch=='l') {
276
                        flags |= LONGINT;
277
                        goto rflag;
278
                } else if (ch=='c') {
279
                        *(cp = buf) = va_arg(ap, int);
280
                        size = 1;
281
                        sign = '\0';
282
                } else if (ch=='D'||ch=='d'||ch=='i') {
283
                        if(ch=='D')
284
                                flags |= LONGINT;
285
                        if (flags&LONGINT) {
286
                                _ulong=va_arg(ap, long);
287
                        } else {
288
                                register int _d;
289
                                _d=va_arg(ap, int);
290
                                _ulong = flags&SHORTINT ? (long)(short)_d : (long)_d;
291
                        }
292
 
293
                        if ((long)_ulong < 0) {
294
                                _ulong = -_ulong;
295
                                sign = '-';
296
                        }
297
                        base = DEC;
298
                        goto number;
299
                } else
300
/*             
301
                if (ch=='n') {
302
                        if (flags & LONGINT)
303
                                *va_arg(ap, long *) = ret;
304
                        else if (flags & SHORTINT)
305
                                *va_arg(ap, short *) = ret;
306
                        else
307
                                *va_arg(ap, int *) = ret;
308
                        continue;       // no output
309
                } else
310
*/
311
#ifndef LIGHTPRINTF                     
312
                if (ch=='O'||ch=='o') {
313
                        if (ch=='O')
314
                                flags |= LONGINT;
315
                        base = OCT;
316
                        goto nosign;
317
                } else if (ch=='p') {
318
                        /*
319
                         * ``The argument shall be a pointer to void.  The
320
                         * value of the pointer is converted to a sequence
321
                         * of printable characters, in an implementation-
322
                         * defined manner.''
323
                         *      -- ANSI X3J11
324
                         */
325
                        /* NOSTRICT */
326
                        _ulong = (unsigned int)va_arg(ap, void *);
327
                        base = HEX;
328
                        flags |= HEXPREFIX;
329
                        ch = 'x';
330
                        goto nosign;
331
                } else if (ch=='s') {  // print a string from RAM
332
                        if ((cp = va_arg(ap, char *)) == NULL) {
333
                                cp=buf;
334
                                cp[0] = '(';
335
                                cp[1] = 'n';
336
                                cp[2] = 'u';
337
                                cp[4] = cp[3] = 'l';
338
                                cp[5] = ')';
339
                                cp[6] = '\0';
340
                        }
341
                        if (prec >= 0) {
342
                                /*
343
                                 * can't use strlen; can only look for the
344
                                 * NUL in the first `prec' characters, and
345
                                 * strlen() will go further.
346
                                 */
347
                                char *p = (char*)memchr(cp, 0, prec);
348
 
349
                                if (p != NULL) {
350
                                        size = p - cp;
351
                                        if (size > prec)
352
                                                size = prec;
353
                                } else
354
                                        size = prec;
355
                        } else
356
                                size = strlen(cp);
357
                        sign = '\0';
358
                } else
359
#endif /* LIGHTPRINTF */                        
360
                if(ch=='U'||ch=='u') {
361
                        if (ch=='U')
362
                                flags |= LONGINT;
363
                        base = DEC;
364
                        goto nosign;
365
                } else if (ch=='X'||ch=='x') {
366
                        base = HEX;
367
                        /* leading 0x/X only if non-zero */
368
                        if (flags & ALT && _ulong != 0)
369
                                flags |= HEXPREFIX;
370
 
371
                        /* unsigned conversions */
372
nosign:                 sign = '\0';
373
                        /*
374
                         * ``... diouXx conversions ... if a precision is
375
                         * specified, the 0 flag will be ignored.''
376
                         *      -- ANSI X3J11
377
                         */
378
number: if ((dprec = prec) >= 0)
379
                                flags &= ~ZEROPAD;
380
 
381
                        /*
382
                         * ``The result of converting a zero value with an
383
                         * explicit precision of zero is no characters.''
384
                         *      -- ANSI X3J11
385
                         */
386
                        cp = buf + BUF;
387
                        if (_ulong != 0 || prec != 0) {
388
                                register unsigned char _d,notlastdigit;
389
                                do {
390
                                        notlastdigit=(_ulong>=base);
391
                                        _d = _ulong % base;
392
 
393
                                        if (_d<10) {
394
                                                _d+='0';
395
                                        } else {
396
                                                _d+='a'-10;
397
                                                if (ch=='X') _d&=~0x20;
398
                                        }
399
                                        *--cp=_d;
400
                                        _ulong /= base;
401
                                } while (notlastdigit);
402
#ifndef LIGHTPRINTF
403
                                // handle octal leading 0 
404
                                if (base==OCT && flags & ALT && *cp != '0')
405
                                        *--cp = '0';
406
#endif
407
                        }
408
 
409
                        size = buf + BUF - cp;
410
        } else {  //default
411
                /* "%?" prints ?, unless ? is NUL */
412
                        if (ch == '\0')
413
                                goto done;
414
                        /* pretend it was %c with argument ch */
415
                        cp = buf;
416
                        *cp = ch;
417
                        size = 1;
418
                        sign = '\0';
419
                }
420
 
421
                /*
422
                 * All reasonable formats wind up here.  At this point,
423
                 * `cp' points to a string which (if not flags&LADJUST)
424
                 * should be padded out to `width' places.  If
425
                 * flags&ZEROPAD, it should first be prefixed by any
426
                 * sign or other prefix; otherwise, it should be blank
427
                 * padded before the prefix is emitted.  After any
428
                 * left-hand padding and prefixing, emit zeroes
429
                 * required by a decimal [diouxX] precision, then print
430
                 * the string proper, then emit zeroes required by any
431
                 * leftover floating precision; finally, if LADJUST,
432
                 * pad with blanks.
433
                 */
434
 
435
                /*
436
                 * compute actual size, so we know how much to pad.
437
                 */
438
                fieldsz = size;
439
 
440
                dpad = dprec - size;
441
                if (dpad < 0)
442
                    dpad = 0;
443
 
444
                if (sign)
445
                        fieldsz++;
446
                else if (flags & HEXPREFIX)
447
                        fieldsz += 2;
448
                fieldsz += dpad;
449
 
450
                /* right-adjusting blank padding */
451
                if ((flags & (LADJUST|ZEROPAD)) == 0)
452
                        PAD_SP(width - fieldsz);
453
 
454
                /* prefix */
455
                if (sign) {
456
                        PRINT(&sign, 1);
457
                } else if (flags & HEXPREFIX) {
458
                        ox[0] = '0';
459
                        ox[1] = ch;
460
                        PRINT(ox, 2);
461
                }
462
 
463
                /* right-adjusting zero padding */
464
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
465
                        PAD_0(width - fieldsz);
466
 
467
                /* leading zeroes from decimal precision */
468
                PAD_0(dpad);
469
 
470
                /* the string or number proper */
471
                PRINT(cp, size);
472
 
473
                /* left-adjusting padding (always blank) */
474
                if (flags & LADJUST)
475
                        PAD_SP(width - fieldsz);
476
        }
477
done:
478
        va_end(ap);
479
}