1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2 Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
20 /* static const char rcsid[] = "$Id$"; */
22 #include <errno.h> /* Need this to make errno declaration right */
24 #include "input-file.h"
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
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
36 * Also looks after line numbers, for e.g. error messages.
40 * We don't care how filthy our buffers are, but our callers assume
41 * that the following sanitation has already been done.
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.
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)
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 */
65 static int buffer_length; /* What is the largest size buffer that */
66 /* input_file_give_next_buffer() could */
69 /* Saved information about the file that .include'd this one. When we
70 hit EOF, we automatically pop to that file. */
72 static char *next_saved_file;
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
79 We may also sequentially read more than one source file in an assembly.
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.
92 char * physical_input_file,
97 typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
98 /* A line ends in '\n' or eof. */
101 line_numberT physical_input_line,
104 /* Struct used to save the state of the input handler during include files */
109 char save_source [AFTER_SIZE];
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 */
121 static void as_1_char(unsigned int c, FILE *stream);
123 static void as_1_char();
124 #endif /* __STDC__ */
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(). */
130 input_scrub_push(saved_position)
131 char *saved_position;
133 register struct input_save *saved;
135 saved = (struct input_save *) xmalloc(sizeof *saved);
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 ();
150 input_scrub_begin (); /* Reinitialize! */
152 return (char *)saved;
156 input_scrub_pop (arg)
159 register struct input_save *saved;
160 char *saved_position;
162 input_scrub_end (); /* Finish off old buffer */
164 saved = (struct input_save *)arg;
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));
180 return saved_position;
187 know(strlen(BEFORE_STRING) == BEFORE_SIZE);
188 know(strlen(AFTER_STRING) == AFTER_SIZE
189 || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
193 buffer_length = input_file_buffer_size ();
195 buffer_start = xmalloc((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
196 bcopy (BEFORE_STRING, buffer_start, (int)BEFORE_SIZE);
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 */
217 /* Start reading input from a new file. */
219 char * /* Return start of caller's part of buffer. */
220 input_scrub_new_file (filename)
223 input_file_open (filename, !flagseen['f']);
224 physical_input_file = filename[0] ? filename : "{standard input}";
225 physical_input_line = 0;
228 return (buffer_start + BEFORE_SIZE);
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. */
237 input_scrub_include_file (filename, position)
241 next_saved_file = input_scrub_push (position);
242 return input_scrub_new_file (filename);
251 input_scrub_next_buffer (bufp)
254 register char * limit; /*->just after last char of buffer. */
256 *bufp = buffer_start + BEFORE_SIZE;
264 limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE);
268 as_warn("Partial line at end of file ignored");
269 return partial_where;
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';)
279 if(p<=buffer_start+BEFORE_SIZE)
280 as_fatal("Source line too long. Please change file '%s' and re-make the assembler.", __FILE__);
283 partial_size = limit-p;
284 bcopy(partial_where, save_source,(int)AFTER_SIZE);
285 bcopy(AFTER_STRING, partial_where, (int)AFTER_SIZE);
290 return partial_where;
293 /* We're not preprocessing. Do the right thing */
297 bcopy (partial_where, buffer_start + BEFORE_SIZE, (int)partial_size);
298 bcopy (save_source, buffer_start + BEFORE_SIZE, (int)AFTER_SIZE);
300 limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
303 register char * p; /* Find last newline. */
305 for (p = limit; * -- p != '\n';)
309 if (p <= buffer_start + BEFORE_SIZE)
311 as_fatal("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
314 partial_size = limit - p;
315 bcopy (partial_where, save_source, (int)AFTER_SIZE);
316 bcopy (AFTER_STRING, partial_where, (int)AFTER_SIZE);
321 if (partial_size > 0)
323 as_warn("Partial line at end of file ignored");
325 /* If we should pop to another file at EOF, do it. */
328 *bufp = input_scrub_pop (next_saved_file); /* Pop state */
329 /* partial_where is now correct to return, since we popped it. */
332 return (partial_where);
336 * The remaining part of this file deals with line numbers, error
337 * messages and so on.
342 seen_at_least_1_file () /* TRUE if we opened any file. */
344 return (physical_input_file != NULL);
348 bump_line_counters ()
350 ++ physical_input_line;
351 ++ logical_input_line;
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.
361 void new_logical_line(fname, line_number)
362 char *fname; /* DON'T destroy it! We point to it! */
366 logical_input_file = fname;
367 } /* if we have a file name */
369 if (line_number >= 0) {
370 logical_input_line = line_number;
371 } /* if we have a line number */
372 } /* new_logical_line() */
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.
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 */
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);
398 (void)putc('\n', stderr);
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);
408 fprintf (stderr," After reading source.\n");
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);
419 fprintf (stderr," Before reading source.\n");
429 * a s _ h o w m u c h ()
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.
437 FILE * stream; /* Opened for write please. */
439 register char * p; /* Scan input line. */
440 /* register char c; JF unused */
442 for (p = input_line_pointer - 1; * p != '\n'; --p)
445 ++ p; /* p->1st char of line. */
446 for (; p <= input_line_pointer; p++)
448 /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
449 /* c = *p & 0xFF; JF unused */
450 as_1_char(*p, stream);
454 static void as_1_char (c,stream)
460 (void)putc('%', stream);
465 (void)putc('^', stream);
468 (void)putc(c, stream);
478 /* end: input_scrub.c */