input-file.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* input_file.c - Deal with Input Files -
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* Confines all details of reading source bytes to this module.
  17. All O/S specific crocks should live here.
  18. What we lose in "efficiency" we gain in modularity.
  19. Note we don't need to #include the "as.h" file. No common coupling! */
  20. #include "as.h"
  21. #include "input-file.h"
  22. #include "safe-ctype.h"
  23. /* This variable is non-zero if the file currently being read should be
  24. preprocessed by app. It is zero if the file can be read straight in. */
  25. int preprocess = 0;
  26. /* This code opens a file, then delivers BUFFER_SIZE character
  27. chunks of the file on demand.
  28. BUFFER_SIZE is supposed to be a number chosen for speed.
  29. The caller only asks once what BUFFER_SIZE is, and asks before
  30. the nature of the input files (if any) is known. */
  31. #define BUFFER_SIZE (32 * 1024)
  32. /* We use static data: the data area is not sharable. */
  33. static FILE *f_in;
  34. static const char *file_name;
  35. /* Struct for saving the state of this module for file includes. */
  36. struct saved_file
  37. {
  38. FILE * f_in;
  39. const char * file_name;
  40. int preprocess;
  41. char * app_save;
  42. };
  43. /* These hooks accommodate most operating systems. */
  44. void
  45. input_file_begin (void)
  46. {
  47. f_in = (FILE *) 0;
  48. }
  49. void
  50. input_file_end (void)
  51. {
  52. }
  53. /* Return BUFFER_SIZE. */
  54. size_t
  55. input_file_buffer_size (void)
  56. {
  57. return (BUFFER_SIZE);
  58. }
  59. /* Push the state of our input, returning a pointer to saved info that
  60. can be restored with input_file_pop (). */
  61. char *
  62. input_file_push (void)
  63. {
  64. struct saved_file *saved;
  65. saved = XNEW (struct saved_file);
  66. saved->f_in = f_in;
  67. saved->file_name = file_name;
  68. saved->preprocess = preprocess;
  69. if (preprocess)
  70. saved->app_save = app_push ();
  71. /* Initialize for new file. */
  72. input_file_begin ();
  73. return (char *) saved;
  74. }
  75. void
  76. input_file_pop (char *arg)
  77. {
  78. struct saved_file *saved = (struct saved_file *) arg;
  79. input_file_end (); /* Close out old file. */
  80. f_in = saved->f_in;
  81. file_name = saved->file_name;
  82. preprocess = saved->preprocess;
  83. if (preprocess)
  84. app_pop (saved->app_save);
  85. free (arg);
  86. }
  87. /* Open the specified file, "" means stdin. Filename must not be null. */
  88. void
  89. input_file_open (const char *filename,
  90. int pre)
  91. {
  92. int c;
  93. char buf[80];
  94. preprocess = pre;
  95. gas_assert (filename != 0); /* Filename may not be NULL. */
  96. if (filename[0])
  97. {
  98. f_in = fopen (filename, FOPEN_RT);
  99. file_name = filename;
  100. }
  101. else
  102. {
  103. /* Use stdin for the input file. */
  104. f_in = stdin;
  105. /* For error messages. */
  106. file_name = _("{standard input}");
  107. }
  108. if (f_in == NULL)
  109. {
  110. as_bad (_("can't open %s for reading: %s"),
  111. file_name, xstrerror (errno));
  112. return;
  113. }
  114. c = getc (f_in);
  115. if (ferror (f_in))
  116. {
  117. as_bad (_("can't read from %s: %s"),
  118. file_name, xstrerror (errno));
  119. fclose (f_in);
  120. f_in = NULL;
  121. return;
  122. }
  123. /* Check for an empty input file. */
  124. if (feof (f_in))
  125. {
  126. fclose (f_in);
  127. f_in = NULL;
  128. return;
  129. }
  130. gas_assert (c != EOF);
  131. if (c == '#')
  132. {
  133. /* Begins with comment, may not want to preprocess. */
  134. c = getc (f_in);
  135. if (c == 'N')
  136. {
  137. if (fgets (buf, sizeof (buf), f_in)
  138. && startswith (buf, "O_APP") && ISSPACE (buf[5]))
  139. preprocess = 0;
  140. if (!strchr (buf, '\n'))
  141. ungetc ('#', f_in); /* It was longer. */
  142. else
  143. ungetc ('\n', f_in);
  144. }
  145. else if (c == 'A')
  146. {
  147. if (fgets (buf, sizeof (buf), f_in)
  148. && startswith (buf, "PP") && ISSPACE (buf[2]))
  149. preprocess = 1;
  150. if (!strchr (buf, '\n'))
  151. ungetc ('#', f_in);
  152. else
  153. ungetc ('\n', f_in);
  154. }
  155. else if (c == '\n')
  156. ungetc ('\n', f_in);
  157. else
  158. ungetc ('#', f_in);
  159. }
  160. else
  161. ungetc (c, f_in);
  162. }
  163. /* Close input file. */
  164. void
  165. input_file_close (void)
  166. {
  167. /* Don't close a null file pointer. */
  168. if (f_in != NULL)
  169. fclose (f_in);
  170. f_in = 0;
  171. }
  172. /* This function is passed to do_scrub_chars. */
  173. static size_t
  174. input_file_get (char *buf, size_t buflen)
  175. {
  176. size_t size;
  177. if (feof (f_in))
  178. return 0;
  179. size = fread (buf, sizeof (char), buflen, f_in);
  180. if (ferror (f_in))
  181. as_bad (_("can't read from %s: %s"), file_name, xstrerror (errno));
  182. return size;
  183. }
  184. /* Read a buffer from the input file. */
  185. char *
  186. input_file_give_next_buffer (char *where /* Where to place 1st character of new buffer. */)
  187. {
  188. char *return_value; /* -> Last char of what we read, + 1. */
  189. size_t size;
  190. if (f_in == (FILE *) 0)
  191. return 0;
  192. /* fflush (stdin); could be done here if you want to synchronise
  193. stdin and stdout, for the case where our input file is stdin.
  194. Since the assembler shouldn't do any output to stdout, we
  195. don't bother to synch output and input. */
  196. if (preprocess)
  197. size = do_scrub_chars (input_file_get, where, BUFFER_SIZE);
  198. else
  199. size = input_file_get (where, BUFFER_SIZE);
  200. if (size)
  201. return_value = where + size;
  202. else
  203. {
  204. if (fclose (f_in))
  205. as_warn (_("can't close %s: %s"), file_name, xstrerror (errno));
  206. f_in = (FILE *) 0;
  207. return_value = 0;
  208. }
  209. return return_value;
  210. }