Subversion Repositories Kolibri OS

Rev

Rev 5222 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* input_file.c - Deal with Input Files -
  2.    Copyright (C) 1987-2015 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. #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 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.     char * file_name;
  52.     int    preprocess;
  53.     char * app_save;
  54.   };
  55. /* These hooks accommodate most operating systems.  */
  56.  
  57. void
  58. input_file_begin (void)
  59. {
  60.   f_in = (FILE *) 0;
  61. }
  62.  
  63. void
  64. input_file_end (void)
  65. {
  66. }
  67.  
  68. /* Return BUFFER_SIZE.  */
  69. size_t
  70. input_file_buffer_size (void)
  71. {
  72.   return (BUFFER_SIZE);
  73. }
  74.  
  75. /* Push the state of our input, returning a pointer to saved info that
  76.    can be restored with input_file_pop ().  */
  77.  
  78. char *
  79. input_file_push (void)
  80. {
  81.   struct saved_file *saved;
  82.  
  83.   saved = (struct saved_file *) xmalloc (sizeof *saved);
  84.  
  85.   saved->f_in = f_in;
  86.   saved->file_name = file_name;
  87.   saved->preprocess = preprocess;
  88.   if (preprocess)
  89.     saved->app_save = app_push ();
  90.  
  91.   /* Initialize for new file.  */
  92.   input_file_begin ();
  93.  
  94.   return (char *) saved;
  95. }
  96.  
  97. void
  98. input_file_pop (char *arg)
  99. {
  100.   struct saved_file *saved = (struct saved_file *) arg;
  101.  
  102.   input_file_end ();            /* Close out old file.  */
  103.  
  104.   f_in = saved->f_in;
  105.   file_name = saved->file_name;
  106.   preprocess = saved->preprocess;
  107.   if (preprocess)
  108.     app_pop (saved->app_save);
  109.  
  110.   free (arg);
  111. }
  112. void
  113. input_file_open (char *filename, /* "" means use stdin. Must not be 0.  */
  114.                  int pre)
  115. {
  116.   int c;
  117.   char buf[80];
  118.  
  119.   preprocess = pre;
  120.  
  121.   gas_assert (filename != 0);   /* Filename may not be NULL.  */
  122.   if (filename[0])
  123.     {
  124.       f_in = fopen (filename, FOPEN_RT);
  125.       file_name = filename;
  126.     }
  127.   else
  128.     {
  129.       /* Use stdin for the input file.  */
  130.       f_in = stdin;
  131.       /* For error messages.  */
  132.       file_name = _("{standard input}");
  133.     }
  134.  
  135.   if (f_in == NULL)
  136.     {
  137.       as_bad (_("can't open %s for reading: %s"),
  138.               file_name, xstrerror (errno));
  139.       return;
  140.     }
  141.  
  142.   c = getc (f_in);
  143.  
  144.   if (ferror (f_in))
  145.     {
  146.       as_bad (_("can't read from %s: %s"),
  147.               file_name, xstrerror (errno));
  148.  
  149.       fclose (f_in);
  150.       f_in = NULL;
  151.       return;
  152.     }
  153.  
  154.   /* Check for an empty input file.  */
  155.   if (feof (f_in))
  156.     {
  157.       fclose (f_in);
  158.       f_in = NULL;
  159.       return;
  160.     }
  161.   gas_assert (c != EOF);
  162.  
  163.   if (c == '#')
  164.     {
  165.       /* Begins with comment, may not want to preprocess.  */
  166.       c = getc (f_in);
  167.       if (c == 'N')
  168.         {
  169.           if (fgets (buf, sizeof (buf), f_in)
  170.               && !strncmp (buf, "O_APP", 5) && ISSPACE (buf[5]))
  171.             preprocess = 0;
  172.           if (!strchr (buf, '\n'))
  173.             ungetc ('#', f_in); /* It was longer.  */
  174.           else
  175.             ungetc ('\n', f_in);
  176.         }
  177.       else if (c == 'A')
  178.         {
  179.           if (fgets (buf, sizeof (buf), f_in)
  180.               && !strncmp (buf, "PP", 2) && ISSPACE (buf[2]))
  181.             preprocess = 1;
  182.           if (!strchr (buf, '\n'))
  183.             ungetc ('#', f_in);
  184.           else
  185.             ungetc ('\n', f_in);
  186.         }
  187.       else if (c == '\n')
  188.         ungetc ('\n', f_in);
  189.       else
  190.         ungetc ('#', f_in);
  191.     }
  192.   else
  193.     ungetc (c, f_in);
  194. }
  195.  
  196. /* Close input file.  */
  197.  
  198. void
  199. input_file_close (void)
  200. {
  201.   /* Don't close a null file pointer.  */
  202.   if (f_in != NULL)
  203.     fclose (f_in);
  204.  
  205.   f_in = 0;
  206. }
  207.  
  208. /* This function is passed to do_scrub_chars.  */
  209.  
  210. static size_t
  211. input_file_get (char *buf, size_t buflen)
  212. {
  213.   size_t size;
  214.  
  215.   if (feof (f_in))
  216.     return 0;
  217.  
  218.   size = fread (buf, sizeof (char), buflen, f_in);
  219.   if (ferror (f_in))
  220.     as_bad (_("can't read from %s: %s"), file_name, xstrerror (errno));
  221.   return size;
  222. }
  223.  
  224. /* Read a buffer from the input file.  */
  225.  
  226. char *
  227. input_file_give_next_buffer (char *where /* Where to place 1st character of new buffer.  */)
  228. {
  229.   char *return_value;           /* -> Last char of what we read, + 1.  */
  230.   size_t size;
  231.  
  232.   if (f_in == (FILE *) 0)
  233.     return 0;
  234.   /* fflush (stdin); could be done here if you want to synchronise
  235.      stdin and stdout, for the case where our input file is stdin.
  236.      Since the assembler shouldn't do any output to stdout, we
  237.      don't bother to synch output and input.  */
  238.   if (preprocess)
  239.     size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
  240.   else
  241.     size = input_file_get (where, BUFFER_SIZE);
  242.  
  243.   if (size)
  244.     return_value = where + size;
  245.   else
  246.     {
  247.       if (fclose (f_in))
  248.         as_warn (_("can't close %s: %s"), file_name, xstrerror (errno));
  249.  
  250.       f_in = (FILE *) 0;
  251.       return_value = 0;
  252.     }
  253.  
  254.   return return_value;
  255. }
  256.