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