source: trunk/src/binutils/gas/input-file.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.5 KB
Line 
1/* input_file.c - Deal with Input Files -
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307, USA. */
21
22/*
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!
27 */
28
29#include <stdio.h>
30#include <string.h>
31
32#include "as.h"
33#include "input-file.h"
34
35static int input_file_get PARAMS ((char *, int));
36
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.
39 */
40int preprocess = 0;
41
42/*
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.
48 */
49
50#define BUFFER_SIZE (32 * 1024)
51
52/*
53 * We use static data: the data area is not sharable.
54 */
55
56static FILE *f_in;
57static char *file_name;
58
59/* Struct for saving the state of this module for file includes. */
60struct saved_file {
61 FILE *f_in;
62 char *file_name;
63 int preprocess;
64 char *app_save;
65};
66
67
68/* These hooks accomodate most operating systems. */
69
70void
71input_file_begin ()
72{
73 f_in = (FILE *) 0;
74}
75
76void
77input_file_end ()
78{
79}
80
81/* Return BUFFER_SIZE. */
82unsigned int
83input_file_buffer_size ()
84{
85 return (BUFFER_SIZE);
86}
87
88int
89input_file_is_open ()
90{
91 return f_in != (FILE *) 0;
92}
93
94/* Push the state of our input, returning a pointer to saved info that
95 can be restored with input_file_pop (). */
96char *
97input_file_push ()
98{
99 register struct saved_file *saved;
100
101 saved = (struct saved_file *) xmalloc (sizeof *saved);
102
103 saved->f_in = f_in;
104 saved->file_name = file_name;
105 saved->preprocess = preprocess;
106 if (preprocess)
107 saved->app_save = app_push ();
108
109 input_file_begin (); /* Initialize for new file */
110
111 return (char *) saved;
112}
113
114void
115input_file_pop (arg)
116 char *arg;
117{
118 register struct saved_file *saved = (struct saved_file *) arg;
119
120 input_file_end (); /* Close out old file */
121
122 f_in = saved->f_in;
123 file_name = saved->file_name;
124 preprocess = saved->preprocess;
125 if (preprocess)
126 app_pop (saved->app_save);
127
128 free (arg);
129}
130
131
132void
133input_file_open (filename, pre)
134 char *filename; /* "" means use stdin. Must not be 0. */
135 int pre;
136{
137 int c;
138 char buf[80];
139
140 preprocess = pre;
141
142 assert (filename != 0); /* Filename may not be NULL. */
143 if (filename[0])
144 { /* We have a file name. Suck it and see. */
145 f_in = fopen (filename, "r");
146 file_name = filename;
147 }
148 else
149 { /* use stdin for the input file. */
150 f_in = stdin;
151 file_name = _("{standard input}"); /* For error messages. */
152 }
153 if (f_in == (FILE *) 0)
154 {
155 as_bad (_("Can't open %s for reading."), file_name);
156 as_perror ("%s", file_name);
157 return;
158 }
159
160 c = getc (f_in);
161 if (c == '#')
162 { /* Begins with comment, may not want to preprocess */
163 c = getc (f_in);
164 if (c == 'N')
165 {
166 fgets (buf, 80, f_in);
167 if (!strcmp (buf, "O_APP\n"))
168 preprocess = 0;
169 if (!strchr (buf, '\n'))
170 ungetc ('#', f_in); /* It was longer */
171 else
172 ungetc ('\n', f_in);
173 }
174 else if (c == '\n')
175 ungetc ('\n', f_in);
176 else
177 ungetc ('#', f_in);
178 }
179 else
180 ungetc (c, f_in);
181}
182
183/* Close input file. */
184void
185input_file_close ()
186{
187 if (f_in != NULL)
188 {
189 fclose (f_in);
190 } /* don't close a null file pointer */
191 f_in = 0;
192}
193
194/* This function is passed to do_scrub_chars. */
195
196static int
197input_file_get (buf, buflen)
198 char *buf;
199 int buflen;
200{
201 int size;
202
203 size = fread (buf, sizeof (char), buflen, f_in);
204 if (size < 0)
205 {
206 as_perror (_("Can't read from %s"), file_name);
207 size = 0;
208 }
209 return size;
210}
211
212/* Read a buffer from the input file. */
213
214char *
215input_file_give_next_buffer (where)
216 char *where; /* Where to place 1st character of new buffer. */
217{
218 char *return_value; /* -> Last char of what we read, + 1. */
219 register int size;
220
221 if (f_in == (FILE *) 0)
222 return 0;
223 /*
224 * fflush (stdin); could be done here if you want to synchronise
225 * stdin and stdout, for the case where our input file is stdin.
226 * Since the assembler shouldn't do any output to stdout, we
227 * don't bother to synch output and input.
228 */
229 if (preprocess)
230 size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
231 else
232 size = fread (where, sizeof (char), BUFFER_SIZE, f_in);
233 if (size < 0)
234 {
235 as_perror (_("Can't read from %s"), file_name);
236 size = 0;
237 }
238 if (size)
239 return_value = where + size;
240 else
241 {
242 if (fclose (f_in))
243 as_perror (_("Can't close %s"), file_name);
244 f_in = (FILE *) 0;
245 return_value = 0;
246 }
247 return (return_value);
248}
Note: See TracBrowser for help on using the repository browser.