1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2004,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * State-machine fallback written by Thomas E. Dickey 2002 *
32 ****************************************************************************/
35 * This function is needed to support vwscanw
38 #include <curses.priv.h>
42 MODULE_ID("$Id: vsscanf.c,v 1.21 2020/02/02 23:34:34 tom Exp $")
44 #if !(HAVE_VFSCANF || HAVE__DOSCAN)
53 ,cError /* anything that isn't ANSI */
74 ,sPercent /* last was '%' beginning a format */
75 ,sNormal /* ...somewhere in the middle */
76 ,sLeft /* last was left square bracket beginning a range */
77 ,sRange /* ...somewhere in the middle */
78 ,sFinal /* last finished a format */
82 final_ch(int ch, OtherType other)
84 ChunkType result = cUnknown;
88 if (other == oUnknown)
126 if (other == oUnknown)
132 if (other == oUnknown)
138 if (other == oUnknown)
150 OtherType result = oUnknown;
165 vsscanf(const char *str, const char *format, va_list ap)
167 #if HAVE_VFSCANF || HAVE__DOSCAN
169 * This code should work on anything descended from AT&T SVr1.
173 strbuf._flag = _IOREAD;
174 strbuf._ptr = strbuf._base = (unsigned char *) str;
175 strbuf._cnt = strlen(str);
176 strbuf._file = _NFILE;
179 return (vfscanf(&strbuf, format, ap));
181 return (_doscan(&strbuf, format, ap));
184 static int can_convert = -1;
189 T((T_CALLED("vsscanf(%s,%s,...)"),
191 _nc_visbuf2(2, format)));
194 * This relies on having a working "%n" format conversion. Check if it
195 * works. Only very old C libraries do not support it.
197 * FIXME: move this check into the configure script.
199 if (can_convert < 0) {
202 if (sscanf("123", "%d%n", &check1, &check2) > 0
212 size_t len_fmt = strlen(format) + 32;
213 char *my_fmt = malloc(len_fmt);
214 ChunkType chunk, ctest;
215 OtherType other, otest;
223 * Split the original format into chunks, adding a "%n" to the end
224 * of each (except of course if it used %n), and use that
225 * information to decide where to start scanning the next chunk.
227 * FIXME: does %n count bytes or characters? If the latter, this
228 * will require further work for multibyte strings.
230 while (*format != '\0') {
236 for (n = 0; format[n] != 0 && state != sFinal; ++n) {
237 my_fmt[n] = format[n];
240 if (format[n] == '%')
244 if (format[n] == '%') {
246 } else if (format[n] == L_SQUARE) {
255 if (format[n] == '^') {
257 my_fmt[n] = format[n];
261 if (format[n] == R_SQUARE) {
267 if (format[n] == '*') {
270 if ((ctest = final_ch(format[n], other)) != cUnknown) {
273 } else if ((otest = other_ch(format[n])) != oUnknown) {
275 } else if (isalpha(UChar(format[n]))) {
288 if (chunk == cUnknown
289 || chunk == cError) {
295 /* add %n, if the format was not that */
296 if (chunk != cAssigned) {
297 _nc_STRCAT(my_fmt, "%n", len_fmt);
302 _nc_STRCAT(my_fmt, "%n", len_fmt);
306 pointer = va_arg(ap, int *);
309 pointer = va_arg(ap, short *);
312 pointer = va_arg(ap, float *);
315 pointer = va_arg(ap, double *);
318 pointer = va_arg(ap, long *);
321 pointer = va_arg(ap, void *);
326 pointer = va_arg(ap, char *);
332 /* do the conversion */
333 T(("...converting chunk #%d type %d(%s,%s)",
335 _nc_visbuf2(1, str + consumed),
336 _nc_visbuf2(2, my_fmt)));
337 if (sscanf(str + consumed, my_fmt, pointer, &eaten) > 0)
346 returnCode(assigned);
352 _nc_vsscanf(void); /* quiet's gcc warning */
356 } /* nonempty for strict ANSI compilers */
357 #endif /* !HAVE_VSSCANF */