[m-rev.] for review: avoid unistd.h reference in runtime headers

Simon Taylor stayl at cs.mu.OZ.AU
Fri Nov 8 17:38:45 AEDT 2002


Estimated hours taken: 1
Branches: main

runtime/mercury_trace_base.{c,h}:
trace/mercury_trace_internal.c:
	Move the code to kill the xterm created for `mdb --window'
	into mercury_trace_internal.c to avoid references to the
	non-ISO header file unistd.h in the runtime headers.

Index: runtime/mercury_trace_base.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_trace_base.c,v
retrieving revision 1.49
diff -u -u -r1.49 mercury_trace_base.c
--- runtime/mercury_trace_base.c	6 Nov 2002 02:02:30 -0000	1.49
+++ runtime/mercury_trace_base.c	7 Nov 2002 15:44:39 -0000
@@ -38,8 +38,7 @@
   #include <sys/wait.h>		/* for the wait system call */
 #endif
 
-MR_bool		MR_have_mdb_window = MR_FALSE;
-pid_t		MR_mdb_window_pid = 0;
+void		(*MR_trace_shutdown)(void) = NULL;
 
 MR_bool		MR_trace_enabled = MR_FALSE;
 MR_Unsigned	MR_trace_call_seqno = 0;
@@ -186,23 +185,12 @@
 	}
 #endif
 
-#if defined(MR_HAVE_KILL) && defined(MR_HAVE_WAIT) && defined(SIGTERM)
 	/*
 	** If mdb started a window, make sure it dies now.
 	*/
-	if (MR_have_mdb_window) {
-		int status;
-		status = kill(MR_mdb_window_pid, SIGTERM);
-		if (status != -1) {
-			do {
-				status = wait(NULL);
-				if (status == -1 && errno != EINTR) {
-					break;
-				}
-			} while (status != MR_mdb_window_pid);
-		}
+	if (MR_trace_shutdown != NULL) {
+		(*MR_trace_shutdown)();
 	}
-#endif
 }
 
 void
Index: runtime/mercury_trace_base.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_trace_base.h,v
retrieving revision 1.30
diff -u -u -r1.30 mercury_trace_base.h
--- runtime/mercury_trace_base.h	6 Nov 2002 02:02:30 -0000	1.30
+++ runtime/mercury_trace_base.h	7 Nov 2002 16:13:40 -0000
@@ -19,9 +19,6 @@
 #include "mercury_stack_layout.h"
 #include "mercury_std.h"
 #include "mercury_tabling.h"	/* for MR_TableNode */
-#ifdef MR_HAVE_UNISTD_H
-  #include <unistd.h>           /* for the write system call and pid_t */
-#endif
 
 /*
 ** This enum should EXACTLY match the definition of the `trace_port_type'
@@ -99,13 +96,9 @@
 extern	void	MR_trace_final(void);
 
 /*
-** MR_have_mdb_window and MR_mdb_window_pid are set by
-** mercury_trace_internal.c after the xterm window for
-** mdb has been spawned. The window process is killed by 
-** MR_trace_final().
+** Kill any windows created by mdb.
 */
-extern	MR_bool	MR_have_mdb_window;
-extern	pid_t	MR_mdb_window_pid;
+extern	void	(*MR_trace_shutdown)(void);
 
 /*
 ** The globals that define the interface between the tracing subsystem
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.147
diff -u -u -r1.147 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	6 Nov 2002 02:02:38 -0000	1.147
+++ trace/mercury_trace_internal.c	8 Nov 2002 03:13:46 -0000
@@ -135,6 +135,19 @@
 static	MR_bool			MR_echo_commands = MR_FALSE;
 
 /*
+** MR_have_mdb_window and MR_mdb_window_pid are set by
+** mercury_trace_internal.c after the xterm window for
+** mdb has been spawned. The window process is killed by
+** MR_trace_internal_kill_mdb_window(), which is called by
+** MR_trace_final() through the MR_trace_shutdown() pointer.
+** This indirect call is used to avoid references to the
+** non-ISO header file <unistd.h> (for pid_t) in the runtime
+** headers.
+*/
+static	MR_bool		MR_have_mdb_window = MR_FALSE;
+static	pid_t		MR_mdb_window_pid = 0;
+
+/*
 ** The details of the source server, if any.
 */
 
@@ -259,6 +272,7 @@
 
 static	void	MR_trace_internal_ensure_init(void);
 static	MR_bool	MR_trace_internal_create_mdb_window(void);
+static	void	MR_trace_internal_kill_mdb_window(void);
 static	void	MR_trace_internal_init_from_env(void);
 static	void	MR_trace_internal_init_from_local(void);
 static	void	MR_trace_internal_init_from_home_dir(void);
@@ -738,7 +752,6 @@
 	int master_fd = -1;
 	int slave_fd = -1;
 	char *slave_name;
-	pid_t child_pid;
 #if defined(MR_HAVE_TERMIOS_H) && defined(MR_HAVE_TCGETATTR) && \
 		defined(MR_HAVE_TCSETATTR) && defined(ECHO) && defined(TCSADRAIN)
 	struct termios termio;
@@ -799,13 +812,13 @@
 	tcsetattr(slave_fd, TCSADRAIN, &termio);
 #endif
 
-	child_pid = fork();
-	if (child_pid == -1) {
+	MR_mdb_window_pid = fork();
+	if (MR_mdb_window_pid == -1) {
 		MR_mdb_perror("fork() for mdb window failed"); 
 		close(master_fd);
 		close(slave_fd);
 		return MR_FALSE;
-	} else if (child_pid == 0) {
+	} else if (MR_mdb_window_pid == 0) {
 		/*
 		** Child - exec() the xterm.
 		*/
@@ -853,6 +866,7 @@
 		int out_fd = -1;
 
 		MR_mdb_in = MR_mdb_out = MR_mdb_err = NULL;
+		MR_have_mdb_window = MR_TRUE;
 
 		close(master_fd);
 
@@ -927,20 +941,11 @@
 		}
 
 		MR_have_mdb_window = MR_TRUE;
-		MR_mdb_window_pid = child_pid;
+		MR_trace_shutdown = MR_trace_internal_kill_mdb_window;
 		return MR_TRUE;
 
 parent_error:
-#if defined(MR_HAVE_KILL) && defined(SIGTERM) && defined(MR_HAVE_WAIT)
-		if (kill(child_pid, SIGTERM) != -1) {
-			do {
-				wait_status = wait(NULL);
-				if (wait_status == -1 && errno != EINTR) {
-					break;	
-				}
-			} while (wait_status != child_pid);
-		}
-#endif
+		MR_trace_internal_kill_mdb_window();
 		if (MR_mdb_in) fclose(MR_mdb_in);
 		if (MR_mdb_out) fclose(MR_mdb_out);
 		if (MR_mdb_err) fclose(MR_mdb_err);
@@ -956,6 +961,25 @@
 		"Sorry, `mdb --window' not supported on this platform.\n");
 	return MR_FALSE;
 #endif /* !MR_HAVE_OPEN, etc. */
+}
+
+static void
+MR_trace_internal_kill_mdb_window(void)
+{
+#if defined(MR_HAVE_KILL) && defined(MR_HAVE_WAIT) && defined(SIGTERM)
+	if (MR_have_mdb_window) {
+		int status;
+		status = kill(MR_mdb_window_pid, SIGTERM);
+		if (status != -1) {
+			do {
+				status = wait(NULL);
+				if (status == -1 && errno != EINTR) {
+					break;
+				}
+			} while (status != MR_mdb_window_pid);
+		}
+	}
+#endif
 }
 
 static void
--------------------------------------------------------------------------
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