1 /****************************************************************************
2 * Copyright (c) 1998-2003,2004 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * State-machine fallback written by Thomas E. Dickey 2002 *
31 ****************************************************************************/
34 * This function is needed to support vwscanw
37 #include <curses.priv.h>
41 MODULE_ID("$Id: vsscanf.c,v 1.18 2004/04/03 20:27:02 tom Exp $")
43 #if !(HAVE_VFSCANF || HAVE__DOSCAN)
52 ,cError
/* anything that isn't ANSI */
73 ,sPercent
/* last was '%' beginning a format */
74 ,sNormal
/* ...somewhere in the middle */
75 ,sLeft
/* last was left square bracket beginning a range */
76 ,sRange
/* ...somewhere in the middle */
77 ,sFinal
/* last finished a format */
81 final_ch(int ch
, OtherType other
)
83 ChunkType result
= cUnknown
;
87 if (other
== oUnknown
)
125 if (other
== oUnknown
)
131 if (other
== oUnknown
)
137 if (other
== oUnknown
)
149 OtherType result
= oUnknown
;
164 vsscanf(const char *str
, const char *format
, va_list ap
)
166 #if HAVE_VFSCANF || HAVE__DOSCAN
168 * This code should work on anything descended from AT&T SVr1.
172 strbuf
._flag
= _IOREAD
;
173 strbuf
._ptr
= strbuf
._base
= (unsigned char *) str
;
174 strbuf
._cnt
= strlen(str
);
175 strbuf
._file
= _NFILE
;
178 return (vfscanf(&strbuf
, format
, ap
));
180 return (_doscan(&strbuf
, format
, ap
));
183 static int can_convert
= -1;
188 T((T_CALLED("vsscanf(%s,%s,...)"),
190 _nc_visbuf2(2, format
)));
193 * This relies on having a working "%n" format conversion. Check if it
194 * works. Only very old C libraries do not support it.
196 * FIXME: move this check into the configure script.
198 if (can_convert
< 0) {
201 if (sscanf("123", "%d%n", &check1
, &check2
) > 0
211 size_t len_fmt
= strlen(format
) + 32;
212 char *my_fmt
= malloc(len_fmt
);
213 ChunkType chunk
, ctest
;
214 OtherType other
, otest
;
222 * Split the original format into chunks, adding a "%n" to the end
223 * of each (except of course if it used %n), and use that
224 * information to decide where to start scanning the next chunk.
226 * FIXME: does %n count bytes or characters? If the latter, this
227 * will require further work for multibyte strings.
229 while (*format
!= '\0') {
235 for (n
= 0; format
[n
] != 0 && state
!= sFinal
; ++n
) {
236 my_fmt
[n
] = format
[n
];
239 if (format
[n
] == '%')
243 if (format
[n
] == '%') {
245 } else if (format
[n
] == L_SQUARE
) {
254 if (format
[n
] == '^') {
256 my_fmt
[n
] = format
[n
];
260 if (format
[n
] == R_SQUARE
) {
266 if (format
[n
] == '*') {
269 if ((ctest
= final_ch(format
[n
], other
)) != cUnknown
) {
272 } else if ((otest
= other_ch(format
[n
])) != oUnknown
) {
274 } else if (isalpha(UChar(format
[n
]))) {
287 if (chunk
== cUnknown
288 || chunk
== cError
) {
294 /* add %n, if the format was not that */
295 if (chunk
!= cAssigned
) {
296 strcat(my_fmt
, "%n");
301 strcat(my_fmt
, "%n");
305 pointer
= va_arg(ap
, int *);
308 pointer
= va_arg(ap
, short *);
311 pointer
= va_arg(ap
, float *);
314 pointer
= va_arg(ap
, double *);
317 pointer
= va_arg(ap
, long *);
320 pointer
= va_arg(ap
, void *);
325 pointer
= va_arg(ap
, char *);
331 /* do the conversion */
332 T(("...converting chunk #%d type %d(%s,%s)",
334 _nc_visbuf2(1, str
+ consumed
),
335 _nc_visbuf2(2, my_fmt
)));
336 if (sscanf(str
+ consumed
, my_fmt
, pointer
, &eaten
) > 0)
345 returnCode(assigned
);
351 _nc_vsscanf(void); /* quiet's gcc warning */
355 } /* nonempty for strict ANSI compilers */
356 #endif /* !HAVE_VSSCANF */