Subversion Repositories NaviCtrl

Rev

Rev 1 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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