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