Subject: [PATCH] ExecWait command

From: Tony Finch <[email-masked]>
Date: Fri, 21 Jun 2002 12:07:26 +0100

The fvwm manual notes the potential for race conditions when using the
Exec command, and I stumbled across this recently when trying to put more
of my X startup under fvwm's control (the xrdb command didn't complete
before xclock started). So I added an ExecWait command for running
programs synchronously. It's OK as it is for short-lived programs, but
for use with longer-lived things it needs support for the EscapeFunc
mechanism.

--- ChangeLog	2002/06/21 05:35:43	1.1847
+++ ChangeLog	2002/06/21 10:16:03
@@ -1,3 +1,13 @@
+2002-06-21  Tony Finch <[email-masked]>
+
+	* fvwm/builtins.c:
+	* fvwm/commands.h:
+	* fvwm/functable.c:
+	* fvwm/fvwm.1:
+	Add an ExecWait command for running a program in a blocking
+	fashion, in order to avoid race conditions of the kind mentioned
+	in the manual under the Exec command.
+
 2002-06-21  olicha  <[email-masked]>
 
 	* fvwm/fvwm.1: 
--- fvwm/builtins.c	2002/06/19 14:11:27	1.359
+++ fvwm/builtins.c	2002/06/21 10:16:03
@@ -1816,6 +1816,28 @@
 	return;
 }
 
+void CMD_ExecWait(F_CMD_ARGS)
+{
+  pid_t pid;
+  char *cmd;
+
+  switch (pid = fork())
+  {
+  case -1:
+    fvwm_msg(ERR,"ExecWait","fork failed (%s)",strerror(errno));
+    break;
+  case 0:
+    cmd = safestrdup(action);
+    execl(exec_shell_name, exec_shell_name, "-c", cmd, NULL);
+    fvwm_msg(ERR,"ExecWait","execl failed (%s)",strerror(errno));
+    exit(100);
+  default:
+    waitpid(pid, NULL, 0);
+    break;
+  }
+  return;
+}
+
 void CMD_Refresh(F_CMD_ARGS)
 {
 	refresh_window(Scr.Root, True);
--- fvwm/commands.h	2002/06/19 14:11:27	1.21
+++ fvwm/commands.h	2002/06/21 10:16:03
@@ -75,6 +75,7 @@
 	F_EWMH_NUMBER_OF_DESKTOPS,
 	F_EXEC,
 	F_EXEC_SETUP,
+	F_EXEC_WAIT,
 	F_FAKE_CLICK,
 	F_FUNCTION,
 	F_GLOBAL_OPTS,
@@ -265,6 +266,7 @@
 void CMD_EwmhNumberOfDesktops(F_CMD_ARGS);
 void CMD_Exec(F_CMD_ARGS);
 void CMD_ExecUseShell(F_CMD_ARGS);
+void CMD_ExecWait(F_CMD_ARGS);
 void CMD_FakeClick(F_CMD_ARGS);
 void CMD_FlipFocus(F_CMD_ARGS);
 void CMD_Focus(F_CMD_ARGS);
--- fvwm/functable.c	2002/06/19 14:12:45	1.1
+++ fvwm/functable.c	2002/06/21 10:16:03
@@ -125,6 +125,7 @@
 		  F_EWMH_NUMBER_OF_DESKTOPS, 0),
 	CMD_ENTRY("exec", CMD_Exec, F_EXEC, 0),
 	CMD_ENTRY("execuseshell", CMD_ExecUseShell, F_EXEC_SETUP, 0),
+	CMD_ENTRY("execwait", CMD_ExecWait, F_EXEC_WAIT, 0),
 	CMD_ENTRY("fakeclick", CMD_FakeClick, F_FAKE_CLICK, 0),
 	CMD_ENTRY("flipfocus", CMD_FlipFocus, F_FLIP_FOCUS, FUNC_NEEDS_WINDOW),
 	CMD_ENTRY("focus", CMD_Focus, F_FOCUS, FUNC_NEEDS_WINDOW),
--- fvwm/fvwm.1	2002/06/21 05:35:44	1.21
+++ fvwm/fvwm.1	2002/06/21 10:16:03
@@ -8145,6 +8145,17 @@
 Read /tmp/file
 .EE
 do not work reliably.
+Use the
+.B ExecWait
+command instead.
+
+.TP
+.BI "ExecWait " command
+Executes
+.IR command
+and waits for it to complete.
+Do not use this for programs that are going to run for any significant
+length of time because fvwm is unresponsive while waiting.
 
 .TP
 .BI "ExecUseShell [" shell "]"

Tony.
-- 
f.a.n.finch <[email-masked]> http://dotat.at/
SHANNON ROCKALL: SOUTHERLY VEERING WESTERLY 4 OR 5. RAIN OR SHOWERS. MODERATE
OR GOOD.
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Dan Espen <[email-masked]>
Date: Fri, 21 Jun 2002 07:17:36 -0400

> for use with longer-lived things it needs support for the EscapeFunc

Piperead is synchronous.

-- 
Dan Espen                           E-mail: [email-masked]
444 Hoes Lane  Room RRC 1C-214      Phone: (732) 699-5570
Piscataway, NJ 08854
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Mikhael Goikhman <[email-masked]>
Date: Fri, 21 Jun 2002 11:20:56 +0000

> for use with longer-lived things it needs support for the EscapeFunc

I am not sure. PipeRead already does exactly this and even more.
"ExecWait something" is equivalant to "PipeRead `something >/dev/null'"

The name ExecWait may be confused with a combination of 2 commands (that
are often used for commands launching windows):

  Exec something
  Wait somepattern

Maybe BlockingExec is a better name.

Regards,
Mikhael.

---

From: Tony Finch <[email-masked]>
Date: Fri, 21 Jun 2002 14:49:18 +0100

> "ExecWait something" is equivalant to "PipeRead `something >/dev/null'"

Funky. How about this as an alternative then?

--- ChangeLog   2002/06/21 05:35:43     1.1847
+++ ChangeLog   2002/06/21 13:48:24
@@ -1,3 +1,8 @@
+2002-06-21  Tony Finch <[email-masked]>
+
+	* fvwm/fvwm.1:
+	Note that PipeRead can be used as a blocking Exec.
+
 2002-06-21  olicha  <[email-masked]>
 
 	* fvwm/fvwm.1: 
--- fvwm.1      2002/06/21 05:35:44     1.21
+++ fvwm.1      2002/06/21 13:46:01
@@ -8145,6 +8145,13 @@
 Read /tmp/file
 .EE
 do not work reliably.
+Instead, you can use a command like
+.EX
+PipeRead "program args 1>&2"
+.EE
+to run a program and wait for it to finish.
+The redirection causes any output from the program to go to stderr
+instead of being read as a sequence of commands by fvwm.
 
 .TP

If you do decide to incorporate ExecWait I don't mind what name you give it.

Tony.
-- 
f.a.n.finch <[email-masked]> http://dotat.at/
NORTH FITZROY SOLE: SOUTH VEERING SOUTHWEST 4 OR 5, OCCASIONALLY 6 AT FIRST.
RAIN OR SHOWERS. MODERATE OR POOR BECOMING GOOD.
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Dan Espen <[email-masked]>
Date: Fri, 21 Jun 2002 10:35:06 -0400

> Funky. How about this as an alternative then?

Something along those lines has been committed.
Thanks.