keep arm dir
[deliverable/binutils-gdb.git] / gas / input-scrub.c
CommitLineData
fecd2382 1/* input_scrub.c - Break up input buffers into whole numbers of lines.
3340f7e5 2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
a39116f1
RP
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. */
fecd2382
RP
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")
f7e9bc5a 51#define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */
fecd2382
RP
52#define BEFORE_SIZE (1)
53#define AFTER_SIZE (1)
54
55static char * buffer_start; /*->1st char of full buffer area. */
56static char * partial_where; /*->after last full line in buffer. */
57static int partial_size; /* >=0. Number of chars in partial line in buffer. */
58static char save_source [AFTER_SIZE];
a39116f1
RP
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. */
fecd2382 63static int buffer_length; /* What is the largest size buffer that */
a39116f1
RP
64/* input_file_give_next_buffer() could */
65/* return to us? */
fecd2382 66
a39116f1
RP
67/* Saved information about the file that .include'd this one. When we hit EOF,
68 we automatically pop to that file. */
fecd2382
RP
69
70static char *next_saved_file;
71
a39116f1
RP
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. */
fecd2382 77
a39116f1
RP
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. */
fecd2382 82
a39116f1
RP
83char *physical_input_file;
84char *logical_input_file;
fecd2382 85
a39116f1
RP
86typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
87/* A line ends in '\n' or eof. */
fecd2382 88
a39116f1
RP
89line_numberT physical_input_line;
90line_numberT logical_input_line;
fecd2382
RP
91
92/* Struct used to save the state of the input handler during include files */
93struct 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
3340f7e5 108#if __STDC__ == 1
fecd2382
RP
109static void as_1_char(unsigned int c, FILE *stream);
110#else /* __STDC__ */
111static void as_1_char();
3340f7e5 112#endif /* not __STDC__ */
fecd2382
RP
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(). */
a39116f1
RP
117char *input_scrub_push(saved_position)
118char *saved_position;
fecd2382
RP
119{
120 register struct input_save *saved;
a39116f1 121
fecd2382 122 saved = (struct input_save *) xmalloc(sizeof *saved);
a39116f1 123
fecd2382
RP
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;
f7e9bc5a 133 memcpy(saved->save_source, save_source, sizeof(save_source));
fecd2382 134 saved->next_saved_file = next_saved_file;
a39116f1
RP
135 saved->input_file_save = input_file_push();
136
b1520b1f
ILT
137 input_file_begin(); /* Reinitialize! */
138 logical_input_line = 0;
139 logical_input_file = (char *)NULL;
a39116f1
RP
140
141 return((char *) saved);
142} /* input_scrub_push() */
fecd2382
RP
143
144char *
a39116f1
RP
145 input_scrub_pop (arg)
146char *arg;
fecd2382
RP
147{
148 register struct input_save *saved;
149 char *saved_position;
a39116f1 150
fecd2382 151 input_scrub_end (); /* Finish off old buffer */
a39116f1 152
fecd2382 153 saved = (struct input_save *)arg;
a39116f1 154
fecd2382
RP
155 input_file_pop (saved->input_file_save);
156 saved_position = saved->saved_position;
157 buffer_start = saved->buffer_start;
158 buffer_length = saved->buffer_length;
159 physical_input_file = saved->physical_input_file;
160 logical_input_file = saved->logical_input_file;
161 physical_input_line = saved->physical_input_line;
162 logical_input_line = saved->logical_input_line;
163 partial_where = saved->partial_where;
164 partial_size = saved->partial_size;
165 next_saved_file = saved->next_saved_file;
f7e9bc5a 166 memcpy(save_source, saved->save_source, sizeof (save_source));
a39116f1 167
fecd2382
RP
168 free(arg);
169 return saved_position;
170}
171
172\f
173void
a39116f1 174 input_scrub_begin ()
fecd2382 175{
a39116f1
RP
176 know(strlen(BEFORE_STRING) == BEFORE_SIZE);
177 know(strlen(AFTER_STRING) == AFTER_SIZE || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
178
179 input_file_begin ();
180
181 buffer_length = input_file_buffer_size ();
182
183 buffer_start = xmalloc((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
f7e9bc5a 184 memcpy(buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
a39116f1
RP
185
186 /* Line number things. */
187 logical_input_line = 0;
188 logical_input_file = (char *)NULL;
189 physical_input_file = NULL; /* No file read yet. */
190 next_saved_file = NULL; /* At EOF, don't pop to any other file */
191 do_scrub_begin();
fecd2382
RP
192}
193
194void
a39116f1 195 input_scrub_end ()
fecd2382 196{
a39116f1
RP
197 if (buffer_start)
198 {
199 free (buffer_start);
200 buffer_start = 0;
201 input_file_end ();
202 }
fecd2382
RP
203}
204
205/* Start reading input from a new file. */
206
207char * /* Return start of caller's part of buffer. */
a39116f1
RP
208 input_scrub_new_file (filename)
209char * filename;
fecd2382 210{
a39116f1
RP
211 input_file_open (filename, !flagseen['f']);
212 physical_input_file = filename[0] ? filename : "{standard input}";
213 physical_input_line = 0;
214
215 partial_size = 0;
216 return (buffer_start + BEFORE_SIZE);
fecd2382
RP
217}
218
219
220/* Include a file from the current file. Save our state, cause it to
221 be restored on EOF, and begin handling a new file. Same result as
222 input_scrub_new_file. */
223
224char *
a39116f1
RP
225 input_scrub_include_file (filename, position)
226char *filename;
227char *position;
fecd2382 228{
a39116f1
RP
229 next_saved_file = input_scrub_push(position);
230 return input_scrub_new_file (filename);
fecd2382
RP
231}
232
233void
a39116f1 234 input_scrub_close ()
fecd2382 235{
a39116f1 236 input_file_close ();
fecd2382
RP
237}
238char *
a39116f1 239 input_scrub_next_buffer (bufp)
fecd2382
RP
240char **bufp;
241{
a39116f1
RP
242 register char * limit; /*->just after last char of buffer. */
243
244 *bufp = buffer_start + BEFORE_SIZE;
245
fecd2382 246#ifdef DONTDEF
a39116f1
RP
247 if(preprocess) {
248 if(save_buffer) {
249 *bufp = save_buffer;
250 save_buffer = 0;
251 }
252 limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE);
253 if (!limit) {
254 partial_where = 0;
255 if(partial_size)
256 as_warn("Partial line at end of file ignored");
257 return partial_where;
258 }
259
260 if(partial_size)
f7e9bc5a 261 memcpy(partial_where, save_source, (int) AFTER_SIZE);
a39116f1
RP
262 do_scrub(partial_where,partial_size,buffer_start+BEFORE_SIZE,limit-(buffer_start+BEFORE_SIZE),&out_string,&out_length);
263 limit=out_string + out_length;
264 for(p=limit;*--p!='\n';)
265 ;
266 p++;
267 if(p<=buffer_start+BEFORE_SIZE)
268 as_fatal("Source line too long. Please change file '%s' and re-make the assembler.", __FILE__);
269
270 partial_where = p;
271 partial_size = limit-p;
f7e9bc5a
SC
272 memcpy(save_source, partial_where, (int) AFTER_SIZE);
273 memcpy(partial_where, AFTER_STRING, (int) AFTER_SIZE);
a39116f1
RP
274
275 save_buffer = *bufp;
276 *bufp = out_string;
277
278 return partial_where;
fecd2382 279 }
a39116f1
RP
280
281 /* We're not preprocessing. Do the right thing */
282#endif
f7e9bc5a
SC
283 if (partial_size) {
284 memcpy(buffer_start + BEFORE_SIZE, partial_where, (int) partial_size);
285 memcpy(buffer_start + BEFORE_SIZE, save_source, (int) AFTER_SIZE);
286 }
a39116f1 287 limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
f7e9bc5a
SC
288 if (limit) {
289 register char * p; /* Find last newline. */
290
291 for (p = limit; *--p != '\n';) ;;
292 ++p;
293 if (p <= buffer_start + BEFORE_SIZE) {
294 as_fatal("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
295 }
296 partial_where = p;
297 partial_size = limit - p;
298 memcpy(save_source, partial_where, (int) AFTER_SIZE);
299 memcpy(partial_where, AFTER_STRING, (int) AFTER_SIZE);
300 } else {
301 partial_where = 0;
302 if (partial_size > 0) {
303 as_warn("Partial line at end of file ignored");
304 }
305 /* If we should pop to another file at EOF, do it. */
306 if (next_saved_file) {
307 *bufp = input_scrub_pop (next_saved_file); /* Pop state */
308 /* partial_where is now correct to return, since we popped it. */
309 }
310 }
311 return(partial_where);
312} /* input_scrub_next_buffer() */
fecd2382
RP
313\f
314/*
315 * The remaining part of this file deals with line numbers, error
316 * messages and so on.
317 */
318
319
320int
a39116f1 321 seen_at_least_1_file () /* TRUE if we opened any file. */
fecd2382 322{
a39116f1 323 return (physical_input_file != NULL);
fecd2382
RP
324}
325
326void
a39116f1 327 bump_line_counters ()
fecd2382 328{
a39116f1
RP
329 ++ physical_input_line;
330 /* ++ logical_input_line; FIXME-now remove this. */
fecd2382
RP
331}
332\f
333/*
334 * new_logical_line()
335 *
336 * Tells us what the new logical line number and file are.
337 * If the line_number is <0, we don't change the current logical line number.
338 * If the fname is NULL, we don't change the current logical file name.
339 */
340void new_logical_line(fname, line_number)
a39116f1
RP
341char *fname; /* DON'T destroy it! We point to it! */
342int line_number;
fecd2382
RP
343{
344 if (fname) {
345 logical_input_file = fname;
346 } /* if we have a file name */
347
348 if (line_number >= 0) {
349 logical_input_line = line_number;
350 } /* if we have a line number */
351} /* new_logical_line() */
352\f
353/*
354 * a s _ w h e r e ()
355 *
356 * Write a line to stderr locating where we are in reading
357 * input source files.
358 * As a sop to the debugger of AS, pretty-print the offending line.
359 */
a39116f1
RP
360void as_where() {
361 char *p;
362 line_numberT line;
363
364 if (logical_input_file && (logical_input_line > 0)) {
365 p = logical_input_file;
366 line = logical_input_line;
367 } else {
368 p = physical_input_file;
369 line = physical_input_line;
370 } /* line number should match file name */
371
372 fprintf(stderr, "%s:%u: ", p, line);
373
fecd2382 374#ifdef DONTDEF
a39116f1
RP
375 if (physical_input_file) {
376 if (input_file_is_open()) { /* we can still read lines from source */
377 fprintf (stderr," @ physical line %ld., file \"%s\"",
378 (long) physical_input_line, physical_input_file);
379 fprintf (stderr," @ logical line %ld., file \"%s\"\n",
380 (long) logical_input_line, logical_input_file);
381 (void)putc(' ', stderr);
382 as_howmuch (stderr);
383 (void)putc('\n', stderr);
384 } else {
385 fprintf(stderr, " After reading source.\n");
386 } /* input file is still open */
387 } else {
388 fprintf(stderr, " Before reading source.\n");
389 } /* we tried to read SOME source */
390#endif /* DONTDEF */
391
392 return;
393} /* as_where() */
fecd2382
RP
394
395
396
397\f
398/*
399 * a s _ h o w m u c h ()
400 *
401 * Output to given stream how much of line we have scanned so far.
402 * Assumes we have scanned up to and including input_line_pointer.
403 * No free '\n' at end of line.
404 */
405void
a39116f1
RP
406 as_howmuch (stream)
407FILE * stream; /* Opened for write please. */
fecd2382 408{
a39116f1
RP
409 register char * p; /* Scan input line. */
410 /* register char c; JF unused */
411
412 for (p = input_line_pointer - 1; * p != '\n'; --p)
413 {
414 }
415 ++ p; /* p->1st char of line. */
416 for (; p <= input_line_pointer; p++)
417 {
418 /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
419 /* c = *p & 0xFF; JF unused */
420 as_1_char(*p, stream);
421 }
fecd2382
RP
422}
423
424static void as_1_char (c,stream)
425unsigned int c;
426FILE *stream;
427{
a39116f1
RP
428 if (c > 127)
429 {
430 (void)putc('%', stream);
431 c -= 128;
432 }
433 if (c < 32)
434 {
435 (void)putc('^', stream);
436 c += '@';
437 }
438 (void)putc(c, stream);
fecd2382
RP
439}
440
441/*
442 * Local Variables:
443 * comment-column: 0
444 * fill-column: 131
445 * End:
446 */
447
8b228fe9 448/* end of input_scrub.c */
This page took 0.137829 seconds and 4 git commands to generate.