[m-rev.] for review: add `-o' option to c2init

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Apr 27 03:18:47 AEST 2001


Another tiny step towards eliminating dependencies on Cygwin.

----------

Estimated hours taken: 2.5

Fix a bug in c2init where, if an error occurred, it would leave the
invalid output file around, causing problems the next time you ran Mmake.

This change also eliminates a dependency on using shell redirection (">")
when invoking system commands.

util/mkinit.c:
	Add new option `-o' for specifying the output file name.
	Also, if this option is specified, and there are any errors,
	then remove the output file. This fixes the bug mentioned above.
	Also, consistently use exit(EXIT_FAILURE) rather than exit(1).

scripts/c2init.in:
	Add new option `-o'/`--output', which just passes `-o' to mkinit.

compiler/modules.m:
compiler/mercury_compile.m:
	Use `-o' rather than shell redirection when invoking c2init.

Workspace: /home/mars/fjh/ws2/mercury
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.198
diff -u -d -r1.198 mercury_compile.m
--- compiler/mercury_compile.m	2001/03/23 11:09:21	1.198
+++ compiler/mercury_compile.m	2001/04/26 17:13:50
@@ -3080,7 +3080,7 @@
 	    ;
 		TraceOpt = ""
 	    },
-	    join_module_list(Modules, ".c", ["> ", InitCFileName], MkInitCmd0),
+	    join_module_list(Modules, ".c", ["-o ", InitCFileName], MkInitCmd0),
 	    { string__append_list(["c2init ", TraceOpt | MkInitCmd0],
 	    	MkInitCmd) },
 	    invoke_shell_command(MkInitCmd, MkInitOK),
Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.158
diff -u -d -r1.158 modules.m
--- compiler/modules.m	2001/04/08 08:59:21	1.158
+++ compiler/modules.m	2001/04/26 08:17:50
@@ -3042,7 +3042,7 @@
 		InitCFileName, " : ", DepFileName, " ", DvFileName, " ",
 			All_C2InitArgsDepString, "\n",
 		"\t$(C2INIT) $(ALL_GRADEFLAGS) $(ALL_C2INITFLAGS) $(",
-			MakeVarName, ".init_cs) $(ALL_C2INITARGS) > ",
+			MakeVarName, ".init_cs) $(ALL_C2INITARGS) -o ",
 			InitCFileName, "\n\n"
 	]),
 
Index: scripts/c2init.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/c2init.in,v
retrieving revision 1.30
diff -u -d -r1.30 c2init.in
--- scripts/c2init.in	2000/11/05 04:15:34	1.30
+++ scripts/c2init.in	2001/04/26 08:01:48
@@ -33,6 +33,7 @@
 extra_inits_opt=""
 aditi_opt=""
 extra_init_dirs=""
+output_file="-"
 
 # include the file `init_grade_options.sh-subr'
 @INIT_GRADE_OPTIONS@
