checkpoint
[binutils-gdb.git] / gas / input-file.c
1 /* input_file.c - Deal with Input Files -
2 Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 * Confines all details of reading source bytes to this module.
22 * All O/S specific crocks should live here.
23 * What we lose in "efficiency" we gain in modularity.
24 * Note we don't need to #include the "as.h" file. No common coupling!
25 */
26
27 #ifdef USG
28 #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOFBF, (size))
29 #endif
30
31 #include <stdio.h>
32 #include <assert.h>
33 #include <string.h>
34
35 #include "as.h"
36 #include "input-file.h"
37
38 /* This variable is non-zero if the file currently being read should be
39 preprocessed by app. It is zero if the file can be read straight in.
40 */
41 int preprocess = 0;
42
43 /*
44 * This code opens a file, then delivers BUFFER_SIZE character
45 * chunks of the file on demand.
46 * BUFFER_SIZE is supposed to be a number chosen for speed.
47 * The caller only asks once what BUFFER_SIZE is, and asks before
48 * the nature of the input files (if any) is known.
49 */
50
51 #define BUFFER_SIZE (32 * 1024)
52
53 /*
54 * We use static data: the data area is not sharable.
55 */
56
57 FILE *f_in;
58 /* static JF remove static so app.c can use file_name */
59 char * file_name;
60
61 /* Struct for saving the state of this module for file includes. */
62 struct saved_file {
63 FILE *f_in;
64 char *file_name;
65 int preprocess;
66 char *app_save;
67 };
68 \f
69 /* These hooks accomodate most operating systems. */
70
71 void input_file_begin() {
72 f_in = (FILE *)0;
73 }
74
75 void input_file_end () { }
76
77 /* Return BUFFER_SIZE. */
78 int input_file_buffer_size() {
79 return (BUFFER_SIZE);
80 }
81
82 int input_file_is_open() {
83 return f_in!=(FILE *)0;
84 }
85
86 /* Push the state of our input, returning a pointer to saved info that
87 can be restored with input_file_pop (). */
88 char *input_file_push () {
89 register struct saved_file *saved;
90
91 saved = (struct saved_file *)xmalloc (sizeof *saved);
92
93 saved->f_in = f_in;
94 saved->file_name = file_name;
95 saved->preprocess = preprocess;
96 if (preprocess)
97 saved->app_save = app_push ();
98
99 input_file_begin (); /* Initialize for new file */
100
101 return (char *)saved;
102 }
103
104 void
105 input_file_pop (arg)
106 char *arg;
107 {
108 register struct saved_file *saved = (struct saved_file *)arg;
109
110 input_file_end (); /* Close out old file */
111
112 f_in = saved->f_in;
113 file_name = saved->file_name;
114 preprocess = saved->preprocess;
115 if (preprocess)
116 app_pop (saved->app_save);
117
118 free(arg);
119 }
120 \f
121 #ifdef DONTDEF /* JF save old version in case we need it */
122 void
123 input_file_open (filename, preprocess, debugging)
124 char * filename; /* "" means use stdin. Must not be 0. */
125 int preprocess; /* TRUE if needs app. */
126 int debugging; /* TRUE if we are debugging assembler. */
127 {
128 assert( filename != 0 ); /* Filename may not be NULL. */
129 if (filename [0])
130 { /* We have a file name. Suck it and see. */
131 file_handle = open (filename, O_RDONLY, 0);
132 file_name = filename;
133 }
134 else
135 { /* use stdin for the input file. */
136 file_handle = fileno (stdin);
137 file_name = "{standard input}"; /* For error messages. */
138 }
139 if (file_handle < 0)
140 as_perror ("Can't open %s for reading", file_name);
141 if ( preprocess )
142 {
143 /*
144 * This code was written in haste for a frobbed BSD 4.2.
145 * I have a flight to catch: will someone please do proper
146 * error checks? - Dean.
147 */
148 int pid;
149 char temporary_file_name [12];
150 int fd;
151 union wait status;
152
153 (void)strcpy (temporary_file_name, "#appXXXXXX");
154 (void)mktemp (temporary_file_name);
155 pid = vfork ();
156 if (pid == -1)
157 {
158 as_perror ("Vfork failed", file_name);
159 _exit (144);
160 }
161 if (pid == 0)
162 {
163 (void)dup2 (file_handle, fileno(stdin));
164 fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
165 if (fd == -1)
166 {
167 (void)write(2,"Can't open temporary\n",21);
168 _exit (99);
169 }
170 (void)dup2 (fd, fileno(stdout));
171 /* JF for testing #define PREPROCESSOR "/lib/app" */
172 #define PREPROCESSOR "./app"
173 execl (PREPROCESSOR, PREPROCESSOR, 0);
174 execl ("app","app",0);
175 (void)write(2,"Exec of app failed. Get help.\n",31);
176 (void)unlink(temporary_file_name);
177 _exit (11);
178 }
179 (void)wait (& status);
180 if (status.w_status & 0xFF00) /* JF was 0xF000, was wrong */
181 {
182 file_handle = -1;
183 as_bad( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
184 }
185 else
186 {
187 file_handle = open (temporary_file_name, O_RDONLY, 0);
188 if ( ! debugging && unlink(temporary_file_name))
189 as_perror ("Can't delete temp file %s", temporary_file_name);
190 }
191 if (file_handle == -1)
192 as_perror ("Can't retrieve temp file %s", temporary_file_name);
193 }
194 }
195 #else
196
197 void
198 input_file_open (filename,pre)
199 char * filename; /* "" means use stdin. Must not be 0. */
200 int pre;
201 {
202 int c;
203 char buf[80];
204
205 preprocess = pre;
206
207 assert( filename != 0 ); /* Filename may not be NULL. */
208 if (filename [0]) { /* We have a file name. Suck it and see. */
209 f_in=fopen(filename,"r");
210 file_name=filename;
211 } else { /* use stdin for the input file. */
212 f_in = stdin;
213 file_name = "{standard input}"; /* For error messages. */
214 }
215 if (f_in==(FILE *)0) {
216 as_perror ("Can't open %s for reading", file_name);
217 return;
218 }
219
220 #ifndef VMS
221 /* Ask stdio to buffer our input at BUFFER_SIZE, with a dynamically
222 allocated buffer. */
223 setvbuf(f_in, (char *)NULL, _IOFBF, BUFFER_SIZE);
224 #endif /* VMS */
225
226 c = getc(f_in);
227 if (c == '#') { /* Begins with comment, may not want to preprocess */
228 c = getc(f_in);
229 if (c == 'N') {
230 fgets(buf,80,f_in);
231 if (!strcmp(buf,"O_APP\n"))
232 preprocess=0;
233 if (!strchr(buf,'\n'))
234 ungetc('#',f_in); /* It was longer */
235 else
236 ungetc('\n',f_in);
237 } else if(c=='\n')
238 ungetc('\n',f_in);
239 else
240 ungetc('#',f_in);
241 } else
242 ungetc(c,f_in);
243
244 #ifdef DONTDEF
245 if ( preprocess ) {
246 char temporary_file_name [17];
247 FILE *f_out;
248
249 (void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
250 (void)mktemp (temporary_file_name);
251 f_out=fopen(temporary_file_name,"w+");
252 if(f_out==(FILE *)0)
253 as_perror("Can't open temp file %s",temporary_file_name);
254
255 /* JF this will have to be moved on any system that
256 does not support removal of open files. */
257 (void)unlink(temporary_file_name);/* JF do it NOW */
258 do_scrub(f_in,f_out);
259 (void)fclose(f_in); /* All done with it */
260 (void)rewind(f_out);
261 f_in=f_out;
262 }
263 #endif
264 }
265 #endif
266
267 /* Close input file. */
268 void input_file_close() {
269 if (f_in != NULL) {
270 fclose (f_in);
271 } /* don't close a null file pointer */
272 f_in = 0;
273 } /* input_file_close() */
274
275 char *
276 input_file_give_next_buffer (where)
277 char * where; /* Where to place 1st character of new buffer. */
278 {
279 char * return_value; /* -> Last char of what we read, + 1. */
280 register int size;
281
282 if (f_in == (FILE *)0)
283 return 0;
284 /*
285 * fflush (stdin); could be done here if you want to synchronise
286 * stdin and stdout, for the case where our input file is stdin.
287 * Since the assembler shouldn't do any output to stdout, we
288 * don't bother to synch output and input.
289 */
290 if(preprocess) {
291 char *p;
292 int n;
293 int ch;
294 extern FILE *scrub_file;
295
296 scrub_file=f_in;
297 for (p = where, n = BUFFER_SIZE; n; --n) {
298
299 ch = do_scrub_next_char(scrub_from_file, scrub_to_file);
300 if (ch == EOF)
301 break;
302 *p++=ch;
303 }
304 size=BUFFER_SIZE-n;
305 } else
306 size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
307 if (size < 0)
308 {
309 as_perror ("Can't read from %s", file_name);
310 size = 0;
311 }
312 if (size)
313 return_value = where + size;
314 else
315 {
316 if (fclose (f_in))
317 as_perror ("Can't close %s", file_name);
318 f_in = (FILE *)0;
319 return_value = 0;
320 }
321 return (return_value);
322 }
323
324 /* end of input-file.c */