read.c 27 KB

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