123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #include "as.h"
- #include "input-file.h"
- #include "safe-ctype.h"
- int preprocess = 0;
- #define BUFFER_SIZE (32 * 1024)
- static FILE *f_in;
- static char *file_name;
- struct saved_file
- {
- FILE * f_in;
- char * file_name;
- int preprocess;
- char * app_save;
- };
- void
- input_file_begin (void)
- {
- f_in = (FILE *) 0;
- }
- void
- input_file_end (void)
- {
- }
- size_t
- input_file_buffer_size (void)
- {
- return (BUFFER_SIZE);
- }
- char *
- input_file_push (void)
- {
- struct saved_file *saved;
- saved = (struct saved_file *) xmalloc (sizeof *saved);
- saved->f_in = f_in;
- saved->file_name = file_name;
- saved->preprocess = preprocess;
- if (preprocess)
- saved->app_save = app_push ();
-
- input_file_begin ();
- return (char *) saved;
- }
- void
- input_file_pop (char *arg)
- {
- struct saved_file *saved = (struct saved_file *) arg;
- input_file_end ();
- f_in = saved->f_in;
- file_name = saved->file_name;
- preprocess = saved->preprocess;
- if (preprocess)
- app_pop (saved->app_save);
- free (arg);
- }
- void
- input_file_open (char *filename,
- int pre)
- {
- int c;
- char buf[80];
- preprocess = pre;
- gas_assert (filename != 0);
- if (filename[0])
- {
- f_in = fopen (filename, FOPEN_RT);
- file_name = filename;
- }
- else
- {
-
- f_in = stdin;
-
- file_name = _("{standard input}");
- }
- if (f_in == NULL)
- {
- as_bad (_("can't open %s for reading: %s"),
- file_name, xstrerror (errno));
- return;
- }
- c = getc (f_in);
- if (ferror (f_in))
- {
- as_bad (_("can't read from %s: %s"),
- file_name, xstrerror (errno));
- fclose (f_in);
- f_in = NULL;
- return;
- }
-
- if (feof (f_in))
- {
- fclose (f_in);
- f_in = NULL;
- return;
- }
- gas_assert (c != EOF);
- if (c == '#')
- {
-
- c = getc (f_in);
- if (c == 'N')
- {
- if (fgets (buf, sizeof (buf), f_in)
- && !strncmp (buf, "O_APP", 5) && ISSPACE (buf[5]))
- preprocess = 0;
- if (!strchr (buf, '\n'))
- ungetc ('#', f_in);
- else
- ungetc ('\n', f_in);
- }
- else if (c == 'A')
- {
- if (fgets (buf, sizeof (buf), f_in)
- && !strncmp (buf, "PP", 2) && ISSPACE (buf[2]))
- preprocess = 1;
- if (!strchr (buf, '\n'))
- ungetc ('#', f_in);
- else
- ungetc ('\n', f_in);
- }
- else if (c == '\n')
- ungetc ('\n', f_in);
- else
- ungetc ('#', f_in);
- }
- else
- ungetc (c, f_in);
- }
- void
- input_file_close (void)
- {
-
- if (f_in != NULL)
- fclose (f_in);
- f_in = 0;
- }
- static size_t
- input_file_get (char *buf, size_t buflen)
- {
- size_t size;
- if (feof (f_in))
- return 0;
- size = fread (buf, sizeof (char), buflen, f_in);
- if (ferror (f_in))
- as_bad (_("can't read from %s: %s"), file_name, xstrerror (errno));
- return size;
- }
- char *
- input_file_give_next_buffer (char *where )
- {
- char *return_value;
- size_t size;
- if (f_in == (FILE *) 0)
- return 0;
-
- if (preprocess)
- size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
- else
- size = input_file_get (where, BUFFER_SIZE);
- if (size)
- return_value = where + size;
- else
- {
- if (fclose (f_in))
- as_warn (_("can't close %s: %s"), file_name, xstrerror (errno));
- f_in = (FILE *) 0;
- return_value = 0;
- }
- return return_value;
- }
|