|
|
1.1 root 1: /* input_file.c - Deal with Input Files -
2: Copyright (C) 1987 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 1, 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
18: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19:
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:
27: #define NDEBUG /* JF remove asserts */
28:
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <assert.h>
33: #include <libc.h>
34: #include "input-file.h"
35: #include "xmalloc.h"
36: #include "input-scrub.h"
37: #include "app.h"
38: #include "as.h"
39:
40: /*
41: * This variable is non-zero if the file currently being read should be
42: * preprocessed by app. It is zero if the file can be read straight in.
43: */
44: #ifndef SUSPECT
45: static
46: #endif
47: int preprocess = 0;
48:
49: /*
50: * This code opens a file, then delivers BUFFER_SIZE character
51: * chunks of the file on demand.
52: * BUFFER_SIZE is supposed to be a number chosen for speed.
53: * The caller only asks once what BUFFER_SIZE is, and asks before
54: * the nature of the input files (if any) is known.
55: */
56:
57: #define BUFFER_SIZE (32 * 1024)
58:
59: #ifndef NeXT
60: static char in_buf[BUFFER_SIZE];
61: #endif /* !defined(NeXT) */
62:
63: /*
64: * We use static data: the data area is not sharable.
65: */
66:
67: FILE *f_in = NULL; /* JF do things the RIGHT way */
68: /* static JF remove static so app.c can use file_name */
69: char *file_name = NULL;
70:
71: /* These hooks accomodate most operating systems. */
72:
73: void
74: input_file_begin(
75: void)
76: {
77: /* file_handle = -1; */
78: f_in = (FILE *)0;
79: }
80:
81: void
82: input_file_end(
83: void)
84: {
85: }
86:
87: int /* Return BUFFER_SIZE. */
88: input_file_buffer_size(
89: void)
90: {
91: return (BUFFER_SIZE);
92: }
93:
94: int
95: input_file_is_open(
96: void)
97: {
98: /* return (file_handle >= 0); */
99: return f_in!=(FILE *)0;
100: }
101:
102: void
103: input_file_open(
104: char *filename, /* "" means use stdin. Must not be 0. */
105: int pre)
106: {
107: int c;
108: char buf[80];
109:
110: preprocess = pre;
111:
112: assert( filename != 0 ); /* Filename may not be NULL. */
113: if (filename [0]) { /* We have a file name. Suck it and see. */
114: f_in=fopen(filename,"r");
115: file_name=filename;
116: } else { /* use stdin for the input file. */
117: f_in = stdin;
118: file_name = "{standard input}"; /* For error messages. */
119: }
120: if (f_in==(FILE *)0) {
121: as_perror ("Can't open source file for input", file_name);
122: return;
123: }
124: #ifdef NeXT /* .include feature */
125: setbuffer(f_in, xmalloc(BUFFER_SIZE), BUFFER_SIZE);
126: #else
127: setbuffer(f_in,in_buf,BUFFER_SIZE);
128: #endif
129: c=getc(f_in);
130: if(c=='#') { /* Begins with comment, may not want to preprocess */
131: c=getc(f_in);
132: if(c=='N') {
133: fgets(buf,80,f_in);
134: if(!strcmp(buf,"O_APP\n"))
135: preprocess=0;
136: if(!index(buf,'\n'))
137: ungetc('#',f_in); /* It was longer */
138: else
139: ungetc('\n',f_in);
140: } else if(c=='\n')
141: ungetc('\n',f_in);
142: else
143: ungetc('#',f_in);
144: } else
145: ungetc(c,f_in);
146: }
147:
148: char *
149: input_file_give_next_buffer(
150: char *where) /* Where to place 1st character of new buffer. */
151: {
152: char * return_value; /* -> Last char of what we read, + 1. */
153: register int size;
154:
155: if (f_in == (FILE *)0)
156: return 0;
157: /*
158: * fflush (stdin); could be done here if you want to synchronise
159: * stdin and stdout, for the case where our input file is stdin.
160: * Since the assembler shouldn't do any output to stdout, we
161: * don't bother to synch output and input.
162: */
163: /* size = read (file_handle, where, BUFFER_SIZE); */
164: if(preprocess) {
165: char *p;
166: int n;
167: int ch;
168:
169: scrub_file=f_in;
170: for(p=where,n=BUFFER_SIZE;n;--n) {
171: ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
172: if(ch==EOF)
173: break;
174: *p++=ch;
175: }
176: size=BUFFER_SIZE-n;
177: } else
178: size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
179: if (size < 0)
180: {
181: as_perror ("Can't read source file: end-of-file faked.", file_name);
182: size = 0;
183: }
184: if (size)
185: return_value = where + size;
186: else
187: {
188: #ifdef NeXT /* .include feature */
189: if(doing_include)
190: free (f_in->_base);
191: #endif /* NeXT .include feature */
192: if (fclose (f_in))
193: as_perror ("Can't close source file -- continuing", file_name);
194: f_in = (FILE *)0;
195: return_value = 0;
196: }
197: return (return_value);
198: }
199:
200: /* end: input_file.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.