]>
Commit | Line | Data |
---|---|---|
fecd2382 RP |
1 | /* input_scrub.c - Break up input buffers into whole numbers of lines. |
2 | Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
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 1, or (at your option) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. */ | |
19 | ||
20 | /* static const char rcsid[] = "$Id$"; */ | |
21 | ||
22 | #include <errno.h> /* Need this to make errno declaration right */ | |
23 | #include "as.h" | |
24 | #include "input-file.h" | |
25 | ||
26 | /* | |
27 | * O/S independent module to supply buffers of sanitised source code | |
28 | * to rest of assembler. We get sanitized input data of arbitrary length. | |
29 | * We break these buffers on line boundaries, recombine pieces that | |
30 | * were broken across buffers, and return a buffer of full lines to | |
31 | * the caller. | |
32 | * The last partial line begins the next buffer we build and return to caller. | |
33 | * The buffer returned to caller is preceeded by BEFORE_STRING and followed | |
34 | * by AFTER_STRING, as sentinels. The last character before AFTER_STRING | |
35 | * is a newline. | |
36 | * Also looks after line numbers, for e.g. error messages. | |
37 | */ | |
38 | ||
39 | /* | |
40 | * We don't care how filthy our buffers are, but our callers assume | |
41 | * that the following sanitation has already been done. | |
42 | * | |
43 | * No comments, reduce a comment to a space. | |
44 | * Reduce a tab to a space unless it is 1st char of line. | |
45 | * All multiple tabs and spaces collapsed into 1 char. Tab only | |
46 | * legal if 1st char of line. | |
47 | * # line file statements converted to .line x;.file y; statements. | |
48 | * Escaped newlines at end of line: remove them but add as many newlines | |
49 | * to end of statement as you removed in the middle, to synch line numbers. | |
50 | */ | |
51 | \f | |
52 | #define BEFORE_STRING ("\n") | |
53 | #define AFTER_STRING ("\0") /* bcopy of 0 chars might choke. */ | |
54 | #define BEFORE_SIZE (1) | |
55 | #define AFTER_SIZE (1) | |
56 | ||
57 | static char * buffer_start; /*->1st char of full buffer area. */ | |
58 | static char * partial_where; /*->after last full line in buffer. */ | |
59 | static int partial_size; /* >=0. Number of chars in partial line in buffer. */ | |
60 | static char save_source [AFTER_SIZE]; | |
61 | /* Because we need AFTER_STRING just after last */ | |
62 | /* full line, it clobbers 1st part of partial */ | |
63 | /* line. So we preserve 1st part of partial */ | |
64 | /* line here. */ | |
65 | static int buffer_length; /* What is the largest size buffer that */ | |
66 | /* input_file_give_next_buffer() could */ | |
67 | /* return to us? */ | |
68 | ||
69 | /* Saved information about the file that .include'd this one. When we | |
70 | hit EOF, we automatically pop to that file. */ | |
71 | ||
72 | static char *next_saved_file; | |
73 | ||
74 | /* | |
75 | We can have more than one source file open at once, though the info for | |
76 | all but the latest one are saved off in a struct input_save. These | |
77 | files remain open, so we are limited by the number of open files allowed | |
78 | by the underlying OS. | |
79 | We may also sequentially read more than one source file in an assembly. | |
80 | */ | |
81 | ||
82 | ||
83 | /* | |
84 | We must track the physical file and line number for error messages. | |
85 | We also track a "logical" file and line number corresponding to (C?) | |
86 | compiler source line numbers. | |
87 | Whenever we open a file we must fill in physical_input_file. So if it is NULL | |
88 | we have not opened any files yet. | |
89 | */ | |
90 | ||
91 | static | |
92 | char * physical_input_file, | |
93 | * logical_input_file; | |
94 | ||
95 | ||
96 | ||
97 | typedef unsigned int line_numberT; /* 1-origin line number in a source file. */ | |
98 | /* A line ends in '\n' or eof. */ | |
99 | ||
100 | static | |
101 | line_numberT physical_input_line, | |
102 | logical_input_line; | |
103 | ||
104 | /* Struct used to save the state of the input handler during include files */ | |
105 | struct input_save { | |
106 | char *buffer_start; | |
107 | char *partial_where; | |
108 | int partial_size; | |
109 | char save_source [AFTER_SIZE]; | |
110 | int buffer_length; | |
111 | char *physical_input_file; | |
112 | char *logical_input_file; | |
113 | line_numberT physical_input_line; | |
114 | line_numberT logical_input_line; | |
115 | char *next_saved_file; /* Chain of input_saves */ | |
116 | char *input_file_save; /* Saved state of input routines */ | |
117 | char *saved_position; /* Caller's saved position in buf */ | |
118 | }; | |
119 | ||
120 | #ifdef __STDC__ | |
121 | static void as_1_char(unsigned int c, FILE *stream); | |
122 | #else /* __STDC__ */ | |
123 | static void as_1_char(); | |
124 | #endif /* __STDC__ */ | |
125 | ||
126 | /* Push the state of input reading and scrubbing so that we can #include. | |
127 | The return value is a 'void *' (fudged for old compilers) to a save | |
128 | area, which can be restored by passing it to input_scrub_pop(). */ | |
129 | char * | |
130 | input_scrub_push(saved_position) | |
131 | char *saved_position; | |
132 | { | |
133 | register struct input_save *saved; | |
134 | ||
135 | saved = (struct input_save *) xmalloc(sizeof *saved); | |
136 | ||
137 | saved->saved_position = saved_position; | |
138 | saved->buffer_start = buffer_start; | |
139 | saved->partial_where = partial_where; | |
140 | saved->partial_size = partial_size; | |
141 | saved->buffer_length = buffer_length; | |
142 | saved->physical_input_file = physical_input_file; | |
143 | saved->logical_input_file = logical_input_file; | |
144 | saved->physical_input_line = physical_input_line; | |
145 | saved->logical_input_line = logical_input_line; | |
146 | bcopy (saved->save_source, save_source, sizeof (save_source)); | |
147 | saved->next_saved_file = next_saved_file; | |
148 | saved->input_file_save = input_file_push (); | |
149 | ||
150 | input_scrub_begin (); /* Reinitialize! */ | |
151 | ||
152 | return (char *)saved; | |
153 | } | |
154 | ||
155 | char * | |
156 | input_scrub_pop (arg) | |
157 | char *arg; | |
158 | { | |
159 | register struct input_save *saved; | |
160 | char *saved_position; | |
161 | ||
162 | input_scrub_end (); /* Finish off old buffer */ | |
163 | ||
164 | saved = (struct input_save *)arg; | |
165 | ||
166 | input_file_pop (saved->input_file_save); | |
167 | saved_position = saved->saved_position; | |
168 | buffer_start = saved->buffer_start; | |
169 | buffer_length = saved->buffer_length; | |
170 | physical_input_file = saved->physical_input_file; | |
171 | logical_input_file = saved->logical_input_file; | |
172 | physical_input_line = saved->physical_input_line; | |
173 | logical_input_line = saved->logical_input_line; | |
174 | partial_where = saved->partial_where; | |
175 | partial_size = saved->partial_size; | |
176 | next_saved_file = saved->next_saved_file; | |
177 | bcopy (save_source, saved->save_source, sizeof (save_source)); | |
178 | ||
179 | free(arg); | |
180 | return saved_position; | |
181 | } | |
182 | ||
183 | \f | |
184 | void | |
185 | input_scrub_begin () | |
186 | { | |
187 | know(strlen(BEFORE_STRING) == BEFORE_SIZE); | |
188 | know(strlen(AFTER_STRING) == AFTER_SIZE | |
189 | || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1)); | |
190 | ||
191 | input_file_begin (); | |
192 | ||
193 | buffer_length = input_file_buffer_size (); | |
194 | ||
195 | buffer_start = xmalloc((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE)); | |
196 | bcopy (BEFORE_STRING, buffer_start, (int)BEFORE_SIZE); | |
197 | ||
198 | /* Line number things. */ | |
199 | logical_input_line = 0; | |
200 | logical_input_file = (char *)NULL; | |
201 | physical_input_file = NULL; /* No file read yet. */ | |
202 | next_saved_file = NULL; /* At EOF, don't pop to any other file */ | |
203 | do_scrub_begin(); | |
204 | } | |
205 | ||
206 | void | |
207 | input_scrub_end () | |
208 | { | |
209 | if (buffer_start) | |
210 | { | |
211 | free (buffer_start); | |
212 | buffer_start = 0; | |
213 | input_file_end (); | |
214 | } | |
215 | } | |
216 | ||
217 | /* Start reading input from a new file. */ | |
218 | ||
219 | char * /* Return start of caller's part of buffer. */ | |
220 | input_scrub_new_file (filename) | |
221 | char * filename; | |
222 | { | |
223 | input_file_open (filename, !flagseen['f']); | |
224 | physical_input_file = filename[0] ? filename : "{standard input}"; | |
225 | physical_input_line = 0; | |
226 | ||
227 | partial_size = 0; | |
228 | return (buffer_start + BEFORE_SIZE); | |
229 | } | |
230 | ||
231 | ||
232 | /* Include a file from the current file. Save our state, cause it to | |
233 | be restored on EOF, and begin handling a new file. Same result as | |
234 | input_scrub_new_file. */ | |
235 | ||
236 | char * | |
237 | input_scrub_include_file (filename, position) | |
238 | char *filename; | |
239 | char *position; | |
240 | { | |
241 | next_saved_file = input_scrub_push (position); | |
242 | return input_scrub_new_file (filename); | |
243 | } | |
244 | ||
245 | void | |
246 | input_scrub_close () | |
247 | { | |
248 | input_file_close (); | |
249 | } | |
250 | char * | |
251 | input_scrub_next_buffer (bufp) | |
252 | char **bufp; | |
253 | { | |
254 | register char * limit; /*->just after last char of buffer. */ | |
255 | ||
256 | *bufp = buffer_start + BEFORE_SIZE; | |
257 | ||
258 | #ifdef DONTDEF | |
259 | if(preprocess) { | |
260 | if(save_buffer) { | |
261 | *bufp = save_buffer; | |
262 | save_buffer = 0; | |
263 | } | |
264 | limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE); | |
265 | if (!limit) { | |
266 | partial_where = 0; | |
267 | if(partial_size) | |
268 | as_warn("Partial line at end of file ignored"); | |
269 | return partial_where; | |
270 | } | |
271 | ||
272 | if(partial_size) | |
273 | bcopy(save_source, partial_where,(int)AFTER_SIZE); | |
274 | do_scrub(partial_where,partial_size,buffer_start+BEFORE_SIZE,limit-(buffer_start+BEFORE_SIZE),&out_string,&out_length); | |
275 | limit=out_string + out_length; | |
276 | for(p=limit;*--p!='\n';) | |
277 | ; | |
278 | p++; | |
279 | if(p<=buffer_start+BEFORE_SIZE) | |
280 | as_fatal("Source line too long. Please change file '%s' and re-make the assembler.", __FILE__); | |
281 | ||
282 | partial_where = p; | |
283 | partial_size = limit-p; | |
284 | bcopy(partial_where, save_source,(int)AFTER_SIZE); | |
285 | bcopy(AFTER_STRING, partial_where, (int)AFTER_SIZE); | |
286 | ||
287 | save_buffer = *bufp; | |
288 | *bufp = out_string; | |
289 | ||
290 | return partial_where; | |
291 | } | |
292 | ||
293 | /* We're not preprocessing. Do the right thing */ | |
294 | #endif | |
295 | if (partial_size) | |
296 | { | |
297 | bcopy (partial_where, buffer_start + BEFORE_SIZE, (int)partial_size); | |
298 | bcopy (save_source, buffer_start + BEFORE_SIZE, (int)AFTER_SIZE); | |
299 | } | |
300 | limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size); | |
301 | if (limit) | |
302 | { | |
303 | register char * p; /* Find last newline. */ | |
304 | ||
305 | for (p = limit; * -- p != '\n';) | |
306 | { | |
307 | } | |
308 | ++ p; | |
309 | if (p <= buffer_start + BEFORE_SIZE) | |
310 | { | |
311 | as_fatal("Source line too long. Please change file %s then rebuild assembler.", __FILE__); | |
312 | } | |
313 | partial_where = p; | |
314 | partial_size = limit - p; | |
315 | bcopy (partial_where, save_source, (int)AFTER_SIZE); | |
316 | bcopy (AFTER_STRING, partial_where, (int)AFTER_SIZE); | |
317 | } | |
318 | else | |
319 | { | |
320 | partial_where = 0; | |
321 | if (partial_size > 0) | |
322 | { | |
323 | as_warn("Partial line at end of file ignored"); | |
324 | } | |
325 | /* If we should pop to another file at EOF, do it. */ | |
326 | if (next_saved_file) | |
327 | { | |
328 | *bufp = input_scrub_pop (next_saved_file); /* Pop state */ | |
329 | /* partial_where is now correct to return, since we popped it. */ | |
330 | } | |
331 | } | |
332 | return (partial_where); | |
333 | } | |
334 | \f | |
335 | /* | |
336 | * The remaining part of this file deals with line numbers, error | |
337 | * messages and so on. | |
338 | */ | |
339 | ||
340 | ||
341 | int | |
342 | seen_at_least_1_file () /* TRUE if we opened any file. */ | |
343 | { | |
344 | return (physical_input_file != NULL); | |
345 | } | |
346 | ||
347 | void | |
348 | bump_line_counters () | |
349 | { | |
350 | ++ physical_input_line; | |
351 | ++ logical_input_line; | |
352 | } | |
353 | \f | |
354 | /* | |
355 | * new_logical_line() | |
356 | * | |
357 | * Tells us what the new logical line number and file are. | |
358 | * If the line_number is <0, we don't change the current logical line number. | |
359 | * If the fname is NULL, we don't change the current logical file name. | |
360 | */ | |
361 | void new_logical_line(fname, line_number) | |
362 | char *fname; /* DON'T destroy it! We point to it! */ | |
363 | int line_number; | |
364 | { | |
365 | if (fname) { | |
366 | logical_input_file = fname; | |
367 | } /* if we have a file name */ | |
368 | ||
369 | if (line_number >= 0) { | |
370 | logical_input_line = line_number; | |
371 | } /* if we have a line number */ | |
372 | } /* new_logical_line() */ | |
373 | \f | |
374 | /* | |
375 | * a s _ w h e r e () | |
376 | * | |
377 | * Write a line to stderr locating where we are in reading | |
378 | * input source files. | |
379 | * As a sop to the debugger of AS, pretty-print the offending line. | |
380 | */ | |
381 | void | |
382 | as_where() | |
383 | { | |
384 | char *p; | |
385 | line_numberT line; | |
386 | ||
387 | if (physical_input_file) | |
388 | { /* we tried to read SOME source */ | |
389 | if (input_file_is_open()) | |
390 | { /* we can still read lines from source */ | |
391 | #ifdef DONTDEF | |
392 | fprintf (stderr," @ physical line %ld., file \"%s\"", | |
393 | (long) physical_input_line, physical_input_file); | |
394 | fprintf (stderr," @ logical line %ld., file \"%s\"\n", | |
395 | (long) logical_input_line, logical_input_file); | |
396 | (void)putc(' ', stderr); | |
397 | as_howmuch (stderr); | |
398 | (void)putc('\n', stderr); | |
399 | #else | |
400 | p = logical_input_file ? logical_input_file : physical_input_file; | |
401 | line = logical_input_line ? logical_input_line : physical_input_line; | |
402 | fprintf(stderr,"%s:%u: ", p, line); | |
403 | #endif | |
404 | } | |
405 | else | |
406 | { | |
407 | #ifdef DONTDEF | |
408 | fprintf (stderr," After reading source.\n"); | |
409 | #else | |
410 | p = logical_input_file ? logical_input_file : physical_input_file; | |
411 | line = logical_input_line ? logical_input_line : physical_input_line; | |
412 | fprintf(stderr, "%s:%d:", p, (int) line); | |
413 | #endif | |
414 | } | |
415 | } | |
416 | else | |
417 | { | |
418 | #ifdef DONTDEF | |
419 | fprintf (stderr," Before reading source.\n"); | |
420 | #else | |
421 | #endif | |
422 | } | |
423 | } | |
424 | ||
425 | ||
426 | ||
427 | \f | |
428 | /* | |
429 | * a s _ h o w m u c h () | |
430 | * | |
431 | * Output to given stream how much of line we have scanned so far. | |
432 | * Assumes we have scanned up to and including input_line_pointer. | |
433 | * No free '\n' at end of line. | |
434 | */ | |
435 | void | |
436 | as_howmuch (stream) | |
437 | FILE * stream; /* Opened for write please. */ | |
438 | { | |
439 | register char * p; /* Scan input line. */ | |
440 | /* register char c; JF unused */ | |
441 | ||
442 | for (p = input_line_pointer - 1; * p != '\n'; --p) | |
443 | { | |
444 | } | |
445 | ++ p; /* p->1st char of line. */ | |
446 | for (; p <= input_line_pointer; p++) | |
447 | { | |
448 | /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */ | |
449 | /* c = *p & 0xFF; JF unused */ | |
450 | as_1_char(*p, stream); | |
451 | } | |
452 | } | |
453 | ||
454 | static void as_1_char (c,stream) | |
455 | unsigned int c; | |
456 | FILE *stream; | |
457 | { | |
458 | if (c > 127) | |
459 | { | |
460 | (void)putc('%', stream); | |
461 | c -= 128; | |
462 | } | |
463 | if (c < 32) | |
464 | { | |
465 | (void)putc('^', stream); | |
466 | c += '@'; | |
467 | } | |
468 | (void)putc(c, stream); | |
469 | } | |
470 | ||
471 | /* | |
472 | * Local Variables: | |
473 | * comment-column: 0 | |
474 | * fill-column: 131 | |
475 | * End: | |
476 | */ | |
477 | ||
478 | /* end: input_scrub.c */ |