This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH setup 1/2] Refactor ::run() so it's more generally useful


Move all the logging of the command it runs in
Move the formatting of the command line used for postinstall script running out

2013-02-01  Jon TURNEY  <jon.turney@dronecode.org.uk>

	* script.cc (::run, Script::run): Move the formatting of the command
	line used for postinstall script running out to Script::run. Move the
	logging of the command and it's output into ::run.
	* script.h: Add ::run() prototype.

Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
---
 script.cc |   33 +++++++++++++++++++--------------
 script.h  |    7 +++++--
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/script.cc b/script.cc
index 6663e8c..4bf0f09 100644
--- a/script.cc
+++ b/script.cc
@@ -196,10 +196,10 @@ OutputLog::out_to(std::ostream &out)
   SetFilePointer(_handle, 0, NULL, FILE_END);
 }
 
-static int
-run (const char *sh, const char *args, const char *file, OutputLog &file_out)
+int
+run (const char *cmdline)
 {
-  char cmdline[MAX_PATH];
+
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
   DWORD flags = CREATE_NEW_CONSOLE;
@@ -207,7 +207,11 @@ run (const char *sh, const char *args, const char *file, OutputLog &file_out)
   BOOL inheritHandles = FALSE;
   BOOL exitCodeValid = FALSE;
 
-  sprintf (cmdline, "%s %s \"%s\"", sh, args, file);
+  log(LOG_PLAIN) << "running: " << cmdline << endLog;
+
+  char tmp_pat[] = "/var/log/setup.log.runXXXXXXX";
+  OutputLog file_out = std::string (mktemp (tmp_pat));
+
   memset (&pi, 0, sizeof (pi));
   memset (&si, 0, sizeof (si));
   si.cb = sizeof (si);
@@ -226,7 +230,7 @@ run (const char *sh, const char *args, const char *file, OutputLog &file_out)
       flags = CREATE_NO_WINDOW;  // Note: this is ignored on Win9x
     }
 
-  BOOL createSucceeded = CreateProcess (0, cmdline, 0, 0, inheritHandles,
+  BOOL createSucceeded = CreateProcess (0, (char *)cmdline, 0, 0, inheritHandles,
 					flags, 0, get_root_dir ().c_str(),
 					&si, &pi);
 
@@ -237,6 +241,10 @@ run (const char *sh, const char *args, const char *file, OutputLog &file_out)
     }
   CloseHandle(pi.hProcess);
   CloseHandle(pi.hThread);
+
+  if (!file_out.isEmpty ())
+    log(LOG_BABBLE) << file_out << endLog;
+
   if (exitCodeValid)
     return exitCode;
   return -GetLastError();
@@ -268,24 +276,21 @@ Script::run() const
     }
 
   int retval;
-  char tmp_pat[] = "/var/log/setup.log.postinstallXXXXXXX";
-  OutputLog file_out = std::string (mktemp (tmp_pat));
+  char cmdline[MAX_PATH];
+
   if (sh.size() && stricmp (extension(), ".sh") == 0)
     {
-      log(LOG_PLAIN) << "running: " << sh << " --norc --noprofile \"" << scriptName << "\"" << endLog;
-      retval = ::run (sh.c_str(), "--norc --noprofile", scriptName.c_str(), file_out);
+      sprintf (cmdline, "%s %s \"%s\"", sh.c_str(), "--norc --noprofile", scriptName.c_str());
+      retval = ::run (cmdline);
     }
   else if (cmd && stricmp (extension(), ".bat") == 0)
     {
-      log(LOG_PLAIN) << "running: " << cmd << " /c \"" << windowsName << "\"" << endLog;
-      retval = ::run (cmd, "/c", windowsName.c_str(), file_out);
+      sprintf (cmdline, "%s %s \"%s\"", cmd, "/c", windowsName.c_str());
+      retval = ::run (cmdline);
     }
   else
     return -ERROR_INVALID_DATA;
 
-  if (!file_out.isEmpty ())
-    log(LOG_BABBLE) << file_out << endLog;
-
   if (retval)
     log(LOG_PLAIN) << "abnormal exit: exit code=" << retval << endLog;
 
diff --git a/script.h b/script.h
index 144fd71..abdd43e 100644
--- a/script.h
+++ b/script.h
@@ -14,7 +14,7 @@
  */
 #ifndef SETUP_SCRIPT_H
 #define SETUP_SCRIPT_H
-   
+
 /* Initialisation stuff for run_script: sh, cmd, CYGWINROOT and PATH */
 void init_run_script ();
 
@@ -24,6 +24,9 @@ int try_run_script (const std::string& dir,
                     const std::string& fname,
                     const std::string& ext);
 
+/* Run a command and capture it's output to the log */
+int run (const char *cmdline);
+
 class Script {
 public:
   static bool isAScript (const std::string& file);
@@ -32,7 +35,7 @@ public:
   std::string fullName() const;
 /* Run the script.  If its suffix is .sh, and we have a Bourne shell, execute
    it using sh.  Otherwise, if the suffix is .bat, execute using cmd.exe (NT)
-   or command.com (9x).  Returns the exit status of the process, or 
+   or command.com (9x).  Returns the exit status of the process, or
    negative error if any.  */
   int run() const;
   bool operator == (const Script s) { return s.scriptName == scriptName; } ;
-- 
1.7.9


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]