2bc2b4a4865290407712061b1c2a6549513907c1
[platform/upstream/binutils.git] / gas / input-scrub.c
1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2    Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA. */
21
22 #include <errno.h>              /* Need this to make errno declaration right */
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.
35  * The buffer returned to caller is preceeded by BEFORE_STRING and followed
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")
55 #define AFTER_STRING ("\0")     /* memcpy of 0 chars might choke. */
56 #define BEFORE_SIZE (1)
57 #define AFTER_SIZE  (1)
58
59 static char *buffer_start;      /*->1st char of full buffer area. */
60 static char *partial_where;     /*->after last full line in buffer. */
61 static int partial_size;        /* >=0. Number of chars in partial line in buffer. */
62 static char save_source[AFTER_SIZE];
63 /* Because we need AFTER_STRING just after last */
64 /* full line, it clobbers 1st part of partial */
65 /* line. So we preserve 1st part of partial */
66 /* line here. */
67 static unsigned int buffer_length;      /* What is the largest size buffer that */
68 /* input_file_give_next_buffer() could */
69 /* return to us? */
70
71 /* The index into an sb structure we are reading from.  -1 if none.  */
72 static int sb_index = -1;
73
74 /* If we are reading from an sb structure, this is it.  */
75 static sb from_sb;
76
77 /* Should we do a conditional check on from_sb? */
78 static int from_sb_is_expansion = 1;
79
80 /* The number of nested sb structures we have included.  */
81 int macro_nest;
82
83 /* We can have more than one source file open at once, though the info for all
84    but the latest one are saved off in a struct input_save.  These files remain
85    open, so we are limited by the number of open files allowed by the
86    underlying OS. We may also sequentially read more than one source file in an
87    assembly. */
88
89 /* We must track the physical file and line number for error messages. We also
90    track a "logical" file and line number corresponding to (C?)  compiler
91    source line numbers.  Whenever we open a file we must fill in
92    physical_input_file. So if it is NULL we have not opened any files yet. */
93
94 static char *physical_input_file;
95 static char *logical_input_file;
96
97 typedef unsigned int line_numberT;      /* 1-origin line number in a source file. */
98 /* A line ends in '\n' or eof. */
99
100 static line_numberT physical_input_line;
101 static int logical_input_line;
102
103 /* Struct used to save the state of the input handler during include files */
104 struct input_save
105   {
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 */
121   };
122
123 static struct input_save *input_scrub_push PARAMS ((char *saved_position));
124 static char *input_scrub_pop PARAMS ((struct input_save *arg));
125 static void as_1_char PARAMS ((unsigned int c, FILE * stream));
126
127 /* Saved information about the file that .include'd this one.  When we hit EOF,
128    we automatically pop to that file. */
129
130 static struct input_save *next_saved_file;
131
132 /* Push the state of input reading and scrubbing so that we can #include.
133    The return value is a 'void *' (fudged for old compilers) to a save
134    area, which can be restored by passing it to input_scrub_pop(). */
135 static struct input_save *
136 input_scrub_push (saved_position)
137      char *saved_position;
138 {
139   register struct input_save *saved;
140
141   saved = (struct input_save *) xmalloc (sizeof *saved);
142
143   saved->saved_position = saved_position;
144   saved->buffer_start = buffer_start;
145   saved->partial_where = partial_where;
146   saved->partial_size = partial_size;
147   saved->buffer_length = buffer_length;
148   saved->physical_input_file = physical_input_file;
149   saved->logical_input_file = logical_input_file;
150   saved->physical_input_line = physical_input_line;
151   saved->logical_input_line = logical_input_line;
152   saved->sb_index = sb_index;
153   saved->from_sb = from_sb;
154   saved->from_sb_is_expansion = from_sb_is_expansion;
155   memcpy (saved->save_source, save_source, sizeof (save_source));
156   saved->next_saved_file = next_saved_file;
157   saved->input_file_save = input_file_push ();
158
159   input_file_begin ();          /* Reinitialize! */
160   logical_input_line = -1;
161   logical_input_file = (char *) NULL;
162   buffer_length = input_file_buffer_size ();
163   sb_index = -1;
164
165   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
166   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
167
168   return saved;
169 }                               /* input_scrub_push() */
170
171 static char *
172 input_scrub_pop (saved)
173      struct input_save *saved;
174 {
175   char *saved_position;
176
177   input_scrub_end ();           /* Finish off old buffer */
178
179   input_file_pop (saved->input_file_save);
180   saved_position = saved->saved_position;
181   buffer_start = saved->buffer_start;
182   buffer_length = saved->buffer_length;
183   physical_input_file = saved->physical_input_file;
184   logical_input_file = saved->logical_input_file;
185   physical_input_line = saved->physical_input_line;
186   logical_input_line = saved->logical_input_line;
187   sb_index = saved->sb_index;
188   from_sb = saved->from_sb;
189   from_sb_is_expansion = saved->from_sb_is_expansion;
190   partial_where = saved->partial_where;
191   partial_size = saved->partial_size;
192   next_saved_file = saved->next_saved_file;
193   memcpy (save_source, saved->save_source, sizeof (save_source));
194
195   free (saved);
196   return saved_position;
197 }
198 \f
199
200 void
201 input_scrub_begin ()
202 {
203   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
204   know (strlen (AFTER_STRING) == AFTER_SIZE || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
205
206   input_file_begin ();
207
208   buffer_length = input_file_buffer_size ();
209
210   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
211   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
212
213   /* Line number things. */
214   logical_input_line = -1;
215   logical_input_file = (char *) NULL;
216   physical_input_file = NULL;   /* No file read yet. */
217   next_saved_file = NULL;       /* At EOF, don't pop to any other file */
218   do_scrub_begin (flag_m68k_mri);
219 }
220
221 void
222 input_scrub_end ()
223 {
224   if (buffer_start)
225     {
226       free (buffer_start);
227       buffer_start = 0;
228       input_file_end ();
229     }
230 }
231
232 /* Start reading input from a new file. */
233
234 char *                          /* Return start of caller's part of buffer. */
235 input_scrub_new_file (filename)
236      char *filename;
237 {
238   input_file_open (filename, !flag_no_comments);
239   physical_input_file = filename[0] ? filename : _("{standard input}");
240   physical_input_line = 0;
241
242   partial_size = 0;
243   return (buffer_start + BEFORE_SIZE);
244 }
245
246
247 /* Include a file from the current file.  Save our state, cause it to
248    be restored on EOF, and begin handling a new file.  Same result as
249    input_scrub_new_file. */
250
251 char *
252 input_scrub_include_file (filename, position)
253      char *filename;
254      char *position;
255 {
256   next_saved_file = input_scrub_push (position);
257   return input_scrub_new_file (filename);
258 }
259
260 /* Start getting input from an sb structure.  This is used when
261    expanding a macro.  */
262
263 void
264 input_scrub_include_sb (from, position, is_expansion)
265      sb *from;
266      char *position;
267      int is_expansion;
268 {
269   if (macro_nest > max_macro_nest)
270     as_fatal (_("macros nested too deeply"));
271   ++macro_nest;
272
273 #ifdef md_macro_start
274   if (is_expansion)
275     {
276       md_macro_start ();
277     }
278 #endif
279
280   next_saved_file = input_scrub_push (position);
281
282   sb_new (&from_sb);
283   from_sb_is_expansion = is_expansion;
284   if (from->len >= 1 && from->ptr[0] != '\n')
285     {
286       /* Add the sentinel required by read.c.  */
287       sb_add_char (&from_sb, '\n');
288     }
289   sb_add_sb (&from_sb, from);
290   sb_index = 1;
291
292   /* These variables are reset by input_scrub_push.  Restore them
293      since we are, after all, still at the same point in the file.  */
294   logical_input_line = next_saved_file->logical_input_line;
295   logical_input_file = next_saved_file->logical_input_file;
296 }
297
298 void
299 input_scrub_close ()
300 {
301   input_file_close ();
302 }
303
304 char *
305 input_scrub_next_buffer (bufp)
306      char **bufp;
307 {
308   register char *limit;         /*->just after last char of buffer. */
309
310   if (sb_index >= 0)
311     {
312       if (sb_index >= from_sb.len)
313         {
314           sb_kill (&from_sb);
315           if (from_sb_is_expansion
316               )
317             {
318               cond_finish_check (macro_nest);
319 #ifdef md_macro_end
320               /* allow the target to clean up per-macro expansion data */
321               md_macro_end ();
322 #endif
323             }
324           --macro_nest;
325           partial_where = NULL;
326           if (next_saved_file != NULL)
327             *bufp = input_scrub_pop (next_saved_file);
328           return partial_where;
329         }
330
331       partial_where = from_sb.ptr + from_sb.len;
332       partial_size = 0;
333       *bufp = from_sb.ptr + sb_index;
334       sb_index = from_sb.len;
335       return partial_where;
336     }
337
338   *bufp = buffer_start + BEFORE_SIZE;
339
340   if (partial_size)
341     {
342       memcpy (buffer_start + BEFORE_SIZE, partial_where,
343               (unsigned int) partial_size);
344       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
345     }
346   limit = input_file_give_next_buffer (buffer_start
347                                        + BEFORE_SIZE
348                                        + partial_size);
349   if (limit)
350     {
351       register char *p;         /* Find last newline. */
352
353       for (p = limit - 1; *p != '\n'; --p)
354         ;
355       ++p;
356
357       while (p <= buffer_start + BEFORE_SIZE)
358         {
359           int limoff;
360
361           limoff = limit - buffer_start;
362           buffer_length += input_file_buffer_size ();
363           buffer_start = xrealloc (buffer_start,
364                                    (BEFORE_SIZE
365                                     + 2 * buffer_length
366                                     + AFTER_SIZE));
367           *bufp = buffer_start + BEFORE_SIZE;
368           limit = input_file_give_next_buffer (buffer_start + limoff);
369
370           if (limit == NULL)
371             {
372               as_warn (_("partial line at end of file ignored"));
373               partial_where = NULL;
374               if (next_saved_file)
375                 *bufp = input_scrub_pop (next_saved_file);
376               return NULL;
377             }
378
379           for (p = limit - 1; *p != '\n'; --p)
380             ;
381           ++p;
382         }
383
384       partial_where = p;
385       partial_size = limit - p;
386       memcpy (save_source, partial_where, (int) AFTER_SIZE);
387       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
388     }
389   else
390     {
391       partial_where = 0;
392       if (partial_size > 0)
393         {
394           as_warn (_("Partial line at end of file ignored"));
395         }
396
397       /* Tell the listing we've finished the file.  */
398       LISTING_EOF ();
399
400       /* If we should pop to another file at EOF, do it. */
401       if (next_saved_file)
402         {
403           *bufp = input_scrub_pop (next_saved_file);    /* Pop state */
404           /* partial_where is now correct to return, since we popped it. */
405         }
406     }
407   return (partial_where);
408 }                               /* input_scrub_next_buffer() */
409 \f
410 /*
411  * The remaining part of this file deals with line numbers, error
412  * messages and so on.
413  */
414
415
416 int
417 seen_at_least_1_file ()         /* TRUE if we opened any file. */
418 {
419   return (physical_input_file != NULL);
420 }
421
422 void
423 bump_line_counters ()
424 {
425   if (sb_index < 0)
426     {
427       ++physical_input_line;
428       if (logical_input_line >= 0)
429         ++logical_input_line;
430     }
431 }
432 \f
433 /*
434  *                      new_logical_line()
435  *
436  * Tells us what the new logical line number and file are.
437  * If the line_number is -1, we don't change the current logical line
438  * number.  If it is -2, we decrement the logical line number (this is
439  * to support the .appfile pseudo-op inserted into the stream by
440  * do_scrub_chars).
441  * If the fname is NULL, we don't change the current logical file name.
442  * Returns nonzero if the filename actually changes.
443  */
444 int
445 new_logical_line (fname, line_number)
446      char *fname;               /* DON'T destroy it! We point to it! */
447      int line_number;
448 {
449   if (line_number >= 0)
450     logical_input_line = line_number;
451   else if (line_number == -2 && logical_input_line > 0)
452     --logical_input_line;
453
454   if (fname
455       && (logical_input_file == NULL
456           || strcmp (logical_input_file, fname)))
457     {
458       logical_input_file = fname;
459       return 1;
460     }
461   else
462     return 0;
463 }                               /* new_logical_line() */
464 \f
465 /*
466  *                      a s _ w h e r e ()
467  *
468  * Return the current file name and line number.
469  * namep should be char * const *, but there are compilers which screw
470  * up declarations like that, and it's easier to avoid it.
471  */
472 void 
473 as_where (namep, linep)
474      char **namep;
475      unsigned int *linep;
476 {
477   if (logical_input_file != NULL
478       && (linep == NULL || logical_input_line >= 0))
479     {
480       *namep = logical_input_file;
481       if (linep != NULL)
482         *linep = logical_input_line;
483     }
484   else if (physical_input_file != NULL)
485     {
486       *namep = physical_input_file;
487       if (linep != NULL)
488         *linep = physical_input_line;
489     }
490   else
491     {
492       *namep = 0;
493       if (linep != NULL)
494         *linep = 0;
495     }
496 }                               /* as_where() */
497 \f
498
499
500
501 /*
502  *                      a s _ h o w m u c h ()
503  *
504  * Output to given stream how much of line we have scanned so far.
505  * Assumes we have scanned up to and including input_line_pointer.
506  * No free '\n' at end of line.
507  */
508 void
509 as_howmuch (stream)
510      FILE *stream;              /* Opened for write please. */
511 {
512   register char *p;             /* Scan input line. */
513   /* register char c; JF unused */
514
515   for (p = input_line_pointer - 1; *p != '\n'; --p)
516     {
517     }
518   ++p;                          /* p->1st char of line. */
519   for (; p <= input_line_pointer; p++)
520     {
521       /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
522       as_1_char ((unsigned char) *p, stream);
523     }
524 }
525
526 static void 
527 as_1_char (c, stream)
528      unsigned int c;
529      FILE *stream;
530 {
531   if (c > 127)
532     {
533       (void) putc ('%', stream);
534       c -= 128;
535     }
536   if (c < 32)
537     {
538       (void) putc ('^', stream);
539       c += '@';
540     }
541   (void) putc (c, stream);
542 }
543
544 /* end of input_scrub.c */