]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* input_file.c - Deal with Input Files - |
7152f1dc | 2 | Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 98, 99, 2000 |
2b47531b | 3 | Free Software Foundation, Inc. |
252b5132 RH |
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 | |
2b47531b ILT |
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. */ | |
252b5132 RH |
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 | ||
2b47531b | 35 | static int input_file_get PARAMS ((char *, int)); |
252b5132 RH |
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 | */ | |
40 | int 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 | ||
56 | static FILE *f_in; | |
57 | static char *file_name; | |
58 | ||
59 | /* Struct for saving the state of this module for file includes. */ | |
7152f1dc KH |
60 | struct saved_file { |
61 | FILE *f_in; | |
62 | char *file_name; | |
63 | int preprocess; | |
64 | char *app_save; | |
65 | }; | |
252b5132 | 66 | \f |
c488923f | 67 | /* These hooks accomodate most operating systems. */ |
252b5132 | 68 | |
c488923f | 69 | void |
252b5132 RH |
70 | input_file_begin () |
71 | { | |
72 | f_in = (FILE *) 0; | |
73 | } | |
74 | ||
c488923f | 75 | void |
252b5132 RH |
76 | input_file_end () |
77 | { | |
78 | } | |
79 | ||
c488923f KH |
80 | /* Return BUFFER_SIZE. */ |
81 | unsigned int | |
252b5132 RH |
82 | input_file_buffer_size () |
83 | { | |
84 | return (BUFFER_SIZE); | |
85 | } | |
86 | ||
c488923f | 87 | int |
252b5132 RH |
88 | input_file_is_open () |
89 | { | |
90 | return f_in != (FILE *) 0; | |
91 | } | |
92 | ||
93 | /* Push the state of our input, returning a pointer to saved info that | |
94 | can be restored with input_file_pop (). */ | |
95 | char * | |
96 | input_file_push () | |
97 | { | |
98 | register struct saved_file *saved; | |
99 | ||
100 | saved = (struct saved_file *) xmalloc (sizeof *saved); | |
101 | ||
102 | saved->f_in = f_in; | |
103 | saved->file_name = file_name; | |
104 | saved->preprocess = preprocess; | |
105 | if (preprocess) | |
106 | saved->app_save = app_push (); | |
107 | ||
108 | input_file_begin (); /* Initialize for new file */ | |
109 | ||
110 | return (char *) saved; | |
111 | } | |
112 | ||
113 | void | |
114 | input_file_pop (arg) | |
115 | char *arg; | |
116 | { | |
117 | register struct saved_file *saved = (struct saved_file *) arg; | |
118 | ||
119 | input_file_end (); /* Close out old file */ | |
120 | ||
121 | f_in = saved->f_in; | |
122 | file_name = saved->file_name; | |
123 | preprocess = saved->preprocess; | |
124 | if (preprocess) | |
125 | app_pop (saved->app_save); | |
126 | ||
127 | free (arg); | |
128 | } | |
129 | \f | |
130 | void | |
131 | input_file_open (filename, pre) | |
c488923f | 132 | char *filename; /* "" means use stdin. Must not be 0. */ |
252b5132 RH |
133 | int pre; |
134 | { | |
135 | int c; | |
136 | char buf[80]; | |
137 | ||
138 | preprocess = pre; | |
139 | ||
c488923f | 140 | assert (filename != 0); /* Filename may not be NULL. */ |
252b5132 | 141 | if (filename[0]) |
c488923f | 142 | { /* We have a file name. Suck it and see. */ |
252b5132 RH |
143 | f_in = fopen (filename, "r"); |
144 | file_name = filename; | |
145 | } | |
146 | else | |
c488923f | 147 | { /* use stdin for the input file. */ |
252b5132 | 148 | f_in = stdin; |
c488923f | 149 | file_name = _("{standard input}"); /* For error messages. */ |
252b5132 RH |
150 | } |
151 | if (f_in == (FILE *) 0) | |
152 | { | |
153 | as_bad (_("Can't open %s for reading."), file_name); | |
154 | as_perror ("%s", file_name); | |
155 | return; | |
156 | } | |
157 | ||
158 | c = getc (f_in); | |
159 | if (c == '#') | |
160 | { /* Begins with comment, may not want to preprocess */ | |
161 | c = getc (f_in); | |
162 | if (c == 'N') | |
163 | { | |
164 | fgets (buf, 80, f_in); | |
165 | if (!strcmp (buf, "O_APP\n")) | |
166 | preprocess = 0; | |
167 | if (!strchr (buf, '\n')) | |
168 | ungetc ('#', f_in); /* It was longer */ | |
169 | else | |
170 | ungetc ('\n', f_in); | |
171 | } | |
172 | else if (c == '\n') | |
173 | ungetc ('\n', f_in); | |
174 | else | |
175 | ungetc ('#', f_in); | |
176 | } | |
177 | else | |
178 | ungetc (c, f_in); | |
179 | } | |
180 | ||
181 | /* Close input file. */ | |
c488923f | 182 | void |
252b5132 RH |
183 | input_file_close () |
184 | { | |
185 | if (f_in != NULL) | |
186 | { | |
187 | fclose (f_in); | |
188 | } /* don't close a null file pointer */ | |
189 | f_in = 0; | |
7152f1dc | 190 | } |
252b5132 RH |
191 | |
192 | /* This function is passed to do_scrub_chars. */ | |
193 | ||
194 | static int | |
2b47531b ILT |
195 | input_file_get (buf, buflen) |
196 | char *buf; | |
197 | int buflen; | |
252b5132 | 198 | { |
252b5132 RH |
199 | int size; |
200 | ||
2b47531b | 201 | size = fread (buf, sizeof (char), buflen, f_in); |
252b5132 RH |
202 | if (size < 0) |
203 | { | |
204 | as_perror (_("Can't read from %s"), file_name); | |
205 | size = 0; | |
206 | } | |
252b5132 RH |
207 | return size; |
208 | } | |
209 | ||
210 | /* Read a buffer from the input file. */ | |
211 | ||
212 | char * | |
213 | input_file_give_next_buffer (where) | |
c488923f | 214 | char *where; /* Where to place 1st character of new buffer. */ |
252b5132 | 215 | { |
c488923f | 216 | char *return_value; /* -> Last char of what we read, + 1. */ |
252b5132 RH |
217 | register int size; |
218 | ||
219 | if (f_in == (FILE *) 0) | |
220 | return 0; | |
221 | /* | |
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. | |
226 | */ | |
227 | if (preprocess) | |
228 | size = do_scrub_chars (input_file_get, where, BUFFER_SIZE); | |
229 | else | |
230 | size = fread (where, sizeof (char), BUFFER_SIZE, f_in); | |
231 | if (size < 0) | |
232 | { | |
233 | as_perror (_("Can't read from %s"), file_name); | |
234 | size = 0; | |
235 | } | |
236 | if (size) | |
237 | return_value = where + size; | |
238 | else | |
239 | { | |
240 | if (fclose (f_in)) | |
241 | as_perror (_("Can't close %s"), file_name); | |
242 | f_in = (FILE *) 0; | |
243 | return_value = 0; | |
244 | } | |
245 | return (return_value); | |
246 | } |