1 /* input_file.c - Deal with Input Files -
2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
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!
28 static char rcsid[] = "$FreeBSD$";
32 #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOFBF, (size))
39 #include "input-file.h"
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.
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.
54 #define BUFFER_SIZE (32 * 1024)
57 * We use static data: the data area is not sharable.
61 /* static JF remove static so app.c can use file_name */
64 /* Struct for saving the state of this module for file includes. */
72 /* These hooks accomodate most operating systems. */
74 void input_file_begin() {
78 void input_file_end () { }
80 /* Return BUFFER_SIZE. */
81 int input_file_buffer_size() {
85 int input_file_is_open() {
86 return f_in != (FILE *)0;
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;
94 saved = (struct saved_file *)xmalloc (sizeof *saved);
97 saved->file_name = file_name;
98 saved->preprocess = preprocess;
100 saved->app_save = app_push ();
102 input_file_begin (); /* Initialize for new file */
104 return (char *)saved;
111 register struct saved_file *saved = (struct saved_file *)arg;
113 input_file_end (); /* Close out old file */
116 file_name = saved->file_name;
117 preprocess = saved->preprocess;
119 app_pop (saved->app_save);
124 #ifdef DONTDEF /* JF save old version in case we need it */
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. */
131 assert( filename != 0 ); /* Filename may not be NULL. */
133 { /* We have a file name. Suck it and see. */
134 file_handle = open (filename, O_RDONLY, 0);
135 file_name = filename;
138 { /* use stdin for the input file. */
139 file_handle = fileno (stdin);
140 file_name = "{standard input}"; /* For error messages. */
143 as_perror ("Can't open %s for reading", file_name);
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.
152 char temporary_file_name[12];
156 (void)strcpy (temporary_file_name, "#appXXXXXX");
157 (void)mktemp (temporary_file_name);
161 as_perror ("Vfork failed", file_name);
166 (void)dup2 (file_handle, fileno(stdin));
167 fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
170 (void)write(2,"Can't open temporary\n",21);
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);
182 (void)wait (& status);
183 if (status.w_status & 0xFF00) /* JF was 0xF000, was wrong */
186 as_bad( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
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);
194 if (file_handle == -1)
195 as_perror ("Can't retrieve temp file %s", temporary_file_name);
201 input_file_open (filename,pre)
202 char * filename; /* "" means use stdin. Must not be 0. */
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");
214 } else { /* use stdin for the input file. */
216 file_name = "{standard input}"; /* For error messages. */
218 if (f_in == (FILE *)0) {
219 as_perror ("Can't open %s for reading", file_name);
224 /* Ask stdio to buffer our input at BUFFER_SIZE, with a dynamically
226 setvbuf(f_in, (char *)NULL, _IOFBF, BUFFER_SIZE);
230 if (c == '#') { /* Begins with comment, may not want to preprocess */
234 if (!strcmp(buf,"O_APP\n"))
236 if (!strchr(buf,'\n'))
237 ungetc('#',f_in); /* It was longer */
240 } else if (c == '\n')
249 char temporary_file_name[17];
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);
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 */
270 /* Close input file. */
271 void input_file_close() {
274 } /* don't close a null file pointer */
276 } /* input_file_close() */
279 input_file_give_next_buffer (where)
280 char * where; /* Where to place 1st character of new buffer. */
282 char * return_value; /* -> Last char of what we read, + 1. */
285 if (f_in == (FILE *)0)
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.
297 extern FILE *scrub_file;
300 for (p = where, n = BUFFER_SIZE; n; --n) {
302 ch = do_scrub_next_char(scrub_from_file, scrub_to_file);
309 size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
312 as_perror ("Can't read from %s", file_name);
316 return_value = where + size;
320 as_perror ("Can't close %s", file_name);
324 return (return_value);
327 /* end of input-file.c */