Browse Source

RunProcess now captures stderr

ModuleSystem
Macoy Madson 10 months ago
parent
commit
89e8559ad4
3 changed files with 29 additions and 1 deletions
  1. +23
    -1
      src/RunProcess.cpp
  2. +1
    -0
      src/Writer.cpp
  3. +5
    -0
      test/Hello.cake

+ 23
- 1
src/RunProcess.cpp View File

@ -22,7 +22,8 @@ void systemExecute()
// PrintBuffer(arg0, "--version");
// If not null terminated, the call will fail
char* arguments[] = {fileToExec, strdup("--version"), nullptr};
// char* arguments[] = {fileToExec, strdup("--version"), nullptr};
char* arguments[] = {fileToExec, strdup("-c"), strdup("test/Hello.cake.cpp"), nullptr};
printf("Running %s\n", fileToExec);
execvp(fileToExec, arguments);
perror("RunProcess execvp() error: ");
@ -30,6 +31,11 @@ void systemExecute()
#endif
}
void subprocessReceiveStdOut(const char* processOutputBuffer)
{
printf("From process: %s", processOutputBuffer);
}
int main()
{
#ifdef UNIX
@ -51,6 +57,12 @@ int main()
// Child
else if (pid == 0)
{
if (dup2(pipeFileDescriptors[PipeWrite], STDOUT_FILENO) == -1 ||
dup2(pipeFileDescriptors[PipeWrite], STDERR_FILENO) == -1)
{
perror("RunProcess: ");
return 1;
}
// Only write
close(pipeFileDescriptors[PipeRead]);
systemExecute();
@ -60,9 +72,19 @@ int main()
// Parent
else
{
if (dup2(pipeFileDescriptors[PipeRead], STDIN_FILENO) == -1)
{
perror("RunProcess: ");
return 1;
}
// Only read
close(pipeFileDescriptors[PipeWrite]);
printf("Created child process %d\n", pid);
char processOutputBuffer[1024];
while (fgets(processOutputBuffer, sizeof(processOutputBuffer), stdin))
{
subprocessReceiveStdOut(processOutputBuffer);
}
int status;
waitpid(pid, &status, 0);
return status;


+ 1
- 0
src/Writer.cpp View File

@ -82,6 +82,7 @@ struct StringOutputState
FILE* fileOut;
};
// TODO Have writer scan strings for \n?
static void Writer_Writef(StringOutputState& state, const char* format, ...)
{
va_list args;


+ 5
- 0
test/Hello.cake View File

@ -0,0 +1,5 @@
(c-import "<stdio.h>")
(defun main(&return int)
(printf "Hello, world! From Cakelisp!\n")
(return 0))

Loading…
Cancel
Save