Enhance redirection features of the OS module.
* Add a function to redirect the output of a command to a file.
* Add a function to capture redirected output from a local command.
(This function replaces a now-unused `run_local` which did no
redirection.)
* Consolidate cmd.exe and Unix shell redirection syntax.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/d7410143
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/d7410143
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/d7410143
Branch: refs/heads/chaz_redirection
Commit: d7410143e04a1ed6e2d2f6612540a9b6cad2b94f
Parents: 2c37b95
Author: Marvin Humphrey <[email protected]>
Authored: Tue Nov 27 16:51:52 2012 -0800
Committer: Marvin Humphrey <[email protected]>
Committed: Wed Nov 28 16:42:19 2012 -0800
----------------------------------------------------------------------
charmonizer/src/Charmonizer/Core/OperatingSystem.c | 47 +++++---------
charmonizer/src/Charmonizer/Core/OperatingSystem.h | 18 ++++--
2 files changed, 29 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/d7410143/charmonizer/src/Charmonizer/Core/OperatingSystem.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Core/OperatingSystem.c
b/charmonizer/src/Charmonizer/Core/OperatingSystem.c
index fb9ad9b..c897285 100644
--- a/charmonizer/src/Charmonizer/Core/OperatingSystem.c
+++ b/charmonizer/src/Charmonizer/Core/OperatingSystem.c
@@ -115,54 +115,41 @@ chaz_OS_remove(const char *name) {
}
int
-chaz_OS_run_local(const char *arg1, ...) {
- va_list args;
- size_t len = strlen(chaz_OS.local_command_start) + strlen(arg1);
- char *command = (char*)malloc(len + 1);
- int retval;
- char *arg;
-
- /* Append all supplied texts. */
- sprintf(command, "%s%s", chaz_OS.local_command_start, arg1);
- va_start(args, arg1);
- while (NULL != (arg = va_arg(args, char*))) {
- len += strlen(arg);
- command = (char*)realloc(command, len + 1);
- strcat(command, arg);
- }
- va_end(args);
-
- /* Run the command. */
- retval = system(command);
- free(command);
+chaz_OS_run_local_redirected(const char *command, const char *path) {
+ size_t size = strlen(command) + strlen(chaz_OS.local_command_start) + 20;
+ char *local_command = (char*)malloc(size);
+ int retval;
+ sprintf(local_command, "%s%s", chaz_OS.local_command_start, command);
+ retval = chaz_OS_run_redirected(local_command, path);
+ free(local_command);
return retval;
}
int
chaz_OS_run_quietly(const char *command) {
+ return chaz_OS_run_redirected(command, chaz_OS.dev_null);
+}
+
+int
+chaz_OS_run_redirected(const char *command, const char *path) {
int retval = 1;
char *quiet_command = NULL;
- if (chaz_OS.shell_type == CHAZ_OS_POSIX) {
+ if (chaz_OS.shell_type == CHAZ_OS_POSIX
+ || chaz_OS.shell_type == CHAZ_OS_CMD_EXE
+ ) {
char pattern[] = "%s > %s 2>&1";
size_t size = sizeof(pattern)
+ strlen(command)
- + strlen(chaz_OS.dev_null)
+ + strlen(path)
+ 10;
quiet_command = (char*)malloc(size);
- sprintf(quiet_command, pattern, command, chaz_OS.dev_null);
- }
- else if (chaz_OS.shell_type == CHAZ_OS_CMD_EXE) {
- char pattern[] = "%s > NUL 2> NUL";
- size_t size = sizeof(pattern) + strlen(command) + 10;
- quiet_command = (char*)malloc(size);
- sprintf(quiet_command, pattern, command);
+ sprintf(quiet_command, pattern, command, path);
}
else {
chaz_Util_die("Don't know the shell type");
}
retval = system(quiet_command);
free(quiet_command);
-
return retval;
}
http://git-wip-us.apache.org/repos/asf/lucy/blob/d7410143/charmonizer/src/Charmonizer/Core/OperatingSystem.h
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Core/OperatingSystem.h
b/charmonizer/src/Charmonizer/Core/OperatingSystem.h
index 8e5a412..fe936d6 100644
--- a/charmonizer/src/Charmonizer/Core/OperatingSystem.h
+++ b/charmonizer/src/Charmonizer/Core/OperatingSystem.h
@@ -31,12 +31,6 @@ extern "C" {
int
chaz_OS_remove(const char *name);
-/* Concatenate all arguments in a NULL-terminated list into a single command
- * string, prepend the appropriate prefix, and invoke via system().
- */
-int
-chaz_OS_run_local(const char *arg1, ...);
-
/* Invoke a command and attempt to suppress output from both stdout and stderr
* (as if they had been sent to /dev/null). If it's not possible to run the
* command quietly, run it anyway.
@@ -44,6 +38,18 @@ chaz_OS_run_local(const char *arg1, ...);
int
chaz_OS_run_quietly(const char *command);
+/* Capture both stdout and stderr for a command to the supplied filepath.
+ */
+int
+chaz_OS_run_redirected(const char *command, const char *path);
+
+/* Run a command beginning with the name of an executable in the current
+ * working directory and capture both stdout and stderr to the supplied
+ * filepath.
+ */
+int
+chaz_OS_run_local_redirected(const char *command, const char *path);
+
/* Attempt to create a directory.
*/
void