Index: driver/cc1.cc
===================================================================
--- driver/cc1.cc	(revision 6dc19544a8ab55bf4ec65a6beabc94ee9570549a)
+++ driver/cc1.cc	(revision 7c8246d867f7b610be8221b2a38ff71d995b33e8)
@@ -10,6 +10,6 @@
 // Created On       : Fri Aug 26 14:23:51 2005
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Mon Sep  3 16:57:05 2018
-// Update Count     : 125
+// Last Modified On : Fri Aug 30 15:36:42 2019
+// Update Count     : 377
 //
 
@@ -19,8 +19,11 @@
 #include <string>
 using std::string;
+#include <algorithm>									// find
 #include <cstdio>										// stderr, stdout, perror, fprintf
 #include <cstdlib>										// getenv, exit, mkstemp
 #include <unistd.h>										// execvp, fork, unlink
 #include <sys/wait.h>									// wait
+#include <fcntl.h>
+
 
 #include "config.h"										// configure info
@@ -30,111 +33,122 @@
 
 
-string compiler_name( CFA_BACKEND_CC );					// path/name of C compiler
-
-string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
-string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
-
-char tmpname[] = P_tmpdir "/CFAXXXXXX";
-int tmpfilefd = -1;
-
-
-bool prefix( string arg, string pre ) {
+static string compiler_path( CFA_BACKEND_CC );			// C compiler path/name
+static bool CFA_flag = false;							// -CFA flag
+static bool save_temps = false;							// -save-temps flag
+static string o_file;
+static string bprefix;
+
+
+static bool prefix( const string & arg, const string & pre ) {
 	return arg.substr( 0, pre.size() ) == pre;
 } // prefix
 
-enum { NumSuffixes = 2 };
-const string suffixes[NumSuffixes] = { "cfa", "hfa", };
-
-void suffix( string arg, const char * args[], int & nargs ) {
-	//std::cerr << arg << std::endl;
+static void suffix( const string & arg, const char * args[], int & nargs ) {
+	enum { NumSuffixes = 3 };
+	static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
+
 	size_t dot = arg.find_last_of( "." );
-	//std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
 	if ( dot == string::npos ) return;
-	string sx = arg.substr( dot + 1 );
-	for ( int i = 0; i < NumSuffixes; i += 1 ) {
-		if ( sx == suffixes[i] ) {
-			args[nargs] = "-x";
-			nargs += 1;
-			args[nargs] = "c";
-			nargs += 1;
-			return;
-		} // if
-	} // for
+	const string * end = suffixes + NumSuffixes;
+	if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) {
+		args[nargs++] = "-x";
+		args[nargs++] = "c";
+	} // if
 } // suffix
 
 
-void checkEnv( const char * args[], int & nargs ) {
-	char *value;
-
-	value = getenv( "__CFA_COMPILER__" );
-	if ( value != NULL ) {
-		compiler_name = value;
-		#ifdef __DEBUG_H__
-		cerr << "env arg:\"" << compiler_name << "\"" << endl;
-		#endif // __DEBUG_H__
-	} // if
-
-	value = getenv( "__GCC_MACHINE__" );
-	if ( value != NULL ) {
-		args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
-		#ifdef __DEBUG_H__
-		cerr << "env arg:\"" << args[nargs] << "\"" << endl;
-		#endif // __DEBUG_H__
-		nargs += 1;
-	} // if
-
-	value = getenv( "__GCC_VERSION__" );
-	if ( value != NULL ) {
-		args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
-		#ifdef __DEBUG_H__
-		cerr << "env arg:\"" << args[nargs] << "\"" << endl;
-		#endif // __DEBUG_H__
-		nargs += 1;
-	} // if
-} // checkEnv
-
-
-void rmtmpfile() {
+static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
+
+static void checkEnv1( const char * args[], int & nargs ) { // stage 1
+	extern char ** environ;
+
+	for ( int i = 0; environ[i]; i += 1 ) {
+		string arg( environ[i] );
+		#ifdef __DEBUG_H__
+		cerr << "env arg:\"" << arg << "\"" << endl;
+		#endif // __DEBUG_H__
+
+		if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
+			string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
+			if ( prefix( val, "-compiler=" ) ) {
+				compiler_path = val.substr( 10 );
+			} // if
+		} // if
+	} // for
+} // checkEnv1
+
+
+static void checkEnv2( const char * args[], int & nargs ) { // stage 2
+	extern char ** environ;
+
+	for ( int i = 0; environ[i]; i += 1 ) {
+		string arg( environ[i] );
+		#ifdef __DEBUG_H__
+		cerr << "env arg:\"" << arg << "\"" << endl;
+		#endif // __DEBUG_H__
+
+		if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
+			string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
+			if ( prefix( val, "-compiler=" ) ) {
+				compiler_path = val.substr( 10 );
+			} else if ( val == "-CFA" ) {
+				CFA_flag = true;
+			} else if ( val == "-save-temps" ) {
+				save_temps = true;
+			} else if ( prefix( val, "-o=" ) ) {		// output file for -CFA
+				o_file = val.substr( 3 );
+			} else if ( prefix( val, "-B=" ) ) {		// location of cfa-cpp
+				bprefix = val.substr( 3 );
+			} else {									// normal flag for cfa-cpp
+				args[nargs++] = ( *new string( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ) ).c_str();
+			} // if
+		} // if
+	} // for
+} // checkEnv2
+
+
+static char tmpname[] = P_tmpdir "/CFAXXXXXX.i";
+static int tmpfilefd = -1;
+static bool startrm = false;
+
+static void rmtmpfile() {
+	if ( tmpfilefd == -1 ) return;						// RACE, file created ?
+
+	startrm = true;										// RACE with C-c C-c
 	if ( unlink( tmpname ) == -1 ) {					// remove tmpname
-		perror ( "CFA Translator error: cpp failed" );
-		exit( EXIT_FAILURE );
-	} // if
-	tmpfilefd = -1;										// mark closed
+		perror ( "CC1 Translator error: failed, unlink" );
+		exit( EXIT_FAILURE );
+	} // if
+	tmpfilefd = -1;										// mark removed
 } // rmtmpfile
 
 
