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