OSDN Git Service

PR fortran/25324
[pf3gnuchains/gcc-fork.git] / gcc / fortran / scanner.c
1 /* Character scanner.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* Set of subroutines to (ultimately) return the next character to the
24    various matching subroutines.  This file's job is to read files and
25    build up lines that are parsed by the parser.  This means that we
26    handle continuation lines and "include" lines.
27
28    The first thing the scanner does is to load an entire file into
29    memory.  We load the entire file into memory for a couple reasons.
30    The first is that we want to be able to deal with nonseekable input
31    (pipes, stdin) and there is a lot of backing up involved during
32    parsing.
33
34    The second is that we want to be able to print the locus of errors,
35    and an error on line 999999 could conflict with something on line
36    one.  Given nonseekable input, we've got to store the whole thing.
37
38    One thing that helps are the column truncation limits that give us
39    an upper bound on the size of individual lines.  We don't store the
40    truncated stuff.
41
42    From the scanner's viewpoint, the higher level subroutines ask for
43    new characters and do a lot of jumping backwards.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "gfortran.h"
48 #include "toplev.h"
49
50 /* Structure for holding module and include file search path.  */
51 typedef struct gfc_directorylist
52 {
53   char *path;
54   struct gfc_directorylist *next;
55 }
56 gfc_directorylist;
57
58 /* List of include file search directories.  */
59 static gfc_directorylist *include_dirs;
60
61 static gfc_file *file_head, *current_file;
62
63 static int continue_flag, end_flag;
64
65 gfc_source_form gfc_current_form;
66 static gfc_linebuf *line_head, *line_tail;
67        
68 locus gfc_current_locus;
69 const char *gfc_source_file;
70 static FILE *gfc_src_file;
71 static char *gfc_src_preprocessor_lines[2];
72
73
74 /* Main scanner initialization.  */
75
76 void
77 gfc_scanner_init_1 (void)
78 {
79   file_head = NULL;
80   line_head = NULL;
81   line_tail = NULL;
82
83   end_flag = 0;
84 }
85
86
87 /* Main scanner destructor.  */
88
89 void
90 gfc_scanner_done_1 (void)
91 {
92   gfc_linebuf *lb;
93   gfc_file *f;
94
95   while(line_head != NULL) 
96     {
97       lb = line_head->next;
98       gfc_free(line_head);
99       line_head = lb;
100     }
101      
102   while(file_head != NULL) 
103     {
104       f = file_head->next;
105       gfc_free(file_head->filename);
106       gfc_free(file_head);
107       file_head = f;    
108     }
109
110 }
111
112
113 /* Adds path to the list pointed to by list.  */
114
115 void
116 gfc_add_include_path (const char *path)
117 {
118   gfc_directorylist *dir;
119   const char *p;
120
121   p = path;
122   while (*p == ' ' || *p == '\t')  /* someone might do 'gfortran "-I include"' */
123     if (*p++ == '\0')
124       return;
125
126   dir = include_dirs;
127   if (!dir)
128     {
129       dir = include_dirs = gfc_getmem (sizeof (gfc_directorylist));
130     }
131   else
132     {
133       while (dir->next)
134         dir = dir->next;
135
136       dir->next = gfc_getmem (sizeof (gfc_directorylist));
137       dir = dir->next;
138     }
139
140   dir->next = NULL;
141   dir->path = gfc_getmem (strlen (p) + 2);
142   strcpy (dir->path, p);
143   strcat (dir->path, "/");      /* make '/' last character */
144 }
145
146
147 /* Release resources allocated for options.  */
148
149 void
150 gfc_release_include_path (void)
151 {
152   gfc_directorylist *p;
153
154   gfc_free (gfc_option.module_dir);
155   while (include_dirs != NULL)
156     {
157       p = include_dirs;
158       include_dirs = include_dirs->next;
159       gfc_free (p->path);
160       gfc_free (p);
161     }
162 }
163
164 /* Opens file for reading, searching through the include directories
165    given if necessary.  If the include_cwd argument is true, we try
166    to open the file in the current directory first.  */
167
168 FILE *
169 gfc_open_included_file (const char *name, const bool include_cwd)
170 {
171   char *fullname;
172   gfc_directorylist *p;
173   FILE *f;
174
175   if (include_cwd)
176     {
177       f = gfc_open_file (name);
178       if (f != NULL)
179         return f;
180     }
181
182   for (p = include_dirs; p; p = p->next)
183     {
184       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
185       strcpy (fullname, p->path);
186       strcat (fullname, name);
187
188       f = gfc_open_file (fullname);
189       if (f != NULL)
190         return f;
191     }
192
193   return NULL;
194 }
195
196 /* Test to see if we're at the end of the main source file.  */
197
198 int
199 gfc_at_end (void)
200 {
201
202   return end_flag;
203 }
204
205
206 /* Test to see if we're at the end of the current file.  */
207
208 int
209 gfc_at_eof (void)
210 {
211
212   if (gfc_at_end ())
213     return 1;
214
215   if (line_head == NULL)
216     return 1;                   /* Null file */
217
218   if (gfc_current_locus.lb == NULL)
219     return 1;
220
221   return 0;
222 }
223
224
225 /* Test to see if we're at the beginning of a new line.  */
226
227 int
228 gfc_at_bol (void)
229 {
230   if (gfc_at_eof ())
231     return 1;
232
233   return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
234 }
235
236
237 /* Test to see if we're at the end of a line.  */
238
239 int
240 gfc_at_eol (void)
241 {
242
243   if (gfc_at_eof ())
244     return 1;
245
246   return (*gfc_current_locus.nextc == '\0');
247 }
248
249
250 /* Advance the current line pointer to the next line.  */
251
252 void
253 gfc_advance_line (void)
254 {
255   if (gfc_at_end ())
256     return;
257
258   if (gfc_current_locus.lb == NULL) 
259     {
260       end_flag = 1;
261       return;
262     } 
263
264   gfc_current_locus.lb = gfc_current_locus.lb->next;
265
266   if (gfc_current_locus.lb != NULL)         
267     gfc_current_locus.nextc = gfc_current_locus.lb->line;
268   else 
269     {
270       gfc_current_locus.nextc = NULL;
271       end_flag = 1;
272     }       
273 }
274
275
276 /* Get the next character from the input, advancing gfc_current_file's
277    locus.  When we hit the end of the line or the end of the file, we
278    start returning a '\n' in order to complete the current statement.
279    No Fortran line conventions are implemented here.
280
281    Requiring explicit advances to the next line prevents the parse
282    pointer from being on the wrong line if the current statement ends
283    prematurely.  */
284
285 static int
286 next_char (void)
287 {
288   int c;
289   
290   if (gfc_current_locus.nextc == NULL)
291     return '\n';
292
293   c = *gfc_current_locus.nextc++;
294   if (c == '\0')
295     {
296       gfc_current_locus.nextc--; /* Remain on this line.  */
297       c = '\n';
298     }
299
300   return c;
301 }
302
303 /* Skip a comment.  When we come here the parse pointer is positioned
304    immediately after the comment character.  If we ever implement
305    compiler directives withing comments, here is where we parse the
306    directive.  */
307
308 static void
309 skip_comment_line (void)
310 {
311   char c;
312
313   do
314     {
315       c = next_char ();
316     }
317   while (c != '\n');
318
319   gfc_advance_line ();
320 }
321
322
323 /* Comment lines are null lines, lines containing only blanks or lines
324    on which the first nonblank line is a '!'.  */
325
326 static void
327 skip_free_comments (void)
328 {
329   locus start;
330   char c;
331
332   for (;;)
333     {
334       start = gfc_current_locus;
335       if (gfc_at_eof ())
336         break;
337
338       do
339         {
340           c = next_char ();
341         }
342       while (gfc_is_whitespace (c));
343
344       if (c == '\n')
345         {
346           gfc_advance_line ();
347           continue;
348         }
349
350       if (c == '!')
351         {
352           skip_comment_line ();
353           continue;
354         }
355
356       break;
357     }
358
359   gfc_current_locus = start;
360 }
361
362
363 /* Skip comment lines in fixed source mode.  We have the same rules as
364    in skip_free_comment(), except that we can have a 'c', 'C' or '*'
365    in column 1, and a '!' cannot be in column 6.  Also, we deal with
366    lines with 'd' or 'D' in column 1, if the user requested this.  */
367
368 static void
369 skip_fixed_comments (void)
370 {
371   locus start;
372   int col;
373   char c;
374
375   for (;;)
376     {
377       start = gfc_current_locus;
378       if (gfc_at_eof ())
379         break;
380
381       c = next_char ();
382       if (c == '\n')
383         {
384           gfc_advance_line ();
385           continue;
386         }
387
388       if (c == '!' || c == 'c' || c == 'C' || c == '*')
389         {
390           skip_comment_line ();
391           continue;
392         }
393
394       if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
395         {
396           if (gfc_option.flag_d_lines == 0)
397             {
398               skip_comment_line ();
399               continue;
400             }
401           else
402             *start.nextc = c = ' ';
403         }
404
405       col = 1;
406
407       while (gfc_is_whitespace (c))
408         {
409           c = next_char ();
410           col++;
411         }
412
413       if (c == '\n')
414         {
415           gfc_advance_line ();
416           continue;
417         }
418
419       if (col != 6 && c == '!')
420         {
421           skip_comment_line ();
422           continue;
423         }
424
425       break;
426     }
427
428   gfc_current_locus = start;
429 }
430
431
432 /* Skips the current line if it is a comment.  Assumes that we are at
433    the start of the current line.  */
434
435 void
436 gfc_skip_comments (void)
437 {
438
439   if (!gfc_at_bol () || gfc_current_form == FORM_FREE)
440     skip_free_comments ();
441   else
442     skip_fixed_comments ();
443 }
444
445
446 /* Get the next character from the input, taking continuation lines
447    and end-of-line comments into account.  This implies that comment
448    lines between continued lines must be eaten here.  For higher-level
449    subroutines, this flattens continued lines into a single logical
450    line.  The in_string flag denotes whether we're inside a character
451    context or not.  */
452
453 int
454 gfc_next_char_literal (int in_string)
455 {
456   locus old_loc;
457   int i, c;
458
459   continue_flag = 0;
460
461 restart:
462   c = next_char ();
463   if (gfc_at_end ())
464     return c;
465
466   if (gfc_current_form == FORM_FREE)
467     {
468
469       if (!in_string && c == '!')
470         {
471           /* This line can't be continued */
472           do
473             {
474               c = next_char ();
475             }
476           while (c != '\n');
477
478           /* Avoid truncation warnings for comment ending lines.  */
479           gfc_current_locus.lb->truncated = 0;
480
481           goto done;
482         }
483
484       if (c != '&')
485         goto done;
486
487       /* If the next nonblank character is a ! or \n, we've got a
488          continuation line.  */
489       old_loc = gfc_current_locus;
490
491       c = next_char ();
492       while (gfc_is_whitespace (c))
493         c = next_char ();
494
495       /* Character constants to be continued cannot have commentary
496          after the '&'.  */
497
498       if (in_string && c != '\n')
499         {
500           gfc_current_locus = old_loc;
501           c = '&';
502           goto done;
503         }
504
505       if (c != '!' && c != '\n')
506         {
507           gfc_current_locus = old_loc;
508           c = '&';
509           goto done;
510         }
511
512       continue_flag = 1;
513       if (c == '!')
514         skip_comment_line ();
515       else
516         gfc_advance_line ();
517
518       /* We've got a continuation line and need to find where it continues.
519          First eat any comment lines.  */
520       gfc_skip_comments ();
521
522       /* Now that we have a non-comment line, probe ahead for the
523          first non-whitespace character.  If it is another '&', then
524          reading starts at the next character, otherwise we must back
525          up to where the whitespace started and resume from there.  */
526
527       old_loc = gfc_current_locus;
528
529       c = next_char ();
530       while (gfc_is_whitespace (c))
531         c = next_char ();
532
533       if (c != '&')
534         gfc_current_locus = old_loc;
535
536     }
537   else
538     {
539       /* Fixed form continuation.  */
540       if (!in_string && c == '!')
541         {
542           /* Skip comment at end of line.  */
543           do
544             {
545               c = next_char ();
546             }
547           while (c != '\n');
548
549           /* Avoid truncation warnings for comment ending lines.  */
550           gfc_current_locus.lb->truncated = 0;
551         }
552
553       if (c != '\n')
554         goto done;
555
556       continue_flag = 1;
557       old_loc = gfc_current_locus;
558
559       gfc_advance_line ();
560       gfc_skip_comments ();
561
562       /* See if this line is a continuation line.  */
563       for (i = 0; i < 5; i++)
564         {
565           c = next_char ();
566           if (c != ' ')
567             goto not_continuation;
568         }
569
570       c = next_char ();
571       if (c == '0' || c == ' ')
572         goto not_continuation;
573     }
574
575   /* Ready to read first character of continuation line, which might
576      be another continuation line!  */
577   goto restart;
578
579 not_continuation:
580   c = '\n';
581   gfc_current_locus = old_loc;
582
583 done:
584   continue_flag = 0;
585   return c;
586 }
587
588
589 /* Get the next character of input, folded to lowercase.  In fixed
590    form mode, we also ignore spaces.  When matcher subroutines are
591    parsing character literals, they have to call
592    gfc_next_char_literal().  */
593
594 int
595 gfc_next_char (void)
596 {
597   int c;
598
599   do
600     {
601       c = gfc_next_char_literal (0);
602     }
603   while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
604
605   return TOLOWER (c);
606 }
607
608
609 int
610 gfc_peek_char (void)
611 {
612   locus old_loc;
613   int c;
614
615   old_loc = gfc_current_locus;
616   c = gfc_next_char ();
617   gfc_current_locus = old_loc;
618
619   return c;
620 }
621
622
623 /* Recover from an error.  We try to get past the current statement
624    and get lined up for the next.  The next statement follows a '\n'
625    or a ';'.  We also assume that we are not within a character
626    constant, and deal with finding a '\'' or '"'.  */
627
628 void
629 gfc_error_recovery (void)
630 {
631   char c, delim;
632
633   if (gfc_at_eof ())
634     return;
635
636   for (;;)
637     {
638       c = gfc_next_char ();
639       if (c == '\n' || c == ';')
640         break;
641
642       if (c != '\'' && c != '"')
643         {
644           if (gfc_at_eof ())
645             break;
646           continue;
647         }
648       delim = c;
649
650       for (;;)
651         {
652           c = next_char ();
653
654           if (c == delim)
655             break;
656           if (c == '\n')
657             return;
658           if (c == '\\')
659             {
660               c = next_char ();
661               if (c == '\n')
662                 return;
663             }
664         }
665       if (gfc_at_eof ())
666         break;
667     }
668 }
669
670
671 /* Read ahead until the next character to be read is not whitespace.  */
672
673 void
674 gfc_gobble_whitespace (void)
675 {
676   locus old_loc;
677   int c;
678
679   do
680     {
681       old_loc = gfc_current_locus;
682       c = gfc_next_char_literal (0);
683     }
684   while (gfc_is_whitespace (c));
685
686   gfc_current_locus = old_loc;
687 }
688
689
690 /* Load a single line into pbuf.
691
692    If pbuf points to a NULL pointer, it is allocated.
693    We truncate lines that are too long, unless we're dealing with
694    preprocessor lines or if the option -ffixed-line-length-none is set,
695    in which case we reallocate the buffer to fit the entire line, if
696    need be.
697    In fixed mode, we expand a tab that occurs within the statement
698    label region to expand to spaces that leave the next character in
699    the source region.
700    load_line returns whether the line was truncated.  */
701
702 static int
703 load_line (FILE * input, char **pbuf, int *pbuflen)
704 {
705   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
706   int trunc_flag = 0;
707   char *buffer;
708
709   /* Determine the maximum allowed line length.
710      The default for free-form is GFC_MAX_LINE, for fixed-form or for
711      unknown form it is 72. Refer to the documentation in gfc_option_t.  */
712   if (gfc_current_form == FORM_FREE)
713     {
714       if (gfc_option.free_line_length == -1)
715         maxlen = GFC_MAX_LINE;
716       else
717         maxlen = gfc_option.free_line_length;
718     }
719   else if (gfc_current_form == FORM_FIXED)
720     {
721       if (gfc_option.fixed_line_length == -1)
722         maxlen = 72;
723       else
724         maxlen = gfc_option.fixed_line_length;
725     }
726   else
727     maxlen = 72;
728
729   if (*pbuf == NULL)
730     {
731       /* Allocate the line buffer, storing its length into buflen.  */
732       if (maxlen > 0)
733         buflen = maxlen;
734       else
735         buflen = GFC_MAX_LINE;
736
737       *pbuf = gfc_getmem (buflen + 1);
738     }
739
740   i = 0;
741   buffer = *pbuf;
742
743   preprocessor_flag = 0;
744   c = fgetc (input);
745   if (c == '#')
746     /* In order to not truncate preprocessor lines, we have to
747        remember that this is one.  */
748     preprocessor_flag = 1;
749   ungetc (c, input);
750
751   for (;;)
752     {
753       c = fgetc (input);
754
755       if (c == EOF)
756         break;
757       if (c == '\n')
758         break;
759
760       if (c == '\r')
761         continue;               /* Gobble characters.  */
762       if (c == '\0')
763         continue;
764
765       if (c == '\032')
766         {
767           /* Ctrl-Z ends the file.  */
768           while (fgetc (input) != EOF);
769           break;
770         }
771
772       if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
773         {                       /* Tab expansion.  */
774           while (i <= 6)
775             {
776               *buffer++ = ' ';
777               i++;
778             }
779
780           continue;
781         }
782
783       *buffer++ = c;
784       i++;
785
786       if (maxlen == 0 || preprocessor_flag)
787         {
788           if (i >= buflen)
789             {
790               /* Reallocate line buffer to double size to hold the
791                  overlong line.  */
792               buflen = buflen * 2;
793               *pbuf = xrealloc (*pbuf, buflen + 1);
794               buffer = (*pbuf)+i;
795             }
796         }
797       else if (i >= maxlen)
798         {
799           /* Truncate the rest of the line.  */
800           for (;;)
801             {
802               c = fgetc (input);
803               if (c == '\n' || c == EOF)
804                 break;
805
806               trunc_flag = 1;
807             }
808
809           ungetc ('\n', input);
810         }
811     }
812
813   /* Pad lines to the selected line length in fixed form.  */
814   if (gfc_current_form == FORM_FIXED
815       && gfc_option.fixed_line_length != 0
816       && !preprocessor_flag
817       && c != EOF)
818     {
819       while (i++ < maxlen)
820         *buffer++ = ' ';
821     }
822
823   *buffer = '\0';
824   *pbuflen = buflen;
825
826   return trunc_flag;
827 }
828
829
830 /* Get a gfc_file structure, initialize it and add it to
831    the file stack.  */
832
833 static gfc_file *
834 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
835 {
836   gfc_file *f;
837
838   f = gfc_getmem (sizeof (gfc_file));
839
840   f->filename = gfc_getmem (strlen (name) + 1);
841   strcpy (f->filename, name);
842
843   f->next = file_head;
844   file_head = f;
845
846   f->included_by = current_file;
847   if (current_file != NULL)
848     f->inclusion_line = current_file->line;
849
850 #ifdef USE_MAPPED_LOCATION
851   linemap_add (&line_table, reason, false, f->filename, 1);
852 #endif
853
854   return f;
855 }
856
857 /* Deal with a line from the C preprocessor. The
858    initial octothorp has already been seen.  */
859
860 static void
861 preprocessor_line (char *c)
862 {
863   bool flag[5];
864   int i, line;
865   char *filename;
866   gfc_file *f;
867   int escaped, unescape;
868
869   c++;
870   while (*c == ' ' || *c == '\t')
871     c++;
872
873   if (*c < '0' || *c > '9')
874     goto bad_cpp_line;
875
876   line = atoi (c);
877
878   c = strchr (c, ' ');
879   if (c == NULL)
880     {
881       /* No file name given.  Set new line number.  */
882       current_file->line = line;
883       return;
884     }
885
886   /* Skip spaces.  */
887   while (*c == ' ' || *c == '\t')
888     c++;
889
890   /* Skip quote.  */
891   if (*c != '"')
892     goto bad_cpp_line;
893   ++c;
894
895   filename = c;
896
897   /* Make filename end at quote.  */
898   unescape = 0;
899   escaped = false;
900   while (*c && ! (! escaped && *c == '"'))
901     {
902       if (escaped)
903         escaped = false;
904       else if (*c == '\\')
905         {
906           escaped = true;
907           unescape++;
908         }
909       ++c;
910     }
911
912   if (! *c)
913     /* Preprocessor line has no closing quote.  */
914     goto bad_cpp_line;
915
916   *c++ = '\0';
917
918   /* Undo effects of cpp_quote_string.  */
919   if (unescape)
920     {
921       char *s = filename;
922       char *d = gfc_getmem (c - filename - unescape);
923
924       filename = d;
925       while (*s)
926         {
927           if (*s == '\\')
928             *d++ = *++s;
929           else
930             *d++ = *s;
931           s++;
932         }
933       *d = '\0';
934     }
935
936   /* Get flags.  */
937
938   flag[1] = flag[2] = flag[3] = flag[4] = false;
939
940   for (;;)
941     {
942       c = strchr (c, ' ');
943       if (c == NULL)
944         break;
945
946       c++;
947       i = atoi (c);
948
949       if (1 <= i && i <= 4)
950         flag[i] = true;
951     }
952
953   /* Interpret flags.  */
954
955   if (flag[1]) /* Starting new file.  */
956     {
957       f = get_file (filename, LC_RENAME);
958       f->up = current_file;
959       current_file = f;
960     }
961
962   if (flag[2]) /* Ending current file.  */
963     {
964       if (!current_file->up
965           || strcmp (current_file->up->filename, filename) != 0)
966         {
967           gfc_warning_now ("%s:%d: file %s left but not entered",
968                            current_file->filename, current_file->line,
969                            filename);
970           if (unescape)
971             gfc_free (filename);
972           return;
973         }
974       current_file = current_file->up;
975     }
976
977   /* The name of the file can be a temporary file produced by
978      cpp. Replace the name if it is different.  */
979
980   if (strcmp (current_file->filename, filename) != 0)
981     {
982       gfc_free (current_file->filename);
983       current_file->filename = gfc_getmem (strlen (filename) + 1);
984       strcpy (current_file->filename, filename);
985     }
986
987   /* Set new line number.  */
988   current_file->line = line;
989   if (unescape)
990     gfc_free (filename);
991   return;
992
993  bad_cpp_line:
994   gfc_warning_now ("%s:%d: Illegal preprocessor directive",
995                    current_file->filename, current_file->line);
996   current_file->line++;
997 }
998
999
1000 static try load_file (const char *, bool);
1001
1002 /* include_line()-- Checks a line buffer to see if it is an include
1003    line.  If so, we call load_file() recursively to load the included
1004    file.  We never return a syntax error because a statement like
1005    "include = 5" is perfectly legal.  We return false if no include was
1006    processed or true if we matched an include.  */
1007
1008 static bool
1009 include_line (char *line)
1010 {
1011   char quote, *c, *begin, *stop;
1012   
1013   c = line;
1014   while (*c == ' ' || *c == '\t')
1015     c++;
1016
1017   if (strncasecmp (c, "include", 7))
1018       return false;
1019
1020   c += 7;
1021   while (*c == ' ' || *c == '\t')
1022     c++;
1023
1024   /* Find filename between quotes.  */
1025   
1026   quote = *c++;
1027   if (quote != '"' && quote != '\'')
1028     return false;
1029
1030   begin = c;
1031
1032   while (*c != quote && *c != '\0')
1033     c++;
1034
1035   if (*c == '\0')
1036     return false;
1037
1038   stop = c++;
1039   
1040   while (*c == ' ' || *c == '\t')
1041     c++;
1042
1043   if (*c != '\0' && *c != '!')
1044     return false;
1045
1046   /* We have an include line at this point.  */
1047
1048   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1049                    read by anything else.  */
1050
1051   load_file (begin, false);
1052   return true;
1053 }
1054
1055 /* Load a file into memory by calling load_line until the file ends.  */
1056
1057 static try
1058 load_file (const char *filename, bool initial)
1059 {
1060   char *line;
1061   gfc_linebuf *b;
1062   gfc_file *f;
1063   FILE *input;
1064   int len, line_len;
1065
1066   for (f = current_file; f; f = f->up)
1067     if (strcmp (filename, f->filename) == 0)
1068       {
1069         gfc_error_now ("File '%s' is being included recursively", filename);
1070         return FAILURE;
1071       }
1072
1073   if (initial)
1074     {
1075       if (gfc_src_file)
1076         {
1077           input = gfc_src_file;
1078           gfc_src_file = NULL;
1079         }
1080       else
1081         input = gfc_open_file (filename);
1082       if (input == NULL)
1083         {
1084           gfc_error_now ("Can't open file '%s'", filename);
1085           return FAILURE;
1086         }
1087     }
1088   else
1089     {
1090       input = gfc_open_included_file (filename, false);
1091       if (input == NULL)
1092         {
1093           gfc_error_now ("Can't open included file '%s'", filename);
1094           return FAILURE;
1095         }
1096     }
1097
1098   /* Load the file.  */
1099
1100   f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1101   f->up = current_file;
1102   current_file = f;
1103   current_file->line = 1;
1104   line = NULL;
1105   line_len = 0;
1106
1107   if (initial && gfc_src_preprocessor_lines[0])
1108     {
1109       preprocessor_line (gfc_src_preprocessor_lines[0]);
1110       gfc_free (gfc_src_preprocessor_lines[0]);
1111       gfc_src_preprocessor_lines[0] = NULL;
1112       if (gfc_src_preprocessor_lines[1])
1113         {
1114           preprocessor_line (gfc_src_preprocessor_lines[1]);
1115           gfc_free (gfc_src_preprocessor_lines[1]);
1116           gfc_src_preprocessor_lines[1] = NULL;
1117         }
1118     }
1119
1120   for (;;)
1121     {
1122       int trunc = load_line (input, &line, &line_len);
1123
1124       len = strlen (line);
1125       if (feof (input) && len == 0)
1126         break;
1127
1128       /* There are three things this line can be: a line of Fortran
1129          source, an include line or a C preprocessor directive.  */
1130
1131       if (line[0] == '#')
1132         {
1133           preprocessor_line (line);
1134           continue;
1135         }
1136
1137       if (include_line (line))
1138         {
1139           current_file->line++;
1140           continue;
1141         }
1142
1143       /* Add line.  */
1144
1145       b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1146
1147 #ifdef USE_MAPPED_LOCATION
1148       b->location
1149         = linemap_line_start (&line_table, current_file->line++, 120);
1150 #else
1151       b->linenum = current_file->line++;
1152 #endif
1153       b->file = current_file;
1154       b->truncated = trunc;
1155       strcpy (b->line, line);
1156
1157       if (line_head == NULL)
1158         line_head = b;
1159       else
1160         line_tail->next = b;
1161
1162       line_tail = b;
1163     }
1164
1165   /* Release the line buffer allocated in load_line.  */
1166   gfc_free (line);
1167
1168   fclose (input);
1169
1170   current_file = current_file->up;
1171 #ifdef USE_MAPPED_LOCATION
1172   linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1173 #endif
1174   return SUCCESS;
1175 }
1176
1177
1178 /* Open a new file and start scanning from that file. Returns SUCCESS
1179    if everything went OK, FAILURE otherwise.  If form == FORM_UKNOWN
1180    it tries to determine the source form from the filename, defaulting
1181    to free form.  */
1182
1183 try
1184 gfc_new_file (void)
1185 {
1186   try result;
1187
1188   result = load_file (gfc_source_file, true);
1189
1190   gfc_current_locus.lb = line_head;
1191   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1192
1193 #if 0 /* Debugging aid.  */
1194   for (; line_head; line_head = line_head->next)
1195     gfc_status ("%s:%3d %s\n", line_head->file->filename, 
1196 #ifdef USE_MAPPED_LOCATION
1197                 LOCATION_LINE (line_head->location),
1198 #else
1199                 line_head->linenum,
1200 #endif
1201                 line_head->line);
1202
1203   exit (0);
1204 #endif
1205
1206   return result;
1207 }
1208
1209 static char *
1210 unescape_filename (const char *ptr)
1211 {
1212   const char *p = ptr, *s;
1213   char *d, *ret;
1214   int escaped, unescape = 0;
1215
1216   /* Make filename end at quote.  */
1217   escaped = false;
1218   while (*p && ! (! escaped && *p == '"'))
1219     {
1220       if (escaped)
1221         escaped = false;
1222       else if (*p == '\\')
1223         {
1224           escaped = true;
1225           unescape++;
1226         }
1227       ++p;
1228     }
1229
1230   if (! *p || p[1])
1231     return NULL;
1232
1233   /* Undo effects of cpp_quote_string.  */
1234   s = ptr;
1235   d = gfc_getmem (p + 1 - ptr - unescape);
1236   ret = d;
1237
1238   while (s != p)
1239     {
1240       if (*s == '\\')
1241         *d++ = *++s;
1242       else
1243         *d++ = *s;
1244       s++;
1245     }
1246   *d = '\0';
1247   return ret;
1248 }
1249
1250 /* For preprocessed files, if the first tokens are of the form # NUM.
1251    handle the directives so we know the original file name.  */
1252
1253 const char *
1254 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1255 {
1256   int c, len;
1257   char *dirname;
1258
1259   gfc_src_file = gfc_open_file (filename);
1260   if (gfc_src_file == NULL)
1261     return NULL;
1262
1263   c = fgetc (gfc_src_file);
1264   ungetc (c, gfc_src_file);
1265
1266   if (c != '#')
1267     return NULL;
1268
1269   len = 0;
1270   load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1271
1272   if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1273     return NULL;
1274
1275   filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1276   if (filename == NULL)
1277     return NULL;
1278
1279   c = fgetc (gfc_src_file);
1280   ungetc (c, gfc_src_file);
1281
1282   if (c != '#')
1283     return filename;
1284
1285   len = 0;
1286   load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1287
1288   if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1289     return filename;
1290
1291   dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1292   if (dirname == NULL)
1293     return filename;
1294
1295   len = strlen (dirname);
1296   if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1297     {
1298       gfc_free (dirname);
1299       return filename;
1300     }
1301   dirname[len - 2] = '\0';
1302   set_src_pwd (dirname);
1303
1304   if (! IS_ABSOLUTE_PATH (filename))
1305     {
1306       char *p = gfc_getmem (len + strlen (filename));
1307
1308       memcpy (p, dirname, len - 2);
1309       p[len - 2] = '/';
1310       strcpy (p + len - 1, filename);
1311       *canon_source_file = p;
1312     }
1313
1314   gfc_free (dirname);
1315   return filename;
1316 }