-void sigTermHandler( __attribute__((unused)) int signal ) {
+static void sigTermHandler( int ) {						// C-c C-c
+	if ( startrm ) return;								// return and let rmtmpfile finish, and then program finishes
+
 	if ( tmpfilefd != -1 ) {							// RACE, file created ?
-		rmtmpfile();									// remove
-		exit( EXIT_FAILURE );							// terminate
-	} // if
+		rmtmpfile();									// remove tmpname
+	} // if
+	exit( EXIT_FAILURE );								// terminate
 } // sigTermHandler
 
 
-void Stage1( const int argc, const char * const argv[] ) {
+static void Stage1( const int argc, const char * const argv[] ) {
 	int code;
-
 	string arg;
-	string bprefix;
-
-	const char *cpp_in = NULL;
-	const char *cpp_out = NULL;
-
-	bool CFA_flag = false;
+
+	const char * cpp_in = nullptr;
+	const char * cpp_out = nullptr;
+
 	bool cpp_flag = false;
-	const char *o_name = NULL;
-
-	const char *args[argc + 100];						// leave space for 100 additional cpp command line values
+	bool o_flag = false;
+
+	const char * args[argc + 100];						// leave space for 100 additional cpp command line values
 	int nargs = 1;										// number of arguments in args list; 0 => command name
-	const char *cargs[20];								// leave space for 20 additional cfa-cpp command line values
-	int ncargs = 1;										// 0 => command name
-
-	signal( SIGINT,  sigTermHandler );
-	signal( SIGTERM, sigTermHandler );
 
 	#ifdef __DEBUG_H__
 	cerr << "Stage1" << endl;
 	#endif // __DEBUG_H__
-	checkEnv( args, nargs );							// arguments passed via environment variables
+	checkEnv1( args, nargs );							// arguments passed via environment variables
 	#ifdef __DEBUG_H__
 	for ( int i = 1; i < argc; i += 1 ) {
@@ -168,30 +182,13 @@
 				i += 1;									// and the argument
 				cpp_flag = true;
-			} else if ( arg == "-D__CFA_PREPROCESS__" ) {
-				CFA_flag = true;
-			} else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
-				i += 1;									// and the argument
-				CFA_flag = true;
-			} else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
-				cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
-				ncargs += 1;
-			} else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
-				cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
-				ncargs += 1;
-				i += 1;									// and the argument
-			} else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
-				bprefix = arg.substr( D__GCC_BPREFIX__.size() );
-			} else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {
-				bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );
-				i += 1;									// and the argument
-
-			// all other flags
+
+				// all other flags
 
 			} else if ( arg == "-o" ) {
 				i += 1;
-				o_name = argv[i];
+				o_flag = true;
+				cpp_out = argv[i];
 			} else {
-				args[nargs] = argv[i];					// pass the flag along
-				nargs += 1;
+				args[nargs++] = argv[i];				// pass the flag along
 				// CPP flags with an argument
 				if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
@@ -199,15 +196,12 @@
 					 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
 					i += 1;
-					args[nargs] = argv[i];				// pass the argument along
-					nargs += 1;
+					args[nargs++] = argv[i];			// pass the argument along
 					#ifdef __DEBUG_H__
 					cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
 					#endif // __DEBUG_H__
 				} else if ( arg == "-MD" || arg == "-MMD" ) {
-					args[nargs] = "-MF";				// insert before file
-					nargs += 1;
+					args[nargs++] = "-MF";				// insert before file
 					i += 1;
-					args[nargs] = argv[i];				// pass the argument along
-					nargs += 1;
+					args[nargs++] = argv[i];			// pass the argument along
 					#ifdef __DEBUG_H__
 					cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
@@ -216,10 +210,10 @@
 			} // if
 		} else {										// obtain input and possibly output files
-			if ( cpp_in == NULL ) {
+			if ( cpp_in == nullptr ) {
 				cpp_in = argv[i];
 				#ifdef __DEBUG_H__
 				cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
 				#endif // __DEBUG_H__
-			} else if ( cpp_out == NULL ) {
+			} else if ( cpp_out == nullptr ) {
 				cpp_out = argv[i];
 				#ifdef __DEBUG_H__
@@ -238,10 +232,10 @@
 		cerr << " " << args[i];
 	} // for
-	if ( cpp_in != NULL ) cerr << " " << cpp_in;
-	if ( cpp_out != NULL ) cerr << " " << cpp_out;
+	if ( cpp_in != nullptr ) cerr << " " << cpp_in;
+	if ( cpp_out != nullptr ) cerr << " " << cpp_out;
 	cerr << endl;
 	#endif // __DEBUG_H__
 
-	if ( cpp_in == NULL ) {
+	if ( cpp_in == nullptr ) {
 		cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
 		exit( EXIT_FAILURE );
@@ -252,19 +246,16 @@
 		// output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
 
-		args[0] = compiler_name.c_str();
+		args[0] = compiler_path.c_str();
 		suffix( cpp_in, args, nargs );					// check suffix
-		args[nargs] = cpp_in;
-		nargs += 1;
-		if ( o_name != NULL ) {							// location for output
-			args[nargs] = "-o";
-			nargs += 1;
-			args[nargs] = o_name;
-			nargs += 1;
-		} // if
-		args[nargs] = NULL;								// terminate argument list
+		args[nargs++] = cpp_in;
+		if ( o_flag ) {									// location for output
+			args[nargs++] = "-o";
+		} // if
+		args[nargs++] = cpp_out;
+		args[nargs] = nullptr;							// terminate argument list
 
 		#ifdef __DEBUG_H__
 		cerr << "nargs: " << nargs << endl;
-		for ( int i = 0; args[i] != NULL; i += 1 ) {
+		for ( int i = 0; args[i] != nullptr; i += 1 ) {
 			cerr << args[i] << " ";
 		} // for
@@ -272,22 +263,10 @@
 		#endif // __DEBUG_H__
 
-		execvp( args[0], (char *const *)args );			// should not return
-		perror( "CFA Translator error: cpp level, execvp" );
-		exit( EXIT_FAILURE );
-	} // if
-
-	// Create a temporary file to store output of the C preprocessor.
-
-	tmpfilefd = mkstemp( tmpname );
-	if ( tmpfilefd == -1 ) {
-		perror( "CFA Translator error: cpp level, mkstemp" );
-		exit( EXIT_FAILURE );
-	} // if
-
-	#ifdef __DEBUG_H__
-	cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
-	#endif // __DEBUG_H__
-
-	// Run the C preprocessor and save the output in tmpfile.
+		execvp( args[0], (char * const *)args );		// should not return
+		perror( "CC1 Translator error: stage 1, execvp" );
+		exit( EXIT_FAILURE );
+	} // if
+
+	// Run the C preprocessor and save the output in the given file.
 
 	if ( fork() == 0 ) {								 // child process ?
@@ -295,18 +274,17 @@
 		// an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
 		// when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
-		if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
-			perror( "CFA Translator error: cpp level, freopen" );
+		if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
+			perror( "CC1 Translator error: stage 1, freopen" );
 			exit( EXIT_FAILURE );
 		} // if
 
-		args[0] = compiler_name.c_str();
+		args[0] = compiler_path.c_str();
 		suffix( cpp_in, args, nargs );					// check suffix
-		args[nargs] = cpp_in;							// input to cpp
-		nargs += 1;
-		args[nargs] = NULL;								// terminate argument list
+		args[nargs++] = cpp_in;							// input to cpp
+		args[nargs] = nullptr;							// terminate argument list
 
 		#ifdef __DEBUG_H__
 		cerr << "cpp nargs: " << nargs << endl;
-		for ( int i = 0; args[i] != NULL; i += 1 ) {
+		for ( int i = 0; args[i] != nullptr; i += 1 ) {
 			cerr << args[i] << " ";
 		} // for
@@ -314,6 +292,7 @@
 		#endif // __DEBUG_H__
 
-		execvp( args[0], (char *const *)args );			// should not return
-		perror( "CFA Translator error: cpp level, execvp" );
+		execvp( args[0], (char * const *)args );		// should not return
+		perror( "CC1 Translator error: stage 1 cpp, execvp" );
+		cerr << " invoked " << args[0] << endl;
 		exit( EXIT_FAILURE );
 	} // if
@@ -325,81 +304,29 @@
 	#endif // __DEBUG_H__
 
-	if ( WIFSIGNALED(code) != 0 ) {						// child failed ?
-		rmtmpfile();									// remove tmpname
-		cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl;
-		exit( EXIT_FAILURE );
-	} // if
-
-	if ( WEXITSTATUS(code) != 0 ) {						// child error ?
-		rmtmpfile();									// remove tmpname
-		exit( WEXITSTATUS( code ) );					// do not continue
-	} // if
-
-	// If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
-	// output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
-
-	if ( fork() == 0 ) {								// child runs CFA
-		cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
-
-		// Source file-name used to generate routine names containing global initializations for TU.
-		cargs[ncargs] = ( *new string( "-F" ) ).c_str();
-		ncargs += 1;
-		cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
-		ncargs += 1;
-
-		cargs[ncargs] = tmpname;
-		ncargs += 1;
-		if ( o_name != NULL ) {
-			cargs[ncargs] = o_name;
-			ncargs += 1;
-		} else if ( ! CFA_flag ) {						// run cfa-cpp ?
-			cargs[ncargs] = cpp_out;
-			ncargs += 1;
-		} // if
-		cargs[ncargs] = NULL;							// terminate argument list
-
-		#ifdef __DEBUG_H__
-		cerr << "cfa-cpp ncargs: " << (o_name ? o_name : "No -o") << " " << CFA_flag << " " << ncargs << endl;
-		for ( int i = 0; cargs[i] != NULL; i += 1 ) {
-			cerr << cargs[i] << " ";
-		} // for
-		cerr << endl;
-		#endif // __DEBUG_H__
-
-		execvp( cargs[0], (char * const *)cargs );		// should not return
-		perror( "CFA Translator error: cpp level, execvp" );
-		exit( EXIT_FAILURE );
-	} // if
-
-	wait( &code );										// wait for child to finish
-
-	#ifdef __DEBUG_H__
-	cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
-	#endif // __DEBUG_H__
-
-	// Must unlink here because file must exist across execvp.
-	rmtmpfile();										// remove tmpname
-
 	if ( WIFSIGNALED(code) ) {							// child failed ?
-		cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
-		exit( EXIT_FAILURE );
-	} // if
-
-	exit( WEXITSTATUS(code) );
+		cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
+		exit( EXIT_FAILURE );
+	} // if
+
+	exit( WEXITSTATUS(code) );							// bad cpp result stops top-level gcc
 } // Stage1
 
 
-void Stage2( const int argc, const char * const * argv ) {
+static void Stage2( const int argc, const char * const * argv ) {
+	int code;
 	string arg;
 
-	const char *cpp_in = NULL;
-
-	const char *args[argc + 100];						// leave space for 100 additional cfa command line values
+	const char * cpp_in = nullptr;
+	const char * cpp_out = nullptr;
+
+	const char * args[argc + 100];						// leave space for 100 additional cfa command line values
 	int nargs = 1;										// number of arguments in args list; 0 => command name
+	const char * cargs[20];								// leave space for 20 additional cfa-cpp command line values
+	int ncargs = 1;										// 0 => command name
 
 	#ifdef __DEBUG_H__
 	cerr << "Stage2" << endl;
 	#endif // __DEBUG_H__
-	checkEnv( args, nargs );							// arguments passed via environment variables
+	checkEnv2( cargs, ncargs );							// arguments passed via environment variables
 	#ifdef __DEBUG_H__
 	for ( int i = 1; i < argc; i += 1 ) {
@@ -430,10 +357,9 @@
 
 			} else {
-				args[nargs] = argv[i];					// pass the flag along
-				nargs += 1;
+				args[nargs++] = argv[i];				// pass the flag along
 				if ( arg == "-o" ) {
 					i += 1;
-					args[nargs] = argv[i];				// pass the argument along
-					nargs += 1;
+					cpp_out = argv[i];
+					args[nargs++] = argv[i];			// pass the argument along
 					#ifdef __DEBUG_H__
 					cerr << "arg:\"" << argv[i] << "\"" << endl;
@@ -442,15 +368,111 @@
 			} // if
 		} else {										// obtain input and possibly output files
-			if ( cpp_in == NULL ) {
+			if ( cpp_in == nullptr ) {
 				cpp_in = argv[i];
 				#ifdef __DEBUG_H__
 				cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
 				#endif // __DEBUG_H__
+			} else if ( cpp_out == nullptr ) {
+				cpp_out = argv[i];
+				#ifdef __DEBUG_H__
+				cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
+				#endif // __DEBUG_H__
 			} else {
-				cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
+				cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
 				exit( EXIT_FAILURE );
 			} // if
 		} // if
 	} // for
+
+	if ( cpp_in == nullptr ) {
+		cerr << "Usage: " << argv[0] << " missing input file" << endl;
+		exit( EXIT_FAILURE );
+	} // if
+	if ( cpp_out == nullptr ) {
+		cerr << "Usage: " << argv[0] << " missing output file" << endl;
+		exit( EXIT_FAILURE );
+	} // if
+
+	// Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
+	// process because variables tmpname and tmpfilefd are cloned.
+
+	string cfa_cpp_out;
+
+	if ( ! CFA_flag ) {									// run compiler ?
+		if ( save_temps ) {
+			cfa_cpp_out = cpp_in;
+			size_t dot = cfa_cpp_out.find_last_of( "." );
+			if ( dot == string::npos ) {
+				cerr << "CC1 Translator error: stage 2, bad file name " << endl;
+				exit( EXIT_FAILURE );
+			} // if
+
+			cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa";
+			if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {
+				perror( "CC1 Translator error: stage 2, creat" );
+				exit( EXIT_FAILURE );
+			} // if
+		} else {
+			tmpfilefd = mkstemps( tmpname, 2 );
+			if ( tmpfilefd == -1 ) {
+				perror( "CC1 Translator error: stage 2, mkstemp" );
+				exit( EXIT_FAILURE );
+			} // if
+			cfa_cpp_out = tmpname;
+		} // if
+		#ifdef __DEBUG_H__
+		cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
+		#endif // __DEBUG_H__
+	} // if
+
+	// If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
+	// output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
+
+	if ( fork() == 0 ) {								// child runs CFA
+		cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
+		cargs[ncargs++] = cpp_in;
+
+		if ( CFA_flag ) {								// run cfa-cpp ?
+			if ( o_file.size() != 0 ) {					// location for output
+				cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();
+			} // if
+		} else {
+			cargs[ncargs++] = cfa_cpp_out.c_str();
+		} // if
+		cargs[ncargs] = nullptr;						// terminate argument list
+
+		#ifdef __DEBUG_H__
+		for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
+			cerr << cargs[i] << " ";
+		} // for
+		cerr << endl;
+		#endif // __DEBUG_H__
+
+		execvp( cargs[0], (char * const *)cargs );		// should not return
+		perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" );
+		cerr << " invoked " << cargs[0] << endl;
+		exit( EXIT_FAILURE );
+	} // if
+
+	wait( &code );										// wait for child to finish
+
+	if ( WIFSIGNALED(code) ) {							// child failed ?
+		rmtmpfile();									// remove tmpname
+		cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
+		exit( EXIT_FAILURE );
+	} // if
+
+	if ( CFA_flag ) {									// no tmpfile created
+		exit( WEXITSTATUS( code ) );					// stop regardless of success or failure
+	} // if
+
+	#ifdef __DEBUG_H__
+	cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
+	#endif // __DEBUG_H__
+
+	if ( WEXITSTATUS(code) ) {							// child error ?
+		rmtmpfile();									// remove tmpname
+		exit( WEXITSTATUS( code ) );					// do not continue
+	} // if
 
 	#ifdef __DEBUG_H__
@@ -459,37 +481,64 @@
 		cerr << " " << args[i];
 	} // for
-	cerr << endl;
-	if ( cpp_in != NULL ) cerr << " " << cpp_in;
-	#endif // __DEBUG_H__
-
-	args[0] = compiler_name.c_str();
-	args[nargs] = "-S";									// only compile and put assembler output in specified file
-	nargs += 1;
-	args[nargs] = cpp_in;
-	nargs += 1;
-	args[nargs] = NULL;									// terminate argument list
-
-	#ifdef __DEBUG_H__
-	cerr << "stage2 nargs: " << nargs << endl;
-	for ( int i = 0; args[i] != NULL; i += 1 ) {
-		cerr << args[i] << " ";
-	} // for
-	cerr << endl;
-	#endif // __DEBUG_H__
-
-	execvp( args[0], (char * const *)args );			// should not return
-	perror( "CFA Translator error: cpp level, execvp" );
-	exit( EXIT_FAILURE );								// tell gcc not to go any further
+	cerr << " " << cpp_in << endl;
+	#endif // __DEBUG_H__
+
+	if ( fork() == 0 ) {								// child runs CFA
+		args[0] = compiler_path.c_str();
+		args[nargs++] = "-S";							// only compile and put assembler output in specified file
+		if ( save_temps ) {								// make gcc accept .ifa suffix
+			args[nargs++] = "-x";
+			args[nargs++] = "cpp-output";
+		} // if
+		args[nargs++] = cfa_cpp_out.c_str();
+		args[nargs] = nullptr;							// terminate argument list
+
+		#ifdef __DEBUG_H__
+		cerr << "stage2 nargs: " << nargs << endl;
+		for ( int i = 0; args[i] != nullptr; i += 1 ) {
+			cerr << args[i] << " ";
+		} // for
+		cerr << endl;
+		#endif // __DEBUG_H__
+
+		execvp( args[0], (char * const *)args );		// should not return
+		perror( "CC1 Translator error: stage 2 cc1, execvp" );
+		cerr << " invoked " << cargs[0] << endl;
+		exit( EXIT_FAILURE );							// tell gcc not to go any further
+	} // if
+
+	wait( &code );										// wait for child to finish
+
+	if ( WIFSIGNALED(code) ) {							// child failed ?
+		rmtmpfile();									// remove tmpname
+		cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
+		exit( EXIT_FAILURE );
+	} // if
+
+	#ifdef __DEBUG_H__
+	cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
+	#endif // __DEBUG_H__
+
+	rmtmpfile();										// remove tmpname
+	exit( WEXITSTATUS( code ) );						// stop regardless of success or failure
 } // Stage2
 
 
+// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
+// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
+// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
+// doubly preprocessed program.
+
 int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
 	#ifdef __DEBUG_H__
-	for ( int i = 0; env[i] != NULL; i += 1 ) {
+	for ( int i = 0; env[i] != nullptr; i += 1 ) {
 		cerr << env[i] << endl;
 	} // for
 	#endif // __DEBUG_H__
 
-	string arg = argv[1];
+	signal( SIGINT,  sigTermHandler );
+	signal( SIGTERM, sigTermHandler );
+
+	string arg( argv[1] );
 
 	// Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
