Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/input-file.c
Line
Count
Source
1
/* input_file.c - Deal with Input Files -
2
   Copyright (C) 1987-2026 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 3, 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 the Free
18
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
/* Confines all details of reading source bytes to this module.
22
   All O/S specific crocks should live here.
23
   What we lose in "efficiency" we gain in modularity.
24
   Note we don't need to #include the "as.h" file. No common coupling!  */
25
26
#include "as.h"
27
#include "input-file.h"
28
#include "safe-ctype.h"
29
30
/* This variable is non-zero if the file currently being read should be
31
   preprocessed by app.  It is zero if the file can be read straight in.  */
32
int preprocess = 0;
33
34
/* This code opens a file, then delivers BUFFER_SIZE character
35
   chunks of the file on demand.
36
   BUFFER_SIZE is supposed to be a number chosen for speed.
37
   The caller only asks once what BUFFER_SIZE is, and asks before
38
   the nature of the input files (if any) is known.  */
39
40
3.61k
#define BUFFER_SIZE (32 * 1024)
41
42
/* We use static data: the data area is not sharable.  */
43
44
static FILE *f_in;
45
static const char *file_name;
46
47
/* Struct for saving the state of this module for file includes.  */
48
struct saved_file
49
  {
50
    FILE * f_in;
51
    const char * file_name;
52
    int    preprocess;
53
    char * app_save;
54
  };
55

56
/* These hooks accommodate most operating systems.  */
57
58
void
59
input_file_begin (void)
60
4.10k
{
61
4.10k
  f_in = NULL;
62
4.10k
}
63
64
void
65
input_file_end (void)
66
4.10k
{
67
4.10k
}
68
69
/* Return BUFFER_SIZE.  */
70
size_t
71
input_file_buffer_size (void)
72
2.33k
{
73
2.33k
  return (BUFFER_SIZE);
74
2.33k
}
75
76
/* Push the state of our input, returning a pointer to saved info that
77
   can be restored with input_file_pop ().  */
78
79
char *
80
input_file_push (void)
81
1.77k
{
82
1.77k
  struct saved_file *saved;
83
84
1.77k
  saved = XNEW (struct saved_file);
85
86
1.77k
  saved->f_in = f_in;
87
1.77k
  saved->file_name = file_name;
88
1.77k
  saved->preprocess = preprocess;
89
1.77k
  if (preprocess)
90
1.77k
    saved->app_save = app_push ();
91
92
  /* Initialize for new file.  */
93
1.77k
  input_file_begin ();
94
95
1.77k
  return (char *) saved;
96
1.77k
}
97
98
void
99
input_file_pop (char *arg)
100
1.77k
{
101
1.77k
  struct saved_file *saved = (struct saved_file *) arg;
102
103
1.77k
  input_file_end ();    /* Close out old file.  */
104
105
1.77k
  f_in = saved->f_in;
106
1.77k
  file_name = saved->file_name;
107
1.77k
  preprocess = saved->preprocess;
108
1.77k
  if (preprocess)
109
1.77k
    app_pop (saved->app_save);
110
111
1.77k
  free (arg);
112
1.77k
}
113

114
/* Open the specified file, "" means stdin.  Filename must not be null.  */
115
116
void
117
input_file_open (const char *filename,
118
     int pre)
