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