]> cygwin.com Git - cygwin-apps/setup.git/commitdiff
Refactor ::run() so it's more generally useful
authorJon TURNEY <jon.turney@dronecode.org.uk>
Fri, 8 Feb 2013 15:38:50 +0000 (15:38 +0000)
committerJon TURNEY <jon.turney@dronecode.org.uk>
Fri, 8 Feb 2013 15:38:50 +0000 (15:38 +0000)
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.

ChangeLog
script.cc
script.h

index d2c29bc3beeb42eae6230ed5413dced741b70ef1..189526311bf1e15b4b37db53ce9136eeadac666c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+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.
+
 2013-01-17  Jon TURNEY  <jon.turney@dronecode.org.uk>
 
        * configure.in: Require automake 1.12.
index 419cebc46fa435a9e21f491489b59a9075e3c0ba..2f8e286e3db697946a5acaef611e3fbd67fe1ebf 100644 (file)
--- 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;
 
index 144fd71518c0125467315ddc81840aea1d113ad5..abdd43e45bdd32b866ac8ae007b0317e7fa45993 100644 (file)
--- 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; } ;
This page took 0.044569 seconds and 5 git commands to generate.