Commit | Line | Data |
---|---|---|
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. |
7e047ac2 | 3 | |
a39116f1 | 4 | This file is part of GAS, the GNU Assembler. |
7e047ac2 | 5 | |
a39116f1 RP |
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. | |
7e047ac2 | 10 | |
a39116f1 RP |
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. | |
7e047ac2 | 15 | |
a39116f1 RP |
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 | |
7e047ac2 | 18 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
fecd2382 | 19 | |
7e047ac2 | 20 | #include <errno.h> /* Need this to make errno declaration right */ |
fecd2382 RP |
21 | #include "as.h" |
22 | #include "input-file.h" | |
7e047ac2 | 23 | #include "sb.h" |
fecd2382 RP |
24 | |
25 | /* | |
26 | * O/S independent module to supply buffers of sanitised source code | |
7e047ac2 | 27 | * to rest of assembler. We get sanitised input data of arbitrary length. |
fecd2382 RP |
28 | * We break these buffers on line boundaries, recombine pieces that |
29 | * were broken across buffers, and return a buffer of full lines to | |
30 | * the caller. | |
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 | |
34 | * is a newline. | |
35 | * Also looks after line numbers, for e.g. error messages. | |
36 | */ | |
37 | ||
38 | /* | |
39 | * We don't care how filthy our buffers are, but our callers assume | |
40 | * that the following sanitation has already been done. | |
41 | * | |
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. | |
49 | */ | |
50 | \f | |
51 | #define BEFORE_STRING ("\n") | |
f7e9bc5a | 52 | #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */ |
fecd2382 RP |
53 | #define BEFORE_SIZE (1) |
54 | #define AFTER_SIZE (1) | |
55 | ||
7e047ac2 ILT |
56 | static char *buffer_start; /*->1st char of full buffer area. */ |
57 | static char *partial_where; /*->after last full line in buffer. */ | |
fecd2382 | 58 | static int partial_size; /* >=0. Number of chars in partial line in buffer. */ |
7e047ac2 | 59 | static char save_source[AFTER_SIZE]; |
a39116f1 RP |
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 */ | |
63 | /* line here. */ | |
7e047ac2 | 64 | static unsigned int buffer_length; /* What is the largest size buffer that */ |
a39116f1 RP |
65 | /* input_file_give_next_buffer() could */ |
66 | /* return to us? */ | |
fecd2382 | 67 | |
7e047ac2 ILT |
68 | /* The index into an sb structure we are reading from. -1 if none. */ |
69 | static int sb_index = -1; | |
70 | ||
71 | /* If we are reading from an sb structure, this is it. */ | |
72 | static sb from_sb; | |
fecd2382 | 73 | |
7e047ac2 ILT |
74 | /* The number of nested sb structures we have included. */ |
75 | static int macro_nest; | |
fecd2382 | 76 | |
a39116f1 RP |
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 | |
81 | assembly. */ | |
fecd2382 | 82 | |
a39116f1 RP |
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. */ | |
fecd2382 | 87 | |
7e047ac2 ILT |
88 | static char *physical_input_file; |
89 | static char *logical_input_file; | |
fecd2382 | 90 | |
7e047ac2 | 91 | typedef unsigned int line_numberT; /* 1-origin line number in a source file. */ |
a39116f1 | 92 | /* A line ends in '\n' or eof. */ |
fecd2382 | 93 | |
7e047ac2 ILT |
94 | static line_numberT physical_input_line; |
95 | static int logical_input_line; | |
fecd2382 RP |
96 | |
97 | /* Struct used to save the state of the input handler during include files */ | |
7e047ac2 ILT |
98 | struct input_save |
99 | { | |
100 | char *buffer_start; | |
101 | char *partial_where; | |
102 | int partial_size; | |
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; | |
109 | int sb_index; | |
110 | sb from_sb; | |
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 */ | |
114 | }; | |
115 | ||
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)); | |
119 | ||
120 | /* Saved information about the file that .include'd this one. When we hit EOF, | |
121 | we automatically pop to that file. */ | |
122 | ||
123 | static struct input_save *next_saved_file; | |
fecd2382 RP |
124 | |
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(). */ | |
7e047ac2 ILT |
128 | static struct input_save * |
129 | input_scrub_push (saved_position) | |
130 | char *saved_position; | |
fecd2382 | 131 | { |
7e047ac2 ILT |
132 | register struct input_save *saved; |
133 | ||
134 | saved = (struct input_save *) xmalloc (sizeof *saved); | |
135 | ||
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 (); | |
150 | ||
151 | input_file_begin (); /* Reinitialize! */ | |
152 | logical_input_line = -1; | |
153 | logical_input_file = (char *) NULL; | |
154 | buffer_length = input_file_buffer_size (); | |
155 | ||
156 | buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE)); | |
157 | memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE); | |
158 | ||
159 | return saved; | |
160 | } /* input_scrub_push() */ | |
161 | ||
162 | static char * | |
163 | input_scrub_pop (saved) | |
164 | struct input_save *saved; | |
fecd2382 | 165 | { |
7e047ac2 ILT |
166 | char *saved_position; |
167 | ||
168 | input_scrub_end (); /* Finish off old buffer */ | |
169 | ||
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)); | |
184 | ||
185 | free (saved); | |
186 | return saved_position; | |
fecd2382 | 187 | } |
fecd2382 | 188 | \f |
7e047ac2 | 189 | |
fecd2382 | 190 | void |
7e047ac2 | 191 | input_scrub_begin () |
fecd2382 | 192 | { |
7e047ac2 ILT |
193 | know (strlen (BEFORE_STRING) == BEFORE_SIZE); |
194 | know (strlen (AFTER_STRING) == AFTER_SIZE || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1)); | |
195 | ||
196 | input_file_begin (); | |
197 | ||
198 | buffer_length = input_file_buffer_size (); | |
199 | ||
200 | buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE)); | |
201 | memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE); | |
202 | ||
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 */ | |
208 | do_scrub_begin (); | |
fecd2382 RP |
209 | } |
210 | ||
211 | void | |
7e047ac2 | 212 | input_scrub_end () |
fecd2382 | 213 | { |
7e047ac2 ILT |
214 | if (buffer_start) |
215 | { | |
216 | free (buffer_start); | |
217 | buffer_start = 0; | |
218 | input_file_end (); | |
219 | } | |
fecd2382 RP |
220 | } |
221 | ||
222 | /* Start reading input from a new file. */ | |
223 | ||
224 | char * /* Return start of caller's part of buffer. */ | |
7e047ac2 ILT |
225 | input_scrub_new_file (filename) |
226 | char *filename; | |
fecd2382 | 227 | { |
7e047ac2 ILT |
228 | input_file_open (filename, !flag_no_comments); |
229 | physical_input_file = filename[0] ? filename : "{standard input}"; | |
230 | physical_input_line = 0; | |
231 | ||
232 | partial_size = 0; | |
233 | return (buffer_start + BEFORE_SIZE); | |
fecd2382 RP |
234 | } |
235 | ||
236 | ||
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. */ | |
240 | ||
241 | char * | |
7e047ac2 ILT |
242 | input_scrub_include_file (filename, position) |
243 | char *filename; | |
244 | char *position; | |
fecd2382 | 245 | { |
7e047ac2 ILT |
246 | next_saved_file = input_scrub_push (position); |
247 | return input_scrub_new_file (filename); | |
fecd2382 RP |
248 | } |
249 | ||
7e047ac2 ILT |
250 | /* Start getting input from an sb structure. This is used when |
251 | expanding a macro. */ | |
252 | ||
fecd2382 | 253 | void |
7e047ac2 ILT |
254 | input_scrub_include_sb (from, position) |
255 | sb *from; | |
256 | char *position; | |
fecd2382 | 257 | { |
7e047ac2 ILT |
258 | if (macro_nest > max_macro_nest) |
259 | as_fatal ("macros nested too deeply"); | |
260 | ++macro_nest; | |
261 | ||
262 | next_saved_file = input_scrub_push (position); | |
263 | ||
264 | sb_new (&from_sb); | |
265 | /* Add the sentinel required by read.c. */ | |
266 | sb_add_char (&from_sb, '\n'); | |
267 | sb_add_sb (&from_sb, from); | |
268 | sb_index = 1; | |
269 | ||
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; | |
fecd2382 | 274 | } |
7e047ac2 ILT |
275 | |
276 | void | |
277 | input_scrub_close () | |
278 | { | |
279 | input_file_close (); | |
280 | } | |
281 | ||
fecd2382 | 282 | char * |
7e047ac2 ILT |
283 | input_scrub_next_buffer (bufp) |
284 | char **bufp; | |
fecd2382 | 285 | { |
7e047ac2 ILT |
286 | register char *limit; /*->just after last char of buffer. */ |
287 | ||
288 | if (sb_index >= 0) | |
289 | { | |
290 | if (sb_index >= from_sb.len) | |
291 | { | |
292 | sb_kill (&from_sb); | |
293 | --macro_nest; | |
294 | partial_where = NULL; | |
295 | if (next_saved_file != NULL) | |
296 | *bufp = input_scrub_pop (next_saved_file); | |
297 | return partial_where; | |
fecd2382 | 298 | } |
7e047ac2 ILT |
299 | |
300 | partial_where = from_sb.ptr + from_sb.len; | |
301 | partial_size = 0; | |
302 | *bufp = from_sb.ptr + sb_index; | |
303 | sb_index = from_sb.len; | |
304 | return partial_where; | |
305 | } | |
306 | ||
307 | *bufp = buffer_start + BEFORE_SIZE; | |
308 | ||
309 | if (partial_size) | |
310 | { | |
311 | memcpy (buffer_start + BEFORE_SIZE, partial_where, | |
312 | (unsigned int) partial_size); | |
313 | memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE); | |
314 | } | |
315 | limit = input_file_give_next_buffer (buffer_start | |
316 | + BEFORE_SIZE | |
317 | + partial_size); | |
318 | if (limit) | |
319 | { | |
320 | register char *p; /* Find last newline. */ | |
321 | ||
322 | for (p = limit; *--p != '\n';);; | |
323 | ++p; | |
324 | if (p <= buffer_start + BEFORE_SIZE) | |
325 | { | |
326 | as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__); | |
327 | } | |
328 | partial_where = p; | |
329 | partial_size = limit - p; | |
330 | memcpy (save_source, partial_where, (int) AFTER_SIZE); | |
331 | memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE); | |
332 | } | |
333 | else | |
334 | { | |
335 | partial_where = 0; | |
336 | if (partial_size > 0) | |
337 | { | |
338 | as_warn ("Partial line at end of file ignored"); | |
f7e9bc5a | 339 | } |
7e047ac2 ILT |
340 | /* If we should pop to another file at EOF, do it. */ |
341 | if (next_saved_file) | |
342 | { | |
343 | *bufp = input_scrub_pop (next_saved_file); /* Pop state */ | |
344 | /* partial_where is now correct to return, since we popped it. */ | |
f7e9bc5a | 345 | } |
7e047ac2 ILT |
346 | } |
347 | return (partial_where); | |
348 | } /* input_scrub_next_buffer() */ | |
fecd2382 RP |
349 | \f |
350 | /* | |
351 | * The remaining part of this file deals with line numbers, error | |
352 | * messages and so on. | |
353 | */ | |
354 | ||
355 | ||
356 | int | |
7e047ac2 | 357 | seen_at_least_1_file () /* TRUE if we opened any file. */ |
fecd2382 | 358 | { |
7e047ac2 | 359 | return (physical_input_file != NULL); |
fecd2382 RP |
360 | } |
361 | ||
362 | void | |
7e047ac2 | 363 | bump_line_counters () |
fecd2382 | 364 | { |
7e047ac2 ILT |
365 | if (sb_index < 0) |
366 | { | |
367 | ++physical_input_line; | |
368 | if (logical_input_line >= 0) | |
369 | ++logical_input_line; | |
370 | } | |
fecd2382 RP |
371 | } |
372 | \f | |
373 | /* | |
374 | * new_logical_line() | |
375 | * | |
376 | * Tells us what the new logical line number and file are. | |
7e047ac2 ILT |
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 | |
2000c643 | 380 | * do_scrub_chars). |
fecd2382 RP |
381 | * If the fname is NULL, we don't change the current logical file name. |
382 | */ | |
7e047ac2 ILT |
383 | void |
384 | new_logical_line (fname, line_number) | |
385 | char *fname; /* DON'T destroy it! We point to it! */ | |
386 | int line_number; | |
fecd2382 | 387 | { |
7e047ac2 ILT |
388 | if (fname) |
389 | { | |
390 | logical_input_file = fname; | |
391 | } /* if we have a file name */ | |
392 | ||
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() */ | |
fecd2382 RP |
398 | \f |
399 | /* | |
400 | * a s _ w h e r e () | |
401 | * | |
7e047ac2 ILT |
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. | |
fecd2382 | 405 | */ |
7e047ac2 ILT |
406 | void |
407 | as_where (namep, linep) | |
408 | char **namep; | |
409 | unsigned int *linep; | |
410 | { | |
411 | if (logical_input_file != NULL | |
412 | && (linep == NULL || logical_input_line >= 0)) | |
413 | { | |
414 | *namep = logical_input_file; | |
415 | if (linep != NULL) | |
416 | *linep = logical_input_line; | |
417 | } | |
418 | else if (physical_input_file != NULL) | |
419 | { | |
420 | *namep = physical_input_file; | |
421 | if (linep != NULL) | |
422 | *linep = physical_input_line; | |
423 | } | |
424 | else | |
425 | { | |
2000c643 | 426 | *namep = 0; |
7e047ac2 ILT |
427 | if (linep != NULL) |
428 | *linep = 0; | |
429 | } | |
430 | } /* as_where() */ | |
431 | \f | |
fecd2382 RP |
432 | |
433 | ||
434 | ||
fecd2382 RP |
435 | /* |
436 | * a s _ h o w m u c h () | |
437 | * | |
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. | |
441 | */ | |
442 | void | |
7e047ac2 ILT |
443 | as_howmuch (stream) |
444 | FILE *stream; /* Opened for write please. */ | |
fecd2382 | 445 | { |
7e047ac2 ILT |
446 | register char *p; /* Scan input line. */ |
447 | /* register char c; JF unused */ | |
448 | ||
449 | for (p = input_line_pointer - 1; *p != '\n'; --p) | |
450 | { | |
451 | } | |
452 | ++p; /* p->1st char of line. */ | |
453 | for (; p <= input_line_pointer; p++) | |
454 | { | |
455 | /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */ | |
7e047ac2 ILT |
456 | as_1_char ((unsigned char) *p, stream); |
457 | } | |
fecd2382 RP |
458 | } |
459 | ||
7e047ac2 ILT |
460 | static void |
461 | as_1_char (c, stream) | |
462 | unsigned int c; | |
463 | FILE *stream; | |
fecd2382 | 464 | { |
7e047ac2 ILT |
465 | if (c > 127) |
466 | { | |
467 | (void) putc ('%', stream); | |
468 | c -= 128; | |
469 | } | |
470 | if (c < 32) | |
471 | { | |
472 | (void) putc ('^', stream); | |
473 | c += '@'; | |
474 | } | |
475 | (void) putc (c, stream); | |
fecd2382 RP |
476 | } |
477 | ||
8b228fe9 | 478 | /* end of input_scrub.c */ |