input-file.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* input_file.c - Deal with Input Files -
  2. Copyright (C) 1987 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 1, 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
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  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. */
  21. #define NDEBUG /* JF remove asserts */
  22. #ifdef USG
  23. #define index strchr
  24. #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOLBF, (size))
  25. #endif
  26. #include <stdio.h>
  27. #include <assert.h>
  28. /* #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/file.h>
  31. #include <sys/wait.h> */
  32. /* #include "style.h" */
  33. #include "input-file.h"
  34. /* This variable is non-zero if the file currently being read should be
  35. preprocessed by app. It is zero if the file can be read straight in.
  36. */
  37. int preprocess = 0;
  38. void as_perror();
  39. /*
  40. * This code opens a file, then delivers BUFFER_SIZE character
  41. * chunks of the file on demand.
  42. * BUFFER_SIZE is supposed to be a number chosen for speed.
  43. * The caller only asks once what BUFFER_SIZE is, and asks before
  44. * the nature of the input files (if any) is known.
  45. */
  46. #define BUFFER_SIZE (32 * 1024)
  47. static char in_buf[BUFFER_SIZE];
  48. /*
  49. * We use static data: the data area is not sharable.
  50. */
  51. FILE *f_in; /* JF do things the RIGHT way */
  52. /* static JF remove static so app.c can use file_name */
  53. char * file_name;
  54. /* These hooks accomodate most operating systems. */
  55. void
  56. input_file_begin ()
  57. {
  58. /* file_handle = -1; */
  59. f_in = (FILE *)0;
  60. }
  61. void
  62. input_file_end ()
  63. {
  64. }
  65. int /* Return BUFFER_SIZE. */
  66. input_file_buffer_size ()
  67. {
  68. return (BUFFER_SIZE);
  69. }
  70. int
  71. input_file_is_open ()
  72. {
  73. /* return (file_handle >= 0); */
  74. return f_in!=(FILE *)0;
  75. }
  76. #ifdef DONTDEF /* JF save old version in case we need it */
  77. void
  78. input_file_open (filename, preprocess, debugging)
  79. char * filename; /* "" means use stdin. Must not be 0. */
  80. int preprocess; /* TRUE if needs app. */
  81. int debugging; /* TRUE if we are debugging assembler. */
  82. {
  83. assert( filename != 0 ); /* Filename may not be NULL. */
  84. if (filename [0])
  85. { /* We have a file name. Suck it and see. */
  86. file_handle = open (filename, O_RDONLY, 0);
  87. file_name = filename;
  88. }
  89. else
  90. { /* use stdin for the input file. */
  91. file_handle = fileno (stdin);
  92. file_name = "{standard input}"; /* For error messages. */
  93. }
  94. if (file_handle < 0)
  95. {
  96. as_perror ("Can't open source file for input", file_name);
  97. }
  98. if ( preprocess )
  99. {
  100. /*
  101. * This code was written in haste for a frobbed BSD 4.2.
  102. * I have a flight to catch: will someone please do proper
  103. * error checks? - Dean.
  104. */
  105. int pid;
  106. char temporary_file_name [12];
  107. int fd;
  108. union wait status;
  109. char *mktemp();
  110. (void)strcpy (temporary_file_name, "#appXXXXXX");
  111. (void)mktemp (temporary_file_name);
  112. pid = vfork ();
  113. if (pid == -1)
  114. {
  115. as_perror ("Can't fork to run app", file_name);
  116. _exit (144);
  117. }
  118. if (pid == 0)
  119. {
  120. (void)dup2 (file_handle, fileno(stdin));
  121. fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
  122. if (fd == -1)
  123. {
  124. (void)write(2,"Can't open temporary\n",21);
  125. _exit (99);
  126. }
  127. (void)dup2 (fd, fileno(stdout));
  128. /* JF for testing #define PREPROCESSOR "/lib/app" */
  129. #define PREPROCESSOR "./app"
  130. execl (PREPROCESSOR, PREPROCESSOR, 0);
  131. execl ("app","app",0);
  132. (void)write(2,"Exec of app failed. Get help.\n",31);
  133. (void)unlink(temporary_file_name);
  134. _exit (11);
  135. }
  136. (void)wait (& status);
  137. if (status.w_status & 0xFF00) /* JF was 0xF000, was wrong */
  138. {
  139. file_handle = -1;
  140. as_warn( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
  141. }
  142. else
  143. {
  144. file_handle = open (temporary_file_name, O_RDONLY, 0);
  145. if ( ! debugging )
  146. {
  147. if (unlink(temporary_file_name))
  148. {
  149. as_perror ("Can't delete temporary file, continuing", temporary_file_name);
  150. }
  151. }
  152. }
  153. if (file_handle == -1)
  154. {
  155. as_perror ("Can't retrieve temporary file", temporary_file_name);
  156. }
  157. }
  158. }
  159. #else
  160. void
  161. input_file_open (filename,pre)
  162. char * filename; /* "" means use stdin. Must not be 0. */
  163. int pre;
  164. {
  165. int c;
  166. char buf[80];
  167. preprocess = pre;
  168. assert( filename != 0 ); /* Filename may not be NULL. */
  169. if (filename [0]) { /* We have a file name. Suck it and see. */
  170. f_in=fopen(filename,"r");
  171. file_name=filename;
  172. } else { /* use stdin for the input file. */
  173. f_in = stdin;
  174. file_name = "{standard input}"; /* For error messages. */
  175. }
  176. if (f_in==(FILE *)0) {
  177. as_perror ("Can't open source file for input", file_name);
  178. return;
  179. }
  180. #ifndef VMS
  181. setbuffer(f_in,in_buf,BUFFER_SIZE);
  182. #endif /* VMS */
  183. c=getc(f_in);
  184. if(c=='#') { /* Begins with comment, may not want to preprocess */
  185. c=getc(f_in);
  186. if(c=='N') {
  187. fgets(buf,80,f_in);
  188. if(!strcmp(buf,"O_APP\n"))
  189. preprocess=0;
  190. if(!index(buf,'\n'))
  191. ungetc('#',f_in); /* It was longer */
  192. else
  193. ungetc('\n',f_in);
  194. } else if(c=='\n')
  195. ungetc('\n',f_in);
  196. else
  197. ungetc('#',f_in);
  198. } else
  199. ungetc(c,f_in);
  200. #ifdef DONTDEF
  201. if ( preprocess ) {
  202. char temporary_file_name [17];
  203. char *mktemp();
  204. FILE *f_out;
  205. (void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
  206. (void)mktemp (temporary_file_name);
  207. f_out=fopen(temporary_file_name,"w+");
  208. if(f_out==(FILE *)0) {
  209. as_perror("Can't open temp file");
  210. }
  211. /* JF this will have to be moved on any system that
  212. does not support removal of open files. */
  213. (void)unlink(temporary_file_name);/* JF do it NOW */
  214. do_scrub(f_in,f_out);
  215. (void)fclose(f_in); /* All done with it */
  216. (void)rewind(f_out);
  217. f_in=f_out;
  218. }
  219. #endif
  220. }
  221. #endif
  222. char *
  223. input_file_give_next_buffer (where)
  224. char * where; /* Where to place 1st character of new buffer. */
  225. {
  226. char * return_value; /* -> Last char of what we read, + 1. */
  227. register int size;
  228. if (f_in == (FILE *)0)
  229. return 0;
  230. /*
  231. * fflush (stdin); could be done here if you want to synchronise
  232. * stdin and stdout, for the case where our input file is stdin.
  233. * Since the assembler shouldn't do any output to stdout, we
  234. * don't bother to synch output and input.
  235. */
  236. /* size = read (file_handle, where, BUFFER_SIZE); */
  237. if(preprocess) {
  238. char *p;
  239. int n;
  240. int ch;
  241. extern FILE *scrub_file;
  242. int scrub_from_file();
  243. void scrub_to_file();
  244. int do_scrub_next_char();
  245. scrub_file=f_in;
  246. for(p=where,n=BUFFER_SIZE;n;--n) {
  247. ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
  248. if(ch==EOF)
  249. break;
  250. *p++=ch;
  251. }
  252. size=BUFFER_SIZE-n;
  253. } else
  254. size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
  255. if (size < 0)
  256. {
  257. as_perror ("Can't read source file: end-of-file faked.", file_name);
  258. size = 0;
  259. }
  260. if (size)
  261. return_value = where + size;
  262. else
  263. {
  264. if (fclose (f_in))
  265. as_perror ("Can't close source file -- continuing", file_name);
  266. f_in = (FILE *)0;
  267. return_value = 0;
  268. }
  269. return (return_value);
  270. }
  271. /* end: input_file.c */