GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / lib / lib_scanf.c
blobe06e41cd15ee7750acc39d2720aa6218296448af
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * Reentrant string scan function File: lib_scanf.c
5 *
6 * This routine is used to scan formatted patterns of a given
7 * string.
8 *
9 * Author:
11 *********************************************************************
13 * Copyright 2000,2001,2002,2003
14 * Broadcom Corporation. All rights reserved.
16 * This software is furnished under license and may be used and
17 * copied only in accordance with the following terms and
18 * conditions. Subject to these conditions, you may download,
19 * copy, install, use, modify and distribute modified or unmodified
20 * copies of this software in source and/or binary form. No title
21 * or ownership is transferred hereby.
23 * 1) Any source code used, modified or distributed must reproduce
24 * and retain this copyright notice and list of conditions
25 * as they appear in the source file.
27 * 2) No right is granted to use any trade name, trademark, or
28 * logo of Broadcom Corporation. The "Broadcom Corporation"
29 * name may not be used to endorse or promote products derived
30 * from this software without the prior written permission of
31 * Broadcom Corporation.
33 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
35 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
37 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
38 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
45 * THE POSSIBILITY OF SUCH DAMAGE.
46 ********************************************************************* */
48 #include <stdarg.h>
49 #include "lib_types.h"
50 #include "lib_scanf.h"
52 #define GETAP(ap) \
53 ap = (char *) va_arg(args,char *);
55 static int
56 match(char *fmt, char cc)
58 int exc = 0;
59 char *cp1;
61 if (!cc)
62 return 0;
63 if (*fmt == '^') {
64 exc = 1;
65 fmt++;
68 for (cp1 = fmt; *cp1 && *cp1 != ']';) {
69 if (cp1[1] == '-' && cp1[2] > cp1[0]) {
70 if (cc >= cp1[0] && cc <= cp1[2])
71 return exc^1;
72 cp1 += 3;
74 else {
75 if (cc == *cp1)
76 return exc^1;
77 cp1++;
80 return exc;
83 static long
84 asclng(char **cp, int width, int base)
86 long answer;
87 char cc, sign, *cp1;
89 answer = sign = 0;
90 for (cp1=*cp; *cp1; cp1++) {
91 if (*cp1 > ' ')
92 break;
94 if (*cp1 == '-' || *cp1 == '+')
95 sign = *cp1++, width--;
96 if (!*cp1 || !width)
97 goto exi1;
98 if (!base) {
99 base = 10;
100 if (*cp1 == '0') {
101 base = 8;
102 goto lab4;
105 else if (base == 16) {
106 if (*cp1 == '0') {
107 lab4: cp1++, width--;
108 if (width > 0) {
109 if (*cp1 == 'x' || *cp1 == 'X')
110 base = 16, cp1++, width--;
114 for (; width && *cp1; cp1++,width--) {
115 if ((cc = *cp1) < '0')
116 break;
117 if (cc <= '9')
118 cc &= 0xf;
119 else {
120 cc &= 0xdf;
121 if (cc >= 'A')
122 cc -= 'A' - 10;
124 if (cc >= base)
125 break;
126 answer = base * answer + cc;
129 exi1:
130 *cp = cp1;
131 if (sign == '-')
132 answer = -answer;
133 return answer;
137 * Reentrant Scan Routine
138 * entry: &input buffer
139 * &format string
140 * &argument list
141 * exit: return value is number of fields scanned
144 lib_sscanf(char *buf, char *fmt, ...)
146 int field; /* field flag: 0 = background, 1 = %field */
147 int sizdef; /* size: 0 = default, 1 = short, 2 = long, 3 = long double */
148 int width; /* field width */
149 int par1;
150 long l1;
151 int nfields;
152 char fch;
153 char *orgbuf, *prebuf;
154 char *ap;
155 va_list args;
157 va_start(args,fmt); /* get variable arg list address */
158 if (!*buf)
159 return -1;
160 nfields = field = sizdef = 0;
161 orgbuf = buf;
162 while ((fch = *fmt++) != 0) {
163 if (!field) {
164 if (fch == '%') {
165 if (*fmt != '%') {
166 field = 1;
167 continue;
169 fch = *fmt++;
171 if (fch <= ' ')
172 for (; *buf== ' '||*buf=='\t'; buf++) ;
173 else if (fch == *buf)
174 buf++;
176 else {
177 width = 0x7fff;
178 if (fch == '*') {
179 width = va_arg(args,int);
180 goto lab6;
182 else if (fch >= '0' && fch <= '9') {
183 fmt--;
184 width = asclng(&fmt,9,10);
185 lab6: fch = *fmt++;
187 if (fch == 'h') {
188 sizdef = 1;
189 goto lab7;
191 else if (fch == 'l') {
192 sizdef = 2;
193 lab7: fch = *fmt++;
196 prebuf = buf;
197 switch (fch) {
198 case 'd': /* signed integer */
199 par1 = 10;
200 goto lab3;
201 case 'o': /* signed integer */
202 par1 = 8;
203 goto lab3;
204 case 'x': /* signed integer */
205 case 'X': /* long signed integer */
206 par1 = 16;
207 goto lab3;
208 case 'u': /* unsigned integer */
209 case 'i': /* signed integer */
210 par1 = 0;
211 lab3: GETAP(ap);
212 l1 = asclng(&buf, width, par1);
213 if (prebuf == buf)
214 break;
215 if (sizdef == 2)
216 *(long *)ap = l1;
217 else if (sizdef == 1)
218 *(short *)ap = l1;
219 else
220 *(int *)ap = l1;
221 goto lab12;
222 case 'c': /* character */
223 GETAP(ap);
224 for (; width && *buf; width--) {
225 *ap++ = *buf++;
226 if (width == 0x7fff)
227 break;
229 goto lab12;
230 case '[': /* search set */
231 GETAP(ap);
232 for (; width && match(fmt,*buf); width--)
233 *ap++ = *buf++;
234 while (*fmt++ != ']');
235 goto lab11;
236 case 's': /* character array */
237 GETAP(ap);
238 for (; *buf==' '||*buf==0x07; buf++) ;
239 for (; width && *buf && *buf>' '; width--)
240 *ap++ = *buf++;
241 lab11: if (prebuf == buf)
242 break;
243 *(char *)ap = 0;
244 goto lab12;
245 case 'n': /* store # chars */
246 GETAP(ap);
247 *(int *)ap = buf - orgbuf;
248 break;
249 case 'p': /* pointer */
250 GETAP(ap);
251 *(long *)ap = asclng(&buf, width, 16);
252 lab12: nfields++;
253 break;
254 default: /* illegal */
255 goto term;
257 field = 0;
259 if (!fch)
260 break;
262 term:
263 return nfields;