(input_file_open): Remove call to stat(). Add a check for getc() failing, and
[external/binutils.git] / gas / input-file.c
1 /* input_file.c - Deal with Input Files -
2    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2003
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* Confines all details of reading source bytes to this module.
23    All O/S specific crocks should live here.
24    What we lose in "efficiency" we gain in modularity.
25    Note we don't need to #include the "as.h" file. No common coupling!  */
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include "as.h"
31 #include "input-file.h"
32 #include "safe-ctype.h"
33
34 static int input_file_get (char *, int);
35
36 /* This variable is non-zero if the file currently being read should be
37    preprocessed by app.  It is zero if the file can be read straight in.  */
38 int preprocess = 0;
39
40 /* This code opens a file, then delivers BUFFER_SIZE character
41    chunks of the file on demand.
42    BUFFER_SIZE is supposed to be a number chosen for speed.
43    The caller only asks once what BUFFER_SIZE is, and asks before
44    the nature of the input files (if any) is known.  */
45
46 #define BUFFER_SIZE (32 * 1024)
47
48 /* We use static data: the data area is not sharable.  */
49
50 static FILE *f_in;
51 static char *file_name;
52
53 /* Struct for saving the state of this module for file includes.  */
54 struct saved_file
55   {
56     FILE * f_in;
57     char * file_name;
58     int    preprocess;
59     char * app_save;
60   };
61 \f
62 /* These hooks accommodate most operating systems.  */
63
64 void
65 input_file_begin (void)
66 {
67   f_in = (FILE *) 0;
68 }
69
70 void
71 input_file_end (void)
72 {
73 }
74
75 /* Return BUFFER_SIZE.  */
76 unsigned int
77 input_file_buffer_size (void)
78 {
79   return (BUFFER_SIZE);
80 }
81
82 int
83 input_file_is_open (void)
84 {
85   return f_in != (FILE *) 0;
86 }
87
88 /* Push the state of our input, returning a pointer to saved info that
89    can be restored with input_file_pop ().  */
90
91 char *
92 input_file_push (void)
93 {
94   register struct saved_file *saved;
95
96   saved = (struct saved_file *) xmalloc (sizeof *saved);
97
98   saved->f_in = f_in;
99   saved->file_name = file_name;
100   saved->preprocess = preprocess;
101   if (preprocess)
102     saved->app_save = app_push ();
103
104   /* Initialize for new file.  */
105   input_file_begin ();
106
107   return (char *) saved;
108 }
109
110 void
111 input_file_pop (char *arg)
112 {
113   register struct saved_file *saved = (struct saved_file *) arg;
114
115   input_file_end ();            /* Close out old file.  */
116
117   f_in = saved->f_in;
118   file_name = saved->file_name;
119   preprocess = saved->preprocess;
120   if (preprocess)
121     app_pop (saved->app_save);
122
123   free (arg);
124 }
125 \f
126 void
127 input_file_open (char *filename, /* "" means use stdin. Must not be 0.  */
128                  int pre)
129 {
130   int c;
131   char buf[80];
132
133   preprocess = pre;
134
135   assert (filename != 0);       /* Filename may not be NULL.  */
136   if (filename[0])
137     {
138       f_in = fopen (filename, FOPEN_RT);
139       file_name = filename;
140     }
141   else
142     {
143       /* Use stdin for the input file.  */
144       f_in = stdin;
145       /* For error messages.  */
146       file_name = _("{standard input}");
147     }
148
149   if (f_in)
150     c = getc (f_in);
151
152   if (f_in == NULL || ferror (f_in))
153     {
154       switch (errno)
155         {
156         case ENOENT:
157           as_bad (_("%s: no such file"), filename);
158           break;
159         case EISDIR:
160           as_bad (_("%s: is a directory"), filename);
161           break;
162         default:
163           as_bad (_("can't open %s for reading"), file_name);
164           as_perror ("%s", file_name);
165         }
166
167       if (f_in)
168         {
169           fclose (f_in);
170           f_in = NULL;
171         }
172       return;
173     }
174
175   if (c == '#')
176     {
177       /* Begins with comment, may not want to preprocess.  */
178       c = getc (f_in);
179       if (c == 'N')
180         {
181           fgets (buf, 80, f_in);
182           if (!strncmp (buf, "O_APP", 5) && ISSPACE (buf[5]))
183             preprocess = 0;
184           if (!strchr (buf, '\n'))
185             ungetc ('#', f_in); /* It was longer.  */
186           else
187             ungetc ('\n', f_in);
188         }
189       else if (c == 'A')
190         {
191           fgets (buf, 80, f_in);
192           if (!strncmp (buf, "PP", 2) && ISSPACE (buf[2]))
193             preprocess = 1;
194           if (!strchr (buf, '\n'))
195             ungetc ('#', f_in);
196           else
197             ungetc ('\n', f_in);
198         }
199       else if (c == '\n')
200         ungetc ('\n', f_in);
201       else
202         ungetc ('#', f_in);
203     }
204   else
205     ungetc (c, f_in);
206 }
207
208 /* Close input file.  */
209
210 void
211 input_file_close (void)
212 {
213   /* Don't close a null file pointer.  */
214   if (f_in != NULL)
215     fclose (f_in);
216
217   f_in = 0;
218 }
219
220 /* This function is passed to do_scrub_chars.  */
221
222 static int
223 input_file_get (char *buf, int buflen)
224 {
225   int size;
226
227   size = fread (buf, sizeof (char), buflen, f_in);
228   if (size < 0)
229     {
230       as_perror (_("Can't read from %s"), file_name);
231       size = 0;
232     }
233   return size;
234 }
235
236 /* Read a buffer from the input file.  */
237
238 char *
239 input_file_give_next_buffer (char *where /* Where to place 1st character of new buffer.  */)
240 {
241   char *return_value;           /* -> Last char of what we read, + 1.  */
242   register int size;
243
244   if (f_in == (FILE *) 0)
245     return 0;
246   /* fflush (stdin); could be done here if you want to synchronise
247      stdin and stdout, for the case where our input file is stdin.
248      Since the assembler shouldn't do any output to stdout, we
249      don't bother to synch output and input.  */
250   if (preprocess)
251     size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
252   else
253     size = fread (where, sizeof (char), BUFFER_SIZE, f_in);
254   if (size < 0)
255     {
256       as_perror (_("Can't read from %s"), file_name);
257       size = 0;
258     }
259   if (size)
260     return_value = where + size;
261   else
262     {
263       if (fclose (f_in))
264         as_perror (_("Can't close %s"), file_name);
265       f_in = (FILE *) 0;
266       return_value = 0;
267     }
268
269   return return_value;
270 }