119
668
{
120
668
  int c;
121
668
  char buf[80];
122
123
668
  preprocess = pre;
124
125
668
  gas_assert (filename != 0); /* Filename may not be NULL.  */
126
668
  if (filename[0])
127
668
    {
128
668
      f_in = fopen (filename, FOPEN_RT);
129
668
      file_name = filename;
130
668
    }
131
0
  else
132
0
    {
133
      /* Use stdin for the input file.  */
134
0
      f_in = stdin;
135
      /* For error messages.  */
136
0
      file_name = _("{standard input}");
137
0
    }
138
139
668
  if (f_in == NULL)
140
27
    {
141
27
      as_bad (_("can't open %s for reading: %s"),
142
27
        file_name, xstrerror (errno));
143
27
      return;
144
27
    }
145
146
641
  c = getc (f_in);
147
148
641
  if (ferror (f_in))
149
74
    {
150
74
      as_bad (_("can't read from %s: %s"),
151
74
        file_name, xstrerror (errno));
152
153
74
      fclose (f_in);
154
74
      f_in = NULL;
155
74
      return;
156
74
    }
157
158
  /* Check for an empty input file.  */
159
567
  if (feof (f_in))
160
0
    {
161
0
      fclose (f_in);
162
0
      f_in = NULL;
163
0
      return;
164
0
    }
165
567
  gas_assert (c != EOF);
166
167
567
  if (strchr (line_comment_chars, '#')
168
567
      ? c == '#'
169
567
      : c && strchr (line_comment_chars, c))
170
18
    {
171
      /* Begins with comment, may not want to preprocess.  */
172
18
      int lead = c;
173
174
18
      c = getc (f_in);
175
18
      if (c == 'N')
176
6
  {
177
6
    char *p = fgets (buf, sizeof (buf), f_in);
178
6
    if (p && startswith (p, "O_APP") && is_end_of_line (p[5]))
179
3
      preprocess = 0;
180
6
    if (!p || !strchr (p, '\n'))
181
2
      ungetc (lead, f_in);
182
4
    else
183
4
      ungetc ('\n', f_in);
184
6
  }
185
12
      else if (c == 'A')
186
0
  {
187
0
    char *p = fgets (buf, sizeof (buf), f_in);
188
0
    if (p && startswith (p, "PP") && is_end_of_line (p[2]))
189
0
      preprocess = 1;
190
0
    if (!p || !strchr (p, '\n'))
191
0
      ungetc (lead, f_in);
192
0
    else
193
0
      ungetc ('\n', f_in);
194
0
  }
195
12
      else if (c == '\n')
196
0
  ungetc ('\n', f_in);
197
12
      else
198
12
  ungetc (lead, f_in);
199
18
    }
200
549
  else
201
549
    ungetc (c, f_in);
202
567
}
203
204
/* Close input file.  */
205
206
void
207
input_file_close (void)
208
567
{
209
  /* Don't close a null file pointer.  */
210
567
  if (f_in != NULL)
211
1
    fclose (f_in);
212
213
567
  f_in = 0;
214
567
}
215
216
/* This function is passed to do_scrub_chars.  */
217
218
static size_t
219
input_file_get (char *buf, size_t buflen)
220
1.91k
{
221
1.91k
  size_t size;
222
223
1.91k
  if (feof (f_in))
224
1.34k
    return 0;
225
226
567
  size = fread (buf, sizeof (char), buflen, f_in);
227
567
  if (ferror (f_in))
228
0
    as_bad (_("can't read from %s: %s"), file_name, xstrerror (errno));
229
567
  return size;
230
1.91k
}
231
232
/* Read a buffer from the input file.  */
233
234
char *
235
input_file_give_next_buffer (char *where /* Where to place 1st character of new buffer.  */)
236
1.40k
{
237
1.40k
  char *return_value;   /* -> Last char of what we read, + 1.  */
238
1.40k
  size_t size;
239
240
1.40k
  if (f_in == NULL)
241
124
    return 0;
242
  /* fflush (stdin); could be done here if you want to synchronise
243
     stdin and stdout, for the case where our input file is stdin.
244
     Since the assembler shouldn't do any output to stdout, we
245
     don't bother to synch output and input.  */
246
1.28k
  if (preprocess)
247
1.27k
    size = do_scrub_chars (input_file_get, where, BUFFER_SIZE,
248
1.27k
                           multibyte_handling == multibyte_warn);
249
6
  else
250
6
    {
251
6
      size = input_file_get (where, BUFFER_SIZE);
252
253
6
      if (multibyte_handling == multibyte_warn)
254
0
  {
255
0
    const unsigned char *start = (const unsigned char *) where;
256
257
0
    (void) scan_for_multibyte_characters (start, start + size,
258
0
            true /* Generate warnings */);
259
0
  }
260
6
    }
261
262
1.28k
  if (size)
263
715
    return_value = where + size;
264
565
  else
265
565
    {
266
565
      if (fclose (f_in))
267
0
  as_warn (_("can't close %s: %s"), file_name, xstrerror (errno));
268
269
565
      f_in = NULL;
270
565
      return_value = 0;
271
565
    }
272
273
1.28k
  return return_value;
274
1.40k
}