This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gas / input-file.c
1 /* input_file.c - Deal with Input Files -
2    Copyright (C) 1987, 1990, 1991, 1992 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   {
64     FILE *f_in;
65     char *file_name;
66     int preprocess;
67     char *app_save;
68   };
69 \f
70 /* These hooks accomodate most operating systems. */
71
72 void 
73 input_file_begin ()
74 {
75   f_in = (FILE *) 0;
76 }
77
78 void 
79 input_file_end ()
80 {
81 }
82
83 /* Return BUFFER_SIZE. */
84 int 
85 input_file_buffer_size ()
86 {
87   return (BUFFER_SIZE);
88 }
89
90 int 
91 input_file_is_open ()
92 {
93   return f_in != (FILE *) 0;
94 }
95
96 /* Push the state of our input, returning a pointer to saved info that
97    can be restored with input_file_pop ().  */
98 char *
99 input_file_push ()
100 {
101   register struct saved_file *saved;
102
103   saved = (struct saved_file *) xmalloc (sizeof *saved);
104
105   saved->f_in = f_in;
106   saved->file_name = file_name;
107   saved->preprocess = preprocess;
108   if (preprocess)
109     saved->app_save = app_push ();
110
111   input_file_begin ();          /* Initialize for new file */
112
113   return (char *) saved;
114 }
115
116 void
117 input_file_pop (arg)
118      char *arg;
119 {
120   register struct saved_file *saved = (struct saved_file *) arg;
121
122   input_file_end ();            /* Close out old file */
123
124   f_in = saved->f_in;
125   file_name = saved->file_name;
126   preprocess = saved->preprocess;
127   if (preprocess)
128     app_pop (saved->app_save);
129
130   free (arg);
131 }
132 \f
133 #ifdef DONTDEF                  /* JF save old version in case we need it */
134 void
135 input_file_open (filename, preprocess, debugging)
136      char *filename;            /* "" means use stdin. Must not be 0. */
137      int preprocess;            /* TRUE if needs app. */
138      int debugging;             /* TRUE if we are debugging assembler. */
139 {
140   assert (filename != 0);       /* Filename may not be NULL. */
141   if (filename[0])
142     {                           /* We have a file name. Suck it and see. */
143       file_handle = open (filename, O_RDONLY, 0);
144       file_name = filename;
145     }
146   else
147     {                           /* use stdin for the input file. */
148       file_handle = fileno (stdin);
149       file_name = "{standard input}";   /* For error messages. */
150     }
151   if (file_handle < 0)
152     as_perror ("Can't open %s for reading", file_name);
153   if (preprocess)
154     {
155       /*
156                      * This code was written in haste for a frobbed BSD 4.2.
157                      * I have a flight to catch: will someone please do proper
158                      * error checks? - Dean.
159                      */
160       int pid;
161       char temporary_file_name[12];
162       int fd;
163       union wait status;
164
165       (void) strcpy (temporary_file_name, "#appXXXXXX");
166       (void) mktemp (temporary_file_name);
167       pid = vfork ();
168       if (pid == -1)
169         {
170           as_perror ("Vfork failed", file_name);
171           _exit (144);
172         }
173       if (pid == 0)
174         {
175           (void) dup2 (file_handle, fileno (stdin));
176           fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
177           if (fd == -1)
178             {
179               (void) write (2, "Can't open temporary\n", 21);
180               _exit (99);
181             }
182           (void) dup2 (fd, fileno (stdout));
183           /* JF for testing #define PREPROCESSOR "/lib/app" */
184 #define PREPROCESSOR "./app"
185           execl (PREPROCESSOR, PREPROCESSOR, 0);
186           execl ("app", "app", 0);
187           (void) write (2, "Exec of app failed.  Get help.\n", 31);
188           (void) unlink (temporary_file_name);
189           _exit (11);
190         }
191       (void) wait (&status);
192       if (status.w_status & 0xFF00)     /* JF was 0xF000, was wrong */
193         {
194           file_handle = -1;
195           as_bad ("Can't preprocess file \"%s\", status = %xx", file_name, status.w_status);
196         }
197       else
198         {
199           file_handle = open (temporary_file_name, O_RDONLY, 0);
200           if (!debugging && unlink (temporary_file_name))
201             as_perror ("Can't delete temp file %s", temporary_file_name);
202         }
203       if (file_handle == -1)
204         as_perror ("Can't retrieve temp file %s", temporary_file_name);
205     }
206 }
207
208 #else
209
210 void
211 input_file_open (filename, pre)
212      char *filename;            /* "" means use stdin. Must not be 0. */
213      int pre;
214 {
215   int c;
216   char buf[80];
217
218   preprocess = pre;
219
220   assert (filename != 0);       /* Filename may not be NULL. */
221   if (filename[0])
222     {                           /* We have a file name. Suck it and see. */
223       f_in = fopen (filename, "r");
224       file_name = filename;
225     }
226   else
227     {                           /* use stdin for the input file. */
228       f_in = stdin;
229       file_name = "{standard input}";   /* For error messages. */
230     }
231   if (f_in == (FILE *) 0)
232     {
233       as_perror ("Can't open %s for reading", file_name);
234       return;
235     }
236
237 #ifdef _IOFBF
238   /* Ask stdio to buffer our input at BUFFER_SIZE, with a dynamically
239            allocated buffer.  */
240   setvbuf (f_in, (char *) NULL, _IOFBF, BUFFER_SIZE);
241 #endif /* VMS */
242
243   c = getc (f_in);
244   if (c == '#')
245     {                           /* Begins with comment, may not want to preprocess */
246       c = getc (f_in);
247       if (c == 'N')
248         {
249           fgets (buf, 80, f_in);
250           if (!strcmp (buf, "O_APP\n"))
251             preprocess = 0;
252           if (!strchr (buf, '\n'))
253             ungetc ('#', f_in); /* It was longer */
254           else
255             ungetc ('\n', f_in);
256         }
257       else if (c == '\n')
258         ungetc ('\n', f_in);
259       else
260         ungetc ('#', f_in);
261     }
262   else
263     ungetc (c, f_in);
264
265 #ifdef DONTDEF
266   if (preprocess)
267     {
268       char temporary_file_name[17];
269       FILE *f_out;
270
271       (void) strcpy (temporary_file_name, "/tmp/#appXXXXXX");
272       (void) mktemp (temporary_file_name);
273       f_out = fopen (temporary_file_name, "w+");
274       if (f_out == (FILE *) 0)
275         as_perror ("Can't open temp file %s", temporary_file_name);
276
277       /* JF this will have to be moved on any system that
278                    does not support removal of open files.  */
279       (void) unlink (temporary_file_name);      /* JF do it NOW */
280       do_scrub (f_in, f_out);
281       (void) fclose (f_in);     /* All done with it */
282       (void) rewind (f_out);
283       f_in = f_out;
284     }
285 #endif
286 }
287
288 #endif
289
290 /* Close input file.  */
291 void 
292 input_file_close ()
293 {
294   if (f_in != NULL)
295     {
296       fclose (f_in);
297     }                           /* don't close a null file pointer */
298   f_in = 0;
299 }                               /* input_file_close() */
300
301 char *
302 input_file_give_next_buffer (where)
303      char *where;               /* Where to place 1st character of new buffer. */
304 {
305   char *return_value;           /* -> Last char of what we read, + 1. */
306   register int size;
307
308   if (f_in == (FILE *) 0)
309     return 0;
310   /*
311          * fflush (stdin); could be done here if you want to synchronise
312          * stdin and stdout, for the case where our input file is stdin.
313          * Since the assembler shouldn't do any output to stdout, we
314          * don't bother to synch output and input.
315          */
316   if (preprocess)
317     {
318       char *p;
319       int n;
320       int ch;
321       extern FILE *scrub_file;
322
323       scrub_file = f_in;
324       for (p = where, n = BUFFER_SIZE; n; --n)
325         {
326
327           ch = do_scrub_next_char (scrub_from_file, scrub_to_file);
328           if (ch == EOF)
329             break;
330           *p++ = ch;
331         }
332       size = BUFFER_SIZE - n;
333     }
334   else
335     size = fread (where, sizeof (char), BUFFER_SIZE, f_in);
336   if (size < 0)
337     {
338       as_perror ("Can't read from %s", file_name);
339       size = 0;
340     }
341   if (size)
342     return_value = where + size;
343   else
344     {
345       if (fclose (f_in))
346         as_perror ("Can't close %s", file_name);
347       f_in = (FILE *) 0;
348       return_value = 0;
349     }
350   return (return_value);
351 }
352
353 /* end of input-file.c */