mb/google/nissa/var/glassway: Modify touch screen ILIT2901 sequence
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / scanw.c
blob97eaf70b4f8a56999418112e5454fa3f38da781c
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: scanw.c,v 1.42 2008/07/14 12:22:13 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: scanw
11 Synopsis:
12 int scanw(const char *fmt, ...);
13 int wscanw(WINDOW *win, const char *fmt, ...);
14 int mvscanw(int y, int x, const char *fmt, ...);
15 int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...);
16 int vwscanw(WINDOW *win, const char *fmt, va_list varglist);
17 int vw_scanw(WINDOW *win, const char *fmt, va_list varglist);
19 Description:
20 These routines correspond to the standard C library's scanf()
21 family. Each gets a string from the window via wgetnstr(), and
22 uses the resulting line as input for the scan.
24 Return Value:
25 On successful completion, these functions return the number of
26 items successfully matched. Otherwise they return ERR.
28 Portability X/Open BSD SYS V
29 scanw Y Y Y
30 wscanw Y Y Y
31 mvscanw Y Y Y
32 mvwscanw Y Y Y
33 vwscanw Y - 4.0
34 vw_scanw Y
36 **man-end****************************************************************/
38 #include <string.h>
40 #ifndef HAVE_VSSCANF
41 # include <stdlib.h>
42 # include <ctype.h>
43 # include <limits.h>
45 static int _pdc_vsscanf(const char *, const char *, va_list);
47 # define vsscanf _pdc_vsscanf
48 #endif
50 int vwscanw(WINDOW *win, const char *fmt, va_list varglist)
52 char scanbuf[256];
54 PDC_LOG(("vwscanw() - called\n"));
56 if (wgetnstr(win, scanbuf, 255) == ERR)
57 return ERR;
59 return vsscanf(scanbuf, fmt, varglist);
62 int scanw(const char *fmt, ...)
64 va_list args;
65 int retval;
67 PDC_LOG(("scanw() - called\n"));
69 va_start(args, fmt);
70 retval = vwscanw(stdscr, fmt, args);
71 va_end(args);
73 return retval;
76 int wscanw(WINDOW *win, const char *fmt, ...)
78 va_list args;
79 int retval;
81 PDC_LOG(("wscanw() - called\n"));
83 va_start(args, fmt);
84 retval = vwscanw(win, fmt, args);
85 va_end(args);
87 return retval;
90 int mvscanw(int y, int x, const char *fmt, ...)
92 va_list args;
93 int retval;
95 PDC_LOG(("mvscanw() - called\n"));
97 if (move(y, x) == ERR)
98 return ERR;
100 va_start(args, fmt);
101 retval = vwscanw(stdscr, fmt, args);
102 va_end(args);
104 return retval;
107 int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...)
109 va_list args;
110 int retval;
112 PDC_LOG(("mvscanw() - called\n"));
114 if (wmove(win, y, x) == ERR)
115 return ERR;
117 va_start(args, fmt);
118 retval = vwscanw(win, fmt, args);
119 va_end(args);
121 return retval;
124 int vw_scanw(WINDOW *win, const char *fmt, va_list varglist)
126 PDC_LOG(("vw_scanw() - called\n"));
128 return vwscanw(win, fmt, varglist);
131 #ifndef HAVE_VSSCANF
133 /* _pdc_vsscanf() - Internal routine to parse and format an input
134 buffer. It scans a series of input fields; each field is formatted
135 according to a supplied format string and the formatted input is
136 stored in the variable number of addresses passed. Returns the number
137 of input fields or EOF on error.
139 Don't compile this unless required. Some compilers (at least Borland
140 C++ 3.0) have to link with math libraries due to the use of floats.
142 Based on vsscanf.c and input.c from emx 0.8f library source,
143 Copyright (c) 1990-1992 by Eberhard Mattes, who has kindly agreed to
144 its inclusion in PDCurses. */
146 #define WHITE(x) ((x) == ' ' || (x) == '\t' || (x) == '\n')
148 #define NEXT(x) \
149 do { \
150 x = *buf++; \
151 if (!x) \
152 return (count ? count : EOF); \
153 ++chars; \
154 } while (0)
156 #define UNGETC() \
157 do { \
158 --buf; --chars; \
159 } while (0)
161 static int _pdc_vsscanf(const char *buf, const char *fmt, va_list arg_ptr)
163 int count, chars, c, width, radix, d, i;
164 int *int_ptr;
165 long *long_ptr;
166 short *short_ptr;
167 char *char_ptr;
168 unsigned char f;
169 char neg, assign, ok, size;
170 long n;
171 char map[256], end;
172 double dx, dd, *dbl_ptr;
173 float *flt_ptr;
174 int exp;
175 char eneg;
177 count = 0;
178 chars = 0;
179 c = 0;
180 while ((f = *fmt) != 0)
182 if (WHITE(f))
186 ++fmt;
187 f = *fmt;
189 while (WHITE(f));
192 c = *buf++;
193 if (!c)
195 if (!f || count)
196 return count;
197 else
198 return EOF;
199 } else
200 ++chars;
202 while (WHITE(c));
203 UNGETC();
204 } else if (f != '%')
206 NEXT(c);
207 if (c != f)
208 return count;
209 ++fmt;
210 } else
212 assign = TRUE;
213 width = INT_MAX;
214 char_ptr = NULL;
215 ++fmt;
216 if (*fmt == '*')
218 assign = FALSE;
219 ++fmt;
221 if (isdigit(*fmt))
223 width = 0;
224 while (isdigit(*fmt))
225 width = width * 10 + (*fmt++ - '0');
226 if (!width)
227 width = INT_MAX;
229 size = 0;
230 if (*fmt == 'h' || *fmt == 'l')
231 size = *fmt++;
232 f = *fmt;
233 switch (f)
235 case 'c':
236 if (width == INT_MAX)
237 width = 1;
238 if (assign)
239 char_ptr = va_arg(arg_ptr, char *);
240 while (width > 0)
242 --width;
243 NEXT(c);
244 if (assign)
246 *char_ptr++ = (char) c;
247 ++count;
250 break;
251 case '[':
252 memset(map, 0, 256);
253 end = 0;
254 ++fmt;
255 if (*fmt == '^')
257 ++fmt;
258 end = 1;
260 i = 0;
261 for (;;)
263 f = (unsigned char) *fmt;
264 switch (f)
266 case 0:
267 /* avoid skipping past 0 */
268 --fmt;
269 NEXT(c);
270 goto string;
271 case ']':
272 if (i > 0)
274 NEXT(c);
275 goto string;
277 __fallthrough;
278 default:
279 if (fmt[1] == '-' && fmt[2]
280 && f < (unsigned char)fmt[2])
282 memset(map + f, 1, (unsigned char)fmt[2] - f);
283 fmt += 2;
285 else
286 map[f] = 1;
287 break;
289 ++fmt;
290 ++i;
292 case 's':
293 memset(map, 0, 256);
294 map[' '] = 1;
295 map['\n'] = 1;
296 map['\r'] = 1;
297 map['\t'] = 1;
298 end = 1;
301 NEXT(c);
303 while (WHITE(c));
304 string:
305 if (assign)
306 char_ptr = va_arg(arg_ptr, char *);
307 while (width > 0 && map[(unsigned char) c] != end)
309 --width;
310 if (assign)
311 *char_ptr++ = (char) c;
312 c = *buf++;
313 if (!c)
314 break;
315 else
316 ++chars;
318 if (assign)
320 *char_ptr = 0;
321 ++count;
323 if (!c)
324 return count;
325 else
326 UNGETC();
327 break;
328 case 'f':
329 case 'e':
330 case 'E':
331 case 'g':
332 case 'G':
333 neg = ok = FALSE;
334 dx = 0.0;
337 NEXT(c);
339 while (WHITE(c));
340 if (c == '+')
342 NEXT(c);
343 --width;
344 } else if (c == '-')
346 neg = TRUE;
347 NEXT(c);
348 --width;
350 while (width > 0 && isdigit(c))
352 --width;
353 dx = dx * 10.0 + (double) (c - '0');
354 ok = TRUE;
355 c = *buf++;
356 if (!c)
357 break;
358 else
359 ++chars;
361 if (width > 0 && c == '.')
363 --width;
364 dd = 10.0;
365 NEXT(c);
366 while (width > 0 && isdigit(c))
368 --width;
369 dx += (double) (c - '0') / dd;
370 dd *= 10.0;
371 ok = TRUE;
372 c = *buf++;
373 if (!c)
374 break;
375 else
376 ++chars;
379 if (!ok)
380 return count;
381 if (width > 0 && (c == 'e' || c == 'E'))
383 eneg = FALSE;
384 exp = 0;
385 NEXT(c);
386 --width;
387 if (width > 0 && c == '+')
389 NEXT(c);
390 --width;
391 } else if (width > 0 && c == '-')
393 eneg = TRUE;
394 NEXT(c);
395 --width;
397 if (!(width > 0 && isdigit(c)))
399 UNGETC();
400 return count;
402 while (width > 0 && isdigit(c))
404 --width;
405 exp = exp * 10 + (c - '0');
406 c = *buf++;
407 if (!c)
408 break;
409 else
410 ++chars;
412 if (eneg)
413 exp = -exp;
414 while (exp > 0)
416 dx *= 10.0;
417 --exp;
419 while (exp < 0)
421 dx /= 10.0;
422 ++exp;
425 if (assign)
427 if (neg)
428 dx = -dx;
429 if (size == 'l')
431 dbl_ptr = va_arg(arg_ptr, double *);
432 *dbl_ptr = dx;
434 else
436 flt_ptr = va_arg(arg_ptr, float *);
437 *flt_ptr = (float)dx;
439 ++count;
441 if (!c)
442 return count;
443 else
444 UNGETC();
445 break;
446 case 'i':
447 neg = FALSE;
448 radix = 10;
451 NEXT(c);
453 while (WHITE(c));
454 if (!(width > 0 && c == '0'))
455 goto scan_complete_number;
456 NEXT(c);
457 --width;
458 if (width > 0 && (c == 'x' || c == 'X'))
460 NEXT(c);
461 radix = 16;
462 --width;
464 else if (width > 0 && (c >= '0' && c <= '7'))
465 radix = 8;
466 goto scan_unsigned_number;
467 case 'd':
468 case 'u':
469 case 'o':
470 case 'x':
471 case 'X':
474 NEXT(c);
476 while (WHITE(c));
477 switch (f)
479 case 'o':
480 radix = 8;
481 break;
482 case 'x':
483 case 'X':
484 radix = 16;
485 break;
486 default:
487 radix = 10;
488 break;
490 scan_complete_number:
491 neg = FALSE;
492 if (width > 0 && c == '+')
494 NEXT(c);
495 --width;
497 else if (width > 0 && c == '-' && radix == 10)
499 neg = TRUE;
500 NEXT(c);
501 --width;
503 scan_unsigned_number:
504 n = 0;
505 ok = FALSE;
506 while (width > 0)
508 --width;
509 if (isdigit(c))
510 d = c - '0';
511 else if (isupper(c))
512 d = c - 'A' + 10;
513 else if (islower(c))
514 d = c - 'a' + 10;
515 else
516 break;
517 if (d < 0 || d >= radix)
518 break;
519 ok = TRUE;
520 n = n * radix + d;
521 c = *buf++;
522 if (!c)
523 break;
524 else
525 ++chars;
527 if (!ok)
528 return count;
529 if (assign)
531 if (neg)
532 n = -n;
533 switch (size)
535 case 'h':
536 short_ptr = va_arg(arg_ptr, short *);
537 *short_ptr = (short) n;
538 break;
539 case 'l':
540 long_ptr = va_arg(arg_ptr, long *);
541 *long_ptr = (long) n;
542 break;
543 default:
544 int_ptr = va_arg(arg_ptr, int *);
545 *int_ptr = (int) n;
547 ++count;
549 if (!c)
550 return count;
551 else
552 UNGETC();
553 break;
554 case 'n':
555 if (assign)
557 int_ptr = va_arg(arg_ptr, int *);
558 *int_ptr = chars;
559 ++count;
561 break;
562 default:
563 if (!f) /* % at end of string */
564 return count;
565 NEXT(c);
566 if (c != f)
567 return count;
568 break;
570 ++fmt;
573 return count;
575 #endif /* HAVE_VSSCANF */