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