1
0

scanw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. scanw
  5. -----
  6. ### Synopsis
  7. int scanw(const char *fmt, ...);
  8. int wscanw(WINDOW *win, const char *fmt, ...);
  9. int mvscanw(int y, int x, const char *fmt, ...);
  10. int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...);
  11. int vwscanw(WINDOW *win, const char *fmt, va_list varglist);
  12. int vw_scanw(WINDOW *win, const char *fmt, va_list varglist);
  13. ### Description
  14. These routines correspond to the standard C library's scanf() family.
  15. Each gets a string from the window via wgetnstr(), and uses the
  16. resulting line as input for the scan.
  17. The duplication between vwscanw() and vw_scanw() is for historic
  18. reasons. In PDCurses, they're the same.
  19. ### Return Value
  20. On successful completion, these functions return the number of items
  21. successfully matched. Otherwise they return ERR.
  22. ### Portability
  23. X/Open ncurses NetBSD
  24. scanw Y Y Y
  25. wscanw Y Y Y
  26. mvscanw Y Y Y
  27. mvwscanw Y Y Y
  28. vwscanw Y Y Y
  29. vw_scanw Y Y Y
  30. **man-end****************************************************************/
  31. #include <string.h>
  32. #ifndef HAVE_VSSCANF
  33. # include <stdlib.h>
  34. # include <ctype.h>
  35. # include <limits.h>
  36. static int _pdc_vsscanf(const char *, const char *, va_list);
  37. # define vsscanf _pdc_vsscanf
  38. #endif
  39. int vwscanw(WINDOW *win, const char *fmt, va_list varglist)
  40. {
  41. char scanbuf[256];
  42. PDC_LOG(("vwscanw() - called\n"));
  43. if (wgetnstr(win, scanbuf, 255) == ERR)
  44. return ERR;
  45. return vsscanf(scanbuf, fmt, varglist);
  46. }
  47. int scanw(const char *fmt, ...)
  48. {
  49. va_list args;
  50. int retval;
  51. PDC_LOG(("scanw() - called\n"));
  52. va_start(args, fmt);
  53. retval = vwscanw(stdscr, fmt, args);
  54. va_end(args);
  55. return retval;
  56. }
  57. int wscanw(WINDOW *win, const char *fmt, ...)
  58. {
  59. va_list args;
  60. int retval;
  61. PDC_LOG(("wscanw() - called\n"));
  62. va_start(args, fmt);
  63. retval = vwscanw(win, fmt, args);
  64. va_end(args);
  65. return retval;
  66. }
  67. int mvscanw(int y, int x, const char *fmt, ...)
  68. {
  69. va_list args;
  70. int retval;
  71. PDC_LOG(("mvscanw() - called\n"));
  72. if (move(y, x) == ERR)
  73. return ERR;
  74. va_start(args, fmt);
  75. retval = vwscanw(stdscr, fmt, args);
  76. va_end(args);
  77. return retval;
  78. }
  79. int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...)
  80. {
  81. va_list args;
  82. int retval;
  83. PDC_LOG(("mvscanw() - called\n"));
  84. if (wmove(win, y, x) == ERR)
  85. return ERR;
  86. va_start(args, fmt);
  87. retval = vwscanw(win, fmt, args);
  88. va_end(args);
  89. return retval;
  90. }
  91. int vw_scanw(WINDOW *win, const char *fmt, va_list varglist)
  92. {
  93. PDC_LOG(("vw_scanw() - called\n"));
  94. return vwscanw(win, fmt, varglist);
  95. }
  96. #ifndef HAVE_VSSCANF
  97. /* _pdc_vsscanf() - Internal routine to parse and format an input
  98. buffer. It scans a series of input fields; each field is formatted
  99. according to a supplied format string and the formatted input is
  100. stored in the variable number of addresses passed. Returns the number
  101. of input fields or EOF on error.
  102. Don't compile this unless required. Some compilers (at least Borland
  103. C++ 3.0) have to link with math libraries due to the use of floats.
  104. Based on vsscanf.c and input.c from emx 0.8f library source,
  105. Copyright (c) 1990-1992 by Eberhard Mattes, who has kindly agreed to
  106. its inclusion in PDCurses. */
  107. #define WHITE(x) ((x) == ' ' || (x) == '\t' || (x) == '\n')
  108. #define NEXT(x) \
  109. do { \
  110. x = *buf++; \
  111. if (!x) \
  112. return (count ? count : EOF); \
  113. ++chars; \
  114. } while (0)
  115. #define UNGETC() \
  116. do { \
  117. --buf; --chars; \
  118. } while (0)
  119. static int _pdc_vsscanf(const char *buf, const char *fmt, va_list arg_ptr)
  120. {
  121. int count, chars, c, width, radix, d, i;
  122. int *int_ptr;
  123. long *long_ptr;
  124. short *short_ptr;
  125. char *char_ptr;
  126. unsigned char f;
  127. char neg, assign, ok, size;
  128. long n;
  129. char map[256], end;
  130. double dx, dd, *dbl_ptr;
  131. float *flt_ptr;
  132. int exp;
  133. char eneg;
  134. count = 0;
  135. chars = 0;
  136. c = 0;
  137. while ((f = *fmt) != 0)
  138. {
  139. if (WHITE(f))
  140. {
  141. do
  142. {
  143. ++fmt;
  144. f = *fmt;
  145. }
  146. while (WHITE(f));
  147. do
  148. {
  149. c = *buf++;
  150. if (!c)
  151. {
  152. if (!f || count)
  153. return count;
  154. else
  155. return EOF;
  156. } else
  157. ++chars;
  158. }
  159. while (WHITE(c));
  160. UNGETC();
  161. } else if (f != '%')
  162. {
  163. NEXT(c);
  164. if (c != f)
  165. return count;
  166. ++fmt;
  167. } else
  168. {
  169. assign = TRUE;
  170. width = INT_MAX;
  171. char_ptr = NULL;
  172. ++fmt;
  173. if (*fmt == '*')
  174. {
  175. assign = FALSE;
  176. ++fmt;
  177. }
  178. if (isdigit(*fmt))
  179. {
  180. width = 0;
  181. while (isdigit(*fmt))
  182. width = width * 10 + (*fmt++ - '0');
  183. if (!width)
  184. width = INT_MAX;
  185. }
  186. size = 0;
  187. if (*fmt == 'h' || *fmt == 'l')
  188. size = *fmt++;
  189. f = *fmt;
  190. switch (f)
  191. {
  192. case 'c':
  193. if (width == INT_MAX)
  194. width = 1;
  195. if (assign)
  196. char_ptr = va_arg(arg_ptr, char *);
  197. while (width > 0)
  198. {
  199. --width;
  200. NEXT(c);
  201. if (assign)
  202. {
  203. *char_ptr++ = (char) c;
  204. ++count;
  205. }
  206. }
  207. break;
  208. case '[':
  209. memset(map, 0, 256);
  210. end = 0;
  211. ++fmt;
  212. if (*fmt == '^')
  213. {
  214. ++fmt;
  215. end = 1;
  216. }
  217. i = 0;
  218. for (;;)
  219. {
  220. f = (unsigned char) *fmt;
  221. switch (f)
  222. {
  223. case 0:
  224. /* avoid skipping past 0 */
  225. --fmt;
  226. NEXT(c);
  227. goto string;
  228. case ']':
  229. if (i > 0)
  230. {
  231. NEXT(c);
  232. goto string;
  233. }
  234. /* no break */
  235. default:
  236. if (fmt[1] == '-' && fmt[2]
  237. && f < (unsigned char)fmt[2])
  238. {
  239. memset(map + f, 1, (unsigned char)fmt[2] - f);
  240. fmt += 2;
  241. }
  242. else
  243. map[f] = 1;
  244. break;
  245. }
  246. ++fmt;
  247. ++i;
  248. }
  249. case 's':
  250. memset(map, 0, 256);
  251. map[' '] = 1;
  252. map['\n'] = 1;
  253. map['\r'] = 1;
  254. map['\t'] = 1;
  255. end = 1;
  256. do
  257. {
  258. NEXT(c);
  259. }
  260. while (WHITE(c));
  261. string:
  262. if (assign)
  263. char_ptr = va_arg(arg_ptr, char *);
  264. while (width > 0 && map[(unsigned char) c] != end)
  265. {
  266. --width;
  267. if (assign)
  268. *char_ptr++ = (char) c;
  269. c = *buf++;
  270. if (!c)
  271. break;
  272. else
  273. ++chars;
  274. }
  275. if (assign)
  276. {
  277. *char_ptr = 0;
  278. ++count;
  279. }
  280. if (!c)
  281. return count;
  282. else
  283. UNGETC();
  284. break;
  285. case 'f':
  286. case 'e':
  287. case 'E':
  288. case 'g':
  289. case 'G':
  290. neg = ok = FALSE;
  291. dx = 0.0;
  292. do
  293. {
  294. NEXT(c);
  295. }
  296. while (WHITE(c));
  297. if (c == '+')
  298. {
  299. NEXT(c);
  300. --width;
  301. } else if (c == '-')
  302. {
  303. neg = TRUE;
  304. NEXT(c);
  305. --width;
  306. }
  307. while (width > 0 && isdigit(c))
  308. {
  309. --width;
  310. dx = dx * 10.0 + (double) (c - '0');
  311. ok = TRUE;
  312. c = *buf++;
  313. if (!c)
  314. break;
  315. else
  316. ++chars;
  317. }
  318. if (width > 0 && c == '.')
  319. {
  320. --width;
  321. dd = 10.0;
  322. NEXT(c);
  323. while (width > 0 && isdigit(c))
  324. {
  325. --width;
  326. dx += (double) (c - '0') / dd;
  327. dd *= 10.0;
  328. ok = TRUE;
  329. c = *buf++;
  330. if (!c)
  331. break;
  332. else
  333. ++chars;
  334. }
  335. }
  336. if (!ok)
  337. return count;
  338. if (width > 0 && (c == 'e' || c == 'E'))
  339. {
  340. eneg = FALSE;
  341. exp = 0;
  342. NEXT(c);
  343. --width;
  344. if (width > 0 && c == '+')
  345. {
  346. NEXT(c);
  347. --width;
  348. } else if (width > 0 && c == '-')
  349. {
  350. eneg = TRUE;
  351. NEXT(c);
  352. --width;
  353. }
  354. if (!(width > 0 && isdigit(c)))
  355. {
  356. UNGETC();
  357. return count;
  358. }
  359. while (width > 0 && isdigit(c))
  360. {
  361. --width;
  362. exp = exp * 10 + (c - '0');
  363. c = *buf++;
  364. if (!c)
  365. break;
  366. else
  367. ++chars;
  368. }
  369. if (eneg)
  370. exp = -exp;
  371. while (exp > 0)
  372. {
  373. dx *= 10.0;
  374. --exp;
  375. }
  376. while (exp < 0)
  377. {
  378. dx /= 10.0;
  379. ++exp;
  380. }
  381. }
  382. if (assign)
  383. {
  384. if (neg)
  385. dx = -dx;
  386. if (size == 'l')
  387. {
  388. dbl_ptr = va_arg(arg_ptr, double *);
  389. *dbl_ptr = dx;
  390. }
  391. else
  392. {
  393. flt_ptr = va_arg(arg_ptr, float *);
  394. *flt_ptr = (float)dx;
  395. }
  396. ++count;
  397. }
  398. if (!c)
  399. return count;
  400. else
  401. UNGETC();
  402. break;
  403. case 'i':
  404. neg = FALSE;
  405. radix = 10;
  406. do
  407. {
  408. NEXT(c);
  409. }
  410. while (WHITE(c));
  411. if (!(width > 0 && c == '0'))
  412. goto scan_complete_number;
  413. NEXT(c);
  414. --width;
  415. if (width > 0 && (c == 'x' || c == 'X'))
  416. {
  417. NEXT(c);
  418. radix = 16;
  419. --width;
  420. }
  421. else if (width > 0 && (c >= '0' && c <= '7'))
  422. radix = 8;
  423. goto scan_unsigned_number;
  424. case 'd':
  425. case 'u':
  426. case 'o':
  427. case 'x':
  428. case 'X':
  429. do
  430. {
  431. NEXT(c);
  432. }
  433. while (WHITE(c));
  434. switch (f)
  435. {
  436. case 'o':
  437. radix = 8;
  438. break;
  439. case 'x':
  440. case 'X':
  441. radix = 16;
  442. break;
  443. default:
  444. radix = 10;
  445. break;
  446. }
  447. scan_complete_number:
  448. neg = FALSE;
  449. if (width > 0 && c == '+')
  450. {
  451. NEXT(c);
  452. --width;
  453. }
  454. else if (width > 0 && c == '-' && radix == 10)
  455. {
  456. neg = TRUE;
  457. NEXT(c);
  458. --width;
  459. }
  460. scan_unsigned_number:
  461. n = 0;
  462. ok = FALSE;
  463. while (width > 0)
  464. {
  465. --width;
  466. if (isdigit(c))
  467. d = c - '0';
  468. else if (isupper(c))
  469. d = c - 'A' + 10;
  470. else if (islower(c))
  471. d = c - 'a' + 10;
  472. else
  473. break;
  474. if (d < 0 || d >= radix)
  475. break;
  476. ok = TRUE;
  477. n = n * radix + d;
  478. c = *buf++;
  479. if (!c)
  480. break;
  481. else
  482. ++chars;
  483. }
  484. if (!ok)
  485. return count;
  486. if (assign)
  487. {
  488. if (neg)
  489. n = -n;
  490. switch (size)
  491. {
  492. case 'h':
  493. short_ptr = va_arg(arg_ptr, short *);
  494. *short_ptr = (short) n;
  495. break;
  496. case 'l':
  497. long_ptr = va_arg(arg_ptr, long *);
  498. *long_ptr = (long) n;
  499. break;
  500. default:
  501. int_ptr = va_arg(arg_ptr, int *);
  502. *int_ptr = (int) n;
  503. }
  504. ++count;
  505. }
  506. if (!c)
  507. return count;
  508. else
  509. UNGETC();
  510. break;
  511. case 'n':
  512. if (assign)
  513. {
  514. int_ptr = va_arg(arg_ptr, int *);
  515. *int_ptr = chars;
  516. ++count;
  517. }
  518. break;
  519. default:
  520. if (!f) /* % at end of string */
  521. return count;
  522. NEXT(c);
  523. if (c != f)
  524. return count;
  525. break;
  526. }
  527. ++fmt;
  528. }
  529. }
  530. return count;
  531. }
  532. #endif /* HAVE_VSSCANF */