1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2 Copyright (C) 1987, 1990, 1991, 1992 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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <errno.h> /* Need this to make errno declaration right */
22 #include "input-file.h"
26 * O/S independent module to supply buffers of sanitised source code
27 * to rest of assembler. We get sanitised input data of arbitrary length.
28 * We break these buffers on line boundaries, recombine pieces that
29 * were broken across buffers, and return a buffer of full lines to
31 * The last partial line begins the next buffer we build and return to caller.
32 * The buffer returned to caller is preceeded by BEFORE_STRING and followed
33 * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
35 * Also looks after line numbers, for e.g. error messages.
39 * We don't care how filthy our buffers are, but our callers assume
40 * that the following sanitation has already been done.
42 * No comments, reduce a comment to a space.
43 * Reduce a tab to a space unless it is 1st char of line.
44 * All multiple tabs and spaces collapsed into 1 char. Tab only
45 * legal if 1st char of line.
46 * # line file statements converted to .line x;.file y; statements.
47 * Escaped newlines at end of line: remove them but add as many newlines
48 * to end of statement as you removed in the middle, to synch line numbers.
51 #define BEFORE_STRING ("\n")
52 #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */
53 #define BEFORE_SIZE (1)
54 #define AFTER_SIZE (1)
56 static char *buffer_start
; /*->1st char of full buffer area. */
57 static char *partial_where
; /*->after last full line in buffer. */
58 static int partial_size
; /* >=0. Number of chars in partial line in buffer. */
59 static char save_source
[AFTER_SIZE
];
60 /* Because we need AFTER_STRING just after last */
61 /* full line, it clobbers 1st part of partial */
62 /* line. So we preserve 1st part of partial */
64 static unsigned int buffer_length
; /* What is the largest size buffer that */
65 /* input_file_give_next_buffer() could */
68 /* The index into an sb structure we are reading from. -1 if none. */
69 static int sb_index
= -1;
71 /* If we are reading from an sb structure, this is it. */
74 /* The number of nested sb structures we have included. */
75 static int macro_nest
;
77 /* We can have more than one source file open at once, though the info for all
78 but the latest one are saved off in a struct input_save. These files remain
79 open, so we are limited by the number of open files allowed by the
80 underlying OS. We may also sequentially read more than one source file in an
83 /* We must track the physical file and line number for error messages. We also
84 track a "logical" file and line number corresponding to (C?) compiler
85 source line numbers. Whenever we open a file we must fill in
86 physical_input_file. So if it is NULL we have not opened any files yet. */
88 static char *physical_input_file
;
89 static char *logical_input_file
;
91 typedef unsigned int line_numberT
; /* 1-origin line number in a source file. */
92 /* A line ends in '\n' or eof. */
94 static line_numberT physical_input_line
;
95 static int logical_input_line
;
97 /* Struct used to save the state of the input handler during include files */
103 char save_source
[AFTER_SIZE
];
104 unsigned int buffer_length
;
105 char *physical_input_file
;
106 char *logical_input_file
;
107 line_numberT physical_input_line
;
108 int logical_input_line
;
111 struct input_save
*next_saved_file
; /* Chain of input_saves */
112 char *input_file_save
; /* Saved state of input routines */
113 char *saved_position
; /* Caller's saved position in buf */
116 static struct input_save
*input_scrub_push
PARAMS ((char *saved_position
));
117 static char *input_scrub_pop
PARAMS ((struct input_save
*arg
));
118 static void as_1_char
PARAMS ((unsigned int c
, FILE * stream
));
120 /* Saved information about the file that .include'd this one. When we hit EOF,
121 we automatically pop to that file. */
123 static struct input_save
*next_saved_file
;
125 /* Push the state of input reading and scrubbing so that we can #include.
126 The return value is a 'void *' (fudged for old compilers) to a save
127 area, which can be restored by passing it to input_scrub_pop(). */
128 static struct input_save
*
129 input_scrub_push (saved_position
)
130 char *saved_position
;
132 register struct input_save
*saved
;
134 saved
= (struct input_save
*) xmalloc (sizeof *saved
);
136 saved
->saved_position
= saved_position
;
137 saved
->buffer_start
= buffer_start
;
138 saved
->partial_where
= partial_where
;
139 saved
->partial_size
= partial_size
;
140 saved
->buffer_length
= buffer_length
;
141 saved
->physical_input_file
= physical_input_file
;
142 saved
->logical_input_file
= logical_input_file
;
143 saved
->physical_input_line
= physical_input_line
;
144 saved
->logical_input_line
= logical_input_line
;
145 saved
->sb_index
= sb_index
;
146 saved
->from_sb
= from_sb
;
147 memcpy (saved
->save_source
, save_source
, sizeof (save_source
));
148 saved
->next_saved_file
= next_saved_file
;
149 saved
->input_file_save
= input_file_push ();
151 input_file_begin (); /* Reinitialize! */
152 logical_input_line
= -1;
153 logical_input_file
= (char *) NULL
;
154 buffer_length
= input_file_buffer_size ();
156 buffer_start
= xmalloc ((BEFORE_SIZE
+ buffer_length
+ buffer_length
+ AFTER_SIZE
));
157 memcpy (buffer_start
, BEFORE_STRING
, (int) BEFORE_SIZE
);
160 } /* input_scrub_push() */
163 input_scrub_pop (saved
)
164 struct input_save
*saved
;
166 char *saved_position
;
168 input_scrub_end (); /* Finish off old buffer */
170 input_file_pop (saved
->input_file_save
);
171 saved_position
= saved
->saved_position
;
172 buffer_start
= saved
->buffer_start
;
173 buffer_length
= saved
->buffer_length
;
174 physical_input_file
= saved
->physical_input_file
;
175 logical_input_file
= saved
->logical_input_file
;
176 physical_input_line
= saved
->physical_input_line
;
177 logical_input_line
= saved
->logical_input_line
;
178 sb_index
= saved
->sb_index
;
179 from_sb
= saved
->from_sb
;
180 partial_where
= saved
->partial_where
;
181 partial_size
= saved
->partial_size
;
182 next_saved_file
= saved
->next_saved_file
;
183 memcpy (save_source
, saved
->save_source
, sizeof (save_source
));
186 return saved_position
;
193 know (strlen (BEFORE_STRING
) == BEFORE_SIZE
);
194 know (strlen (AFTER_STRING
) == AFTER_SIZE
|| (AFTER_STRING
[0] == '\0' && AFTER_SIZE
== 1));
198 buffer_length
= input_file_buffer_size ();
200 buffer_start
= xmalloc ((BEFORE_SIZE
+ buffer_length
+ buffer_length
+ AFTER_SIZE
));
201 memcpy (buffer_start
, BEFORE_STRING
, (int) BEFORE_SIZE
);
203 /* Line number things. */
204 logical_input_line
= -1;
205 logical_input_file
= (char *) NULL
;
206 physical_input_file
= NULL
; /* No file read yet. */
207 next_saved_file
= NULL
; /* At EOF, don't pop to any other file */
222 /* Start reading input from a new file. */
224 char * /* Return start of caller's part of buffer. */
225 input_scrub_new_file (filename
)
228 input_file_open (filename
, !flag_no_comments
);
229 physical_input_file
= filename
[0] ? filename
: "{standard input}";
230 physical_input_line
= 0;
233 return (buffer_start
+ BEFORE_SIZE
);
237 /* Include a file from the current file. Save our state, cause it to
238 be restored on EOF, and begin handling a new file. Same result as
239 input_scrub_new_file. */
242 input_scrub_include_file (filename
, position
)
246 next_saved_file
= input_scrub_push (position
);
247 return input_scrub_new_file (filename
);
250 /* Start getting input from an sb structure. This is used when
251 expanding a macro. */
254 input_scrub_include_sb (from
, position
)
258 if (macro_nest
> max_macro_nest
)
259 as_fatal ("macros nested too deeply");
262 next_saved_file
= input_scrub_push (position
);
265 /* Add the sentinel required by read.c. */
266 sb_add_char (&from_sb
, '\n');
267 sb_add_sb (&from_sb
, from
);
270 /* These variables are reset by input_scrub_push. Restore them
271 since we are, after all, still at the same point in the file. */
272 logical_input_line
= next_saved_file
->logical_input_line
;
273 logical_input_file
= next_saved_file
->logical_input_file
;
283 input_scrub_next_buffer (bufp
)
286 register char *limit
; /*->just after last char of buffer. */
290 if (sb_index
>= from_sb
.len
)
294 partial_where
= NULL
;
295 if (next_saved_file
!= NULL
)
296 *bufp
= input_scrub_pop (next_saved_file
);
297 return partial_where
;
300 partial_where
= from_sb
.ptr
+ from_sb
.len
;
302 *bufp
= from_sb
.ptr
+ sb_index
;
303 sb_index
= from_sb
.len
;
304 return partial_where
;
307 *bufp
= buffer_start
+ BEFORE_SIZE
;
311 memcpy (buffer_start
+ BEFORE_SIZE
, partial_where
,
312 (unsigned int) partial_size
);
313 memcpy (buffer_start
+ BEFORE_SIZE
, save_source
, AFTER_SIZE
);
315 limit
= input_file_give_next_buffer (buffer_start
320 register char *p
; /* Find last newline. */
322 for (p
= limit
; *--p
!= '\n';);;
324 if (p
<= buffer_start
+ BEFORE_SIZE
)
326 as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__
);
329 partial_size
= limit
- p
;
330 memcpy (save_source
, partial_where
, (int) AFTER_SIZE
);
331 memcpy (partial_where
, AFTER_STRING
, (int) AFTER_SIZE
);
336 if (partial_size
> 0)
338 as_warn ("Partial line at end of file ignored");
340 /* If we should pop to another file at EOF, do it. */
343 *bufp
= input_scrub_pop (next_saved_file
); /* Pop state */
344 /* partial_where is now correct to return, since we popped it. */
347 return (partial_where
);
348 } /* input_scrub_next_buffer() */
351 * The remaining part of this file deals with line numbers, error
352 * messages and so on.
357 seen_at_least_1_file () /* TRUE if we opened any file. */
359 return (physical_input_file
!= NULL
);
363 bump_line_counters ()
367 ++physical_input_line
;
368 if (logical_input_line
>= 0)
369 ++logical_input_line
;
376 * Tells us what the new logical line number and file are.
377 * If the line_number is -1, we don't change the current logical line
378 * number. If it is -2, we decrement the logical line number (this is
379 * to support the .appfile pseudo-op inserted into the stream by
381 * If the fname is NULL, we don't change the current logical file name.
384 new_logical_line (fname
, line_number
)
385 char *fname
; /* DON'T destroy it! We point to it! */
390 logical_input_file
= fname
;
391 } /* if we have a file name */
393 if (line_number
>= 0)
394 logical_input_line
= line_number
;
395 else if (line_number
== -2 && logical_input_line
> 0)
396 --logical_input_line
;
397 } /* new_logical_line() */
402 * Return the current file name and line number.
403 * namep should be char * const *, but there are compilers which screw
404 * up declarations like that, and it's easier to avoid it.
407 as_where (namep
, linep
)
411 if (logical_input_file
!= NULL
412 && (linep
== NULL
|| logical_input_line
>= 0))
414 *namep
= logical_input_file
;
416 *linep
= logical_input_line
;
418 else if (physical_input_file
!= NULL
)
420 *namep
= physical_input_file
;
422 *linep
= physical_input_line
;
436 * a s _ h o w m u c h ()
438 * Output to given stream how much of line we have scanned so far.
439 * Assumes we have scanned up to and including input_line_pointer.
440 * No free '\n' at end of line.
444 FILE *stream
; /* Opened for write please. */
446 register char *p
; /* Scan input line. */
447 /* register char c; JF unused */
449 for (p
= input_line_pointer
- 1; *p
!= '\n'; --p
)
452 ++p
; /* p->1st char of line. */
453 for (; p
<= input_line_pointer
; p
++)
455 /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
456 as_1_char ((unsigned char) *p
, stream
);
461 as_1_char (c
, stream
)
467 (void) putc ('%', stream
);
472 (void) putc ('^', stream
);
475 (void) putc (c
, stream
);
478 /* end of input_scrub.c */