transfer.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. Namelist transfer functions contributed by Paul Thomas
  4. F2003 I/O support contributed by Jerry DeLisle
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* transfer.c -- Top level handling of data transfer statements. */
  22. #include "io.h"
  23. #include "fbuf.h"
  24. #include "format.h"
  25. #include "unix.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. /* Calling conventions: Data transfer statements are unlike other
  31. library calls in that they extend over several calls.
  32. The first call is always a call to st_read() or st_write(). These
  33. subroutines return no status unless a namelist read or write is
  34. being done, in which case there is the usual status. No further
  35. calls are necessary in this case.
  36. For other sorts of data transfer, there are zero or more data
  37. transfer statement that depend on the format of the data transfer
  38. statement. For READ (and for backwards compatibily: for WRITE), one has
  39. transfer_integer
  40. transfer_logical
  41. transfer_character
  42. transfer_character_wide
  43. transfer_real
  44. transfer_complex
  45. transfer_real128
  46. transfer_complex128
  47. and for WRITE
  48. transfer_integer_write
  49. transfer_logical_write
  50. transfer_character_write
  51. transfer_character_wide_write
  52. transfer_real_write
  53. transfer_complex_write
  54. transfer_real128_write
  55. transfer_complex128_write
  56. These subroutines do not return status. The *128 functions
  57. are in the file transfer128.c.
  58. The last call is a call to st_[read|write]_done(). While
  59. something can easily go wrong with the initial st_read() or
  60. st_write(), an error inhibits any data from actually being
  61. transferred. */
  62. extern void transfer_integer (st_parameter_dt *, void *, int);
  63. export_proto(transfer_integer);
  64. extern void transfer_integer_write (st_parameter_dt *, void *, int);
  65. export_proto(transfer_integer_write);
  66. extern void transfer_real (st_parameter_dt *, void *, int);
  67. export_proto(transfer_real);
  68. extern void transfer_real_write (st_parameter_dt *, void *, int);
  69. export_proto(transfer_real_write);
  70. extern void transfer_logical (st_parameter_dt *, void *, int);
  71. export_proto(transfer_logical);
  72. extern void transfer_logical_write (st_parameter_dt *, void *, int);
  73. export_proto(transfer_logical_write);
  74. extern void transfer_character (st_parameter_dt *, void *, int);
  75. export_proto(transfer_character);
  76. extern void transfer_character_write (st_parameter_dt *, void *, int);
  77. export_proto(transfer_character_write);
  78. extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
  79. export_proto(transfer_character_wide);
  80. extern void transfer_character_wide_write (st_parameter_dt *,
  81. void *, int, int);
  82. export_proto(transfer_character_wide_write);
  83. extern void transfer_complex (st_parameter_dt *, void *, int);
  84. export_proto(transfer_complex);
  85. extern void transfer_complex_write (st_parameter_dt *, void *, int);
  86. export_proto(transfer_complex_write);
  87. extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
  88. gfc_charlen_type);
  89. export_proto(transfer_array);
  90. extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
  91. gfc_charlen_type);
  92. export_proto(transfer_array_write);
  93. static void us_read (st_parameter_dt *, int);
  94. static void us_write (st_parameter_dt *, int);
  95. static void next_record_r_unf (st_parameter_dt *, int);
  96. static void next_record_w_unf (st_parameter_dt *, int);
  97. static const st_option advance_opt[] = {
  98. {"yes", ADVANCE_YES},
  99. {"no", ADVANCE_NO},
  100. {NULL, 0}
  101. };
  102. static const st_option decimal_opt[] = {
  103. {"point", DECIMAL_POINT},
  104. {"comma", DECIMAL_COMMA},
  105. {NULL, 0}
  106. };
  107. static const st_option round_opt[] = {
  108. {"up", ROUND_UP},
  109. {"down", ROUND_DOWN},
  110. {"zero", ROUND_ZERO},
  111. {"nearest", ROUND_NEAREST},
  112. {"compatible", ROUND_COMPATIBLE},
  113. {"processor_defined", ROUND_PROCDEFINED},
  114. {NULL, 0}
  115. };
  116. static const st_option sign_opt[] = {
  117. {"plus", SIGN_SP},
  118. {"suppress", SIGN_SS},
  119. {"processor_defined", SIGN_S},
  120. {NULL, 0}
  121. };
  122. static const st_option blank_opt[] = {
  123. {"null", BLANK_NULL},
  124. {"zero", BLANK_ZERO},
  125. {NULL, 0}
  126. };
  127. static const st_option delim_opt[] = {
  128. {"apostrophe", DELIM_APOSTROPHE},
  129. {"quote", DELIM_QUOTE},
  130. {"none", DELIM_NONE},
  131. {NULL, 0}
  132. };
  133. static const st_option pad_opt[] = {
  134. {"yes", PAD_YES},
  135. {"no", PAD_NO},
  136. {NULL, 0}
  137. };
  138. typedef enum
  139. { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
  140. FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
  141. }
  142. file_mode;
  143. static file_mode
  144. current_mode (st_parameter_dt *dtp)
  145. {
  146. file_mode m;
  147. m = FORM_UNSPECIFIED;
  148. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
  149. {
  150. m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
  151. FORMATTED_DIRECT : UNFORMATTED_DIRECT;
  152. }
  153. else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
  154. {
  155. m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
  156. FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
  157. }
  158. else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
  159. {
  160. m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
  161. FORMATTED_STREAM : UNFORMATTED_STREAM;
  162. }
  163. return m;
  164. }
  165. /* Mid level data transfer statements. */
  166. /* Read sequential file - internal unit */
  167. static char *
  168. read_sf_internal (st_parameter_dt *dtp, int * length)
  169. {
  170. static char *empty_string[0];
  171. char *base;
  172. int lorig;
  173. /* Zero size array gives internal unit len of 0. Nothing to read. */
  174. if (dtp->internal_unit_len == 0
  175. && dtp->u.p.current_unit->pad_status == PAD_NO)
  176. hit_eof (dtp);
  177. /* If we have seen an eor previously, return a length of 0. The
  178. caller is responsible for correctly padding the input field. */
  179. if (dtp->u.p.sf_seen_eor)
  180. {
  181. *length = 0;
  182. /* Just return something that isn't a NULL pointer, otherwise the
  183. caller thinks an error occurred. */
  184. return (char*) empty_string;
  185. }
  186. lorig = *length;
  187. if (is_char4_unit(dtp))
  188. {
  189. int i;
  190. gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
  191. length);
  192. base = fbuf_alloc (dtp->u.p.current_unit, lorig);
  193. for (i = 0; i < *length; i++, p++)
  194. base[i] = *p > 255 ? '?' : (unsigned char) *p;
  195. }
  196. else
  197. base = mem_alloc_r (dtp->u.p.current_unit->s, length);
  198. if (unlikely (lorig > *length))
  199. {
  200. hit_eof (dtp);
  201. return NULL;
  202. }
  203. dtp->u.p.current_unit->bytes_left -= *length;
  204. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  205. dtp->u.p.size_used += (GFC_IO_INT) *length;
  206. return base;
  207. }
  208. /* When reading sequential formatted records we have a problem. We
  209. don't know how long the line is until we read the trailing newline,
  210. and we don't want to read too much. If we read too much, we might
  211. have to do a physical seek backwards depending on how much data is
  212. present, and devices like terminals aren't seekable and would cause
  213. an I/O error.
  214. Given this, the solution is to read a byte at a time, stopping if
  215. we hit the newline. For small allocations, we use a static buffer.
  216. For larger allocations, we are forced to allocate memory on the
  217. heap. Hopefully this won't happen very often. */
  218. /* Read sequential file - external unit */
  219. static char *
  220. read_sf (st_parameter_dt *dtp, int * length)
  221. {
  222. static char *empty_string[0];
  223. int q, q2;
  224. int n, lorig, seen_comma;
  225. /* If we have seen an eor previously, return a length of 0. The
  226. caller is responsible for correctly padding the input field. */
  227. if (dtp->u.p.sf_seen_eor)
  228. {
  229. *length = 0;
  230. /* Just return something that isn't a NULL pointer, otherwise the
  231. caller thinks an error occurred. */
  232. return (char*) empty_string;
  233. }
  234. n = seen_comma = 0;
  235. /* Read data into format buffer and scan through it. */
  236. lorig = *length;
  237. while (n < *length)
  238. {
  239. q = fbuf_getc (dtp->u.p.current_unit);
  240. if (q == EOF)
  241. break;
  242. else if (q == '\n' || q == '\r')
  243. {
  244. /* Unexpected end of line. Set the position. */
  245. dtp->u.p.sf_seen_eor = 1;
  246. /* If we see an EOR during non-advancing I/O, we need to skip
  247. the rest of the I/O statement. Set the corresponding flag. */
  248. if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
  249. dtp->u.p.eor_condition = 1;
  250. /* If we encounter a CR, it might be a CRLF. */
  251. if (q == '\r') /* Probably a CRLF */
  252. {
  253. /* See if there is an LF. */
  254. q2 = fbuf_getc (dtp->u.p.current_unit);
  255. if (q2 == '\n')
  256. dtp->u.p.sf_seen_eor = 2;
  257. else if (q2 != EOF) /* Oops, seek back. */
  258. fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
  259. }
  260. /* Without padding, terminate the I/O statement without assigning
  261. the value. With padding, the value still needs to be assigned,
  262. so we can just continue with a short read. */
  263. if (dtp->u.p.current_unit->pad_status == PAD_NO)
  264. {
  265. generate_error (&dtp->common, LIBERROR_EOR, NULL);
  266. return NULL;
  267. }
  268. *length = n;
  269. goto done;
  270. }
  271. /* Short circuit the read if a comma is found during numeric input.
  272. The flag is set to zero during character reads so that commas in
  273. strings are not ignored */
  274. else if (q == ',')
  275. if (dtp->u.p.sf_read_comma == 1)
  276. {
  277. seen_comma = 1;
  278. notify_std (&dtp->common, GFC_STD_GNU,
  279. "Comma in formatted numeric read.");
  280. break;
  281. }
  282. n++;
  283. }
  284. *length = n;
  285. /* A short read implies we hit EOF, unless we hit EOR, a comma, or
  286. some other stuff. Set the relevant flags. */
  287. if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
  288. {
  289. if (n > 0)
  290. {
  291. if (dtp->u.p.advance_status == ADVANCE_NO)
  292. {
  293. if (dtp->u.p.current_unit->pad_status == PAD_NO)
  294. {
  295. hit_eof (dtp);
  296. return NULL;
  297. }
  298. else
  299. dtp->u.p.eor_condition = 1;
  300. }
  301. else
  302. dtp->u.p.at_eof = 1;
  303. }
  304. else if (dtp->u.p.advance_status == ADVANCE_NO
  305. || dtp->u.p.current_unit->pad_status == PAD_NO
  306. || dtp->u.p.current_unit->bytes_left
  307. == dtp->u.p.current_unit->recl)
  308. {
  309. hit_eof (dtp);
  310. return NULL;
  311. }
  312. }
  313. done:
  314. dtp->u.p.current_unit->bytes_left -= n;
  315. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  316. dtp->u.p.size_used += (GFC_IO_INT) n;
  317. /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
  318. fbuf_getc might reallocate the buffer. So return current pointer
  319. minus all the advances, which is n plus up to two characters
  320. of newline or comma. */
  321. return fbuf_getptr (dtp->u.p.current_unit)
  322. - n - dtp->u.p.sf_seen_eor - seen_comma;
  323. }
  324. /* Function for reading the next couple of bytes from the current
  325. file, advancing the current position. We return NULL on end of record or
  326. end of file. This function is only for formatted I/O, unformatted uses
  327. read_block_direct.
  328. If the read is short, then it is because the current record does not
  329. have enough data to satisfy the read request and the file was
  330. opened with PAD=YES. The caller must assume tailing spaces for
  331. short reads. */
  332. void *
  333. read_block_form (st_parameter_dt *dtp, int * nbytes)
  334. {
  335. char *source;
  336. int norig;
  337. if (!is_stream_io (dtp))
  338. {
  339. if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
  340. {
  341. /* For preconnected units with default record length, set bytes left
  342. to unit record length and proceed, otherwise error. */
  343. if (dtp->u.p.current_unit->unit_number == options.stdin_unit
  344. && dtp->u.p.current_unit->recl == DEFAULT_RECL)
  345. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  346. else
  347. {
  348. if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
  349. && !is_internal_unit (dtp))
  350. {
  351. /* Not enough data left. */
  352. generate_error (&dtp->common, LIBERROR_EOR, NULL);
  353. return NULL;
  354. }
  355. }
  356. if (unlikely (dtp->u.p.current_unit->bytes_left == 0
  357. && !is_internal_unit(dtp)))
  358. {
  359. hit_eof (dtp);
  360. return NULL;
  361. }
  362. *nbytes = dtp->u.p.current_unit->bytes_left;
  363. }
  364. }
  365. if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
  366. (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
  367. dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
  368. {
  369. if (is_internal_unit (dtp))
  370. source = read_sf_internal (dtp, nbytes);
  371. else
  372. source = read_sf (dtp, nbytes);
  373. dtp->u.p.current_unit->strm_pos +=
  374. (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
  375. return source;
  376. }
  377. /* If we reach here, we can assume it's direct access. */
  378. dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
  379. norig = *nbytes;
  380. source = fbuf_read (dtp->u.p.current_unit, nbytes);
  381. fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
  382. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  383. dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
  384. if (norig != *nbytes)
  385. {
  386. /* Short read, this shouldn't happen. */
  387. if (dtp->u.p.current_unit->pad_status == PAD_NO)
  388. {
  389. generate_error (&dtp->common, LIBERROR_EOR, NULL);
  390. source = NULL;
  391. }
  392. }
  393. dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
  394. return source;
  395. }
  396. /* Read a block from a character(kind=4) internal unit, to be transferred into
  397. a character(kind=4) variable. Note: Portions of this code borrowed from
  398. read_sf_internal. */
  399. void *
  400. read_block_form4 (st_parameter_dt *dtp, int * nbytes)
  401. {
  402. static gfc_char4_t *empty_string[0];
  403. gfc_char4_t *source;
  404. int lorig;
  405. if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
  406. *nbytes = dtp->u.p.current_unit->bytes_left;
  407. /* Zero size array gives internal unit len of 0. Nothing to read. */
  408. if (dtp->internal_unit_len == 0
  409. && dtp->u.p.current_unit->pad_status == PAD_NO)
  410. hit_eof (dtp);
  411. /* If we have seen an eor previously, return a length of 0. The
  412. caller is responsible for correctly padding the input field. */
  413. if (dtp->u.p.sf_seen_eor)
  414. {
  415. *nbytes = 0;
  416. /* Just return something that isn't a NULL pointer, otherwise the
  417. caller thinks an error occurred. */
  418. return empty_string;
  419. }
  420. lorig = *nbytes;
  421. source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
  422. if (unlikely (lorig > *nbytes))
  423. {
  424. hit_eof (dtp);
  425. return NULL;
  426. }
  427. dtp->u.p.current_unit->bytes_left -= *nbytes;
  428. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  429. dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
  430. return source;
  431. }
  432. /* Reads a block directly into application data space. This is for
  433. unformatted files. */
  434. static void
  435. read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
  436. {
  437. ssize_t to_read_record;
  438. ssize_t have_read_record;
  439. ssize_t to_read_subrecord;
  440. ssize_t have_read_subrecord;
  441. int short_record;
  442. if (is_stream_io (dtp))
  443. {
  444. have_read_record = sread (dtp->u.p.current_unit->s, buf,
  445. nbytes);
  446. if (unlikely (have_read_record < 0))
  447. {
  448. generate_error (&dtp->common, LIBERROR_OS, NULL);
  449. return;
  450. }
  451. dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
  452. if (unlikely ((ssize_t) nbytes != have_read_record))
  453. {
  454. /* Short read, e.g. if we hit EOF. For stream files,
  455. we have to set the end-of-file condition. */
  456. hit_eof (dtp);
  457. }
  458. return;
  459. }
  460. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
  461. {
  462. if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
  463. {
  464. short_record = 1;
  465. to_read_record = dtp->u.p.current_unit->bytes_left;
  466. nbytes = to_read_record;
  467. }
  468. else
  469. {
  470. short_record = 0;
  471. to_read_record = nbytes;
  472. }
  473. dtp->u.p.current_unit->bytes_left -= to_read_record;
  474. to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
  475. if (unlikely (to_read_record < 0))
  476. {
  477. generate_error (&dtp->common, LIBERROR_OS, NULL);
  478. return;
  479. }
  480. if (to_read_record != (ssize_t) nbytes)
  481. {
  482. /* Short read, e.g. if we hit EOF. Apparently, we read
  483. more than was written to the last record. */
  484. return;
  485. }
  486. if (unlikely (short_record))
  487. {
  488. generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
  489. }
  490. return;
  491. }
  492. /* Unformatted sequential. We loop over the subrecords, reading
  493. until the request has been fulfilled or the record has run out
  494. of continuation subrecords. */
  495. /* Check whether we exceed the total record length. */
  496. if (dtp->u.p.current_unit->flags.has_recl
  497. && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
  498. {
  499. to_read_record = dtp->u.p.current_unit->bytes_left;
  500. short_record = 1;
  501. }
  502. else
  503. {
  504. to_read_record = nbytes;
  505. short_record = 0;
  506. }
  507. have_read_record = 0;
  508. while(1)
  509. {
  510. if (dtp->u.p.current_unit->bytes_left_subrecord
  511. < (gfc_offset) to_read_record)
  512. {
  513. to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
  514. to_read_record -= to_read_subrecord;
  515. }
  516. else
  517. {
  518. to_read_subrecord = to_read_record;
  519. to_read_record = 0;
  520. }
  521. dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
  522. have_read_subrecord = sread (dtp->u.p.current_unit->s,
  523. buf + have_read_record, to_read_subrecord);
  524. if (unlikely (have_read_subrecord < 0))
  525. {
  526. generate_error (&dtp->common, LIBERROR_OS, NULL);
  527. return;
  528. }
  529. have_read_record += have_read_subrecord;
  530. if (unlikely (to_read_subrecord != have_read_subrecord))
  531. {
  532. /* Short read, e.g. if we hit EOF. This means the record
  533. structure has been corrupted, or the trailing record
  534. marker would still be present. */
  535. generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
  536. return;
  537. }
  538. if (to_read_record > 0)
  539. {
  540. if (likely (dtp->u.p.current_unit->continued))
  541. {
  542. next_record_r_unf (dtp, 0);
  543. us_read (dtp, 1);
  544. }
  545. else
  546. {
  547. /* Let's make sure the file position is correctly pre-positioned
  548. for the next read statement. */
  549. dtp->u.p.current_unit->current_record = 0;
  550. next_record_r_unf (dtp, 0);
  551. generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
  552. return;
  553. }
  554. }
  555. else
  556. {
  557. /* Normal exit, the read request has been fulfilled. */
  558. break;
  559. }
  560. }
  561. dtp->u.p.current_unit->bytes_left -= have_read_record;
  562. if (unlikely (short_record))
  563. {
  564. generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
  565. return;
  566. }
  567. return;
  568. }
  569. /* Function for writing a block of bytes to the current file at the
  570. current position, advancing the file pointer. We are given a length
  571. and return a pointer to a buffer that the caller must (completely)
  572. fill in. Returns NULL on error. */
  573. void *
  574. write_block (st_parameter_dt *dtp, int length)
  575. {
  576. char *dest;
  577. if (!is_stream_io (dtp))
  578. {
  579. if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
  580. {
  581. /* For preconnected units with default record length, set bytes left
  582. to unit record length and proceed, otherwise error. */
  583. if (likely ((dtp->u.p.current_unit->unit_number
  584. == options.stdout_unit
  585. || dtp->u.p.current_unit->unit_number
  586. == options.stderr_unit)
  587. && dtp->u.p.current_unit->recl == DEFAULT_RECL))
  588. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  589. else
  590. {
  591. generate_error (&dtp->common, LIBERROR_EOR, NULL);
  592. return NULL;
  593. }
  594. }
  595. dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
  596. }
  597. if (is_internal_unit (dtp))
  598. {
  599. if (dtp->common.unit) /* char4 internel unit. */
  600. {
  601. gfc_char4_t *dest4;
  602. dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
  603. if (dest4 == NULL)
  604. {
  605. generate_error (&dtp->common, LIBERROR_END, NULL);
  606. return NULL;
  607. }
  608. return dest4;
  609. }
  610. else
  611. dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
  612. if (dest == NULL)
  613. {
  614. generate_error (&dtp->common, LIBERROR_END, NULL);
  615. return NULL;
  616. }
  617. if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
  618. generate_error (&dtp->common, LIBERROR_END, NULL);
  619. }
  620. else
  621. {
  622. dest = fbuf_alloc (dtp->u.p.current_unit, length);
  623. if (dest == NULL)
  624. {
  625. generate_error (&dtp->common, LIBERROR_OS, NULL);
  626. return NULL;
  627. }
  628. }
  629. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  630. dtp->u.p.size_used += (GFC_IO_INT) length;
  631. dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
  632. return dest;
  633. }
  634. /* High level interface to swrite(), taking care of errors. This is only
  635. called for unformatted files. There are three cases to consider:
  636. Stream I/O, unformatted direct, unformatted sequential. */
  637. static bool
  638. write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
  639. {
  640. ssize_t have_written;
  641. ssize_t to_write_subrecord;
  642. int short_record;
  643. /* Stream I/O. */
  644. if (is_stream_io (dtp))
  645. {
  646. have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
  647. if (unlikely (have_written < 0))
  648. {
  649. generate_error (&dtp->common, LIBERROR_OS, NULL);
  650. return false;
  651. }
  652. dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
  653. return true;
  654. }
  655. /* Unformatted direct access. */
  656. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
  657. {
  658. if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
  659. {
  660. generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
  661. return false;
  662. }
  663. if (buf == NULL && nbytes == 0)
  664. return true;
  665. have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
  666. if (unlikely (have_written < 0))
  667. {
  668. generate_error (&dtp->common, LIBERROR_OS, NULL);
  669. return false;
  670. }
  671. dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
  672. dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
  673. return true;
  674. }
  675. /* Unformatted sequential. */
  676. have_written = 0;
  677. if (dtp->u.p.current_unit->flags.has_recl
  678. && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
  679. {
  680. nbytes = dtp->u.p.current_unit->bytes_left;
  681. short_record = 1;
  682. }
  683. else
  684. {
  685. short_record = 0;
  686. }
  687. while (1)
  688. {
  689. to_write_subrecord =
  690. (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
  691. (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
  692. dtp->u.p.current_unit->bytes_left_subrecord -=
  693. (gfc_offset) to_write_subrecord;
  694. to_write_subrecord = swrite (dtp->u.p.current_unit->s,
  695. buf + have_written, to_write_subrecord);
  696. if (unlikely (to_write_subrecord < 0))
  697. {
  698. generate_error (&dtp->common, LIBERROR_OS, NULL);
  699. return false;
  700. }
  701. dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
  702. nbytes -= to_write_subrecord;
  703. have_written += to_write_subrecord;
  704. if (nbytes == 0)
  705. break;
  706. next_record_w_unf (dtp, 1);
  707. us_write (dtp, 1);
  708. }
  709. dtp->u.p.current_unit->bytes_left -= have_written;
  710. if (unlikely (short_record))
  711. {
  712. generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
  713. return false;
  714. }
  715. return true;
  716. }
  717. /* Reverse memcpy - used for byte swapping. */
  718. static void
  719. reverse_memcpy (void *dest, const void *src, size_t n)
  720. {
  721. char *d, *s;
  722. size_t i;
  723. d = (char *) dest;
  724. s = (char *) src + n - 1;
  725. /* Write with ascending order - this is likely faster
  726. on modern architectures because of write combining. */
  727. for (i=0; i<n; i++)
  728. *(d++) = *(s--);
  729. }
  730. /* Utility function for byteswapping an array, using the bswap
  731. builtins if possible. dest and src can overlap completely, or then
  732. they must point to separate objects; partial overlaps are not
  733. allowed. */
  734. static void
  735. bswap_array (void *dest, const void *src, size_t size, size_t nelems)
  736. {
  737. const char *ps;
  738. char *pd;
  739. switch (size)
  740. {
  741. case 1:
  742. break;
  743. case 2:
  744. for (size_t i = 0; i < nelems; i++)
  745. ((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]);
  746. break;
  747. case 4:
  748. for (size_t i = 0; i < nelems; i++)
  749. ((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]);
  750. break;
  751. case 8:
  752. for (size_t i = 0; i < nelems; i++)
  753. ((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]);
  754. break;
  755. case 12:
  756. ps = src;
  757. pd = dest;
  758. for (size_t i = 0; i < nelems; i++)
  759. {
  760. uint32_t tmp;
  761. memcpy (&tmp, ps, 4);
  762. *(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8));
  763. *(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4));
  764. *(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp);
  765. ps += size;
  766. pd += size;
  767. }
  768. break;
  769. case 16:
  770. ps = src;
  771. pd = dest;
  772. for (size_t i = 0; i < nelems; i++)
  773. {
  774. uint64_t tmp;
  775. memcpy (&tmp, ps, 8);
  776. *(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8));
  777. *(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp);
  778. ps += size;
  779. pd += size;
  780. }
  781. break;
  782. default:
  783. pd = dest;
  784. if (dest != src)
  785. {
  786. ps = src;
  787. for (size_t i = 0; i < nelems; i++)
  788. {
  789. reverse_memcpy (pd, ps, size);
  790. ps += size;
  791. pd += size;
  792. }
  793. }
  794. else
  795. {
  796. /* In-place byte swap. */
  797. for (size_t i = 0; i < nelems; i++)
  798. {
  799. char tmp, *low = pd, *high = pd + size - 1;
  800. for (size_t j = 0; j < size/2; j++)
  801. {
  802. tmp = *low;
  803. *low = *high;
  804. *high = tmp;
  805. low++;
  806. high--;
  807. }
  808. pd += size;
  809. }
  810. }
  811. }
  812. }
  813. /* Master function for unformatted reads. */
  814. static void
  815. unformatted_read (st_parameter_dt *dtp, bt type,
  816. void *dest, int kind, size_t size, size_t nelems)
  817. {
  818. if (type == BT_CHARACTER)
  819. size *= GFC_SIZE_OF_CHAR_KIND(kind);
  820. read_block_direct (dtp, dest, size * nelems);
  821. if (unlikely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_SWAP)
  822. && kind != 1)
  823. {
  824. /* Handle wide chracters. */
  825. if (type == BT_CHARACTER)
  826. {
  827. nelems *= size;
  828. size = kind;
  829. }
  830. /* Break up complex into its constituent reals. */
  831. else if (type == BT_COMPLEX)
  832. {
  833. nelems *= 2;
  834. size /= 2;
  835. }
  836. bswap_array (dest, dest, size, nelems);
  837. }
  838. }
  839. /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
  840. bytes on 64 bit machines. The unused bytes are not initialized and never
  841. used, which can show an error with memory checking analyzers like
  842. valgrind. */
  843. static void
  844. unformatted_write (st_parameter_dt *dtp, bt type,
  845. void *source, int kind, size_t size, size_t nelems)
  846. {
  847. if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
  848. || kind == 1)
  849. {
  850. size_t stride = type == BT_CHARACTER ?
  851. size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
  852. write_buf (dtp, source, stride * nelems);
  853. }
  854. else
  855. {
  856. #define BSWAP_BUFSZ 512
  857. char buffer[BSWAP_BUFSZ];
  858. char *p;
  859. size_t nrem;
  860. p = source;
  861. /* Handle wide chracters. */
  862. if (type == BT_CHARACTER && kind != 1)
  863. {
  864. nelems *= size;
  865. size = kind;
  866. }
  867. /* Break up complex into its constituent reals. */
  868. if (type == BT_COMPLEX)
  869. {
  870. nelems *= 2;
  871. size /= 2;
  872. }
  873. /* By now, all complex variables have been split into their
  874. constituent reals. */
  875. nrem = nelems;
  876. do
  877. {
  878. size_t nc;
  879. if (size * nrem > BSWAP_BUFSZ)
  880. nc = BSWAP_BUFSZ / size;
  881. else
  882. nc = nrem;
  883. bswap_array (buffer, p, size, nc);
  884. write_buf (dtp, buffer, size * nc);
  885. p += size * nc;
  886. nrem -= nc;
  887. }
  888. while (nrem > 0);
  889. }
  890. }
  891. /* Return a pointer to the name of a type. */
  892. const char *
  893. type_name (bt type)
  894. {
  895. const char *p;
  896. switch (type)
  897. {
  898. case BT_INTEGER:
  899. p = "INTEGER";
  900. break;
  901. case BT_LOGICAL:
  902. p = "LOGICAL";
  903. break;
  904. case BT_CHARACTER:
  905. p = "CHARACTER";
  906. break;
  907. case BT_REAL:
  908. p = "REAL";
  909. break;
  910. case BT_COMPLEX:
  911. p = "COMPLEX";
  912. break;
  913. default:
  914. internal_error (NULL, "type_name(): Bad type");
  915. }
  916. return p;
  917. }
  918. /* Write a constant string to the output.
  919. This is complicated because the string can have doubled delimiters
  920. in it. The length in the format node is the true length. */
  921. static void
  922. write_constant_string (st_parameter_dt *dtp, const fnode *f)
  923. {
  924. char c, delimiter, *p, *q;
  925. int length;
  926. length = f->u.string.length;
  927. if (length == 0)
  928. return;
  929. p = write_block (dtp, length);
  930. if (p == NULL)
  931. return;
  932. q = f->u.string.p;
  933. delimiter = q[-1];
  934. for (; length > 0; length--)
  935. {
  936. c = *p++ = *q++;
  937. if (c == delimiter && c != 'H' && c != 'h')
  938. q++; /* Skip the doubled delimiter. */
  939. }
  940. }
  941. /* Given actual and expected types in a formatted data transfer, make
  942. sure they agree. If not, an error message is generated. Returns
  943. nonzero if something went wrong. */
  944. static int
  945. require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
  946. {
  947. #define BUFLEN 100
  948. char buffer[BUFLEN];
  949. if (actual == expected)
  950. return 0;
  951. /* Adjust item_count before emitting error message. */
  952. snprintf (buffer, BUFLEN,
  953. "Expected %s for item %d in formatted transfer, got %s",
  954. type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
  955. format_error (dtp, f, buffer);
  956. return 1;
  957. }
  958. static int
  959. require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f)
  960. {
  961. #define BUFLEN 100
  962. char buffer[BUFLEN];
  963. if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX)
  964. return 0;
  965. /* Adjust item_count before emitting error message. */
  966. snprintf (buffer, BUFLEN,
  967. "Expected numeric type for item %d in formatted transfer, got %s",
  968. dtp->u.p.item_count - 1, type_name (actual));
  969. format_error (dtp, f, buffer);
  970. return 1;
  971. }
  972. /* This function is in the main loop for a formatted data transfer
  973. statement. It would be natural to implement this as a coroutine
  974. with the user program, but C makes that awkward. We loop,
  975. processing format elements. When we actually have to transfer
  976. data instead of just setting flags, we return control to the user
  977. program which calls a function that supplies the address and type
  978. of the next element, then comes back here to process it. */
  979. static void
  980. formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
  981. size_t size)
  982. {
  983. int pos, bytes_used;
  984. const fnode *f;
  985. format_token t;
  986. int n;
  987. int consume_data_flag;
  988. /* Change a complex data item into a pair of reals. */
  989. n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
  990. if (type == BT_COMPLEX)
  991. {
  992. type = BT_REAL;
  993. size /= 2;
  994. }
  995. /* If there's an EOR condition, we simulate finalizing the transfer
  996. by doing nothing. */
  997. if (dtp->u.p.eor_condition)
  998. return;
  999. /* Set this flag so that commas in reads cause the read to complete before
  1000. the entire field has been read. The next read field will start right after
  1001. the comma in the stream. (Set to 0 for character reads). */
  1002. dtp->u.p.sf_read_comma =
  1003. dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
  1004. for (;;)
  1005. {
  1006. /* If reversion has occurred and there is another real data item,
  1007. then we have to move to the next record. */
  1008. if (dtp->u.p.reversion_flag && n > 0)
  1009. {
  1010. dtp->u.p.reversion_flag = 0;
  1011. next_record (dtp, 0);
  1012. }
  1013. consume_data_flag = 1;
  1014. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1015. break;
  1016. f = next_format (dtp);
  1017. if (f == NULL)
  1018. {
  1019. /* No data descriptors left. */
  1020. if (unlikely (n > 0))
  1021. generate_error (&dtp->common, LIBERROR_FORMAT,
  1022. "Insufficient data descriptors in format after reversion");
  1023. return;
  1024. }
  1025. t = f->format;
  1026. bytes_used = (int)(dtp->u.p.current_unit->recl
  1027. - dtp->u.p.current_unit->bytes_left);
  1028. if (is_stream_io(dtp))
  1029. bytes_used = 0;
  1030. switch (t)
  1031. {
  1032. case FMT_I:
  1033. if (n == 0)
  1034. goto need_read_data;
  1035. if (require_type (dtp, BT_INTEGER, type, f))
  1036. return;
  1037. read_decimal (dtp, f, p, kind);
  1038. break;
  1039. case FMT_B:
  1040. if (n == 0)
  1041. goto need_read_data;
  1042. if (!(compile_options.allow_std & GFC_STD_GNU)
  1043. && require_numeric_type (dtp, type, f))
  1044. return;
  1045. if (!(compile_options.allow_std & GFC_STD_F2008)
  1046. && require_type (dtp, BT_INTEGER, type, f))
  1047. return;
  1048. read_radix (dtp, f, p, kind, 2);
  1049. break;
  1050. case FMT_O:
  1051. if (n == 0)
  1052. goto need_read_data;
  1053. if (!(compile_options.allow_std & GFC_STD_GNU)
  1054. && require_numeric_type (dtp, type, f))
  1055. return;
  1056. if (!(compile_options.allow_std & GFC_STD_F2008)
  1057. && require_type (dtp, BT_INTEGER, type, f))
  1058. return;
  1059. read_radix (dtp, f, p, kind, 8);
  1060. break;
  1061. case FMT_Z:
  1062. if (n == 0)
  1063. goto need_read_data;
  1064. if (!(compile_options.allow_std & GFC_STD_GNU)
  1065. && require_numeric_type (dtp, type, f))
  1066. return;
  1067. if (!(compile_options.allow_std & GFC_STD_F2008)
  1068. && require_type (dtp, BT_INTEGER, type, f))
  1069. return;
  1070. read_radix (dtp, f, p, kind, 16);
  1071. break;
  1072. case FMT_A:
  1073. if (n == 0)
  1074. goto need_read_data;
  1075. /* It is possible to have FMT_A with something not BT_CHARACTER such
  1076. as when writing out hollerith strings, so check both type
  1077. and kind before calling wide character routines. */
  1078. if (type == BT_CHARACTER && kind == 4)
  1079. read_a_char4 (dtp, f, p, size);
  1080. else
  1081. read_a (dtp, f, p, size);
  1082. break;
  1083. case FMT_L:
  1084. if (n == 0)
  1085. goto need_read_data;
  1086. read_l (dtp, f, p, kind);
  1087. break;
  1088. case FMT_D:
  1089. if (n == 0)
  1090. goto need_read_data;
  1091. if (require_type (dtp, BT_REAL, type, f))
  1092. return;
  1093. read_f (dtp, f, p, kind);
  1094. break;
  1095. case FMT_E:
  1096. if (n == 0)
  1097. goto need_read_data;
  1098. if (require_type (dtp, BT_REAL, type, f))
  1099. return;
  1100. read_f (dtp, f, p, kind);
  1101. break;
  1102. case FMT_EN:
  1103. if (n == 0)
  1104. goto need_read_data;
  1105. if (require_type (dtp, BT_REAL, type, f))
  1106. return;
  1107. read_f (dtp, f, p, kind);
  1108. break;
  1109. case FMT_ES:
  1110. if (n == 0)
  1111. goto need_read_data;
  1112. if (require_type (dtp, BT_REAL, type, f))
  1113. return;
  1114. read_f (dtp, f, p, kind);
  1115. break;
  1116. case FMT_F:
  1117. if (n == 0)
  1118. goto need_read_data;
  1119. if (require_type (dtp, BT_REAL, type, f))
  1120. return;
  1121. read_f (dtp, f, p, kind);
  1122. break;
  1123. case FMT_G:
  1124. if (n == 0)
  1125. goto need_read_data;
  1126. switch (type)
  1127. {
  1128. case BT_INTEGER:
  1129. read_decimal (dtp, f, p, kind);
  1130. break;
  1131. case BT_LOGICAL:
  1132. read_l (dtp, f, p, kind);
  1133. break;
  1134. case BT_CHARACTER:
  1135. if (kind == 4)
  1136. read_a_char4 (dtp, f, p, size);
  1137. else
  1138. read_a (dtp, f, p, size);
  1139. break;
  1140. case BT_REAL:
  1141. read_f (dtp, f, p, kind);
  1142. break;
  1143. default:
  1144. internal_error (&dtp->common, "formatted_transfer(): Bad type");
  1145. }
  1146. break;
  1147. case FMT_STRING:
  1148. consume_data_flag = 0;
  1149. format_error (dtp, f, "Constant string in input format");
  1150. return;
  1151. /* Format codes that don't transfer data. */
  1152. case FMT_X:
  1153. case FMT_TR:
  1154. consume_data_flag = 0;
  1155. dtp->u.p.skips += f->u.n;
  1156. pos = bytes_used + dtp->u.p.skips - 1;
  1157. dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
  1158. read_x (dtp, f->u.n);
  1159. break;
  1160. case FMT_TL:
  1161. case FMT_T:
  1162. consume_data_flag = 0;
  1163. if (f->format == FMT_TL)
  1164. {
  1165. /* Handle the special case when no bytes have been used yet.
  1166. Cannot go below zero. */
  1167. if (bytes_used == 0)
  1168. {
  1169. dtp->u.p.pending_spaces -= f->u.n;
  1170. dtp->u.p.skips -= f->u.n;
  1171. dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
  1172. }
  1173. pos = bytes_used - f->u.n;
  1174. }
  1175. else /* FMT_T */
  1176. pos = f->u.n - 1;
  1177. /* Standard 10.6.1.1: excessive left tabbing is reset to the
  1178. left tab limit. We do not check if the position has gone
  1179. beyond the end of record because a subsequent tab could
  1180. bring us back again. */
  1181. pos = pos < 0 ? 0 : pos;
  1182. dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
  1183. dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
  1184. + pos - dtp->u.p.max_pos;
  1185. dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
  1186. ? 0 : dtp->u.p.pending_spaces;
  1187. if (dtp->u.p.skips == 0)
  1188. break;
  1189. /* Adjust everything for end-of-record condition */
  1190. if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
  1191. {
  1192. dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
  1193. dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
  1194. bytes_used = pos;
  1195. dtp->u.p.sf_seen_eor = 0;
  1196. }
  1197. if (dtp->u.p.skips < 0)
  1198. {
  1199. if (is_internal_unit (dtp))
  1200. sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
  1201. else
  1202. fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
  1203. dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
  1204. dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  1205. }
  1206. else
  1207. read_x (dtp, dtp->u.p.skips);
  1208. break;
  1209. case FMT_S:
  1210. consume_data_flag = 0;
  1211. dtp->u.p.sign_status = SIGN_S;
  1212. break;
  1213. case FMT_SS:
  1214. consume_data_flag = 0;
  1215. dtp->u.p.sign_status = SIGN_SS;
  1216. break;
  1217. case FMT_SP:
  1218. consume_data_flag = 0;
  1219. dtp->u.p.sign_status = SIGN_SP;
  1220. break;
  1221. case FMT_BN:
  1222. consume_data_flag = 0 ;
  1223. dtp->u.p.blank_status = BLANK_NULL;
  1224. break;
  1225. case FMT_BZ:
  1226. consume_data_flag = 0;
  1227. dtp->u.p.blank_status = BLANK_ZERO;
  1228. break;
  1229. case FMT_DC:
  1230. consume_data_flag = 0;
  1231. dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
  1232. break;
  1233. case FMT_DP:
  1234. consume_data_flag = 0;
  1235. dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
  1236. break;
  1237. case FMT_RC:
  1238. consume_data_flag = 0;
  1239. dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
  1240. break;
  1241. case FMT_RD:
  1242. consume_data_flag = 0;
  1243. dtp->u.p.current_unit->round_status = ROUND_DOWN;
  1244. break;
  1245. case FMT_RN:
  1246. consume_data_flag = 0;
  1247. dtp->u.p.current_unit->round_status = ROUND_NEAREST;
  1248. break;
  1249. case FMT_RP:
  1250. consume_data_flag = 0;
  1251. dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
  1252. break;
  1253. case FMT_RU:
  1254. consume_data_flag = 0;
  1255. dtp->u.p.current_unit->round_status = ROUND_UP;
  1256. break;
  1257. case FMT_RZ:
  1258. consume_data_flag = 0;
  1259. dtp->u.p.current_unit->round_status = ROUND_ZERO;
  1260. break;
  1261. case FMT_P:
  1262. consume_data_flag = 0;
  1263. dtp->u.p.scale_factor = f->u.k;
  1264. break;
  1265. case FMT_DOLLAR:
  1266. consume_data_flag = 0;
  1267. dtp->u.p.seen_dollar = 1;
  1268. break;
  1269. case FMT_SLASH:
  1270. consume_data_flag = 0;
  1271. dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  1272. next_record (dtp, 0);
  1273. break;
  1274. case FMT_COLON:
  1275. /* A colon descriptor causes us to exit this loop (in
  1276. particular preventing another / descriptor from being
  1277. processed) unless there is another data item to be
  1278. transferred. */
  1279. consume_data_flag = 0;
  1280. if (n == 0)
  1281. return;
  1282. break;
  1283. default:
  1284. internal_error (&dtp->common, "Bad format node");
  1285. }
  1286. /* Adjust the item count and data pointer. */
  1287. if ((consume_data_flag > 0) && (n > 0))
  1288. {
  1289. n--;
  1290. p = ((char *) p) + size;
  1291. }
  1292. dtp->u.p.skips = 0;
  1293. pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
  1294. dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
  1295. }
  1296. return;
  1297. /* Come here when we need a data descriptor but don't have one. We
  1298. push the current format node back onto the input, then return and
  1299. let the user program call us back with the data. */
  1300. need_read_data:
  1301. unget_format (dtp, f);
  1302. }
  1303. static void
  1304. formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
  1305. size_t size)
  1306. {
  1307. int pos, bytes_used;
  1308. const fnode *f;
  1309. format_token t;
  1310. int n;
  1311. int consume_data_flag;
  1312. /* Change a complex data item into a pair of reals. */
  1313. n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
  1314. if (type == BT_COMPLEX)
  1315. {
  1316. type = BT_REAL;
  1317. size /= 2;
  1318. }
  1319. /* If there's an EOR condition, we simulate finalizing the transfer
  1320. by doing nothing. */
  1321. if (dtp->u.p.eor_condition)
  1322. return;
  1323. /* Set this flag so that commas in reads cause the read to complete before
  1324. the entire field has been read. The next read field will start right after
  1325. the comma in the stream. (Set to 0 for character reads). */
  1326. dtp->u.p.sf_read_comma =
  1327. dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
  1328. for (;;)
  1329. {
  1330. /* If reversion has occurred and there is another real data item,
  1331. then we have to move to the next record. */
  1332. if (dtp->u.p.reversion_flag && n > 0)
  1333. {
  1334. dtp->u.p.reversion_flag = 0;
  1335. next_record (dtp, 0);
  1336. }
  1337. consume_data_flag = 1;
  1338. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1339. break;
  1340. f = next_format (dtp);
  1341. if (f == NULL)
  1342. {
  1343. /* No data descriptors left. */
  1344. if (unlikely (n > 0))
  1345. generate_error (&dtp->common, LIBERROR_FORMAT,
  1346. "Insufficient data descriptors in format after reversion");
  1347. return;
  1348. }
  1349. /* Now discharge T, TR and X movements to the right. This is delayed
  1350. until a data producing format to suppress trailing spaces. */
  1351. t = f->format;
  1352. if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
  1353. && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
  1354. || t == FMT_Z || t == FMT_F || t == FMT_E
  1355. || t == FMT_EN || t == FMT_ES || t == FMT_G
  1356. || t == FMT_L || t == FMT_A || t == FMT_D))
  1357. || t == FMT_STRING))
  1358. {
  1359. if (dtp->u.p.skips > 0)
  1360. {
  1361. int tmp;
  1362. write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
  1363. tmp = (int)(dtp->u.p.current_unit->recl
  1364. - dtp->u.p.current_unit->bytes_left);
  1365. dtp->u.p.max_pos =
  1366. dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
  1367. }
  1368. if (dtp->u.p.skips < 0)
  1369. {
  1370. if (is_internal_unit (dtp))
  1371. sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
  1372. else
  1373. fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
  1374. dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
  1375. }
  1376. dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  1377. }
  1378. bytes_used = (int)(dtp->u.p.current_unit->recl
  1379. - dtp->u.p.current_unit->bytes_left);
  1380. if (is_stream_io(dtp))
  1381. bytes_used = 0;
  1382. switch (t)
  1383. {
  1384. case FMT_I:
  1385. if (n == 0)
  1386. goto need_data;
  1387. if (require_type (dtp, BT_INTEGER, type, f))
  1388. return;
  1389. write_i (dtp, f, p, kind);
  1390. break;
  1391. case FMT_B:
  1392. if (n == 0)
  1393. goto need_data;
  1394. if (!(compile_options.allow_std & GFC_STD_GNU)
  1395. && require_numeric_type (dtp, type, f))
  1396. return;
  1397. if (!(compile_options.allow_std & GFC_STD_F2008)
  1398. && require_type (dtp, BT_INTEGER, type, f))
  1399. return;
  1400. write_b (dtp, f, p, kind);
  1401. break;
  1402. case FMT_O:
  1403. if (n == 0)
  1404. goto need_data;
  1405. if (!(compile_options.allow_std & GFC_STD_GNU)
  1406. && require_numeric_type (dtp, type, f))
  1407. return;
  1408. if (!(compile_options.allow_std & GFC_STD_F2008)
  1409. && require_type (dtp, BT_INTEGER, type, f))
  1410. return;
  1411. write_o (dtp, f, p, kind);
  1412. break;
  1413. case FMT_Z:
  1414. if (n == 0)
  1415. goto need_data;
  1416. if (!(compile_options.allow_std & GFC_STD_GNU)
  1417. && require_numeric_type (dtp, type, f))
  1418. return;
  1419. if (!(compile_options.allow_std & GFC_STD_F2008)
  1420. && require_type (dtp, BT_INTEGER, type, f))
  1421. return;
  1422. write_z (dtp, f, p, kind);
  1423. break;
  1424. case FMT_A:
  1425. if (n == 0)
  1426. goto need_data;
  1427. /* It is possible to have FMT_A with something not BT_CHARACTER such
  1428. as when writing out hollerith strings, so check both type
  1429. and kind before calling wide character routines. */
  1430. if (type == BT_CHARACTER && kind == 4)
  1431. write_a_char4 (dtp, f, p, size);
  1432. else
  1433. write_a (dtp, f, p, size);
  1434. break;
  1435. case FMT_L:
  1436. if (n == 0)
  1437. goto need_data;
  1438. write_l (dtp, f, p, kind);
  1439. break;
  1440. case FMT_D:
  1441. if (n == 0)
  1442. goto need_data;
  1443. if (require_type (dtp, BT_REAL, type, f))
  1444. return;
  1445. write_d (dtp, f, p, kind);
  1446. break;
  1447. case FMT_E:
  1448. if (n == 0)
  1449. goto need_data;
  1450. if (require_type (dtp, BT_REAL, type, f))
  1451. return;
  1452. write_e (dtp, f, p, kind);
  1453. break;
  1454. case FMT_EN:
  1455. if (n == 0)
  1456. goto need_data;
  1457. if (require_type (dtp, BT_REAL, type, f))
  1458. return;
  1459. write_en (dtp, f, p, kind);
  1460. break;
  1461. case FMT_ES:
  1462. if (n == 0)
  1463. goto need_data;
  1464. if (require_type (dtp, BT_REAL, type, f))
  1465. return;
  1466. write_es (dtp, f, p, kind);
  1467. break;
  1468. case FMT_F:
  1469. if (n == 0)
  1470. goto need_data;
  1471. if (require_type (dtp, BT_REAL, type, f))
  1472. return;
  1473. write_f (dtp, f, p, kind);
  1474. break;
  1475. case FMT_G:
  1476. if (n == 0)
  1477. goto need_data;
  1478. switch (type)
  1479. {
  1480. case BT_INTEGER:
  1481. write_i (dtp, f, p, kind);
  1482. break;
  1483. case BT_LOGICAL:
  1484. write_l (dtp, f, p, kind);
  1485. break;
  1486. case BT_CHARACTER:
  1487. if (kind == 4)
  1488. write_a_char4 (dtp, f, p, size);
  1489. else
  1490. write_a (dtp, f, p, size);
  1491. break;
  1492. case BT_REAL:
  1493. if (f->u.real.w == 0)
  1494. write_real_g0 (dtp, p, kind, f->u.real.d);
  1495. else
  1496. write_d (dtp, f, p, kind);
  1497. break;
  1498. default:
  1499. internal_error (&dtp->common,
  1500. "formatted_transfer(): Bad type");
  1501. }
  1502. break;
  1503. case FMT_STRING:
  1504. consume_data_flag = 0;
  1505. write_constant_string (dtp, f);
  1506. break;
  1507. /* Format codes that don't transfer data. */
  1508. case FMT_X:
  1509. case FMT_TR:
  1510. consume_data_flag = 0;
  1511. dtp->u.p.skips += f->u.n;
  1512. pos = bytes_used + dtp->u.p.skips - 1;
  1513. dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
  1514. /* Writes occur just before the switch on f->format, above, so
  1515. that trailing blanks are suppressed, unless we are doing a
  1516. non-advancing write in which case we want to output the blanks
  1517. now. */
  1518. if (dtp->u.p.advance_status == ADVANCE_NO)
  1519. {
  1520. write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
  1521. dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  1522. }
  1523. break;
  1524. case FMT_TL:
  1525. case FMT_T:
  1526. consume_data_flag = 0;
  1527. if (f->format == FMT_TL)
  1528. {
  1529. /* Handle the special case when no bytes have been used yet.
  1530. Cannot go below zero. */
  1531. if (bytes_used == 0)
  1532. {
  1533. dtp->u.p.pending_spaces -= f->u.n;
  1534. dtp->u.p.skips -= f->u.n;
  1535. dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
  1536. }
  1537. pos = bytes_used - f->u.n;
  1538. }
  1539. else /* FMT_T */
  1540. pos = f->u.n - dtp->u.p.pending_spaces - 1;
  1541. /* Standard 10.6.1.1: excessive left tabbing is reset to the
  1542. left tab limit. We do not check if the position has gone
  1543. beyond the end of record because a subsequent tab could
  1544. bring us back again. */
  1545. pos = pos < 0 ? 0 : pos;
  1546. dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
  1547. dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
  1548. + pos - dtp->u.p.max_pos;
  1549. dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
  1550. ? 0 : dtp->u.p.pending_spaces;
  1551. break;
  1552. case FMT_S:
  1553. consume_data_flag = 0;
  1554. dtp->u.p.sign_status = SIGN_S;
  1555. break;
  1556. case FMT_SS:
  1557. consume_data_flag = 0;
  1558. dtp->u.p.sign_status = SIGN_SS;
  1559. break;
  1560. case FMT_SP:
  1561. consume_data_flag = 0;
  1562. dtp->u.p.sign_status = SIGN_SP;
  1563. break;
  1564. case FMT_BN:
  1565. consume_data_flag = 0 ;
  1566. dtp->u.p.blank_status = BLANK_NULL;
  1567. break;
  1568. case FMT_BZ:
  1569. consume_data_flag = 0;
  1570. dtp->u.p.blank_status = BLANK_ZERO;
  1571. break;
  1572. case FMT_DC:
  1573. consume_data_flag = 0;
  1574. dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
  1575. break;
  1576. case FMT_DP:
  1577. consume_data_flag = 0;
  1578. dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
  1579. break;
  1580. case FMT_RC:
  1581. consume_data_flag = 0;
  1582. dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
  1583. break;
  1584. case FMT_RD:
  1585. consume_data_flag = 0;
  1586. dtp->u.p.current_unit->round_status = ROUND_DOWN;
  1587. break;
  1588. case FMT_RN:
  1589. consume_data_flag = 0;
  1590. dtp->u.p.current_unit->round_status = ROUND_NEAREST;
  1591. break;
  1592. case FMT_RP:
  1593. consume_data_flag = 0;
  1594. dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
  1595. break;
  1596. case FMT_RU:
  1597. consume_data_flag = 0;
  1598. dtp->u.p.current_unit->round_status = ROUND_UP;
  1599. break;
  1600. case FMT_RZ:
  1601. consume_data_flag = 0;
  1602. dtp->u.p.current_unit->round_status = ROUND_ZERO;
  1603. break;
  1604. case FMT_P:
  1605. consume_data_flag = 0;
  1606. dtp->u.p.scale_factor = f->u.k;
  1607. break;
  1608. case FMT_DOLLAR:
  1609. consume_data_flag = 0;
  1610. dtp->u.p.seen_dollar = 1;
  1611. break;
  1612. case FMT_SLASH:
  1613. consume_data_flag = 0;
  1614. dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  1615. next_record (dtp, 0);
  1616. break;
  1617. case FMT_COLON:
  1618. /* A colon descriptor causes us to exit this loop (in
  1619. particular preventing another / descriptor from being
  1620. processed) unless there is another data item to be
  1621. transferred. */
  1622. consume_data_flag = 0;
  1623. if (n == 0)
  1624. return;
  1625. break;
  1626. default:
  1627. internal_error (&dtp->common, "Bad format node");
  1628. }
  1629. /* Adjust the item count and data pointer. */
  1630. if ((consume_data_flag > 0) && (n > 0))
  1631. {
  1632. n--;
  1633. p = ((char *) p) + size;
  1634. }
  1635. pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
  1636. dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
  1637. }
  1638. return;
  1639. /* Come here when we need a data descriptor but don't have one. We
  1640. push the current format node back onto the input, then return and
  1641. let the user program call us back with the data. */
  1642. need_data:
  1643. unget_format (dtp, f);
  1644. }
  1645. /* This function is first called from data_init_transfer to initiate the loop
  1646. over each item in the format, transferring data as required. Subsequent
  1647. calls to this function occur for each data item foound in the READ/WRITE
  1648. statement. The item_count is incremented for each call. Since the first
  1649. call is from data_transfer_init, the item_count is always one greater than
  1650. the actual count number of the item being transferred. */
  1651. static void
  1652. formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
  1653. size_t size, size_t nelems)
  1654. {
  1655. size_t elem;
  1656. char *tmp;
  1657. tmp = (char *) p;
  1658. size_t stride = type == BT_CHARACTER ?
  1659. size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
  1660. if (dtp->u.p.mode == READING)
  1661. {
  1662. /* Big loop over all the elements. */
  1663. for (elem = 0; elem < nelems; elem++)
  1664. {
  1665. dtp->u.p.item_count++;
  1666. formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
  1667. }
  1668. }
  1669. else
  1670. {
  1671. /* Big loop over all the elements. */
  1672. for (elem = 0; elem < nelems; elem++)
  1673. {
  1674. dtp->u.p.item_count++;
  1675. formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
  1676. }
  1677. }
  1678. }
  1679. /* Data transfer entry points. The type of the data entity is
  1680. implicit in the subroutine call. This prevents us from having to
  1681. share a common enum with the compiler. */
  1682. void
  1683. transfer_integer (st_parameter_dt *dtp, void *p, int kind)
  1684. {
  1685. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1686. return;
  1687. dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
  1688. }
  1689. void
  1690. transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
  1691. {
  1692. transfer_integer (dtp, p, kind);
  1693. }
  1694. void
  1695. transfer_real (st_parameter_dt *dtp, void *p, int kind)
  1696. {
  1697. size_t size;
  1698. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1699. return;
  1700. size = size_from_real_kind (kind);
  1701. dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
  1702. }
  1703. void
  1704. transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
  1705. {
  1706. transfer_real (dtp, p, kind);
  1707. }
  1708. void
  1709. transfer_logical (st_parameter_dt *dtp, void *p, int kind)
  1710. {
  1711. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1712. return;
  1713. dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
  1714. }
  1715. void
  1716. transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
  1717. {
  1718. transfer_logical (dtp, p, kind);
  1719. }
  1720. void
  1721. transfer_character (st_parameter_dt *dtp, void *p, int len)
  1722. {
  1723. static char *empty_string[0];
  1724. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1725. return;
  1726. /* Strings of zero length can have p == NULL, which confuses the
  1727. transfer routines into thinking we need more data elements. To avoid
  1728. this, we give them a nice pointer. */
  1729. if (len == 0 && p == NULL)
  1730. p = empty_string;
  1731. /* Set kind here to 1. */
  1732. dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
  1733. }
  1734. void
  1735. transfer_character_write (st_parameter_dt *dtp, void *p, int len)
  1736. {
  1737. transfer_character (dtp, p, len);
  1738. }
  1739. void
  1740. transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
  1741. {
  1742. static char *empty_string[0];
  1743. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1744. return;
  1745. /* Strings of zero length can have p == NULL, which confuses the
  1746. transfer routines into thinking we need more data elements. To avoid
  1747. this, we give them a nice pointer. */
  1748. if (len == 0 && p == NULL)
  1749. p = empty_string;
  1750. /* Here we pass the actual kind value. */
  1751. dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
  1752. }
  1753. void
  1754. transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
  1755. {
  1756. transfer_character_wide (dtp, p, len, kind);
  1757. }
  1758. void
  1759. transfer_complex (st_parameter_dt *dtp, void *p, int kind)
  1760. {
  1761. size_t size;
  1762. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1763. return;
  1764. size = size_from_complex_kind (kind);
  1765. dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
  1766. }
  1767. void
  1768. transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
  1769. {
  1770. transfer_complex (dtp, p, kind);
  1771. }
  1772. void
  1773. transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
  1774. gfc_charlen_type charlen)
  1775. {
  1776. index_type count[GFC_MAX_DIMENSIONS];
  1777. index_type extent[GFC_MAX_DIMENSIONS];
  1778. index_type stride[GFC_MAX_DIMENSIONS];
  1779. index_type stride0, rank, size, n;
  1780. size_t tsize;
  1781. char *data;
  1782. bt iotype;
  1783. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1784. return;
  1785. iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
  1786. size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
  1787. rank = GFC_DESCRIPTOR_RANK (desc);
  1788. for (n = 0; n < rank; n++)
  1789. {
  1790. count[n] = 0;
  1791. stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
  1792. extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
  1793. /* If the extent of even one dimension is zero, then the entire
  1794. array section contains zero elements, so we return after writing
  1795. a zero array record. */
  1796. if (extent[n] <= 0)
  1797. {
  1798. data = NULL;
  1799. tsize = 0;
  1800. dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
  1801. return;
  1802. }
  1803. }
  1804. stride0 = stride[0];
  1805. /* If the innermost dimension has a stride of 1, we can do the transfer
  1806. in contiguous chunks. */
  1807. if (stride0 == size)
  1808. tsize = extent[0];
  1809. else
  1810. tsize = 1;
  1811. data = GFC_DESCRIPTOR_DATA (desc);
  1812. while (data)
  1813. {
  1814. dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
  1815. data += stride0 * tsize;
  1816. count[0] += tsize;
  1817. n = 0;
  1818. while (count[n] == extent[n])
  1819. {
  1820. count[n] = 0;
  1821. data -= stride[n] * extent[n];
  1822. n++;
  1823. if (n == rank)
  1824. {
  1825. data = NULL;
  1826. break;
  1827. }
  1828. else
  1829. {
  1830. count[n]++;
  1831. data += stride[n];
  1832. }
  1833. }
  1834. }
  1835. }
  1836. void
  1837. transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
  1838. gfc_charlen_type charlen)
  1839. {
  1840. transfer_array (dtp, desc, kind, charlen);
  1841. }
  1842. /* Preposition a sequential unformatted file while reading. */
  1843. static void
  1844. us_read (st_parameter_dt *dtp, int continued)
  1845. {
  1846. ssize_t n, nr;
  1847. GFC_INTEGER_4 i4;
  1848. GFC_INTEGER_8 i8;
  1849. gfc_offset i;
  1850. if (compile_options.record_marker == 0)
  1851. n = sizeof (GFC_INTEGER_4);
  1852. else
  1853. n = compile_options.record_marker;
  1854. nr = sread (dtp->u.p.current_unit->s, &i, n);
  1855. if (unlikely (nr < 0))
  1856. {
  1857. generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
  1858. return;
  1859. }
  1860. else if (nr == 0)
  1861. {
  1862. hit_eof (dtp);
  1863. return; /* end of file */
  1864. }
  1865. else if (unlikely (n != nr))
  1866. {
  1867. generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
  1868. return;
  1869. }
  1870. /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
  1871. if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
  1872. {
  1873. switch (nr)
  1874. {
  1875. case sizeof(GFC_INTEGER_4):
  1876. memcpy (&i4, &i, sizeof (i4));
  1877. i = i4;
  1878. break;
  1879. case sizeof(GFC_INTEGER_8):
  1880. memcpy (&i8, &i, sizeof (i8));
  1881. i = i8;
  1882. break;
  1883. default:
  1884. runtime_error ("Illegal value for record marker");
  1885. break;
  1886. }
  1887. }
  1888. else
  1889. {
  1890. uint32_t u32;
  1891. uint64_t u64;
  1892. switch (nr)
  1893. {
  1894. case sizeof(GFC_INTEGER_4):
  1895. memcpy (&u32, &i, sizeof (u32));
  1896. u32 = __builtin_bswap32 (u32);
  1897. memcpy (&i4, &u32, sizeof (i4));
  1898. i = i4;
  1899. break;
  1900. case sizeof(GFC_INTEGER_8):
  1901. memcpy (&u64, &i, sizeof (u64));
  1902. u64 = __builtin_bswap64 (u64);
  1903. memcpy (&i8, &u64, sizeof (i8));
  1904. i = i8;
  1905. break;
  1906. default:
  1907. runtime_error ("Illegal value for record marker");
  1908. break;
  1909. }
  1910. }
  1911. if (i >= 0)
  1912. {
  1913. dtp->u.p.current_unit->bytes_left_subrecord = i;
  1914. dtp->u.p.current_unit->continued = 0;
  1915. }
  1916. else
  1917. {
  1918. dtp->u.p.current_unit->bytes_left_subrecord = -i;
  1919. dtp->u.p.current_unit->continued = 1;
  1920. }
  1921. if (! continued)
  1922. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  1923. }
  1924. /* Preposition a sequential unformatted file while writing. This
  1925. amount to writing a bogus length that will be filled in later. */
  1926. static void
  1927. us_write (st_parameter_dt *dtp, int continued)
  1928. {
  1929. ssize_t nbytes;
  1930. gfc_offset dummy;
  1931. dummy = 0;
  1932. if (compile_options.record_marker == 0)
  1933. nbytes = sizeof (GFC_INTEGER_4);
  1934. else
  1935. nbytes = compile_options.record_marker ;
  1936. if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
  1937. generate_error (&dtp->common, LIBERROR_OS, NULL);
  1938. /* For sequential unformatted, if RECL= was not specified in the OPEN
  1939. we write until we have more bytes than can fit in the subrecord
  1940. markers, then we write a new subrecord. */
  1941. dtp->u.p.current_unit->bytes_left_subrecord =
  1942. dtp->u.p.current_unit->recl_subrecord;
  1943. dtp->u.p.current_unit->continued = continued;
  1944. }
  1945. /* Position to the next record prior to transfer. We are assumed to
  1946. be before the next record. We also calculate the bytes in the next
  1947. record. */
  1948. static void
  1949. pre_position (st_parameter_dt *dtp)
  1950. {
  1951. if (dtp->u.p.current_unit->current_record)
  1952. return; /* Already positioned. */
  1953. switch (current_mode (dtp))
  1954. {
  1955. case FORMATTED_STREAM:
  1956. case UNFORMATTED_STREAM:
  1957. /* There are no records with stream I/O. If the position was specified
  1958. data_transfer_init has already positioned the file. If no position
  1959. was specified, we continue from where we last left off. I.e.
  1960. there is nothing to do here. */
  1961. break;
  1962. case UNFORMATTED_SEQUENTIAL:
  1963. if (dtp->u.p.mode == READING)
  1964. us_read (dtp, 0);
  1965. else
  1966. us_write (dtp, 0);
  1967. break;
  1968. case FORMATTED_SEQUENTIAL:
  1969. case FORMATTED_DIRECT:
  1970. case UNFORMATTED_DIRECT:
  1971. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  1972. break;
  1973. }
  1974. dtp->u.p.current_unit->current_record = 1;
  1975. }
  1976. /* Initialize things for a data transfer. This code is common for
  1977. both reading and writing. */
  1978. static void
  1979. data_transfer_init (st_parameter_dt *dtp, int read_flag)
  1980. {
  1981. unit_flags u_flags; /* Used for creating a unit if needed. */
  1982. GFC_INTEGER_4 cf = dtp->common.flags;
  1983. namelist_info *ionml;
  1984. ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
  1985. memset (&dtp->u.p, 0, sizeof (dtp->u.p));
  1986. dtp->u.p.ionml = ionml;
  1987. dtp->u.p.mode = read_flag ? READING : WRITING;
  1988. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1989. return;
  1990. if ((cf & IOPARM_DT_HAS_SIZE) != 0)
  1991. dtp->u.p.size_used = 0; /* Initialize the count. */
  1992. dtp->u.p.current_unit = get_unit (dtp, 1);
  1993. if (dtp->u.p.current_unit->s == NULL)
  1994. { /* Open the unit with some default flags. */
  1995. st_parameter_open opp;
  1996. unit_convert conv;
  1997. if (dtp->common.unit < 0)
  1998. {
  1999. close_unit (dtp->u.p.current_unit);
  2000. dtp->u.p.current_unit = NULL;
  2001. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2002. "Bad unit number in statement");
  2003. return;
  2004. }
  2005. memset (&u_flags, '\0', sizeof (u_flags));
  2006. u_flags.access = ACCESS_SEQUENTIAL;
  2007. u_flags.action = ACTION_READWRITE;
  2008. /* Is it unformatted? */
  2009. if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
  2010. | IOPARM_DT_IONML_SET)))
  2011. u_flags.form = FORM_UNFORMATTED;
  2012. else
  2013. u_flags.form = FORM_UNSPECIFIED;
  2014. u_flags.delim = DELIM_UNSPECIFIED;
  2015. u_flags.blank = BLANK_UNSPECIFIED;
  2016. u_flags.pad = PAD_UNSPECIFIED;
  2017. u_flags.decimal = DECIMAL_UNSPECIFIED;
  2018. u_flags.encoding = ENCODING_UNSPECIFIED;
  2019. u_flags.async = ASYNC_UNSPECIFIED;
  2020. u_flags.round = ROUND_UNSPECIFIED;
  2021. u_flags.sign = SIGN_UNSPECIFIED;
  2022. u_flags.status = STATUS_UNKNOWN;
  2023. conv = get_unformatted_convert (dtp->common.unit);
  2024. if (conv == GFC_CONVERT_NONE)
  2025. conv = compile_options.convert;
  2026. /* We use big_endian, which is 0 on little-endian machines
  2027. and 1 on big-endian machines. */
  2028. switch (conv)
  2029. {
  2030. case GFC_CONVERT_NATIVE:
  2031. case GFC_CONVERT_SWAP:
  2032. break;
  2033. case GFC_CONVERT_BIG:
  2034. conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
  2035. break;
  2036. case GFC_CONVERT_LITTLE:
  2037. conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
  2038. break;
  2039. default:
  2040. internal_error (&opp.common, "Illegal value for CONVERT");
  2041. break;
  2042. }
  2043. u_flags.convert = conv;
  2044. opp.common = dtp->common;
  2045. opp.common.flags &= IOPARM_COMMON_MASK;
  2046. dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
  2047. dtp->common.flags &= ~IOPARM_COMMON_MASK;
  2048. dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
  2049. if (dtp->u.p.current_unit == NULL)
  2050. return;
  2051. }
  2052. /* Check the action. */
  2053. if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
  2054. {
  2055. generate_error (&dtp->common, LIBERROR_BAD_ACTION,
  2056. "Cannot read from file opened for WRITE");
  2057. return;
  2058. }
  2059. if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
  2060. {
  2061. generate_error (&dtp->common, LIBERROR_BAD_ACTION,
  2062. "Cannot write to file opened for READ");
  2063. return;
  2064. }
  2065. dtp->u.p.first_item = 1;
  2066. /* Check the format. */
  2067. if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
  2068. parse_format (dtp);
  2069. if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
  2070. && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
  2071. != 0)
  2072. {
  2073. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2074. "Format present for UNFORMATTED data transfer");
  2075. return;
  2076. }
  2077. if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
  2078. {
  2079. if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
  2080. {
  2081. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2082. "A format cannot be specified with a namelist");
  2083. return;
  2084. }
  2085. }
  2086. else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
  2087. !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
  2088. {
  2089. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2090. "Missing format for FORMATTED data transfer");
  2091. return;
  2092. }
  2093. if (is_internal_unit (dtp)
  2094. && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
  2095. {
  2096. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2097. "Internal file cannot be accessed by UNFORMATTED "
  2098. "data transfer");
  2099. return;
  2100. }
  2101. /* Check the record or position number. */
  2102. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
  2103. && (cf & IOPARM_DT_HAS_REC) == 0)
  2104. {
  2105. generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
  2106. "Direct access data transfer requires record number");
  2107. return;
  2108. }
  2109. if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
  2110. {
  2111. if ((cf & IOPARM_DT_HAS_REC) != 0)
  2112. {
  2113. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2114. "Record number not allowed for sequential access "
  2115. "data transfer");
  2116. return;
  2117. }
  2118. if (compile_options.warn_std &&
  2119. dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
  2120. {
  2121. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2122. "Sequential READ or WRITE not allowed after "
  2123. "EOF marker, possibly use REWIND or BACKSPACE");
  2124. return;
  2125. }
  2126. }
  2127. /* Process the ADVANCE option. */
  2128. dtp->u.p.advance_status
  2129. = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
  2130. find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
  2131. "Bad ADVANCE parameter in data transfer statement");
  2132. if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
  2133. {
  2134. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
  2135. {
  2136. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2137. "ADVANCE specification conflicts with sequential "
  2138. "access");
  2139. return;
  2140. }
  2141. if (is_internal_unit (dtp))
  2142. {
  2143. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2144. "ADVANCE specification conflicts with internal file");
  2145. return;
  2146. }
  2147. if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
  2148. != IOPARM_DT_HAS_FORMAT)
  2149. {
  2150. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2151. "ADVANCE specification requires an explicit format");
  2152. return;
  2153. }
  2154. }
  2155. if (read_flag)
  2156. {
  2157. dtp->u.p.current_unit->previous_nonadvancing_write = 0;
  2158. if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
  2159. {
  2160. generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
  2161. "EOR specification requires an ADVANCE specification "
  2162. "of NO");
  2163. return;
  2164. }
  2165. if ((cf & IOPARM_DT_HAS_SIZE) != 0
  2166. && dtp->u.p.advance_status != ADVANCE_NO)
  2167. {
  2168. generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
  2169. "SIZE specification requires an ADVANCE "
  2170. "specification of NO");
  2171. return;
  2172. }
  2173. }
  2174. else
  2175. { /* Write constraints. */
  2176. if ((cf & IOPARM_END) != 0)
  2177. {
  2178. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2179. "END specification cannot appear in a write "
  2180. "statement");
  2181. return;
  2182. }
  2183. if ((cf & IOPARM_EOR) != 0)
  2184. {
  2185. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2186. "EOR specification cannot appear in a write "
  2187. "statement");
  2188. return;
  2189. }
  2190. if ((cf & IOPARM_DT_HAS_SIZE) != 0)
  2191. {
  2192. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2193. "SIZE specification cannot appear in a write "
  2194. "statement");
  2195. return;
  2196. }
  2197. }
  2198. if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
  2199. dtp->u.p.advance_status = ADVANCE_YES;
  2200. /* Check the decimal mode. */
  2201. dtp->u.p.current_unit->decimal_status
  2202. = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
  2203. find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
  2204. decimal_opt, "Bad DECIMAL parameter in data transfer "
  2205. "statement");
  2206. if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
  2207. dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
  2208. /* Check the round mode. */
  2209. dtp->u.p.current_unit->round_status
  2210. = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
  2211. find_option (&dtp->common, dtp->round, dtp->round_len,
  2212. round_opt, "Bad ROUND parameter in data transfer "
  2213. "statement");
  2214. if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
  2215. dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
  2216. /* Check the sign mode. */
  2217. dtp->u.p.sign_status
  2218. = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
  2219. find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
  2220. "Bad SIGN parameter in data transfer statement");
  2221. if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
  2222. dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
  2223. /* Check the blank mode. */
  2224. dtp->u.p.blank_status
  2225. = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
  2226. find_option (&dtp->common, dtp->blank, dtp->blank_len,
  2227. blank_opt,
  2228. "Bad BLANK parameter in data transfer statement");
  2229. if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
  2230. dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
  2231. /* Check the delim mode. */
  2232. dtp->u.p.current_unit->delim_status
  2233. = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
  2234. find_option (&dtp->common, dtp->delim, dtp->delim_len,
  2235. delim_opt, "Bad DELIM parameter in data transfer statement");
  2236. if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
  2237. {
  2238. if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
  2239. dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
  2240. else
  2241. dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
  2242. }
  2243. /* Check the pad mode. */
  2244. dtp->u.p.current_unit->pad_status
  2245. = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
  2246. find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
  2247. "Bad PAD parameter in data transfer statement");
  2248. if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
  2249. dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
  2250. /* Check to see if we might be reading what we wrote before */
  2251. if (dtp->u.p.mode != dtp->u.p.current_unit->mode
  2252. && !is_internal_unit (dtp))
  2253. {
  2254. int pos = fbuf_reset (dtp->u.p.current_unit);
  2255. if (pos != 0)
  2256. sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
  2257. sflush(dtp->u.p.current_unit->s);
  2258. }
  2259. /* Check the POS= specifier: that it is in range and that it is used with a
  2260. unit that has been connected for STREAM access. F2003 9.5.1.10. */
  2261. if (((cf & IOPARM_DT_HAS_POS) != 0))
  2262. {
  2263. if (is_stream_io (dtp))
  2264. {
  2265. if (dtp->pos <= 0)
  2266. {
  2267. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2268. "POS=specifier must be positive");
  2269. return;
  2270. }
  2271. if (dtp->pos >= dtp->u.p.current_unit->maxrec)
  2272. {
  2273. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2274. "POS=specifier too large");
  2275. return;
  2276. }
  2277. dtp->rec = dtp->pos;
  2278. if (dtp->u.p.mode == READING)
  2279. {
  2280. /* Reset the endfile flag; if we hit EOF during reading
  2281. we'll set the flag and generate an error at that point
  2282. rather than worrying about it here. */
  2283. dtp->u.p.current_unit->endfile = NO_ENDFILE;
  2284. }
  2285. if (dtp->pos != dtp->u.p.current_unit->strm_pos)
  2286. {
  2287. fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
  2288. if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
  2289. {
  2290. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2291. return;
  2292. }
  2293. dtp->u.p.current_unit->strm_pos = dtp->pos;
  2294. }
  2295. }
  2296. else
  2297. {
  2298. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2299. "POS=specifier not allowed, "
  2300. "Try OPEN with ACCESS='stream'");
  2301. return;
  2302. }
  2303. }
  2304. /* Sanity checks on the record number. */
  2305. if ((cf & IOPARM_DT_HAS_REC) != 0)
  2306. {
  2307. if (dtp->rec <= 0)
  2308. {
  2309. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2310. "Record number must be positive");
  2311. return;
  2312. }
  2313. if (dtp->rec >= dtp->u.p.current_unit->maxrec)
  2314. {
  2315. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2316. "Record number too large");
  2317. return;
  2318. }
  2319. /* Make sure format buffer is reset. */
  2320. if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
  2321. fbuf_reset (dtp->u.p.current_unit);
  2322. /* Check whether the record exists to be read. Only
  2323. a partial record needs to exist. */
  2324. if (dtp->u.p.mode == READING && (dtp->rec - 1)
  2325. * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
  2326. {
  2327. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2328. "Non-existing record number");
  2329. return;
  2330. }
  2331. /* Position the file. */
  2332. if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
  2333. * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
  2334. {
  2335. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2336. return;
  2337. }
  2338. /* TODO: This is required to maintain compatibility between
  2339. 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
  2340. if (is_stream_io (dtp))
  2341. dtp->u.p.current_unit->strm_pos = dtp->rec;
  2342. /* TODO: Un-comment this code when ABI changes from 4.3.
  2343. if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
  2344. {
  2345. generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
  2346. "Record number not allowed for stream access "
  2347. "data transfer");
  2348. return;
  2349. } */
  2350. }
  2351. /* Bugware for badly written mixed C-Fortran I/O. */
  2352. if (!is_internal_unit (dtp))
  2353. flush_if_preconnected(dtp->u.p.current_unit->s);
  2354. dtp->u.p.current_unit->mode = dtp->u.p.mode;
  2355. /* Set the maximum position reached from the previous I/O operation. This
  2356. could be greater than zero from a previous non-advancing write. */
  2357. dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
  2358. pre_position (dtp);
  2359. /* Set up the subroutine that will handle the transfers. */
  2360. if (read_flag)
  2361. {
  2362. if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
  2363. dtp->u.p.transfer = unformatted_read;
  2364. else
  2365. {
  2366. if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
  2367. {
  2368. dtp->u.p.last_char = EOF - 1;
  2369. dtp->u.p.transfer = list_formatted_read;
  2370. }
  2371. else
  2372. dtp->u.p.transfer = formatted_transfer;
  2373. }
  2374. }
  2375. else
  2376. {
  2377. if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
  2378. dtp->u.p.transfer = unformatted_write;
  2379. else
  2380. {
  2381. if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
  2382. dtp->u.p.transfer = list_formatted_write;
  2383. else
  2384. dtp->u.p.transfer = formatted_transfer;
  2385. }
  2386. }
  2387. /* Make sure that we don't do a read after a nonadvancing write. */
  2388. if (read_flag)
  2389. {
  2390. if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
  2391. {
  2392. generate_error (&dtp->common, LIBERROR_BAD_OPTION,
  2393. "Cannot READ after a nonadvancing WRITE");
  2394. return;
  2395. }
  2396. }
  2397. else
  2398. {
  2399. if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
  2400. dtp->u.p.current_unit->read_bad = 1;
  2401. }
  2402. if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
  2403. {
  2404. #ifdef HAVE_USELOCALE
  2405. dtp->u.p.old_locale = uselocale (c_locale);
  2406. #else
  2407. __gthread_mutex_lock (&old_locale_lock);
  2408. if (!old_locale_ctr++)
  2409. {
  2410. old_locale = setlocale (LC_NUMERIC, NULL);
  2411. setlocale (LC_NUMERIC, "C");
  2412. }
  2413. __gthread_mutex_unlock (&old_locale_lock);
  2414. #endif
  2415. /* Start the data transfer if we are doing a formatted transfer. */
  2416. if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
  2417. && dtp->u.p.ionml == NULL)
  2418. formatted_transfer (dtp, 0, NULL, 0, 0, 1);
  2419. }
  2420. }
  2421. /* Initialize an array_loop_spec given the array descriptor. The function
  2422. returns the index of the last element of the array, and also returns
  2423. starting record, where the first I/O goes to (necessary in case of
  2424. negative strides). */
  2425. gfc_offset
  2426. init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
  2427. gfc_offset *start_record)
  2428. {
  2429. int rank = GFC_DESCRIPTOR_RANK(desc);
  2430. int i;
  2431. gfc_offset index;
  2432. int empty;
  2433. empty = 0;
  2434. index = 1;
  2435. *start_record = 0;
  2436. for (i=0; i<rank; i++)
  2437. {
  2438. ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
  2439. ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
  2440. ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
  2441. ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
  2442. empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
  2443. < GFC_DESCRIPTOR_LBOUND(desc,i));
  2444. if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
  2445. {
  2446. index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
  2447. * GFC_DESCRIPTOR_STRIDE(desc,i);
  2448. }
  2449. else
  2450. {
  2451. index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
  2452. * GFC_DESCRIPTOR_STRIDE(desc,i);
  2453. *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
  2454. * GFC_DESCRIPTOR_STRIDE(desc,i);
  2455. }
  2456. }
  2457. if (empty)
  2458. return 0;
  2459. else
  2460. return index;
  2461. }
  2462. /* Determine the index to the next record in an internal unit array by
  2463. by incrementing through the array_loop_spec. */
  2464. gfc_offset
  2465. next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
  2466. {
  2467. int i, carry;
  2468. gfc_offset index;
  2469. carry = 1;
  2470. index = 0;
  2471. for (i = 0; i < dtp->u.p.current_unit->rank; i++)
  2472. {
  2473. if (carry)
  2474. {
  2475. ls[i].idx++;
  2476. if (ls[i].idx > ls[i].end)
  2477. {
  2478. ls[i].idx = ls[i].start;
  2479. carry = 1;
  2480. }
  2481. else
  2482. carry = 0;
  2483. }
  2484. index = index + (ls[i].idx - ls[i].start) * ls[i].step;
  2485. }
  2486. *finished = carry;
  2487. return index;
  2488. }
  2489. /* Skip to the end of the current record, taking care of an optional
  2490. record marker of size bytes. If the file is not seekable, we
  2491. read chunks of size MAX_READ until we get to the right
  2492. position. */
  2493. static void
  2494. skip_record (st_parameter_dt *dtp, ssize_t bytes)
  2495. {
  2496. ssize_t rlength, readb;
  2497. #define MAX_READ 4096
  2498. char p[MAX_READ];
  2499. dtp->u.p.current_unit->bytes_left_subrecord += bytes;
  2500. if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
  2501. return;
  2502. /* Direct access files do not generate END conditions,
  2503. only I/O errors. */
  2504. if (sseek (dtp->u.p.current_unit->s,
  2505. dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
  2506. {
  2507. /* Seeking failed, fall back to seeking by reading data. */
  2508. while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
  2509. {
  2510. rlength =
  2511. (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
  2512. MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
  2513. readb = sread (dtp->u.p.current_unit->s, p, rlength);
  2514. if (readb < 0)
  2515. {
  2516. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2517. return;
  2518. }
  2519. dtp->u.p.current_unit->bytes_left_subrecord -= readb;
  2520. }
  2521. return;
  2522. }
  2523. dtp->u.p.current_unit->bytes_left_subrecord = 0;
  2524. }
  2525. /* Advance to the next record reading unformatted files, taking
  2526. care of subrecords. If complete_record is nonzero, we loop
  2527. until all subrecords are cleared. */
  2528. static void
  2529. next_record_r_unf (st_parameter_dt *dtp, int complete_record)
  2530. {
  2531. size_t bytes;
  2532. bytes = compile_options.record_marker == 0 ?
  2533. sizeof (GFC_INTEGER_4) : compile_options.record_marker;
  2534. while(1)
  2535. {
  2536. /* Skip over tail */
  2537. skip_record (dtp, bytes);
  2538. if ( ! (complete_record && dtp->u.p.current_unit->continued))
  2539. return;
  2540. us_read (dtp, 1);
  2541. }
  2542. }
  2543. static gfc_offset
  2544. min_off (gfc_offset a, gfc_offset b)
  2545. {
  2546. return (a < b ? a : b);
  2547. }
  2548. /* Space to the next record for read mode. */
  2549. static void
  2550. next_record_r (st_parameter_dt *dtp, int done)
  2551. {
  2552. gfc_offset record;
  2553. int bytes_left;
  2554. char p;
  2555. int cc;
  2556. switch (current_mode (dtp))
  2557. {
  2558. /* No records in unformatted STREAM I/O. */
  2559. case UNFORMATTED_STREAM:
  2560. return;
  2561. case UNFORMATTED_SEQUENTIAL:
  2562. next_record_r_unf (dtp, 1);
  2563. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  2564. break;
  2565. case FORMATTED_DIRECT:
  2566. case UNFORMATTED_DIRECT:
  2567. skip_record (dtp, dtp->u.p.current_unit->bytes_left);
  2568. break;
  2569. case FORMATTED_STREAM:
  2570. case FORMATTED_SEQUENTIAL:
  2571. /* read_sf has already terminated input because of an '\n', or
  2572. we have hit EOF. */
  2573. if (dtp->u.p.sf_seen_eor)
  2574. {
  2575. dtp->u.p.sf_seen_eor = 0;
  2576. break;
  2577. }
  2578. if (is_internal_unit (dtp))
  2579. {
  2580. if (is_array_io (dtp))
  2581. {
  2582. int finished;
  2583. record = next_array_record (dtp, dtp->u.p.current_unit->ls,
  2584. &finished);
  2585. if (!done && finished)
  2586. hit_eof (dtp);
  2587. /* Now seek to this record. */
  2588. record = record * dtp->u.p.current_unit->recl;
  2589. if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
  2590. {
  2591. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  2592. break;
  2593. }
  2594. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  2595. }
  2596. else
  2597. {
  2598. bytes_left = (int) dtp->u.p.current_unit->bytes_left;
  2599. bytes_left = min_off (bytes_left,
  2600. ssize (dtp->u.p.current_unit->s)
  2601. - stell (dtp->u.p.current_unit->s));
  2602. if (sseek (dtp->u.p.current_unit->s,
  2603. bytes_left, SEEK_CUR) < 0)
  2604. {
  2605. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  2606. break;
  2607. }
  2608. dtp->u.p.current_unit->bytes_left
  2609. = dtp->u.p.current_unit->recl;
  2610. }
  2611. break;
  2612. }
  2613. else
  2614. {
  2615. do
  2616. {
  2617. errno = 0;
  2618. cc = fbuf_getc (dtp->u.p.current_unit);
  2619. if (cc == EOF)
  2620. {
  2621. if (errno != 0)
  2622. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2623. else
  2624. {
  2625. if (is_stream_io (dtp)
  2626. || dtp->u.p.current_unit->pad_status == PAD_NO
  2627. || dtp->u.p.current_unit->bytes_left
  2628. == dtp->u.p.current_unit->recl)
  2629. hit_eof (dtp);
  2630. }
  2631. break;
  2632. }
  2633. if (is_stream_io (dtp))
  2634. dtp->u.p.current_unit->strm_pos++;
  2635. p = (char) cc;
  2636. }
  2637. while (p != '\n');
  2638. }
  2639. break;
  2640. }
  2641. }
  2642. /* Small utility function to write a record marker, taking care of
  2643. byte swapping and of choosing the correct size. */
  2644. static int
  2645. write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
  2646. {
  2647. size_t len;
  2648. GFC_INTEGER_4 buf4;
  2649. GFC_INTEGER_8 buf8;
  2650. if (compile_options.record_marker == 0)
  2651. len = sizeof (GFC_INTEGER_4);
  2652. else
  2653. len = compile_options.record_marker;
  2654. /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
  2655. if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
  2656. {
  2657. switch (len)
  2658. {
  2659. case sizeof (GFC_INTEGER_4):
  2660. buf4 = buf;
  2661. return swrite (dtp->u.p.current_unit->s, &buf4, len);
  2662. break;
  2663. case sizeof (GFC_INTEGER_8):
  2664. buf8 = buf;
  2665. return swrite (dtp->u.p.current_unit->s, &buf8, len);
  2666. break;
  2667. default:
  2668. runtime_error ("Illegal value for record marker");
  2669. break;
  2670. }
  2671. }
  2672. else
  2673. {
  2674. uint32_t u32;
  2675. uint64_t u64;
  2676. switch (len)
  2677. {
  2678. case sizeof (GFC_INTEGER_4):
  2679. buf4 = buf;
  2680. memcpy (&u32, &buf4, sizeof (u32));
  2681. u32 = __builtin_bswap32 (u32);
  2682. return swrite (dtp->u.p.current_unit->s, &u32, len);
  2683. break;
  2684. case sizeof (GFC_INTEGER_8):
  2685. buf8 = buf;
  2686. memcpy (&u64, &buf8, sizeof (u64));
  2687. u64 = __builtin_bswap64 (u64);
  2688. return swrite (dtp->u.p.current_unit->s, &u64, len);
  2689. break;
  2690. default:
  2691. runtime_error ("Illegal value for record marker");
  2692. break;
  2693. }
  2694. }
  2695. }
  2696. /* Position to the next (sub)record in write mode for
  2697. unformatted sequential files. */
  2698. static void
  2699. next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
  2700. {
  2701. gfc_offset m, m_write, record_marker;
  2702. /* Bytes written. */
  2703. m = dtp->u.p.current_unit->recl_subrecord
  2704. - dtp->u.p.current_unit->bytes_left_subrecord;
  2705. if (compile_options.record_marker == 0)
  2706. record_marker = sizeof (GFC_INTEGER_4);
  2707. else
  2708. record_marker = compile_options.record_marker;
  2709. /* Seek to the head and overwrite the bogus length with the real
  2710. length. */
  2711. if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
  2712. SEEK_CUR) < 0))
  2713. goto io_error;
  2714. if (next_subrecord)
  2715. m_write = -m;
  2716. else
  2717. m_write = m;
  2718. if (unlikely (write_us_marker (dtp, m_write) < 0))
  2719. goto io_error;
  2720. /* Seek past the end of the current record. */
  2721. if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
  2722. goto io_error;
  2723. /* Write the length tail. If we finish a record containing
  2724. subrecords, we write out the negative length. */
  2725. if (dtp->u.p.current_unit->continued)
  2726. m_write = -m;
  2727. else
  2728. m_write = m;
  2729. if (unlikely (write_us_marker (dtp, m_write) < 0))
  2730. goto io_error;
  2731. return;
  2732. io_error:
  2733. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2734. return;
  2735. }
  2736. /* Utility function like memset() but operating on streams. Return
  2737. value is same as for POSIX write(). */
  2738. static ssize_t
  2739. sset (stream * s, int c, ssize_t nbyte)
  2740. {
  2741. #define WRITE_CHUNK 256
  2742. char p[WRITE_CHUNK];
  2743. ssize_t bytes_left, trans;
  2744. if (nbyte < WRITE_CHUNK)
  2745. memset (p, c, nbyte);
  2746. else
  2747. memset (p, c, WRITE_CHUNK);
  2748. bytes_left = nbyte;
  2749. while (bytes_left > 0)
  2750. {
  2751. trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
  2752. trans = swrite (s, p, trans);
  2753. if (trans <= 0)
  2754. return trans;
  2755. bytes_left -= trans;
  2756. }
  2757. return nbyte - bytes_left;
  2758. }
  2759. /* Position to the next record in write mode. */
  2760. static void
  2761. next_record_w (st_parameter_dt *dtp, int done)
  2762. {
  2763. gfc_offset m, record, max_pos;
  2764. int length;
  2765. /* Zero counters for X- and T-editing. */
  2766. max_pos = dtp->u.p.max_pos;
  2767. dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
  2768. switch (current_mode (dtp))
  2769. {
  2770. /* No records in unformatted STREAM I/O. */
  2771. case UNFORMATTED_STREAM:
  2772. return;
  2773. case FORMATTED_DIRECT:
  2774. if (dtp->u.p.current_unit->bytes_left == 0)
  2775. break;
  2776. fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
  2777. fbuf_flush (dtp->u.p.current_unit, WRITING);
  2778. if (sset (dtp->u.p.current_unit->s, ' ',
  2779. dtp->u.p.current_unit->bytes_left)
  2780. != dtp->u.p.current_unit->bytes_left)
  2781. goto io_error;
  2782. break;
  2783. case UNFORMATTED_DIRECT:
  2784. if (dtp->u.p.current_unit->bytes_left > 0)
  2785. {
  2786. length = (int) dtp->u.p.current_unit->bytes_left;
  2787. if (sset (dtp->u.p.current_unit->s, 0, length) != length)
  2788. goto io_error;
  2789. }
  2790. break;
  2791. case UNFORMATTED_SEQUENTIAL:
  2792. next_record_w_unf (dtp, 0);
  2793. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  2794. break;
  2795. case FORMATTED_STREAM:
  2796. case FORMATTED_SEQUENTIAL:
  2797. if (is_internal_unit (dtp))
  2798. {
  2799. char *p;
  2800. if (is_array_io (dtp))
  2801. {
  2802. int finished;
  2803. length = (int) dtp->u.p.current_unit->bytes_left;
  2804. /* If the farthest position reached is greater than current
  2805. position, adjust the position and set length to pad out
  2806. whats left. Otherwise just pad whats left.
  2807. (for character array unit) */
  2808. m = dtp->u.p.current_unit->recl
  2809. - dtp->u.p.current_unit->bytes_left;
  2810. if (max_pos > m)
  2811. {
  2812. length = (int) (max_pos - m);
  2813. if (sseek (dtp->u.p.current_unit->s,
  2814. length, SEEK_CUR) < 0)
  2815. {
  2816. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  2817. return;
  2818. }
  2819. length = (int) (dtp->u.p.current_unit->recl - max_pos);
  2820. }
  2821. p = write_block (dtp, length);
  2822. if (p == NULL)
  2823. return;
  2824. if (unlikely (is_char4_unit (dtp)))
  2825. {
  2826. gfc_char4_t *p4 = (gfc_char4_t *) p;
  2827. memset4 (p4, ' ', length);
  2828. }
  2829. else
  2830. memset (p, ' ', length);
  2831. /* Now that the current record has been padded out,
  2832. determine where the next record in the array is. */
  2833. record = next_array_record (dtp, dtp->u.p.current_unit->ls,
  2834. &finished);
  2835. if (finished)
  2836. dtp->u.p.current_unit->endfile = AT_ENDFILE;
  2837. /* Now seek to this record */
  2838. record = record * dtp->u.p.current_unit->recl;
  2839. if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
  2840. {
  2841. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  2842. return;
  2843. }
  2844. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  2845. }
  2846. else
  2847. {
  2848. length = 1;
  2849. /* If this is the last call to next_record move to the farthest
  2850. position reached and set length to pad out the remainder
  2851. of the record. (for character scaler unit) */
  2852. if (done)
  2853. {
  2854. m = dtp->u.p.current_unit->recl
  2855. - dtp->u.p.current_unit->bytes_left;
  2856. if (max_pos > m)
  2857. {
  2858. length = (int) (max_pos - m);
  2859. if (sseek (dtp->u.p.current_unit->s,
  2860. length, SEEK_CUR) < 0)
  2861. {
  2862. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  2863. return;
  2864. }
  2865. length = (int) (dtp->u.p.current_unit->recl - max_pos);
  2866. }
  2867. else
  2868. length = (int) dtp->u.p.current_unit->bytes_left;
  2869. }
  2870. if (length > 0)
  2871. {
  2872. p = write_block (dtp, length);
  2873. if (p == NULL)
  2874. return;
  2875. if (unlikely (is_char4_unit (dtp)))
  2876. {
  2877. gfc_char4_t *p4 = (gfc_char4_t *) p;
  2878. memset4 (p4, (gfc_char4_t) ' ', length);
  2879. }
  2880. else
  2881. memset (p, ' ', length);
  2882. }
  2883. }
  2884. }
  2885. else
  2886. {
  2887. #ifdef HAVE_CRLF
  2888. const int len = 2;
  2889. #else
  2890. const int len = 1;
  2891. #endif
  2892. fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
  2893. char * p = fbuf_alloc (dtp->u.p.current_unit, len);
  2894. if (!p)
  2895. goto io_error;
  2896. #ifdef HAVE_CRLF
  2897. *(p++) = '\r';
  2898. #endif
  2899. *p = '\n';
  2900. if (is_stream_io (dtp))
  2901. {
  2902. dtp->u.p.current_unit->strm_pos += len;
  2903. if (dtp->u.p.current_unit->strm_pos
  2904. < ssize (dtp->u.p.current_unit->s))
  2905. unit_truncate (dtp->u.p.current_unit,
  2906. dtp->u.p.current_unit->strm_pos - 1,
  2907. &dtp->common);
  2908. }
  2909. }
  2910. break;
  2911. io_error:
  2912. generate_error (&dtp->common, LIBERROR_OS, NULL);
  2913. break;
  2914. }
  2915. }
  2916. /* Position to the next record, which means moving to the end of the
  2917. current record. This can happen under several different
  2918. conditions. If the done flag is not set, we get ready to process
  2919. the next record. */
  2920. void
  2921. next_record (st_parameter_dt *dtp, int done)
  2922. {
  2923. gfc_offset fp; /* File position. */
  2924. dtp->u.p.current_unit->read_bad = 0;
  2925. if (dtp->u.p.mode == READING)
  2926. next_record_r (dtp, done);
  2927. else
  2928. next_record_w (dtp, done);
  2929. if (!is_stream_io (dtp))
  2930. {
  2931. /* Since we have changed the position, set it to unspecified so
  2932. that INQUIRE(POSITION=) knows it needs to look into it. */
  2933. if (done)
  2934. dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
  2935. dtp->u.p.current_unit->current_record = 0;
  2936. if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
  2937. {
  2938. fp = stell (dtp->u.p.current_unit->s);
  2939. /* Calculate next record, rounding up partial records. */
  2940. dtp->u.p.current_unit->last_record =
  2941. (fp + dtp->u.p.current_unit->recl - 1) /
  2942. dtp->u.p.current_unit->recl;
  2943. }
  2944. else
  2945. dtp->u.p.current_unit->last_record++;
  2946. }
  2947. if (!done)
  2948. pre_position (dtp);
  2949. fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
  2950. smarkeor (dtp->u.p.current_unit->s);
  2951. }
  2952. /* Finalize the current data transfer. For a nonadvancing transfer,
  2953. this means advancing to the next record. For internal units close the
  2954. stream associated with the unit. */
  2955. static void
  2956. finalize_transfer (st_parameter_dt *dtp)
  2957. {
  2958. GFC_INTEGER_4 cf = dtp->common.flags;
  2959. if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
  2960. *dtp->size = dtp->u.p.size_used;
  2961. if (dtp->u.p.eor_condition)
  2962. {
  2963. generate_error (&dtp->common, LIBERROR_EOR, NULL);
  2964. goto done;
  2965. }
  2966. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  2967. {
  2968. if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
  2969. dtp->u.p.current_unit->current_record = 0;
  2970. goto done;
  2971. }
  2972. if ((dtp->u.p.ionml != NULL)
  2973. && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
  2974. {
  2975. if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
  2976. namelist_read (dtp);
  2977. else
  2978. namelist_write (dtp);
  2979. }
  2980. dtp->u.p.transfer = NULL;
  2981. if (dtp->u.p.current_unit == NULL)
  2982. goto done;
  2983. if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
  2984. {
  2985. finish_list_read (dtp);
  2986. goto done;
  2987. }
  2988. if (dtp->u.p.mode == WRITING)
  2989. dtp->u.p.current_unit->previous_nonadvancing_write
  2990. = dtp->u.p.advance_status == ADVANCE_NO;
  2991. if (is_stream_io (dtp))
  2992. {
  2993. if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
  2994. && dtp->u.p.advance_status != ADVANCE_NO)
  2995. next_record (dtp, 1);
  2996. goto done;
  2997. }
  2998. dtp->u.p.current_unit->current_record = 0;
  2999. if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
  3000. {
  3001. fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
  3002. dtp->u.p.seen_dollar = 0;
  3003. goto done;
  3004. }
  3005. /* For non-advancing I/O, save the current maximum position for use in the
  3006. next I/O operation if needed. */
  3007. if (dtp->u.p.advance_status == ADVANCE_NO)
  3008. {
  3009. int bytes_written = (int) (dtp->u.p.current_unit->recl
  3010. - dtp->u.p.current_unit->bytes_left);
  3011. dtp->u.p.current_unit->saved_pos =
  3012. dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
  3013. fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
  3014. goto done;
  3015. }
  3016. else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
  3017. && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
  3018. fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
  3019. dtp->u.p.current_unit->saved_pos = 0;
  3020. next_record (dtp, 1);
  3021. done:
  3022. #ifdef HAVE_USELOCALE
  3023. if (dtp->u.p.old_locale != (locale_t) 0)
  3024. {
  3025. uselocale (dtp->u.p.old_locale);
  3026. dtp->u.p.old_locale = (locale_t) 0;
  3027. }
  3028. #else
  3029. __gthread_mutex_lock (&old_locale_lock);
  3030. if (!--old_locale_ctr)
  3031. {
  3032. setlocale (LC_NUMERIC, old_locale);
  3033. old_locale = NULL;
  3034. }
  3035. __gthread_mutex_unlock (&old_locale_lock);
  3036. #endif
  3037. }
  3038. /* Transfer function for IOLENGTH. It doesn't actually do any
  3039. data transfer, it just updates the length counter. */
  3040. static void
  3041. iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
  3042. void *dest __attribute__ ((unused)),
  3043. int kind __attribute__((unused)),
  3044. size_t size, size_t nelems)
  3045. {
  3046. if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
  3047. *dtp->iolength += (GFC_IO_INT) (size * nelems);
  3048. }
  3049. /* Initialize the IOLENGTH data transfer. This function is in essence
  3050. a very much simplified version of data_transfer_init(), because it
  3051. doesn't have to deal with units at all. */
  3052. static void
  3053. iolength_transfer_init (st_parameter_dt *dtp)
  3054. {
  3055. if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
  3056. *dtp->iolength = 0;
  3057. memset (&dtp->u.p, 0, sizeof (dtp->u.p));
  3058. /* Set up the subroutine that will handle the transfers. */
  3059. dtp->u.p.transfer = iolength_transfer;
  3060. }
  3061. /* Library entry point for the IOLENGTH form of the INQUIRE
  3062. statement. The IOLENGTH form requires no I/O to be performed, but
  3063. it must still be a runtime library call so that we can determine
  3064. the iolength for dynamic arrays and such. */
  3065. extern void st_iolength (st_parameter_dt *);
  3066. export_proto(st_iolength);
  3067. void
  3068. st_iolength (st_parameter_dt *dtp)
  3069. {
  3070. library_start (&dtp->common);
  3071. iolength_transfer_init (dtp);
  3072. }
  3073. extern void st_iolength_done (st_parameter_dt *);
  3074. export_proto(st_iolength_done);
  3075. void
  3076. st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
  3077. {
  3078. free_ionml (dtp);
  3079. library_end ();
  3080. }
  3081. /* The READ statement. */
  3082. extern void st_read (st_parameter_dt *);
  3083. export_proto(st_read);
  3084. void
  3085. st_read (st_parameter_dt *dtp)
  3086. {
  3087. library_start (&dtp->common);
  3088. data_transfer_init (dtp, 1);
  3089. }
  3090. extern void st_read_done (st_parameter_dt *);
  3091. export_proto(st_read_done);
  3092. void
  3093. st_read_done (st_parameter_dt *dtp)
  3094. {
  3095. finalize_transfer (dtp);
  3096. if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
  3097. free_format_data (dtp->u.p.fmt);
  3098. free_ionml (dtp);
  3099. if (dtp->u.p.current_unit != NULL)
  3100. unlock_unit (dtp->u.p.current_unit);
  3101. free_internal_unit (dtp);
  3102. library_end ();
  3103. }
  3104. extern void st_write (st_parameter_dt *);
  3105. export_proto(st_write);
  3106. void
  3107. st_write (st_parameter_dt *dtp)
  3108. {
  3109. library_start (&dtp->common);
  3110. data_transfer_init (dtp, 0);
  3111. }
  3112. extern void st_write_done (st_parameter_dt *);
  3113. export_proto(st_write_done);
  3114. void
  3115. st_write_done (st_parameter_dt *dtp)
  3116. {
  3117. finalize_transfer (dtp);
  3118. /* Deal with endfile conditions associated with sequential files. */
  3119. if (dtp->u.p.current_unit != NULL
  3120. && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
  3121. switch (dtp->u.p.current_unit->endfile)
  3122. {
  3123. case AT_ENDFILE: /* Remain at the endfile record. */
  3124. break;
  3125. case AFTER_ENDFILE:
  3126. dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
  3127. break;
  3128. case NO_ENDFILE:
  3129. /* Get rid of whatever is after this record. */
  3130. if (!is_internal_unit (dtp))
  3131. unit_truncate (dtp->u.p.current_unit,
  3132. stell (dtp->u.p.current_unit->s),
  3133. &dtp->common);
  3134. dtp->u.p.current_unit->endfile = AT_ENDFILE;
  3135. break;
  3136. }
  3137. if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
  3138. free_format_data (dtp->u.p.fmt);
  3139. free_ionml (dtp);
  3140. if (dtp->u.p.current_unit != NULL)
  3141. unlock_unit (dtp->u.p.current_unit);
  3142. free_internal_unit (dtp);
  3143. library_end ();
  3144. }
  3145. /* F2003: This is a stub for the runtime portion of the WAIT statement. */
  3146. void
  3147. st_wait (st_parameter_wait *wtp __attribute__((unused)))
  3148. {
  3149. }
  3150. /* Receives the scalar information for namelist objects and stores it
  3151. in a linked list of namelist_info types. */
  3152. extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
  3153. GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
  3154. export_proto(st_set_nml_var);
  3155. void
  3156. st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
  3157. GFC_INTEGER_4 len, gfc_charlen_type string_length,
  3158. GFC_INTEGER_4 dtype)
  3159. {
  3160. namelist_info *t1 = NULL;
  3161. namelist_info *nml;
  3162. size_t var_name_len = strlen (var_name);
  3163. nml = (namelist_info*) xmalloc (sizeof (namelist_info));
  3164. nml->mem_pos = var_addr;
  3165. nml->var_name = (char*) xmalloc (var_name_len + 1);
  3166. memcpy (nml->var_name, var_name, var_name_len);
  3167. nml->var_name[var_name_len] = '\0';
  3168. nml->len = (int) len;
  3169. nml->string_length = (index_type) string_length;
  3170. nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
  3171. nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
  3172. nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
  3173. if (nml->var_rank > 0)
  3174. {
  3175. nml->dim = (descriptor_dimension*)
  3176. xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
  3177. nml->ls = (array_loop_spec*)
  3178. xmallocarray (nml->var_rank, sizeof (array_loop_spec));
  3179. }
  3180. else
  3181. {
  3182. nml->dim = NULL;
  3183. nml->ls = NULL;
  3184. }
  3185. nml->next = NULL;
  3186. if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
  3187. {
  3188. dtp->common.flags |= IOPARM_DT_IONML_SET;
  3189. dtp->u.p.ionml = nml;
  3190. }
  3191. else
  3192. {
  3193. for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
  3194. t1->next = nml;
  3195. }
  3196. }
  3197. /* Store the dimensional information for the namelist object. */
  3198. extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
  3199. index_type, index_type,
  3200. index_type);
  3201. export_proto(st_set_nml_var_dim);
  3202. void
  3203. st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
  3204. index_type stride, index_type lbound,
  3205. index_type ubound)
  3206. {
  3207. namelist_info * nml;
  3208. int n;
  3209. n = (int)n_dim;
  3210. for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
  3211. GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
  3212. }
  3213. /* Once upon a time, a poor innocent Fortran program was reading a
  3214. file, when suddenly it hit the end-of-file (EOF). Unfortunately
  3215. the OS doesn't tell whether we're at the EOF or whether we already
  3216. went past it. Luckily our hero, libgfortran, keeps track of this.
  3217. Call this function when you detect an EOF condition. See Section
  3218. 9.10.2 in F2003. */
  3219. void
  3220. hit_eof (st_parameter_dt * dtp)
  3221. {
  3222. dtp->u.p.current_unit->flags.position = POSITION_APPEND;
  3223. if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
  3224. switch (dtp->u.p.current_unit->endfile)
  3225. {
  3226. case NO_ENDFILE:
  3227. case AT_ENDFILE:
  3228. generate_error (&dtp->common, LIBERROR_END, NULL);
  3229. if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
  3230. {
  3231. dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
  3232. dtp->u.p.current_unit->current_record = 0;
  3233. }
  3234. else
  3235. dtp->u.p.current_unit->endfile = AT_ENDFILE;
  3236. break;
  3237. case AFTER_ENDFILE:
  3238. generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
  3239. dtp->u.p.current_unit->current_record = 0;
  3240. break;
  3241. }
  3242. else
  3243. {
  3244. /* Non-sequential files don't have an ENDFILE record, so we
  3245. can't be at AFTER_ENDFILE. */
  3246. dtp->u.p.current_unit->endfile = AT_ENDFILE;
  3247. generate_error (&dtp->common, LIBERROR_END, NULL);
  3248. dtp->u.p.current_unit->current_record = 0;
  3249. }
  3250. }