1 /* input_file.c - Deal with Input Files -
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 * Confines all details of reading source bytes to this module.
24 * All O/S specific crocks should live here.
25 * What we lose in "efficiency" we gain in modularity.
26 * Note we don't need to #include the "as.h" file. No common coupling!
33 #include "input-file.h"
35 static int input_file_get
PARAMS ((char *, int));
37 /* This variable is non-zero if the file currently being read should be
38 preprocessed by app. It is zero if the file can be read straight in.
43 * This code opens a file, then delivers BUFFER_SIZE character
44 * chunks of the file on demand.
45 * BUFFER_SIZE is supposed to be a number chosen for speed.
46 * The caller only asks once what BUFFER_SIZE is, and asks before
47 * the nature of the input files (if any) is known.
50 #define BUFFER_SIZE (32 * 1024)
53 * We use static data: the data area is not sharable.
57 static char *file_name
;
59 /* Struct for saving the state of this module for file includes. */
68 /* These hooks accomodate most operating systems. */
81 /* Return BUFFER_SIZE. */
83 input_file_buffer_size ()
91 return f_in
!= (FILE *) 0;
94 /* Push the state of our input, returning a pointer to saved info that
95 can be restored with input_file_pop (). */
99 register struct saved_file
*saved
;
101 saved
= (struct saved_file
*) xmalloc (sizeof *saved
);
104 saved
->file_name
= file_name
;
105 saved
->preprocess
= preprocess
;
107 saved
->app_save
= app_push ();
109 input_file_begin (); /* Initialize for new file */
111 return (char *) saved
;
118 register struct saved_file
*saved
= (struct saved_file
*) arg
;
120 input_file_end (); /* Close out old file */
123 file_name
= saved
->file_name
;
124 preprocess
= saved
->preprocess
;
126 app_pop (saved
->app_save
);
132 input_file_open (filename
, pre
)
133 char *filename
; /* "" means use stdin. Must not be 0. */
141 assert (filename
!= 0); /* Filename may not be NULL. */
143 { /* We have a file name. Suck it and see. */
144 f_in
= fopen (filename
, "r");
145 file_name
= filename
;
148 { /* use stdin for the input file. */
150 file_name
= _("{standard input}"); /* For error messages. */
152 if (f_in
== (FILE *) 0)
154 as_bad (_("Can't open %s for reading."), file_name
);
155 as_perror ("%s", file_name
);
161 { /* Begins with comment, may not want to preprocess */
165 fgets (buf
, 80, f_in
);
166 if (!strcmp (buf
, "O_APP\n"))
168 if (!strchr (buf
, '\n'))
169 ungetc ('#', f_in
); /* It was longer */
182 /* Close input file. */
189 } /* don't close a null file pointer */
191 } /* input_file_close() */
193 /* This function is passed to do_scrub_chars. */
196 input_file_get (buf
, buflen
)
202 size
= fread (buf
, sizeof (char), buflen
, f_in
);
205 as_perror (_("Can't read from %s"), file_name
);
211 /* Read a buffer from the input file. */
214 input_file_give_next_buffer (where
)
215 char *where
; /* Where to place 1st character of new buffer. */
217 char *return_value
; /* -> Last char of what we read, + 1. */
220 if (f_in
== (FILE *) 0)
223 * fflush (stdin); could be done here if you want to synchronise
224 * stdin and stdout, for the case where our input file is stdin.
225 * Since the assembler shouldn't do any output to stdout, we
226 * don't bother to synch output and input.
229 size
= do_scrub_chars (input_file_get
, where
, BUFFER_SIZE
);
231 size
= fread (where
, sizeof (char), BUFFER_SIZE
, f_in
);
234 as_perror (_("Can't read from %s"), file_name
);
238 return_value
= where
+ size
;
242 as_perror (_("Can't close %s"), file_name
);
246 return (return_value
);
249 /* end of input-file.c */