kconfig: simplify symbol-search code
[carl9170fw.git] / carlfw / src / printf.c
1 /*
2  * Copyright (C) 2004,2008  Kustaa Nyholm
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "carl9170.h"
20 #include "printf.h"
21
22 #ifdef CONFIG_CARL9170FW_PRINTF
23 static char *bf;
24 static char buf[12];
25 static unsigned int num;
26 static char uc;
27 static char zs;
28
29 static void out(const char c)
30 {
31         *bf++ = c;
32 }
33
34 static void outDgt(const char dgt)
35 {
36         out(dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10));
37         zs = 1;
38 }
39
40 static void divOut(const unsigned int d)
41 {
42         unsigned char dgt = 0;
43
44         while (num >= d) {
45                 num -= d;
46                 dgt++;
47         }
48
49         if (zs || dgt > 0)
50                 outDgt(dgt);
51 }
52
53 void tfp_printf(const char *fmt, ...)
54 {
55         va_list va;
56         char *p;
57         unsigned int i;
58         char ch;
59
60         va_start(va, fmt);
61
62         while ((ch = *(fmt++))) {
63                 if (ch != '%') {
64                         putcharacter(ch);
65                 } else {
66                         char lz = 0;
67                         char w = 0;
68                         ch = *(fmt++);
69
70                         if (ch == '0') {
71                                 ch = *(fmt++);
72                                 lz = 1;
73                         }
74
75                         if (ch >= '0' && ch <= '9') {
76                                 w = 0;
77                                 while (ch >= '0' && ch <= '9') {
78                                         w = (((w << 2) + w) << 1) + ch - '0';
79                                         ch = *fmt++;
80                                         }
81                         }
82
83                         bf = buf;
84                         p = bf;
85                         zs = 0;
86
87                         switch (ch) {
88                         case 0:
89                                 goto abort;
90
91                         case 'u':
92                         case 'd':
93                                 num = va_arg(va, unsigned int);
94                                 if (ch == 'd' && (int) num < 0) {
95                                         num = -(int)num;
96                                         out('-');
97                                 }
98
99                                 for (i = 100000000; i != 1; i /= 10)
100                                         divOut(i);
101
102                                 outDgt(num);
103                                 break;
104
105                         case 'p':
106                         case 'x':
107                         case 'X':
108                                 uc = ch == 'X';
109                                 num = va_arg(va, unsigned int);
110                                 for (i = 0x10000000; i != 0x1; i >>= 4)
111                                         divOut(i);
112
113                                 outDgt(num);
114                                 break;
115
116                         case 'c':
117                                 out((char)(va_arg(va, int)));
118                                 break;
119
120                         case 's':
121                                 p = va_arg(va, char*);
122                                 break;
123                         case '%':
124                                 out('%');
125                                 break;
126
127                         default:
128                                 break;
129                                 }
130
131                         *bf = 0;
132                         bf = p;
133                         while (*bf++ && w > 0)
134                                 w--;
135
136                         while (w-- > 0)
137                                 putcharacter(lz ? '0' : ' ');
138
139                         while ((ch = *p++))
140                                 putcharacter(ch);
141                 }
142         }
143
144 abort:
145         putcharacter('\0');
146         va_end(va);
147 }
148
149 #else
150
151 void min_printf(const char *fmt, ...)
152 {
153         char ch;
154
155         do {
156                 ch = *(fmt++);
157                 putcharacter(ch);
158         } while (ch);
159 }
160
161 #endif /* CONFIG_CARL9170FW_PRINTF */