OSDN Git Service

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