bfd/
[deliverable/binutils-gdb.git] / gas / input-scrub.c
CommitLineData
252b5132 1/* input_scrub.c - Break up input buffers into whole numbers of lines.
f7e42eb4 2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
ebd1c875 3 2000, 2001, 2003, 2006
252b5132
RH
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
252b5132 22
252b5132
RH
23#include "as.h"
24#include "input-file.h"
25#include "sb.h"
26#include "listing.h"
27
28/*
29 * O/S independent module to supply buffers of sanitised source code
30 * to rest of assembler. We get sanitised input data of arbitrary length.
31 * We break these buffers on line boundaries, recombine pieces that
32 * were broken across buffers, and return a buffer of full lines to
33 * the caller.
34 * The last partial line begins the next buffer we build and return to caller.
47eebc20 35 * The buffer returned to caller is preceded by BEFORE_STRING and followed
252b5132
RH
36 * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
37 * is a newline.
38 * Also looks after line numbers, for e.g. error messages.
39 */
40
41/*
42 * We don't care how filthy our buffers are, but our callers assume
43 * that the following sanitation has already been done.
44 *
45 * No comments, reduce a comment to a space.
46 * Reduce a tab to a space unless it is 1st char of line.
47 * All multiple tabs and spaces collapsed into 1 char. Tab only
48 * legal if 1st char of line.
49 * # line file statements converted to .line x;.file y; statements.
50 * Escaped newlines at end of line: remove them but add as many newlines
51 * to end of statement as you removed in the middle, to synch line numbers.
52 */
53\f
54#define BEFORE_STRING ("\n")
e13b337a 55#define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */
252b5132
RH
56#define BEFORE_SIZE (1)
57#define AFTER_SIZE (1)
58
e13b337a
KH
59static char *buffer_start; /*->1st char of full buffer area. */
60static char *partial_where; /*->after last full line in buffer. */
61static int partial_size; /* >=0. Number of chars in partial line in buffer. */
7152f1dc
KH
62
63/* Because we need AFTER_STRING just after last full line, it clobbers
64 1st part of partial line. So we preserve 1st part of partial line
65 here. */
252b5132 66static char save_source[AFTER_SIZE];
7152f1dc
KH
67
68/* What is the largest size buffer that input_file_give_next_buffer()
69 could return to us? */
70static unsigned int buffer_length;
252b5132
RH
71
72/* The index into an sb structure we are reading from. -1 if none. */
73static int sb_index = -1;
74
75/* If we are reading from an sb structure, this is it. */
76static sb from_sb;
77
9f10757c
TW
78/* Should we do a conditional check on from_sb? */
79static int from_sb_is_expansion = 1;
80
252b5132
RH
81/* The number of nested sb structures we have included. */
82int macro_nest;
83
84/* We can have more than one source file open at once, though the info for all
85 but the latest one are saved off in a struct input_save. These files remain
86 open, so we are limited by the number of open files allowed by the
87 underlying OS. We may also sequentially read more than one source file in an
e13b337a 88 assembly. */
252b5132
RH
89
90/* We must track the physical file and line number for error messages. We also
91 track a "logical" file and line number corresponding to (C?) compiler
92 source line numbers. Whenever we open a file we must fill in
e13b337a 93 physical_input_file. So if it is NULL we have not opened any files yet. */
252b5132
RH
94
95static char *physical_input_file;
96static char *logical_input_file;
97
e13b337a
KH
98typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
99/* A line ends in '\n' or eof. */
252b5132
RH
100
101static line_numberT physical_input_line;
102static int logical_input_line;
103
104/* Struct used to save the state of the input handler during include files */
ee515fb7 105struct input_save {
e972090a
NC
106 char * buffer_start;
107 char * partial_where;
108 int partial_size;
109 char save_source[AFTER_SIZE];
110 unsigned int buffer_length;
111 char * physical_input_file;
112 char * logical_input_file;
113 line_numberT physical_input_line;
114 int logical_input_line;
115 int sb_index;
116 sb from_sb;
117 int from_sb_is_expansion; /* Should we do a conditional check? */
118 struct input_save * next_saved_file; /* Chain of input_saves. */
119 char * input_file_save; /* Saved state of input routines. */
120 char * saved_position; /* Caller's saved position in buf. */
7152f1dc 121};
252b5132 122
b1f1fa96
KH
123static struct input_save *input_scrub_push (char *saved_position);
124static char *input_scrub_pop (struct input_save *arg);
252b5132
RH
125
126/* Saved information about the file that .include'd this one. When we hit EOF,
e13b337a 127 we automatically pop to that file. */
252b5132
RH
128
129static struct input_save *next_saved_file;
130
131/* Push the state of input reading and scrubbing so that we can #include.
132 The return value is a 'void *' (fudged for old compilers) to a save
e13b337a 133 area, which can be restored by passing it to input_scrub_pop(). */
7152f1dc 134
252b5132 135static struct input_save *
b1f1fa96 136input_scrub_push (char *saved_position)
252b5132
RH
137{
138 register struct input_save *saved;
139
140 saved = (struct input_save *) xmalloc (sizeof *saved);
141
142 saved->saved_position = saved_position;
143 saved->buffer_start = buffer_start;
144 saved->partial_where = partial_where;
145 saved->partial_size = partial_size;
146 saved->buffer_length = buffer_length;
147 saved->physical_input_file = physical_input_file;
148 saved->logical_input_file = logical_input_file;
149 saved->physical_input_line = physical_input_line;
150 saved->logical_input_line = logical_input_line;
151 saved->sb_index = sb_index;
152 saved->from_sb = from_sb;
9f10757c 153 saved->from_sb_is_expansion = from_sb_is_expansion;
252b5132
RH
154 memcpy (saved->save_source, save_source, sizeof (save_source));
155 saved->next_saved_file = next_saved_file;
156 saved->input_file_save = input_file_push ();
157
158 input_file_begin (); /* Reinitialize! */
159 logical_input_line = -1;
160 logical_input_file = (char *) NULL;
161 buffer_length = input_file_buffer_size ();
162 sb_index = -1;
163
164 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
165 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
166
167 return saved;
7152f1dc 168}
252b5132
RH
169
170static char *
b1f1fa96 171input_scrub_pop (struct input_save *saved)
252b5132
RH
172{
173 char *saved_position;
174
175 input_scrub_end (); /* Finish off old buffer */
176
177 input_file_pop (saved->input_file_save);
178 saved_position = saved->saved_position;
179 buffer_start = saved->buffer_start;
180 buffer_length = saved->buffer_length;
181 physical_input_file = saved->physical_input_file;
182 logical_input_file = saved->logical_input_file;
183 physical_input_line = saved->physical_input_line;
184 logical_input_line = saved->logical_input_line;
185 sb_index = saved->sb_index;
186 from_sb = saved->from_sb;
9f10757c 187 from_sb_is_expansion = saved->from_sb_is_expansion;
252b5132
RH
188 partial_where = saved->partial_where;
189 partial_size = saved->partial_size;
190 next_saved_file = saved->next_saved_file;
191 memcpy (save_source, saved->save_source, sizeof (save_source));
192
193 free (saved);
194 return saved_position;
195}
196\f
252b5132 197void
b1f1fa96 198input_scrub_begin (void)
252b5132
RH
199{
200 know (strlen (BEFORE_STRING) == BEFORE_SIZE);
7152f1dc
KH
201 know (strlen (AFTER_STRING) == AFTER_SIZE
202 || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
252b5132
RH
203
204 input_file_begin ();
205
206 buffer_length = input_file_buffer_size ();
207
208 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
209 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
210
e13b337a 211 /* Line number things. */
252b5132
RH
212 logical_input_line = -1;
213 logical_input_file = (char *) NULL;
e13b337a 214 physical_input_file = NULL; /* No file read yet. */
252b5132
RH
215 next_saved_file = NULL; /* At EOF, don't pop to any other file */
216 do_scrub_begin (flag_m68k_mri);
217}
218
219void
b1f1fa96 220input_scrub_end (void)
252b5132
RH
221{
222 if (buffer_start)
223 {
224 free (buffer_start);
225 buffer_start = 0;
226 input_file_end ();
227 }
228}
229
7152f1dc
KH
230/* Start reading input from a new file.
231 Return start of caller's part of buffer. */
252b5132 232
7152f1dc 233char *
b1f1fa96 234input_scrub_new_file (char *filename)
252b5132
RH
235{
236 input_file_open (filename, !flag_no_comments);
237 physical_input_file = filename[0] ? filename : _("{standard input}");
238 physical_input_line = 0;
239
240 partial_size = 0;
241 return (buffer_start + BEFORE_SIZE);
242}
243
252b5132
RH
244/* Include a file from the current file. Save our state, cause it to
245 be restored on EOF, and begin handling a new file. Same result as
e13b337a 246 input_scrub_new_file. */
252b5132
RH
247
248char *
b1f1fa96 249input_scrub_include_file (char *filename, char *position)
252b5132
RH
250{
251 next_saved_file = input_scrub_push (position);
252 return input_scrub_new_file (filename);
253}
254
255/* Start getting input from an sb structure. This is used when
256 expanding a macro. */
257
258void
b1f1fa96 259input_scrub_include_sb (sb *from, char *position, int is_expansion)
252b5132
RH
260{
261 if (macro_nest > max_macro_nest)
dcb87e5c 262 as_fatal (_("macros nested too deeply"));
252b5132
RH
263 ++macro_nest;
264
9f10757c
TW
265#ifdef md_macro_start
266 if (is_expansion)
267 {
268 md_macro_start ();
269 }
270#endif
271
252b5132
RH
272 next_saved_file = input_scrub_push (position);
273
274 sb_new (&from_sb);
9f10757c 275 from_sb_is_expansion = is_expansion;
252b5132
RH
276 if (from->len >= 1 && from->ptr[0] != '\n')
277 {
278 /* Add the sentinel required by read.c. */
279 sb_add_char (&from_sb, '\n');
280 }
c19d1205 281 sb_scrub_and_add_sb (&from_sb, from);
252b5132
RH
282 sb_index = 1;
283
284 /* These variables are reset by input_scrub_push. Restore them
285 since we are, after all, still at the same point in the file. */
286 logical_input_line = next_saved_file->logical_input_line;
287 logical_input_file = next_saved_file->logical_input_file;
288}
289
290void
b1f1fa96 291input_scrub_close (void)
252b5132
RH
292{
293 input_file_close ();
294}
295
296char *
b1f1fa96 297input_scrub_next_buffer (char **bufp)
252b5132 298{
e13b337a 299 register char *limit; /*->just after last char of buffer. */
252b5132
RH
300
301 if (sb_index >= 0)
302 {
303 if (sb_index >= from_sb.len)
304 {
305 sb_kill (&from_sb);
7152f1dc 306 if (from_sb_is_expansion
dcb87e5c 307 )
7152f1dc
KH
308 {
309 cond_finish_check (macro_nest);
9f10757c 310#ifdef md_macro_end
7152f1dc
KH
311 /* Allow the target to clean up per-macro expansion
312 data. */
313 md_macro_end ();
9f10757c 314#endif
7152f1dc
KH
315 }
316 --macro_nest;
252b5132
RH
317 partial_where = NULL;
318 if (next_saved_file != NULL)
319 *bufp = input_scrub_pop (next_saved_file);
320 return partial_where;
321 }
322
323 partial_where = from_sb.ptr + from_sb.len;
324 partial_size = 0;
325 *bufp = from_sb.ptr + sb_index;
326 sb_index = from_sb.len;
327 return partial_where;
328 }
329
330 *bufp = buffer_start + BEFORE_SIZE;
331
332 if (partial_size)
333 {
334 memcpy (buffer_start + BEFORE_SIZE, partial_where,
335 (unsigned int) partial_size);
336 memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
337 }
338 limit = input_file_give_next_buffer (buffer_start
339 + BEFORE_SIZE
340 + partial_size);
341 if (limit)
342 {
e13b337a 343 register char *p; /* Find last newline. */
252b5132
RH
344
345 for (p = limit - 1; *p != '\n'; --p)
346 ;
347 ++p;
348
349 while (p <= buffer_start + BEFORE_SIZE)
350 {
351 int limoff;
352
353 limoff = limit - buffer_start;
354 buffer_length += input_file_buffer_size ();
355 buffer_start = xrealloc (buffer_start,
356 (BEFORE_SIZE
357 + 2 * buffer_length
358 + AFTER_SIZE));
359 *bufp = buffer_start + BEFORE_SIZE;
360 limit = input_file_give_next_buffer (buffer_start + limoff);
361
362 if (limit == NULL)
363 {
364 as_warn (_("partial line at end of file ignored"));
365 partial_where = NULL;
366 if (next_saved_file)
367 *bufp = input_scrub_pop (next_saved_file);
368 return NULL;
369 }
370
371 for (p = limit - 1; *p != '\n'; --p)
372 ;
373 ++p;
374 }
375
376 partial_where = p;
377 partial_size = limit - p;
378 memcpy (save_source, partial_where, (int) AFTER_SIZE);
379 memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
380 }
381 else
382 {
383 partial_where = 0;
384 if (partial_size > 0)
385 {
0e389e77 386 as_warn (_("partial line at end of file ignored"));
252b5132
RH
387 }
388
389 /* Tell the listing we've finished the file. */
390 LISTING_EOF ();
391
e13b337a 392 /* If we should pop to another file at EOF, do it. */
252b5132
RH
393 if (next_saved_file)
394 {
395 *bufp = input_scrub_pop (next_saved_file); /* Pop state */
e13b337a 396 /* partial_where is now correct to return, since we popped it. */
252b5132
RH
397 }
398 }
399 return (partial_where);
7152f1dc 400}
252b5132 401\f
7152f1dc
KH
402/* The remaining part of this file deals with line numbers, error
403 messages and so on. Return TRUE if we opened any file. */
252b5132 404
252b5132 405int
b1f1fa96 406seen_at_least_1_file (void)
252b5132
RH
407{
408 return (physical_input_file != NULL);
409}
410
411void
b1f1fa96 412bump_line_counters (void)
252b5132
RH
413{
414 if (sb_index < 0)
415 {
416 ++physical_input_line;
417 if (logical_input_line >= 0)
418 ++logical_input_line;
419 }
420}
421\f
7152f1dc
KH
422/* Tells us what the new logical line number and file are.
423 If the line_number is -1, we don't change the current logical line
424 number. If it is -2, we decrement the logical line number (this is
425 to support the .appfile pseudo-op inserted into the stream by
426 do_scrub_chars).
427 If the fname is NULL, we don't change the current logical file name.
428 Returns nonzero if the filename actually changes. */
429
252b5132 430int
b1f1fa96
KH
431new_logical_line (char *fname, /* DON'T destroy it! We point to it! */
432 int line_number)
252b5132
RH
433{
434 if (line_number >= 0)
435 logical_input_line = line_number;
436 else if (line_number == -2 && logical_input_line > 0)
437 --logical_input_line;
438
439 if (fname
440 && (logical_input_file == NULL
441 || strcmp (logical_input_file, fname)))
442 {
443 logical_input_file = fname;
444 return 1;
445 }
446 else
447 return 0;
7152f1dc 448}
252b5132 449\f
7152f1dc
KH
450/* Return the current file name and line number.
451 namep should be char * const *, but there are compilers which screw
452 up declarations like that, and it's easier to avoid it. */
453
e13b337a 454void
b1f1fa96 455as_where (char **namep, unsigned int *linep)
252b5132
RH
456{
457 if (logical_input_file != NULL
458 && (linep == NULL || logical_input_line >= 0))
459 {
460 *namep = logical_input_file;
461 if (linep != NULL)
462 *linep = logical_input_line;
463 }
464 else if (physical_input_file != NULL)
465 {
466 *namep = physical_input_file;
467 if (linep != NULL)
468 *linep = physical_input_line;
469 }
470 else
471 {
472 *namep = 0;
473 if (linep != NULL)
474 *linep = 0;
475 }
7152f1dc 476}
This page took 0.356391 seconds and 4 git commands to generate.