Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / gnu / usr.bin / as / input-scrub.c
1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2    Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
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. */
19
20 /*
21  * $FreeBSD: src/gnu/usr.bin/as/input-scrub.c,v 1.7 1999/08/27 23:34:18 peter Exp $
22  * $DragonFly: src/gnu/usr.bin/as/Attic/input-scrub.c,v 1.2 2003/06/17 04:25:44 dillon Exp $
23  */
24 #include <errno.h>              /* Need this to make errno declaration right */
25 #include "as.h"
26 #include "input-file.h"
27
28 /*
29  * O/S independent module to supply buffers of sanitised source code
30  * to rest of assembler. We get sanitized 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 /* Saved information about the file that .include'd this one.  When we hit EOF,
72    we automatically pop to that file. */
73
74 static char *next_saved_file;
75
76 /* We can have more than one source file open at once, though the info for all
77    but the latest one are saved off in a struct input_save.  These files remain
78    open, so we are limited by the number of open files allowed by the
79    underlying OS. We may also sequentially read more than one source file in an
80    assembly. */
81
82 /* We must track the physical file and line number for error messages. We also
83    track a "logical" file and line number corresponding to (C?)  compiler
84    source line numbers.  Whenever we open a file we must fill in
85    physical_input_file. So if it is NULL we have not opened any files yet. */
86
87 static char *physical_input_file;
88 static char *logical_input_file;
89
90 typedef unsigned int line_numberT;      /* 1-origin line number in a source file. */
91 /* A line ends in '\n' or eof. */
92
93 static line_numberT physical_input_line;
94 static int logical_input_line;
95
96 /* Struct used to save the state of the input handler during include files */
97 struct input_save
98   {
99     char *buffer_start;
100     char *partial_where;
101     int partial_size;
102     char save_source[AFTER_SIZE];
103     unsigned int buffer_length;
104     char *physical_input_file;
105     char *logical_input_file;
106     line_numberT physical_input_line;
107     int logical_input_line;
108     char *next_saved_file;      /* Chain of input_saves */
109     char *input_file_save;      /* Saved state of input routines */
110     char *saved_position;       /* Caller's saved position in buf */
111   };
112
113 static char *input_scrub_push PARAMS ((char *saved_position));
114 static char *input_scrub_pop PARAMS ((char *arg));
115 static void as_1_char PARAMS ((unsigned int c, FILE * stream));
116
117 /* Push the state of input reading and scrubbing so that we can #include.
118    The return value is a 'void *' (fudged for old compilers) to a save
119    area, which can be restored by passing it to input_scrub_pop(). */
120 static char *
121 input_scrub_push (saved_position)
122      char *saved_position;
123 {
124   register struct input_save *saved;
125
126   saved = (struct input_save *) xmalloc (sizeof *saved);
127
128   saved->saved_position = saved_position;
129   saved->buffer_start = buffer_start;
130   saved->partial_where = partial_where;
131   saved->partial_size = partial_size;
132   saved->buffer_length = buffer_length;
133   saved->physical_input_file = physical_input_file;
134   saved->logical_input_file = logical_input_file;
135   saved->physical_input_line = physical_input_line;
136   saved->logical_input_line = logical_input_line;
137   memcpy (saved->save_source, save_source, sizeof (save_source));
138   saved->next_saved_file = next_saved_file;
139   saved->input_file_save = input_file_push ();
140
141   input_file_begin ();          /* Reinitialize! */
142   logical_input_line = -1;
143   logical_input_file = (char *) NULL;
144   buffer_length = input_file_buffer_size ();
145
146   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
147   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
148
149   return ((char *) saved);
150 }                               /* input_scrub_push() */
151
152 static char *
153 input_scrub_pop (arg)
154      char *arg;
155 {
156   register struct input_save *saved;
157   char *saved_position;
158
159   input_scrub_end ();           /* Finish off old buffer */
160
161   saved = (struct input_save *) arg;
162
163   input_file_pop (saved->input_file_save);
164   saved_position = saved->saved_position;
165   buffer_start = saved->buffer_start;
166   buffer_length = saved->buffer_length;
167   physical_input_file = saved->physical_input_file;
168   logical_input_file = saved->logical_input_file;
169   physical_input_line = saved->physical_input_line;
170   logical_input_line = saved->logical_input_line;
171   partial_where = saved->partial_where;
172   partial_size = saved->partial_size;
173   next_saved_file = saved->next_saved_file;
174   memcpy (save_source, saved->save_source, sizeof (save_source));
175
176   free (arg);
177   return saved_position;
178 }
179 \f
180
181 void
182 input_scrub_begin ()
183 {
184   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
185   know (strlen (AFTER_STRING) == AFTER_SIZE || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
186
187   input_file_begin ();
188
189   buffer_length = input_file_buffer_size ();
190
191   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
192   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
193
194   /* Line number things. */
195   logical_input_line = -1;
196   logical_input_file = (char *) NULL;
197   physical_input_file = NULL;   /* No file read yet. */
198   next_saved_file = NULL;       /* At EOF, don't pop to any other file */
199   do_scrub_begin ();
200 }
201
202 void
203 input_scrub_end ()
204 {
205   if (buffer_start)
206     {
207       free (buffer_start);
208       buffer_start = 0;
209       input_file_end ();
210     }
211 }
212
213 /* Start reading input from a new file. */
214
215 char *                          /* Return start of caller's part of buffer. */
216 input_scrub_new_file (filename)
217      char *filename;
218 {
219   input_file_open (filename, !flagseen['f']);
220   physical_input_file = filename[0] ? filename : "{standard input}";
221   physical_input_line = 0;
222
223   partial_size = 0;
224   return (buffer_start + BEFORE_SIZE);
225 }
226
227
228 /* Include a file from the current file.  Save our state, cause it to
229    be restored on EOF, and begin handling a new file.  Same result as
230    input_scrub_new_file. */
231
232 char *
233 input_scrub_include_file (filename, position)
234      char *filename;
235      char *position;
236 {
237   next_saved_file = input_scrub_push (position);
238   return input_scrub_new_file (filename);
239 }
240
241 void
242 input_scrub_close ()
243 {
244   input_file_close ();
245 }
246
247 char *
248 input_scrub_next_buffer (bufp)
249      char **bufp;
250 {
251   register char *limit;         /*->just after last char of buffer. */
252
253   *bufp = buffer_start + BEFORE_SIZE;
254
255   if (partial_size)
256     {
257       memcpy (buffer_start + BEFORE_SIZE, partial_where,
258               (unsigned int) partial_size);
259       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
260     }
261   limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
262   if (limit)
263     {
264       register char *p;         /* Find last newline. */
265
266       for (p = limit; *--p != '\n';);;
267       ++p;
268       if (p <= buffer_start + BEFORE_SIZE)
269         {
270           as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
271         }
272       partial_where = p;
273       partial_size = limit - p;
274       memcpy (save_source, partial_where, (int) AFTER_SIZE);
275       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
276     }
277   else
278     {
279       partial_where = 0;
280       if (partial_size > 0)
281         {
282           as_warn ("Partial line at end of file ignored");
283         }
284       /* If we should pop to another file at EOF, do it. */
285       if (next_saved_file)
286         {
287           *bufp = input_scrub_pop (next_saved_file);    /* Pop state */
288           /* partial_where is now correct to return, since we popped it. */
289         }
290     }
291   return (partial_where);
292 }                               /* input_scrub_next_buffer() */
293 \f
294 /*
295  * The remaining part of this file deals with line numbers, error
296  * messages and so on.
297  */
298
299
300 int
301 seen_at_least_1_file ()         /* TRUE if we opened any file. */
302 {
303   return (physical_input_file != NULL);
304 }
305
306 void
307 bump_line_counters ()
308 {
309   ++physical_input_line;
310   if (logical_input_line >= 0)
311     ++logical_input_line;
312 }
313 \f
314 /*
315  *                      new_logical_line()
316  *
317  * Tells us what the new logical line number and file are.
318  * If the line_number is -1, we don't change the current logical line
319  * number.  If it is -2, we decrement the logical line number (this is
320  * to support the .appfile pseudo-op inserted into the stream by
321  * do_scrub_next_char).
322  * If the fname is NULL, we don't change the current logical file name.
323  */
324 void
325 new_logical_line (fname, line_number)
326      char *fname;               /* DON'T destroy it! We point to it! */
327      int line_number;
328 {
329   if (fname)
330     {
331       logical_input_file = fname;
332     }                           /* if we have a file name */
333
334   if (line_number >= 0)
335     logical_input_line = line_number;
336   else if (line_number == -2 && logical_input_line > 0)
337     --logical_input_line;
338 }                               /* new_logical_line() */
339 \f
340 /*
341  *                      a s _ w h e r e ()
342  *
343  * Return the current file name and line number.
344  * namep should be char * const *, but there are compilers which screw
345  * up declarations like that, and it's easier to avoid it.
346  */
347 void
348 as_where (namep, linep)
349      char **namep;
350      unsigned int *linep;
351 {
352   if (logical_input_file != NULL
353       && (linep == NULL || logical_input_line >= 0))
354     {
355       *namep = logical_input_file;
356       if (linep != NULL)
357         *linep = logical_input_line;
358     }
359   else if (physical_input_file != NULL)
360     {
361       *namep = physical_input_file;
362       if (linep != NULL)
363         *linep = physical_input_line;
364     }
365   else
366     {
367       *namep = (char *) "*unknown*";
368       if (linep != NULL)
369         *linep = 0;
370     }
371 }                               /* as_where() */
372 \f
373
374
375
376 /*
377  *                      a s _ h o w m u c h ()
378  *
379  * Output to given stream how much of line we have scanned so far.
380  * Assumes we have scanned up to and including input_line_pointer.
381  * No free '\n' at end of line.
382  */
383 void
384 as_howmuch (stream)
385      FILE *stream;              /* Opened for write please. */
386 {
387   register char *p;             /* Scan input line. */
388   /* register char c; JF unused */
389
390   for (p = input_line_pointer - 1; *p != '\n'; --p)
391     {
392     }
393   ++p;                          /* p->1st char of line. */
394   for (; p <= input_line_pointer; p++)
395     {
396       /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
397       /* c = *p & 0xFF; JF unused */
398       as_1_char ((unsigned char) *p, stream);
399     }
400 }
401
402 static void
403 as_1_char (c, stream)
404      unsigned int c;
405      FILE *stream;
406 {
407   if (c > 127)
408     {
409       (void) putc ('%', stream);
410       c -= 128;
411     }
412   if (c < 32)
413     {
414       (void) putc ('^', stream);
415       c += '@';
416     }
417   (void) putc (c, stream);
418 }
419
420 /* end of input_scrub.c */