@@ -69,6 +70,9 @@
 	-I <directory>, --init-file-directory <directory>
 		Include <directory> in the list of directories searched to
 		locate \`.init' files.
+	-o <filename>, --output <filename>
+		Output the generated C program to the specified file,
+		rather than sending it to the standard output.
 	-w <label>, --entry-point <label>
 		Set entry point to <label>.
 		(Default value corresponds to main/2.)
@@ -109,6 +113,9 @@
 
 	-I|--init-file-directory)
 		extra_init_dirs="$extra_init_dirs -I $2"; shift;;
+	
+	-o|--output)
+		output_file="$2"; shift;;
 
 	-w|--entry-point)
 		defentry_opt="-w$2"; shift;;
@@ -174,10 +181,12 @@
 case $# in
 	0) exec $MKINIT $aditi_opt -c"$maxcalls" $init_opt $trace_opt \
 		$library_opt $defentry_opt $extra_inits_opt \
+		-o "$output_file" \
 		$extra_init_dirs $EXTRA_INIT_FILES $MERCURY_ALL_LIB_MODS
 	   ;;
 	*) exec $MKINIT $aditi_opt -c"$maxcalls" $init_opt $trace_opt \
 		$library_opt $defentry_opt $extra_inits_opt \
+		-o "$output_file" \
 		$extra_init_dirs "$@" $EXTRA_INIT_FILES $MERCURY_ALL_LIB_MODS
 	   ;;
 esac
Index: util/mkinit.c
===================================================================
RCS file: /home/mercury1/repository/mercury/util/mkinit.c,v
retrieving revision 1.74
diff -u -d -r1.74 mkinit.c
--- util/mkinit.c	2001/04/09 16:38:09	1.74
+++ util/mkinit.c	2001/04/26 08:26:28
@@ -54,6 +54,7 @@
 static const char *MR_progname = NULL;
 
 /* options and arguments, set by parse_options() */
+static const char *output_file_name = NULL;
 static const char *entry_point = "mercury__main_2_0";
 static const char *hl_entry_point = "main_2_p_0";
 static int maxcalls = MAXCALLS;
@@ -260,6 +261,7 @@
 /* --- function prototypes --- */
 static	void	parse_options(int argc, char *argv[]);
 static	void	usage(void);
+static	void	set_output_file(void);
 static	void	do_path_search(void);
 static	char	*find_init_file(const char *basename);
 static	bool	file_exists(const char *filename);
@@ -324,6 +326,8 @@
 
 	parse_options(argc, argv);
 
+	set_output_file();
+
 	do_path_search();
 	output_headers();
 
@@ -348,6 +352,10 @@
 		fputs("/* Force syntax error, since there were */\n", stdout);
 		fputs("/* errors in the generation of this file */\n", stdout);
 		fputs("#error \"You need to remake this file\"\n", stdout);
+		if (output_file_name != NULL) {
+			(void) fclose(stdout);
+			(void) remove(output_file_name);
+		}
 		return EXIT_FAILURE;
 	}
 
@@ -363,7 +371,7 @@
 	int		i;
 	String_List	*tmp_slist;
 
-	while ((c = getopt(argc, argv, "ac:iI:ltw:x")) != EOF) {
+	while ((c = getopt(argc, argv, "ac:iI:lo:tw:x")) != EOF) {
 		switch (c) {
 		case 'a':
 			aditi = TRUE;
@@ -397,6 +405,14 @@
 			output_main_func = FALSE;
 			break;
 
+		case 'o':
+			if (strcmp(optarg, "-") == 0) {
+				output_file_name = NULL; /* output to stdout */
+			} else {
+				output_file_name = optarg;
+			}
+			break;
+
 		case 't':
 			need_tracing = TRUE;
 			need_initialization_code = TRUE;
@@ -427,12 +443,35 @@
 usage(void)
 {
 	fprintf(stderr,
-"Usage: mkinit [-a] [-c maxcalls] [-w entry] [-i] [-l] [-t] [-x] files...\n");
-	exit(1);
+"Usage: mkinit [options] files...\n"
+"Options: [-a] [-c maxcalls] [-o filename] [-w entry] [-i] [-l] [-t] [-x]\n");
+	exit(EXIT_FAILURE);
 }
 
 /*---------------------------------------------------------------------------*/
 
+/*
+** If the `-o' option was used to specify the output file,
+** and the file name specified is not `-' (which we take to mean stdout),
+** then reassign stdout to the specified file.
+*/
+static void
+set_output_file(void)
+{
+	if (output_file_name != NULL) {
+		FILE *result = freopen(output_file_name, "w", stdout);
+		if (result == NULL) {
+			fprintf(stderr,
+				"%s: error opening output file `%s': %s\n",
+				MR_progname, output_file_name,
+				strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+	}
+}
+
+/*---------------------------------------------------------------------------*/
+
 	/*
 	** Scan the list of files for ones not found in the current
 	** directory, and replace them with their full path equivalent
@@ -681,7 +720,7 @@
 				"special characters are not supported.\n");
 			fprintf(stderr, "File name `%s' contains special "
 				"character `%c'.\n", filename, filename[i]);
-			exit(1);
+			exit(EXIT_FAILURE);
 		}
 	}
 
@@ -949,7 +988,7 @@
 	void *mem;
 	if ((mem = malloc(size)) == NULL) {
 		fprintf(stderr, "Out of memory\n");
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 	return mem;
 }

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list