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