reference : http://wiki.codeblocks.org/index.php?title=Scripting_commands#IO_namespace (http://wiki.codeblocks.org/index.php?title=Scripting_commands#IO_namespace)
In a script plugin during the use of
local int_err = ::IO.Execute(cmd)
local str_out = ::IO.ExecuteAndGetOutput(cmd)
can not retrieve messages from any errors in 'cmd'
I also propose
local str_error = ::IO.ExecuteAndGetError (cmd)
Is it possible to make this fix ?
Index: src/sdk/scripting/bindings/sc_io.cpp
===================================================================
--- src/sdk/scripting/bindings/sc_io.cpp (révision 7820)
+++ src/sdk/scripting/bindings/sc_io.cpp (copie de travail)
@@ -188,6 +188,16 @@
return wxExecute(command, output, wxEXEC_NODISABLE);
}
+ wxString ExecuteAndGetError(const wxString& command)
+ {
+ if (!SecurityAllows(_T("Execute"), command))
+ return wxEmptyString;
+ wxArrayString output;
+ wxArrayString error;
+ wxExecute(command, output, error, wxEXEC_NODISABLE);
+ return GetStringFromArray(error, _T("\n"));
+ }
+
wxString ExecuteAndGetOutput(const wxString& command)
{
if (!SecurityAllows(_T("Execute"), command))
@@ -216,7 +226,10 @@
staticFunc(&IOLib::RemoveFile, "RemoveFile").
staticFunc(&IOLib::WriteFileContents, "WriteFileContents").
staticFunc(&IOLib::Execute, "Execute").
+ staticFunc(&IOLib::ExecuteAndGetError, "ExecuteAndGetError").
staticFunc(&IOLib::ExecuteAndGetOutput, "ExecuteAndGetOutput").
+
+
#endif // NO_INSECURE_SCRIPTS
staticFunc(&IOLib::GetCwd, "GetCwd").
@MortenMacFly
I made many tests on this version with r7822
wxString ExecuteAndGetOutputAndError(const wxString& command)
{
if (!SecurityAllows(_T("Execute"), command))
return wxEmptyString;
wxArrayString output;
wxArrayString error;
wxString str_out;
wxExecute(command, output, error, wxEXEC_NODISABLE);
// first 'stderr'
if (!error.IsEmpty())
str_out += _T("stderr :\n") + GetStringFromArray(error, _T("\n"));
if (!output.IsEmpty())
str_out += _T("stdout :\n") + GetStringFromArray(output, _T("\n"));
return str_out;
}
especially for simultaneously 'sdtout' and 'stderr' (the latter with warnings and errors)
I put 'stderr' at the start to test it first.
what do you think?
@MortenMacFly
BTW: I just looked at you patch: I still don't believe you need an additional function. I thought you'd do it by extending the existing one?!
I had not read the correction message!, but of course this is possible.
I have considered your suggestion.
In my case 'stderr' can give me an ERROR or just a WARNING without error.
But it is the reading 'stderr' can I know!
If it's only a warning, I also need to read 'stdout'.
If it is an error, 'sdtout' will be empty.
This is why I commented on the 'else'.
In this version, I can either get or not the errors / warnings
With the command, I can always choose to use 'stdout or not
wxString ExecuteAndGetOutputAndError(const wxString& command, bool prepend_error = false)
{
if (!SecurityAllows(_T("Execute"), command))
return wxEmptyString;
wxArrayString output;
wxArrayString error;
wxString str_out;
wxExecute(command, output, error, wxEXEC_NODISABLE);
// first 'stderr'
if (prepend_error && !error.IsEmpty())
str_out += _T("stderr :\n") + GetStringFromArray(error, _T("\n"));
//else
if (!output.IsEmpty())
str_out += _T("stdout :\n") + GetStringFromArray(output, _T("\n"));
return str_out;
}
This is why I commented on the 'else'.
I guess there was a slight mis-understanding. What I mean was something like this:
wxString ExecuteAndGetOutputAndError(const wxString& command, bool prepend_error = true)
{
if (!SecurityAllows(_T("Execute"), command))
return wxEmptyString;
wxArrayString output;
wxArrayString error;
wxExecute(command, output, error, wxEXEC_NODISABLE);
wxString str_out;
if (prepend_error && !error.IsEmpty())
str_out += GetStringFromArray(error, _T("\n"));
if (!output.IsEmpty())
str_out += GetStringFromArray(output, _T("\n"));
if (!prepend_error && !error.IsEmpty())
str_out += GetStringFromArray(error, _T("\n"));
return str_out;
}
(UNTESTED!)
As you see, I just wanted to give the user an option to append or prepend errors - depending what he believes is the best option to merge stdout and stderr. As errors are generally more important (and if not the user will use ExecuteAndGetOutput anyways) the default setting should be to pre-pend. Also, I skipped _T("stderr :\n") and _T("stdout :\n") as this screws the real output.
After several tries, I think the best solution would be:
wxString ExecuteAndGetOutputAndError(const wxString& command, bool with_error = false}
{
if (!SecurityAllows(_T("Execute"), command))
return wxEmptyString;
wxArrayString output;
wxArrayString error;
wxString str_out;
wxExecute(command, output, error, wxEXEC_NODISABLE);
if (with_error && !error.IsEmpty() )
str_out += GetStringFromArray(error, _T("\n"));
if (!output.IsEmpty())
str_out += GetStringFromArray(output, _T("\n"));
return str_out;
}
well it looks like 'ExecuteAndGetOutput (const wxString & command)' plus 'stderr' option.
In 'command' you can always redirect 'stdout to a file and get only' stderr 'on the function.
[attachment deleted by admin]
yes, I tested with 'wxUSE_STREAMS = 1' for 'wxmsw28u_gcc_custom.dll'.
For an external command 'mytool.exe 1> foo.txt',
for an internal command 'cmd / C dir 1> foo.txt'
and I get errors with 'ExecuteAndGetOutputAndError (command, true)'
or the opposite '... 2> err.txt ' and ' ExecuteAndGetOutputAndError (command) '
Can be simplified by 'ExecuteAndGetOutput (wxString, bool = false)'
without affecting existing programs :
wxString ExecuteAndGetOutput(const wxString& command, bool with_error = false
{
if (!SecurityAllows(_T("Execute"), command))
return wxEmptyString;
wxArrayString output;
wxArrayString error;
wxString str_out;
wxExecute(command, output, error, wxEXEC_NODISABLE);
if (with_error && !error.IsEmpty() )
str_out += GetStringFromArray(error, _T("\n"));
if (!output.IsEmpty())
str_out += GetStringFromArray(output, _T("\n"));
return str_out;
}