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