From: Jon TURNEY Date: Fri, 8 Feb 2013 15:38:50 +0000 (+0000) Subject: Refactor ::run() so it's more generally useful X-Git-Tag: release_2.869~87 X-Git-Url: https://cygwin.com/git/?a=commitdiff_plain;h=f2952a6cd89d6feed89d7d237823cb9572c901e4;p=cygwin-apps%2Fsetup.git Refactor ::run() so it's more generally useful 2013-02-01 Jon TURNEY * 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. --- diff --git a/ChangeLog b/ChangeLog index d2c29bc3..18952631 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2013-02-01 Jon TURNEY + + * 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 * configure.in: Require automake 1.12. diff --git a/script.cc b/script.cc index 419cebc4..2f8e286e 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 144fd715..abdd43e4 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; } ;