1*49403Sbostic /*-
2*49403Sbostic  * This code is derived from software copyrighted by the Free Software
3*49403Sbostic  * Foundation.
4*49403Sbostic  *
5*49403Sbostic  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6*49403Sbostic  */
7*49403Sbostic 
8*49403Sbostic #ifndef lint
9*49403Sbostic static char sccsid[] = "@(#)input-file.c	6.2 (Berkeley) 05/08/91";
10*49403Sbostic #endif /* not lint */
11*49403Sbostic 
1247549Sdonn /* input_file.c - Deal with Input Files -
1347549Sdonn    Copyright (C) 1987 Free Software Foundation, Inc.
1447549Sdonn 
1547549Sdonn This file is part of GAS, the GNU Assembler.
1647549Sdonn 
1747549Sdonn GAS is free software; you can redistribute it and/or modify
1847549Sdonn it under the terms of the GNU General Public License as published by
1947549Sdonn the Free Software Foundation; either version 1, or (at your option)
2047549Sdonn any later version.
2147549Sdonn 
2247549Sdonn GAS is distributed in the hope that it will be useful,
2347549Sdonn but WITHOUT ANY WARRANTY; without even the implied warranty of
2447549Sdonn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2547549Sdonn GNU General Public License for more details.
2647549Sdonn 
2747549Sdonn You should have received a copy of the GNU General Public License
2847549Sdonn along with GAS; see the file COPYING.  If not, write to
2947549Sdonn the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
3047549Sdonn 
3147549Sdonn /*
3247549Sdonn  * Confines all details of reading source bytes to this module.
3347549Sdonn  * All O/S specific crocks should live here.
3447549Sdonn  * What we lose in "efficiency" we gain in modularity.
3547549Sdonn  * Note we don't need to #include the "as.h" file. No common coupling!
3647549Sdonn  */
3747549Sdonn 
3847549Sdonn #define NDEBUG		/* JF remove asserts */
3947549Sdonn 
4047549Sdonn #ifdef USG
4147549Sdonn #define index strchr
4247549Sdonn /* JF:  What's the difference between _IOLBF and _IOFBF ? */
4347549Sdonn #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOFBF, (size))
4447549Sdonn #endif
4547549Sdonn 
4647549Sdonn #include <stdio.h>
4747549Sdonn #include <assert.h>
4847549Sdonn /* #include <sys/types.h>
4947549Sdonn #include <sys/stat.h>
5047549Sdonn #include <sys/file.h>
5147549Sdonn #include <sys/wait.h> */
5247549Sdonn 
5347549Sdonn /* #include "style.h" */
5447549Sdonn #include "input-file.h"
5547549Sdonn 
5647549Sdonn /* This variable is non-zero if the file currently being read should be
5747549Sdonn    preprocessed by app.  It is zero if the file can be read straight in.
5847549Sdonn  */
5947549Sdonn int preprocess = 0;
6047549Sdonn 
6147549Sdonn void	as_perror();
6247549Sdonn 
6347549Sdonn /*
6447549Sdonn  * This code opens a file, then delivers BUFFER_SIZE character
6547549Sdonn  * chunks of the file on demand.
6647549Sdonn  * BUFFER_SIZE is supposed to be a number chosen for speed.
6747549Sdonn  * The caller only asks once what BUFFER_SIZE is, and asks before
6847549Sdonn  * the nature of the input files (if any) is known.
6947549Sdonn  */
7047549Sdonn 
7147549Sdonn #define BUFFER_SIZE (32 * 1024)
7247549Sdonn 
7347549Sdonn static char in_buf[BUFFER_SIZE];
7447549Sdonn 
7547549Sdonn /*
7647549Sdonn  * We use static data: the data area is not sharable.
7747549Sdonn  */
7847549Sdonn 
7947549Sdonn FILE *f_in;	/* JF do things the RIGHT way */
8047549Sdonn /* static JF remove static so app.c can use file_name */
8147549Sdonn char *	file_name;
8247549Sdonn 
8347549Sdonn /* These hooks accomodate most operating systems. */
8447549Sdonn 
8547549Sdonn void
input_file_begin()8647549Sdonn input_file_begin ()
8747549Sdonn {
8847549Sdonn   /* file_handle = -1; */
8947549Sdonn   f_in = (FILE *)0;
9047549Sdonn }
9147549Sdonn 
9247549Sdonn void
input_file_end()9347549Sdonn input_file_end ()
9447549Sdonn {
9547549Sdonn }
9647549Sdonn 
9747549Sdonn int				/* Return BUFFER_SIZE. */
input_file_buffer_size()9847549Sdonn input_file_buffer_size ()
9947549Sdonn {
10047549Sdonn   return (BUFFER_SIZE);
10147549Sdonn }
10247549Sdonn 
10347549Sdonn int
input_file_is_open()10447549Sdonn input_file_is_open ()
10547549Sdonn {
10647549Sdonn   /* return (file_handle >= 0); */
10747549Sdonn   return f_in!=(FILE *)0;
10847549Sdonn }
10947549Sdonn 
11047549Sdonn #ifdef DONTDEF		/* JF save old version in case we need it */
11147549Sdonn void
input_file_open(filename,preprocess,debugging)11247549Sdonn input_file_open (filename, preprocess, debugging)
11347549Sdonn      char *	filename;	/* "" means use stdin. Must not be 0. */
11447549Sdonn      int	preprocess;	/* TRUE if needs app. */
11547549Sdonn      int	debugging;	/* TRUE if we are debugging assembler. */
11647549Sdonn {
11747549Sdonn   assert( filename != 0 );	/* Filename may not be NULL. */
11847549Sdonn   if (filename [0])
11947549Sdonn     {				/* We have a file name. Suck it and see. */
12047549Sdonn       file_handle = open (filename, O_RDONLY, 0);
12147549Sdonn       file_name = filename;
12247549Sdonn     }
12347549Sdonn   else
12447549Sdonn     {				/* use stdin for the input file. */
12547549Sdonn       file_handle = fileno (stdin);
12647549Sdonn       file_name = "{standard input}"; /* For error messages. */
12747549Sdonn     }
12847549Sdonn   if (file_handle < 0)
12947549Sdonn       as_perror ("Can't open %s for reading", file_name);
13047549Sdonn   if ( preprocess )
13147549Sdonn     {
13247549Sdonn /*
13347549Sdonn  * This code was written in haste for a frobbed BSD 4.2.
13447549Sdonn  * I have a flight to catch: will someone please do proper
13547549Sdonn  * error checks? - Dean.
13647549Sdonn  */
13747549Sdonn       int	pid;
13847549Sdonn       char temporary_file_name [12];
13947549Sdonn       int	fd;
14047549Sdonn       union wait	status;
14147549Sdonn       char	*mktemp();
14247549Sdonn 
14347549Sdonn       (void)strcpy (temporary_file_name, "#appXXXXXX");
14447549Sdonn       (void)mktemp (temporary_file_name);
14547549Sdonn       pid = vfork ();
14647549Sdonn       if (pid == -1)
14747549Sdonn 	{
14847549Sdonn 	  as_perror ("Vfork failed", file_name);
14947549Sdonn 	  _exit (144);
15047549Sdonn 	}
15147549Sdonn       if (pid == 0)
15247549Sdonn 	{
15347549Sdonn 	  (void)dup2 (file_handle, fileno(stdin));
15447549Sdonn 	  fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
15547549Sdonn 	  if (fd == -1)
15647549Sdonn 	    {
15747549Sdonn 	      (void)write(2,"Can't open temporary\n",21);
15847549Sdonn 	      _exit (99);
15947549Sdonn 	    }
16047549Sdonn 	  (void)dup2 (fd, fileno(stdout));
16147549Sdonn /* JF for testing #define PREPROCESSOR "/lib/app" */
16247549Sdonn #define PREPROCESSOR "./app"
16347549Sdonn 	  execl (PREPROCESSOR, PREPROCESSOR, 0);
16447549Sdonn 	  execl ("app","app",0);
16547549Sdonn 	  (void)write(2,"Exec of app failed.  Get help.\n",31);
16647549Sdonn 	  (void)unlink(temporary_file_name);
16747549Sdonn 	  _exit (11);
16847549Sdonn 	}
16947549Sdonn       (void)wait (& status);
17047549Sdonn       if (status.w_status & 0xFF00)		/* JF was 0xF000, was wrong */
17147549Sdonn 	{
17247549Sdonn 	  file_handle = -1;
17347549Sdonn 	  as_warn( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
17447549Sdonn 	}
17547549Sdonn       else
17647549Sdonn 	{
17747549Sdonn 	  file_handle = open (temporary_file_name, O_RDONLY, 0);
17847549Sdonn 	  if ( ! debugging && unlink(temporary_file_name))
17947549Sdonn 	    as_perror ("Can't delete temp file %s", temporary_file_name);
18047549Sdonn 	}
18147549Sdonn       if (file_handle == -1)
18247549Sdonn 	  as_perror ("Can't retrieve temp file %s", temporary_file_name);
18347549Sdonn     }
18447549Sdonn }
18547549Sdonn #else
18647549Sdonn 
18747549Sdonn void
input_file_open(filename,pre)18847549Sdonn input_file_open (filename,pre)
18947549Sdonn      char *	filename;	/* "" means use stdin. Must not be 0. */
19047549Sdonn      int pre;
19147549Sdonn {
19247549Sdonn 	int	c;
19347549Sdonn 	char	buf[80];
19447549Sdonn 
19547549Sdonn 	preprocess = pre;
19647549Sdonn 
19747549Sdonn 	assert( filename != 0 );	/* Filename may not be NULL. */
19847549Sdonn 	if (filename [0]) {	/* We have a file name. Suck it and see. */
19947549Sdonn 		f_in=fopen(filename,"r");
20047549Sdonn 		file_name=filename;
20147549Sdonn 	} else {			/* use stdin for the input file. */
20247549Sdonn 		f_in = stdin;
20347549Sdonn 		file_name = "{standard input}"; /* For error messages. */
20447549Sdonn 	}
20547549Sdonn 	if (f_in==(FILE *)0) {
20647549Sdonn 		as_perror ("Can't open %s for reading", file_name);
20747549Sdonn 		return;
20847549Sdonn 	}
20947549Sdonn #ifndef VMS
21047549Sdonn 	setbuffer(f_in,in_buf,BUFFER_SIZE);
21147549Sdonn #endif /* VMS */
21247549Sdonn 	c=getc(f_in);
21347549Sdonn 	if(c=='#') {	/* Begins with comment, may not want to preprocess */
21447549Sdonn 		c=getc(f_in);
21547549Sdonn 		if(c=='N') {
21647549Sdonn 			fgets(buf,80,f_in);
21747549Sdonn 			if(!strcmp(buf,"O_APP\n"))
21847549Sdonn 				preprocess=0;
21947549Sdonn 			if(!index(buf,'\n'))
22047549Sdonn 				ungetc('#',f_in);	/* It was longer */
22147549Sdonn 			else
22247549Sdonn 				ungetc('\n',f_in);
22347549Sdonn 		} else if(c=='\n')
22447549Sdonn 			ungetc('\n',f_in);
22547549Sdonn 		else
22647549Sdonn 			ungetc('#',f_in);
22747549Sdonn 	} else
22847549Sdonn 		ungetc(c,f_in);
22947549Sdonn 
23047549Sdonn #ifdef DONTDEF
23147549Sdonn 	if ( preprocess ) {
23247549Sdonn 		char temporary_file_name [17];
23347549Sdonn 		char	*mktemp();
23447549Sdonn 		FILE	*f_out;
23547549Sdonn 
23647549Sdonn 		(void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
23747549Sdonn 		(void)mktemp (temporary_file_name);
23847549Sdonn 		f_out=fopen(temporary_file_name,"w+");
23947549Sdonn 		if(f_out==(FILE *)0)
24047549Sdonn 			as_perror("Can't open temp file %s",temporary_file_name);
24147549Sdonn 
24247549Sdonn 			/* JF this will have to be moved on any system that
24347549Sdonn 			   does not support removal of open files.  */
24447549Sdonn 		(void)unlink(temporary_file_name);/* JF do it NOW */
24547549Sdonn 		do_scrub(f_in,f_out);
24647549Sdonn 		(void)fclose(f_in);	/* All done with it */
24747549Sdonn 		(void)rewind(f_out);
24847549Sdonn 		f_in=f_out;
24947549Sdonn 	}
25047549Sdonn #endif
25147549Sdonn }
25247549Sdonn #endif
25347549Sdonn 
25447549Sdonn char *
input_file_give_next_buffer(where)25547549Sdonn input_file_give_next_buffer (where)
25647549Sdonn      char *		where;	/* Where to place 1st character of new buffer. */
25747549Sdonn {
25847549Sdonn   char *	return_value;	/* -> Last char of what we read, + 1. */
25947549Sdonn   register int	size;
26047549Sdonn 
26147549Sdonn   if (f_in == (FILE *)0)
26247549Sdonn       return 0;
26347549Sdonn       /*
26447549Sdonn        * fflush (stdin); could be done here if you want to synchronise
26547549Sdonn        * stdin and stdout, for the case where our input file is stdin.
26647549Sdonn        * Since the assembler shouldn't do any output to stdout, we
26747549Sdonn        * don't bother to synch output and input.
26847549Sdonn        */
26947549Sdonn   /* size = read (file_handle, where, BUFFER_SIZE); */
27047549Sdonn   if(preprocess) {
27147549Sdonn 	char *p;
27247549Sdonn 	int n;
27347549Sdonn 	int ch;
27447549Sdonn 	extern FILE *scrub_file;
27547549Sdonn 	int scrub_from_file();
27647549Sdonn 	void scrub_to_file();
27747549Sdonn 	int do_scrub_next_char();
27847549Sdonn 
27947549Sdonn 	scrub_file=f_in;
28047549Sdonn 	for(p=where,n=BUFFER_SIZE;n;--n) {
28147549Sdonn 		ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
28247549Sdonn 		if(ch==EOF)
28347549Sdonn 			break;
28447549Sdonn 		*p++=ch;
28547549Sdonn 	}
28647549Sdonn 	size=BUFFER_SIZE-n;
28747549Sdonn   } else
28847549Sdonn 	size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
28947549Sdonn   if (size < 0)
29047549Sdonn     {
29147549Sdonn       as_perror ("Can't read from %s", file_name);
29247549Sdonn       size = 0;
29347549Sdonn     }
29447549Sdonn   if (size)
29547549Sdonn     return_value = where + size;
29647549Sdonn   else
29747549Sdonn     {
29847549Sdonn       if (fclose (f_in))
29947549Sdonn 	as_perror ("Can't close %s", file_name);
30047549Sdonn       f_in = (FILE *)0;
30147549Sdonn       return_value = 0;
30247549Sdonn     }
30347549Sdonn   return (return_value);
30447549Sdonn }
30547549Sdonn 
30647549Sdonn /* end: input_file.c */
307