1 /* input_file.c - Deal with Input Files -
2 Copyright (C) 1987, 1990, 1991, 1992, 1998 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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!
31 #include "input-file.h"
33 static int input_file_get PARAMS ((char **));
35 /* This variable is non-zero if the file currently being read should be
36 preprocessed by app. It is zero if the file can be read straight in.
41 * This code opens a file, then delivers BUFFER_SIZE character
42 * chunks of the file on demand.
43 * BUFFER_SIZE is supposed to be a number chosen for speed.
44 * The caller only asks once what BUFFER_SIZE is, and asks before
45 * the nature of the input files (if any) is known.
48 #define BUFFER_SIZE (32 * 1024)
51 * We use static data: the data area is not sharable.
55 static char *file_name;
57 /* Struct for saving the state of this module for file includes. */
66 /* These hooks accomodate most operating systems. */
79 /* Return BUFFER_SIZE. */
81 input_file_buffer_size ()
89 return f_in != (FILE *) 0;
92 /* Push the state of our input, returning a pointer to saved info that
93 can be restored with input_file_pop (). */
97 register struct saved_file *saved;
99 saved = (struct saved_file *) xmalloc (sizeof *saved);
102 saved->file_name = file_name;
103 saved->preprocess = preprocess;
105 saved->app_save = app_push ();
107 input_file_begin (); /* Initialize for new file */
109 return (char *) saved;
116 register struct saved_file *saved = (struct saved_file *) arg;
118 input_file_end (); /* Close out old file */
121 file_name = saved->file_name;
122 preprocess = saved->preprocess;
124 app_pop (saved->app_save);
130 input_file_open (filename, pre)
131 char *filename; /* "" means use stdin. Must not be 0. */
139 assert (filename != 0); /* Filename may not be NULL. */
141 { /* We have a file name. Suck it and see. */
142 f_in = fopen (filename, "r");
143 file_name = filename;
146 { /* use stdin for the input file. */
148 file_name = _("{standard input}"); /* For error messages. */
150 if (f_in == (FILE *) 0)
152 as_bad (_("Can't open %s for reading."), file_name);
153 as_perror ("%s", file_name);
159 { /* Begins with comment, may not want to preprocess */
163 fgets (buf, 80, f_in);
164 if (!strcmp (buf, "O_APP\n"))
166 if (!strchr (buf, '\n'))
167 ungetc ('#', f_in); /* It was longer */
180 /* Close input file. */
187 } /* don't close a null file pointer */
189 } /* input_file_close() */
191 /* This function is passed to do_scrub_chars. */
194 input_file_get (from)
197 static char buf[BUFFER_SIZE];
200 size = fread (buf, sizeof (char), sizeof buf, f_in);
203 as_perror (_("Can't read from %s"), file_name);
210 /* Read a buffer from the input file. */
213 input_file_give_next_buffer (where)
214 char *where; /* Where to place 1st character of new buffer. */
216 char *return_value; /* -> Last char of what we read, + 1. */
219 if (f_in == (FILE *) 0)
222 * fflush (stdin); could be done here if you want to synchronise
223 * stdin and stdout, for the case where our input file is stdin.
224 * Since the assembler shouldn't do any output to stdout, we
225 * don't bother to synch output and input.
228 size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
230 size = fread (where, sizeof (char), BUFFER_SIZE, f_in);
233 as_perror (_("Can't read from %s"), file_name);
237 return_value = where + size;
241 as_perror (_("Can't close %s"), file_name);
245 return (return_value);
248 /* end of input-file.c */