read.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. F2003 I/O support contributed by Jerry DeLisle
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "io.h"
  21. #include "fbuf.h"
  22. #include "format.h"
  23. #include "unix.h"
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. typedef unsigned char uchar;
  30. /* read.c -- Deal with formatted reads */
  31. /* set_integer()-- All of the integer assignments come here to
  32. actually place the value into memory. */
  33. void
  34. set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
  35. {
  36. switch (length)
  37. {
  38. #ifdef HAVE_GFC_INTEGER_16
  39. /* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */
  40. case 10:
  41. case 16:
  42. {
  43. GFC_INTEGER_16 tmp = value;
  44. memcpy (dest, (void *) &tmp, length);
  45. }
  46. break;
  47. #endif
  48. case 8:
  49. {
  50. GFC_INTEGER_8 tmp = value;
  51. memcpy (dest, (void *) &tmp, length);
  52. }
  53. break;
  54. case 4:
  55. {
  56. GFC_INTEGER_4 tmp = value;
  57. memcpy (dest, (void *) &tmp, length);
  58. }
  59. break;
  60. case 2:
  61. {
  62. GFC_INTEGER_2 tmp = value;
  63. memcpy (dest, (void *) &tmp, length);
  64. }
  65. break;
  66. case 1:
  67. {
  68. GFC_INTEGER_1 tmp = value;
  69. memcpy (dest, (void *) &tmp, length);
  70. }
  71. break;
  72. default:
  73. internal_error (NULL, "Bad integer kind");
  74. }
  75. }
  76. /* Max signed value of size give by length argument. */
  77. GFC_UINTEGER_LARGEST
  78. si_max (int length)
  79. {
  80. #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
  81. GFC_UINTEGER_LARGEST value;
  82. #endif
  83. switch (length)
  84. {
  85. #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
  86. case 16:
  87. case 10:
  88. value = 1;
  89. for (int n = 1; n < 4 * length; n++)
  90. value = (value << 2) + 3;
  91. return value;
  92. #endif
  93. case 8:
  94. return GFC_INTEGER_8_HUGE;
  95. case 4:
  96. return GFC_INTEGER_4_HUGE;
  97. case 2:
  98. return GFC_INTEGER_2_HUGE;
  99. case 1:
  100. return GFC_INTEGER_1_HUGE;
  101. default:
  102. internal_error (NULL, "Bad integer kind");
  103. }
  104. }
  105. /* convert_real()-- Convert a character representation of a floating
  106. point number to the machine number. Returns nonzero if there is an
  107. invalid input. Note: many architectures (e.g. IA-64, HP-PA)
  108. require that the storage pointed to by the dest argument is
  109. properly aligned for the type in question. */
  110. int
  111. convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length)
  112. {
  113. char *endptr = NULL;
  114. int round_mode, old_round_mode;
  115. switch (dtp->u.p.current_unit->round_status)
  116. {
  117. case ROUND_COMPATIBLE:
  118. /* FIXME: As NEAREST but round away from zero for a tie. */
  119. case ROUND_UNSPECIFIED:
  120. /* Should not occur. */
  121. case ROUND_PROCDEFINED:
  122. round_mode = ROUND_NEAREST;
  123. break;
  124. default:
  125. round_mode = dtp->u.p.current_unit->round_status;
  126. break;
  127. }
  128. old_round_mode = get_fpu_rounding_mode();
  129. set_fpu_rounding_mode (round_mode);
  130. switch (length)
  131. {
  132. case 4:
  133. *((GFC_REAL_4*) dest) =
  134. #if defined(HAVE_STRTOF)
  135. gfc_strtof (buffer, &endptr);
  136. #else
  137. (GFC_REAL_4) gfc_strtod (buffer, &endptr);
  138. #endif
  139. break;
  140. case 8:
  141. *((GFC_REAL_8*) dest) = gfc_strtod (buffer, &endptr);
  142. break;
  143. #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
  144. case 10:
  145. *((GFC_REAL_10*) dest) = gfc_strtold (buffer, &endptr);
  146. break;
  147. #endif
  148. #if defined(HAVE_GFC_REAL_16)
  149. # if defined(GFC_REAL_16_IS_FLOAT128)
  150. case 16:
  151. *((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, &endptr);
  152. break;
  153. # elif defined(HAVE_STRTOLD)
  154. case 16:
  155. *((GFC_REAL_16*) dest) = gfc_strtold (buffer, &endptr);
  156. break;
  157. # endif
  158. #endif
  159. default:
  160. internal_error (&dtp->common, "Unsupported real kind during IO");
  161. }
  162. set_fpu_rounding_mode (old_round_mode);
  163. if (buffer == endptr)
  164. {
  165. generate_error (&dtp->common, LIBERROR_READ_VALUE,
  166. "Error during floating point read");
  167. next_record (dtp, 1);
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. /* convert_infnan()-- Convert character INF/NAN representation to the
  173. machine number. Note: many architectures (e.g. IA-64, HP-PA) require
  174. that the storage pointed to by the dest argument is properly aligned
  175. for the type in question. */
  176. int
  177. convert_infnan (st_parameter_dt *dtp, void *dest, const char *buffer,
  178. int length)
  179. {
  180. const char *s = buffer;
  181. int is_inf, plus = 1;
  182. if (*s == '+')
  183. s++;
  184. else if (*s == '-')
  185. {
  186. s++;
  187. plus = 0;
  188. }
  189. is_inf = *s == 'i';
  190. switch (length)
  191. {
  192. case 4:
  193. if (is_inf)
  194. *((GFC_REAL_4*) dest) = plus ? __builtin_inff () : -__builtin_inff ();
  195. else
  196. *((GFC_REAL_4*) dest) = plus ? __builtin_nanf ("") : -__builtin_nanf ("");
  197. break;
  198. case 8:
  199. if (is_inf)
  200. *((GFC_REAL_8*) dest) = plus ? __builtin_inf () : -__builtin_inf ();
  201. else
  202. *((GFC_REAL_8*) dest) = plus ? __builtin_nan ("") : -__builtin_nan ("");
  203. break;
  204. #if defined(HAVE_GFC_REAL_10)
  205. case 10:
  206. if (is_inf)
  207. *((GFC_REAL_10*) dest) = plus ? __builtin_infl () : -__builtin_infl ();
  208. else
  209. *((GFC_REAL_10*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl ("");
  210. break;
  211. #endif
  212. #if defined(HAVE_GFC_REAL_16)
  213. # if defined(GFC_REAL_16_IS_FLOAT128)
  214. case 16:
  215. *((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, NULL);
  216. break;
  217. # else
  218. case 16:
  219. if (is_inf)
  220. *((GFC_REAL_16*) dest) = plus ? __builtin_infl () : -__builtin_infl ();
  221. else
  222. *((GFC_REAL_16*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl ("");
  223. break;
  224. # endif
  225. #endif
  226. default:
  227. internal_error (&dtp->common, "Unsupported real kind during IO");
  228. }
  229. return 0;
  230. }
  231. /* read_l()-- Read a logical value */
  232. void
  233. read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
  234. {
  235. char *p;
  236. int w;
  237. w = f->u.w;
  238. p = read_block_form (dtp, &w);
  239. if (p == NULL)
  240. return;
  241. while (*p == ' ')
  242. {
  243. if (--w == 0)
  244. goto bad;
  245. p++;
  246. }
  247. if (*p == '.')
  248. {
  249. if (--w == 0)
  250. goto bad;
  251. p++;
  252. }
  253. switch (*p)
  254. {
  255. case 't':
  256. case 'T':
  257. set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
  258. break;
  259. case 'f':
  260. case 'F':
  261. set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
  262. break;
  263. default:
  264. bad:
  265. generate_error (&dtp->common, LIBERROR_READ_VALUE,
  266. "Bad value on logical read");
  267. next_record (dtp, 1);
  268. break;
  269. }
  270. }
  271. static gfc_char4_t
  272. read_utf8 (st_parameter_dt *dtp, int *nbytes)
  273. {
  274. static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
  275. static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  276. int i, nb, nread;
  277. gfc_char4_t c;
  278. char *s;
  279. *nbytes = 1;
  280. s = read_block_form (dtp, nbytes);
  281. if (s == NULL)
  282. return 0;
  283. /* If this is a short read, just return. */
  284. if (*nbytes == 0)
  285. return 0;
  286. c = (uchar) s[0];
  287. if (c < 0x80)
  288. return c;
  289. /* The number of leading 1-bits in the first byte indicates how many
  290. bytes follow. */
  291. for (nb = 2; nb < 7; nb++)
  292. if ((c & ~masks[nb-1]) == patns[nb-1])
  293. goto found;
  294. goto invalid;
  295. found:
  296. c = (c & masks[nb-1]);
  297. nread = nb - 1;
  298. s = read_block_form (dtp, &nread);
  299. if (s == NULL)
  300. return 0;
  301. /* Decode the bytes read. */
  302. for (i = 1; i < nb; i++)
  303. {
  304. gfc_char4_t n = *s++;
  305. if ((n & 0xC0) != 0x80)
  306. goto invalid;
  307. c = ((c << 6) + (n & 0x3F));
  308. }
  309. /* Make sure the shortest possible encoding was used. */
  310. if (c <= 0x7F && nb > 1) goto invalid;
  311. if (c <= 0x7FF && nb > 2) goto invalid;
  312. if (c <= 0xFFFF && nb > 3) goto invalid;
  313. if (c <= 0x1FFFFF && nb > 4) goto invalid;
  314. if (c <= 0x3FFFFFF && nb > 5) goto invalid;
  315. /* Make sure the character is valid. */
  316. if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
  317. goto invalid;
  318. return c;
  319. invalid:
  320. generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
  321. return (gfc_char4_t) '?';
  322. }
  323. static void
  324. read_utf8_char1 (st_parameter_dt *dtp, char *p, int len, int width)
  325. {
  326. gfc_char4_t c;
  327. char *dest;
  328. int nbytes;
  329. int i, j;
  330. len = (width < len) ? len : width;
  331. dest = (char *) p;
  332. /* Proceed with decoding one character at a time. */
  333. for (j = 0; j < len; j++, dest++)
  334. {
  335. c = read_utf8 (dtp, &nbytes);
  336. /* Check for a short read and if so, break out. */
  337. if (nbytes == 0)
  338. break;
  339. *dest = c > 255 ? '?' : (uchar) c;
  340. }
  341. /* If there was a short read, pad the remaining characters. */
  342. for (i = j; i < len; i++)
  343. *dest++ = ' ';
  344. return;
  345. }
  346. static void
  347. read_default_char1 (st_parameter_dt *dtp, char *p, int len, int width)
  348. {
  349. char *s;
  350. int m, n;
  351. s = read_block_form (dtp, &width);
  352. if (s == NULL)
  353. return;
  354. if (width > len)
  355. s += (width - len);
  356. m = (width > len) ? len : width;
  357. memcpy (p, s, m);
  358. n = len - width;
  359. if (n > 0)
  360. memset (p + m, ' ', n);
  361. }
  362. static void
  363. read_utf8_char4 (st_parameter_dt *dtp, void *p, int len, int width)
  364. {
  365. gfc_char4_t *dest;
  366. int nbytes;
  367. int i, j;
  368. len = (width < len) ? len : width;
  369. dest = (gfc_char4_t *) p;
  370. /* Proceed with decoding one character at a time. */
  371. for (j = 0; j < len; j++, dest++)
  372. {
  373. *dest = read_utf8 (dtp, &nbytes);
  374. /* Check for a short read and if so, break out. */
  375. if (nbytes == 0)
  376. break;
  377. }
  378. /* If there was a short read, pad the remaining characters. */
  379. for (i = j; i < len; i++)
  380. *dest++ = (gfc_char4_t) ' ';
  381. return;
  382. }
  383. static void
  384. read_default_char4 (st_parameter_dt *dtp, char *p, int len, int width)
  385. {
  386. int m, n;
  387. gfc_char4_t *dest;
  388. if (is_char4_unit(dtp))
  389. {
  390. gfc_char4_t *s4;
  391. s4 = (gfc_char4_t *) read_block_form4 (dtp, &width);
  392. if (s4 == NULL)
  393. return;
  394. if (width > len)
  395. s4 += (width - len);
  396. m = ((int) width > len) ? len : (int) width;
  397. dest = (gfc_char4_t *) p;
  398. for (n = 0; n < m; n++)
  399. *dest++ = *s4++;
  400. for (n = 0; n < len - (int) width; n++)
  401. *dest++ = (gfc_char4_t) ' ';
  402. }
  403. else
  404. {
  405. char *s;
  406. s = read_block_form (dtp, &width);
  407. if (s == NULL)
  408. return;
  409. if (width > len)
  410. s += (width - len);
  411. m = ((int) width > len) ? len : (int) width;
  412. dest = (gfc_char4_t *) p;
  413. for (n = 0; n < m; n++, dest++, s++)
  414. *dest = (unsigned char ) *s;
  415. for (n = 0; n < len - (int) width; n++, dest++)
  416. *dest = (unsigned char) ' ';
  417. }
  418. }
  419. /* read_a()-- Read a character record into a KIND=1 character destination,
  420. processing UTF-8 encoding if necessary. */
  421. void
  422. read_a (st_parameter_dt *dtp, const fnode *f, char *p, int length)
  423. {
  424. int wi;
  425. int w;
  426. wi = f->u.w;
  427. if (wi == -1) /* '(A)' edit descriptor */
  428. wi = length;
  429. w = wi;
  430. /* Read in w characters, treating comma as not a separator. */
  431. dtp->u.p.sf_read_comma = 0;
  432. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  433. read_utf8_char1 (dtp, p, length, w);
  434. else
  435. read_default_char1 (dtp, p, length, w);
  436. dtp->u.p.sf_read_comma =
  437. dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
  438. }
  439. /* read_a_char4()-- Read a character record into a KIND=4 character destination,
  440. processing UTF-8 encoding if necessary. */
  441. void
  442. read_a_char4 (st_parameter_dt *dtp, const fnode *f, char *p, int length)
  443. {
  444. int w;
  445. w = f->u.w;
  446. if (w == -1) /* '(A)' edit descriptor */
  447. w = length;
  448. /* Read in w characters, treating comma as not a separator. */
  449. dtp->u.p.sf_read_comma = 0;
  450. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  451. read_utf8_char4 (dtp, p, length, w);
  452. else
  453. read_default_char4 (dtp, p, length, w);
  454. dtp->u.p.sf_read_comma =
  455. dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
  456. }
  457. /* eat_leading_spaces()-- Given a character pointer and a width,
  458. * ignore the leading spaces. */
  459. static char *
  460. eat_leading_spaces (int *width, char *p)
  461. {
  462. for (;;)
  463. {
  464. if (*width == 0 || *p != ' ')
  465. break;
  466. (*width)--;
  467. p++;
  468. }
  469. return p;
  470. }
  471. static char
  472. next_char (st_parameter_dt *dtp, char **p, int *w)
  473. {
  474. char c, *q;
  475. if (*w == 0)
  476. return '\0';
  477. q = *p;
  478. c = *q++;
  479. *p = q;
  480. (*w)--;
  481. if (c != ' ')
  482. return c;
  483. if (dtp->u.p.blank_status != BLANK_UNSPECIFIED)
  484. return ' '; /* return a blank to signal a null */
  485. /* At this point, the rest of the field has to be trailing blanks */
  486. while (*w > 0)
  487. {
  488. if (*q++ != ' ')
  489. return '?';
  490. (*w)--;
  491. }
  492. *p = q;
  493. return '\0';
  494. }
  495. /* read_decimal()-- Read a decimal integer value. The values here are
  496. * signed values. */
  497. void
  498. read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
  499. {
  500. GFC_UINTEGER_LARGEST value, maxv, maxv_10;
  501. GFC_INTEGER_LARGEST v;
  502. int w, negative;
  503. char c, *p;
  504. w = f->u.w;
  505. p = read_block_form (dtp, &w);
  506. if (p == NULL)
  507. return;
  508. p = eat_leading_spaces (&w, p);
  509. if (w == 0)
  510. {
  511. set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
  512. return;
  513. }
  514. negative = 0;
  515. switch (*p)
  516. {
  517. case '-':
  518. negative = 1;
  519. /* Fall through */
  520. case '+':
  521. p++;
  522. if (--w == 0)
  523. goto bad;
  524. /* Fall through */
  525. default:
  526. break;
  527. }
  528. maxv = si_max (length);
  529. if (negative)
  530. maxv++;
  531. maxv_10 = maxv / 10;
  532. /* At this point we have a digit-string */
  533. value = 0;
  534. for (;;)
  535. {
  536. c = next_char (dtp, &p, &w);
  537. if (c == '\0')
  538. break;
  539. if (c == ' ')
  540. {
  541. if (dtp->u.p.blank_status == BLANK_NULL)
  542. {
  543. /* Skip spaces. */
  544. for ( ; w > 0; p++, w--)
  545. if (*p != ' ') break;
  546. continue;
  547. }
  548. if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
  549. }
  550. if (c < '0' || c > '9')
  551. goto bad;
  552. if (value > maxv_10)
  553. goto overflow;
  554. c -= '0';
  555. value = 10 * value;
  556. if (value > maxv - c)
  557. goto overflow;
  558. value += c;
  559. }
  560. if (negative)
  561. v = -value;
  562. else
  563. v = value;
  564. set_integer (dest, v, length);
  565. return;
  566. bad:
  567. generate_error (&dtp->common, LIBERROR_READ_VALUE,
  568. "Bad value during integer read");
  569. next_record (dtp, 1);
  570. return;
  571. overflow:
  572. generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
  573. "Value overflowed during integer read");
  574. next_record (dtp, 1);
  575. }
  576. /* read_radix()-- This function reads values for non-decimal radixes.
  577. * The difference here is that we treat the values here as unsigned
  578. * values for the purposes of overflow. If minus sign is present and
  579. * the top bit is set, the value will be incorrect. */
  580. void
  581. read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
  582. int radix)
  583. {
  584. GFC_UINTEGER_LARGEST value, maxv, maxv_r;
  585. GFC_INTEGER_LARGEST v;
  586. int w, negative;
  587. char c, *p;
  588. w = f->u.w;
  589. p = read_block_form (dtp, &w);
  590. if (p == NULL)
  591. return;
  592. p = eat_leading_spaces (&w, p);
  593. if (w == 0)
  594. {
  595. set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
  596. return;
  597. }
  598. /* Maximum unsigned value, assuming two's complement. */
  599. maxv = 2 * si_max (length) + 1;
  600. maxv_r = maxv / radix;
  601. negative = 0;
  602. value = 0;
  603. switch (*p)
  604. {
  605. case '-':
  606. negative = 1;
  607. /* Fall through */
  608. case '+':
  609. p++;
  610. if (--w == 0)
  611. goto bad;
  612. /* Fall through */
  613. default:
  614. break;
  615. }
  616. /* At this point we have a digit-string */
  617. value = 0;
  618. for (;;)
  619. {
  620. c = next_char (dtp, &p, &w);
  621. if (c == '\0')
  622. break;
  623. if (c == ' ')
  624. {
  625. if (dtp->u.p.blank_status == BLANK_NULL) continue;
  626. if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
  627. }
  628. switch (radix)
  629. {
  630. case 2:
  631. if (c < '0' || c > '1')
  632. goto bad;
  633. break;
  634. case 8:
  635. if (c < '0' || c > '7')
  636. goto bad;
  637. break;
  638. case 16:
  639. switch (c)
  640. {
  641. case '0':
  642. case '1':
  643. case '2':
  644. case '3':
  645. case '4':
  646. case '5':
  647. case '6':
  648. case '7':
  649. case '8':
  650. case '9':
  651. break;
  652. case 'a':
  653. case 'b':
  654. case 'c':
  655. case 'd':
  656. case 'e':
  657. case 'f':
  658. c = c - 'a' + '9' + 1;
  659. break;
  660. case 'A':
  661. case 'B':
  662. case 'C':
  663. case 'D':
  664. case 'E':
  665. case 'F':
  666. c = c - 'A' + '9' + 1;
  667. break;
  668. default:
  669. goto bad;
  670. }
  671. break;
  672. }
  673. if (value > maxv_r)
  674. goto overflow;
  675. c -= '0';
  676. value = radix * value;
  677. if (maxv - c < value)
  678. goto overflow;
  679. value += c;
  680. }
  681. v = value;
  682. if (negative)
  683. v = -v;
  684. set_integer (dest, v, length);
  685. return;
  686. bad:
  687. generate_error (&dtp->common, LIBERROR_READ_VALUE,
  688. "Bad value during integer read");
  689. next_record (dtp, 1);
  690. return;
  691. overflow:
  692. generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
  693. "Value overflowed during integer read");
  694. next_record (dtp, 1);
  695. }
  696. /* read_f()-- Read a floating point number with F-style editing, which
  697. is what all of the other floating point descriptors behave as. The
  698. tricky part is that optional spaces are allowed after an E or D,
  699. and the implicit decimal point if a decimal point is not present in
  700. the input. */
  701. void
  702. read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
  703. {
  704. #define READF_TMP 50
  705. char tmp[READF_TMP];
  706. size_t buf_size = 0;
  707. int w, seen_dp, exponent;
  708. int exponent_sign;
  709. const char *p;
  710. char *buffer;
  711. char *out;
  712. int seen_int_digit; /* Seen a digit before the decimal point? */
  713. int seen_dec_digit; /* Seen a digit after the decimal point? */
  714. seen_dp = 0;
  715. seen_int_digit = 0;
  716. seen_dec_digit = 0;
  717. exponent_sign = 1;
  718. exponent = 0;
  719. w = f->u.w;
  720. buffer = tmp;
  721. /* Read in the next block. */
  722. p = read_block_form (dtp, &w);
  723. if (p == NULL)
  724. return;
  725. p = eat_leading_spaces (&w, (char*) p);
  726. if (w == 0)
  727. goto zero;
  728. /* In this buffer we're going to re-format the number cleanly to be parsed
  729. by convert_real in the end; this assures we're using strtod from the
  730. C library for parsing and thus probably get the best accuracy possible.
  731. This process may add a '+0.0' in front of the number as well as change the
  732. exponent because of an implicit decimal point or the like. Thus allocating
  733. strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
  734. original buffer had should be enough. */
  735. buf_size = w + 11;
  736. if (buf_size > READF_TMP)
  737. buffer = xmalloc (buf_size);
  738. out = buffer;
  739. /* Optional sign */
  740. if (*p == '-' || *p == '+')
  741. {
  742. if (*p == '-')
  743. *(out++) = '-';
  744. ++p;
  745. --w;
  746. }
  747. p = eat_leading_spaces (&w, (char*) p);
  748. if (w == 0)
  749. goto zero;
  750. /* Check for Infinity or NaN. */
  751. if (unlikely ((w >= 3 && (*p == 'i' || *p == 'I' || *p == 'n' || *p == 'N'))))
  752. {
  753. int seen_paren = 0;
  754. char *save = out;
  755. /* Scan through the buffer keeping track of spaces and parenthesis. We
  756. null terminate the string as soon as we see a left paren or if we are
  757. BLANK_NULL mode. Leading spaces have already been skipped above,
  758. trailing spaces are ignored by converting to '\0'. A space
  759. between "NaN" and the optional perenthesis is not permitted. */
  760. while (w > 0)
  761. {
  762. *out = tolower (*p);
  763. switch (*p)
  764. {
  765. case ' ':
  766. if (dtp->u.p.blank_status == BLANK_ZERO)
  767. {
  768. *out = '0';
  769. break;
  770. }
  771. *out = '\0';
  772. if (seen_paren == 1)
  773. goto bad_float;
  774. break;
  775. case '(':
  776. seen_paren++;
  777. *out = '\0';
  778. break;
  779. case ')':
  780. if (seen_paren++ != 1)
  781. goto bad_float;
  782. break;
  783. default:
  784. if (!isalnum (*out))
  785. goto bad_float;
  786. }
  787. --w;
  788. ++p;
  789. ++out;
  790. }
  791. *out = '\0';
  792. if (seen_paren != 0 && seen_paren != 2)
  793. goto bad_float;
  794. if ((strcmp (save, "inf") == 0) || (strcmp (save, "infinity") == 0))
  795. {
  796. if (seen_paren)
  797. goto bad_float;
  798. }
  799. else if (strcmp (save, "nan") != 0)
  800. goto bad_float;
  801. convert_infnan (dtp, dest, buffer, length);
  802. if (buf_size > READF_TMP)
  803. free (buffer);
  804. return;
  805. }
  806. /* Process the mantissa string. */
  807. while (w > 0)
  808. {
  809. switch (*p)
  810. {
  811. case ',':
  812. if (dtp->u.p.current_unit->decimal_status != DECIMAL_COMMA)
  813. goto bad_float;
  814. /* Fall through. */
  815. case '.':
  816. if (seen_dp)
  817. goto bad_float;
  818. if (!seen_int_digit)
  819. *(out++) = '0';
  820. *(out++) = '.';
  821. seen_dp = 1;
  822. break;
  823. case ' ':
  824. if (dtp->u.p.blank_status == BLANK_ZERO)
  825. {
  826. *(out++) = '0';
  827. goto found_digit;
  828. }
  829. else if (dtp->u.p.blank_status == BLANK_NULL)
  830. break;
  831. else
  832. /* TODO: Should we check instead that there are only trailing
  833. blanks here, as is done below for exponents? */
  834. goto done;
  835. /* Fall through. */
  836. case '0':
  837. case '1':
  838. case '2':
  839. case '3':
  840. case '4':
  841. case '5':
  842. case '6':
  843. case '7':
  844. case '8':
  845. case '9':
  846. *(out++) = *p;
  847. found_digit:
  848. if (!seen_dp)
  849. seen_int_digit = 1;
  850. else
  851. seen_dec_digit = 1;
  852. break;
  853. case '-':
  854. case '+':
  855. goto exponent;
  856. case 'e':
  857. case 'E':
  858. case 'd':
  859. case 'D':
  860. case 'q':
  861. case 'Q':
  862. ++p;
  863. --w;
  864. goto exponent;
  865. default:
  866. goto bad_float;
  867. }
  868. ++p;
  869. --w;
  870. }
  871. /* No exponent has been seen, so we use the current scale factor. */
  872. exponent = - dtp->u.p.scale_factor;
  873. goto done;
  874. /* At this point the start of an exponent has been found. */
  875. exponent:
  876. p = eat_leading_spaces (&w, (char*) p);
  877. if (*p == '-' || *p == '+')
  878. {
  879. if (*p == '-')
  880. exponent_sign = -1;
  881. ++p;
  882. --w;
  883. }
  884. /* At this point a digit string is required. We calculate the value
  885. of the exponent in order to take account of the scale factor and
  886. the d parameter before explict conversion takes place. */
  887. if (w == 0)
  888. goto bad_float;
  889. if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
  890. {
  891. while (w > 0 && isdigit (*p))
  892. {
  893. exponent *= 10;
  894. exponent += *p - '0';
  895. ++p;
  896. --w;
  897. }
  898. /* Only allow trailing blanks. */
  899. while (w > 0)
  900. {
  901. if (*p != ' ')
  902. goto bad_float;
  903. ++p;
  904. --w;
  905. }
  906. }
  907. else /* BZ or BN status is enabled. */
  908. {
  909. while (w > 0)
  910. {
  911. if (*p == ' ')
  912. {
  913. if (dtp->u.p.blank_status == BLANK_ZERO)
  914. exponent *= 10;
  915. else
  916. assert (dtp->u.p.blank_status == BLANK_NULL);
  917. }
  918. else if (!isdigit (*p))
  919. goto bad_float;
  920. else
  921. {
  922. exponent *= 10;
  923. exponent += *p - '0';
  924. }
  925. ++p;
  926. --w;
  927. }
  928. }
  929. exponent *= exponent_sign;
  930. done:
  931. /* Use the precision specified in the format if no decimal point has been
  932. seen. */
  933. if (!seen_dp)
  934. exponent -= f->u.real.d;
  935. /* Output a trailing '0' after decimal point if not yet found. */
  936. if (seen_dp && !seen_dec_digit)
  937. *(out++) = '0';
  938. /* Handle input of style "E+NN" by inserting a 0 for the
  939. significand. */
  940. else if (!seen_int_digit && !seen_dec_digit)
  941. {
  942. notify_std (&dtp->common, GFC_STD_LEGACY,
  943. "REAL input of style 'E+NN'");
  944. *(out++) = '0';
  945. }
  946. /* Print out the exponent to finish the reformatted number. Maximum 4
  947. digits for the exponent. */
  948. if (exponent != 0)
  949. {
  950. int dig;
  951. *(out++) = 'e';
  952. if (exponent < 0)
  953. {
  954. *(out++) = '-';
  955. exponent = - exponent;
  956. }
  957. if (exponent >= 10000)
  958. goto bad_float;
  959. for (dig = 3; dig >= 0; --dig)
  960. {
  961. out[dig] = (char) ('0' + exponent % 10);
  962. exponent /= 10;
  963. }
  964. out += 4;
  965. }
  966. *(out++) = '\0';
  967. /* Do the actual conversion. */
  968. convert_real (dtp, dest, buffer, length);
  969. if (buf_size > READF_TMP)
  970. free (buffer);
  971. return;
  972. /* The value read is zero. */
  973. zero:
  974. switch (length)
  975. {
  976. case 4:
  977. *((GFC_REAL_4 *) dest) = 0.0;
  978. break;
  979. case 8:
  980. *((GFC_REAL_8 *) dest) = 0.0;
  981. break;
  982. #ifdef HAVE_GFC_REAL_10
  983. case 10:
  984. *((GFC_REAL_10 *) dest) = 0.0;
  985. break;
  986. #endif
  987. #ifdef HAVE_GFC_REAL_16
  988. case 16:
  989. *((GFC_REAL_16 *) dest) = 0.0;
  990. break;
  991. #endif
  992. default:
  993. internal_error (&dtp->common, "Unsupported real kind during IO");
  994. }
  995. return;
  996. bad_float:
  997. if (buf_size > READF_TMP)
  998. free (buffer);
  999. generate_error (&dtp->common, LIBERROR_READ_VALUE,
  1000. "Bad value during floating point read");
  1001. next_record (dtp, 1);
  1002. return;
  1003. }
  1004. /* read_x()-- Deal with the X/TR descriptor. We just read some data
  1005. * and never look at it. */
  1006. void
  1007. read_x (st_parameter_dt *dtp, int n)
  1008. {
  1009. int length, q, q2;
  1010. if ((dtp->u.p.current_unit->pad_status == PAD_NO || is_internal_unit (dtp))
  1011. && dtp->u.p.current_unit->bytes_left < n)
  1012. n = dtp->u.p.current_unit->bytes_left;
  1013. if (n == 0)
  1014. return;
  1015. length = n;
  1016. if (is_internal_unit (dtp))
  1017. {
  1018. mem_alloc_r (dtp->u.p.current_unit->s, &length);
  1019. if (unlikely (length < n))
  1020. n = length;
  1021. goto done;
  1022. }
  1023. if (dtp->u.p.sf_seen_eor)
  1024. return;
  1025. n = 0;
  1026. while (n < length)
  1027. {
  1028. q = fbuf_getc (dtp->u.p.current_unit);
  1029. if (q == EOF)
  1030. break;
  1031. else if (q == '\n' || q == '\r')
  1032. {
  1033. /* Unexpected end of line. Set the position. */
  1034. dtp->u.p.sf_seen_eor = 1;
  1035. /* If we see an EOR during non-advancing I/O, we need to skip
  1036. the rest of the I/O statement. Set the corresponding flag. */
  1037. if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
  1038. dtp->u.p.eor_condition = 1;
  1039. /* If we encounter a CR, it might be a CRLF. */
  1040. if (q == '\r') /* Probably a CRLF */
  1041. {
  1042. /* See if there is an LF. */
  1043. q2 = fbuf_getc (dtp->u.p.current_unit);
  1044. if (q2 == '\n')
  1045. dtp->u.p.sf_seen_eor = 2;
  1046. else if (q2 != EOF) /* Oops, seek back. */
  1047. fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
  1048. }
  1049. goto done;
  1050. }
  1051. n++;
  1052. }
  1053. done:
  1054. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  1055. dtp->u.p.size_used += (GFC_IO_INT) n;
  1056. dtp->u.p.current_unit->bytes_left -= n;
  1057. dtp->u.p.current_unit->strm_pos += (gfc_offset) n;
  1058. }