list_read.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. Namelist input 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. #include "io.h"
  22. #include "fbuf.h"
  23. #include "unix.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. typedef unsigned char uchar;
  28. /* List directed input. Several parsing subroutines are practically
  29. reimplemented from formatted input, the reason being that there are
  30. all kinds of small differences between formatted and list directed
  31. parsing. */
  32. /* Subroutines for reading characters from the input. Because a
  33. repeat count is ambiguous with an integer, we have to read the
  34. whole digit string before seeing if there is a '*' which signals
  35. the repeat count. Since we can have a lot of potential leading
  36. zeros, we have to be able to back up by arbitrary amount. Because
  37. the input might not be seekable, we have to buffer the data
  38. ourselves. */
  39. #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
  40. case '5': case '6': case '7': case '8': case '9'
  41. #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
  42. case '\r': case ';'
  43. /* This macro assumes that we're operating on a variable. */
  44. #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
  45. || c == '\t' || c == '\r' || c == ';')
  46. /* Maximum repeat count. Less than ten times the maximum signed int32. */
  47. #define MAX_REPEAT 200000000
  48. #define MSGLEN 100
  49. /* Wrappers for calling the current worker functions. */
  50. #define next_char(dtp) ((dtp)->u.p.current_unit->next_char_fn_ptr (dtp))
  51. #define push_char(dtp, c) ((dtp)->u.p.current_unit->push_char_fn_ptr (dtp, c))
  52. /* Worker function to save a default KIND=1 character to a string
  53. buffer, enlarging it as necessary. */
  54. static void
  55. push_char_default (st_parameter_dt *dtp, int c)
  56. {
  57. if (dtp->u.p.saved_string == NULL)
  58. {
  59. // Plain malloc should suffice here, zeroing not needed?
  60. dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, 1);
  61. dtp->u.p.saved_length = SCRATCH_SIZE;
  62. dtp->u.p.saved_used = 0;
  63. }
  64. if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
  65. {
  66. dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
  67. dtp->u.p.saved_string =
  68. xrealloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
  69. // Also this should not be necessary.
  70. memset (dtp->u.p.saved_string + dtp->u.p.saved_used, 0,
  71. dtp->u.p.saved_length - dtp->u.p.saved_used);
  72. }
  73. dtp->u.p.saved_string[dtp->u.p.saved_used++] = (char) c;
  74. }
  75. /* Worker function to save a KIND=4 character to a string buffer,
  76. enlarging the buffer as necessary. */
  77. static void
  78. push_char4 (st_parameter_dt *dtp, int c)
  79. {
  80. gfc_char4_t *new, *p = (gfc_char4_t *) dtp->u.p.saved_string;
  81. if (p == NULL)
  82. {
  83. dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, sizeof (gfc_char4_t));
  84. dtp->u.p.saved_length = SCRATCH_SIZE;
  85. dtp->u.p.saved_used = 0;
  86. p = (gfc_char4_t *) dtp->u.p.saved_string;
  87. }
  88. if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
  89. {
  90. dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
  91. p = xrealloc (p, dtp->u.p.saved_length * sizeof (gfc_char4_t));
  92. memset4 (new + dtp->u.p.saved_used, 0,
  93. dtp->u.p.saved_length - dtp->u.p.saved_used);
  94. }
  95. p[dtp->u.p.saved_used++] = c;
  96. }
  97. /* Free the input buffer if necessary. */
  98. static void
  99. free_saved (st_parameter_dt *dtp)
  100. {
  101. if (dtp->u.p.saved_string == NULL)
  102. return;
  103. free (dtp->u.p.saved_string);
  104. dtp->u.p.saved_string = NULL;
  105. dtp->u.p.saved_used = 0;
  106. }
  107. /* Free the line buffer if necessary. */
  108. static void
  109. free_line (st_parameter_dt *dtp)
  110. {
  111. dtp->u.p.line_buffer_pos = 0;
  112. dtp->u.p.line_buffer_enabled = 0;
  113. if (dtp->u.p.line_buffer == NULL)
  114. return;
  115. free (dtp->u.p.line_buffer);
  116. dtp->u.p.line_buffer = NULL;
  117. }
  118. /* Unget saves the last character so when reading the next character,
  119. we need to check to see if there is a character waiting. Similar,
  120. if the line buffer is being used to read_logical, check it too. */
  121. static int
  122. check_buffers (st_parameter_dt *dtp)
  123. {
  124. int c;
  125. c = '\0';
  126. if (dtp->u.p.last_char != EOF - 1)
  127. {
  128. dtp->u.p.at_eol = 0;
  129. c = dtp->u.p.last_char;
  130. dtp->u.p.last_char = EOF - 1;
  131. goto done;
  132. }
  133. /* Read from line_buffer if enabled. */
  134. if (dtp->u.p.line_buffer_enabled)
  135. {
  136. dtp->u.p.at_eol = 0;
  137. c = dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos];
  138. if (c != '\0' && dtp->u.p.line_buffer_pos < 64)
  139. {
  140. dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos] = '\0';
  141. dtp->u.p.line_buffer_pos++;
  142. goto done;
  143. }
  144. dtp->u.p.line_buffer_pos = 0;
  145. dtp->u.p.line_buffer_enabled = 0;
  146. }
  147. done:
  148. dtp->u.p.at_eol = (c == '\n' || c == EOF);
  149. return c;
  150. }
  151. /* Worker function for default character encoded file. */
  152. static int
  153. next_char_default (st_parameter_dt *dtp)
  154. {
  155. int c;
  156. /* Always check the unget and line buffer first. */
  157. if ((c = check_buffers (dtp)))
  158. return c;
  159. c = fbuf_getc (dtp->u.p.current_unit);
  160. if (c != EOF && is_stream_io (dtp))
  161. dtp->u.p.current_unit->strm_pos++;
  162. dtp->u.p.at_eol = (c == '\n' || c == EOF);
  163. return c;
  164. }
  165. /* Worker function for internal and array I/O units. */
  166. static int
  167. next_char_internal (st_parameter_dt *dtp)
  168. {
  169. ssize_t length;
  170. gfc_offset record;
  171. int c;
  172. /* Always check the unget and line buffer first. */
  173. if ((c = check_buffers (dtp)))
  174. return c;
  175. /* Handle the end-of-record and end-of-file conditions for
  176. internal array unit. */
  177. if (is_array_io (dtp))
  178. {
  179. if (dtp->u.p.at_eof)
  180. return EOF;
  181. /* Check for "end-of-record" condition. */
  182. if (dtp->u.p.current_unit->bytes_left == 0)
  183. {
  184. int finished;
  185. c = '\n';
  186. record = next_array_record (dtp, dtp->u.p.current_unit->ls,
  187. &finished);
  188. /* Check for "end-of-file" condition. */
  189. if (finished)
  190. {
  191. dtp->u.p.at_eof = 1;
  192. goto done;
  193. }
  194. record *= dtp->u.p.current_unit->recl;
  195. if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
  196. return EOF;
  197. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  198. goto done;
  199. }
  200. }
  201. /* Get the next character and handle end-of-record conditions. */
  202. if (dtp->common.unit) /* Check for kind=4 internal unit. */
  203. length = sread (dtp->u.p.current_unit->s, &c, 1);
  204. else
  205. {
  206. char cc;
  207. length = sread (dtp->u.p.current_unit->s, &cc, 1);
  208. c = cc;
  209. }
  210. if (unlikely (length < 0))
  211. {
  212. generate_error (&dtp->common, LIBERROR_OS, NULL);
  213. return '\0';
  214. }
  215. if (is_array_io (dtp))
  216. {
  217. /* Check whether we hit EOF. */
  218. if (unlikely (length == 0))
  219. {
  220. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  221. return '\0';
  222. }
  223. dtp->u.p.current_unit->bytes_left--;
  224. }
  225. else
  226. {
  227. if (dtp->u.p.at_eof)
  228. return EOF;
  229. if (length == 0)
  230. {
  231. c = '\n';
  232. dtp->u.p.at_eof = 1;
  233. }
  234. }
  235. done:
  236. dtp->u.p.at_eol = (c == '\n' || c == EOF);
  237. return c;
  238. }
  239. /* Worker function for UTF encoded files. */
  240. static int
  241. next_char_utf8 (st_parameter_dt *dtp)
  242. {
  243. static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
  244. static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  245. int i, nb;
  246. gfc_char4_t c;
  247. /* Always check the unget and line buffer first. */
  248. if (!(c = check_buffers (dtp)))
  249. c = fbuf_getc (dtp->u.p.current_unit);
  250. if (c < 0x80)
  251. goto utf_done;
  252. /* The number of leading 1-bits in the first byte indicates how many
  253. bytes follow. */
  254. for (nb = 2; nb < 7; nb++)
  255. if ((c & ~masks[nb-1]) == patns[nb-1])
  256. goto found;
  257. goto invalid;
  258. found:
  259. c = (c & masks[nb-1]);
  260. /* Decode the bytes read. */
  261. for (i = 1; i < nb; i++)
  262. {
  263. gfc_char4_t n = fbuf_getc (dtp->u.p.current_unit);
  264. if ((n & 0xC0) != 0x80)
  265. goto invalid;
  266. c = ((c << 6) + (n & 0x3F));
  267. }
  268. /* Make sure the shortest possible encoding was used. */
  269. if (c <= 0x7F && nb > 1) goto invalid;
  270. if (c <= 0x7FF && nb > 2) goto invalid;
  271. if (c <= 0xFFFF && nb > 3) goto invalid;
  272. if (c <= 0x1FFFFF && nb > 4) goto invalid;
  273. if (c <= 0x3FFFFFF && nb > 5) goto invalid;
  274. /* Make sure the character is valid. */
  275. if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
  276. goto invalid;
  277. utf_done:
  278. dtp->u.p.at_eol = (c == '\n' || c == (gfc_char4_t) EOF);
  279. return (int) c;
  280. invalid:
  281. generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
  282. return (gfc_char4_t) '?';
  283. }
  284. /* Push a character back onto the input. */
  285. static void
  286. unget_char (st_parameter_dt *dtp, int c)
  287. {
  288. dtp->u.p.last_char = c;
  289. }
  290. /* Skip over spaces in the input. Returns the nonspace character that
  291. terminated the eating and also places it back on the input. */
  292. static int
  293. eat_spaces (st_parameter_dt *dtp)
  294. {
  295. int c;
  296. /* If internal character array IO, peak ahead and seek past spaces.
  297. This is an optimization unique to character arrays with large
  298. character lengths (PR38199). This code eliminates numerous calls
  299. to next_character. */
  300. if (is_array_io (dtp) && (dtp->u.p.last_char == EOF - 1))
  301. {
  302. gfc_offset offset = stell (dtp->u.p.current_unit->s);
  303. gfc_offset i;
  304. if (dtp->common.unit) /* kind=4 */
  305. {
  306. for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
  307. {
  308. if (dtp->internal_unit[(offset + i) * sizeof (gfc_char4_t)]
  309. != (gfc_char4_t)' ')
  310. break;
  311. }
  312. }
  313. else
  314. {
  315. for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
  316. {
  317. if (dtp->internal_unit[offset + i] != ' ')
  318. break;
  319. }
  320. }
  321. if (i != 0)
  322. {
  323. sseek (dtp->u.p.current_unit->s, offset + i, SEEK_SET);
  324. dtp->u.p.current_unit->bytes_left -= i;
  325. }
  326. }
  327. /* Now skip spaces, EOF and EOL are handled in next_char. */
  328. do
  329. c = next_char (dtp);
  330. while (c != EOF && (c == ' ' || c == '\t'));
  331. unget_char (dtp, c);
  332. return c;
  333. }
  334. /* This function reads characters through to the end of the current
  335. line and just ignores them. Returns 0 for success and LIBERROR_END
  336. if it hit EOF. */
  337. static int
  338. eat_line (st_parameter_dt *dtp)
  339. {
  340. int c;
  341. do
  342. c = next_char (dtp);
  343. while (c != EOF && c != '\n');
  344. if (c == EOF)
  345. return LIBERROR_END;
  346. return 0;
  347. }
  348. /* Skip over a separator. Technically, we don't always eat the whole
  349. separator. This is because if we've processed the last input item,
  350. then a separator is unnecessary. Plus the fact that operating
  351. systems usually deliver console input on a line basis.
  352. The upshot is that if we see a newline as part of reading a
  353. separator, we stop reading. If there are more input items, we
  354. continue reading the separator with finish_separator() which takes
  355. care of the fact that we may or may not have seen a comma as part
  356. of the separator.
  357. Returns 0 for success, and non-zero error code otherwise. */
  358. static int
  359. eat_separator (st_parameter_dt *dtp)
  360. {
  361. int c, n;
  362. int err = 0;
  363. eat_spaces (dtp);
  364. dtp->u.p.comma_flag = 0;
  365. if ((c = next_char (dtp)) == EOF)
  366. return LIBERROR_END;
  367. switch (c)
  368. {
  369. case ',':
  370. if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  371. {
  372. unget_char (dtp, c);
  373. break;
  374. }
  375. /* Fall through. */
  376. case ';':
  377. dtp->u.p.comma_flag = 1;
  378. eat_spaces (dtp);
  379. break;
  380. case '/':
  381. dtp->u.p.input_complete = 1;
  382. break;
  383. case '\r':
  384. if ((n = next_char(dtp)) == EOF)
  385. return LIBERROR_END;
  386. if (n != '\n')
  387. {
  388. unget_char (dtp, n);
  389. break;
  390. }
  391. /* Fall through. */
  392. case '\n':
  393. dtp->u.p.at_eol = 1;
  394. if (dtp->u.p.namelist_mode)
  395. {
  396. do
  397. {
  398. if ((c = next_char (dtp)) == EOF)
  399. return LIBERROR_END;
  400. if (c == '!')
  401. {
  402. err = eat_line (dtp);
  403. if (err)
  404. return err;
  405. c = '\n';
  406. }
  407. }
  408. while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
  409. unget_char (dtp, c);
  410. }
  411. break;
  412. case '!':
  413. if (dtp->u.p.namelist_mode)
  414. { /* Eat a namelist comment. */
  415. err = eat_line (dtp);
  416. if (err)
  417. return err;
  418. break;
  419. }
  420. /* Fall Through... */
  421. default:
  422. unget_char (dtp, c);
  423. break;
  424. }
  425. return err;
  426. }
  427. /* Finish processing a separator that was interrupted by a newline.
  428. If we're here, then another data item is present, so we finish what
  429. we started on the previous line. Return 0 on success, error code
  430. on failure. */
  431. static int
  432. finish_separator (st_parameter_dt *dtp)
  433. {
  434. int c;
  435. int err = LIBERROR_OK;
  436. restart:
  437. eat_spaces (dtp);
  438. if ((c = next_char (dtp)) == EOF)
  439. return LIBERROR_END;
  440. switch (c)
  441. {
  442. case ',':
  443. if (dtp->u.p.comma_flag)
  444. unget_char (dtp, c);
  445. else
  446. {
  447. if ((c = eat_spaces (dtp)) == EOF)
  448. return LIBERROR_END;
  449. if (c == '\n' || c == '\r')
  450. goto restart;
  451. }
  452. break;
  453. case '/':
  454. dtp->u.p.input_complete = 1;
  455. if (!dtp->u.p.namelist_mode)
  456. return err;
  457. break;
  458. case '\n':
  459. case '\r':
  460. goto restart;
  461. case '!':
  462. if (dtp->u.p.namelist_mode)
  463. {
  464. err = eat_line (dtp);
  465. if (err)
  466. return err;
  467. goto restart;
  468. }
  469. /* Fall through. */
  470. default:
  471. unget_char (dtp, c);
  472. break;
  473. }
  474. return err;
  475. }
  476. /* This function is needed to catch bad conversions so that namelist can
  477. attempt to see if dtp->u.p.saved_string contains a new object name rather
  478. than a bad value. */
  479. static int
  480. nml_bad_return (st_parameter_dt *dtp, char c)
  481. {
  482. if (dtp->u.p.namelist_mode)
  483. {
  484. dtp->u.p.nml_read_error = 1;
  485. unget_char (dtp, c);
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. /* Convert an unsigned string to an integer. The length value is -1
  491. if we are working on a repeat count. Returns nonzero if we have a
  492. range problem. As a side effect, frees the dtp->u.p.saved_string. */
  493. static int
  494. convert_integer (st_parameter_dt *dtp, int length, int negative)
  495. {
  496. char c, *buffer, message[MSGLEN];
  497. int m;
  498. GFC_UINTEGER_LARGEST v, max, max10;
  499. GFC_INTEGER_LARGEST value;
  500. buffer = dtp->u.p.saved_string;
  501. v = 0;
  502. if (length == -1)
  503. max = MAX_REPEAT;
  504. else
  505. {
  506. max = si_max (length);
  507. if (negative)
  508. max++;
  509. }
  510. max10 = max / 10;
  511. for (;;)
  512. {
  513. c = *buffer++;
  514. if (c == '\0')
  515. break;
  516. c -= '0';
  517. if (v > max10)
  518. goto overflow;
  519. v = 10 * v;
  520. if (v > max - c)
  521. goto overflow;
  522. v += c;
  523. }
  524. m = 0;
  525. if (length != -1)
  526. {
  527. if (negative)
  528. value = -v;
  529. else
  530. value = v;
  531. set_integer (dtp->u.p.value, value, length);
  532. }
  533. else
  534. {
  535. dtp->u.p.repeat_count = v;
  536. if (dtp->u.p.repeat_count == 0)
  537. {
  538. snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
  539. dtp->u.p.item_count);
  540. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  541. m = 1;
  542. }
  543. }
  544. free_saved (dtp);
  545. return m;
  546. overflow:
  547. if (length == -1)
  548. snprintf (message, MSGLEN, "Repeat count overflow in item %d of list input",
  549. dtp->u.p.item_count);
  550. else
  551. snprintf (message, MSGLEN, "Integer overflow while reading item %d",
  552. dtp->u.p.item_count);
  553. free_saved (dtp);
  554. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  555. return 1;
  556. }
  557. /* Parse a repeat count for logical and complex values which cannot
  558. begin with a digit. Returns nonzero if we are done, zero if we
  559. should continue on. */
  560. static int
  561. parse_repeat (st_parameter_dt *dtp)
  562. {
  563. char message[MSGLEN];
  564. int c, repeat;
  565. if ((c = next_char (dtp)) == EOF)
  566. goto bad_repeat;
  567. switch (c)
  568. {
  569. CASE_DIGITS:
  570. repeat = c - '0';
  571. break;
  572. CASE_SEPARATORS:
  573. unget_char (dtp, c);
  574. eat_separator (dtp);
  575. return 1;
  576. default:
  577. unget_char (dtp, c);
  578. return 0;
  579. }
  580. for (;;)
  581. {
  582. c = next_char (dtp);
  583. switch (c)
  584. {
  585. CASE_DIGITS:
  586. repeat = 10 * repeat + c - '0';
  587. if (repeat > MAX_REPEAT)
  588. {
  589. snprintf (message, MSGLEN,
  590. "Repeat count overflow in item %d of list input",
  591. dtp->u.p.item_count);
  592. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  593. return 1;
  594. }
  595. break;
  596. case '*':
  597. if (repeat == 0)
  598. {
  599. snprintf (message, MSGLEN,
  600. "Zero repeat count in item %d of list input",
  601. dtp->u.p.item_count);
  602. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  603. return 1;
  604. }
  605. goto done;
  606. default:
  607. goto bad_repeat;
  608. }
  609. }
  610. done:
  611. dtp->u.p.repeat_count = repeat;
  612. return 0;
  613. bad_repeat:
  614. free_saved (dtp);
  615. if (c == EOF)
  616. {
  617. free_line (dtp);
  618. hit_eof (dtp);
  619. return 1;
  620. }
  621. else
  622. eat_line (dtp);
  623. snprintf (message, MSGLEN, "Bad repeat count in item %d of list input",
  624. dtp->u.p.item_count);
  625. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  626. return 1;
  627. }
  628. /* To read a logical we have to look ahead in the input stream to make sure
  629. there is not an equal sign indicating a variable name. To do this we use
  630. line_buffer to point to a temporary buffer, pushing characters there for
  631. possible later reading. */
  632. static void
  633. l_push_char (st_parameter_dt *dtp, char c)
  634. {
  635. if (dtp->u.p.line_buffer == NULL)
  636. dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
  637. dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos++] = c;
  638. }
  639. /* Read a logical character on the input. */
  640. static void
  641. read_logical (st_parameter_dt *dtp, int length)
  642. {
  643. char message[MSGLEN];
  644. int c, i, v;
  645. if (parse_repeat (dtp))
  646. return;
  647. c = tolower (next_char (dtp));
  648. l_push_char (dtp, c);
  649. switch (c)
  650. {
  651. case 't':
  652. v = 1;
  653. c = next_char (dtp);
  654. l_push_char (dtp, c);
  655. if (!is_separator(c) && c != EOF)
  656. goto possible_name;
  657. unget_char (dtp, c);
  658. break;
  659. case 'f':
  660. v = 0;
  661. c = next_char (dtp);
  662. l_push_char (dtp, c);
  663. if (!is_separator(c) && c != EOF)
  664. goto possible_name;
  665. unget_char (dtp, c);
  666. break;
  667. case '.':
  668. c = tolower (next_char (dtp));
  669. switch (c)
  670. {
  671. case 't':
  672. v = 1;
  673. break;
  674. case 'f':
  675. v = 0;
  676. break;
  677. default:
  678. goto bad_logical;
  679. }
  680. break;
  681. CASE_SEPARATORS:
  682. case EOF:
  683. unget_char (dtp, c);
  684. eat_separator (dtp);
  685. return; /* Null value. */
  686. default:
  687. /* Save the character in case it is the beginning
  688. of the next object name. */
  689. unget_char (dtp, c);
  690. goto bad_logical;
  691. }
  692. dtp->u.p.saved_type = BT_LOGICAL;
  693. dtp->u.p.saved_length = length;
  694. /* Eat trailing garbage. */
  695. do
  696. c = next_char (dtp);
  697. while (c != EOF && !is_separator (c));
  698. unget_char (dtp, c);
  699. eat_separator (dtp);
  700. set_integer ((int *) dtp->u.p.value, v, length);
  701. free_line (dtp);
  702. return;
  703. possible_name:
  704. for(i = 0; i < 63; i++)
  705. {
  706. c = next_char (dtp);
  707. if (is_separator(c))
  708. {
  709. /* All done if this is not a namelist read. */
  710. if (!dtp->u.p.namelist_mode)
  711. goto logical_done;
  712. unget_char (dtp, c);
  713. eat_separator (dtp);
  714. c = next_char (dtp);
  715. if (c != '=')
  716. {
  717. unget_char (dtp, c);
  718. goto logical_done;
  719. }
  720. }
  721. l_push_char (dtp, c);
  722. if (c == '=')
  723. {
  724. dtp->u.p.nml_read_error = 1;
  725. dtp->u.p.line_buffer_enabled = 1;
  726. dtp->u.p.line_buffer_pos = 0;
  727. return;
  728. }
  729. }
  730. bad_logical:
  731. if (nml_bad_return (dtp, c))
  732. {
  733. free_line (dtp);
  734. return;
  735. }
  736. free_saved (dtp);
  737. if (c == EOF)
  738. {
  739. free_line (dtp);
  740. hit_eof (dtp);
  741. return;
  742. }
  743. else if (c != '\n')
  744. eat_line (dtp);
  745. snprintf (message, MSGLEN, "Bad logical value while reading item %d",
  746. dtp->u.p.item_count);
  747. free_line (dtp);
  748. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  749. return;
  750. logical_done:
  751. dtp->u.p.saved_type = BT_LOGICAL;
  752. dtp->u.p.saved_length = length;
  753. set_integer ((int *) dtp->u.p.value, v, length);
  754. free_saved (dtp);
  755. free_line (dtp);
  756. }
  757. /* Reading integers is tricky because we can actually be reading a
  758. repeat count. We have to store the characters in a buffer because
  759. we could be reading an integer that is larger than the default int
  760. used for repeat counts. */
  761. static void
  762. read_integer (st_parameter_dt *dtp, int length)
  763. {
  764. char message[MSGLEN];
  765. int c, negative;
  766. negative = 0;
  767. c = next_char (dtp);
  768. switch (c)
  769. {
  770. case '-':
  771. negative = 1;
  772. /* Fall through... */
  773. case '+':
  774. if ((c = next_char (dtp)) == EOF)
  775. goto bad_integer;
  776. goto get_integer;
  777. CASE_SEPARATORS: /* Single null. */
  778. unget_char (dtp, c);
  779. eat_separator (dtp);
  780. return;
  781. CASE_DIGITS:
  782. push_char (dtp, c);
  783. break;
  784. default:
  785. goto bad_integer;
  786. }
  787. /* Take care of what may be a repeat count. */
  788. for (;;)
  789. {
  790. c = next_char (dtp);
  791. switch (c)
  792. {
  793. CASE_DIGITS:
  794. push_char (dtp, c);
  795. break;
  796. case '*':
  797. push_char (dtp, '\0');
  798. goto repeat;
  799. CASE_SEPARATORS: /* Not a repeat count. */
  800. case EOF:
  801. goto done;
  802. default:
  803. goto bad_integer;
  804. }
  805. }
  806. repeat:
  807. if (convert_integer (dtp, -1, 0))
  808. return;
  809. /* Get the real integer. */
  810. if ((c = next_char (dtp)) == EOF)
  811. goto bad_integer;
  812. switch (c)
  813. {
  814. CASE_DIGITS:
  815. break;
  816. CASE_SEPARATORS:
  817. unget_char (dtp, c);
  818. eat_separator (dtp);
  819. return;
  820. case '-':
  821. negative = 1;
  822. /* Fall through... */
  823. case '+':
  824. c = next_char (dtp);
  825. break;
  826. }
  827. get_integer:
  828. if (!isdigit (c))
  829. goto bad_integer;
  830. push_char (dtp, c);
  831. for (;;)
  832. {
  833. c = next_char (dtp);
  834. switch (c)
  835. {
  836. CASE_DIGITS:
  837. push_char (dtp, c);
  838. break;
  839. CASE_SEPARATORS:
  840. case EOF:
  841. goto done;
  842. default:
  843. goto bad_integer;
  844. }
  845. }
  846. bad_integer:
  847. if (nml_bad_return (dtp, c))
  848. return;
  849. free_saved (dtp);
  850. if (c == EOF)
  851. {
  852. free_line (dtp);
  853. hit_eof (dtp);
  854. return;
  855. }
  856. else if (c != '\n')
  857. eat_line (dtp);
  858. snprintf (message, MSGLEN, "Bad integer for item %d in list input",
  859. dtp->u.p.item_count);
  860. free_line (dtp);
  861. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  862. return;
  863. done:
  864. unget_char (dtp, c);
  865. eat_separator (dtp);
  866. push_char (dtp, '\0');
  867. if (convert_integer (dtp, length, negative))
  868. {
  869. free_saved (dtp);
  870. return;
  871. }
  872. free_saved (dtp);
  873. dtp->u.p.saved_type = BT_INTEGER;
  874. }
  875. /* Read a character variable. */
  876. static void
  877. read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
  878. {
  879. char quote, message[MSGLEN];
  880. int c;
  881. quote = ' '; /* Space means no quote character. */
  882. if ((c = next_char (dtp)) == EOF)
  883. goto eof;
  884. switch (c)
  885. {
  886. CASE_DIGITS:
  887. push_char (dtp, c);
  888. break;
  889. CASE_SEPARATORS:
  890. case EOF:
  891. unget_char (dtp, c); /* NULL value. */
  892. eat_separator (dtp);
  893. return;
  894. case '"':
  895. case '\'':
  896. quote = c;
  897. goto get_string;
  898. default:
  899. if (dtp->u.p.namelist_mode)
  900. {
  901. if (dtp->u.p.current_unit->delim_status == DELIM_NONE)
  902. {
  903. /* No delimiters so finish reading the string now. */
  904. int i;
  905. push_char (dtp, c);
  906. for (i = dtp->u.p.ionml->string_length; i > 1; i--)
  907. {
  908. if ((c = next_char (dtp)) == EOF)
  909. goto done_eof;
  910. push_char (dtp, c);
  911. }
  912. dtp->u.p.saved_type = BT_CHARACTER;
  913. free_line (dtp);
  914. return;
  915. }
  916. unget_char (dtp, c);
  917. return;
  918. }
  919. push_char (dtp, c);
  920. goto get_string;
  921. }
  922. /* Deal with a possible repeat count. */
  923. for (;;)
  924. {
  925. c = next_char (dtp);
  926. switch (c)
  927. {
  928. CASE_DIGITS:
  929. push_char (dtp, c);
  930. break;
  931. CASE_SEPARATORS:
  932. case EOF:
  933. unget_char (dtp, c);
  934. goto done; /* String was only digits! */
  935. case '*':
  936. push_char (dtp, '\0');
  937. goto got_repeat;
  938. default:
  939. push_char (dtp, c);
  940. goto get_string; /* Not a repeat count after all. */
  941. }
  942. }
  943. got_repeat:
  944. if (convert_integer (dtp, -1, 0))
  945. return;
  946. /* Now get the real string. */
  947. if ((c = next_char (dtp)) == EOF)
  948. goto eof;
  949. switch (c)
  950. {
  951. CASE_SEPARATORS:
  952. unget_char (dtp, c); /* Repeated NULL values. */
  953. eat_separator (dtp);
  954. return;
  955. case '"':
  956. case '\'':
  957. quote = c;
  958. break;
  959. default:
  960. push_char (dtp, c);
  961. break;
  962. }
  963. get_string:
  964. for (;;)
  965. {
  966. if ((c = next_char (dtp)) == EOF)
  967. goto done_eof;
  968. switch (c)
  969. {
  970. case '"':
  971. case '\'':
  972. if (c != quote)
  973. {
  974. push_char (dtp, c);
  975. break;
  976. }
  977. /* See if we have a doubled quote character or the end of
  978. the string. */
  979. if ((c = next_char (dtp)) == EOF)
  980. goto done_eof;
  981. if (c == quote)
  982. {
  983. push_char (dtp, quote);
  984. break;
  985. }
  986. unget_char (dtp, c);
  987. goto done;
  988. CASE_SEPARATORS:
  989. if (quote == ' ')
  990. {
  991. unget_char (dtp, c);
  992. goto done;
  993. }
  994. if (c != '\n' && c != '\r')
  995. push_char (dtp, c);
  996. break;
  997. default:
  998. push_char (dtp, c);
  999. break;
  1000. }
  1001. }
  1002. /* At this point, we have to have a separator, or else the string is
  1003. invalid. */
  1004. done:
  1005. c = next_char (dtp);
  1006. done_eof:
  1007. if (is_separator (c) || c == '!' || c == EOF)
  1008. {
  1009. unget_char (dtp, c);
  1010. eat_separator (dtp);
  1011. dtp->u.p.saved_type = BT_CHARACTER;
  1012. }
  1013. else
  1014. {
  1015. free_saved (dtp);
  1016. snprintf (message, MSGLEN, "Invalid string input in item %d",
  1017. dtp->u.p.item_count);
  1018. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1019. }
  1020. free_line (dtp);
  1021. return;
  1022. eof:
  1023. free_saved (dtp);
  1024. free_line (dtp);
  1025. hit_eof (dtp);
  1026. }
  1027. /* Parse a component of a complex constant or a real number that we
  1028. are sure is already there. This is a straight real number parser. */
  1029. static int
  1030. parse_real (st_parameter_dt *dtp, void *buffer, int length)
  1031. {
  1032. char message[MSGLEN];
  1033. int c, m, seen_dp;
  1034. if ((c = next_char (dtp)) == EOF)
  1035. goto bad;
  1036. if (c == '-' || c == '+')
  1037. {
  1038. push_char (dtp, c);
  1039. if ((c = next_char (dtp)) == EOF)
  1040. goto bad;
  1041. }
  1042. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1043. c = '.';
  1044. if (!isdigit (c) && c != '.')
  1045. {
  1046. if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
  1047. goto inf_nan;
  1048. else
  1049. goto bad;
  1050. }
  1051. push_char (dtp, c);
  1052. seen_dp = (c == '.') ? 1 : 0;
  1053. for (;;)
  1054. {
  1055. if ((c = next_char (dtp)) == EOF)
  1056. goto bad;
  1057. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1058. c = '.';
  1059. switch (c)
  1060. {
  1061. CASE_DIGITS:
  1062. push_char (dtp, c);
  1063. break;
  1064. case '.':
  1065. if (seen_dp)
  1066. goto bad;
  1067. seen_dp = 1;
  1068. push_char (dtp, c);
  1069. break;
  1070. case 'e':
  1071. case 'E':
  1072. case 'd':
  1073. case 'D':
  1074. case 'q':
  1075. case 'Q':
  1076. push_char (dtp, 'e');
  1077. goto exp1;
  1078. case '-':
  1079. case '+':
  1080. push_char (dtp, 'e');
  1081. push_char (dtp, c);
  1082. if ((c = next_char (dtp)) == EOF)
  1083. goto bad;
  1084. goto exp2;
  1085. CASE_SEPARATORS:
  1086. case EOF:
  1087. goto done;
  1088. default:
  1089. goto done;
  1090. }
  1091. }
  1092. exp1:
  1093. if ((c = next_char (dtp)) == EOF)
  1094. goto bad;
  1095. if (c != '-' && c != '+')
  1096. push_char (dtp, '+');
  1097. else
  1098. {
  1099. push_char (dtp, c);
  1100. c = next_char (dtp);
  1101. }
  1102. exp2:
  1103. if (!isdigit (c))
  1104. goto bad;
  1105. push_char (dtp, c);
  1106. for (;;)
  1107. {
  1108. if ((c = next_char (dtp)) == EOF)
  1109. goto bad;
  1110. switch (c)
  1111. {
  1112. CASE_DIGITS:
  1113. push_char (dtp, c);
  1114. break;
  1115. CASE_SEPARATORS:
  1116. case EOF:
  1117. unget_char (dtp, c);
  1118. goto done;
  1119. default:
  1120. goto done;
  1121. }
  1122. }
  1123. done:
  1124. unget_char (dtp, c);
  1125. push_char (dtp, '\0');
  1126. m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
  1127. free_saved (dtp);
  1128. return m;
  1129. done_infnan:
  1130. unget_char (dtp, c);
  1131. push_char (dtp, '\0');
  1132. m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
  1133. free_saved (dtp);
  1134. return m;
  1135. inf_nan:
  1136. /* Match INF and Infinity. */
  1137. if ((c == 'i' || c == 'I')
  1138. && ((c = next_char (dtp)) == 'n' || c == 'N')
  1139. && ((c = next_char (dtp)) == 'f' || c == 'F'))
  1140. {
  1141. c = next_char (dtp);
  1142. if ((c != 'i' && c != 'I')
  1143. || ((c == 'i' || c == 'I')
  1144. && ((c = next_char (dtp)) == 'n' || c == 'N')
  1145. && ((c = next_char (dtp)) == 'i' || c == 'I')
  1146. && ((c = next_char (dtp)) == 't' || c == 'T')
  1147. && ((c = next_char (dtp)) == 'y' || c == 'Y')
  1148. && (c = next_char (dtp))))
  1149. {
  1150. if (is_separator (c) || (c == EOF))
  1151. unget_char (dtp, c);
  1152. push_char (dtp, 'i');
  1153. push_char (dtp, 'n');
  1154. push_char (dtp, 'f');
  1155. goto done_infnan;
  1156. }
  1157. } /* Match NaN. */
  1158. else if (((c = next_char (dtp)) == 'a' || c == 'A')
  1159. && ((c = next_char (dtp)) == 'n' || c == 'N')
  1160. && (c = next_char (dtp)))
  1161. {
  1162. if (is_separator (c) || (c == EOF))
  1163. unget_char (dtp, c);
  1164. push_char (dtp, 'n');
  1165. push_char (dtp, 'a');
  1166. push_char (dtp, 'n');
  1167. /* Match "NAN(alphanum)". */
  1168. if (c == '(')
  1169. {
  1170. for ( ; c != ')'; c = next_char (dtp))
  1171. if (is_separator (c))
  1172. goto bad;
  1173. c = next_char (dtp);
  1174. if (is_separator (c) || (c == EOF))
  1175. unget_char (dtp, c);
  1176. }
  1177. goto done_infnan;
  1178. }
  1179. bad:
  1180. if (nml_bad_return (dtp, c))
  1181. return 0;
  1182. free_saved (dtp);
  1183. if (c == EOF)
  1184. {
  1185. free_line (dtp);
  1186. hit_eof (dtp);
  1187. return 1;
  1188. }
  1189. else if (c != '\n')
  1190. eat_line (dtp);
  1191. snprintf (message, MSGLEN, "Bad floating point number for item %d",
  1192. dtp->u.p.item_count);
  1193. free_line (dtp);
  1194. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1195. return 1;
  1196. }
  1197. /* Reading a complex number is straightforward because we can tell
  1198. what it is right away. */
  1199. static void
  1200. read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
  1201. {
  1202. char message[MSGLEN];
  1203. int c;
  1204. if (parse_repeat (dtp))
  1205. return;
  1206. c = next_char (dtp);
  1207. switch (c)
  1208. {
  1209. case '(':
  1210. break;
  1211. CASE_SEPARATORS:
  1212. case EOF:
  1213. unget_char (dtp, c);
  1214. eat_separator (dtp);
  1215. return;
  1216. default:
  1217. goto bad_complex;
  1218. }
  1219. eol_1:
  1220. eat_spaces (dtp);
  1221. c = next_char (dtp);
  1222. if (c == '\n' || c== '\r')
  1223. goto eol_1;
  1224. else
  1225. unget_char (dtp, c);
  1226. if (parse_real (dtp, dest, kind))
  1227. return;
  1228. eol_2:
  1229. eat_spaces (dtp);
  1230. c = next_char (dtp);
  1231. if (c == '\n' || c== '\r')
  1232. goto eol_2;
  1233. else
  1234. unget_char (dtp, c);
  1235. if (next_char (dtp)
  1236. != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
  1237. goto bad_complex;
  1238. eol_3:
  1239. eat_spaces (dtp);
  1240. c = next_char (dtp);
  1241. if (c == '\n' || c== '\r')
  1242. goto eol_3;
  1243. else
  1244. unget_char (dtp, c);
  1245. if (parse_real (dtp, dest + size / 2, kind))
  1246. return;
  1247. eol_4:
  1248. eat_spaces (dtp);
  1249. c = next_char (dtp);
  1250. if (c == '\n' || c== '\r')
  1251. goto eol_4;
  1252. else
  1253. unget_char (dtp, c);
  1254. if (next_char (dtp) != ')')
  1255. goto bad_complex;
  1256. c = next_char (dtp);
  1257. if (!is_separator (c) && (c != EOF))
  1258. goto bad_complex;
  1259. unget_char (dtp, c);
  1260. eat_separator (dtp);
  1261. free_saved (dtp);
  1262. dtp->u.p.saved_type = BT_COMPLEX;
  1263. return;
  1264. bad_complex:
  1265. if (nml_bad_return (dtp, c))
  1266. return;
  1267. free_saved (dtp);
  1268. if (c == EOF)
  1269. {
  1270. free_line (dtp);
  1271. hit_eof (dtp);
  1272. return;
  1273. }
  1274. else if (c != '\n')
  1275. eat_line (dtp);
  1276. snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
  1277. dtp->u.p.item_count);
  1278. free_line (dtp);
  1279. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1280. }
  1281. /* Parse a real number with a possible repeat count. */
  1282. static void
  1283. read_real (st_parameter_dt *dtp, void * dest, int length)
  1284. {
  1285. char message[MSGLEN];
  1286. int c;
  1287. int seen_dp;
  1288. int is_inf;
  1289. seen_dp = 0;
  1290. c = next_char (dtp);
  1291. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1292. c = '.';
  1293. switch (c)
  1294. {
  1295. CASE_DIGITS:
  1296. push_char (dtp, c);
  1297. break;
  1298. case '.':
  1299. push_char (dtp, c);
  1300. seen_dp = 1;
  1301. break;
  1302. case '+':
  1303. case '-':
  1304. goto got_sign;
  1305. CASE_SEPARATORS:
  1306. unget_char (dtp, c); /* Single null. */
  1307. eat_separator (dtp);
  1308. return;
  1309. case 'i':
  1310. case 'I':
  1311. case 'n':
  1312. case 'N':
  1313. goto inf_nan;
  1314. default:
  1315. goto bad_real;
  1316. }
  1317. /* Get the digit string that might be a repeat count. */
  1318. for (;;)
  1319. {
  1320. c = next_char (dtp);
  1321. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1322. c = '.';
  1323. switch (c)
  1324. {
  1325. CASE_DIGITS:
  1326. push_char (dtp, c);
  1327. break;
  1328. case '.':
  1329. if (seen_dp)
  1330. goto bad_real;
  1331. seen_dp = 1;
  1332. push_char (dtp, c);
  1333. goto real_loop;
  1334. case 'E':
  1335. case 'e':
  1336. case 'D':
  1337. case 'd':
  1338. case 'Q':
  1339. case 'q':
  1340. goto exp1;
  1341. case '+':
  1342. case '-':
  1343. push_char (dtp, 'e');
  1344. push_char (dtp, c);
  1345. c = next_char (dtp);
  1346. goto exp2;
  1347. case '*':
  1348. push_char (dtp, '\0');
  1349. goto got_repeat;
  1350. CASE_SEPARATORS:
  1351. case EOF:
  1352. if (c != '\n' && c != ',' && c != '\r' && c != ';')
  1353. unget_char (dtp, c);
  1354. goto done;
  1355. default:
  1356. goto bad_real;
  1357. }
  1358. }
  1359. got_repeat:
  1360. if (convert_integer (dtp, -1, 0))
  1361. return;
  1362. /* Now get the number itself. */
  1363. if ((c = next_char (dtp)) == EOF)
  1364. goto bad_real;
  1365. if (is_separator (c))
  1366. { /* Repeated null value. */
  1367. unget_char (dtp, c);
  1368. eat_separator (dtp);
  1369. return;
  1370. }
  1371. if (c != '-' && c != '+')
  1372. push_char (dtp, '+');
  1373. else
  1374. {
  1375. got_sign:
  1376. push_char (dtp, c);
  1377. if ((c = next_char (dtp)) == EOF)
  1378. goto bad_real;
  1379. }
  1380. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1381. c = '.';
  1382. if (!isdigit (c) && c != '.')
  1383. {
  1384. if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
  1385. goto inf_nan;
  1386. else
  1387. goto bad_real;
  1388. }
  1389. if (c == '.')
  1390. {
  1391. if (seen_dp)
  1392. goto bad_real;
  1393. else
  1394. seen_dp = 1;
  1395. }
  1396. push_char (dtp, c);
  1397. real_loop:
  1398. for (;;)
  1399. {
  1400. c = next_char (dtp);
  1401. if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
  1402. c = '.';
  1403. switch (c)
  1404. {
  1405. CASE_DIGITS:
  1406. push_char (dtp, c);
  1407. break;
  1408. CASE_SEPARATORS:
  1409. case EOF:
  1410. goto done;
  1411. case '.':
  1412. if (seen_dp)
  1413. goto bad_real;
  1414. seen_dp = 1;
  1415. push_char (dtp, c);
  1416. break;
  1417. case 'E':
  1418. case 'e':
  1419. case 'D':
  1420. case 'd':
  1421. case 'Q':
  1422. case 'q':
  1423. goto exp1;
  1424. case '+':
  1425. case '-':
  1426. push_char (dtp, 'e');
  1427. push_char (dtp, c);
  1428. c = next_char (dtp);
  1429. goto exp2;
  1430. default:
  1431. goto bad_real;
  1432. }
  1433. }
  1434. exp1:
  1435. push_char (dtp, 'e');
  1436. if ((c = next_char (dtp)) == EOF)
  1437. goto bad_real;
  1438. if (c != '+' && c != '-')
  1439. push_char (dtp, '+');
  1440. else
  1441. {
  1442. push_char (dtp, c);
  1443. c = next_char (dtp);
  1444. }
  1445. exp2:
  1446. if (!isdigit (c))
  1447. goto bad_real;
  1448. push_char (dtp, c);
  1449. for (;;)
  1450. {
  1451. c = next_char (dtp);
  1452. switch (c)
  1453. {
  1454. CASE_DIGITS:
  1455. push_char (dtp, c);
  1456. break;
  1457. CASE_SEPARATORS:
  1458. case EOF:
  1459. goto done;
  1460. default:
  1461. goto bad_real;
  1462. }
  1463. }
  1464. done:
  1465. unget_char (dtp, c);
  1466. eat_separator (dtp);
  1467. push_char (dtp, '\0');
  1468. if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
  1469. {
  1470. free_saved (dtp);
  1471. return;
  1472. }
  1473. free_saved (dtp);
  1474. dtp->u.p.saved_type = BT_REAL;
  1475. return;
  1476. inf_nan:
  1477. l_push_char (dtp, c);
  1478. is_inf = 0;
  1479. /* Match INF and Infinity. */
  1480. if (c == 'i' || c == 'I')
  1481. {
  1482. c = next_char (dtp);
  1483. l_push_char (dtp, c);
  1484. if (c != 'n' && c != 'N')
  1485. goto unwind;
  1486. c = next_char (dtp);
  1487. l_push_char (dtp, c);
  1488. if (c != 'f' && c != 'F')
  1489. goto unwind;
  1490. c = next_char (dtp);
  1491. l_push_char (dtp, c);
  1492. if (!is_separator (c) && (c != EOF))
  1493. {
  1494. if (c != 'i' && c != 'I')
  1495. goto unwind;
  1496. c = next_char (dtp);
  1497. l_push_char (dtp, c);
  1498. if (c != 'n' && c != 'N')
  1499. goto unwind;
  1500. c = next_char (dtp);
  1501. l_push_char (dtp, c);
  1502. if (c != 'i' && c != 'I')
  1503. goto unwind;
  1504. c = next_char (dtp);
  1505. l_push_char (dtp, c);
  1506. if (c != 't' && c != 'T')
  1507. goto unwind;
  1508. c = next_char (dtp);
  1509. l_push_char (dtp, c);
  1510. if (c != 'y' && c != 'Y')
  1511. goto unwind;
  1512. c = next_char (dtp);
  1513. l_push_char (dtp, c);
  1514. }
  1515. is_inf = 1;
  1516. } /* Match NaN. */
  1517. else
  1518. {
  1519. c = next_char (dtp);
  1520. l_push_char (dtp, c);
  1521. if (c != 'a' && c != 'A')
  1522. goto unwind;
  1523. c = next_char (dtp);
  1524. l_push_char (dtp, c);
  1525. if (c != 'n' && c != 'N')
  1526. goto unwind;
  1527. c = next_char (dtp);
  1528. l_push_char (dtp, c);
  1529. /* Match NAN(alphanum). */
  1530. if (c == '(')
  1531. {
  1532. for (c = next_char (dtp); c != ')'; c = next_char (dtp))
  1533. if (is_separator (c))
  1534. goto unwind;
  1535. else
  1536. l_push_char (dtp, c);
  1537. l_push_char (dtp, ')');
  1538. c = next_char (dtp);
  1539. l_push_char (dtp, c);
  1540. }
  1541. }
  1542. if (!is_separator (c) && (c != EOF))
  1543. goto unwind;
  1544. if (dtp->u.p.namelist_mode)
  1545. {
  1546. if (c == ' ' || c =='\n' || c == '\r')
  1547. {
  1548. do
  1549. {
  1550. if ((c = next_char (dtp)) == EOF)
  1551. goto bad_real;
  1552. }
  1553. while (c == ' ' || c =='\n' || c == '\r');
  1554. l_push_char (dtp, c);
  1555. if (c == '=')
  1556. goto unwind;
  1557. }
  1558. }
  1559. if (is_inf)
  1560. {
  1561. push_char (dtp, 'i');
  1562. push_char (dtp, 'n');
  1563. push_char (dtp, 'f');
  1564. }
  1565. else
  1566. {
  1567. push_char (dtp, 'n');
  1568. push_char (dtp, 'a');
  1569. push_char (dtp, 'n');
  1570. }
  1571. free_line (dtp);
  1572. unget_char (dtp, c);
  1573. eat_separator (dtp);
  1574. push_char (dtp, '\0');
  1575. if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
  1576. return;
  1577. free_saved (dtp);
  1578. dtp->u.p.saved_type = BT_REAL;
  1579. return;
  1580. unwind:
  1581. if (dtp->u.p.namelist_mode)
  1582. {
  1583. dtp->u.p.nml_read_error = 1;
  1584. dtp->u.p.line_buffer_enabled = 1;
  1585. dtp->u.p.line_buffer_pos = 0;
  1586. return;
  1587. }
  1588. bad_real:
  1589. if (nml_bad_return (dtp, c))
  1590. return;
  1591. free_saved (dtp);
  1592. if (c == EOF)
  1593. {
  1594. free_line (dtp);
  1595. hit_eof (dtp);
  1596. return;
  1597. }
  1598. else if (c != '\n')
  1599. eat_line (dtp);
  1600. snprintf (message, MSGLEN, "Bad real number in item %d of list input",
  1601. dtp->u.p.item_count);
  1602. free_line (dtp);
  1603. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1604. }
  1605. /* Check the current type against the saved type to make sure they are
  1606. compatible. Returns nonzero if incompatible. */
  1607. static int
  1608. check_type (st_parameter_dt *dtp, bt type, int kind)
  1609. {
  1610. char message[MSGLEN];
  1611. if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
  1612. {
  1613. snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
  1614. type_name (dtp->u.p.saved_type), type_name (type),
  1615. dtp->u.p.item_count);
  1616. free_line (dtp);
  1617. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1618. return 1;
  1619. }
  1620. if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
  1621. return 0;
  1622. if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
  1623. || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
  1624. {
  1625. snprintf (message, MSGLEN,
  1626. "Read kind %d %s where kind %d is required for item %d",
  1627. type == BT_COMPLEX ? dtp->u.p.saved_length / 2
  1628. : dtp->u.p.saved_length,
  1629. type_name (dtp->u.p.saved_type), kind,
  1630. dtp->u.p.item_count);
  1631. free_line (dtp);
  1632. generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
  1633. return 1;
  1634. }
  1635. return 0;
  1636. }
  1637. /* Initialize the function pointers to select the correct versions of
  1638. next_char and push_char depending on what we are doing. */
  1639. static void
  1640. set_workers (st_parameter_dt *dtp)
  1641. {
  1642. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  1643. {
  1644. dtp->u.p.current_unit->next_char_fn_ptr = &next_char_utf8;
  1645. dtp->u.p.current_unit->push_char_fn_ptr = &push_char4;
  1646. }
  1647. else if (is_internal_unit (dtp))
  1648. {
  1649. dtp->u.p.current_unit->next_char_fn_ptr = &next_char_internal;
  1650. dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
  1651. }
  1652. else
  1653. {
  1654. dtp->u.p.current_unit->next_char_fn_ptr = &next_char_default;
  1655. dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
  1656. }
  1657. }
  1658. /* Top level data transfer subroutine for list reads. Because we have
  1659. to deal with repeat counts, the data item is always saved after
  1660. reading, usually in the dtp->u.p.value[] array. If a repeat count is
  1661. greater than one, we copy the data item multiple times. */
  1662. static int
  1663. list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
  1664. int kind, size_t size)
  1665. {
  1666. gfc_char4_t *q, *r;
  1667. int c, i, m;
  1668. int err = 0;
  1669. dtp->u.p.namelist_mode = 0;
  1670. /* Set the next_char and push_char worker functions. */
  1671. set_workers (dtp);
  1672. if (dtp->u.p.first_item)
  1673. {
  1674. dtp->u.p.first_item = 0;
  1675. dtp->u.p.input_complete = 0;
  1676. dtp->u.p.repeat_count = 1;
  1677. dtp->u.p.at_eol = 0;
  1678. if ((c = eat_spaces (dtp)) == EOF)
  1679. {
  1680. err = LIBERROR_END;
  1681. goto cleanup;
  1682. }
  1683. if (is_separator (c))
  1684. {
  1685. /* Found a null value. */
  1686. dtp->u.p.repeat_count = 0;
  1687. eat_separator (dtp);
  1688. /* Set end-of-line flag. */
  1689. if (c == '\n' || c == '\r')
  1690. {
  1691. dtp->u.p.at_eol = 1;
  1692. if (finish_separator (dtp) == LIBERROR_END)
  1693. {
  1694. err = LIBERROR_END;
  1695. goto cleanup;
  1696. }
  1697. }
  1698. else
  1699. goto cleanup;
  1700. }
  1701. }
  1702. else
  1703. {
  1704. if (dtp->u.p.repeat_count > 0)
  1705. {
  1706. if (check_type (dtp, type, kind))
  1707. return err;
  1708. goto set_value;
  1709. }
  1710. if (dtp->u.p.input_complete)
  1711. goto cleanup;
  1712. if (dtp->u.p.at_eol)
  1713. finish_separator (dtp);
  1714. else
  1715. {
  1716. eat_spaces (dtp);
  1717. /* Trailing spaces prior to end of line. */
  1718. if (dtp->u.p.at_eol)
  1719. finish_separator (dtp);
  1720. }
  1721. dtp->u.p.saved_type = BT_UNKNOWN;
  1722. dtp->u.p.repeat_count = 1;
  1723. }
  1724. switch (type)
  1725. {
  1726. case BT_INTEGER:
  1727. read_integer (dtp, kind);
  1728. break;
  1729. case BT_LOGICAL:
  1730. read_logical (dtp, kind);
  1731. break;
  1732. case BT_CHARACTER:
  1733. read_character (dtp, kind);
  1734. break;
  1735. case BT_REAL:
  1736. read_real (dtp, p, kind);
  1737. /* Copy value back to temporary if needed. */
  1738. if (dtp->u.p.repeat_count > 0)
  1739. memcpy (dtp->u.p.value, p, size);
  1740. break;
  1741. case BT_COMPLEX:
  1742. read_complex (dtp, p, kind, size);
  1743. /* Copy value back to temporary if needed. */
  1744. if (dtp->u.p.repeat_count > 0)
  1745. memcpy (dtp->u.p.value, p, size);
  1746. break;
  1747. default:
  1748. internal_error (&dtp->common, "Bad type for list read");
  1749. }
  1750. if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
  1751. dtp->u.p.saved_length = size;
  1752. if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  1753. goto cleanup;
  1754. set_value:
  1755. switch (dtp->u.p.saved_type)
  1756. {
  1757. case BT_COMPLEX:
  1758. case BT_REAL:
  1759. if (dtp->u.p.repeat_count > 0)
  1760. memcpy (p, dtp->u.p.value, size);
  1761. break;
  1762. case BT_INTEGER:
  1763. case BT_LOGICAL:
  1764. memcpy (p, dtp->u.p.value, size);
  1765. break;
  1766. case BT_CHARACTER:
  1767. if (dtp->u.p.saved_string)
  1768. {
  1769. m = ((int) size < dtp->u.p.saved_used)
  1770. ? (int) size : dtp->u.p.saved_used;
  1771. q = (gfc_char4_t *) p;
  1772. r = (gfc_char4_t *) dtp->u.p.saved_string;
  1773. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  1774. for (i = 0; i < m; i++)
  1775. *q++ = *r++;
  1776. else
  1777. {
  1778. if (kind == 1)
  1779. memcpy (p, dtp->u.p.saved_string, m);
  1780. else
  1781. for (i = 0; i < m; i++)
  1782. *q++ = *r++;
  1783. }
  1784. }
  1785. else
  1786. /* Just delimiters encountered, nothing to copy but SPACE. */
  1787. m = 0;
  1788. if (m < (int) size)
  1789. {
  1790. if (kind == 1)
  1791. memset (((char *) p) + m, ' ', size - m);
  1792. else
  1793. {
  1794. q = (gfc_char4_t *) p;
  1795. for (i = m; i < (int) size; i++)
  1796. q[i] = (unsigned char) ' ';
  1797. }
  1798. }
  1799. break;
  1800. case BT_UNKNOWN:
  1801. break;
  1802. default:
  1803. internal_error (&dtp->common, "Bad type for list read");
  1804. }
  1805. if (--dtp->u.p.repeat_count <= 0)
  1806. free_saved (dtp);
  1807. cleanup:
  1808. if (err == LIBERROR_END)
  1809. {
  1810. free_line (dtp);
  1811. hit_eof (dtp);
  1812. }
  1813. fbuf_flush_list (dtp->u.p.current_unit, LIST_READING);
  1814. return err;
  1815. }
  1816. void
  1817. list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
  1818. size_t size, size_t nelems)
  1819. {
  1820. size_t elem;
  1821. char *tmp;
  1822. size_t stride = type == BT_CHARACTER ?
  1823. size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
  1824. int err;
  1825. tmp = (char *) p;
  1826. /* Big loop over all the elements. */
  1827. for (elem = 0; elem < nelems; elem++)
  1828. {
  1829. dtp->u.p.item_count++;
  1830. err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
  1831. kind, size);
  1832. if (err)
  1833. break;
  1834. }
  1835. }
  1836. /* Finish a list read. */
  1837. void
  1838. finish_list_read (st_parameter_dt *dtp)
  1839. {
  1840. free_saved (dtp);
  1841. fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
  1842. if (dtp->u.p.at_eol)
  1843. {
  1844. dtp->u.p.at_eol = 0;
  1845. return;
  1846. }
  1847. if (!is_internal_unit (dtp))
  1848. {
  1849. int c;
  1850. /* Set the next_char and push_char worker functions. */
  1851. set_workers (dtp);
  1852. c = next_char (dtp);
  1853. if (c == EOF)
  1854. {
  1855. free_line (dtp);
  1856. hit_eof (dtp);
  1857. return;
  1858. }
  1859. if (c != '\n')
  1860. eat_line (dtp);
  1861. }
  1862. free_line (dtp);
  1863. }
  1864. /* NAMELIST INPUT
  1865. void namelist_read (st_parameter_dt *dtp)
  1866. calls:
  1867. static void nml_match_name (char *name, int len)
  1868. static int nml_query (st_parameter_dt *dtp)
  1869. static int nml_get_obj_data (st_parameter_dt *dtp,
  1870. namelist_info **prev_nl, char *, size_t)
  1871. calls:
  1872. static void nml_untouch_nodes (st_parameter_dt *dtp)
  1873. static namelist_info * find_nml_node (st_parameter_dt *dtp,
  1874. char * var_name)
  1875. static int nml_parse_qualifier(descriptor_dimension * ad,
  1876. array_loop_spec * ls, int rank, char *)
  1877. static void nml_touch_nodes (namelist_info * nl)
  1878. static int nml_read_obj (namelist_info *nl, index_type offset,
  1879. namelist_info **prev_nl, char *, size_t,
  1880. index_type clow, index_type chigh)
  1881. calls:
  1882. -itself- */
  1883. /* Inputs a rank-dimensional qualifier, which can contain
  1884. singlets, doublets, triplets or ':' with the standard meanings. */
  1885. static bool
  1886. nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
  1887. array_loop_spec *ls, int rank, bt nml_elem_type,
  1888. char *parse_err_msg, size_t parse_err_msg_size,
  1889. int *parsed_rank)
  1890. {
  1891. int dim;
  1892. int indx;
  1893. int neg;
  1894. int null_flag;
  1895. int is_array_section, is_char;
  1896. int c;
  1897. is_char = 0;
  1898. is_array_section = 0;
  1899. dtp->u.p.expanded_read = 0;
  1900. /* See if this is a character substring qualifier we are looking for. */
  1901. if (rank == -1)
  1902. {
  1903. rank = 1;
  1904. is_char = 1;
  1905. }
  1906. /* The next character in the stream should be the '('. */
  1907. if ((c = next_char (dtp)) == EOF)
  1908. goto err_ret;
  1909. /* Process the qualifier, by dimension and triplet. */
  1910. for (dim=0; dim < rank; dim++ )
  1911. {
  1912. for (indx=0; indx<3; indx++)
  1913. {
  1914. free_saved (dtp);
  1915. eat_spaces (dtp);
  1916. neg = 0;
  1917. /* Process a potential sign. */
  1918. if ((c = next_char (dtp)) == EOF)
  1919. goto err_ret;
  1920. switch (c)
  1921. {
  1922. case '-':
  1923. neg = 1;
  1924. break;
  1925. case '+':
  1926. break;
  1927. default:
  1928. unget_char (dtp, c);
  1929. break;
  1930. }
  1931. /* Process characters up to the next ':' , ',' or ')'. */
  1932. for (;;)
  1933. {
  1934. c = next_char (dtp);
  1935. switch (c)
  1936. {
  1937. case EOF:
  1938. goto err_ret;
  1939. case ':':
  1940. is_array_section = 1;
  1941. break;
  1942. case ',': case ')':
  1943. if ((c==',' && dim == rank -1)
  1944. || (c==')' && dim < rank -1))
  1945. {
  1946. if (is_char)
  1947. snprintf (parse_err_msg, parse_err_msg_size,
  1948. "Bad substring qualifier");
  1949. else
  1950. snprintf (parse_err_msg, parse_err_msg_size,
  1951. "Bad number of index fields");
  1952. goto err_ret;
  1953. }
  1954. break;
  1955. CASE_DIGITS:
  1956. push_char (dtp, c);
  1957. continue;
  1958. case ' ': case '\t': case '\r': case '\n':
  1959. eat_spaces (dtp);
  1960. break;
  1961. default:
  1962. if (is_char)
  1963. snprintf (parse_err_msg, parse_err_msg_size,
  1964. "Bad character in substring qualifier");
  1965. else
  1966. snprintf (parse_err_msg, parse_err_msg_size,
  1967. "Bad character in index");
  1968. goto err_ret;
  1969. }
  1970. if ((c == ',' || c == ')') && indx == 0
  1971. && dtp->u.p.saved_string == 0)
  1972. {
  1973. if (is_char)
  1974. snprintf (parse_err_msg, parse_err_msg_size,
  1975. "Null substring qualifier");
  1976. else
  1977. snprintf (parse_err_msg, parse_err_msg_size,
  1978. "Null index field");
  1979. goto err_ret;
  1980. }
  1981. if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
  1982. || (indx == 2 && dtp->u.p.saved_string == 0))
  1983. {
  1984. if (is_char)
  1985. snprintf (parse_err_msg, parse_err_msg_size,
  1986. "Bad substring qualifier");
  1987. else
  1988. snprintf (parse_err_msg, parse_err_msg_size,
  1989. "Bad index triplet");
  1990. goto err_ret;
  1991. }
  1992. if (is_char && !is_array_section)
  1993. {
  1994. snprintf (parse_err_msg, parse_err_msg_size,
  1995. "Missing colon in substring qualifier");
  1996. goto err_ret;
  1997. }
  1998. /* If '( : ? )' or '( ? : )' break and flag read failure. */
  1999. null_flag = 0;
  2000. if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
  2001. || (indx==1 && dtp->u.p.saved_string == 0))
  2002. {
  2003. null_flag = 1;
  2004. break;
  2005. }
  2006. /* Now read the index. */
  2007. if (convert_integer (dtp, sizeof(index_type), neg))
  2008. {
  2009. if (is_char)
  2010. snprintf (parse_err_msg, parse_err_msg_size,
  2011. "Bad integer substring qualifier");
  2012. else
  2013. snprintf (parse_err_msg, parse_err_msg_size,
  2014. "Bad integer in index");
  2015. goto err_ret;
  2016. }
  2017. break;
  2018. }
  2019. /* Feed the index values to the triplet arrays. */
  2020. if (!null_flag)
  2021. {
  2022. if (indx == 0)
  2023. memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
  2024. if (indx == 1)
  2025. memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
  2026. if (indx == 2)
  2027. memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
  2028. }
  2029. /* Singlet or doublet indices. */
  2030. if (c==',' || c==')')
  2031. {
  2032. if (indx == 0)
  2033. {
  2034. memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
  2035. /* If -std=f95/2003 or an array section is specified,
  2036. do not allow excess data to be processed. */
  2037. if (is_array_section == 1
  2038. || !(compile_options.allow_std & GFC_STD_GNU)
  2039. || nml_elem_type == BT_DERIVED)
  2040. ls[dim].end = ls[dim].start;
  2041. else
  2042. dtp->u.p.expanded_read = 1;
  2043. }
  2044. /* Check for non-zero rank. */
  2045. if (is_array_section == 1 && ls[dim].start != ls[dim].end)
  2046. *parsed_rank = 1;
  2047. break;
  2048. }
  2049. }
  2050. if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
  2051. {
  2052. int i;
  2053. dtp->u.p.expanded_read = 0;
  2054. for (i = 0; i < dim; i++)
  2055. ls[i].end = ls[i].start;
  2056. }
  2057. /* Check the values of the triplet indices. */
  2058. if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
  2059. || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
  2060. || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
  2061. || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
  2062. {
  2063. if (is_char)
  2064. snprintf (parse_err_msg, parse_err_msg_size,
  2065. "Substring out of range");
  2066. else
  2067. snprintf (parse_err_msg, parse_err_msg_size,
  2068. "Index %d out of range", dim + 1);
  2069. goto err_ret;
  2070. }
  2071. if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
  2072. || (ls[dim].step == 0))
  2073. {
  2074. snprintf (parse_err_msg, parse_err_msg_size,
  2075. "Bad range in index %d", dim + 1);
  2076. goto err_ret;
  2077. }
  2078. /* Initialise the loop index counter. */
  2079. ls[dim].idx = ls[dim].start;
  2080. }
  2081. eat_spaces (dtp);
  2082. return true;
  2083. err_ret:
  2084. /* The EOF error message is issued by hit_eof. Return true so that the
  2085. caller does not use parse_err_msg and parse_err_msg_size to generate
  2086. an unrelated error message. */
  2087. if (c == EOF)
  2088. {
  2089. hit_eof (dtp);
  2090. dtp->u.p.input_complete = 1;
  2091. return true;
  2092. }
  2093. return false;
  2094. }
  2095. static bool
  2096. extended_look_ahead (char *p, char *q)
  2097. {
  2098. char *r, *s;
  2099. /* Scan ahead to find a '%' in the p string. */
  2100. for(r = p, s = q; *r && *s; s++)
  2101. if ((*s == '%' || *s == '+') && strcmp (r + 1, s + 1) == 0)
  2102. return true;
  2103. return false;
  2104. }
  2105. static bool
  2106. strcmp_extended_type (char *p, char *q)
  2107. {
  2108. char *r, *s;
  2109. for (r = p, s = q; *r && *s; r++, s++)
  2110. {
  2111. if (*r != *s)
  2112. {
  2113. if (*r == '%' && *s == '+' && extended_look_ahead (r, s))
  2114. return true;
  2115. break;
  2116. }
  2117. }
  2118. return false;
  2119. }
  2120. static namelist_info *
  2121. find_nml_node (st_parameter_dt *dtp, char * var_name)
  2122. {
  2123. namelist_info * t = dtp->u.p.ionml;
  2124. while (t != NULL)
  2125. {
  2126. if (strcmp (var_name, t->var_name) == 0)
  2127. {
  2128. t->touched = 1;
  2129. return t;
  2130. }
  2131. if (strcmp_extended_type (var_name, t->var_name))
  2132. {
  2133. t->touched = 1;
  2134. return t;
  2135. }
  2136. t = t->next;
  2137. }
  2138. return NULL;
  2139. }
  2140. /* Visits all the components of a derived type that have
  2141. not explicitly been identified in the namelist input.
  2142. touched is set and the loop specification initialised
  2143. to default values */
  2144. static void
  2145. nml_touch_nodes (namelist_info * nl)
  2146. {
  2147. index_type len = strlen (nl->var_name) + 1;
  2148. int dim;
  2149. char * ext_name = xmalloc (len + 1);
  2150. memcpy (ext_name, nl->var_name, len-1);
  2151. memcpy (ext_name + len - 1, "%", 2);
  2152. for (nl = nl->next; nl; nl = nl->next)
  2153. {
  2154. if (strncmp (nl->var_name, ext_name, len) == 0)
  2155. {
  2156. nl->touched = 1;
  2157. for (dim=0; dim < nl->var_rank; dim++)
  2158. {
  2159. nl->ls[dim].step = 1;
  2160. nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
  2161. nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
  2162. nl->ls[dim].idx = nl->ls[dim].start;
  2163. }
  2164. }
  2165. else
  2166. break;
  2167. }
  2168. free (ext_name);
  2169. return;
  2170. }
  2171. /* Resets touched for the entire list of nml_nodes, ready for a
  2172. new object. */
  2173. static void
  2174. nml_untouch_nodes (st_parameter_dt *dtp)
  2175. {
  2176. namelist_info * t;
  2177. for (t = dtp->u.p.ionml; t; t = t->next)
  2178. t->touched = 0;
  2179. return;
  2180. }
  2181. /* Attempts to input name to namelist name. Returns
  2182. dtp->u.p.nml_read_error = 1 on no match. */
  2183. static void
  2184. nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
  2185. {
  2186. index_type i;
  2187. int c;
  2188. dtp->u.p.nml_read_error = 0;
  2189. for (i = 0; i < len; i++)
  2190. {
  2191. c = next_char (dtp);
  2192. if (c == EOF || (tolower (c) != tolower (name[i])))
  2193. {
  2194. dtp->u.p.nml_read_error = 1;
  2195. break;
  2196. }
  2197. }
  2198. }
  2199. /* If the namelist read is from stdin, output the current state of the
  2200. namelist to stdout. This is used to implement the non-standard query
  2201. features, ? and =?. If c == '=' the full namelist is printed. Otherwise
  2202. the names alone are printed. */
  2203. static void
  2204. nml_query (st_parameter_dt *dtp, char c)
  2205. {
  2206. gfc_unit * temp_unit;
  2207. namelist_info * nl;
  2208. index_type len;
  2209. char * p;
  2210. #ifdef HAVE_CRLF
  2211. static const index_type endlen = 2;
  2212. static const char endl[] = "\r\n";
  2213. static const char nmlend[] = "&end\r\n";
  2214. #else
  2215. static const index_type endlen = 1;
  2216. static const char endl[] = "\n";
  2217. static const char nmlend[] = "&end\n";
  2218. #endif
  2219. if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
  2220. return;
  2221. /* Store the current unit and transfer to stdout. */
  2222. temp_unit = dtp->u.p.current_unit;
  2223. dtp->u.p.current_unit = find_unit (options.stdout_unit);
  2224. if (dtp->u.p.current_unit)
  2225. {
  2226. dtp->u.p.mode = WRITING;
  2227. next_record (dtp, 0);
  2228. /* Write the namelist in its entirety. */
  2229. if (c == '=')
  2230. namelist_write (dtp);
  2231. /* Or write the list of names. */
  2232. else
  2233. {
  2234. /* "&namelist_name\n" */
  2235. len = dtp->namelist_name_len;
  2236. p = write_block (dtp, len - 1 + endlen);
  2237. if (!p)
  2238. goto query_return;
  2239. memcpy (p, "&", 1);
  2240. memcpy ((char*)(p + 1), dtp->namelist_name, len);
  2241. memcpy ((char*)(p + len + 1), &endl, endlen);
  2242. for (nl = dtp->u.p.ionml; nl; nl = nl->next)
  2243. {
  2244. /* " var_name\n" */
  2245. len = strlen (nl->var_name);
  2246. p = write_block (dtp, len + endlen);
  2247. if (!p)
  2248. goto query_return;
  2249. memcpy (p, " ", 1);
  2250. memcpy ((char*)(p + 1), nl->var_name, len);
  2251. memcpy ((char*)(p + len + 1), &endl, endlen);
  2252. }
  2253. /* "&end\n" */
  2254. p = write_block (dtp, endlen + 4);
  2255. if (!p)
  2256. goto query_return;
  2257. memcpy (p, &nmlend, endlen + 4);
  2258. }
  2259. /* Flush the stream to force immediate output. */
  2260. fbuf_flush (dtp->u.p.current_unit, WRITING);
  2261. sflush (dtp->u.p.current_unit->s);
  2262. unlock_unit (dtp->u.p.current_unit);
  2263. }
  2264. query_return:
  2265. /* Restore the current unit. */
  2266. dtp->u.p.current_unit = temp_unit;
  2267. dtp->u.p.mode = READING;
  2268. return;
  2269. }
  2270. /* Reads and stores the input for the namelist object nl. For an array,
  2271. the function loops over the ranges defined by the loop specification.
  2272. This default to all the data or to the specification from a qualifier.
  2273. nml_read_obj recursively calls itself to read derived types. It visits
  2274. all its own components but only reads data for those that were touched
  2275. when the name was parsed. If a read error is encountered, an attempt is
  2276. made to return to read a new object name because the standard allows too
  2277. little data to be available. On the other hand, too much data is an
  2278. error. */
  2279. static bool
  2280. nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
  2281. namelist_info **pprev_nl, char *nml_err_msg,
  2282. size_t nml_err_msg_size, index_type clow, index_type chigh)
  2283. {
  2284. namelist_info * cmp;
  2285. char * obj_name;
  2286. int nml_carry;
  2287. int len;
  2288. int dim;
  2289. index_type dlen;
  2290. index_type m;
  2291. size_t obj_name_len;
  2292. void * pdata;
  2293. /* If we have encountered a previous read error or this object has not been
  2294. touched in name parsing, just return. */
  2295. if (dtp->u.p.nml_read_error || !nl->touched)
  2296. return true;
  2297. dtp->u.p.repeat_count = 0;
  2298. eat_spaces (dtp);
  2299. len = nl->len;
  2300. switch (nl->type)
  2301. {
  2302. case BT_INTEGER:
  2303. case BT_LOGICAL:
  2304. dlen = len;
  2305. break;
  2306. case BT_REAL:
  2307. dlen = size_from_real_kind (len);
  2308. break;
  2309. case BT_COMPLEX:
  2310. dlen = size_from_complex_kind (len);
  2311. break;
  2312. case BT_CHARACTER:
  2313. dlen = chigh ? (chigh - clow + 1) : nl->string_length;
  2314. break;
  2315. default:
  2316. dlen = 0;
  2317. }
  2318. do
  2319. {
  2320. /* Update the pointer to the data, using the current index vector */
  2321. pdata = (void*)(nl->mem_pos + offset);
  2322. for (dim = 0; dim < nl->var_rank; dim++)
  2323. pdata = (void*)(pdata + (nl->ls[dim].idx
  2324. - GFC_DESCRIPTOR_LBOUND(nl,dim))
  2325. * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
  2326. /* If we are finished with the repeat count, try to read next value. */
  2327. nml_carry = 0;
  2328. if (--dtp->u.p.repeat_count <= 0)
  2329. {
  2330. if (dtp->u.p.input_complete)
  2331. return true;
  2332. if (dtp->u.p.at_eol)
  2333. finish_separator (dtp);
  2334. if (dtp->u.p.input_complete)
  2335. return true;
  2336. dtp->u.p.saved_type = BT_UNKNOWN;
  2337. free_saved (dtp);
  2338. switch (nl->type)
  2339. {
  2340. case BT_INTEGER:
  2341. read_integer (dtp, len);
  2342. break;
  2343. case BT_LOGICAL:
  2344. read_logical (dtp, len);
  2345. break;
  2346. case BT_CHARACTER:
  2347. read_character (dtp, len);
  2348. break;
  2349. case BT_REAL:
  2350. /* Need to copy data back from the real location to the temp in
  2351. order to handle nml reads into arrays. */
  2352. read_real (dtp, pdata, len);
  2353. memcpy (dtp->u.p.value, pdata, dlen);
  2354. break;
  2355. case BT_COMPLEX:
  2356. /* Same as for REAL, copy back to temp. */
  2357. read_complex (dtp, pdata, len, dlen);
  2358. memcpy (dtp->u.p.value, pdata, dlen);
  2359. break;
  2360. case BT_DERIVED:
  2361. obj_name_len = strlen (nl->var_name) + 1;
  2362. obj_name = xmalloc (obj_name_len+1);
  2363. memcpy (obj_name, nl->var_name, obj_name_len-1);
  2364. memcpy (obj_name + obj_name_len - 1, "%", 2);
  2365. /* If reading a derived type, disable the expanded read warning
  2366. since a single object can have multiple reads. */
  2367. dtp->u.p.expanded_read = 0;
  2368. /* Now loop over the components. */
  2369. for (cmp = nl->next;
  2370. cmp &&
  2371. !strncmp (cmp->var_name, obj_name, obj_name_len);
  2372. cmp = cmp->next)
  2373. {
  2374. /* Jump over nested derived type by testing if the potential
  2375. component name contains '%'. */
  2376. if (strchr (cmp->var_name + obj_name_len, '%'))
  2377. continue;
  2378. if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
  2379. pprev_nl, nml_err_msg, nml_err_msg_size,
  2380. clow, chigh))
  2381. {
  2382. free (obj_name);
  2383. return false;
  2384. }
  2385. if (dtp->u.p.input_complete)
  2386. {
  2387. free (obj_name);
  2388. return true;
  2389. }
  2390. }
  2391. free (obj_name);
  2392. goto incr_idx;
  2393. default:
  2394. snprintf (nml_err_msg, nml_err_msg_size,
  2395. "Bad type for namelist object %s", nl->var_name);
  2396. internal_error (&dtp->common, nml_err_msg);
  2397. goto nml_err_ret;
  2398. }
  2399. }
  2400. /* The standard permits array data to stop short of the number of
  2401. elements specified in the loop specification. In this case, we
  2402. should be here with dtp->u.p.nml_read_error != 0. Control returns to
  2403. nml_get_obj_data and an attempt is made to read object name. */
  2404. *pprev_nl = nl;
  2405. if (dtp->u.p.nml_read_error)
  2406. {
  2407. dtp->u.p.expanded_read = 0;
  2408. return true;
  2409. }
  2410. if (dtp->u.p.saved_type == BT_UNKNOWN)
  2411. {
  2412. dtp->u.p.expanded_read = 0;
  2413. goto incr_idx;
  2414. }
  2415. switch (dtp->u.p.saved_type)
  2416. {
  2417. case BT_COMPLEX:
  2418. case BT_REAL:
  2419. case BT_INTEGER:
  2420. case BT_LOGICAL:
  2421. memcpy (pdata, dtp->u.p.value, dlen);
  2422. break;
  2423. case BT_CHARACTER:
  2424. if (dlen < dtp->u.p.saved_used)
  2425. {
  2426. if (compile_options.bounds_check)
  2427. {
  2428. snprintf (nml_err_msg, nml_err_msg_size,
  2429. "Namelist object '%s' truncated on read.",
  2430. nl->var_name);
  2431. generate_warning (&dtp->common, nml_err_msg);
  2432. }
  2433. m = dlen;
  2434. }
  2435. else
  2436. m = dtp->u.p.saved_used;
  2437. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  2438. {
  2439. gfc_char4_t *q4, *p4 = pdata;
  2440. int i;
  2441. q4 = (gfc_char4_t *) dtp->u.p.saved_string;
  2442. p4 += clow -1;
  2443. for (i = 0; i < m; i++)
  2444. *p4++ = *q4++;
  2445. if (m < dlen)
  2446. for (i = 0; i < dlen - m; i++)
  2447. *p4++ = (gfc_char4_t) ' ';
  2448. }
  2449. else
  2450. {
  2451. pdata = (void*)( pdata + clow - 1 );
  2452. memcpy (pdata, dtp->u.p.saved_string, m);
  2453. if (m < dlen)
  2454. memset ((void*)( pdata + m ), ' ', dlen - m);
  2455. }
  2456. break;
  2457. default:
  2458. break;
  2459. }
  2460. /* Warn if a non-standard expanded read occurs. A single read of a
  2461. single object is acceptable. If a second read occurs, issue a warning
  2462. and set the flag to zero to prevent further warnings. */
  2463. if (dtp->u.p.expanded_read == 2)
  2464. {
  2465. notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
  2466. dtp->u.p.expanded_read = 0;
  2467. }
  2468. /* If the expanded read warning flag is set, increment it,
  2469. indicating that a single read has occurred. */
  2470. if (dtp->u.p.expanded_read >= 1)
  2471. dtp->u.p.expanded_read++;
  2472. /* Break out of loop if scalar. */
  2473. if (!nl->var_rank)
  2474. break;
  2475. /* Now increment the index vector. */
  2476. incr_idx:
  2477. nml_carry = 1;
  2478. for (dim = 0; dim < nl->var_rank; dim++)
  2479. {
  2480. nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
  2481. nml_carry = 0;
  2482. if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
  2483. ||
  2484. ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
  2485. {
  2486. nl->ls[dim].idx = nl->ls[dim].start;
  2487. nml_carry = 1;
  2488. }
  2489. }
  2490. } while (!nml_carry);
  2491. if (dtp->u.p.repeat_count > 1)
  2492. {
  2493. snprintf (nml_err_msg, nml_err_msg_size,
  2494. "Repeat count too large for namelist object %s", nl->var_name);
  2495. goto nml_err_ret;
  2496. }
  2497. return true;
  2498. nml_err_ret:
  2499. return false;
  2500. }
  2501. /* Parses the object name, including array and substring qualifiers. It
  2502. iterates over derived type components, touching those components and
  2503. setting their loop specifications, if there is a qualifier. If the
  2504. object is itself a derived type, its components and subcomponents are
  2505. touched. nml_read_obj is called at the end and this reads the data in
  2506. the manner specified by the object name. */
  2507. static bool
  2508. nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
  2509. char *nml_err_msg, size_t nml_err_msg_size)
  2510. {
  2511. int c;
  2512. namelist_info * nl;
  2513. namelist_info * first_nl = NULL;
  2514. namelist_info * root_nl = NULL;
  2515. int dim, parsed_rank;
  2516. int component_flag, qualifier_flag;
  2517. index_type clow, chigh;
  2518. int non_zero_rank_count;
  2519. /* Look for end of input or object name. If '?' or '=?' are encountered
  2520. in stdin, print the node names or the namelist to stdout. */
  2521. eat_separator (dtp);
  2522. if (dtp->u.p.input_complete)
  2523. return true;
  2524. if (dtp->u.p.at_eol)
  2525. finish_separator (dtp);
  2526. if (dtp->u.p.input_complete)
  2527. return true;
  2528. if ((c = next_char (dtp)) == EOF)
  2529. goto nml_err_ret;
  2530. switch (c)
  2531. {
  2532. case '=':
  2533. if ((c = next_char (dtp)) == EOF)
  2534. goto nml_err_ret;
  2535. if (c != '?')
  2536. {
  2537. snprintf (nml_err_msg, nml_err_msg_size,
  2538. "namelist read: misplaced = sign");
  2539. goto nml_err_ret;
  2540. }
  2541. nml_query (dtp, '=');
  2542. return true;
  2543. case '?':
  2544. nml_query (dtp, '?');
  2545. return true;
  2546. case '$':
  2547. case '&':
  2548. nml_match_name (dtp, "end", 3);
  2549. if (dtp->u.p.nml_read_error)
  2550. {
  2551. snprintf (nml_err_msg, nml_err_msg_size,
  2552. "namelist not terminated with / or &end");
  2553. goto nml_err_ret;
  2554. }
  2555. /* Fall through. */
  2556. case '/':
  2557. dtp->u.p.input_complete = 1;
  2558. return true;
  2559. default :
  2560. break;
  2561. }
  2562. /* Untouch all nodes of the namelist and reset the flags that are set for
  2563. derived type components. */
  2564. nml_untouch_nodes (dtp);
  2565. component_flag = 0;
  2566. qualifier_flag = 0;
  2567. non_zero_rank_count = 0;
  2568. /* Get the object name - should '!' and '\n' be permitted separators? */
  2569. get_name:
  2570. free_saved (dtp);
  2571. do
  2572. {
  2573. if (!is_separator (c))
  2574. push_char_default (dtp, tolower(c));
  2575. if ((c = next_char (dtp)) == EOF)
  2576. goto nml_err_ret;
  2577. }
  2578. while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
  2579. unget_char (dtp, c);
  2580. /* Check that the name is in the namelist and get pointer to object.
  2581. Three error conditions exist: (i) An attempt is being made to
  2582. identify a non-existent object, following a failed data read or
  2583. (ii) The object name does not exist or (iii) Too many data items
  2584. are present for an object. (iii) gives the same error message
  2585. as (i) */
  2586. push_char_default (dtp, '\0');
  2587. if (component_flag)
  2588. {
  2589. #define EXT_STACK_SZ 100
  2590. char ext_stack[EXT_STACK_SZ];
  2591. char *ext_name;
  2592. size_t var_len = strlen (root_nl->var_name);
  2593. size_t saved_len
  2594. = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
  2595. size_t ext_size = var_len + saved_len + 1;
  2596. if (ext_size > EXT_STACK_SZ)
  2597. ext_name = xmalloc (ext_size);
  2598. else
  2599. ext_name = ext_stack;
  2600. memcpy (ext_name, root_nl->var_name, var_len);
  2601. if (dtp->u.p.saved_string)
  2602. memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
  2603. ext_name[var_len + saved_len] = '\0';
  2604. nl = find_nml_node (dtp, ext_name);
  2605. if (ext_size > EXT_STACK_SZ)
  2606. free (ext_name);
  2607. }
  2608. else
  2609. nl = find_nml_node (dtp, dtp->u.p.saved_string);
  2610. if (nl == NULL)
  2611. {
  2612. if (dtp->u.p.nml_read_error && *pprev_nl)
  2613. snprintf (nml_err_msg, nml_err_msg_size,
  2614. "Bad data for namelist object %s", (*pprev_nl)->var_name);
  2615. else
  2616. snprintf (nml_err_msg, nml_err_msg_size,
  2617. "Cannot match namelist object name %s",
  2618. dtp->u.p.saved_string);
  2619. goto nml_err_ret;
  2620. }
  2621. /* Get the length, data length, base pointer and rank of the variable.
  2622. Set the default loop specification first. */
  2623. for (dim=0; dim < nl->var_rank; dim++)
  2624. {
  2625. nl->ls[dim].step = 1;
  2626. nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
  2627. nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
  2628. nl->ls[dim].idx = nl->ls[dim].start;
  2629. }
  2630. /* Check to see if there is a qualifier: if so, parse it.*/
  2631. if (c == '(' && nl->var_rank)
  2632. {
  2633. parsed_rank = 0;
  2634. if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
  2635. nl->type, nml_err_msg, nml_err_msg_size,
  2636. &parsed_rank))
  2637. {
  2638. char *nml_err_msg_end = strchr (nml_err_msg, '\0');
  2639. snprintf (nml_err_msg_end,
  2640. nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
  2641. " for namelist variable %s", nl->var_name);
  2642. goto nml_err_ret;
  2643. }
  2644. if (parsed_rank > 0)
  2645. non_zero_rank_count++;
  2646. qualifier_flag = 1;
  2647. if ((c = next_char (dtp)) == EOF)
  2648. goto nml_err_ret;
  2649. unget_char (dtp, c);
  2650. }
  2651. else if (nl->var_rank > 0)
  2652. non_zero_rank_count++;
  2653. /* Now parse a derived type component. The root namelist_info address
  2654. is backed up, as is the previous component level. The component flag
  2655. is set and the iteration is made by jumping back to get_name. */
  2656. if (c == '%')
  2657. {
  2658. if (nl->type != BT_DERIVED)
  2659. {
  2660. snprintf (nml_err_msg, nml_err_msg_size,
  2661. "Attempt to get derived component for %s", nl->var_name);
  2662. goto nml_err_ret;
  2663. }
  2664. /* Don't move first_nl further in the list if a qualifier was found. */
  2665. if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
  2666. first_nl = nl;
  2667. root_nl = nl;
  2668. component_flag = 1;
  2669. if ((c = next_char (dtp)) == EOF)
  2670. goto nml_err_ret;
  2671. goto get_name;
  2672. }
  2673. /* Parse a character qualifier, if present. chigh = 0 is a default
  2674. that signals that the string length = string_length. */
  2675. clow = 1;
  2676. chigh = 0;
  2677. if (c == '(' && nl->type == BT_CHARACTER)
  2678. {
  2679. descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
  2680. array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
  2681. if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
  2682. nml_err_msg, nml_err_msg_size, &parsed_rank))
  2683. {
  2684. char *nml_err_msg_end = strchr (nml_err_msg, '\0');
  2685. snprintf (nml_err_msg_end,
  2686. nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
  2687. " for namelist variable %s", nl->var_name);
  2688. goto nml_err_ret;
  2689. }
  2690. clow = ind[0].start;
  2691. chigh = ind[0].end;
  2692. if (ind[0].step != 1)
  2693. {
  2694. snprintf (nml_err_msg, nml_err_msg_size,
  2695. "Step not allowed in substring qualifier"
  2696. " for namelist object %s", nl->var_name);
  2697. goto nml_err_ret;
  2698. }
  2699. if ((c = next_char (dtp)) == EOF)
  2700. goto nml_err_ret;
  2701. unget_char (dtp, c);
  2702. }
  2703. /* Make sure no extraneous qualifiers are there. */
  2704. if (c == '(')
  2705. {
  2706. snprintf (nml_err_msg, nml_err_msg_size,
  2707. "Qualifier for a scalar or non-character namelist object %s",
  2708. nl->var_name);
  2709. goto nml_err_ret;
  2710. }
  2711. /* Make sure there is no more than one non-zero rank object. */
  2712. if (non_zero_rank_count > 1)
  2713. {
  2714. snprintf (nml_err_msg, nml_err_msg_size,
  2715. "Multiple sub-objects with non-zero rank in namelist object %s",
  2716. nl->var_name);
  2717. non_zero_rank_count = 0;
  2718. goto nml_err_ret;
  2719. }
  2720. /* According to the standard, an equal sign MUST follow an object name. The
  2721. following is possibly lax - it allows comments, blank lines and so on to
  2722. intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
  2723. free_saved (dtp);
  2724. eat_separator (dtp);
  2725. if (dtp->u.p.input_complete)
  2726. return true;
  2727. if (dtp->u.p.at_eol)
  2728. finish_separator (dtp);
  2729. if (dtp->u.p.input_complete)
  2730. return true;
  2731. if ((c = next_char (dtp)) == EOF)
  2732. goto nml_err_ret;
  2733. if (c != '=')
  2734. {
  2735. snprintf (nml_err_msg, nml_err_msg_size,
  2736. "Equal sign must follow namelist object name %s",
  2737. nl->var_name);
  2738. goto nml_err_ret;
  2739. }
  2740. /* If a derived type, touch its components and restore the root
  2741. namelist_info if we have parsed a qualified derived type
  2742. component. */
  2743. if (nl->type == BT_DERIVED)
  2744. nml_touch_nodes (nl);
  2745. if (first_nl)
  2746. {
  2747. if (first_nl->var_rank == 0)
  2748. {
  2749. if (component_flag && qualifier_flag)
  2750. nl = first_nl;
  2751. }
  2752. else
  2753. nl = first_nl;
  2754. }
  2755. dtp->u.p.nml_read_error = 0;
  2756. if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
  2757. clow, chigh))
  2758. goto nml_err_ret;
  2759. return true;
  2760. nml_err_ret:
  2761. /* The EOF error message is issued by hit_eof. Return true so that the
  2762. caller does not use nml_err_msg and nml_err_msg_size to generate
  2763. an unrelated error message. */
  2764. if (c == EOF)
  2765. {
  2766. dtp->u.p.input_complete = 1;
  2767. unget_char (dtp, c);
  2768. hit_eof (dtp);
  2769. return true;
  2770. }
  2771. return false;
  2772. }
  2773. /* Entry point for namelist input. Goes through input until namelist name
  2774. is matched. Then cycles through nml_get_obj_data until the input is
  2775. completed or there is an error. */
  2776. void
  2777. namelist_read (st_parameter_dt *dtp)
  2778. {
  2779. int c;
  2780. char nml_err_msg[200];
  2781. /* Initialize the error string buffer just in case we get an unexpected fail
  2782. somewhere and end up at nml_err_ret. */
  2783. strcpy (nml_err_msg, "Internal namelist read error");
  2784. /* Pointer to the previously read object, in case attempt is made to read
  2785. new object name. Should this fail, error message can give previous
  2786. name. */
  2787. namelist_info *prev_nl = NULL;
  2788. dtp->u.p.namelist_mode = 1;
  2789. dtp->u.p.input_complete = 0;
  2790. dtp->u.p.expanded_read = 0;
  2791. /* Set the next_char and push_char worker functions. */
  2792. set_workers (dtp);
  2793. /* Look for &namelist_name . Skip all characters, testing for $nmlname.
  2794. Exit on success or EOF. If '?' or '=?' encountered in stdin, print
  2795. node names or namelist on stdout. */
  2796. find_nml_name:
  2797. c = next_char (dtp);
  2798. switch (c)
  2799. {
  2800. case '$':
  2801. case '&':
  2802. break;
  2803. case '!':
  2804. eat_line (dtp);
  2805. goto find_nml_name;
  2806. case '=':
  2807. c = next_char (dtp);
  2808. if (c == '?')
  2809. nml_query (dtp, '=');
  2810. else
  2811. unget_char (dtp, c);
  2812. goto find_nml_name;
  2813. case '?':
  2814. nml_query (dtp, '?');
  2815. goto find_nml_name;
  2816. case EOF:
  2817. return;
  2818. default:
  2819. goto find_nml_name;
  2820. }
  2821. /* Match the name of the namelist. */
  2822. nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
  2823. if (dtp->u.p.nml_read_error)
  2824. goto find_nml_name;
  2825. /* A trailing space is required, we give a little latitude here, 10.9.1. */
  2826. c = next_char (dtp);
  2827. if (!is_separator(c) && c != '!')
  2828. {
  2829. unget_char (dtp, c);
  2830. goto find_nml_name;
  2831. }
  2832. unget_char (dtp, c);
  2833. eat_separator (dtp);
  2834. /* Ready to read namelist objects. If there is an error in input
  2835. from stdin, output the error message and continue. */
  2836. while (!dtp->u.p.input_complete)
  2837. {
  2838. if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
  2839. {
  2840. if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
  2841. goto nml_err_ret;
  2842. generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
  2843. }
  2844. /* Reset the previous namelist pointer if we know we are not going
  2845. to be doing multiple reads within a single namelist object. */
  2846. if (prev_nl && prev_nl->var_rank == 0)
  2847. prev_nl = NULL;
  2848. }
  2849. free_saved (dtp);
  2850. free_line (dtp);
  2851. return;
  2852. nml_err_ret:
  2853. /* All namelist error calls return from here */
  2854. free_saved (dtp);
  2855. free_line (dtp);
  2856. generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
  2857. return;
  2858. }